Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/page_alloc.c |
| 3 | * |
| 4 | * Manages the free list, the system allocates free pages here. |
| 5 | * Note that kmalloc() lives in slab.c |
| 6 | * |
| 7 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| 8 | * Swap reorganised 29.12.95, Stephen Tweedie |
| 9 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| 10 | * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999 |
| 11 | * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999 |
| 12 | * Zone balancing, Kanoj Sarcar, SGI, Jan 2000 |
| 13 | * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002 |
| 14 | * (lots of bits borrowed from Ingo Molnar & Andrew Morton) |
| 15 | */ |
| 16 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/stddef.h> |
| 18 | #include <linux/mm.h> |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 19 | #include <linux/highmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/swap.h> |
| 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/pagemap.h> |
KOSAKI Motohiro | 10ed273 | 2008-03-04 14:28:32 -0800 | [diff] [blame] | 23 | #include <linux/jiffies.h> |
Yinghai Lu | edbe7d2 | 2010-08-25 13:39:16 -0700 | [diff] [blame] | 24 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/compiler.h> |
Randy Dunlap | 9f15833 | 2005-09-13 01:25:16 -0700 | [diff] [blame] | 26 | #include <linux/kernel.h> |
Andrey Ryabinin | b8c73fc | 2015-02-13 14:39:28 -0800 | [diff] [blame] | 27 | #include <linux/kasan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/suspend.h> |
| 30 | #include <linux/pagevec.h> |
| 31 | #include <linux/blkdev.h> |
| 32 | #include <linux/slab.h> |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 33 | #include <linux/ratelimit.h> |
David Rientjes | 5a3135c2 | 2007-10-16 23:25:53 -0700 | [diff] [blame] | 34 | #include <linux/oom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/topology.h> |
| 36 | #include <linux/sysctl.h> |
| 37 | #include <linux/cpu.h> |
| 38 | #include <linux/cpuset.h> |
Dave Hansen | bdc8cb9 | 2005-10-29 18:16:53 -0700 | [diff] [blame] | 39 | #include <linux/memory_hotplug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/nodemask.h> |
| 41 | #include <linux/vmalloc.h> |
KOSAKI Motohiro | a6cccdc | 2011-05-24 17:11:33 -0700 | [diff] [blame] | 42 | #include <linux/vmstat.h> |
Christoph Lameter | 4be38e3 | 2006-01-06 00:11:17 -0800 | [diff] [blame] | 43 | #include <linux/mempolicy.h> |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 44 | #include <linux/memremap.h> |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 45 | #include <linux/stop_machine.h> |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 46 | #include <linux/sort.h> |
| 47 | #include <linux/pfn.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 48 | #include <linux/backing-dev.h> |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 49 | #include <linux/fault-inject.h> |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 50 | #include <linux/page-isolation.h> |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 51 | #include <linux/page_ext.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 52 | #include <linux/debugobjects.h> |
Catalin Marinas | dbb1f81 | 2009-06-11 13:23:19 +0100 | [diff] [blame] | 53 | #include <linux/kmemleak.h> |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 54 | #include <linux/compaction.h> |
Mel Gorman | 0d3d062 | 2009-09-21 17:02:44 -0700 | [diff] [blame] | 55 | #include <trace/events/kmem.h> |
Michal Hocko | d379f01 | 2017-02-22 15:42:00 -0800 | [diff] [blame] | 56 | #include <trace/events/oom.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 57 | #include <linux/prefetch.h> |
Lisa Du | 6e543d5 | 2013-09-11 14:22:36 -0700 | [diff] [blame] | 58 | #include <linux/mm_inline.h> |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 59 | #include <linux/migrate.h> |
David Rientjes | 949f7ec | 2013-04-29 15:07:48 -0700 | [diff] [blame] | 60 | #include <linux/hugetlb.h> |
Clark Williams | 8bd75c7 | 2013-02-07 09:47:07 -0600 | [diff] [blame] | 61 | #include <linux/sched/rt.h> |
Ingo Molnar | 5b3cc15 | 2017-02-02 20:43:54 +0100 | [diff] [blame] | 62 | #include <linux/sched/mm.h> |
Joonsoo Kim | 48c96a3 | 2014-12-12 16:56:01 -0800 | [diff] [blame] | 63 | #include <linux/page_owner.h> |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 64 | #include <linux/kthread.h> |
Vladimir Davydov | 4949148 | 2016-07-26 15:24:24 -0700 | [diff] [blame] | 65 | #include <linux/memcontrol.h> |
Steven Rostedt (VMware) | 42c269c | 2017-03-03 16:15:39 -0500 | [diff] [blame] | 66 | #include <linux/ftrace.h> |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 67 | #include <linux/lockdep.h> |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 68 | #include <linux/nmi.h> |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 69 | #include <linux/psi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 71 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <asm/tlbflush.h> |
Andrew Morton | ac924c6 | 2006-05-15 09:43:59 -0700 | [diff] [blame] | 73 | #include <asm/div64.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include "internal.h" |
| 75 | |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 76 | /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ |
| 77 | static DEFINE_MUTEX(pcp_batch_high_lock); |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 78 | #define MIN_PERCPU_PAGELIST_FRACTION (8) |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 79 | |
Lee Schermerhorn | 7281201 | 2010-05-26 14:44:56 -0700 | [diff] [blame] | 80 | #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID |
| 81 | DEFINE_PER_CPU(int, numa_node); |
| 82 | EXPORT_PER_CPU_SYMBOL(numa_node); |
| 83 | #endif |
| 84 | |
Kemi Wang | 4518085 | 2017-11-15 17:38:22 -0800 | [diff] [blame] | 85 | DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key); |
| 86 | |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 87 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
| 88 | /* |
| 89 | * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly. |
| 90 | * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined. |
| 91 | * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem() |
| 92 | * defined in <linux/topology.h>. |
| 93 | */ |
| 94 | DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */ |
| 95 | EXPORT_PER_CPU_SYMBOL(_numa_mem_); |
Joonsoo Kim | ad2c814 | 2014-10-09 15:26:13 -0700 | [diff] [blame] | 96 | int _node_numa_mem_[MAX_NUMNODES]; |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 97 | #endif |
| 98 | |
Mel Gorman | bd233f5 | 2017-02-24 14:56:56 -0800 | [diff] [blame] | 99 | /* work_structs for global per-cpu drains */ |
| 100 | DEFINE_MUTEX(pcpu_drain_mutex); |
| 101 | DEFINE_PER_CPU(struct work_struct, pcpu_drain); |
| 102 | |
Emese Revfy | 38addce | 2016-06-20 20:41:19 +0200 | [diff] [blame] | 103 | #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY |
Kees Cook | 58bea41 | 2016-10-19 00:08:04 +0200 | [diff] [blame] | 104 | volatile unsigned long latent_entropy __latent_entropy; |
Emese Revfy | 38addce | 2016-06-20 20:41:19 +0200 | [diff] [blame] | 105 | EXPORT_SYMBOL(latent_entropy); |
| 106 | #endif |
| 107 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | /* |
Christoph Lameter | 1380891 | 2007-10-16 01:25:27 -0700 | [diff] [blame] | 109 | * Array of node states. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | */ |
Christoph Lameter | 1380891 | 2007-10-16 01:25:27 -0700 | [diff] [blame] | 111 | nodemask_t node_states[NR_NODE_STATES] __read_mostly = { |
| 112 | [N_POSSIBLE] = NODE_MASK_ALL, |
| 113 | [N_ONLINE] = { { [0] = 1UL } }, |
| 114 | #ifndef CONFIG_NUMA |
| 115 | [N_NORMAL_MEMORY] = { { [0] = 1UL } }, |
| 116 | #ifdef CONFIG_HIGHMEM |
| 117 | [N_HIGH_MEMORY] = { { [0] = 1UL } }, |
| 118 | #endif |
Lai Jiangshan | 20b2f52 | 2012-12-12 13:52:00 -0800 | [diff] [blame] | 119 | [N_MEMORY] = { { [0] = 1UL } }, |
Christoph Lameter | 1380891 | 2007-10-16 01:25:27 -0700 | [diff] [blame] | 120 | [N_CPU] = { { [0] = 1UL } }, |
| 121 | #endif /* NUMA */ |
| 122 | }; |
| 123 | EXPORT_SYMBOL(node_states); |
| 124 | |
Jiang Liu | c3d5f5f | 2013-07-03 15:03:14 -0700 | [diff] [blame] | 125 | /* Protect totalram_pages and zone->managed_pages */ |
| 126 | static DEFINE_SPINLOCK(managed_page_count_lock); |
| 127 | |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 128 | atomic_long_t _totalram_pages __read_mostly; |
| 129 | EXPORT_SYMBOL(_totalram_pages); |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 130 | unsigned long totalreserve_pages __read_mostly; |
Pintu Kumar | e48322a | 2014-12-18 16:17:15 -0800 | [diff] [blame] | 131 | unsigned long totalcma_pages __read_mostly; |
Johannes Weiner | ab8fabd | 2012-01-10 15:07:42 -0800 | [diff] [blame] | 132 | |
Hugh Dickins | 1b76b02 | 2012-05-11 01:00:07 -0700 | [diff] [blame] | 133 | int percpu_pagelist_fraction; |
Benjamin Herrenschmidt | dcce284 | 2009-06-18 13:24:12 +1000 | [diff] [blame] | 134 | gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
Vlastimil Babka | bb14c2c | 2015-09-08 15:01:25 -0700 | [diff] [blame] | 136 | /* |
| 137 | * A cached value of the page's pageblock's migratetype, used when the page is |
| 138 | * put on a pcplist. Used to avoid the pageblock migratetype lookup when |
| 139 | * freeing from pcplists in most cases, at the cost of possibly becoming stale. |
| 140 | * Also the migratetype set in the page does not necessarily match the pcplist |
| 141 | * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any |
| 142 | * other index - this ensures that it will be put on the correct CMA freelist. |
| 143 | */ |
| 144 | static inline int get_pcppage_migratetype(struct page *page) |
| 145 | { |
| 146 | return page->index; |
| 147 | } |
| 148 | |
| 149 | static inline void set_pcppage_migratetype(struct page *page, int migratetype) |
| 150 | { |
| 151 | page->index = migratetype; |
| 152 | } |
| 153 | |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 154 | #ifdef CONFIG_PM_SLEEP |
| 155 | /* |
| 156 | * The following functions are used by the suspend/hibernate code to temporarily |
| 157 | * change gfp_allowed_mask in order to avoid using I/O during memory allocations |
| 158 | * while devices are suspended. To avoid races with the suspend/hibernate code, |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 159 | * they should always be called with system_transition_mutex held |
| 160 | * (gfp_allowed_mask also should only be modified with system_transition_mutex |
| 161 | * held, unless the suspend/hibernate code is guaranteed not to run in parallel |
| 162 | * with that modification). |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 163 | */ |
Rafael J. Wysocki | c9e664f | 2010-12-03 22:57:45 +0100 | [diff] [blame] | 164 | |
| 165 | static gfp_t saved_gfp_mask; |
| 166 | |
| 167 | void pm_restore_gfp_mask(void) |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 168 | { |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 169 | WARN_ON(!mutex_is_locked(&system_transition_mutex)); |
Rafael J. Wysocki | c9e664f | 2010-12-03 22:57:45 +0100 | [diff] [blame] | 170 | if (saved_gfp_mask) { |
| 171 | gfp_allowed_mask = saved_gfp_mask; |
| 172 | saved_gfp_mask = 0; |
| 173 | } |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Rafael J. Wysocki | c9e664f | 2010-12-03 22:57:45 +0100 | [diff] [blame] | 176 | void pm_restrict_gfp_mask(void) |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 177 | { |
Pingfan Liu | 55f2503 | 2018-07-31 16:51:32 +0800 | [diff] [blame] | 178 | WARN_ON(!mutex_is_locked(&system_transition_mutex)); |
Rafael J. Wysocki | c9e664f | 2010-12-03 22:57:45 +0100 | [diff] [blame] | 179 | WARN_ON(saved_gfp_mask); |
| 180 | saved_gfp_mask = gfp_allowed_mask; |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 181 | gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS); |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 182 | } |
Mel Gorman | f90ac39 | 2012-01-10 15:07:15 -0800 | [diff] [blame] | 183 | |
| 184 | bool pm_suspended_storage(void) |
| 185 | { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 186 | if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
Mel Gorman | f90ac39 | 2012-01-10 15:07:15 -0800 | [diff] [blame] | 187 | return false; |
| 188 | return true; |
| 189 | } |
Rafael J. Wysocki | 452aa69 | 2010-03-05 13:42:13 -0800 | [diff] [blame] | 190 | #endif /* CONFIG_PM_SLEEP */ |
| 191 | |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 193 | unsigned int pageblock_order __read_mostly; |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 194 | #endif |
| 195 | |
Hugh Dickins | d98c7a0 | 2006-02-14 13:52:59 -0800 | [diff] [blame] | 196 | static void __free_pages_ok(struct page *page, unsigned int order); |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* |
| 199 | * results with 256, 32 in the lowmem_reserve sysctl: |
| 200 | * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high) |
| 201 | * 1G machine -> (16M dma, 784M normal, 224M high) |
| 202 | * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA |
| 203 | * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL |
Yaowei Bai | 84109e1 | 2015-02-12 15:00:22 -0800 | [diff] [blame] | 204 | * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA |
Andi Kleen | a2f1b42 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 205 | * |
| 206 | * TBD: should special case ZONE_DMA32 machines here - in those we normally |
| 207 | * don't need any ZONE_NORMAL reservation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | */ |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 209 | int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES] = { |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 210 | #ifdef CONFIG_ZONE_DMA |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 211 | [ZONE_DMA] = 256, |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 212 | #endif |
Christoph Lameter | fb0e794 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 213 | #ifdef CONFIG_ZONE_DMA32 |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 214 | [ZONE_DMA32] = 256, |
Christoph Lameter | fb0e794 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 215 | #endif |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 216 | [ZONE_NORMAL] = 32, |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 217 | #ifdef CONFIG_HIGHMEM |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 218 | [ZONE_HIGHMEM] = 0, |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 219 | #endif |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 220 | [ZONE_MOVABLE] = 0, |
Christoph Lameter | 2f1b624 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 221 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | EXPORT_SYMBOL(totalram_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 225 | static char * const zone_names[MAX_NR_ZONES] = { |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 226 | #ifdef CONFIG_ZONE_DMA |
Christoph Lameter | 2f1b624 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 227 | "DMA", |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 228 | #endif |
Christoph Lameter | fb0e794 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 229 | #ifdef CONFIG_ZONE_DMA32 |
Christoph Lameter | 2f1b624 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 230 | "DMA32", |
Christoph Lameter | fb0e794 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 231 | #endif |
Christoph Lameter | 2f1b624 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 232 | "Normal", |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 233 | #ifdef CONFIG_HIGHMEM |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 234 | "HighMem", |
Christoph Lameter | e53ef38 | 2006-09-25 23:31:14 -0700 | [diff] [blame] | 235 | #endif |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 236 | "Movable", |
Dan Williams | 033fbae | 2015-08-09 15:29:06 -0400 | [diff] [blame] | 237 | #ifdef CONFIG_ZONE_DEVICE |
| 238 | "Device", |
| 239 | #endif |
Christoph Lameter | 2f1b624 | 2006-09-25 23:31:13 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
Vlastimil Babka | 60f3035 | 2016-03-15 14:56:08 -0700 | [diff] [blame] | 242 | char * const migratetype_names[MIGRATE_TYPES] = { |
| 243 | "Unmovable", |
| 244 | "Movable", |
| 245 | "Reclaimable", |
| 246 | "HighAtomic", |
| 247 | #ifdef CONFIG_CMA |
| 248 | "CMA", |
| 249 | #endif |
| 250 | #ifdef CONFIG_MEMORY_ISOLATION |
| 251 | "Isolate", |
| 252 | #endif |
| 253 | }; |
| 254 | |
Kirill A. Shutemov | f1e6155 | 2015-11-06 16:29:50 -0800 | [diff] [blame] | 255 | compound_page_dtor * const compound_page_dtors[] = { |
| 256 | NULL, |
| 257 | free_compound_page, |
| 258 | #ifdef CONFIG_HUGETLB_PAGE |
| 259 | free_huge_page, |
| 260 | #endif |
Kirill A. Shutemov | 9a98225 | 2016-01-15 16:54:17 -0800 | [diff] [blame] | 261 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 262 | free_transhuge_page, |
| 263 | #endif |
Kirill A. Shutemov | f1e6155 | 2015-11-06 16:29:50 -0800 | [diff] [blame] | 264 | }; |
| 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | int min_free_kbytes = 1024; |
Han Pingtian | 42aa83c | 2014-01-23 15:53:28 -0800 | [diff] [blame] | 267 | int user_min_free_kbytes = -1; |
Johannes Weiner | 795ae7a | 2016-03-17 14:19:14 -0700 | [diff] [blame] | 268 | int watermark_scale_factor = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
David Rientjes | 7f16f91 | 2018-04-05 16:23:12 -0700 | [diff] [blame] | 270 | static unsigned long nr_kernel_pages __meminitdata; |
| 271 | static unsigned long nr_all_pages __meminitdata; |
| 272 | static unsigned long dma_reserve __meminitdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 274 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
David Rientjes | 7f16f91 | 2018-04-05 16:23:12 -0700 | [diff] [blame] | 275 | static unsigned long arch_zone_lowest_possible_pfn[MAX_NR_ZONES] __meminitdata; |
| 276 | static unsigned long arch_zone_highest_possible_pfn[MAX_NR_ZONES] __meminitdata; |
| 277 | static unsigned long required_kernelcore __initdata; |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 278 | static unsigned long required_kernelcore_percent __initdata; |
David Rientjes | 7f16f91 | 2018-04-05 16:23:12 -0700 | [diff] [blame] | 279 | static unsigned long required_movablecore __initdata; |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 280 | static unsigned long required_movablecore_percent __initdata; |
David Rientjes | 7f16f91 | 2018-04-05 16:23:12 -0700 | [diff] [blame] | 281 | static unsigned long zone_movable_pfn[MAX_NUMNODES] __meminitdata; |
| 282 | static bool mirrored_kernelcore __meminitdata; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 283 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 284 | /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ |
| 285 | int movable_zone; |
| 286 | EXPORT_SYMBOL(movable_zone); |
| 287 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 288 | |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 289 | #if MAX_NUMNODES > 1 |
| 290 | int nr_node_ids __read_mostly = MAX_NUMNODES; |
Christoph Lameter | 62bc62a | 2009-06-16 15:32:15 -0700 | [diff] [blame] | 291 | int nr_online_nodes __read_mostly = 1; |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 292 | EXPORT_SYMBOL(nr_node_ids); |
Christoph Lameter | 62bc62a | 2009-06-16 15:32:15 -0700 | [diff] [blame] | 293 | EXPORT_SYMBOL(nr_online_nodes); |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 294 | #endif |
| 295 | |
Mel Gorman | 9ef9acb | 2007-10-16 01:25:54 -0700 | [diff] [blame] | 296 | int page_group_by_mobility_disabled __read_mostly; |
| 297 | |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 298 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 299 | /* Returns true if the struct page for the pfn is uninitialised */ |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 300 | static inline bool __meminit early_page_uninitialised(unsigned long pfn) |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 301 | { |
Mel Gorman | ef70b6f | 2016-07-14 12:07:23 -0700 | [diff] [blame] | 302 | int nid = early_pfn_to_nid(pfn); |
| 303 | |
| 304 | if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn) |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 305 | return true; |
| 306 | |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | /* |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 311 | * Returns true when the remaining initialisation should be deferred until |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 312 | * later in the boot cycle when it can be parallelised. |
| 313 | */ |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 314 | static bool __meminit |
| 315 | defer_init(int nid, unsigned long pfn, unsigned long end_pfn) |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 316 | { |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 317 | static unsigned long prev_end_pfn, nr_initialised; |
| 318 | |
| 319 | /* |
| 320 | * prev_end_pfn static that contains the end of previous zone |
| 321 | * No need to protect because called very early in boot before smp_init. |
| 322 | */ |
| 323 | if (prev_end_pfn != end_pfn) { |
| 324 | prev_end_pfn = end_pfn; |
| 325 | nr_initialised = 0; |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 328 | /* Always populate low zones for address-constrained allocations */ |
| 329 | if (end_pfn < pgdat_end_pfn(NODE_DATA(nid))) |
| 330 | return false; |
| 331 | nr_initialised++; |
| 332 | if ((nr_initialised > NODE_DATA(nid)->static_init_pgcnt) && |
| 333 | (pfn & (PAGES_PER_SECTION - 1)) == 0) { |
| 334 | NODE_DATA(nid)->first_deferred_pfn = pfn; |
| 335 | return true; |
| 336 | } |
| 337 | return false; |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 338 | } |
| 339 | #else |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 340 | static inline bool early_page_uninitialised(unsigned long pfn) |
| 341 | { |
| 342 | return false; |
| 343 | } |
| 344 | |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 345 | static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn) |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 346 | { |
Pavel Tatashin | d3035be | 2018-10-26 15:09:37 -0700 | [diff] [blame] | 347 | return false; |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 348 | } |
| 349 | #endif |
| 350 | |
Mel Gorman | 0b423ca | 2016-05-19 17:14:27 -0700 | [diff] [blame] | 351 | /* Return a pointer to the bitmap storing bits affecting a block of pages */ |
| 352 | static inline unsigned long *get_pageblock_bitmap(struct page *page, |
| 353 | unsigned long pfn) |
| 354 | { |
| 355 | #ifdef CONFIG_SPARSEMEM |
| 356 | return __pfn_to_section(pfn)->pageblock_flags; |
| 357 | #else |
| 358 | return page_zone(page)->pageblock_flags; |
| 359 | #endif /* CONFIG_SPARSEMEM */ |
| 360 | } |
| 361 | |
| 362 | static inline int pfn_to_bitidx(struct page *page, unsigned long pfn) |
| 363 | { |
| 364 | #ifdef CONFIG_SPARSEMEM |
| 365 | pfn &= (PAGES_PER_SECTION-1); |
| 366 | return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
| 367 | #else |
| 368 | pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages); |
| 369 | return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
| 370 | #endif /* CONFIG_SPARSEMEM */ |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages |
| 375 | * @page: The page within the block of interest |
| 376 | * @pfn: The target page frame number |
| 377 | * @end_bitidx: The last bit of interest to retrieve |
| 378 | * @mask: mask of bits that the caller is interested in |
| 379 | * |
| 380 | * Return: pageblock_bits flags |
| 381 | */ |
| 382 | static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page, |
| 383 | unsigned long pfn, |
| 384 | unsigned long end_bitidx, |
| 385 | unsigned long mask) |
| 386 | { |
| 387 | unsigned long *bitmap; |
| 388 | unsigned long bitidx, word_bitidx; |
| 389 | unsigned long word; |
| 390 | |
| 391 | bitmap = get_pageblock_bitmap(page, pfn); |
| 392 | bitidx = pfn_to_bitidx(page, pfn); |
| 393 | word_bitidx = bitidx / BITS_PER_LONG; |
| 394 | bitidx &= (BITS_PER_LONG-1); |
| 395 | |
| 396 | word = bitmap[word_bitidx]; |
| 397 | bitidx += end_bitidx; |
| 398 | return (word >> (BITS_PER_LONG - bitidx - 1)) & mask; |
| 399 | } |
| 400 | |
| 401 | unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn, |
| 402 | unsigned long end_bitidx, |
| 403 | unsigned long mask) |
| 404 | { |
| 405 | return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask); |
| 406 | } |
| 407 | |
| 408 | static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn) |
| 409 | { |
| 410 | return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages |
| 415 | * @page: The page within the block of interest |
| 416 | * @flags: The flags to set |
| 417 | * @pfn: The target page frame number |
| 418 | * @end_bitidx: The last bit of interest |
| 419 | * @mask: mask of bits that the caller is interested in |
| 420 | */ |
| 421 | void set_pfnblock_flags_mask(struct page *page, unsigned long flags, |
| 422 | unsigned long pfn, |
| 423 | unsigned long end_bitidx, |
| 424 | unsigned long mask) |
| 425 | { |
| 426 | unsigned long *bitmap; |
| 427 | unsigned long bitidx, word_bitidx; |
| 428 | unsigned long old_word, word; |
| 429 | |
| 430 | BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4); |
| 431 | |
| 432 | bitmap = get_pageblock_bitmap(page, pfn); |
| 433 | bitidx = pfn_to_bitidx(page, pfn); |
| 434 | word_bitidx = bitidx / BITS_PER_LONG; |
| 435 | bitidx &= (BITS_PER_LONG-1); |
| 436 | |
| 437 | VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page); |
| 438 | |
| 439 | bitidx += end_bitidx; |
| 440 | mask <<= (BITS_PER_LONG - bitidx - 1); |
| 441 | flags <<= (BITS_PER_LONG - bitidx - 1); |
| 442 | |
| 443 | word = READ_ONCE(bitmap[word_bitidx]); |
| 444 | for (;;) { |
| 445 | old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags); |
| 446 | if (word == old_word) |
| 447 | break; |
| 448 | word = old_word; |
| 449 | } |
| 450 | } |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 451 | |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 452 | void set_pageblock_migratetype(struct page *page, int migratetype) |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 453 | { |
KOSAKI Motohiro | 5d0f3f7 | 2013-11-12 15:08:18 -0800 | [diff] [blame] | 454 | if (unlikely(page_group_by_mobility_disabled && |
| 455 | migratetype < MIGRATE_PCPTYPES)) |
Mel Gorman | 49255c6 | 2009-06-16 15:31:58 -0700 | [diff] [blame] | 456 | migratetype = MIGRATE_UNMOVABLE; |
| 457 | |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 458 | set_pageblock_flags_group(page, (unsigned long)migratetype, |
| 459 | PB_migrate, PB_migrate_end); |
| 460 | } |
| 461 | |
Nick Piggin | 13e7444 | 2006-01-06 00:10:58 -0800 | [diff] [blame] | 462 | #ifdef CONFIG_DEBUG_VM |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 463 | static int page_outside_zone_boundaries(struct zone *zone, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Dave Hansen | bdc8cb9 | 2005-10-29 18:16:53 -0700 | [diff] [blame] | 465 | int ret = 0; |
| 466 | unsigned seq; |
| 467 | unsigned long pfn = page_to_pfn(page); |
Cody P Schafer | b5e6a5a | 2013-02-22 16:35:28 -0800 | [diff] [blame] | 468 | unsigned long sp, start_pfn; |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 469 | |
Dave Hansen | bdc8cb9 | 2005-10-29 18:16:53 -0700 | [diff] [blame] | 470 | do { |
| 471 | seq = zone_span_seqbegin(zone); |
Cody P Schafer | b5e6a5a | 2013-02-22 16:35:28 -0800 | [diff] [blame] | 472 | start_pfn = zone->zone_start_pfn; |
| 473 | sp = zone->spanned_pages; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 474 | if (!zone_spans_pfn(zone, pfn)) |
Dave Hansen | bdc8cb9 | 2005-10-29 18:16:53 -0700 | [diff] [blame] | 475 | ret = 1; |
| 476 | } while (zone_span_seqretry(zone, seq)); |
| 477 | |
Cody P Schafer | b5e6a5a | 2013-02-22 16:35:28 -0800 | [diff] [blame] | 478 | if (ret) |
Dave Hansen | 613813e | 2014-06-04 16:07:27 -0700 | [diff] [blame] | 479 | pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n", |
| 480 | pfn, zone_to_nid(zone), zone->name, |
| 481 | start_pfn, start_pfn + sp); |
Cody P Schafer | b5e6a5a | 2013-02-22 16:35:28 -0800 | [diff] [blame] | 482 | |
Dave Hansen | bdc8cb9 | 2005-10-29 18:16:53 -0700 | [diff] [blame] | 483 | return ret; |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | static int page_is_consistent(struct zone *zone, struct page *page) |
| 487 | { |
Andy Whitcroft | 14e0729 | 2007-05-06 14:49:14 -0700 | [diff] [blame] | 488 | if (!pfn_valid_within(page_to_pfn(page))) |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 489 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | if (zone != page_zone(page)) |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 491 | return 0; |
| 492 | |
| 493 | return 1; |
| 494 | } |
| 495 | /* |
| 496 | * Temporary debugging check for pages not lying within a given zone. |
| 497 | */ |
Matthias Kaehlcke | d73d3c9f | 2017-07-06 15:39:23 -0700 | [diff] [blame] | 498 | static int __maybe_unused bad_range(struct zone *zone, struct page *page) |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 499 | { |
| 500 | if (page_outside_zone_boundaries(zone, page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return 1; |
Dave Hansen | c6a57e1 | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 502 | if (!page_is_consistent(zone, page)) |
| 503 | return 1; |
| 504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | return 0; |
| 506 | } |
Nick Piggin | 13e7444 | 2006-01-06 00:10:58 -0800 | [diff] [blame] | 507 | #else |
Matthias Kaehlcke | d73d3c9f | 2017-07-06 15:39:23 -0700 | [diff] [blame] | 508 | static inline int __maybe_unused bad_range(struct zone *zone, struct page *page) |
Nick Piggin | 13e7444 | 2006-01-06 00:10:58 -0800 | [diff] [blame] | 509 | { |
| 510 | return 0; |
| 511 | } |
| 512 | #endif |
| 513 | |
Kirill A. Shutemov | d230dec | 2014-04-07 15:37:38 -0700 | [diff] [blame] | 514 | static void bad_page(struct page *page, const char *reason, |
| 515 | unsigned long bad_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Hugh Dickins | d936cf9 | 2009-01-06 14:40:12 -0800 | [diff] [blame] | 517 | static unsigned long resume; |
| 518 | static unsigned long nr_shown; |
| 519 | static unsigned long nr_unshown; |
| 520 | |
| 521 | /* |
| 522 | * Allow a burst of 60 reports, then keep quiet for that minute; |
| 523 | * or allow a steady drip of one report per second. |
| 524 | */ |
| 525 | if (nr_shown == 60) { |
| 526 | if (time_before(jiffies, resume)) { |
| 527 | nr_unshown++; |
| 528 | goto out; |
| 529 | } |
| 530 | if (nr_unshown) { |
Vlastimil Babka | ff8e811 | 2016-03-15 14:56:24 -0700 | [diff] [blame] | 531 | pr_alert( |
Hugh Dickins | 1e9e636 | 2009-01-06 14:40:13 -0800 | [diff] [blame] | 532 | "BUG: Bad page state: %lu messages suppressed\n", |
Hugh Dickins | d936cf9 | 2009-01-06 14:40:12 -0800 | [diff] [blame] | 533 | nr_unshown); |
| 534 | nr_unshown = 0; |
| 535 | } |
| 536 | nr_shown = 0; |
| 537 | } |
| 538 | if (nr_shown++ == 0) |
| 539 | resume = jiffies + 60 * HZ; |
| 540 | |
Vlastimil Babka | ff8e811 | 2016-03-15 14:56:24 -0700 | [diff] [blame] | 541 | pr_alert("BUG: Bad page state in process %s pfn:%05lx\n", |
Hugh Dickins | 3dc1474 | 2009-01-06 14:40:08 -0800 | [diff] [blame] | 542 | current->comm, page_to_pfn(page)); |
Vlastimil Babka | ff8e811 | 2016-03-15 14:56:24 -0700 | [diff] [blame] | 543 | __dump_page(page, reason); |
| 544 | bad_flags &= page->flags; |
| 545 | if (bad_flags) |
| 546 | pr_alert("bad because of flags: %#lx(%pGp)\n", |
| 547 | bad_flags, &bad_flags); |
Vlastimil Babka | 4e46211 | 2016-03-15 14:56:21 -0700 | [diff] [blame] | 548 | dump_page_owner(page); |
KAMEZAWA Hiroyuki | 52d4b9a | 2008-10-18 20:28:16 -0700 | [diff] [blame] | 549 | |
Dave Jones | 4f31888 | 2011-10-31 17:07:24 -0700 | [diff] [blame] | 550 | print_modules(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | dump_stack(); |
Hugh Dickins | d936cf9 | 2009-01-06 14:40:12 -0800 | [diff] [blame] | 552 | out: |
Hugh Dickins | 8cc3b39 | 2009-01-06 14:40:06 -0800 | [diff] [blame] | 553 | /* Leave bad fields for debug, except PageBuddy could make trouble */ |
Mel Gorman | 22b751c | 2013-02-22 16:34:59 -0800 | [diff] [blame] | 554 | page_mapcount_reset(page); /* remove PageBuddy */ |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 555 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | /* |
| 559 | * Higher-order pages are called "compound pages". They are structured thusly: |
| 560 | * |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 561 | * The first PAGE_SIZE page is called the "head page" and have PG_head set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | * |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 563 | * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded |
| 564 | * in bit 0 of page->compound_head. The rest of bits is pointer to head page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | * |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 566 | * The first tail page's ->compound_dtor holds the offset in array of compound |
| 567 | * page destructors. See compound_page_dtors. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | * |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 569 | * The first tail page's ->compound_order holds the order of allocation. |
Hugh Dickins | 41d78ba | 2006-02-14 13:52:58 -0800 | [diff] [blame] | 570 | * This usage means that zero-order pages may not be compound. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | */ |
Hugh Dickins | d98c7a0 | 2006-02-14 13:52:59 -0800 | [diff] [blame] | 572 | |
Kirill A. Shutemov | 9a98225 | 2016-01-15 16:54:17 -0800 | [diff] [blame] | 573 | void free_compound_page(struct page *page) |
Hugh Dickins | d98c7a0 | 2006-02-14 13:52:59 -0800 | [diff] [blame] | 574 | { |
Christoph Lameter | d85f338 | 2007-05-06 14:49:39 -0700 | [diff] [blame] | 575 | __free_pages_ok(page, compound_order(page)); |
Hugh Dickins | d98c7a0 | 2006-02-14 13:52:59 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 578 | void prep_compound_page(struct page *page, unsigned int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
| 580 | int i; |
| 581 | int nr_pages = 1 << order; |
| 582 | |
Kirill A. Shutemov | f1e6155 | 2015-11-06 16:29:50 -0800 | [diff] [blame] | 583 | set_compound_page_dtor(page, COMPOUND_PAGE_DTOR); |
Christoph Lameter | d85f338 | 2007-05-06 14:49:39 -0700 | [diff] [blame] | 584 | set_compound_order(page, order); |
Christoph Lameter | 6d77795 | 2007-05-06 14:49:40 -0700 | [diff] [blame] | 585 | __SetPageHead(page); |
Andy Whitcroft | 18229df | 2008-11-06 12:53:27 -0800 | [diff] [blame] | 586 | for (i = 1; i < nr_pages; i++) { |
| 587 | struct page *p = page + i; |
Youquan Song | 58a84aa | 2011-12-08 14:34:18 -0800 | [diff] [blame] | 588 | set_page_count(p, 0); |
Kirill A. Shutemov | 1c290f6 | 2016-01-15 16:52:07 -0800 | [diff] [blame] | 589 | p->mapping = TAIL_MAPPING; |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 590 | set_compound_head(p, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 592 | atomic_set(compound_mapcount_ptr(page), -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 595 | #ifdef CONFIG_DEBUG_PAGEALLOC |
| 596 | unsigned int _debug_guardpage_minorder; |
Christian Borntraeger | ea6eabb | 2016-03-15 14:55:30 -0700 | [diff] [blame] | 597 | bool _debug_pagealloc_enabled __read_mostly |
| 598 | = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT); |
Joonsoo Kim | 505f6d2 | 2016-03-17 14:17:56 -0700 | [diff] [blame] | 599 | EXPORT_SYMBOL(_debug_pagealloc_enabled); |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 600 | bool _debug_guardpage_enabled __read_mostly; |
| 601 | |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 602 | static int __init early_debug_pagealloc(char *buf) |
| 603 | { |
| 604 | if (!buf) |
| 605 | return -EINVAL; |
Minfei Huang | 2a138dc | 2016-05-20 16:58:13 -0700 | [diff] [blame] | 606 | return kstrtobool(buf, &_debug_pagealloc_enabled); |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 607 | } |
| 608 | early_param("debug_pagealloc", early_debug_pagealloc); |
| 609 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 610 | static bool need_debug_guardpage(void) |
| 611 | { |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 612 | /* If we don't use debug_pagealloc, we don't need guard page */ |
| 613 | if (!debug_pagealloc_enabled()) |
| 614 | return false; |
| 615 | |
Joonsoo Kim | f1c1e9f | 2016-10-07 16:58:18 -0700 | [diff] [blame] | 616 | if (!debug_guardpage_minorder()) |
| 617 | return false; |
| 618 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 619 | return true; |
| 620 | } |
| 621 | |
| 622 | static void init_debug_guardpage(void) |
| 623 | { |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 624 | if (!debug_pagealloc_enabled()) |
| 625 | return; |
| 626 | |
Joonsoo Kim | f1c1e9f | 2016-10-07 16:58:18 -0700 | [diff] [blame] | 627 | if (!debug_guardpage_minorder()) |
| 628 | return; |
| 629 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 630 | _debug_guardpage_enabled = true; |
| 631 | } |
| 632 | |
| 633 | struct page_ext_operations debug_guardpage_ops = { |
| 634 | .need = need_debug_guardpage, |
| 635 | .init = init_debug_guardpage, |
| 636 | }; |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 637 | |
| 638 | static int __init debug_guardpage_minorder_setup(char *buf) |
| 639 | { |
| 640 | unsigned long res; |
| 641 | |
| 642 | if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 643 | pr_err("Bad debug_guardpage_minorder value\n"); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 644 | return 0; |
| 645 | } |
| 646 | _debug_guardpage_minorder = res; |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 647 | pr_info("Setting debug_guardpage_minorder to %lu\n", res); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 648 | return 0; |
| 649 | } |
Joonsoo Kim | f1c1e9f | 2016-10-07 16:58:18 -0700 | [diff] [blame] | 650 | early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 651 | |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 652 | static inline bool set_page_guard(struct zone *zone, struct page *page, |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 653 | unsigned int order, int migratetype) |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 654 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 655 | struct page_ext *page_ext; |
| 656 | |
| 657 | if (!debug_guardpage_enabled()) |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 658 | return false; |
| 659 | |
| 660 | if (order >= debug_guardpage_minorder()) |
| 661 | return false; |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 662 | |
| 663 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 664 | if (unlikely(!page_ext)) |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 665 | return false; |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 666 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 667 | __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags); |
| 668 | |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 669 | INIT_LIST_HEAD(&page->lru); |
| 670 | set_page_private(page, order); |
| 671 | /* Guard pages are not available for any usage */ |
| 672 | __mod_zone_freepage_state(zone, -(1 << order), migratetype); |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 673 | |
| 674 | return true; |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 677 | static inline void clear_page_guard(struct zone *zone, struct page *page, |
| 678 | unsigned int order, int migratetype) |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 679 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 680 | struct page_ext *page_ext; |
| 681 | |
| 682 | if (!debug_guardpage_enabled()) |
| 683 | return; |
| 684 | |
| 685 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 686 | if (unlikely(!page_ext)) |
| 687 | return; |
| 688 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 689 | __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags); |
| 690 | |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 691 | set_page_private(page, 0); |
| 692 | if (!is_migrate_isolate(migratetype)) |
| 693 | __mod_zone_freepage_state(zone, (1 << order), migratetype); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 694 | } |
| 695 | #else |
Joonsoo Kim | 980ac16 | 2016-10-07 16:58:27 -0700 | [diff] [blame] | 696 | struct page_ext_operations debug_guardpage_ops; |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 697 | static inline bool set_page_guard(struct zone *zone, struct page *page, |
| 698 | unsigned int order, int migratetype) { return false; } |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 699 | static inline void clear_page_guard(struct zone *zone, struct page *page, |
| 700 | unsigned int order, int migratetype) {} |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 701 | #endif |
| 702 | |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 703 | static inline void set_page_order(struct page *page, unsigned int order) |
Andrew Morton | 6aa3001b2 | 2006-04-18 22:20:52 -0700 | [diff] [blame] | 704 | { |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 705 | set_page_private(page, order); |
Nick Piggin | 676165a | 2006-04-10 11:21:48 +1000 | [diff] [blame] | 706 | __SetPageBuddy(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | static inline void rmv_page_order(struct page *page) |
| 710 | { |
Nick Piggin | 676165a | 2006-04-10 11:21:48 +1000 | [diff] [blame] | 711 | __ClearPageBuddy(page); |
Hugh Dickins | 4c21e2f | 2005-10-29 18:16:40 -0700 | [diff] [blame] | 712 | set_page_private(page, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | * This function checks whether a page is free && is the buddy |
Matthew Wilcox | 6e292b9 | 2018-06-07 17:08:18 -0700 | [diff] [blame] | 717 | * we can coalesce a page and its buddy if |
Vlastimil Babka | 13ad59d | 2017-02-22 15:41:51 -0800 | [diff] [blame] | 718 | * (a) the buddy is not in a hole (check before calling!) && |
Nick Piggin | 676165a | 2006-04-10 11:21:48 +1000 | [diff] [blame] | 719 | * (b) the buddy is in the buddy system && |
Andy Whitcroft | cb2b95e | 2006-06-23 02:03:01 -0700 | [diff] [blame] | 720 | * (c) a page and its buddy have the same order && |
| 721 | * (d) a page and its buddy are in the same zone. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | * |
Matthew Wilcox | 6e292b9 | 2018-06-07 17:08:18 -0700 | [diff] [blame] | 723 | * For recording whether a page is in the buddy system, we set PageBuddy. |
| 724 | * Setting, clearing, and testing PageBuddy is serialized by zone->lock. |
Nick Piggin | 676165a | 2006-04-10 11:21:48 +1000 | [diff] [blame] | 725 | * |
| 726 | * For recording page's order, we use page_private(page). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | */ |
Andy Whitcroft | cb2b95e | 2006-06-23 02:03:01 -0700 | [diff] [blame] | 728 | static inline int page_is_buddy(struct page *page, struct page *buddy, |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 729 | unsigned int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | { |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 731 | if (page_is_guard(buddy) && page_order(buddy) == order) { |
Mel Gorman | d34c5fa | 2014-06-04 16:10:10 -0700 | [diff] [blame] | 732 | if (page_zone_id(page) != page_zone_id(buddy)) |
| 733 | return 0; |
| 734 | |
Weijie Yang | 4c5018c | 2015-02-10 14:11:39 -0800 | [diff] [blame] | 735 | VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); |
| 736 | |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 737 | return 1; |
| 738 | } |
| 739 | |
Andy Whitcroft | cb2b95e | 2006-06-23 02:03:01 -0700 | [diff] [blame] | 740 | if (PageBuddy(buddy) && page_order(buddy) == order) { |
Mel Gorman | d34c5fa | 2014-06-04 16:10:10 -0700 | [diff] [blame] | 741 | /* |
| 742 | * zone check is done late to avoid uselessly |
| 743 | * calculating zone/node ids for pages that could |
| 744 | * never merge. |
| 745 | */ |
| 746 | if (page_zone_id(page) != page_zone_id(buddy)) |
| 747 | return 0; |
| 748 | |
Weijie Yang | 4c5018c | 2015-02-10 14:11:39 -0800 | [diff] [blame] | 749 | VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); |
| 750 | |
Andrew Morton | 6aa3001b2 | 2006-04-18 22:20:52 -0700 | [diff] [blame] | 751 | return 1; |
Nick Piggin | 676165a | 2006-04-10 11:21:48 +1000 | [diff] [blame] | 752 | } |
Andrew Morton | 6aa3001b2 | 2006-04-18 22:20:52 -0700 | [diff] [blame] | 753 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | /* |
| 757 | * Freeing function for a buddy system allocator. |
| 758 | * |
| 759 | * The concept of a buddy system is to maintain direct-mapped table |
| 760 | * (containing bit values) for memory blocks of various "orders". |
| 761 | * The bottom level table contains the map for the smallest allocatable |
| 762 | * units of memory (here, pages), and each level above it describes |
| 763 | * pairs of units from the levels below, hence, "buddies". |
| 764 | * At a high level, all that happens here is marking the table entry |
| 765 | * at the bottom level available, and propagating the changes upward |
| 766 | * as necessary, plus some accounting needed to play nicely with other |
| 767 | * parts of the VM system. |
| 768 | * At each level, we keep a list of pages, which are heads of continuous |
Matthew Wilcox | 6e292b9 | 2018-06-07 17:08:18 -0700 | [diff] [blame] | 769 | * free pages of length of (1 << order) and marked with PageBuddy. |
| 770 | * Page's order is recorded in page_private(page) field. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | * So when we are allocating or freeing one, we can derive the state of the |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 772 | * other. That is, if we allocate a small block, and both were |
| 773 | * free, the remainder of the region must be split into blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | * If a block is freed, and its buddy is also free, then this |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 775 | * triggers coalescing into a block of larger size. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | * |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 777 | * -- nyc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | */ |
| 779 | |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 780 | static inline void __free_one_page(struct page *page, |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 781 | unsigned long pfn, |
Mel Gorman | ed0ae21 | 2009-06-16 15:32:07 -0700 | [diff] [blame] | 782 | struct zone *zone, unsigned int order, |
| 783 | int migratetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | { |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 785 | unsigned long combined_pfn; |
| 786 | unsigned long uninitialized_var(buddy_pfn); |
Corrado Zoccolo | 6dda9d5 | 2010-05-24 14:31:54 -0700 | [diff] [blame] | 787 | struct page *buddy; |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 788 | unsigned int max_order; |
| 789 | |
| 790 | max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | |
Cody P Schafer | d29bb97 | 2013-02-22 16:35:25 -0800 | [diff] [blame] | 792 | VM_BUG_ON(!zone_is_initialized(zone)); |
Kirill A. Shutemov | 6e9f0d5 | 2015-02-11 15:25:50 -0800 | [diff] [blame] | 793 | VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | |
Mel Gorman | ed0ae21 | 2009-06-16 15:32:07 -0700 | [diff] [blame] | 795 | VM_BUG_ON(migratetype == -1); |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 796 | if (likely(!is_migrate_isolate(migratetype))) |
Joonsoo Kim | 8f82b55 | 2014-11-13 15:19:18 -0800 | [diff] [blame] | 797 | __mod_zone_freepage_state(zone, 1 << order, migratetype); |
Mel Gorman | ed0ae21 | 2009-06-16 15:32:07 -0700 | [diff] [blame] | 798 | |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 799 | VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 800 | VM_BUG_ON_PAGE(bad_range(zone, page), page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 802 | continue_merging: |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 803 | while (order < max_order - 1) { |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 804 | buddy_pfn = __find_buddy_pfn(pfn, order); |
| 805 | buddy = page + (buddy_pfn - pfn); |
Vlastimil Babka | 13ad59d | 2017-02-22 15:41:51 -0800 | [diff] [blame] | 806 | |
| 807 | if (!pfn_valid_within(buddy_pfn)) |
| 808 | goto done_merging; |
Andy Whitcroft | cb2b95e | 2006-06-23 02:03:01 -0700 | [diff] [blame] | 809 | if (!page_is_buddy(page, buddy, order)) |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 810 | goto done_merging; |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 811 | /* |
| 812 | * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page, |
| 813 | * merge with it and move up one order. |
| 814 | */ |
| 815 | if (page_is_guard(buddy)) { |
Joonsoo Kim | 2847cf9 | 2014-12-12 16:55:01 -0800 | [diff] [blame] | 816 | clear_page_guard(zone, buddy, order, migratetype); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 817 | } else { |
| 818 | list_del(&buddy->lru); |
| 819 | zone->free_area[order].nr_free--; |
| 820 | rmv_page_order(buddy); |
| 821 | } |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 822 | combined_pfn = buddy_pfn & pfn; |
| 823 | page = page + (combined_pfn - pfn); |
| 824 | pfn = combined_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | order++; |
| 826 | } |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 827 | if (max_order < MAX_ORDER) { |
| 828 | /* If we are here, it means order is >= pageblock_order. |
| 829 | * We want to prevent merge between freepages on isolate |
| 830 | * pageblock and normal pageblock. Without this, pageblock |
| 831 | * isolation could cause incorrect freepage or CMA accounting. |
| 832 | * |
| 833 | * We don't want to hit this code for the more frequent |
| 834 | * low-order merging. |
| 835 | */ |
| 836 | if (unlikely(has_isolate_pageblock(zone))) { |
| 837 | int buddy_mt; |
| 838 | |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 839 | buddy_pfn = __find_buddy_pfn(pfn, order); |
| 840 | buddy = page + (buddy_pfn - pfn); |
Vlastimil Babka | d9dddbf | 2016-03-25 14:21:50 -0700 | [diff] [blame] | 841 | buddy_mt = get_pageblock_migratetype(buddy); |
| 842 | |
| 843 | if (migratetype != buddy_mt |
| 844 | && (is_migrate_isolate(migratetype) || |
| 845 | is_migrate_isolate(buddy_mt))) |
| 846 | goto done_merging; |
| 847 | } |
| 848 | max_order++; |
| 849 | goto continue_merging; |
| 850 | } |
| 851 | |
| 852 | done_merging: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | set_page_order(page, order); |
Corrado Zoccolo | 6dda9d5 | 2010-05-24 14:31:54 -0700 | [diff] [blame] | 854 | |
| 855 | /* |
| 856 | * If this is not the largest possible page, check if the buddy |
| 857 | * of the next-highest order is free. If it is, it's possible |
| 858 | * that pages are being freed that will coalesce soon. In case, |
| 859 | * that is happening, add the free page to the tail of the list |
| 860 | * so it's less likely to be used soon and more likely to be merged |
| 861 | * as a higher order page |
| 862 | */ |
Vlastimil Babka | 13ad59d | 2017-02-22 15:41:51 -0800 | [diff] [blame] | 863 | if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) { |
Corrado Zoccolo | 6dda9d5 | 2010-05-24 14:31:54 -0700 | [diff] [blame] | 864 | struct page *higher_page, *higher_buddy; |
Vlastimil Babka | 76741e7 | 2017-02-22 15:41:48 -0800 | [diff] [blame] | 865 | combined_pfn = buddy_pfn & pfn; |
| 866 | higher_page = page + (combined_pfn - pfn); |
| 867 | buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); |
| 868 | higher_buddy = higher_page + (buddy_pfn - combined_pfn); |
Tony Luck | b4fb8f6 | 2017-03-08 09:35:39 -0800 | [diff] [blame] | 869 | if (pfn_valid_within(buddy_pfn) && |
| 870 | page_is_buddy(higher_page, higher_buddy, order + 1)) { |
Corrado Zoccolo | 6dda9d5 | 2010-05-24 14:31:54 -0700 | [diff] [blame] | 871 | list_add_tail(&page->lru, |
| 872 | &zone->free_area[order].free_list[migratetype]); |
| 873 | goto out; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | list_add(&page->lru, &zone->free_area[order].free_list[migratetype]); |
| 878 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | zone->free_area[order].nr_free++; |
| 880 | } |
| 881 | |
Mel Gorman | 7bfec6f | 2016-05-19 17:14:15 -0700 | [diff] [blame] | 882 | /* |
| 883 | * A bad page could be due to a number of fields. Instead of multiple branches, |
| 884 | * try and check multiple fields with one check. The caller must do a detailed |
| 885 | * check if necessary. |
| 886 | */ |
| 887 | static inline bool page_expected_state(struct page *page, |
| 888 | unsigned long check_flags) |
| 889 | { |
| 890 | if (unlikely(atomic_read(&page->_mapcount) != -1)) |
| 891 | return false; |
| 892 | |
| 893 | if (unlikely((unsigned long)page->mapping | |
| 894 | page_ref_count(page) | |
| 895 | #ifdef CONFIG_MEMCG |
| 896 | (unsigned long)page->mem_cgroup | |
| 897 | #endif |
| 898 | (page->flags & check_flags))) |
| 899 | return false; |
| 900 | |
| 901 | return true; |
| 902 | } |
| 903 | |
Mel Gorman | bb552ac | 2016-05-19 17:14:18 -0700 | [diff] [blame] | 904 | static void free_pages_check_bad(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
Mel Gorman | 7bfec6f | 2016-05-19 17:14:15 -0700 | [diff] [blame] | 906 | const char *bad_reason; |
| 907 | unsigned long bad_flags; |
| 908 | |
Mel Gorman | 7bfec6f | 2016-05-19 17:14:15 -0700 | [diff] [blame] | 909 | bad_reason = NULL; |
| 910 | bad_flags = 0; |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 911 | |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 912 | if (unlikely(atomic_read(&page->_mapcount) != -1)) |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 913 | bad_reason = "nonzero mapcount"; |
| 914 | if (unlikely(page->mapping != NULL)) |
| 915 | bad_reason = "non-NULL mapping"; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 916 | if (unlikely(page_ref_count(page) != 0)) |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 917 | bad_reason = "nonzero _refcount"; |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 918 | if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) { |
| 919 | bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set"; |
| 920 | bad_flags = PAGE_FLAGS_CHECK_AT_FREE; |
| 921 | } |
Johannes Weiner | 9edad6e | 2014-12-10 15:44:58 -0800 | [diff] [blame] | 922 | #ifdef CONFIG_MEMCG |
| 923 | if (unlikely(page->mem_cgroup)) |
| 924 | bad_reason = "page still charged to cgroup"; |
| 925 | #endif |
Mel Gorman | 7bfec6f | 2016-05-19 17:14:15 -0700 | [diff] [blame] | 926 | bad_page(page, bad_reason, bad_flags); |
Mel Gorman | bb552ac | 2016-05-19 17:14:18 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static inline int free_pages_check(struct page *page) |
| 930 | { |
Mel Gorman | da838d4 | 2016-05-19 17:14:21 -0700 | [diff] [blame] | 931 | if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE))) |
Mel Gorman | bb552ac | 2016-05-19 17:14:18 -0700 | [diff] [blame] | 932 | return 0; |
Mel Gorman | bb552ac | 2016-05-19 17:14:18 -0700 | [diff] [blame] | 933 | |
| 934 | /* Something has gone sideways, find it */ |
| 935 | free_pages_check_bad(page); |
Mel Gorman | 7bfec6f | 2016-05-19 17:14:15 -0700 | [diff] [blame] | 936 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 939 | static int free_tail_pages_check(struct page *head_page, struct page *page) |
| 940 | { |
| 941 | int ret = 1; |
| 942 | |
| 943 | /* |
| 944 | * We rely page->lru.next never has bit 0 set, unless the page |
| 945 | * is PageTail(). Let's make sure that's true even for poisoned ->lru. |
| 946 | */ |
| 947 | BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1); |
| 948 | |
| 949 | if (!IS_ENABLED(CONFIG_DEBUG_VM)) { |
| 950 | ret = 0; |
| 951 | goto out; |
| 952 | } |
| 953 | switch (page - head_page) { |
| 954 | case 1: |
Matthew Wilcox | 4da1984 | 2018-06-07 17:08:50 -0700 | [diff] [blame] | 955 | /* the first tail page: ->mapping may be compound_mapcount() */ |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 956 | if (unlikely(compound_mapcount(page))) { |
| 957 | bad_page(page, "nonzero compound_mapcount", 0); |
| 958 | goto out; |
| 959 | } |
| 960 | break; |
| 961 | case 2: |
| 962 | /* |
| 963 | * the second tail page: ->mapping is |
Matthew Wilcox | fa3015b | 2018-06-07 17:08:42 -0700 | [diff] [blame] | 964 | * deferred_list.next -- ignore value. |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 965 | */ |
| 966 | break; |
| 967 | default: |
| 968 | if (page->mapping != TAIL_MAPPING) { |
| 969 | bad_page(page, "corrupted mapping in tail page", 0); |
| 970 | goto out; |
| 971 | } |
| 972 | break; |
| 973 | } |
| 974 | if (unlikely(!PageTail(page))) { |
| 975 | bad_page(page, "PageTail not set", 0); |
| 976 | goto out; |
| 977 | } |
| 978 | if (unlikely(compound_head(page) != head_page)) { |
| 979 | bad_page(page, "compound_head not consistent", 0); |
| 980 | goto out; |
| 981 | } |
| 982 | ret = 0; |
| 983 | out: |
| 984 | page->mapping = NULL; |
| 985 | clear_compound_head(page); |
| 986 | return ret; |
| 987 | } |
| 988 | |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 989 | static __always_inline bool free_pages_prepare(struct page *page, |
| 990 | unsigned int order, bool check_free) |
| 991 | { |
| 992 | int bad = 0; |
| 993 | |
| 994 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 995 | |
| 996 | trace_mm_page_free(page, order); |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 997 | |
| 998 | /* |
| 999 | * Check tail pages before head page information is cleared to |
| 1000 | * avoid checking PageCompound for order-0 pages. |
| 1001 | */ |
| 1002 | if (unlikely(order)) { |
| 1003 | bool compound = PageCompound(page); |
| 1004 | int i; |
| 1005 | |
| 1006 | VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); |
| 1007 | |
Kirill A. Shutemov | 9a73f61 | 2016-07-26 15:25:53 -0700 | [diff] [blame] | 1008 | if (compound) |
| 1009 | ClearPageDoubleMap(page); |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1010 | for (i = 1; i < (1 << order); i++) { |
| 1011 | if (compound) |
| 1012 | bad += free_tail_pages_check(page, page + i); |
| 1013 | if (unlikely(free_pages_check(page + i))) { |
| 1014 | bad++; |
| 1015 | continue; |
| 1016 | } |
| 1017 | (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
| 1018 | } |
| 1019 | } |
Minchan Kim | bda807d | 2016-07-26 15:23:05 -0700 | [diff] [blame] | 1020 | if (PageMappingFlags(page)) |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1021 | page->mapping = NULL; |
Vladimir Davydov | c4159a7 | 2016-08-08 23:03:12 +0300 | [diff] [blame] | 1022 | if (memcg_kmem_enabled() && PageKmemcg(page)) |
Vladimir Davydov | 4949148 | 2016-07-26 15:24:24 -0700 | [diff] [blame] | 1023 | memcg_kmem_uncharge(page, order); |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1024 | if (check_free) |
| 1025 | bad += free_pages_check(page); |
| 1026 | if (bad) |
| 1027 | return false; |
| 1028 | |
| 1029 | page_cpupid_reset_last(page); |
| 1030 | page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
| 1031 | reset_page_owner(page, order); |
| 1032 | |
| 1033 | if (!PageHighMem(page)) { |
| 1034 | debug_check_no_locks_freed(page_address(page), |
| 1035 | PAGE_SIZE << order); |
| 1036 | debug_check_no_obj_freed(page_address(page), |
| 1037 | PAGE_SIZE << order); |
| 1038 | } |
| 1039 | arch_free_page(page, order); |
| 1040 | kernel_poison_pages(page, 1 << order, 0); |
| 1041 | kernel_map_pages(page, 1 << order, 0); |
seokhoon.yoon | 29b52de | 2016-05-20 16:58:47 -0700 | [diff] [blame] | 1042 | kasan_free_pages(page, order); |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1043 | |
| 1044 | return true; |
| 1045 | } |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 1046 | |
| 1047 | #ifdef CONFIG_DEBUG_VM |
| 1048 | static inline bool free_pcp_prepare(struct page *page) |
| 1049 | { |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1050 | return free_pages_prepare(page, 0, true); |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | static inline bool bulkfree_pcp_prepare(struct page *page) |
| 1054 | { |
| 1055 | return false; |
| 1056 | } |
| 1057 | #else |
| 1058 | static bool free_pcp_prepare(struct page *page) |
| 1059 | { |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1060 | return free_pages_prepare(page, 0, false); |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | static bool bulkfree_pcp_prepare(struct page *page) |
| 1064 | { |
| 1065 | return free_pages_check(page); |
| 1066 | } |
| 1067 | #endif /* CONFIG_DEBUG_VM */ |
| 1068 | |
Aaron Lu | 9733416 | 2018-04-05 16:24:14 -0700 | [diff] [blame] | 1069 | static inline void prefetch_buddy(struct page *page) |
| 1070 | { |
| 1071 | unsigned long pfn = page_to_pfn(page); |
| 1072 | unsigned long buddy_pfn = __find_buddy_pfn(pfn, 0); |
| 1073 | struct page *buddy = page + (buddy_pfn - pfn); |
| 1074 | |
| 1075 | prefetch(buddy); |
| 1076 | } |
| 1077 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | /* |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1079 | * Frees a number of pages from the PCP lists |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | * Assumes all pages on list are in same zone, and of same order. |
Renaud Lienhart | 207f36e | 2005-09-10 00:26:59 -0700 | [diff] [blame] | 1081 | * count is the number of pages to free. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * |
| 1083 | * If the zone was previously in an "all pages pinned" state then look to |
| 1084 | * see if this freeing clears that state. |
| 1085 | * |
| 1086 | * And clear the zone's pages_scanned counter, to hold off the "all pages are |
| 1087 | * pinned" detection logic. |
| 1088 | */ |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1089 | static void free_pcppages_bulk(struct zone *zone, int count, |
| 1090 | struct per_cpu_pages *pcp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | { |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1092 | int migratetype = 0; |
Mel Gorman | a6f9edd6 | 2009-09-21 17:03:20 -0700 | [diff] [blame] | 1093 | int batch_free = 0; |
Aaron Lu | 9733416 | 2018-04-05 16:24:14 -0700 | [diff] [blame] | 1094 | int prefetch_nr = 0; |
Mel Gorman | 3777999 | 2016-05-19 17:13:58 -0700 | [diff] [blame] | 1095 | bool isolated_pageblocks; |
Aaron Lu | 0a5f4e5 | 2018-04-05 16:24:10 -0700 | [diff] [blame] | 1096 | struct page *page, *tmp; |
| 1097 | LIST_HEAD(head); |
Mel Gorman | f2260e6 | 2009-06-16 15:32:13 -0700 | [diff] [blame] | 1098 | |
Mel Gorman | e5b31ac | 2016-05-19 17:14:24 -0700 | [diff] [blame] | 1099 | while (count) { |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1100 | struct list_head *list; |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 1101 | |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1102 | /* |
Mel Gorman | a6f9edd6 | 2009-09-21 17:03:20 -0700 | [diff] [blame] | 1103 | * Remove pages from lists in a round-robin fashion. A |
| 1104 | * batch_free count is maintained that is incremented when an |
| 1105 | * empty list is encountered. This is so more pages are freed |
| 1106 | * off fuller lists instead of spinning excessively around empty |
| 1107 | * lists |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1108 | */ |
| 1109 | do { |
Mel Gorman | a6f9edd6 | 2009-09-21 17:03:20 -0700 | [diff] [blame] | 1110 | batch_free++; |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 1111 | if (++migratetype == MIGRATE_PCPTYPES) |
| 1112 | migratetype = 0; |
| 1113 | list = &pcp->lists[migratetype]; |
| 1114 | } while (list_empty(list)); |
| 1115 | |
Namhyung Kim | 1d16871 | 2011-03-22 16:32:45 -0700 | [diff] [blame] | 1116 | /* This is the only non-empty list. Free them all. */ |
| 1117 | if (batch_free == MIGRATE_PCPTYPES) |
Mel Gorman | e5b31ac | 2016-05-19 17:14:24 -0700 | [diff] [blame] | 1118 | batch_free = count; |
Namhyung Kim | 1d16871 | 2011-03-22 16:32:45 -0700 | [diff] [blame] | 1119 | |
Mel Gorman | a6f9edd6 | 2009-09-21 17:03:20 -0700 | [diff] [blame] | 1120 | do { |
Geliang Tang | a16601c | 2016-01-14 15:20:30 -0800 | [diff] [blame] | 1121 | page = list_last_entry(list, struct page, lru); |
Aaron Lu | 0a5f4e5 | 2018-04-05 16:24:10 -0700 | [diff] [blame] | 1122 | /* must delete to avoid corrupting pcp list */ |
Mel Gorman | a6f9edd6 | 2009-09-21 17:03:20 -0700 | [diff] [blame] | 1123 | list_del(&page->lru); |
Aaron Lu | 77ba906 | 2018-04-05 16:24:06 -0700 | [diff] [blame] | 1124 | pcp->count--; |
Vlastimil Babka | aa016d1 | 2015-09-08 15:01:22 -0700 | [diff] [blame] | 1125 | |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 1126 | if (bulkfree_pcp_prepare(page)) |
| 1127 | continue; |
| 1128 | |
Aaron Lu | 0a5f4e5 | 2018-04-05 16:24:10 -0700 | [diff] [blame] | 1129 | list_add_tail(&page->lru, &head); |
Aaron Lu | 9733416 | 2018-04-05 16:24:14 -0700 | [diff] [blame] | 1130 | |
| 1131 | /* |
| 1132 | * We are going to put the page back to the global |
| 1133 | * pool, prefetch its buddy to speed up later access |
| 1134 | * under zone->lock. It is believed the overhead of |
| 1135 | * an additional test and calculating buddy_pfn here |
| 1136 | * can be offset by reduced memory latency later. To |
| 1137 | * avoid excessive prefetching due to large count, only |
| 1138 | * prefetch buddy for the first pcp->batch nr of pages. |
| 1139 | */ |
| 1140 | if (prefetch_nr++ < pcp->batch) |
| 1141 | prefetch_buddy(page); |
Mel Gorman | e5b31ac | 2016-05-19 17:14:24 -0700 | [diff] [blame] | 1142 | } while (--count && --batch_free && !list_empty(list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | } |
Aaron Lu | 0a5f4e5 | 2018-04-05 16:24:10 -0700 | [diff] [blame] | 1144 | |
| 1145 | spin_lock(&zone->lock); |
| 1146 | isolated_pageblocks = has_isolate_pageblock(zone); |
| 1147 | |
| 1148 | /* |
| 1149 | * Use safe version since after __free_one_page(), |
| 1150 | * page->lru.next will not point to original list. |
| 1151 | */ |
| 1152 | list_for_each_entry_safe(page, tmp, &head, lru) { |
| 1153 | int mt = get_pcppage_migratetype(page); |
| 1154 | /* MIGRATE_ISOLATE page should not go to pcplists */ |
| 1155 | VM_BUG_ON_PAGE(is_migrate_isolate(mt), page); |
| 1156 | /* Pageblock could have been isolated meanwhile */ |
| 1157 | if (unlikely(isolated_pageblocks)) |
| 1158 | mt = get_pageblock_migratetype(page); |
| 1159 | |
| 1160 | __free_one_page(page, page_to_pfn(page), zone, 0, mt); |
| 1161 | trace_mm_page_pcpu_drain(page, 0, mt); |
| 1162 | } |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1163 | spin_unlock(&zone->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 1166 | static void free_one_page(struct zone *zone, |
| 1167 | struct page *page, unsigned long pfn, |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 1168 | unsigned int order, |
Mel Gorman | ed0ae21 | 2009-06-16 15:32:07 -0700 | [diff] [blame] | 1169 | int migratetype) |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 1170 | { |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1171 | spin_lock(&zone->lock); |
Joonsoo Kim | ad53f92 | 2014-11-13 15:19:11 -0800 | [diff] [blame] | 1172 | if (unlikely(has_isolate_pageblock(zone) || |
| 1173 | is_migrate_isolate(migratetype))) { |
| 1174 | migratetype = get_pfnblock_migratetype(page, pfn); |
Joonsoo Kim | ad53f92 | 2014-11-13 15:19:11 -0800 | [diff] [blame] | 1175 | } |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 1176 | __free_one_page(page, pfn, zone, order, migratetype); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1177 | spin_unlock(&zone->lock); |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 1178 | } |
| 1179 | |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1180 | static void __meminit __init_single_page(struct page *page, unsigned long pfn, |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 1181 | unsigned long zone, int nid) |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1182 | { |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 1183 | mm_zero_struct_page(page); |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1184 | set_page_links(page, zone, nid, pfn); |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1185 | init_page_count(page); |
| 1186 | page_mapcount_reset(page); |
| 1187 | page_cpupid_reset_last(page); |
Andrey Konovalov | 2813b9c | 2018-12-28 00:30:57 -0800 | [diff] [blame] | 1188 | page_kasan_tag_reset(page); |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1189 | |
Robin Holt | 1e8ce83 | 2015-06-30 14:56:45 -0700 | [diff] [blame] | 1190 | INIT_LIST_HEAD(&page->lru); |
| 1191 | #ifdef WANT_PAGE_VIRTUAL |
| 1192 | /* The shift won't overflow because ZONE_NORMAL is below 4G. */ |
| 1193 | if (!is_highmem_idx(zone)) |
| 1194 | set_page_address(page, __va(pfn << PAGE_SHIFT)); |
| 1195 | #endif |
| 1196 | } |
| 1197 | |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1198 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
Arnd Bergmann | 57148a6 | 2017-10-03 16:15:10 -0700 | [diff] [blame] | 1199 | static void __meminit init_reserved_page(unsigned long pfn) |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1200 | { |
| 1201 | pg_data_t *pgdat; |
| 1202 | int nid, zid; |
| 1203 | |
| 1204 | if (!early_page_uninitialised(pfn)) |
| 1205 | return; |
| 1206 | |
| 1207 | nid = early_pfn_to_nid(pfn); |
| 1208 | pgdat = NODE_DATA(nid); |
| 1209 | |
| 1210 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 1211 | struct zone *zone = &pgdat->node_zones[zid]; |
| 1212 | |
| 1213 | if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone)) |
| 1214 | break; |
| 1215 | } |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 1216 | __init_single_page(pfn_to_page(pfn), pfn, zid, nid); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1217 | } |
| 1218 | #else |
| 1219 | static inline void init_reserved_page(unsigned long pfn) |
| 1220 | { |
| 1221 | } |
| 1222 | #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
| 1223 | |
Nathan Zimmer | 92923ca | 2015-06-30 14:56:48 -0700 | [diff] [blame] | 1224 | /* |
| 1225 | * Initialised pages do not have PageReserved set. This function is |
| 1226 | * called for each range allocated by the bootmem allocator and |
| 1227 | * marks the pages PageReserved. The remaining valid pages are later |
| 1228 | * sent to the buddy page allocator. |
| 1229 | */ |
Stefan Bader | 4b50bcc | 2016-05-20 16:58:38 -0700 | [diff] [blame] | 1230 | void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) |
Nathan Zimmer | 92923ca | 2015-06-30 14:56:48 -0700 | [diff] [blame] | 1231 | { |
| 1232 | unsigned long start_pfn = PFN_DOWN(start); |
| 1233 | unsigned long end_pfn = PFN_UP(end); |
| 1234 | |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1235 | for (; start_pfn < end_pfn; start_pfn++) { |
| 1236 | if (pfn_valid(start_pfn)) { |
| 1237 | struct page *page = pfn_to_page(start_pfn); |
| 1238 | |
| 1239 | init_reserved_page(start_pfn); |
Kirill A. Shutemov | 1d798ca | 2015-11-06 16:29:54 -0800 | [diff] [blame] | 1240 | |
| 1241 | /* Avoid false-positive PageTail() */ |
| 1242 | INIT_LIST_HEAD(&page->lru); |
| 1243 | |
Alexander Duyck | d483da5 | 2018-10-26 15:07:48 -0700 | [diff] [blame] | 1244 | /* |
| 1245 | * no need for atomic set_bit because the struct |
| 1246 | * page is not visible yet so nobody should |
| 1247 | * access it yet. |
| 1248 | */ |
| 1249 | __SetPageReserved(page); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1250 | } |
| 1251 | } |
Nathan Zimmer | 92923ca | 2015-06-30 14:56:48 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
KOSAKI Motohiro | ec95f53 | 2010-05-24 14:32:38 -0700 | [diff] [blame] | 1254 | static void __free_pages_ok(struct page *page, unsigned int order) |
| 1255 | { |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1256 | unsigned long flags; |
Minchan Kim | 95e3441 | 2012-10-08 16:32:11 -0700 | [diff] [blame] | 1257 | int migratetype; |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 1258 | unsigned long pfn = page_to_pfn(page); |
KOSAKI Motohiro | ec95f53 | 2010-05-24 14:32:38 -0700 | [diff] [blame] | 1259 | |
Mel Gorman | e2769db | 2016-05-19 17:14:38 -0700 | [diff] [blame] | 1260 | if (!free_pages_prepare(page, order, true)) |
KOSAKI Motohiro | ec95f53 | 2010-05-24 14:32:38 -0700 | [diff] [blame] | 1261 | return; |
| 1262 | |
Mel Gorman | cfc47a2 | 2014-06-04 16:10:19 -0700 | [diff] [blame] | 1263 | migratetype = get_pfnblock_migratetype(page, pfn); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1264 | local_irq_save(flags); |
| 1265 | __count_vm_events(PGFREE, 1 << order); |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 1266 | free_one_page(page_zone(page), page, pfn, order, migratetype); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 1267 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
Li Zhang | 949698a | 2016-05-19 17:11:37 -0700 | [diff] [blame] | 1270 | static void __init __free_pages_boot_core(struct page *page, unsigned int order) |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 1271 | { |
Johannes Weiner | c399307 | 2012-01-10 15:08:10 -0800 | [diff] [blame] | 1272 | unsigned int nr_pages = 1 << order; |
Yinghai Lu | e2d0bd2 | 2013-09-11 14:20:37 -0700 | [diff] [blame] | 1273 | struct page *p = page; |
Johannes Weiner | c399307 | 2012-01-10 15:08:10 -0800 | [diff] [blame] | 1274 | unsigned int loop; |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 1275 | |
Yinghai Lu | e2d0bd2 | 2013-09-11 14:20:37 -0700 | [diff] [blame] | 1276 | prefetchw(p); |
| 1277 | for (loop = 0; loop < (nr_pages - 1); loop++, p++) { |
| 1278 | prefetchw(p + 1); |
Johannes Weiner | c399307 | 2012-01-10 15:08:10 -0800 | [diff] [blame] | 1279 | __ClearPageReserved(p); |
| 1280 | set_page_count(p, 0); |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 1281 | } |
Yinghai Lu | e2d0bd2 | 2013-09-11 14:20:37 -0700 | [diff] [blame] | 1282 | __ClearPageReserved(p); |
| 1283 | set_page_count(p, 0); |
Johannes Weiner | c399307 | 2012-01-10 15:08:10 -0800 | [diff] [blame] | 1284 | |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 1285 | atomic_long_add(nr_pages, &page_zone(page)->managed_pages); |
Johannes Weiner | c399307 | 2012-01-10 15:08:10 -0800 | [diff] [blame] | 1286 | set_page_refcounted(page); |
| 1287 | __free_pages(page, order); |
David Howells | a226f6c | 2006-01-06 00:11:08 -0800 | [diff] [blame] | 1288 | } |
| 1289 | |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1290 | #if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \ |
| 1291 | defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) |
Mel Gorman | 7ace991 | 2015-08-06 15:46:13 -0700 | [diff] [blame] | 1292 | |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1293 | static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata; |
| 1294 | |
| 1295 | int __meminit early_pfn_to_nid(unsigned long pfn) |
| 1296 | { |
Mel Gorman | 7ace991 | 2015-08-06 15:46:13 -0700 | [diff] [blame] | 1297 | static DEFINE_SPINLOCK(early_pfn_lock); |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1298 | int nid; |
| 1299 | |
Mel Gorman | 7ace991 | 2015-08-06 15:46:13 -0700 | [diff] [blame] | 1300 | spin_lock(&early_pfn_lock); |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1301 | nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache); |
Mel Gorman | 7ace991 | 2015-08-06 15:46:13 -0700 | [diff] [blame] | 1302 | if (nid < 0) |
Mel Gorman | e4568d3 | 2016-07-14 12:07:20 -0700 | [diff] [blame] | 1303 | nid = first_online_node; |
Mel Gorman | 7ace991 | 2015-08-06 15:46:13 -0700 | [diff] [blame] | 1304 | spin_unlock(&early_pfn_lock); |
| 1305 | |
| 1306 | return nid; |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1307 | } |
| 1308 | #endif |
| 1309 | |
| 1310 | #ifdef CONFIG_NODES_SPAN_OTHER_NODES |
Matthias Kaehlcke | d73d3c9f | 2017-07-06 15:39:23 -0700 | [diff] [blame] | 1311 | static inline bool __meminit __maybe_unused |
| 1312 | meminit_pfn_in_nid(unsigned long pfn, int node, |
| 1313 | struct mminit_pfnnid_cache *state) |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1314 | { |
| 1315 | int nid; |
| 1316 | |
| 1317 | nid = __early_pfn_to_nid(pfn, state); |
| 1318 | if (nid >= 0 && nid != node) |
| 1319 | return false; |
| 1320 | return true; |
| 1321 | } |
| 1322 | |
| 1323 | /* Only safe to use early in boot when initialisation is single-threaded */ |
| 1324 | static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node) |
| 1325 | { |
| 1326 | return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache); |
| 1327 | } |
| 1328 | |
| 1329 | #else |
| 1330 | |
| 1331 | static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node) |
| 1332 | { |
| 1333 | return true; |
| 1334 | } |
Matthias Kaehlcke | d73d3c9f | 2017-07-06 15:39:23 -0700 | [diff] [blame] | 1335 | static inline bool __meminit __maybe_unused |
| 1336 | meminit_pfn_in_nid(unsigned long pfn, int node, |
| 1337 | struct mminit_pfnnid_cache *state) |
Mel Gorman | 75a592a | 2015-06-30 14:56:59 -0700 | [diff] [blame] | 1338 | { |
| 1339 | return true; |
| 1340 | } |
| 1341 | #endif |
| 1342 | |
| 1343 | |
Mike Rapoport | 7c2ee34 | 2018-10-30 15:09:36 -0700 | [diff] [blame] | 1344 | void __init memblock_free_pages(struct page *page, unsigned long pfn, |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 1345 | unsigned int order) |
| 1346 | { |
| 1347 | if (early_page_uninitialised(pfn)) |
| 1348 | return; |
Li Zhang | 949698a | 2016-05-19 17:11:37 -0700 | [diff] [blame] | 1349 | return __free_pages_boot_core(page, order); |
Mel Gorman | 3a80a7f | 2015-06-30 14:57:02 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1352 | /* |
| 1353 | * Check that the whole (or subset of) a pageblock given by the interval of |
| 1354 | * [start_pfn, end_pfn) is valid and within the same zone, before scanning it |
| 1355 | * with the migration of free compaction scanner. The scanners then need to |
| 1356 | * use only pfn_valid_within() check for arches that allow holes within |
| 1357 | * pageblocks. |
| 1358 | * |
| 1359 | * Return struct page pointer of start_pfn, or NULL if checks were not passed. |
| 1360 | * |
| 1361 | * It's possible on some configurations to have a setup like node0 node1 node0 |
| 1362 | * i.e. it's possible that all pages within a zones range of pages do not |
| 1363 | * belong to a single zone. We assume that a border between node0 and node1 |
| 1364 | * can occur within a single pageblock, but not a node0 node1 node0 |
| 1365 | * interleaving within a single pageblock. It is therefore sufficient to check |
| 1366 | * the first and last page of a pageblock and avoid checking each individual |
| 1367 | * page in a pageblock. |
| 1368 | */ |
| 1369 | struct page *__pageblock_pfn_to_page(unsigned long start_pfn, |
| 1370 | unsigned long end_pfn, struct zone *zone) |
| 1371 | { |
| 1372 | struct page *start_page; |
| 1373 | struct page *end_page; |
| 1374 | |
| 1375 | /* end_pfn is one past the range we are checking */ |
| 1376 | end_pfn--; |
| 1377 | |
| 1378 | if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) |
| 1379 | return NULL; |
| 1380 | |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 1381 | start_page = pfn_to_online_page(start_pfn); |
| 1382 | if (!start_page) |
| 1383 | return NULL; |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1384 | |
| 1385 | if (page_zone(start_page) != zone) |
| 1386 | return NULL; |
| 1387 | |
| 1388 | end_page = pfn_to_page(end_pfn); |
| 1389 | |
| 1390 | /* This gives a shorter code than deriving page_zone(end_page) */ |
| 1391 | if (page_zone_id(start_page) != page_zone_id(end_page)) |
| 1392 | return NULL; |
| 1393 | |
| 1394 | return start_page; |
| 1395 | } |
| 1396 | |
| 1397 | void set_zone_contiguous(struct zone *zone) |
| 1398 | { |
| 1399 | unsigned long block_start_pfn = zone->zone_start_pfn; |
| 1400 | unsigned long block_end_pfn; |
| 1401 | |
| 1402 | block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages); |
| 1403 | for (; block_start_pfn < zone_end_pfn(zone); |
| 1404 | block_start_pfn = block_end_pfn, |
| 1405 | block_end_pfn += pageblock_nr_pages) { |
| 1406 | |
| 1407 | block_end_pfn = min(block_end_pfn, zone_end_pfn(zone)); |
| 1408 | |
| 1409 | if (!__pageblock_pfn_to_page(block_start_pfn, |
| 1410 | block_end_pfn, zone)) |
| 1411 | return; |
| 1412 | } |
| 1413 | |
| 1414 | /* We confirm that there is no hole */ |
| 1415 | zone->contiguous = true; |
| 1416 | } |
| 1417 | |
| 1418 | void clear_zone_contiguous(struct zone *zone) |
| 1419 | { |
| 1420 | zone->contiguous = false; |
| 1421 | } |
| 1422 | |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1423 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1424 | static void __init deferred_free_range(unsigned long pfn, |
| 1425 | unsigned long nr_pages) |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1426 | { |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1427 | struct page *page; |
| 1428 | unsigned long i; |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1429 | |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1430 | if (!nr_pages) |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1431 | return; |
| 1432 | |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1433 | page = pfn_to_page(pfn); |
| 1434 | |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1435 | /* Free a large naturally-aligned chunk if possible */ |
Xishi Qiu | e780149 | 2016-10-07 16:58:09 -0700 | [diff] [blame] | 1436 | if (nr_pages == pageblock_nr_pages && |
| 1437 | (pfn & (pageblock_nr_pages - 1)) == 0) { |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 1438 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
Xishi Qiu | e780149 | 2016-10-07 16:58:09 -0700 | [diff] [blame] | 1439 | __free_pages_boot_core(page, pageblock_order); |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1440 | return; |
| 1441 | } |
| 1442 | |
Xishi Qiu | e780149 | 2016-10-07 16:58:09 -0700 | [diff] [blame] | 1443 | for (i = 0; i < nr_pages; i++, page++, pfn++) { |
| 1444 | if ((pfn & (pageblock_nr_pages - 1)) == 0) |
| 1445 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
Li Zhang | 949698a | 2016-05-19 17:11:37 -0700 | [diff] [blame] | 1446 | __free_pages_boot_core(page, 0); |
Xishi Qiu | e780149 | 2016-10-07 16:58:09 -0700 | [diff] [blame] | 1447 | } |
Mel Gorman | a4de83d | 2015-06-30 14:57:16 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Nicolai Stange | d3cd131 | 2015-08-06 15:46:16 -0700 | [diff] [blame] | 1450 | /* Completion tracking for deferred_init_memmap() threads */ |
| 1451 | static atomic_t pgdat_init_n_undone __initdata; |
| 1452 | static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp); |
| 1453 | |
| 1454 | static inline void __init pgdat_init_report_one_done(void) |
| 1455 | { |
| 1456 | if (atomic_dec_and_test(&pgdat_init_n_undone)) |
| 1457 | complete(&pgdat_init_all_done_comp); |
| 1458 | } |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1459 | |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1460 | /* |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1461 | * Returns true if page needs to be initialized or freed to buddy allocator. |
| 1462 | * |
| 1463 | * First we check if pfn is valid on architectures where it is possible to have |
| 1464 | * holes within pageblock_nr_pages. On systems where it is not possible, this |
| 1465 | * function is optimized out. |
| 1466 | * |
| 1467 | * Then, we check if a current large page is valid by only checking the validity |
| 1468 | * of the head pfn. |
| 1469 | * |
| 1470 | * Finally, meminit_pfn_in_nid is checked on systems where pfns can interleave |
| 1471 | * within a node: a pfn is between start and end of a node, but does not belong |
| 1472 | * to this memory node. |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1473 | */ |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1474 | static inline bool __init |
| 1475 | deferred_pfn_valid(int nid, unsigned long pfn, |
| 1476 | struct mminit_pfnnid_cache *nid_init_state) |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1477 | { |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1478 | if (!pfn_valid_within(pfn)) |
| 1479 | return false; |
| 1480 | if (!(pfn & (pageblock_nr_pages - 1)) && !pfn_valid(pfn)) |
| 1481 | return false; |
| 1482 | if (!meminit_pfn_in_nid(pfn, nid, nid_init_state)) |
| 1483 | return false; |
| 1484 | return true; |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1485 | } |
| 1486 | |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1487 | /* |
| 1488 | * Free pages to buddy allocator. Try to free aligned pages in |
| 1489 | * pageblock_nr_pages sizes. |
| 1490 | */ |
| 1491 | static void __init deferred_free_pages(int nid, int zid, unsigned long pfn, |
| 1492 | unsigned long end_pfn) |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1493 | { |
| 1494 | struct mminit_pfnnid_cache nid_init_state = { }; |
| 1495 | unsigned long nr_pgmask = pageblock_nr_pages - 1; |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1496 | unsigned long nr_free = 0; |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1497 | |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1498 | for (; pfn < end_pfn; pfn++) { |
| 1499 | if (!deferred_pfn_valid(nid, pfn, &nid_init_state)) { |
| 1500 | deferred_free_range(pfn - nr_free, nr_free); |
| 1501 | nr_free = 0; |
| 1502 | } else if (!(pfn & nr_pgmask)) { |
| 1503 | deferred_free_range(pfn - nr_free, nr_free); |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1504 | nr_free = 1; |
Pavel Tatashin | 3a2d7fa | 2018-04-05 16:22:27 -0700 | [diff] [blame] | 1505 | touch_nmi_watchdog(); |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1506 | } else { |
| 1507 | nr_free++; |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1508 | } |
| 1509 | } |
| 1510 | /* Free the last block of pages to allocator */ |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1511 | deferred_free_range(pfn - nr_free, nr_free); |
| 1512 | } |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1513 | |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1514 | /* |
| 1515 | * Initialize struct pages. We minimize pfn page lookups and scheduler checks |
| 1516 | * by performing it only once every pageblock_nr_pages. |
| 1517 | * Return number of pages initialized. |
| 1518 | */ |
| 1519 | static unsigned long __init deferred_init_pages(int nid, int zid, |
| 1520 | unsigned long pfn, |
| 1521 | unsigned long end_pfn) |
| 1522 | { |
| 1523 | struct mminit_pfnnid_cache nid_init_state = { }; |
| 1524 | unsigned long nr_pgmask = pageblock_nr_pages - 1; |
| 1525 | unsigned long nr_pages = 0; |
| 1526 | struct page *page = NULL; |
| 1527 | |
| 1528 | for (; pfn < end_pfn; pfn++) { |
| 1529 | if (!deferred_pfn_valid(nid, pfn, &nid_init_state)) { |
| 1530 | page = NULL; |
| 1531 | continue; |
| 1532 | } else if (!page || !(pfn & nr_pgmask)) { |
| 1533 | page = pfn_to_page(pfn); |
Pavel Tatashin | 3a2d7fa | 2018-04-05 16:22:27 -0700 | [diff] [blame] | 1534 | touch_nmi_watchdog(); |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1535 | } else { |
| 1536 | page++; |
| 1537 | } |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 1538 | __init_single_page(page, pfn, zid, nid); |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1539 | nr_pages++; |
| 1540 | } |
| 1541 | return (nr_pages); |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1542 | } |
| 1543 | |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1544 | /* Initialise remaining memory on a node */ |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1545 | static int __init deferred_init_memmap(void *data) |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1546 | { |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1547 | pg_data_t *pgdat = data; |
| 1548 | int nid = pgdat->node_id; |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1549 | unsigned long start = jiffies; |
| 1550 | unsigned long nr_pages = 0; |
Pavel Tatashin | 3a2d7fa | 2018-04-05 16:22:27 -0700 | [diff] [blame] | 1551 | unsigned long spfn, epfn, first_init_pfn, flags; |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1552 | phys_addr_t spa, epa; |
| 1553 | int zid; |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1554 | struct zone *zone; |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1555 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1556 | u64 i; |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1557 | |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1558 | /* Bind memory initialisation thread to a local node if possible */ |
| 1559 | if (!cpumask_empty(cpumask)) |
| 1560 | set_cpus_allowed_ptr(current, cpumask); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1561 | |
Pavel Tatashin | 3a2d7fa | 2018-04-05 16:22:27 -0700 | [diff] [blame] | 1562 | pgdat_resize_lock(pgdat, &flags); |
| 1563 | first_init_pfn = pgdat->first_deferred_pfn; |
| 1564 | if (first_init_pfn == ULONG_MAX) { |
| 1565 | pgdat_resize_unlock(pgdat, &flags); |
| 1566 | pgdat_init_report_one_done(); |
| 1567 | return 0; |
| 1568 | } |
| 1569 | |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1570 | /* Sanity check boundaries */ |
| 1571 | BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn); |
| 1572 | BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat)); |
| 1573 | pgdat->first_deferred_pfn = ULONG_MAX; |
| 1574 | |
| 1575 | /* Only the highest zone is deferred so find it */ |
| 1576 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| 1577 | zone = pgdat->node_zones + zid; |
| 1578 | if (first_init_pfn < zone_end_pfn(zone)) |
| 1579 | break; |
| 1580 | } |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1581 | first_init_pfn = max(zone->zone_start_pfn, first_init_pfn); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1582 | |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1583 | /* |
| 1584 | * Initialize and free pages. We do it in two loops: first we initialize |
| 1585 | * struct page, than free to buddy allocator, because while we are |
| 1586 | * freeing pages we can access pages that are ahead (computing buddy |
| 1587 | * page in __free_one_page()). |
| 1588 | */ |
Pavel Tatashin | 2f47a91 | 2017-11-15 17:36:09 -0800 | [diff] [blame] | 1589 | for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) { |
| 1590 | spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa)); |
| 1591 | epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa)); |
Pavel Tatashin | 80b1f41 | 2018-01-31 16:16:30 -0800 | [diff] [blame] | 1592 | nr_pages += deferred_init_pages(nid, zid, spfn, epfn); |
| 1593 | } |
| 1594 | for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) { |
| 1595 | spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa)); |
| 1596 | epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa)); |
| 1597 | deferred_free_pages(nid, zid, spfn, epfn); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1598 | } |
Pavel Tatashin | 3a2d7fa | 2018-04-05 16:22:27 -0700 | [diff] [blame] | 1599 | pgdat_resize_unlock(pgdat, &flags); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1600 | |
| 1601 | /* Sanity check that the next zone really is unpopulated */ |
| 1602 | WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone)); |
| 1603 | |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1604 | pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages, |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1605 | jiffies_to_msecs(jiffies - start)); |
Nicolai Stange | d3cd131 | 2015-08-06 15:46:16 -0700 | [diff] [blame] | 1606 | |
| 1607 | pgdat_init_report_one_done(); |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1608 | return 0; |
| 1609 | } |
Pavel Tatashin | c9e97a1 | 2018-04-05 16:22:31 -0700 | [diff] [blame] | 1610 | |
| 1611 | /* |
| 1612 | * During boot we initialize deferred pages on-demand, as needed, but once |
| 1613 | * page_alloc_init_late() has finished, the deferred pages are all initialized, |
| 1614 | * and we can permanently disable that path. |
| 1615 | */ |
| 1616 | static DEFINE_STATIC_KEY_TRUE(deferred_pages); |
| 1617 | |
| 1618 | /* |
| 1619 | * If this zone has deferred pages, try to grow it by initializing enough |
| 1620 | * deferred pages to satisfy the allocation specified by order, rounded up to |
| 1621 | * the nearest PAGES_PER_SECTION boundary. So we're adding memory in increments |
| 1622 | * of SECTION_SIZE bytes by initializing struct pages in increments of |
| 1623 | * PAGES_PER_SECTION * sizeof(struct page) bytes. |
| 1624 | * |
| 1625 | * Return true when zone was grown, otherwise return false. We return true even |
| 1626 | * when we grow less than requested, to let the caller decide if there are |
| 1627 | * enough pages to satisfy the allocation. |
| 1628 | * |
| 1629 | * Note: We use noinline because this function is needed only during boot, and |
| 1630 | * it is called from a __ref function _deferred_grow_zone. This way we are |
| 1631 | * making sure that it is not inlined into permanent text section. |
| 1632 | */ |
| 1633 | static noinline bool __init |
| 1634 | deferred_grow_zone(struct zone *zone, unsigned int order) |
| 1635 | { |
| 1636 | int zid = zone_idx(zone); |
| 1637 | int nid = zone_to_nid(zone); |
| 1638 | pg_data_t *pgdat = NODE_DATA(nid); |
| 1639 | unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION); |
| 1640 | unsigned long nr_pages = 0; |
| 1641 | unsigned long first_init_pfn, spfn, epfn, t, flags; |
| 1642 | unsigned long first_deferred_pfn = pgdat->first_deferred_pfn; |
| 1643 | phys_addr_t spa, epa; |
| 1644 | u64 i; |
| 1645 | |
| 1646 | /* Only the last zone may have deferred pages */ |
| 1647 | if (zone_end_pfn(zone) != pgdat_end_pfn(pgdat)) |
| 1648 | return false; |
| 1649 | |
| 1650 | pgdat_resize_lock(pgdat, &flags); |
| 1651 | |
| 1652 | /* |
| 1653 | * If deferred pages have been initialized while we were waiting for |
| 1654 | * the lock, return true, as the zone was grown. The caller will retry |
| 1655 | * this zone. We won't return to this function since the caller also |
| 1656 | * has this static branch. |
| 1657 | */ |
| 1658 | if (!static_branch_unlikely(&deferred_pages)) { |
| 1659 | pgdat_resize_unlock(pgdat, &flags); |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| 1663 | /* |
| 1664 | * If someone grew this zone while we were waiting for spinlock, return |
| 1665 | * true, as there might be enough pages already. |
| 1666 | */ |
| 1667 | if (first_deferred_pfn != pgdat->first_deferred_pfn) { |
| 1668 | pgdat_resize_unlock(pgdat, &flags); |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
| 1672 | first_init_pfn = max(zone->zone_start_pfn, first_deferred_pfn); |
| 1673 | |
| 1674 | if (first_init_pfn >= pgdat_end_pfn(pgdat)) { |
| 1675 | pgdat_resize_unlock(pgdat, &flags); |
| 1676 | return false; |
| 1677 | } |
| 1678 | |
| 1679 | for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) { |
| 1680 | spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa)); |
| 1681 | epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa)); |
| 1682 | |
| 1683 | while (spfn < epfn && nr_pages < nr_pages_needed) { |
| 1684 | t = ALIGN(spfn + PAGES_PER_SECTION, PAGES_PER_SECTION); |
| 1685 | first_deferred_pfn = min(t, epfn); |
| 1686 | nr_pages += deferred_init_pages(nid, zid, spfn, |
| 1687 | first_deferred_pfn); |
| 1688 | spfn = first_deferred_pfn; |
| 1689 | } |
| 1690 | |
| 1691 | if (nr_pages >= nr_pages_needed) |
| 1692 | break; |
| 1693 | } |
| 1694 | |
| 1695 | for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) { |
| 1696 | spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa)); |
| 1697 | epfn = min_t(unsigned long, first_deferred_pfn, PFN_DOWN(epa)); |
| 1698 | deferred_free_pages(nid, zid, spfn, epfn); |
| 1699 | |
| 1700 | if (first_deferred_pfn == epfn) |
| 1701 | break; |
| 1702 | } |
| 1703 | pgdat->first_deferred_pfn = first_deferred_pfn; |
| 1704 | pgdat_resize_unlock(pgdat, &flags); |
| 1705 | |
| 1706 | return nr_pages > 0; |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * deferred_grow_zone() is __init, but it is called from |
| 1711 | * get_page_from_freelist() during early boot until deferred_pages permanently |
| 1712 | * disables this call. This is why we have refdata wrapper to avoid warning, |
| 1713 | * and to ensure that the function body gets unloaded. |
| 1714 | */ |
| 1715 | static bool __ref |
| 1716 | _deferred_grow_zone(struct zone *zone, unsigned int order) |
| 1717 | { |
| 1718 | return deferred_grow_zone(zone, order); |
| 1719 | } |
| 1720 | |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1721 | #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1722 | |
| 1723 | void __init page_alloc_init_late(void) |
| 1724 | { |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1725 | struct zone *zone; |
| 1726 | |
| 1727 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1728 | int nid; |
| 1729 | |
Nicolai Stange | d3cd131 | 2015-08-06 15:46:16 -0700 | [diff] [blame] | 1730 | /* There will be num_node_state(N_MEMORY) threads */ |
| 1731 | atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY)); |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1732 | for_each_node_state(nid, N_MEMORY) { |
Mel Gorman | 0e1cc95 | 2015-06-30 14:57:27 -0700 | [diff] [blame] | 1733 | kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid); |
| 1734 | } |
| 1735 | |
| 1736 | /* Block until all are initialised */ |
Nicolai Stange | d3cd131 | 2015-08-06 15:46:16 -0700 | [diff] [blame] | 1737 | wait_for_completion(&pgdat_init_all_done_comp); |
Mel Gorman | 4248b0d | 2015-08-06 15:46:20 -0700 | [diff] [blame] | 1738 | |
Pavel Tatashin | c9e97a1 | 2018-04-05 16:22:31 -0700 | [diff] [blame] | 1739 | /* |
| 1740 | * We initialized the rest of the deferred pages. Permanently disable |
| 1741 | * on-demand struct page initialization. |
| 1742 | */ |
| 1743 | static_branch_disable(&deferred_pages); |
| 1744 | |
Mel Gorman | 4248b0d | 2015-08-06 15:46:20 -0700 | [diff] [blame] | 1745 | /* Reinit limits that are based on free pages after the kernel is up */ |
| 1746 | files_maxfiles_init(); |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1747 | #endif |
Pavel Tatashin | 3010f87 | 2017-08-18 15:16:05 -0700 | [diff] [blame] | 1748 | #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
| 1749 | /* Discard memblock private memory */ |
| 1750 | memblock_discard(); |
| 1751 | #endif |
Joonsoo Kim | 7cf91a9 | 2016-03-15 14:57:51 -0700 | [diff] [blame] | 1752 | |
| 1753 | for_each_populated_zone(zone) |
| 1754 | set_zone_contiguous(zone); |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1755 | } |
Mel Gorman | 7e18adb | 2015-06-30 14:57:05 -0700 | [diff] [blame] | 1756 | |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1757 | #ifdef CONFIG_CMA |
Li Zhong | 9cf510a | 2013-08-23 13:52:52 +0800 | [diff] [blame] | 1758 | /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1759 | void __init init_cma_reserved_pageblock(struct page *page) |
| 1760 | { |
| 1761 | unsigned i = pageblock_nr_pages; |
| 1762 | struct page *p = page; |
| 1763 | |
| 1764 | do { |
| 1765 | __ClearPageReserved(p); |
| 1766 | set_page_count(p, 0); |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 1767 | } while (++p, --i); |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1768 | |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1769 | set_pageblock_migratetype(page, MIGRATE_CMA); |
Michal Nazarewicz | dc78327 | 2014-07-02 15:22:35 -0700 | [diff] [blame] | 1770 | |
| 1771 | if (pageblock_order >= MAX_ORDER) { |
| 1772 | i = pageblock_nr_pages; |
| 1773 | p = page; |
| 1774 | do { |
| 1775 | set_page_refcounted(p); |
| 1776 | __free_pages(p, MAX_ORDER - 1); |
| 1777 | p += MAX_ORDER_NR_PAGES; |
| 1778 | } while (i -= MAX_ORDER_NR_PAGES); |
| 1779 | } else { |
| 1780 | set_page_refcounted(page); |
| 1781 | __free_pages(page, pageblock_order); |
| 1782 | } |
| 1783 | |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 1784 | adjust_managed_page_count(page, pageblock_nr_pages); |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1785 | } |
| 1786 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | /* |
| 1789 | * The order of subdivision here is critical for the IO subsystem. |
| 1790 | * Please do not alter this order without good reasons and regression |
| 1791 | * testing. Specifically, as large blocks of memory are subdivided, |
| 1792 | * the order in which smaller blocks are delivered depends on the order |
| 1793 | * they're subdivided in this function. This is the primary factor |
| 1794 | * influencing the order in which pages are delivered to the IO |
| 1795 | * subsystem according to empirical testing, and this is also justified |
| 1796 | * by considering the behavior of a buddy system containing a single |
| 1797 | * large block of memory acted on by a series of small allocations. |
| 1798 | * This behavior is a critical factor in sglist merging's success. |
| 1799 | * |
Nadia Yvette Chambers | 6d49e35 | 2012-12-06 10:39:54 +0100 | [diff] [blame] | 1800 | * -- nyc |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | */ |
Nick Piggin | 085cc7d5 | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 1802 | static inline void expand(struct zone *zone, struct page *page, |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 1803 | int low, int high, struct free_area *area, |
| 1804 | int migratetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | { |
| 1806 | unsigned long size = 1 << high; |
| 1807 | |
| 1808 | while (high > low) { |
| 1809 | area--; |
| 1810 | high--; |
| 1811 | size >>= 1; |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 1812 | VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 1813 | |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 1814 | /* |
| 1815 | * Mark as guard pages (or page), that will allow to |
| 1816 | * merge back to allocator when buddy will be freed. |
| 1817 | * Corresponding page table entries will not be touched, |
| 1818 | * pages will stay not present in virtual address space |
| 1819 | */ |
| 1820 | if (set_page_guard(zone, &page[size], high, migratetype)) |
Stanislaw Gruszka | c0a32fc | 2012-01-10 15:07:28 -0800 | [diff] [blame] | 1821 | continue; |
Joonsoo Kim | acbc15a | 2016-10-07 16:58:15 -0700 | [diff] [blame] | 1822 | |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 1823 | list_add(&page[size].lru, &area->free_list[migratetype]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | area->nr_free++; |
| 1825 | set_page_order(&page[size], high); |
| 1826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Vlastimil Babka | 4e61180 | 2016-05-19 17:14:41 -0700 | [diff] [blame] | 1829 | static void check_new_page_bad(struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | { |
Vlastimil Babka | 4e61180 | 2016-05-19 17:14:41 -0700 | [diff] [blame] | 1831 | const char *bad_reason = NULL; |
| 1832 | unsigned long bad_flags = 0; |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1833 | |
Kirill A. Shutemov | 53f9263 | 2016-01-15 16:53:42 -0800 | [diff] [blame] | 1834 | if (unlikely(atomic_read(&page->_mapcount) != -1)) |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1835 | bad_reason = "nonzero mapcount"; |
| 1836 | if (unlikely(page->mapping != NULL)) |
| 1837 | bad_reason = "non-NULL mapping"; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 1838 | if (unlikely(page_ref_count(page) != 0)) |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1839 | bad_reason = "nonzero _count"; |
Naoya Horiguchi | f4c18e6 | 2015-08-06 15:47:08 -0700 | [diff] [blame] | 1840 | if (unlikely(page->flags & __PG_HWPOISON)) { |
| 1841 | bad_reason = "HWPoisoned (hardware-corrupted)"; |
| 1842 | bad_flags = __PG_HWPOISON; |
Naoya Horiguchi | e570f56 | 2016-05-20 16:58:50 -0700 | [diff] [blame] | 1843 | /* Don't complain about hwpoisoned pages */ |
| 1844 | page_mapcount_reset(page); /* remove PageBuddy */ |
| 1845 | return; |
Naoya Horiguchi | f4c18e6 | 2015-08-06 15:47:08 -0700 | [diff] [blame] | 1846 | } |
Dave Hansen | f0b791a | 2014-01-23 15:52:49 -0800 | [diff] [blame] | 1847 | if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) { |
| 1848 | bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set"; |
| 1849 | bad_flags = PAGE_FLAGS_CHECK_AT_PREP; |
| 1850 | } |
Johannes Weiner | 9edad6e | 2014-12-10 15:44:58 -0800 | [diff] [blame] | 1851 | #ifdef CONFIG_MEMCG |
| 1852 | if (unlikely(page->mem_cgroup)) |
| 1853 | bad_reason = "page still charged to cgroup"; |
| 1854 | #endif |
Vlastimil Babka | 4e61180 | 2016-05-19 17:14:41 -0700 | [diff] [blame] | 1855 | bad_page(page, bad_reason, bad_flags); |
| 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * This page is about to be returned from the page allocator |
| 1860 | */ |
| 1861 | static inline int check_new_page(struct page *page) |
| 1862 | { |
| 1863 | if (likely(page_expected_state(page, |
| 1864 | PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON))) |
| 1865 | return 0; |
| 1866 | |
| 1867 | check_new_page_bad(page); |
| 1868 | return 1; |
Wu Fengguang | 2a7684a | 2009-09-16 11:50:12 +0200 | [diff] [blame] | 1869 | } |
| 1870 | |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 1871 | static inline bool free_pages_prezeroed(void) |
Laura Abbott | 1414c7f | 2016-03-15 14:56:30 -0700 | [diff] [blame] | 1872 | { |
| 1873 | return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) && |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 1874 | page_poisoning_enabled(); |
Laura Abbott | 1414c7f | 2016-03-15 14:56:30 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
Mel Gorman | 479f854 | 2016-05-19 17:14:35 -0700 | [diff] [blame] | 1877 | #ifdef CONFIG_DEBUG_VM |
| 1878 | static bool check_pcp_refill(struct page *page) |
| 1879 | { |
| 1880 | return false; |
| 1881 | } |
| 1882 | |
| 1883 | static bool check_new_pcp(struct page *page) |
| 1884 | { |
| 1885 | return check_new_page(page); |
| 1886 | } |
| 1887 | #else |
| 1888 | static bool check_pcp_refill(struct page *page) |
| 1889 | { |
| 1890 | return check_new_page(page); |
| 1891 | } |
| 1892 | static bool check_new_pcp(struct page *page) |
| 1893 | { |
| 1894 | return false; |
| 1895 | } |
| 1896 | #endif /* CONFIG_DEBUG_VM */ |
| 1897 | |
| 1898 | static bool check_new_pages(struct page *page, unsigned int order) |
| 1899 | { |
| 1900 | int i; |
| 1901 | for (i = 0; i < (1 << order); i++) { |
| 1902 | struct page *p = page + i; |
| 1903 | |
| 1904 | if (unlikely(check_new_page(p))) |
| 1905 | return true; |
| 1906 | } |
| 1907 | |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
Joonsoo Kim | 46f24fd | 2016-07-26 15:23:58 -0700 | [diff] [blame] | 1911 | inline void post_alloc_hook(struct page *page, unsigned int order, |
| 1912 | gfp_t gfp_flags) |
| 1913 | { |
| 1914 | set_page_private(page, 0); |
| 1915 | set_page_refcounted(page); |
| 1916 | |
| 1917 | arch_alloc_page(page, order); |
| 1918 | kernel_map_pages(page, 1 << order, 1); |
| 1919 | kernel_poison_pages(page, 1 << order, 1); |
| 1920 | kasan_alloc_pages(page, order); |
| 1921 | set_page_owner(page, order, gfp_flags); |
| 1922 | } |
| 1923 | |
Mel Gorman | 479f854 | 2016-05-19 17:14:35 -0700 | [diff] [blame] | 1924 | static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 1925 | unsigned int alloc_flags) |
Wu Fengguang | 2a7684a | 2009-09-16 11:50:12 +0200 | [diff] [blame] | 1926 | { |
| 1927 | int i; |
Hugh Dickins | 689bceb | 2005-11-21 21:32:20 -0800 | [diff] [blame] | 1928 | |
Joonsoo Kim | 46f24fd | 2016-07-26 15:23:58 -0700 | [diff] [blame] | 1929 | post_alloc_hook(page, order, gfp_flags); |
Nick Piggin | 17cf440 | 2006-03-22 00:08:41 -0800 | [diff] [blame] | 1930 | |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 1931 | if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO)) |
Anisse Astier | f4d2897 | 2015-06-24 16:56:36 -0700 | [diff] [blame] | 1932 | for (i = 0; i < (1 << order); i++) |
| 1933 | clear_highpage(page + i); |
Nick Piggin | 17cf440 | 2006-03-22 00:08:41 -0800 | [diff] [blame] | 1934 | |
| 1935 | if (order && (gfp_flags & __GFP_COMP)) |
| 1936 | prep_compound_page(page, order); |
| 1937 | |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 1938 | /* |
Michal Hocko | 2f064f3 | 2015-08-21 14:11:51 -0700 | [diff] [blame] | 1939 | * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 1940 | * allocate the page. The expectation is that the caller is taking |
| 1941 | * steps that will free more memory. The caller should avoid the page |
| 1942 | * being used for !PFMEMALLOC purposes. |
| 1943 | */ |
Michal Hocko | 2f064f3 | 2015-08-21 14:11:51 -0700 | [diff] [blame] | 1944 | if (alloc_flags & ALLOC_NO_WATERMARKS) |
| 1945 | set_page_pfmemalloc(page); |
| 1946 | else |
| 1947 | clear_page_pfmemalloc(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1950 | /* |
| 1951 | * Go through the free lists for the given migratetype and remove |
| 1952 | * the smallest available page from the freelists |
| 1953 | */ |
Aaron Lu | 85ccc8f | 2017-11-15 17:36:53 -0800 | [diff] [blame] | 1954 | static __always_inline |
Mel Gorman | 728ec98 | 2009-06-16 15:32:04 -0700 | [diff] [blame] | 1955 | struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1956 | int migratetype) |
| 1957 | { |
| 1958 | unsigned int current_order; |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 1959 | struct free_area *area; |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1960 | struct page *page; |
| 1961 | |
| 1962 | /* Find a page of the appropriate size in the preferred list */ |
| 1963 | for (current_order = order; current_order < MAX_ORDER; ++current_order) { |
| 1964 | area = &(zone->free_area[current_order]); |
Geliang Tang | a16601c | 2016-01-14 15:20:30 -0800 | [diff] [blame] | 1965 | page = list_first_entry_or_null(&area->free_list[migratetype], |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1966 | struct page, lru); |
Geliang Tang | a16601c | 2016-01-14 15:20:30 -0800 | [diff] [blame] | 1967 | if (!page) |
| 1968 | continue; |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1969 | list_del(&page->lru); |
| 1970 | rmv_page_order(page); |
| 1971 | area->nr_free--; |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1972 | expand(zone, page, order, current_order, area, migratetype); |
Vlastimil Babka | bb14c2c | 2015-09-08 15:01:25 -0700 | [diff] [blame] | 1973 | set_pcppage_migratetype(page, migratetype); |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 1974 | return page; |
| 1975 | } |
| 1976 | |
| 1977 | return NULL; |
| 1978 | } |
| 1979 | |
| 1980 | |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 1981 | /* |
| 1982 | * This array describes the order lists are fallen back to when |
| 1983 | * the free lists for the desirable migrate type are depleted |
| 1984 | */ |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1985 | static int fallbacks[MIGRATE_TYPES][4] = { |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 1986 | [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
| 1987 | [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
| 1988 | [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES }, |
Joonsoo Kim | dc67647 | 2015-04-14 15:45:15 -0700 | [diff] [blame] | 1989 | #ifdef CONFIG_CMA |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 1990 | [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */ |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 1991 | #endif |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 1992 | #ifdef CONFIG_MEMORY_ISOLATION |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 1993 | [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */ |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 1994 | #endif |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 1995 | }; |
| 1996 | |
Joonsoo Kim | dc67647 | 2015-04-14 15:45:15 -0700 | [diff] [blame] | 1997 | #ifdef CONFIG_CMA |
Aaron Lu | 85ccc8f | 2017-11-15 17:36:53 -0800 | [diff] [blame] | 1998 | static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone, |
Joonsoo Kim | dc67647 | 2015-04-14 15:45:15 -0700 | [diff] [blame] | 1999 | unsigned int order) |
| 2000 | { |
| 2001 | return __rmqueue_smallest(zone, order, MIGRATE_CMA); |
| 2002 | } |
| 2003 | #else |
| 2004 | static inline struct page *__rmqueue_cma_fallback(struct zone *zone, |
| 2005 | unsigned int order) { return NULL; } |
| 2006 | #endif |
| 2007 | |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2008 | /* |
| 2009 | * Move the free pages in a range to the free lists of the requested type. |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 2010 | * Note that start_page and end_pages are not aligned on a pageblock |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2011 | * boundary. If alignment is required, use move_freepages_block() |
| 2012 | */ |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2013 | static int move_freepages(struct zone *zone, |
Adrian Bunk | b69a728 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 2014 | struct page *start_page, struct page *end_page, |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2015 | int migratetype, int *num_movable) |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2016 | { |
| 2017 | struct page *page; |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 2018 | unsigned int order; |
Mel Gorman | d100313 | 2007-10-16 01:26:00 -0700 | [diff] [blame] | 2019 | int pages_moved = 0; |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2020 | |
| 2021 | #ifndef CONFIG_HOLES_IN_ZONE |
| 2022 | /* |
| 2023 | * page_zone is not safe to call in this context when |
| 2024 | * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant |
| 2025 | * anyway as we check zone boundaries in move_freepages_block(). |
| 2026 | * Remove at a later date when no bug reports exist related to |
Mel Gorman | ac0e5b7 | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 2027 | * grouping pages by mobility |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2028 | */ |
Ard Biesheuvel | 3e04040 | 2018-03-14 19:29:37 +0000 | [diff] [blame] | 2029 | VM_BUG_ON(pfn_valid(page_to_pfn(start_page)) && |
| 2030 | pfn_valid(page_to_pfn(end_page)) && |
| 2031 | page_zone(start_page) != page_zone(end_page)); |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2032 | #endif |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2033 | for (page = start_page; page <= end_page;) { |
| 2034 | if (!pfn_valid_within(page_to_pfn(page))) { |
| 2035 | page++; |
| 2036 | continue; |
| 2037 | } |
| 2038 | |
Ard Biesheuvel | f073bdc | 2017-01-10 16:58:00 -0800 | [diff] [blame] | 2039 | /* Make sure we are not inadvertently changing nodes */ |
| 2040 | VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page); |
| 2041 | |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2042 | if (!PageBuddy(page)) { |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2043 | /* |
| 2044 | * We assume that pages that could be isolated for |
| 2045 | * migration are movable. But we don't actually try |
| 2046 | * isolating, as that would be expensive. |
| 2047 | */ |
| 2048 | if (num_movable && |
| 2049 | (PageLRU(page) || __PageMovable(page))) |
| 2050 | (*num_movable)++; |
| 2051 | |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2052 | page++; |
| 2053 | continue; |
| 2054 | } |
| 2055 | |
| 2056 | order = page_order(page); |
Kirill A. Shutemov | 84be48d | 2011-03-22 16:33:41 -0700 | [diff] [blame] | 2057 | list_move(&page->lru, |
| 2058 | &zone->free_area[order].free_list[migratetype]); |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2059 | page += 1 << order; |
Mel Gorman | d100313 | 2007-10-16 01:26:00 -0700 | [diff] [blame] | 2060 | pages_moved += 1 << order; |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2061 | } |
| 2062 | |
Mel Gorman | d100313 | 2007-10-16 01:26:00 -0700 | [diff] [blame] | 2063 | return pages_moved; |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Minchan Kim | ee6f509 | 2012-07-31 16:43:50 -0700 | [diff] [blame] | 2066 | int move_freepages_block(struct zone *zone, struct page *page, |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2067 | int migratetype, int *num_movable) |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2068 | { |
| 2069 | unsigned long start_pfn, end_pfn; |
| 2070 | struct page *start_page, *end_page; |
| 2071 | |
David Rientjes | 4a22212 | 2018-10-26 15:09:24 -0700 | [diff] [blame] | 2072 | if (num_movable) |
| 2073 | *num_movable = 0; |
| 2074 | |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2075 | start_pfn = page_to_pfn(page); |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 2076 | start_pfn = start_pfn & ~(pageblock_nr_pages-1); |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2077 | start_page = pfn_to_page(start_pfn); |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 2078 | end_page = start_page + pageblock_nr_pages - 1; |
| 2079 | end_pfn = start_pfn + pageblock_nr_pages - 1; |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2080 | |
| 2081 | /* Do not cross zone boundaries */ |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 2082 | if (!zone_spans_pfn(zone, start_pfn)) |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2083 | start_page = page; |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 2084 | if (!zone_spans_pfn(zone, end_pfn)) |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2085 | return 0; |
| 2086 | |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2087 | return move_freepages(zone, start_page, end_page, migratetype, |
| 2088 | num_movable); |
Mel Gorman | c361be5 | 2007-10-16 01:25:51 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Mel Gorman | 2f66a68 | 2009-09-21 17:02:31 -0700 | [diff] [blame] | 2091 | static void change_pageblock_range(struct page *pageblock_page, |
| 2092 | int start_order, int migratetype) |
| 2093 | { |
| 2094 | int nr_pageblocks = 1 << (start_order - pageblock_order); |
| 2095 | |
| 2096 | while (nr_pageblocks--) { |
| 2097 | set_pageblock_migratetype(pageblock_page, migratetype); |
| 2098 | pageblock_page += pageblock_nr_pages; |
| 2099 | } |
| 2100 | } |
| 2101 | |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2102 | /* |
Vlastimil Babka | 9c0415e | 2015-02-11 15:28:21 -0800 | [diff] [blame] | 2103 | * When we are falling back to another migratetype during allocation, try to |
| 2104 | * steal extra free pages from the same pageblocks to satisfy further |
| 2105 | * allocations, instead of polluting multiple pageblocks. |
| 2106 | * |
| 2107 | * If we are stealing a relatively large buddy page, it is likely there will |
| 2108 | * be more free pages in the pageblock, so try to steal them all. For |
| 2109 | * reclaimable and unmovable allocations, we steal regardless of page size, |
| 2110 | * as fragmentation caused by those allocations polluting movable pageblocks |
| 2111 | * is worse than movable allocations stealing from unmovable and reclaimable |
| 2112 | * pageblocks. |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2113 | */ |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2114 | static bool can_steal_fallback(unsigned int order, int start_mt) |
| 2115 | { |
| 2116 | /* |
| 2117 | * Leaving this order check is intended, although there is |
| 2118 | * relaxed order check in next check. The reason is that |
| 2119 | * we can actually steal whole pageblock if this condition met, |
| 2120 | * but, below check doesn't guarantee it and that is just heuristic |
| 2121 | * so could be changed anytime. |
| 2122 | */ |
| 2123 | if (order >= pageblock_order) |
| 2124 | return true; |
| 2125 | |
| 2126 | if (order >= pageblock_order / 2 || |
| 2127 | start_mt == MIGRATE_RECLAIMABLE || |
| 2128 | start_mt == MIGRATE_UNMOVABLE || |
| 2129 | page_group_by_mobility_disabled) |
| 2130 | return true; |
| 2131 | |
| 2132 | return false; |
| 2133 | } |
| 2134 | |
| 2135 | /* |
| 2136 | * This function implements actual steal behaviour. If order is large enough, |
| 2137 | * we can steal whole pageblock. If not, we first move freepages in this |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2138 | * pageblock to our migratetype and determine how many already-allocated pages |
| 2139 | * are there in the pageblock with a compatible migratetype. If at least half |
| 2140 | * of pages are free or compatible, we can change migratetype of the pageblock |
| 2141 | * itself, so pages freed in the future will be put on the correct free list. |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2142 | */ |
| 2143 | static void steal_suitable_fallback(struct zone *zone, struct page *page, |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2144 | int start_type, bool whole_block) |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2145 | { |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 2146 | unsigned int current_order = page_order(page); |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2147 | struct free_area *area; |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2148 | int free_pages, movable_pages, alike_pages; |
| 2149 | int old_block_type; |
| 2150 | |
| 2151 | old_block_type = get_pageblock_migratetype(page); |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2152 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2153 | /* |
| 2154 | * This can happen due to races and we want to prevent broken |
| 2155 | * highatomic accounting. |
| 2156 | */ |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2157 | if (is_migrate_highatomic(old_block_type)) |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2158 | goto single_page; |
| 2159 | |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2160 | /* Take ownership for orders >= pageblock_order */ |
| 2161 | if (current_order >= pageblock_order) { |
| 2162 | change_pageblock_range(page, current_order, start_type); |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2163 | goto single_page; |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2164 | } |
| 2165 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2166 | /* We are not allowed to try stealing from the whole block */ |
| 2167 | if (!whole_block) |
| 2168 | goto single_page; |
| 2169 | |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2170 | free_pages = move_freepages_block(zone, page, start_type, |
| 2171 | &movable_pages); |
| 2172 | /* |
| 2173 | * Determine how many pages are compatible with our allocation. |
| 2174 | * For movable allocation, it's the number of movable pages which |
| 2175 | * we just obtained. For other types it's a bit more tricky. |
| 2176 | */ |
| 2177 | if (start_type == MIGRATE_MOVABLE) { |
| 2178 | alike_pages = movable_pages; |
| 2179 | } else { |
| 2180 | /* |
| 2181 | * If we are falling back a RECLAIMABLE or UNMOVABLE allocation |
| 2182 | * to MOVABLE pageblock, consider all non-movable pages as |
| 2183 | * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or |
| 2184 | * vice versa, be conservative since we can't distinguish the |
| 2185 | * exact migratetype of non-movable pages. |
| 2186 | */ |
| 2187 | if (old_block_type == MIGRATE_MOVABLE) |
| 2188 | alike_pages = pageblock_nr_pages |
| 2189 | - (free_pages + movable_pages); |
| 2190 | else |
| 2191 | alike_pages = 0; |
| 2192 | } |
| 2193 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2194 | /* moving whole block can fail due to zone boundary conditions */ |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2195 | if (!free_pages) |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2196 | goto single_page; |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2197 | |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2198 | /* |
| 2199 | * If a sufficient number of pages in the block are either free or of |
| 2200 | * comparable migratability as our allocation, claim the whole block. |
| 2201 | */ |
| 2202 | if (free_pages + alike_pages >= (1 << (pageblock_order-1)) || |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2203 | page_group_by_mobility_disabled) |
| 2204 | set_pageblock_migratetype(page, start_type); |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2205 | |
| 2206 | return; |
| 2207 | |
| 2208 | single_page: |
| 2209 | area = &zone->free_area[current_order]; |
| 2210 | list_move(&page->lru, &area->free_list[start_type]); |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2211 | } |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2212 | |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2213 | /* |
| 2214 | * Check whether there is a suitable fallback freepage with requested order. |
| 2215 | * If only_stealable is true, this function returns fallback_mt only if |
| 2216 | * we can steal other freepages all together. This would help to reduce |
| 2217 | * fragmentation due to mixed migratetype pages in one pageblock. |
| 2218 | */ |
| 2219 | int find_suitable_fallback(struct free_area *area, unsigned int order, |
| 2220 | int migratetype, bool only_stealable, bool *can_steal) |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2221 | { |
| 2222 | int i; |
| 2223 | int fallback_mt; |
| 2224 | |
| 2225 | if (area->nr_free == 0) |
| 2226 | return -1; |
| 2227 | |
| 2228 | *can_steal = false; |
| 2229 | for (i = 0;; i++) { |
| 2230 | fallback_mt = fallbacks[migratetype][i]; |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 2231 | if (fallback_mt == MIGRATE_TYPES) |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2232 | break; |
| 2233 | |
| 2234 | if (list_empty(&area->free_list[fallback_mt])) |
| 2235 | continue; |
| 2236 | |
| 2237 | if (can_steal_fallback(order, migratetype)) |
| 2238 | *can_steal = true; |
| 2239 | |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2240 | if (!only_stealable) |
| 2241 | return fallback_mt; |
| 2242 | |
| 2243 | if (*can_steal) |
| 2244 | return fallback_mt; |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2245 | } |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2246 | |
| 2247 | return -1; |
Srivatsa S. Bhat | fef903e | 2013-09-11 14:20:35 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2250 | /* |
| 2251 | * Reserve a pageblock for exclusive use of high-order atomic allocations if |
| 2252 | * there are no empty page blocks that contain a page with a suitable order |
| 2253 | */ |
| 2254 | static void reserve_highatomic_pageblock(struct page *page, struct zone *zone, |
| 2255 | unsigned int alloc_order) |
| 2256 | { |
| 2257 | int mt; |
| 2258 | unsigned long max_managed, flags; |
| 2259 | |
| 2260 | /* |
| 2261 | * Limit the number reserved to 1 pageblock or roughly 1% of a zone. |
| 2262 | * Check is race-prone but harmless. |
| 2263 | */ |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 2264 | max_managed = (zone_managed_pages(zone) / 100) + pageblock_nr_pages; |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2265 | if (zone->nr_reserved_highatomic >= max_managed) |
| 2266 | return; |
| 2267 | |
| 2268 | spin_lock_irqsave(&zone->lock, flags); |
| 2269 | |
| 2270 | /* Recheck the nr_reserved_highatomic limit under the lock */ |
| 2271 | if (zone->nr_reserved_highatomic >= max_managed) |
| 2272 | goto out_unlock; |
| 2273 | |
| 2274 | /* Yoink! */ |
| 2275 | mt = get_pageblock_migratetype(page); |
Xishi Qiu | a6ffdc0 | 2017-05-03 14:52:52 -0700 | [diff] [blame] | 2276 | if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt) |
| 2277 | && !is_migrate_cma(mt)) { |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2278 | zone->nr_reserved_highatomic += pageblock_nr_pages; |
| 2279 | set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2280 | move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL); |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | out_unlock: |
| 2284 | spin_unlock_irqrestore(&zone->lock, flags); |
| 2285 | } |
| 2286 | |
| 2287 | /* |
| 2288 | * Used when an allocation is about to fail under memory pressure. This |
| 2289 | * potentially hurts the reliability of high-order allocations when under |
| 2290 | * intense memory pressure but failed atomic allocations should be easier |
| 2291 | * to recover from than an OOM. |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 2292 | * |
| 2293 | * If @force is true, try to unreserve a pageblock even though highatomic |
| 2294 | * pageblock is exhausted. |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2295 | */ |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 2296 | static bool unreserve_highatomic_pageblock(const struct alloc_context *ac, |
| 2297 | bool force) |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2298 | { |
| 2299 | struct zonelist *zonelist = ac->zonelist; |
| 2300 | unsigned long flags; |
| 2301 | struct zoneref *z; |
| 2302 | struct zone *zone; |
| 2303 | struct page *page; |
| 2304 | int order; |
Minchan Kim | 04c8716 | 2016-12-12 16:42:11 -0800 | [diff] [blame] | 2305 | bool ret; |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2306 | |
| 2307 | for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx, |
| 2308 | ac->nodemask) { |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 2309 | /* |
| 2310 | * Preserve at least one pageblock unless memory pressure |
| 2311 | * is really high. |
| 2312 | */ |
| 2313 | if (!force && zone->nr_reserved_highatomic <= |
| 2314 | pageblock_nr_pages) |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2315 | continue; |
| 2316 | |
| 2317 | spin_lock_irqsave(&zone->lock, flags); |
| 2318 | for (order = 0; order < MAX_ORDER; order++) { |
| 2319 | struct free_area *area = &(zone->free_area[order]); |
| 2320 | |
Geliang Tang | a16601c | 2016-01-14 15:20:30 -0800 | [diff] [blame] | 2321 | page = list_first_entry_or_null( |
| 2322 | &area->free_list[MIGRATE_HIGHATOMIC], |
| 2323 | struct page, lru); |
| 2324 | if (!page) |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2325 | continue; |
| 2326 | |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2327 | /* |
Minchan Kim | 4855e4a | 2016-12-12 16:42:08 -0800 | [diff] [blame] | 2328 | * In page freeing path, migratetype change is racy so |
| 2329 | * we can counter several free pages in a pageblock |
| 2330 | * in this loop althoug we changed the pageblock type |
| 2331 | * from highatomic to ac->migratetype. So we should |
| 2332 | * adjust the count once. |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2333 | */ |
Xishi Qiu | a6ffdc0 | 2017-05-03 14:52:52 -0700 | [diff] [blame] | 2334 | if (is_migrate_highatomic_page(page)) { |
Minchan Kim | 4855e4a | 2016-12-12 16:42:08 -0800 | [diff] [blame] | 2335 | /* |
| 2336 | * It should never happen but changes to |
| 2337 | * locking could inadvertently allow a per-cpu |
| 2338 | * drain to add pages to MIGRATE_HIGHATOMIC |
| 2339 | * while unreserving so be safe and watch for |
| 2340 | * underflows. |
| 2341 | */ |
| 2342 | zone->nr_reserved_highatomic -= min( |
| 2343 | pageblock_nr_pages, |
| 2344 | zone->nr_reserved_highatomic); |
| 2345 | } |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2346 | |
| 2347 | /* |
| 2348 | * Convert to ac->migratetype and avoid the normal |
| 2349 | * pageblock stealing heuristics. Minimally, the caller |
| 2350 | * is doing the work and needs the pages. More |
| 2351 | * importantly, if the block was always converted to |
| 2352 | * MIGRATE_UNMOVABLE or another type then the number |
| 2353 | * of pageblocks that cannot be completely freed |
| 2354 | * may increase. |
| 2355 | */ |
| 2356 | set_pageblock_migratetype(page, ac->migratetype); |
Vlastimil Babka | 02aa0cd | 2017-05-08 15:54:40 -0700 | [diff] [blame] | 2357 | ret = move_freepages_block(zone, page, ac->migratetype, |
| 2358 | NULL); |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 2359 | if (ret) { |
| 2360 | spin_unlock_irqrestore(&zone->lock, flags); |
| 2361 | return ret; |
| 2362 | } |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2363 | } |
| 2364 | spin_unlock_irqrestore(&zone->lock, flags); |
| 2365 | } |
Minchan Kim | 04c8716 | 2016-12-12 16:42:11 -0800 | [diff] [blame] | 2366 | |
| 2367 | return false; |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 2368 | } |
| 2369 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2370 | /* |
| 2371 | * Try finding a free buddy page on the fallback list and put it on the free |
| 2372 | * list of requested migratetype, possibly along with other pages from the same |
| 2373 | * block, depending on fragmentation avoidance heuristics. Returns true if |
| 2374 | * fallback was found so that __rmqueue_smallest() can grab it. |
Rasmus Villemoes | b002529 | 2017-07-10 15:49:26 -0700 | [diff] [blame] | 2375 | * |
| 2376 | * The use of signed ints for order and current_order is a deliberate |
| 2377 | * deviation from the rest of this file, to make the for loop |
| 2378 | * condition simpler. |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2379 | */ |
Aaron Lu | 85ccc8f | 2017-11-15 17:36:53 -0800 | [diff] [blame] | 2380 | static __always_inline bool |
Rasmus Villemoes | b002529 | 2017-07-10 15:49:26 -0700 | [diff] [blame] | 2381 | __rmqueue_fallback(struct zone *zone, int order, int start_migratetype) |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2382 | { |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 2383 | struct free_area *area; |
Rasmus Villemoes | b002529 | 2017-07-10 15:49:26 -0700 | [diff] [blame] | 2384 | int current_order; |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2385 | struct page *page; |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2386 | int fallback_mt; |
| 2387 | bool can_steal; |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2388 | |
Vlastimil Babka | 7a8f58f | 2017-07-10 15:47:14 -0700 | [diff] [blame] | 2389 | /* |
| 2390 | * Find the largest available free page in the other list. This roughly |
| 2391 | * approximates finding the pageblock with the most free pages, which |
| 2392 | * would be too costly to do exactly. |
| 2393 | */ |
Rasmus Villemoes | b002529 | 2017-07-10 15:49:26 -0700 | [diff] [blame] | 2394 | for (current_order = MAX_ORDER - 1; current_order >= order; |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 2395 | --current_order) { |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2396 | area = &(zone->free_area[current_order]); |
| 2397 | fallback_mt = find_suitable_fallback(area, current_order, |
Joonsoo Kim | 2149cda | 2015-04-14 15:45:21 -0700 | [diff] [blame] | 2398 | start_migratetype, false, &can_steal); |
Joonsoo Kim | 4eb7dce | 2015-04-14 15:45:18 -0700 | [diff] [blame] | 2399 | if (fallback_mt == -1) |
| 2400 | continue; |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2401 | |
Vlastimil Babka | 7a8f58f | 2017-07-10 15:47:14 -0700 | [diff] [blame] | 2402 | /* |
| 2403 | * We cannot steal all free pages from the pageblock and the |
| 2404 | * requested migratetype is movable. In that case it's better to |
| 2405 | * steal and split the smallest available page instead of the |
| 2406 | * largest available page, because even if the next movable |
| 2407 | * allocation falls back into a different pageblock than this |
| 2408 | * one, it won't cause permanent fragmentation. |
| 2409 | */ |
| 2410 | if (!can_steal && start_migratetype == MIGRATE_MOVABLE |
| 2411 | && current_order > order) |
| 2412 | goto find_smallest; |
Mel Gorman | e010487 | 2007-10-16 01:25:53 -0700 | [diff] [blame] | 2413 | |
Vlastimil Babka | 7a8f58f | 2017-07-10 15:47:14 -0700 | [diff] [blame] | 2414 | goto do_steal; |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2417 | return false; |
Vlastimil Babka | 7a8f58f | 2017-07-10 15:47:14 -0700 | [diff] [blame] | 2418 | |
| 2419 | find_smallest: |
| 2420 | for (current_order = order; current_order < MAX_ORDER; |
| 2421 | current_order++) { |
| 2422 | area = &(zone->free_area[current_order]); |
| 2423 | fallback_mt = find_suitable_fallback(area, current_order, |
| 2424 | start_migratetype, false, &can_steal); |
| 2425 | if (fallback_mt != -1) |
| 2426 | break; |
| 2427 | } |
| 2428 | |
| 2429 | /* |
| 2430 | * This should not happen - we already found a suitable fallback |
| 2431 | * when looking for the largest page. |
| 2432 | */ |
| 2433 | VM_BUG_ON(current_order == MAX_ORDER); |
| 2434 | |
| 2435 | do_steal: |
| 2436 | page = list_first_entry(&area->free_list[fallback_mt], |
| 2437 | struct page, lru); |
| 2438 | |
| 2439 | steal_suitable_fallback(zone, page, start_migratetype, can_steal); |
| 2440 | |
| 2441 | trace_mm_page_alloc_extfrag(page, order, current_order, |
| 2442 | start_migratetype, fallback_mt); |
| 2443 | |
| 2444 | return true; |
| 2445 | |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 2448 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | * Do the hard work of removing an element from the buddy allocator. |
| 2450 | * Call me with the zone->lock already held. |
| 2451 | */ |
Aaron Lu | 85ccc8f | 2017-11-15 17:36:53 -0800 | [diff] [blame] | 2452 | static __always_inline struct page * |
| 2453 | __rmqueue(struct zone *zone, unsigned int order, int migratetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | struct page *page; |
| 2456 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2457 | retry: |
Mel Gorman | 56fd56b | 2007-10-16 01:25:58 -0700 | [diff] [blame] | 2458 | page = __rmqueue_smallest(zone, order, migratetype); |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 2459 | if (unlikely(!page)) { |
Joonsoo Kim | dc67647 | 2015-04-14 15:45:15 -0700 | [diff] [blame] | 2460 | if (migratetype == MIGRATE_MOVABLE) |
| 2461 | page = __rmqueue_cma_fallback(zone, order); |
| 2462 | |
Vlastimil Babka | 3bc48f9 | 2017-05-08 15:54:37 -0700 | [diff] [blame] | 2463 | if (!page && __rmqueue_fallback(zone, order, migratetype)) |
| 2464 | goto retry; |
Mel Gorman | 728ec98 | 2009-06-16 15:32:04 -0700 | [diff] [blame] | 2465 | } |
| 2466 | |
Mel Gorman | 0d3d062 | 2009-09-21 17:02:44 -0700 | [diff] [blame] | 2467 | trace_mm_page_alloc_zone_locked(page, order, migratetype); |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2468 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | } |
| 2470 | |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 2471 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | * Obtain a specified number of elements from the buddy allocator, all under |
| 2473 | * a single hold of the lock, for efficiency. Add them to the supplied list. |
| 2474 | * Returns the number of new pages which were placed at *list. |
| 2475 | */ |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 2476 | static int rmqueue_bulk(struct zone *zone, unsigned int order, |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2477 | unsigned long count, struct list_head *list, |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2478 | int migratetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | { |
Mel Gorman | a6de734 | 2016-12-12 16:44:41 -0800 | [diff] [blame] | 2480 | int i, alloced = 0; |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 2481 | |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2482 | spin_lock(&zone->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | for (i = 0; i < count; ++i) { |
Mel Gorman | 6ac0206 | 2016-01-14 15:20:28 -0800 | [diff] [blame] | 2484 | struct page *page = __rmqueue(zone, order, migratetype); |
Nick Piggin | 085cc7d5 | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 2485 | if (unlikely(page == NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | break; |
Mel Gorman | 81eabcb | 2007-12-17 16:20:05 -0800 | [diff] [blame] | 2487 | |
Mel Gorman | 479f854 | 2016-05-19 17:14:35 -0700 | [diff] [blame] | 2488 | if (unlikely(check_pcp_refill(page))) |
| 2489 | continue; |
| 2490 | |
Mel Gorman | 81eabcb | 2007-12-17 16:20:05 -0800 | [diff] [blame] | 2491 | /* |
Vlastimil Babka | 0fac3ba | 2017-11-15 17:38:07 -0800 | [diff] [blame] | 2492 | * Split buddy pages returned by expand() are received here in |
| 2493 | * physical page order. The page is added to the tail of |
| 2494 | * caller's list. From the callers perspective, the linked list |
| 2495 | * is ordered by page number under some conditions. This is |
| 2496 | * useful for IO devices that can forward direction from the |
| 2497 | * head, thus also in the physical page order. This is useful |
| 2498 | * for IO devices that can merge IO requests if the physical |
| 2499 | * pages are ordered properly. |
Mel Gorman | 81eabcb | 2007-12-17 16:20:05 -0800 | [diff] [blame] | 2500 | */ |
Vlastimil Babka | 0fac3ba | 2017-11-15 17:38:07 -0800 | [diff] [blame] | 2501 | list_add_tail(&page->lru, list); |
Mel Gorman | a6de734 | 2016-12-12 16:44:41 -0800 | [diff] [blame] | 2502 | alloced++; |
Vlastimil Babka | bb14c2c | 2015-09-08 15:01:25 -0700 | [diff] [blame] | 2503 | if (is_migrate_cma(get_pcppage_migratetype(page))) |
Bartlomiej Zolnierkiewicz | d1ce749 | 2012-10-08 16:32:02 -0700 | [diff] [blame] | 2504 | __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, |
| 2505 | -(1 << order)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | } |
Mel Gorman | a6de734 | 2016-12-12 16:44:41 -0800 | [diff] [blame] | 2507 | |
| 2508 | /* |
| 2509 | * i pages were removed from the buddy list even if some leak due |
| 2510 | * to check_pcp_refill failing so adjust NR_FREE_PAGES based |
| 2511 | * on i. Do not confuse with 'alloced' which is the number of |
| 2512 | * pages added to the pcp list. |
| 2513 | */ |
Mel Gorman | f2260e6 | 2009-06-16 15:32:13 -0700 | [diff] [blame] | 2514 | __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order)); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2515 | spin_unlock(&zone->lock); |
Mel Gorman | a6de734 | 2016-12-12 16:44:41 -0800 | [diff] [blame] | 2516 | return alloced; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Christoph Lameter | 4ae7c03 | 2005-06-21 17:14:57 -0700 | [diff] [blame] | 2519 | #ifdef CONFIG_NUMA |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 2520 | /* |
Christoph Lameter | 4037d45 | 2007-05-09 02:35:14 -0700 | [diff] [blame] | 2521 | * Called from the vmstat counter updater to drain pagesets of this |
| 2522 | * currently executing processor on remote nodes after they have |
| 2523 | * expired. |
| 2524 | * |
Christoph Lameter | 879336c | 2006-03-22 00:09:08 -0800 | [diff] [blame] | 2525 | * Note that this function must be called with the thread pinned to |
| 2526 | * a single processor. |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 2527 | */ |
Christoph Lameter | 4037d45 | 2007-05-09 02:35:14 -0700 | [diff] [blame] | 2528 | void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) |
Christoph Lameter | 4ae7c03 | 2005-06-21 17:14:57 -0700 | [diff] [blame] | 2529 | { |
Christoph Lameter | 4ae7c03 | 2005-06-21 17:14:57 -0700 | [diff] [blame] | 2530 | unsigned long flags; |
Michal Nazarewicz | 7be12fc | 2014-08-06 16:05:15 -0700 | [diff] [blame] | 2531 | int to_drain, batch; |
Christoph Lameter | 4ae7c03 | 2005-06-21 17:14:57 -0700 | [diff] [blame] | 2532 | |
Christoph Lameter | 4037d45 | 2007-05-09 02:35:14 -0700 | [diff] [blame] | 2533 | local_irq_save(flags); |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 2534 | batch = READ_ONCE(pcp->batch); |
Michal Nazarewicz | 7be12fc | 2014-08-06 16:05:15 -0700 | [diff] [blame] | 2535 | to_drain = min(pcp->count, batch); |
Aaron Lu | 77ba906 | 2018-04-05 16:24:06 -0700 | [diff] [blame] | 2536 | if (to_drain > 0) |
KOSAKI Motohiro | 2a13515 | 2012-07-31 16:42:53 -0700 | [diff] [blame] | 2537 | free_pcppages_bulk(zone, to_drain, pcp); |
Christoph Lameter | 4037d45 | 2007-05-09 02:35:14 -0700 | [diff] [blame] | 2538 | local_irq_restore(flags); |
Christoph Lameter | 4ae7c03 | 2005-06-21 17:14:57 -0700 | [diff] [blame] | 2539 | } |
| 2540 | #endif |
| 2541 | |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2542 | /* |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2543 | * Drain pcplists of the indicated processor and zone. |
| 2544 | * |
| 2545 | * The processor must either be the current processor and the |
| 2546 | * thread pinned to the current processor or a processor that |
| 2547 | * is not online. |
| 2548 | */ |
| 2549 | static void drain_pages_zone(unsigned int cpu, struct zone *zone) |
| 2550 | { |
| 2551 | unsigned long flags; |
| 2552 | struct per_cpu_pageset *pset; |
| 2553 | struct per_cpu_pages *pcp; |
| 2554 | |
| 2555 | local_irq_save(flags); |
| 2556 | pset = per_cpu_ptr(zone->pageset, cpu); |
| 2557 | |
| 2558 | pcp = &pset->pcp; |
Aaron Lu | 77ba906 | 2018-04-05 16:24:06 -0700 | [diff] [blame] | 2559 | if (pcp->count) |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2560 | free_pcppages_bulk(zone, pcp->count, pcp); |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2561 | local_irq_restore(flags); |
| 2562 | } |
| 2563 | |
| 2564 | /* |
| 2565 | * Drain pcplists of all zones on the indicated processor. |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2566 | * |
| 2567 | * The processor must either be the current processor and the |
| 2568 | * thread pinned to the current processor or a processor that |
| 2569 | * is not online. |
| 2570 | */ |
| 2571 | static void drain_pages(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | { |
| 2573 | struct zone *zone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 2575 | for_each_populated_zone(zone) { |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2576 | drain_pages_zone(cpu, zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | } |
| 2578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2580 | /* |
| 2581 | * Spill all of this CPU's per-cpu pages back into the buddy allocator. |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2582 | * |
| 2583 | * The CPU has to be pinned. When zone parameter is non-NULL, spill just |
| 2584 | * the single zone's pages. |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2585 | */ |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2586 | void drain_local_pages(struct zone *zone) |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2587 | { |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2588 | int cpu = smp_processor_id(); |
| 2589 | |
| 2590 | if (zone) |
| 2591 | drain_pages_zone(cpu, zone); |
| 2592 | else |
| 2593 | drain_pages(cpu); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2594 | } |
| 2595 | |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2596 | static void drain_local_pages_wq(struct work_struct *work) |
| 2597 | { |
Michal Hocko | a459eeb | 2017-02-24 14:56:35 -0800 | [diff] [blame] | 2598 | /* |
| 2599 | * drain_all_pages doesn't use proper cpu hotplug protection so |
| 2600 | * we can race with cpu offline when the WQ can move this from |
| 2601 | * a cpu pinned worker to an unbound one. We can operate on a different |
| 2602 | * cpu which is allright but we also have to make sure to not move to |
| 2603 | * a different one. |
| 2604 | */ |
| 2605 | preempt_disable(); |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2606 | drain_local_pages(NULL); |
Michal Hocko | a459eeb | 2017-02-24 14:56:35 -0800 | [diff] [blame] | 2607 | preempt_enable(); |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2608 | } |
| 2609 | |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2610 | /* |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2611 | * Spill all the per-cpu pages from all CPUs back into the buddy allocator. |
| 2612 | * |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2613 | * When zone parameter is non-NULL, spill just the single zone's pages. |
| 2614 | * |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2615 | * Note that this can be extremely slow as the draining happens in a workqueue. |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2616 | */ |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2617 | void drain_all_pages(struct zone *zone) |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2618 | { |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2619 | int cpu; |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2620 | |
| 2621 | /* |
| 2622 | * Allocate in the BSS so we wont require allocation in |
| 2623 | * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y |
| 2624 | */ |
| 2625 | static cpumask_t cpus_with_pcps; |
| 2626 | |
Michal Hocko | ce61287 | 2017-04-07 16:05:05 -0700 | [diff] [blame] | 2627 | /* |
| 2628 | * Make sure nobody triggers this path before mm_percpu_wq is fully |
| 2629 | * initialized. |
| 2630 | */ |
| 2631 | if (WARN_ON_ONCE(!mm_percpu_wq)) |
| 2632 | return; |
| 2633 | |
Mel Gorman | bd233f5 | 2017-02-24 14:56:56 -0800 | [diff] [blame] | 2634 | /* |
| 2635 | * Do not drain if one is already in progress unless it's specific to |
| 2636 | * a zone. Such callers are primarily CMA and memory hotplug and need |
| 2637 | * the drain to be complete when the call returns. |
| 2638 | */ |
| 2639 | if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) { |
| 2640 | if (!zone) |
| 2641 | return; |
| 2642 | mutex_lock(&pcpu_drain_mutex); |
| 2643 | } |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2644 | |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2645 | /* |
| 2646 | * We don't care about racing with CPU hotplug event |
| 2647 | * as offline notification will cause the notified |
| 2648 | * cpu to drain that CPU pcps and on_each_cpu_mask |
| 2649 | * disables preemption as part of its processing |
| 2650 | */ |
| 2651 | for_each_online_cpu(cpu) { |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2652 | struct per_cpu_pageset *pcp; |
| 2653 | struct zone *z; |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2654 | bool has_pcps = false; |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2655 | |
| 2656 | if (zone) { |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2657 | pcp = per_cpu_ptr(zone->pageset, cpu); |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2658 | if (pcp->pcp.count) |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2659 | has_pcps = true; |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2660 | } else { |
| 2661 | for_each_populated_zone(z) { |
| 2662 | pcp = per_cpu_ptr(z->pageset, cpu); |
| 2663 | if (pcp->pcp.count) { |
| 2664 | has_pcps = true; |
| 2665 | break; |
| 2666 | } |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2667 | } |
| 2668 | } |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 2669 | |
Gilad Ben-Yossef | 7404649 | 2012-03-28 14:42:45 -0700 | [diff] [blame] | 2670 | if (has_pcps) |
| 2671 | cpumask_set_cpu(cpu, &cpus_with_pcps); |
| 2672 | else |
| 2673 | cpumask_clear_cpu(cpu, &cpus_with_pcps); |
| 2674 | } |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2675 | |
Mel Gorman | bd233f5 | 2017-02-24 14:56:56 -0800 | [diff] [blame] | 2676 | for_each_cpu(cpu, &cpus_with_pcps) { |
| 2677 | struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu); |
| 2678 | INIT_WORK(work, drain_local_pages_wq); |
Michal Hocko | ce61287 | 2017-04-07 16:05:05 -0700 | [diff] [blame] | 2679 | queue_work_on(cpu, mm_percpu_wq, work); |
Mel Gorman | 0ccce3b | 2017-02-24 14:56:32 -0800 | [diff] [blame] | 2680 | } |
Mel Gorman | bd233f5 | 2017-02-24 14:56:56 -0800 | [diff] [blame] | 2681 | for_each_cpu(cpu, &cpus_with_pcps) |
| 2682 | flush_work(per_cpu_ptr(&pcpu_drain, cpu)); |
| 2683 | |
| 2684 | mutex_unlock(&pcpu_drain_mutex); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 2685 | } |
| 2686 | |
Rafael J. Wysocki | 296699d | 2007-07-29 23:27:18 +0200 | [diff] [blame] | 2687 | #ifdef CONFIG_HIBERNATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2688 | |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 2689 | /* |
| 2690 | * Touch the watchdog for every WD_PAGE_COUNT pages. |
| 2691 | */ |
| 2692 | #define WD_PAGE_COUNT (128*1024) |
| 2693 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | void mark_free_pages(struct zone *zone) |
| 2695 | { |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 2696 | unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT; |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2697 | unsigned long flags; |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 2698 | unsigned int order, t; |
Geliang Tang | 86760a2 | 2016-01-14 15:20:33 -0800 | [diff] [blame] | 2699 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | |
Xishi Qiu | 8080fc0 | 2013-09-11 14:21:45 -0700 | [diff] [blame] | 2701 | if (zone_is_empty(zone)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | return; |
| 2703 | |
| 2704 | spin_lock_irqsave(&zone->lock, flags); |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2705 | |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 2706 | max_zone_pfn = zone_end_pfn(zone); |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2707 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| 2708 | if (pfn_valid(pfn)) { |
Geliang Tang | 86760a2 | 2016-01-14 15:20:33 -0800 | [diff] [blame] | 2709 | page = pfn_to_page(pfn); |
Joonsoo Kim | ba6b097 | 2016-05-19 17:12:16 -0700 | [diff] [blame] | 2710 | |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 2711 | if (!--page_count) { |
| 2712 | touch_nmi_watchdog(); |
| 2713 | page_count = WD_PAGE_COUNT; |
| 2714 | } |
| 2715 | |
Joonsoo Kim | ba6b097 | 2016-05-19 17:12:16 -0700 | [diff] [blame] | 2716 | if (page_zone(page) != zone) |
| 2717 | continue; |
| 2718 | |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2719 | if (!swsusp_page_is_forbidden(page)) |
| 2720 | swsusp_unset_page_free(page); |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2721 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2723 | for_each_migratetype_order(order, t) { |
Geliang Tang | 86760a2 | 2016-01-14 15:20:33 -0800 | [diff] [blame] | 2724 | list_for_each_entry(page, |
| 2725 | &zone->free_area[order].free_list[t], lru) { |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2726 | unsigned long i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | |
Geliang Tang | 86760a2 | 2016-01-14 15:20:33 -0800 | [diff] [blame] | 2728 | pfn = page_to_pfn(page); |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 2729 | for (i = 0; i < (1UL << order); i++) { |
| 2730 | if (!--page_count) { |
| 2731 | touch_nmi_watchdog(); |
| 2732 | page_count = WD_PAGE_COUNT; |
| 2733 | } |
Rafael J. Wysocki | 7be9823 | 2007-05-06 14:50:42 -0700 | [diff] [blame] | 2734 | swsusp_set_page_free(pfn_to_page(pfn + i)); |
Chen Yu | 556b969 | 2017-08-25 15:55:30 -0700 | [diff] [blame] | 2735 | } |
Rafael J. Wysocki | f623f0d | 2006-09-25 23:32:49 -0700 | [diff] [blame] | 2736 | } |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 2737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | spin_unlock_irqrestore(&zone->lock, flags); |
| 2739 | } |
Mel Gorman | e2c55dc | 2007-10-16 01:25:50 -0700 | [diff] [blame] | 2740 | #endif /* CONFIG_PM */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2742 | static bool free_unref_page_prepare(struct page *page, unsigned long pfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | { |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 2744 | int migratetype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | |
Mel Gorman | 4db7548 | 2016-05-19 17:14:32 -0700 | [diff] [blame] | 2746 | if (!free_pcp_prepare(page)) |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2747 | return false; |
Hugh Dickins | 689bceb | 2005-11-21 21:32:20 -0800 | [diff] [blame] | 2748 | |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 2749 | migratetype = get_pfnblock_migratetype(page, pfn); |
Vlastimil Babka | bb14c2c | 2015-09-08 15:01:25 -0700 | [diff] [blame] | 2750 | set_pcppage_migratetype(page, migratetype); |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2751 | return true; |
| 2752 | } |
| 2753 | |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2754 | static void free_unref_page_commit(struct page *page, unsigned long pfn) |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2755 | { |
| 2756 | struct zone *zone = page_zone(page); |
| 2757 | struct per_cpu_pages *pcp; |
| 2758 | int migratetype; |
| 2759 | |
| 2760 | migratetype = get_pcppage_migratetype(page); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2761 | __count_vm_event(PGFREE); |
Mel Gorman | da456f1 | 2009-06-16 15:32:08 -0700 | [diff] [blame] | 2762 | |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 2763 | /* |
| 2764 | * We only track unmovable, reclaimable and movable on pcp lists. |
| 2765 | * Free ISOLATE pages back to the allocator because they are being |
Xishi Qiu | a6ffdc0 | 2017-05-03 14:52:52 -0700 | [diff] [blame] | 2766 | * offlined but treat HIGHATOMIC as movable pages so we can get those |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 2767 | * areas back if necessary. Otherwise, we may have to free |
| 2768 | * excessively into the page allocator |
| 2769 | */ |
| 2770 | if (migratetype >= MIGRATE_PCPTYPES) { |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 2771 | if (unlikely(is_migrate_isolate(migratetype))) { |
Mel Gorman | dc4b0ca | 2014-06-04 16:10:17 -0700 | [diff] [blame] | 2772 | free_one_page(zone, page, pfn, 0, migratetype); |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2773 | return; |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 2774 | } |
| 2775 | migratetype = MIGRATE_MOVABLE; |
| 2776 | } |
| 2777 | |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 2778 | pcp = &this_cpu_ptr(zone->pageset)->pcp; |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2779 | list_add(&page->lru, &pcp->lists[migratetype]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2780 | pcp->count++; |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 2781 | if (pcp->count >= pcp->high) { |
Jason Low | 4db0c3c | 2015-04-15 16:14:08 -0700 | [diff] [blame] | 2782 | unsigned long batch = READ_ONCE(pcp->batch); |
Cody P Schafer | 998d39cb | 2013-07-03 15:01:32 -0700 | [diff] [blame] | 2783 | free_pcppages_bulk(zone, batch, pcp); |
Nick Piggin | 48db57f | 2006-01-08 01:00:42 -0800 | [diff] [blame] | 2784 | } |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2785 | } |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 2786 | |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2787 | /* |
| 2788 | * Free a 0-order page |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2789 | */ |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2790 | void free_unref_page(struct page *page) |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2791 | { |
| 2792 | unsigned long flags; |
| 2793 | unsigned long pfn = page_to_pfn(page); |
| 2794 | |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2795 | if (!free_unref_page_prepare(page, pfn)) |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2796 | return; |
| 2797 | |
| 2798 | local_irq_save(flags); |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2799 | free_unref_page_commit(page, pfn); |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2800 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 2803 | /* |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2804 | * Free a list of 0-order pages |
| 2805 | */ |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2806 | void free_unref_page_list(struct list_head *list) |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2807 | { |
| 2808 | struct page *page, *next; |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2809 | unsigned long flags, pfn; |
Lucas Stach | c24ad77 | 2017-12-14 15:32:55 -0800 | [diff] [blame] | 2810 | int batch_count = 0; |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2811 | |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2812 | /* Prepare pages for freeing */ |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2813 | list_for_each_entry_safe(page, next, list, lru) { |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2814 | pfn = page_to_pfn(page); |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2815 | if (!free_unref_page_prepare(page, pfn)) |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2816 | list_del(&page->lru); |
| 2817 | set_page_private(page, pfn); |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2818 | } |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2819 | |
| 2820 | local_irq_save(flags); |
| 2821 | list_for_each_entry_safe(page, next, list, lru) { |
| 2822 | unsigned long pfn = page_private(page); |
| 2823 | |
| 2824 | set_page_private(page, 0); |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 2825 | trace_mm_page_free_batched(page); |
| 2826 | free_unref_page_commit(page, pfn); |
Lucas Stach | c24ad77 | 2017-12-14 15:32:55 -0800 | [diff] [blame] | 2827 | |
| 2828 | /* |
| 2829 | * Guard against excessive IRQ disabled times when we get |
| 2830 | * a large list of pages to free. |
| 2831 | */ |
| 2832 | if (++batch_count == SWAP_CLUSTER_MAX) { |
| 2833 | local_irq_restore(flags); |
| 2834 | batch_count = 0; |
| 2835 | local_irq_save(flags); |
| 2836 | } |
Mel Gorman | 9cca35d4 | 2017-11-15 17:37:37 -0800 | [diff] [blame] | 2837 | } |
| 2838 | local_irq_restore(flags); |
Konstantin Khlebnikov | cc59850 | 2012-01-10 15:07:04 -0800 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | /* |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 2842 | * split_page takes a non-compound higher-order page, and splits it into |
| 2843 | * n (1<<order) sub-pages: page[0..n] |
| 2844 | * Each sub-page must be freed individually. |
| 2845 | * |
| 2846 | * Note: this is probably too low level an operation for use in drivers. |
| 2847 | * Please consult with lkml before using this in your driver. |
| 2848 | */ |
| 2849 | void split_page(struct page *page, unsigned int order) |
| 2850 | { |
| 2851 | int i; |
| 2852 | |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 2853 | VM_BUG_ON_PAGE(PageCompound(page), page); |
| 2854 | VM_BUG_ON_PAGE(!page_count(page), page); |
Vegard Nossum | b1eeab6 | 2008-11-25 16:55:53 +0100 | [diff] [blame] | 2855 | |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 2856 | for (i = 1; i < (1 << order); i++) |
Nick Piggin | 7835e98 | 2006-03-22 00:08:40 -0800 | [diff] [blame] | 2857 | set_page_refcounted(page + i); |
Joonsoo Kim | a9627bc | 2016-07-26 15:23:49 -0700 | [diff] [blame] | 2858 | split_page_owner(page, order); |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 2859 | } |
K. Y. Srinivasan | 5853ff2 | 2013-03-25 15:47:38 -0700 | [diff] [blame] | 2860 | EXPORT_SYMBOL_GPL(split_page); |
Nick Piggin | 8dfcc9b | 2006-03-22 00:08:05 -0800 | [diff] [blame] | 2861 | |
Joonsoo Kim | 3c60509 | 2014-11-13 15:19:21 -0800 | [diff] [blame] | 2862 | int __isolate_free_page(struct page *page, unsigned int order) |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2863 | { |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2864 | unsigned long watermark; |
| 2865 | struct zone *zone; |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 2866 | int mt; |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2867 | |
| 2868 | BUG_ON(!PageBuddy(page)); |
| 2869 | |
| 2870 | zone = page_zone(page); |
Marek Szyprowski | 2e30abd | 2012-12-11 16:02:57 -0800 | [diff] [blame] | 2871 | mt = get_pageblock_migratetype(page); |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2872 | |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 2873 | if (!is_migrate_isolate(mt)) { |
Vlastimil Babka | 8348faf | 2016-10-07 16:58:00 -0700 | [diff] [blame] | 2874 | /* |
| 2875 | * Obey watermarks as if the page was being allocated. We can |
| 2876 | * emulate a high-order watermark check with a raised order-0 |
| 2877 | * watermark, because we already know our high-order page |
| 2878 | * exists. |
| 2879 | */ |
| 2880 | watermark = min_wmark_pages(zone) + (1UL << order); |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 2881 | if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) |
Marek Szyprowski | 2e30abd | 2012-12-11 16:02:57 -0800 | [diff] [blame] | 2882 | return 0; |
| 2883 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2884 | __mod_zone_freepage_state(zone, -(1UL << order), mt); |
Marek Szyprowski | 2e30abd | 2012-12-11 16:02:57 -0800 | [diff] [blame] | 2885 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2886 | |
| 2887 | /* Remove page from free list */ |
| 2888 | list_del(&page->lru); |
| 2889 | zone->free_area[order].nr_free--; |
| 2890 | rmv_page_order(page); |
Bartlomiej Zolnierkiewicz | 2139cbe | 2012-10-08 16:32:00 -0700 | [diff] [blame] | 2891 | |
zhong jiang | 400bc7f | 2016-07-28 15:45:07 -0700 | [diff] [blame] | 2892 | /* |
| 2893 | * Set the pageblock if the isolated page is at least half of a |
| 2894 | * pageblock |
| 2895 | */ |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2896 | if (order >= pageblock_order - 1) { |
| 2897 | struct page *endpage = page + (1 << order) - 1; |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2898 | for (; page < endpage; page += pageblock_nr_pages) { |
| 2899 | int mt = get_pageblock_migratetype(page); |
Minchan Kim | 88ed365 | 2016-12-12 16:42:05 -0800 | [diff] [blame] | 2900 | if (!is_migrate_isolate(mt) && !is_migrate_cma(mt) |
Xishi Qiu | a6ffdc0 | 2017-05-03 14:52:52 -0700 | [diff] [blame] | 2901 | && !is_migrate_highatomic(mt)) |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 2902 | set_pageblock_migratetype(page, |
| 2903 | MIGRATE_MOVABLE); |
| 2904 | } |
Mel Gorman | 748446b | 2010-05-24 14:32:27 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
Joonsoo Kim | f3a14ce | 2015-07-17 16:24:15 -0700 | [diff] [blame] | 2907 | |
Mel Gorman | 8fb74b9 | 2013-01-11 14:32:16 -0800 | [diff] [blame] | 2908 | return 1UL << order; |
Mel Gorman | 1fb3f8c | 2012-10-08 16:29:12 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | /* |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2912 | * Update NUMA hit/miss statistics |
| 2913 | * |
| 2914 | * Must be called with interrupts disabled. |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2915 | */ |
Michal Hocko | 41b6167 | 2017-01-10 16:57:42 -0800 | [diff] [blame] | 2916 | static inline void zone_statistics(struct zone *preferred_zone, struct zone *z) |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2917 | { |
| 2918 | #ifdef CONFIG_NUMA |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 2919 | enum numa_stat_item local_stat = NUMA_LOCAL; |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2920 | |
Kemi Wang | 4518085 | 2017-11-15 17:38:22 -0800 | [diff] [blame] | 2921 | /* skip numa counters update if numa stats is disabled */ |
| 2922 | if (!static_branch_likely(&vm_numa_stat_key)) |
| 2923 | return; |
| 2924 | |
Pavel Tatashin | c1093b7 | 2018-08-21 21:53:32 -0700 | [diff] [blame] | 2925 | if (zone_to_nid(z) != numa_node_id()) |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2926 | local_stat = NUMA_OTHER; |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2927 | |
Pavel Tatashin | c1093b7 | 2018-08-21 21:53:32 -0700 | [diff] [blame] | 2928 | if (zone_to_nid(z) == zone_to_nid(preferred_zone)) |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 2929 | __inc_numa_state(z, NUMA_HIT); |
Michal Hocko | 2df2663 | 2017-01-10 16:57:39 -0800 | [diff] [blame] | 2930 | else { |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 2931 | __inc_numa_state(z, NUMA_MISS); |
| 2932 | __inc_numa_state(preferred_zone, NUMA_FOREIGN); |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2933 | } |
Kemi Wang | 3a321d2 | 2017-09-08 16:12:48 -0700 | [diff] [blame] | 2934 | __inc_numa_state(z, local_stat); |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2935 | #endif |
| 2936 | } |
| 2937 | |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2938 | /* Remove page from the per-cpu list, caller must protect the list */ |
| 2939 | static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype, |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2940 | struct per_cpu_pages *pcp, |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2941 | struct list_head *list) |
| 2942 | { |
| 2943 | struct page *page; |
| 2944 | |
| 2945 | do { |
| 2946 | if (list_empty(list)) { |
| 2947 | pcp->count += rmqueue_bulk(zone, 0, |
| 2948 | pcp->batch, list, |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2949 | migratetype); |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2950 | if (unlikely(list_empty(list))) |
| 2951 | return NULL; |
| 2952 | } |
| 2953 | |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2954 | page = list_first_entry(list, struct page, lru); |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2955 | list_del(&page->lru); |
| 2956 | pcp->count--; |
| 2957 | } while (check_new_pcp(page)); |
| 2958 | |
| 2959 | return page; |
| 2960 | } |
| 2961 | |
| 2962 | /* Lock and remove page from the per-cpu list */ |
| 2963 | static struct page *rmqueue_pcplist(struct zone *preferred_zone, |
| 2964 | struct zone *zone, unsigned int order, |
| 2965 | gfp_t gfp_flags, int migratetype) |
| 2966 | { |
| 2967 | struct per_cpu_pages *pcp; |
| 2968 | struct list_head *list; |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2969 | struct page *page; |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2970 | unsigned long flags; |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2971 | |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2972 | local_irq_save(flags); |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2973 | pcp = &this_cpu_ptr(zone->pageset)->pcp; |
| 2974 | list = &pcp->lists[migratetype]; |
Mel Gorman | 453f85d | 2017-11-15 17:38:03 -0800 | [diff] [blame] | 2975 | page = __rmqueue_pcplist(zone, migratetype, pcp, list); |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2976 | if (page) { |
| 2977 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); |
| 2978 | zone_statistics(preferred_zone, zone); |
| 2979 | } |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2980 | local_irq_restore(flags); |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2981 | return page; |
| 2982 | } |
| 2983 | |
Mel Gorman | 060e741 | 2016-05-19 17:13:27 -0700 | [diff] [blame] | 2984 | /* |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 2985 | * Allocate a page from the given zone. Use pcplists for order-0 allocations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | */ |
Mel Gorman | 0a15c3e | 2009-06-16 15:32:05 -0700 | [diff] [blame] | 2987 | static inline |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2988 | struct page *rmqueue(struct zone *preferred_zone, |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 2989 | struct zone *zone, unsigned int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 2990 | gfp_t gfp_flags, unsigned int alloc_flags, |
| 2991 | int migratetype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2992 | { |
| 2993 | unsigned long flags; |
Hugh Dickins | 689bceb | 2005-11-21 21:32:20 -0800 | [diff] [blame] | 2994 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2995 | |
Mel Gorman | d34b073 | 2017-04-20 14:37:43 -0700 | [diff] [blame] | 2996 | if (likely(order == 0)) { |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 2997 | page = rmqueue_pcplist(preferred_zone, zone, order, |
| 2998 | gfp_flags, migratetype); |
| 2999 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 3002 | /* |
| 3003 | * We most definitely don't want callers attempting to |
| 3004 | * allocate greater than order-1 page units with __GFP_NOFAIL. |
| 3005 | */ |
| 3006 | WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); |
| 3007 | spin_lock_irqsave(&zone->lock, flags); |
| 3008 | |
| 3009 | do { |
| 3010 | page = NULL; |
| 3011 | if (alloc_flags & ALLOC_HARDER) { |
| 3012 | page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); |
| 3013 | if (page) |
| 3014 | trace_mm_page_alloc_zone_locked(page, order, migratetype); |
| 3015 | } |
| 3016 | if (!page) |
| 3017 | page = __rmqueue(zone, order, migratetype); |
| 3018 | } while (page && check_new_pages(page, order)); |
| 3019 | spin_unlock(&zone->lock); |
| 3020 | if (!page) |
| 3021 | goto failed; |
| 3022 | __mod_zone_freepage_state(zone, -(1 << order), |
| 3023 | get_pcppage_migratetype(page)); |
| 3024 | |
Mel Gorman | 16709d1 | 2016-07-28 15:46:56 -0700 | [diff] [blame] | 3025 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); |
Michal Hocko | 41b6167 | 2017-01-10 16:57:42 -0800 | [diff] [blame] | 3026 | zone_statistics(preferred_zone, zone); |
Nick Piggin | a74609f | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 3027 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 3029 | out: |
| 3030 | VM_BUG_ON_PAGE(page && bad_range(zone, page), page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | return page; |
Nick Piggin | a74609f | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 3032 | |
| 3033 | failed: |
| 3034 | local_irq_restore(flags); |
Nick Piggin | a74609f | 2006-01-06 00:11:20 -0800 | [diff] [blame] | 3035 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3038 | #ifdef CONFIG_FAIL_PAGE_ALLOC |
| 3039 | |
Akinobu Mita | b2588c4 | 2011-07-26 16:09:03 -0700 | [diff] [blame] | 3040 | static struct { |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3041 | struct fault_attr attr; |
| 3042 | |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 3043 | bool ignore_gfp_highmem; |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 3044 | bool ignore_gfp_reclaim; |
Akinobu Mita | 5411499 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 3045 | u32 min_order; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3046 | } fail_page_alloc = { |
| 3047 | .attr = FAULT_ATTR_INITIALIZER, |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 3048 | .ignore_gfp_reclaim = true, |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 3049 | .ignore_gfp_highmem = true, |
Akinobu Mita | 5411499 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 3050 | .min_order = 1, |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3051 | }; |
| 3052 | |
| 3053 | static int __init setup_fail_page_alloc(char *str) |
| 3054 | { |
| 3055 | return setup_fault_attr(&fail_page_alloc.attr, str); |
| 3056 | } |
| 3057 | __setup("fail_page_alloc=", setup_fail_page_alloc); |
| 3058 | |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3059 | static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3060 | { |
Akinobu Mita | 5411499 | 2007-07-15 23:40:23 -0700 | [diff] [blame] | 3061 | if (order < fail_page_alloc.min_order) |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3062 | return false; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3063 | if (gfp_mask & __GFP_NOFAIL) |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3064 | return false; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3065 | if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM)) |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3066 | return false; |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 3067 | if (fail_page_alloc.ignore_gfp_reclaim && |
| 3068 | (gfp_mask & __GFP_DIRECT_RECLAIM)) |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3069 | return false; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3070 | |
| 3071 | return should_fail(&fail_page_alloc.attr, 1 << order); |
| 3072 | } |
| 3073 | |
| 3074 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 3075 | |
| 3076 | static int __init fail_page_alloc_debugfs(void) |
| 3077 | { |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3078 | umode_t mode = S_IFREG | 0600; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3079 | struct dentry *dir; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3080 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 3081 | dir = fault_create_debugfs_attr("fail_page_alloc", NULL, |
| 3082 | &fail_page_alloc.attr); |
| 3083 | if (IS_ERR(dir)) |
| 3084 | return PTR_ERR(dir); |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3085 | |
Akinobu Mita | b2588c4 | 2011-07-26 16:09:03 -0700 | [diff] [blame] | 3086 | if (!debugfs_create_bool("ignore-gfp-wait", mode, dir, |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 3087 | &fail_page_alloc.ignore_gfp_reclaim)) |
Akinobu Mita | b2588c4 | 2011-07-26 16:09:03 -0700 | [diff] [blame] | 3088 | goto fail; |
| 3089 | if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir, |
| 3090 | &fail_page_alloc.ignore_gfp_highmem)) |
| 3091 | goto fail; |
| 3092 | if (!debugfs_create_u32("min-order", mode, dir, |
| 3093 | &fail_page_alloc.min_order)) |
| 3094 | goto fail; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3095 | |
Akinobu Mita | b2588c4 | 2011-07-26 16:09:03 -0700 | [diff] [blame] | 3096 | return 0; |
| 3097 | fail: |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 3098 | debugfs_remove_recursive(dir); |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3099 | |
Akinobu Mita | b2588c4 | 2011-07-26 16:09:03 -0700 | [diff] [blame] | 3100 | return -ENOMEM; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | late_initcall(fail_page_alloc_debugfs); |
| 3104 | |
| 3105 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |
| 3106 | |
| 3107 | #else /* CONFIG_FAIL_PAGE_ALLOC */ |
| 3108 | |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3109 | static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3110 | { |
Gavin Shan | deaf386 | 2012-07-31 16:41:51 -0700 | [diff] [blame] | 3111 | return false; |
Akinobu Mita | 933e312 | 2006-12-08 02:39:45 -0800 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | #endif /* CONFIG_FAIL_PAGE_ALLOC */ |
| 3115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | /* |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3117 | * Return true if free base pages are above 'mark'. For high-order checks it |
| 3118 | * will return true of the order-0 watermark is reached and there is at least |
| 3119 | * one free page of a suitable size. Checking now avoids taking the zone lock |
| 3120 | * to check in the allocation paths if no pages are free. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | */ |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 3122 | bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
| 3123 | int classzone_idx, unsigned int alloc_flags, |
| 3124 | long free_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | { |
Christoph Lameter | d23ad42 | 2007-02-10 01:43:02 -0800 | [diff] [blame] | 3126 | long min = mark; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | int o; |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3128 | const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3129 | |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3130 | /* free_pages may go negative - that's OK */ |
Michal Hocko | df0a6da | 2012-01-10 15:08:02 -0800 | [diff] [blame] | 3131 | free_pages -= (1 << order) - 1; |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3132 | |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3133 | if (alloc_flags & ALLOC_HIGH) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | min -= min / 2; |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3135 | |
| 3136 | /* |
| 3137 | * If the caller does not have rights to ALLOC_HARDER then subtract |
| 3138 | * the high-atomic reserves. This will over-estimate the size of the |
| 3139 | * atomic reserve but it avoids a search. |
| 3140 | */ |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3141 | if (likely(!alloc_harder)) { |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3142 | free_pages -= z->nr_reserved_highatomic; |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3143 | } else { |
| 3144 | /* |
| 3145 | * OOM victims can try even harder than normal ALLOC_HARDER |
| 3146 | * users on the grounds that it's definitely going to be in |
| 3147 | * the exit path shortly and free memory. Any allocation it |
| 3148 | * makes during the free path will be small and short-lived. |
| 3149 | */ |
| 3150 | if (alloc_flags & ALLOC_OOM) |
| 3151 | min -= min / 2; |
| 3152 | else |
| 3153 | min -= min / 4; |
| 3154 | } |
| 3155 | |
Mel Gorman | e2b1919 | 2015-11-06 16:28:09 -0800 | [diff] [blame] | 3156 | |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3157 | #ifdef CONFIG_CMA |
| 3158 | /* If allocation can't use CMA areas don't use free CMA pages */ |
| 3159 | if (!(alloc_flags & ALLOC_CMA)) |
| 3160 | free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES); |
| 3161 | #endif |
| 3162 | |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3163 | /* |
| 3164 | * Check watermarks for an order-0 allocation request. If these |
| 3165 | * are not met, then a high-order request also cannot go ahead |
| 3166 | * even if a suitable page happened to be free. |
| 3167 | */ |
| 3168 | if (free_pages <= min + z->lowmem_reserve[classzone_idx]) |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 3169 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3170 | |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3171 | /* If this is an order-0 request then the watermark is fine */ |
| 3172 | if (!order) |
| 3173 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3175 | /* For a high-order request, check at least one suitable page is free */ |
| 3176 | for (o = order; o < MAX_ORDER; o++) { |
| 3177 | struct free_area *area = &z->free_area[o]; |
| 3178 | int mt; |
| 3179 | |
| 3180 | if (!area->nr_free) |
| 3181 | continue; |
| 3182 | |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3183 | for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) { |
| 3184 | if (!list_empty(&area->free_list[mt])) |
| 3185 | return true; |
| 3186 | } |
| 3187 | |
| 3188 | #ifdef CONFIG_CMA |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3189 | if ((alloc_flags & ALLOC_CMA) && |
| 3190 | !list_empty(&area->free_list[MIGRATE_CMA])) { |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3191 | return true; |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3192 | } |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3193 | #endif |
Vlastimil Babka | b050e37 | 2017-11-15 17:38:30 -0800 | [diff] [blame] | 3194 | if (alloc_harder && |
| 3195 | !list_empty(&area->free_list[MIGRATE_HIGHATOMIC])) |
| 3196 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | } |
Mel Gorman | 97a16fc | 2015-11-06 16:28:40 -0800 | [diff] [blame] | 3198 | return false; |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 3199 | } |
| 3200 | |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 3201 | bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3202 | int classzone_idx, unsigned int alloc_flags) |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 3203 | { |
| 3204 | return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags, |
| 3205 | zone_page_state(z, NR_FREE_PAGES)); |
| 3206 | } |
| 3207 | |
Mel Gorman | 48ee5f3 | 2016-05-19 17:14:07 -0700 | [diff] [blame] | 3208 | static inline bool zone_watermark_fast(struct zone *z, unsigned int order, |
| 3209 | unsigned long mark, int classzone_idx, unsigned int alloc_flags) |
| 3210 | { |
| 3211 | long free_pages = zone_page_state(z, NR_FREE_PAGES); |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3212 | long cma_pages = 0; |
| 3213 | |
| 3214 | #ifdef CONFIG_CMA |
| 3215 | /* If allocation can't use CMA areas don't use free CMA pages */ |
| 3216 | if (!(alloc_flags & ALLOC_CMA)) |
| 3217 | cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES); |
| 3218 | #endif |
Mel Gorman | 48ee5f3 | 2016-05-19 17:14:07 -0700 | [diff] [blame] | 3219 | |
| 3220 | /* |
| 3221 | * Fast check for order-0 only. If this fails then the reserves |
| 3222 | * need to be calculated. There is a corner case where the check |
| 3223 | * passes but only the high-order atomic reserve are free. If |
| 3224 | * the caller is !atomic then it'll uselessly search the free |
| 3225 | * list. That corner case is then slower but it is harmless. |
| 3226 | */ |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3227 | if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx]) |
Mel Gorman | 48ee5f3 | 2016-05-19 17:14:07 -0700 | [diff] [blame] | 3228 | return true; |
| 3229 | |
| 3230 | return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags, |
| 3231 | free_pages); |
| 3232 | } |
| 3233 | |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 3234 | bool zone_watermark_ok_safe(struct zone *z, unsigned int order, |
Mel Gorman | e2b1919 | 2015-11-06 16:28:09 -0800 | [diff] [blame] | 3235 | unsigned long mark, int classzone_idx) |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 3236 | { |
| 3237 | long free_pages = zone_page_state(z, NR_FREE_PAGES); |
| 3238 | |
| 3239 | if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark) |
| 3240 | free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES); |
| 3241 | |
Mel Gorman | e2b1919 | 2015-11-06 16:28:09 -0800 | [diff] [blame] | 3242 | return __zone_watermark_ok(z, order, mark, classzone_idx, 0, |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 3243 | free_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | } |
| 3245 | |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 3246 | #ifdef CONFIG_NUMA |
David Rientjes | 957f822 | 2012-10-08 16:33:24 -0700 | [diff] [blame] | 3247 | static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
| 3248 | { |
Gavin Shan | e02dc01 | 2017-02-24 14:59:33 -0800 | [diff] [blame] | 3249 | return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <= |
Mel Gorman | 5f7a75a | 2014-06-04 16:07:15 -0700 | [diff] [blame] | 3250 | RECLAIM_DISTANCE; |
David Rientjes | 957f822 | 2012-10-08 16:33:24 -0700 | [diff] [blame] | 3251 | } |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 3252 | #else /* CONFIG_NUMA */ |
David Rientjes | 957f822 | 2012-10-08 16:33:24 -0700 | [diff] [blame] | 3253 | static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
| 3254 | { |
| 3255 | return true; |
| 3256 | } |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 3257 | #endif /* CONFIG_NUMA */ |
| 3258 | |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3259 | /* |
Paul Jackson | 0798e51 | 2006-12-06 20:31:38 -0800 | [diff] [blame] | 3260 | * get_page_from_freelist goes through the zonelist trying to allocate |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3261 | * a page. |
| 3262 | */ |
| 3263 | static struct page * |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3264 | get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, |
| 3265 | const struct alloc_context *ac) |
Martin Hicks | 753ee72 | 2005-06-21 17:14:41 -0700 | [diff] [blame] | 3266 | { |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 3267 | struct zoneref *z = ac->preferred_zoneref; |
Mel Gorman | 5117f45 | 2009-06-16 15:31:59 -0700 | [diff] [blame] | 3268 | struct zone *zone; |
Mel Gorman | 3b8c0be | 2016-07-28 15:46:53 -0700 | [diff] [blame] | 3269 | struct pglist_data *last_pgdat_dirty_limit = NULL; |
| 3270 | |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3271 | /* |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 3272 | * Scan zonelist, looking for a zone with enough free. |
Vladimir Davydov | 344736f | 2014-10-20 15:50:30 +0400 | [diff] [blame] | 3273 | * See also __cpuset_node_allowed() comment in kernel/cpuset.c. |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3274 | */ |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 3275 | for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3276 | ac->nodemask) { |
Mel Gorman | be06af0 | 2016-05-19 17:13:47 -0700 | [diff] [blame] | 3277 | struct page *page; |
Johannes Weiner | e085dbc | 2013-09-11 14:20:46 -0700 | [diff] [blame] | 3278 | unsigned long mark; |
| 3279 | |
Mel Gorman | 664eedd | 2014-06-04 16:10:08 -0700 | [diff] [blame] | 3280 | if (cpusets_enabled() && |
| 3281 | (alloc_flags & ALLOC_CPUSET) && |
Vlastimil Babka | 002f290 | 2016-05-19 17:14:30 -0700 | [diff] [blame] | 3282 | !__cpuset_zone_allowed(zone, gfp_mask)) |
Mel Gorman | cd38b11 | 2011-07-25 17:12:29 -0700 | [diff] [blame] | 3283 | continue; |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3284 | /* |
| 3285 | * When allocating a page cache page for writing, we |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 3286 | * want to get it from a node that is within its dirty |
| 3287 | * limit, such that no single node holds more than its |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3288 | * proportional share of globally allowed dirty pages. |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 3289 | * The dirty limits take into account the node's |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3290 | * lowmem reserves and high watermark so that kswapd |
| 3291 | * should be able to balance it without having to |
| 3292 | * write pages from its LRU list. |
| 3293 | * |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3294 | * XXX: For now, allow allocations to potentially |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 3295 | * exceed the per-node dirty limit in the slowpath |
Mel Gorman | c9ab0c4 | 2015-11-06 16:28:12 -0800 | [diff] [blame] | 3296 | * (spread_dirty_pages unset) before going into reclaim, |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3297 | * which is important when on a NUMA setup the allowed |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 3298 | * nodes are together not big enough to reach the |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3299 | * global limit. The proper fix for these situations |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 3300 | * will require awareness of nodes in the |
Johannes Weiner | a756cf5 | 2012-01-10 15:07:49 -0800 | [diff] [blame] | 3301 | * dirty-throttling and the flusher threads. |
| 3302 | */ |
Mel Gorman | 3b8c0be | 2016-07-28 15:46:53 -0700 | [diff] [blame] | 3303 | if (ac->spread_dirty_pages) { |
| 3304 | if (last_pgdat_dirty_limit == zone->zone_pgdat) |
| 3305 | continue; |
| 3306 | |
| 3307 | if (!node_dirty_ok(zone->zone_pgdat)) { |
| 3308 | last_pgdat_dirty_limit = zone->zone_pgdat; |
| 3309 | continue; |
| 3310 | } |
| 3311 | } |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3312 | |
Johannes Weiner | e085dbc | 2013-09-11 14:20:46 -0700 | [diff] [blame] | 3313 | mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK]; |
Mel Gorman | 48ee5f3 | 2016-05-19 17:14:07 -0700 | [diff] [blame] | 3314 | if (!zone_watermark_fast(zone, order, mark, |
Mel Gorman | 93ea996 | 2016-05-19 17:14:13 -0700 | [diff] [blame] | 3315 | ac_classzone_idx(ac), alloc_flags)) { |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3316 | int ret; |
| 3317 | |
Pavel Tatashin | c9e97a1 | 2018-04-05 16:22:31 -0700 | [diff] [blame] | 3318 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| 3319 | /* |
| 3320 | * Watermark failed for this zone, but see if we can |
| 3321 | * grow this zone if it contains deferred pages. |
| 3322 | */ |
| 3323 | if (static_branch_unlikely(&deferred_pages)) { |
| 3324 | if (_deferred_grow_zone(zone, order)) |
| 3325 | goto try_this_zone; |
| 3326 | } |
| 3327 | #endif |
Mel Gorman | 5dab291 | 2014-06-04 16:10:14 -0700 | [diff] [blame] | 3328 | /* Checked here to keep the fast path fast */ |
| 3329 | BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK); |
| 3330 | if (alloc_flags & ALLOC_NO_WATERMARKS) |
| 3331 | goto try_this_zone; |
| 3332 | |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 3333 | if (node_reclaim_mode == 0 || |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 3334 | !zone_allows_reclaim(ac->preferred_zoneref->zone, zone)) |
Mel Gorman | cd38b11 | 2011-07-25 17:12:29 -0700 | [diff] [blame] | 3335 | continue; |
| 3336 | |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 3337 | ret = node_reclaim(zone->zone_pgdat, gfp_mask, order); |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3338 | switch (ret) { |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 3339 | case NODE_RECLAIM_NOSCAN: |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3340 | /* did not scan */ |
Mel Gorman | cd38b11 | 2011-07-25 17:12:29 -0700 | [diff] [blame] | 3341 | continue; |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 3342 | case NODE_RECLAIM_FULL: |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3343 | /* scanned but unreclaimable */ |
Mel Gorman | cd38b11 | 2011-07-25 17:12:29 -0700 | [diff] [blame] | 3344 | continue; |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3345 | default: |
| 3346 | /* did we reclaim enough */ |
Mel Gorman | fed2719 | 2013-04-29 15:07:57 -0700 | [diff] [blame] | 3347 | if (zone_watermark_ok(zone, order, mark, |
Mel Gorman | 93ea996 | 2016-05-19 17:14:13 -0700 | [diff] [blame] | 3348 | ac_classzone_idx(ac), alloc_flags)) |
Mel Gorman | fed2719 | 2013-04-29 15:07:57 -0700 | [diff] [blame] | 3349 | goto try_this_zone; |
| 3350 | |
Mel Gorman | fed2719 | 2013-04-29 15:07:57 -0700 | [diff] [blame] | 3351 | continue; |
Paul Jackson | 0798e51 | 2006-12-06 20:31:38 -0800 | [diff] [blame] | 3352 | } |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3353 | } |
| 3354 | |
Mel Gorman | fa5e084 | 2009-06-16 15:33:22 -0700 | [diff] [blame] | 3355 | try_this_zone: |
Mel Gorman | 066b239 | 2017-02-24 14:56:26 -0800 | [diff] [blame] | 3356 | page = rmqueue(ac->preferred_zoneref->zone, zone, order, |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3357 | gfp_mask, alloc_flags, ac->migratetype); |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 3358 | if (page) { |
Mel Gorman | 479f854 | 2016-05-19 17:14:35 -0700 | [diff] [blame] | 3359 | prep_new_page(page, order, gfp_mask, alloc_flags); |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3360 | |
| 3361 | /* |
| 3362 | * If this is a high-order atomic allocation then check |
| 3363 | * if the pageblock should be reserved for the future |
| 3364 | */ |
| 3365 | if (unlikely(order && (alloc_flags & ALLOC_HARDER))) |
| 3366 | reserve_highatomic_pageblock(page, zone, order); |
| 3367 | |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 3368 | return page; |
Pavel Tatashin | c9e97a1 | 2018-04-05 16:22:31 -0700 | [diff] [blame] | 3369 | } else { |
| 3370 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| 3371 | /* Try again if zone has deferred pages */ |
| 3372 | if (static_branch_unlikely(&deferred_pages)) { |
| 3373 | if (_deferred_grow_zone(zone, order)) |
| 3374 | goto try_this_zone; |
| 3375 | } |
| 3376 | #endif |
Vlastimil Babka | 7537919 | 2015-02-11 15:25:38 -0800 | [diff] [blame] | 3377 | } |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 3378 | } |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 3379 | |
Mel Gorman | 4ffeaf3 | 2014-08-06 16:07:22 -0700 | [diff] [blame] | 3380 | return NULL; |
Martin Hicks | 753ee72 | 2005-06-21 17:14:41 -0700 | [diff] [blame] | 3381 | } |
| 3382 | |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 3383 | static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask) |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3384 | { |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3385 | unsigned int filter = SHOW_MEM_FILTER_NODES; |
Michal Hocko | aa18750 | 2017-02-22 15:41:45 -0800 | [diff] [blame] | 3386 | static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1); |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3387 | |
Michal Hocko | 2c029a1 | 2018-10-26 15:06:49 -0700 | [diff] [blame] | 3388 | if (!__ratelimit(&show_mem_rs)) |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3389 | return; |
| 3390 | |
| 3391 | /* |
| 3392 | * This documents exceptions given to allocations in certain |
| 3393 | * contexts that are allowed to allocate outside current's set |
| 3394 | * of allowed nodes. |
| 3395 | */ |
| 3396 | if (!(gfp_mask & __GFP_NOMEMALLOC)) |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3397 | if (tsk_is_oom_victim(current) || |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3398 | (current->flags & (PF_MEMALLOC | PF_EXITING))) |
| 3399 | filter &= ~SHOW_MEM_FILTER_NODES; |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 3400 | if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM)) |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3401 | filter &= ~SHOW_MEM_FILTER_NODES; |
| 3402 | |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 3403 | show_mem(filter, nodemask); |
Michal Hocko | aa18750 | 2017-02-22 15:41:45 -0800 | [diff] [blame] | 3404 | } |
| 3405 | |
Michal Hocko | a8e9925 | 2017-02-22 15:46:10 -0800 | [diff] [blame] | 3406 | void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...) |
Michal Hocko | aa18750 | 2017-02-22 15:41:45 -0800 | [diff] [blame] | 3407 | { |
| 3408 | struct va_format vaf; |
| 3409 | va_list args; |
| 3410 | static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 3411 | DEFAULT_RATELIMIT_BURST); |
| 3412 | |
Tetsuo Handa | 0f7896f | 2017-05-03 14:55:34 -0700 | [diff] [blame] | 3413 | if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs)) |
Michal Hocko | aa18750 | 2017-02-22 15:41:45 -0800 | [diff] [blame] | 3414 | return; |
| 3415 | |
Michal Hocko | 7877cdc | 2016-10-07 17:01:55 -0700 | [diff] [blame] | 3416 | va_start(args, fmt); |
| 3417 | vaf.fmt = fmt; |
| 3418 | vaf.va = &args; |
Michal Hocko | 0205f75 | 2017-11-15 17:39:14 -0800 | [diff] [blame] | 3419 | pr_warn("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl\n", |
| 3420 | current->comm, &vaf, gfp_mask, &gfp_mask, |
| 3421 | nodemask_pr_args(nodemask)); |
Michal Hocko | 7877cdc | 2016-10-07 17:01:55 -0700 | [diff] [blame] | 3422 | va_end(args); |
Joe Perches | 3ee9a4f | 2011-10-31 17:08:35 -0700 | [diff] [blame] | 3423 | |
Michal Hocko | a8e9925 | 2017-02-22 15:46:10 -0800 | [diff] [blame] | 3424 | cpuset_print_current_mems_allowed(); |
Joe Perches | 3ee9a4f | 2011-10-31 17:08:35 -0700 | [diff] [blame] | 3425 | |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3426 | dump_stack(); |
David Rientjes | 685dbf6 | 2017-02-22 15:46:28 -0800 | [diff] [blame] | 3427 | warn_alloc_show_mem(gfp_mask, nodemask); |
Dave Hansen | a238ab5 | 2011-05-24 17:12:16 -0700 | [diff] [blame] | 3428 | } |
| 3429 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3430 | static inline struct page * |
Michal Hocko | 6c18ba7 | 2017-02-22 15:46:25 -0800 | [diff] [blame] | 3431 | __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order, |
| 3432 | unsigned int alloc_flags, |
| 3433 | const struct alloc_context *ac) |
| 3434 | { |
| 3435 | struct page *page; |
| 3436 | |
| 3437 | page = get_page_from_freelist(gfp_mask, order, |
| 3438 | alloc_flags|ALLOC_CPUSET, ac); |
| 3439 | /* |
| 3440 | * fallback to ignore cpuset restriction if our nodes |
| 3441 | * are depleted |
| 3442 | */ |
| 3443 | if (!page) |
| 3444 | page = get_page_from_freelist(gfp_mask, order, |
| 3445 | alloc_flags, ac); |
| 3446 | |
| 3447 | return page; |
| 3448 | } |
| 3449 | |
| 3450 | static inline struct page * |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3451 | __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3452 | const struct alloc_context *ac, unsigned long *did_some_progress) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3453 | { |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 3454 | struct oom_control oc = { |
| 3455 | .zonelist = ac->zonelist, |
| 3456 | .nodemask = ac->nodemask, |
Vladimir Davydov | 2a966b7 | 2016-07-26 15:22:33 -0700 | [diff] [blame] | 3457 | .memcg = NULL, |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 3458 | .gfp_mask = gfp_mask, |
| 3459 | .order = order, |
David Rientjes | 6e0fc46 | 2015-09-08 15:00:36 -0700 | [diff] [blame] | 3460 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | |
Johannes Weiner | 9879de7 | 2015-01-26 12:58:32 -0800 | [diff] [blame] | 3463 | *did_some_progress = 0; |
| 3464 | |
Johannes Weiner | 9879de7 | 2015-01-26 12:58:32 -0800 | [diff] [blame] | 3465 | /* |
Johannes Weiner | dc56401 | 2015-06-24 16:57:19 -0700 | [diff] [blame] | 3466 | * Acquire the oom lock. If that fails, somebody else is |
| 3467 | * making progress for us. |
Johannes Weiner | 9879de7 | 2015-01-26 12:58:32 -0800 | [diff] [blame] | 3468 | */ |
Johannes Weiner | dc56401 | 2015-06-24 16:57:19 -0700 | [diff] [blame] | 3469 | if (!mutex_trylock(&oom_lock)) { |
Johannes Weiner | 9879de7 | 2015-01-26 12:58:32 -0800 | [diff] [blame] | 3470 | *did_some_progress = 1; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3471 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | return NULL; |
| 3473 | } |
Jens Axboe | 6b1de91 | 2005-11-17 21:35:02 +0100 | [diff] [blame] | 3474 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3475 | /* |
| 3476 | * Go through the zonelist yet one more time, keep very high watermark |
| 3477 | * here, this is only to catch a parallel oom killing, we must fail if |
Tetsuo Handa | e746bf7 | 2017-08-31 16:15:20 -0700 | [diff] [blame] | 3478 | * we're still under heavy pressure. But make sure that this reclaim |
| 3479 | * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY |
| 3480 | * allocation which will never fail due to oom_lock already held. |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3481 | */ |
Tetsuo Handa | e746bf7 | 2017-08-31 16:15:20 -0700 | [diff] [blame] | 3482 | page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) & |
| 3483 | ~__GFP_DIRECT_RECLAIM, order, |
| 3484 | ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac); |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 3485 | if (page) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3486 | goto out; |
| 3487 | |
Michal Hocko | 06ad276 | 2017-02-22 15:46:22 -0800 | [diff] [blame] | 3488 | /* Coredumps can quickly deplete all memory reserves */ |
| 3489 | if (current->flags & PF_DUMPCORE) |
| 3490 | goto out; |
| 3491 | /* The OOM killer will not help higher order allocs */ |
| 3492 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
| 3493 | goto out; |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 3494 | /* |
| 3495 | * We have already exhausted all our reclaim opportunities without any |
| 3496 | * success so it is time to admit defeat. We will skip the OOM killer |
| 3497 | * because it is very likely that the caller has a more reasonable |
| 3498 | * fallback than shooting a random task. |
| 3499 | */ |
| 3500 | if (gfp_mask & __GFP_RETRY_MAYFAIL) |
| 3501 | goto out; |
Michal Hocko | 06ad276 | 2017-02-22 15:46:22 -0800 | [diff] [blame] | 3502 | /* The OOM killer does not needlessly kill tasks for lowmem */ |
| 3503 | if (ac->high_zoneidx < ZONE_NORMAL) |
| 3504 | goto out; |
| 3505 | if (pm_suspended_storage()) |
| 3506 | goto out; |
| 3507 | /* |
| 3508 | * XXX: GFP_NOFS allocations should rather fail than rely on |
| 3509 | * other request to make a forward progress. |
| 3510 | * We are in an unfortunate situation where out_of_memory cannot |
| 3511 | * do much for this context but let's try it to at least get |
| 3512 | * access to memory reserved if the current task is killed (see |
| 3513 | * out_of_memory). Once filesystems are ready to handle allocation |
| 3514 | * failures more gracefully we should just bail out here. |
| 3515 | */ |
Michal Hocko | 3da88fb3 | 2016-05-19 17:13:09 -0700 | [diff] [blame] | 3516 | |
Michal Hocko | 06ad276 | 2017-02-22 15:46:22 -0800 | [diff] [blame] | 3517 | /* The OOM killer may not free memory on a specific node */ |
| 3518 | if (gfp_mask & __GFP_THISNODE) |
| 3519 | goto out; |
| 3520 | |
Shile Zhang | 3c2c648 | 2018-01-31 16:20:07 -0800 | [diff] [blame] | 3521 | /* Exhausted what can be done so it's blame time */ |
Michal Hocko | 5020e28 | 2016-01-14 15:20:36 -0800 | [diff] [blame] | 3522 | if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) { |
Michal Hocko | c32b3cb | 2015-02-11 15:26:24 -0800 | [diff] [blame] | 3523 | *did_some_progress = 1; |
Michal Hocko | 5020e28 | 2016-01-14 15:20:36 -0800 | [diff] [blame] | 3524 | |
Michal Hocko | 6c18ba7 | 2017-02-22 15:46:25 -0800 | [diff] [blame] | 3525 | /* |
| 3526 | * Help non-failing allocations by giving them access to memory |
| 3527 | * reserves |
| 3528 | */ |
| 3529 | if (gfp_mask & __GFP_NOFAIL) |
| 3530 | page = __alloc_pages_cpuset_fallback(gfp_mask, order, |
Michal Hocko | 5020e28 | 2016-01-14 15:20:36 -0800 | [diff] [blame] | 3531 | ALLOC_NO_WATERMARKS, ac); |
Michal Hocko | 5020e28 | 2016-01-14 15:20:36 -0800 | [diff] [blame] | 3532 | } |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3533 | out: |
Johannes Weiner | dc56401 | 2015-06-24 16:57:19 -0700 | [diff] [blame] | 3534 | mutex_unlock(&oom_lock); |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3535 | return page; |
| 3536 | } |
| 3537 | |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3538 | /* |
| 3539 | * Maximum number of compaction retries wit a progress before OOM |
| 3540 | * killer is consider as the only way to move forward. |
| 3541 | */ |
| 3542 | #define MAX_COMPACT_RETRIES 16 |
| 3543 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3544 | #ifdef CONFIG_COMPACTION |
| 3545 | /* Try memory compaction for high-order allocations before reclaim */ |
| 3546 | static struct page * |
| 3547 | __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3548 | unsigned int alloc_flags, const struct alloc_context *ac, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 3549 | enum compact_priority prio, enum compact_result *compact_result) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3550 | { |
Vlastimil Babka | 98dd3b4 | 2014-10-09 15:27:04 -0700 | [diff] [blame] | 3551 | struct page *page; |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3552 | unsigned long pflags; |
Vlastimil Babka | 499118e | 2017-05-08 15:59:50 -0700 | [diff] [blame] | 3553 | unsigned int noreclaim_flag; |
Vlastimil Babka | 53853e2 | 2014-10-09 15:27:02 -0700 | [diff] [blame] | 3554 | |
Mel Gorman | 6619971 | 2012-01-12 17:19:41 -0800 | [diff] [blame] | 3555 | if (!order) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3556 | return NULL; |
| 3557 | |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3558 | psi_memstall_enter(&pflags); |
Vlastimil Babka | 499118e | 2017-05-08 15:59:50 -0700 | [diff] [blame] | 3559 | noreclaim_flag = memalloc_noreclaim_save(); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3560 | |
Michal Hocko | c5d01d0 | 2016-05-20 16:56:53 -0700 | [diff] [blame] | 3561 | *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac, |
Vlastimil Babka | c3486f5 | 2016-07-28 15:49:30 -0700 | [diff] [blame] | 3562 | prio); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3563 | |
Vlastimil Babka | 499118e | 2017-05-08 15:59:50 -0700 | [diff] [blame] | 3564 | memalloc_noreclaim_restore(noreclaim_flag); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3565 | psi_memstall_leave(&pflags); |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3566 | |
Michal Hocko | c5d01d0 | 2016-05-20 16:56:53 -0700 | [diff] [blame] | 3567 | if (*compact_result <= COMPACT_INACTIVE) |
Vlastimil Babka | 98dd3b4 | 2014-10-09 15:27:04 -0700 | [diff] [blame] | 3568 | return NULL; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3569 | |
Vlastimil Babka | 98dd3b4 | 2014-10-09 15:27:04 -0700 | [diff] [blame] | 3570 | /* |
| 3571 | * At least in one zone compaction wasn't deferred or skipped, so let's |
| 3572 | * count a compaction stall |
| 3573 | */ |
| 3574 | count_vm_event(COMPACTSTALL); |
| 3575 | |
Vlastimil Babka | 31a6c19 | 2016-07-28 15:49:13 -0700 | [diff] [blame] | 3576 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
Vlastimil Babka | 98dd3b4 | 2014-10-09 15:27:04 -0700 | [diff] [blame] | 3577 | |
| 3578 | if (page) { |
| 3579 | struct zone *zone = page_zone(page); |
| 3580 | |
| 3581 | zone->compact_blockskip_flush = false; |
| 3582 | compaction_defer_reset(zone, order, true); |
| 3583 | count_vm_event(COMPACTSUCCESS); |
| 3584 | return page; |
| 3585 | } |
| 3586 | |
| 3587 | /* |
Vlastimil Babka | 98dd3b4 | 2014-10-09 15:27:04 -0700 | [diff] [blame] | 3588 | * It's bad if compaction run occurs and fails. The most likely reason |
| 3589 | * is that pages exist, but not enough to satisfy watermarks. |
| 3590 | */ |
| 3591 | count_vm_event(COMPACTFAIL); |
| 3592 | |
| 3593 | cond_resched(); |
| 3594 | |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3595 | return NULL; |
| 3596 | } |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3597 | |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3598 | static inline bool |
| 3599 | should_compact_retry(struct alloc_context *ac, int order, int alloc_flags, |
| 3600 | enum compact_result compact_result, |
| 3601 | enum compact_priority *compact_priority, |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3602 | int *compaction_retries) |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3603 | { |
| 3604 | int max_retries = MAX_COMPACT_RETRIES; |
Vlastimil Babka | c2033b0 | 2016-10-07 17:00:34 -0700 | [diff] [blame] | 3605 | int min_priority; |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3606 | bool ret = false; |
| 3607 | int retries = *compaction_retries; |
| 3608 | enum compact_priority priority = *compact_priority; |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3609 | |
| 3610 | if (!order) |
| 3611 | return false; |
| 3612 | |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3613 | if (compaction_made_progress(compact_result)) |
| 3614 | (*compaction_retries)++; |
| 3615 | |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3616 | /* |
| 3617 | * compaction considers all the zone as desperately out of memory |
| 3618 | * so it doesn't really make much sense to retry except when the |
| 3619 | * failure could be caused by insufficient priority |
| 3620 | */ |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3621 | if (compaction_failed(compact_result)) |
| 3622 | goto check_priority; |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3623 | |
| 3624 | /* |
| 3625 | * make sure the compaction wasn't deferred or didn't bail out early |
| 3626 | * due to locks contention before we declare that we should give up. |
| 3627 | * But do not retry if the given zonelist is not suitable for |
| 3628 | * compaction. |
| 3629 | */ |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3630 | if (compaction_withdrawn(compact_result)) { |
| 3631 | ret = compaction_zonelist_suitable(ac, order, alloc_flags); |
| 3632 | goto out; |
| 3633 | } |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3634 | |
| 3635 | /* |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 3636 | * !costly requests are much more important than __GFP_RETRY_MAYFAIL |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3637 | * costly ones because they are de facto nofail and invoke OOM |
| 3638 | * killer to move on while costly can fail and users are ready |
| 3639 | * to cope with that. 1/4 retries is rather arbitrary but we |
| 3640 | * would need much more detailed feedback from compaction to |
| 3641 | * make a better decision. |
| 3642 | */ |
| 3643 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
| 3644 | max_retries /= 4; |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3645 | if (*compaction_retries <= max_retries) { |
| 3646 | ret = true; |
| 3647 | goto out; |
| 3648 | } |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3649 | |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3650 | /* |
| 3651 | * Make sure there are attempts at the highest priority if we exhausted |
| 3652 | * all retries or failed at the lower priorities. |
| 3653 | */ |
| 3654 | check_priority: |
Vlastimil Babka | c2033b0 | 2016-10-07 17:00:34 -0700 | [diff] [blame] | 3655 | min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ? |
| 3656 | MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY; |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3657 | |
Vlastimil Babka | c2033b0 | 2016-10-07 17:00:34 -0700 | [diff] [blame] | 3658 | if (*compact_priority > min_priority) { |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3659 | (*compact_priority)--; |
| 3660 | *compaction_retries = 0; |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3661 | ret = true; |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3662 | } |
Michal Hocko | 65190cf | 2017-02-22 15:42:03 -0800 | [diff] [blame] | 3663 | out: |
| 3664 | trace_compact_retry(order, priority, compact_result, retries, max_retries, ret); |
| 3665 | return ret; |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3666 | } |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3667 | #else |
| 3668 | static inline struct page * |
| 3669 | __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3670 | unsigned int alloc_flags, const struct alloc_context *ac, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 3671 | enum compact_priority prio, enum compact_result *compact_result) |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3672 | { |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3673 | *compact_result = COMPACT_SKIPPED; |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3674 | return NULL; |
| 3675 | } |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3676 | |
| 3677 | static inline bool |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 3678 | should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags, |
| 3679 | enum compact_result compact_result, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 3680 | enum compact_priority *compact_priority, |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 3681 | int *compaction_retries) |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3682 | { |
Michal Hocko | 31e49bf | 2016-05-20 16:57:15 -0700 | [diff] [blame] | 3683 | struct zone *zone; |
| 3684 | struct zoneref *z; |
| 3685 | |
| 3686 | if (!order || order > PAGE_ALLOC_COSTLY_ORDER) |
| 3687 | return false; |
| 3688 | |
| 3689 | /* |
| 3690 | * There are setups with compaction disabled which would prefer to loop |
| 3691 | * inside the allocator rather than hit the oom killer prematurely. |
| 3692 | * Let's give them a good hope and keep retrying while the order-0 |
| 3693 | * watermarks are OK. |
| 3694 | */ |
| 3695 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| 3696 | ac->nodemask) { |
| 3697 | if (zone_watermark_ok(zone, 0, min_wmark_pages(zone), |
| 3698 | ac_classzone_idx(ac), alloc_flags)) |
| 3699 | return true; |
| 3700 | } |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 3701 | return false; |
| 3702 | } |
Vlastimil Babka | 3250845 | 2016-10-07 17:00:28 -0700 | [diff] [blame] | 3703 | #endif /* CONFIG_COMPACTION */ |
Mel Gorman | 56de726 | 2010-05-24 14:32:30 -0700 | [diff] [blame] | 3704 | |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3705 | #ifdef CONFIG_LOCKDEP |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3706 | static struct lockdep_map __fs_reclaim_map = |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3707 | STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map); |
| 3708 | |
| 3709 | static bool __need_fs_reclaim(gfp_t gfp_mask) |
| 3710 | { |
| 3711 | gfp_mask = current_gfp_context(gfp_mask); |
| 3712 | |
| 3713 | /* no reclaim without waiting on it */ |
| 3714 | if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) |
| 3715 | return false; |
| 3716 | |
| 3717 | /* this guy won't enter reclaim */ |
Tetsuo Handa | 2e517d68 | 2018-03-22 16:17:10 -0700 | [diff] [blame] | 3718 | if (current->flags & PF_MEMALLOC) |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3719 | return false; |
| 3720 | |
| 3721 | /* We're only interested __GFP_FS allocations for now */ |
| 3722 | if (!(gfp_mask & __GFP_FS)) |
| 3723 | return false; |
| 3724 | |
| 3725 | if (gfp_mask & __GFP_NOLOCKDEP) |
| 3726 | return false; |
| 3727 | |
| 3728 | return true; |
| 3729 | } |
| 3730 | |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3731 | void __fs_reclaim_acquire(void) |
| 3732 | { |
| 3733 | lock_map_acquire(&__fs_reclaim_map); |
| 3734 | } |
| 3735 | |
| 3736 | void __fs_reclaim_release(void) |
| 3737 | { |
| 3738 | lock_map_release(&__fs_reclaim_map); |
| 3739 | } |
| 3740 | |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3741 | void fs_reclaim_acquire(gfp_t gfp_mask) |
| 3742 | { |
| 3743 | if (__need_fs_reclaim(gfp_mask)) |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3744 | __fs_reclaim_acquire(); |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3745 | } |
| 3746 | EXPORT_SYMBOL_GPL(fs_reclaim_acquire); |
| 3747 | |
| 3748 | void fs_reclaim_release(gfp_t gfp_mask) |
| 3749 | { |
| 3750 | if (__need_fs_reclaim(gfp_mask)) |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3751 | __fs_reclaim_release(); |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3752 | } |
| 3753 | EXPORT_SYMBOL_GPL(fs_reclaim_release); |
| 3754 | #endif |
| 3755 | |
Marek Szyprowski | bba9071 | 2012-01-25 12:09:52 +0100 | [diff] [blame] | 3756 | /* Perform direct synchronous page reclaim */ |
| 3757 | static int |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3758 | __perform_reclaim(gfp_t gfp_mask, unsigned int order, |
| 3759 | const struct alloc_context *ac) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3760 | { |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3761 | struct reclaim_state reclaim_state; |
Marek Szyprowski | bba9071 | 2012-01-25 12:09:52 +0100 | [diff] [blame] | 3762 | int progress; |
Vlastimil Babka | 499118e | 2017-05-08 15:59:50 -0700 | [diff] [blame] | 3763 | unsigned int noreclaim_flag; |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3764 | unsigned long pflags; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3765 | |
| 3766 | cond_resched(); |
| 3767 | |
| 3768 | /* We now go into synchronous reclaim */ |
| 3769 | cpuset_memory_pressure_bump(); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3770 | psi_memstall_enter(&pflags); |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 3771 | fs_reclaim_acquire(gfp_mask); |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3772 | noreclaim_flag = memalloc_noreclaim_save(); |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3773 | reclaim_state.reclaimed_slab = 0; |
Andrew Morton | c06b1fc | 2011-01-13 15:47:32 -0800 | [diff] [blame] | 3774 | current->reclaim_state = &reclaim_state; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3775 | |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3776 | progress = try_to_free_pages(ac->zonelist, order, gfp_mask, |
| 3777 | ac->nodemask); |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3778 | |
Andrew Morton | c06b1fc | 2011-01-13 15:47:32 -0800 | [diff] [blame] | 3779 | current->reclaim_state = NULL; |
Vlastimil Babka | 499118e | 2017-05-08 15:59:50 -0700 | [diff] [blame] | 3780 | memalloc_noreclaim_restore(noreclaim_flag); |
Omar Sandoval | 9378132 | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 3781 | fs_reclaim_release(gfp_mask); |
Johannes Weiner | eb41468 | 2018-10-26 15:06:27 -0700 | [diff] [blame] | 3782 | psi_memstall_leave(&pflags); |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3783 | |
| 3784 | cond_resched(); |
| 3785 | |
Marek Szyprowski | bba9071 | 2012-01-25 12:09:52 +0100 | [diff] [blame] | 3786 | return progress; |
| 3787 | } |
| 3788 | |
| 3789 | /* The really slow allocator path where we enter direct reclaim */ |
| 3790 | static inline struct page * |
| 3791 | __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order, |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3792 | unsigned int alloc_flags, const struct alloc_context *ac, |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3793 | unsigned long *did_some_progress) |
Marek Szyprowski | bba9071 | 2012-01-25 12:09:52 +0100 | [diff] [blame] | 3794 | { |
| 3795 | struct page *page = NULL; |
| 3796 | bool drained = false; |
| 3797 | |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 3798 | *did_some_progress = __perform_reclaim(gfp_mask, order, ac); |
Mel Gorman | 9ee493c | 2010-09-09 16:38:18 -0700 | [diff] [blame] | 3799 | if (unlikely(!(*did_some_progress))) |
| 3800 | return NULL; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3801 | |
Mel Gorman | 9ee493c | 2010-09-09 16:38:18 -0700 | [diff] [blame] | 3802 | retry: |
Vlastimil Babka | 31a6c19 | 2016-07-28 15:49:13 -0700 | [diff] [blame] | 3803 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
Mel Gorman | 9ee493c | 2010-09-09 16:38:18 -0700 | [diff] [blame] | 3804 | |
| 3805 | /* |
| 3806 | * If an allocation failed after direct reclaim, it could be because |
Mel Gorman | 0aaa29a | 2015-11-06 16:28:37 -0800 | [diff] [blame] | 3807 | * pages are pinned on the per-cpu lists or in high alloc reserves. |
| 3808 | * Shrink them them and try again |
Mel Gorman | 9ee493c | 2010-09-09 16:38:18 -0700 | [diff] [blame] | 3809 | */ |
| 3810 | if (!page && !drained) { |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 3811 | unreserve_highatomic_pageblock(ac, false); |
Vlastimil Babka | 93481ff | 2014-12-10 15:43:01 -0800 | [diff] [blame] | 3812 | drain_all_pages(NULL); |
Mel Gorman | 9ee493c | 2010-09-09 16:38:18 -0700 | [diff] [blame] | 3813 | drained = true; |
| 3814 | goto retry; |
| 3815 | } |
| 3816 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3817 | return page; |
| 3818 | } |
| 3819 | |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 3820 | static void wake_all_kswapds(unsigned int order, gfp_t gfp_mask, |
| 3821 | const struct alloc_context *ac) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3822 | { |
| 3823 | struct zoneref *z; |
| 3824 | struct zone *zone; |
Mel Gorman | e1a5563 | 2016-07-28 15:46:26 -0700 | [diff] [blame] | 3825 | pg_data_t *last_pgdat = NULL; |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 3826 | enum zone_type high_zoneidx = ac->high_zoneidx; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3827 | |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 3828 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, high_zoneidx, |
| 3829 | ac->nodemask) { |
Mel Gorman | e1a5563 | 2016-07-28 15:46:26 -0700 | [diff] [blame] | 3830 | if (last_pgdat != zone->zone_pgdat) |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 3831 | wakeup_kswapd(zone, gfp_mask, order, high_zoneidx); |
Mel Gorman | e1a5563 | 2016-07-28 15:46:26 -0700 | [diff] [blame] | 3832 | last_pgdat = zone->zone_pgdat; |
| 3833 | } |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 3834 | } |
| 3835 | |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3836 | static inline unsigned int |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3837 | gfp_to_alloc_flags(gfp_t gfp_mask) |
| 3838 | { |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 3839 | unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET; |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3840 | |
Mel Gorman | a56f57f | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3841 | /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */ |
Namhyung Kim | e6223a3 | 2010-10-26 14:21:59 -0700 | [diff] [blame] | 3842 | BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH); |
Mel Gorman | a56f57f | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3843 | |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3844 | /* |
| 3845 | * The caller may dip into page reserves a bit more if the caller |
| 3846 | * cannot run direct reclaim, or if the caller has realtime scheduling |
| 3847 | * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 3848 | * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3849 | */ |
Namhyung Kim | e6223a3 | 2010-10-26 14:21:59 -0700 | [diff] [blame] | 3850 | alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH); |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3851 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 3852 | if (gfp_mask & __GFP_ATOMIC) { |
Andrea Arcangeli | 5c3240d | 2011-01-13 15:46:49 -0800 | [diff] [blame] | 3853 | /* |
David Rientjes | b104a35 | 2014-07-30 16:08:24 -0700 | [diff] [blame] | 3854 | * Not worth trying to allocate harder for __GFP_NOMEMALLOC even |
| 3855 | * if it can't schedule. |
Andrea Arcangeli | 5c3240d | 2011-01-13 15:46:49 -0800 | [diff] [blame] | 3856 | */ |
David Rientjes | b104a35 | 2014-07-30 16:08:24 -0700 | [diff] [blame] | 3857 | if (!(gfp_mask & __GFP_NOMEMALLOC)) |
Andrea Arcangeli | 5c3240d | 2011-01-13 15:46:49 -0800 | [diff] [blame] | 3858 | alloc_flags |= ALLOC_HARDER; |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3859 | /* |
David Rientjes | b104a35 | 2014-07-30 16:08:24 -0700 | [diff] [blame] | 3860 | * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the |
Vladimir Davydov | 344736f | 2014-10-20 15:50:30 +0400 | [diff] [blame] | 3861 | * comment for __cpuset_node_allowed(). |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3862 | */ |
| 3863 | alloc_flags &= ~ALLOC_CPUSET; |
Andrew Morton | c06b1fc | 2011-01-13 15:47:32 -0800 | [diff] [blame] | 3864 | } else if (unlikely(rt_task(current)) && !in_interrupt()) |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3865 | alloc_flags |= ALLOC_HARDER; |
| 3866 | |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 3867 | #ifdef CONFIG_CMA |
| 3868 | if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) |
| 3869 | alloc_flags |= ALLOC_CMA; |
| 3870 | #endif |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 3871 | return alloc_flags; |
| 3872 | } |
| 3873 | |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3874 | static bool oom_reserves_allowed(struct task_struct *tsk) |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3875 | { |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3876 | if (!tsk_is_oom_victim(tsk)) |
Vlastimil Babka | 31a6c19 | 2016-07-28 15:49:13 -0700 | [diff] [blame] | 3877 | return false; |
| 3878 | |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3879 | /* |
| 3880 | * !MMU doesn't have oom reaper so give access to memory reserves |
| 3881 | * only to the thread with TIF_MEMDIE set |
| 3882 | */ |
| 3883 | if (!IS_ENABLED(CONFIG_MMU) && !test_thread_flag(TIF_MEMDIE)) |
| 3884 | return false; |
Vlastimil Babka | 31a6c19 | 2016-07-28 15:49:13 -0700 | [diff] [blame] | 3885 | |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 3886 | return true; |
| 3887 | } |
| 3888 | |
| 3889 | /* |
| 3890 | * Distinguish requests which really need access to full memory |
| 3891 | * reserves from oom victims which can live with a portion of it |
| 3892 | */ |
| 3893 | static inline int __gfp_pfmemalloc_flags(gfp_t gfp_mask) |
| 3894 | { |
| 3895 | if (unlikely(gfp_mask & __GFP_NOMEMALLOC)) |
| 3896 | return 0; |
| 3897 | if (gfp_mask & __GFP_MEMALLOC) |
| 3898 | return ALLOC_NO_WATERMARKS; |
| 3899 | if (in_serving_softirq() && (current->flags & PF_MEMALLOC)) |
| 3900 | return ALLOC_NO_WATERMARKS; |
| 3901 | if (!in_interrupt()) { |
| 3902 | if (current->flags & PF_MEMALLOC) |
| 3903 | return ALLOC_NO_WATERMARKS; |
| 3904 | else if (oom_reserves_allowed(current)) |
| 3905 | return ALLOC_OOM; |
| 3906 | } |
| 3907 | |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
| 3911 | bool gfp_pfmemalloc_allowed(gfp_t gfp_mask) |
| 3912 | { |
| 3913 | return !!__gfp_pfmemalloc_flags(gfp_mask); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3914 | } |
| 3915 | |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3916 | /* |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3917 | * Checks whether it makes sense to retry the reclaim to make a forward progress |
| 3918 | * for the given allocation request. |
Johannes Weiner | 491d79a | 2017-05-03 14:52:16 -0700 | [diff] [blame] | 3919 | * |
| 3920 | * We give up when we either have tried MAX_RECLAIM_RETRIES in a row |
| 3921 | * without success, or when we couldn't even meet the watermark if we |
| 3922 | * reclaimed all remaining pages on the LRU lists. |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3923 | * |
| 3924 | * Returns true if a retry is viable or false to enter the oom path. |
| 3925 | */ |
| 3926 | static inline bool |
| 3927 | should_reclaim_retry(gfp_t gfp_mask, unsigned order, |
| 3928 | struct alloc_context *ac, int alloc_flags, |
Vlastimil Babka | 423b452 | 2016-10-07 17:00:40 -0700 | [diff] [blame] | 3929 | bool did_some_progress, int *no_progress_loops) |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3930 | { |
| 3931 | struct zone *zone; |
| 3932 | struct zoneref *z; |
Michal Hocko | 15f570b | 2018-10-26 15:03:31 -0700 | [diff] [blame] | 3933 | bool ret = false; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3934 | |
| 3935 | /* |
Vlastimil Babka | 423b452 | 2016-10-07 17:00:40 -0700 | [diff] [blame] | 3936 | * Costly allocations might have made a progress but this doesn't mean |
| 3937 | * their order will become available due to high fragmentation so |
| 3938 | * always increment the no progress counter for them |
| 3939 | */ |
| 3940 | if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER) |
| 3941 | *no_progress_loops = 0; |
| 3942 | else |
| 3943 | (*no_progress_loops)++; |
| 3944 | |
| 3945 | /* |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3946 | * Make sure we converge to OOM if we cannot make any progress |
| 3947 | * several times in the row. |
| 3948 | */ |
Minchan Kim | 04c8716 | 2016-12-12 16:42:11 -0800 | [diff] [blame] | 3949 | if (*no_progress_loops > MAX_RECLAIM_RETRIES) { |
| 3950 | /* Before OOM, exhaust highatomic_reserve */ |
Minchan Kim | 29fac03 | 2016-12-12 16:42:14 -0800 | [diff] [blame] | 3951 | return unreserve_highatomic_pageblock(ac, true); |
Minchan Kim | 04c8716 | 2016-12-12 16:42:11 -0800 | [diff] [blame] | 3952 | } |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3953 | |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3954 | /* |
Mel Gorman | bca6759 | 2016-07-28 15:47:05 -0700 | [diff] [blame] | 3955 | * Keep reclaiming pages while there is a chance this will lead |
| 3956 | * somewhere. If none of the target zones can satisfy our allocation |
| 3957 | * request even if all reclaimable pages are considered then we are |
| 3958 | * screwed and have to go OOM. |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3959 | */ |
| 3960 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| 3961 | ac->nodemask) { |
| 3962 | unsigned long available; |
Michal Hocko | ede3771 | 2016-05-20 16:57:03 -0700 | [diff] [blame] | 3963 | unsigned long reclaimable; |
Michal Hocko | d379f01 | 2017-02-22 15:42:00 -0800 | [diff] [blame] | 3964 | unsigned long min_wmark = min_wmark_pages(zone); |
| 3965 | bool wmark; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3966 | |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 3967 | available = reclaimable = zone_reclaimable_pages(zone); |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 3968 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3969 | |
| 3970 | /* |
Johannes Weiner | 491d79a | 2017-05-03 14:52:16 -0700 | [diff] [blame] | 3971 | * Would the allocation succeed if we reclaimed all |
| 3972 | * reclaimable pages? |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3973 | */ |
Michal Hocko | d379f01 | 2017-02-22 15:42:00 -0800 | [diff] [blame] | 3974 | wmark = __zone_watermark_ok(zone, order, min_wmark, |
| 3975 | ac_classzone_idx(ac), alloc_flags, available); |
| 3976 | trace_reclaim_retry_zone(z, order, reclaimable, |
| 3977 | available, min_wmark, *no_progress_loops, wmark); |
| 3978 | if (wmark) { |
Michal Hocko | ede3771 | 2016-05-20 16:57:03 -0700 | [diff] [blame] | 3979 | /* |
| 3980 | * If we didn't make any progress and have a lot of |
| 3981 | * dirty + writeback pages then we should wait for |
| 3982 | * an IO to complete to slow down the reclaim and |
| 3983 | * prevent from pre mature OOM |
| 3984 | */ |
| 3985 | if (!did_some_progress) { |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 3986 | unsigned long write_pending; |
Michal Hocko | ede3771 | 2016-05-20 16:57:03 -0700 | [diff] [blame] | 3987 | |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 3988 | write_pending = zone_page_state_snapshot(zone, |
| 3989 | NR_ZONE_WRITE_PENDING); |
Michal Hocko | ede3771 | 2016-05-20 16:57:03 -0700 | [diff] [blame] | 3990 | |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 3991 | if (2 * write_pending > reclaimable) { |
Michal Hocko | ede3771 | 2016-05-20 16:57:03 -0700 | [diff] [blame] | 3992 | congestion_wait(BLK_RW_ASYNC, HZ/10); |
| 3993 | return true; |
| 3994 | } |
| 3995 | } |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 3996 | |
Michal Hocko | 15f570b | 2018-10-26 15:03:31 -0700 | [diff] [blame] | 3997 | ret = true; |
| 3998 | goto out; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 3999 | } |
| 4000 | } |
| 4001 | |
Michal Hocko | 15f570b | 2018-10-26 15:03:31 -0700 | [diff] [blame] | 4002 | out: |
| 4003 | /* |
| 4004 | * Memory allocation/reclaim might be called from a WQ context and the |
| 4005 | * current implementation of the WQ concurrency control doesn't |
| 4006 | * recognize that a particular WQ is congested if the worker thread is |
| 4007 | * looping without ever sleeping. Therefore we have to do a short sleep |
| 4008 | * here rather than calling cond_resched(). |
| 4009 | */ |
| 4010 | if (current->flags & PF_WQ_WORKER) |
| 4011 | schedule_timeout_uninterruptible(1); |
| 4012 | else |
| 4013 | cond_resched(); |
| 4014 | return ret; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4015 | } |
| 4016 | |
Vlastimil Babka | 902b628 | 2017-07-06 15:39:56 -0700 | [diff] [blame] | 4017 | static inline bool |
| 4018 | check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac) |
| 4019 | { |
| 4020 | /* |
| 4021 | * It's possible that cpuset's mems_allowed and the nodemask from |
| 4022 | * mempolicy don't intersect. This should be normally dealt with by |
| 4023 | * policy_nodemask(), but it's possible to race with cpuset update in |
| 4024 | * such a way the check therein was true, and then it became false |
| 4025 | * before we got our cpuset_mems_cookie here. |
| 4026 | * This assumes that for all allocations, ac->nodemask can come only |
| 4027 | * from MPOL_BIND mempolicy (whose documented semantics is to be ignored |
| 4028 | * when it does not intersect with the cpuset restrictions) or the |
| 4029 | * caller can deal with a violated nodemask. |
| 4030 | */ |
| 4031 | if (cpusets_enabled() && ac->nodemask && |
| 4032 | !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) { |
| 4033 | ac->nodemask = NULL; |
| 4034 | return true; |
| 4035 | } |
| 4036 | |
| 4037 | /* |
| 4038 | * When updating a task's mems_allowed or mempolicy nodemask, it is |
| 4039 | * possible to race with parallel threads in such a way that our |
| 4040 | * allocation can fail while the mask is being updated. If we are about |
| 4041 | * to fail, check if the cpuset changed during allocation and if so, |
| 4042 | * retry. |
| 4043 | */ |
| 4044 | if (read_mems_allowed_retry(cpuset_mems_cookie)) |
| 4045 | return true; |
| 4046 | |
| 4047 | return false; |
| 4048 | } |
| 4049 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4050 | static inline struct page * |
| 4051 | __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 4052 | struct alloc_context *ac) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4053 | { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4054 | bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM; |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 4055 | const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4056 | struct page *page = NULL; |
Mel Gorman | c603844 | 2016-05-19 17:13:38 -0700 | [diff] [blame] | 4057 | unsigned int alloc_flags; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4058 | unsigned long did_some_progress; |
Vlastimil Babka | 5ce9bfe | 2017-01-24 15:18:38 -0800 | [diff] [blame] | 4059 | enum compact_priority compact_priority; |
Michal Hocko | c5d01d0 | 2016-05-20 16:56:53 -0700 | [diff] [blame] | 4060 | enum compact_result compact_result; |
Vlastimil Babka | 5ce9bfe | 2017-01-24 15:18:38 -0800 | [diff] [blame] | 4061 | int compaction_retries; |
| 4062 | int no_progress_loops; |
Vlastimil Babka | 5ce9bfe | 2017-01-24 15:18:38 -0800 | [diff] [blame] | 4063 | unsigned int cpuset_mems_cookie; |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 4064 | int reserve_flags; |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 4065 | |
Christoph Lameter | 952f3b5 | 2006-12-06 20:33:26 -0800 | [diff] [blame] | 4066 | /* |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4067 | * We also sanity check to catch abuse of atomic reserves being used by |
| 4068 | * callers that are not in atomic context. |
| 4069 | */ |
| 4070 | if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) == |
| 4071 | (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM))) |
| 4072 | gfp_mask &= ~__GFP_ATOMIC; |
| 4073 | |
Vlastimil Babka | 5ce9bfe | 2017-01-24 15:18:38 -0800 | [diff] [blame] | 4074 | retry_cpuset: |
| 4075 | compaction_retries = 0; |
| 4076 | no_progress_loops = 0; |
| 4077 | compact_priority = DEF_COMPACT_PRIORITY; |
| 4078 | cpuset_mems_cookie = read_mems_allowed_begin(); |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4079 | |
| 4080 | /* |
| 4081 | * The fast path uses conservative alloc_flags to succeed only until |
| 4082 | * kswapd needs to be woken up, and to avoid the cost of setting up |
| 4083 | * alloc_flags precisely. So we do that now. |
| 4084 | */ |
| 4085 | alloc_flags = gfp_to_alloc_flags(gfp_mask); |
| 4086 | |
Vlastimil Babka | e47483b | 2017-01-24 15:18:41 -0800 | [diff] [blame] | 4087 | /* |
| 4088 | * We need to recalculate the starting point for the zonelist iterator |
| 4089 | * because we might have used different nodemask in the fast path, or |
| 4090 | * there was a cpuset modification and we are retrying - otherwise we |
| 4091 | * could end up iterating over non-eligible zones endlessly. |
| 4092 | */ |
| 4093 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, |
| 4094 | ac->high_zoneidx, ac->nodemask); |
| 4095 | if (!ac->preferred_zoneref->zone) |
| 4096 | goto nopage; |
| 4097 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4098 | if (gfp_mask & __GFP_KSWAPD_RECLAIM) |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 4099 | wake_all_kswapds(order, gfp_mask, ac); |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 4100 | |
Paul Jackson | 9bf2229 | 2005-09-06 15:18:12 -0700 | [diff] [blame] | 4101 | /* |
Vlastimil Babka | 2377123 | 2016-07-28 15:49:16 -0700 | [diff] [blame] | 4102 | * The adjusted alloc_flags might result in immediate success, so try |
| 4103 | * that first |
| 4104 | */ |
| 4105 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
| 4106 | if (page) |
| 4107 | goto got_pg; |
| 4108 | |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4109 | /* |
| 4110 | * For costly allocations, try direct compaction first, as it's likely |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 4111 | * that we have enough base pages and don't need to reclaim. For non- |
| 4112 | * movable high-order allocations, do that as well, as compaction will |
| 4113 | * try prevent permanent fragmentation by migrating from blocks of the |
| 4114 | * same migratetype. |
| 4115 | * Don't try this for allocations that are allowed to ignore |
| 4116 | * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen. |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4117 | */ |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 4118 | if (can_direct_reclaim && |
| 4119 | (costly_order || |
| 4120 | (order > 0 && ac->migratetype != MIGRATE_MOVABLE)) |
| 4121 | && !gfp_pfmemalloc_allowed(gfp_mask)) { |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4122 | page = __alloc_pages_direct_compact(gfp_mask, order, |
| 4123 | alloc_flags, ac, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 4124 | INIT_COMPACT_PRIORITY, |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4125 | &compact_result); |
| 4126 | if (page) |
| 4127 | goto got_pg; |
| 4128 | |
Vlastimil Babka | 3eb2771 | 2016-07-28 15:49:22 -0700 | [diff] [blame] | 4129 | /* |
| 4130 | * Checks for costly allocations with __GFP_NORETRY, which |
| 4131 | * includes THP page fault allocations |
| 4132 | */ |
Vlastimil Babka | 282722b | 2017-05-08 15:54:49 -0700 | [diff] [blame] | 4133 | if (costly_order && (gfp_mask & __GFP_NORETRY)) { |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4134 | /* |
| 4135 | * If compaction is deferred for high-order allocations, |
| 4136 | * it is because sync compaction recently failed. If |
| 4137 | * this is the case and the caller requested a THP |
| 4138 | * allocation, we do not want to heavily disrupt the |
| 4139 | * system, so we fail the allocation instead of entering |
| 4140 | * direct reclaim. |
| 4141 | */ |
| 4142 | if (compact_result == COMPACT_DEFERRED) |
| 4143 | goto nopage; |
| 4144 | |
| 4145 | /* |
Vlastimil Babka | 3eb2771 | 2016-07-28 15:49:22 -0700 | [diff] [blame] | 4146 | * Looks like reclaim/compaction is worth trying, but |
| 4147 | * sync compaction could be very expensive, so keep |
Vlastimil Babka | 2516035 | 2016-07-28 15:49:25 -0700 | [diff] [blame] | 4148 | * using async compaction. |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4149 | */ |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 4150 | compact_priority = INIT_COMPACT_PRIORITY; |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4151 | } |
| 4152 | } |
Vlastimil Babka | 2377123 | 2016-07-28 15:49:16 -0700 | [diff] [blame] | 4153 | |
| 4154 | retry: |
| 4155 | /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */ |
| 4156 | if (gfp_mask & __GFP_KSWAPD_RECLAIM) |
David Rientjes | 5ecd9d4 | 2018-04-05 16:25:16 -0700 | [diff] [blame] | 4157 | wake_all_kswapds(order, gfp_mask, ac); |
Vlastimil Babka | 2377123 | 2016-07-28 15:49:16 -0700 | [diff] [blame] | 4158 | |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 4159 | reserve_flags = __gfp_pfmemalloc_flags(gfp_mask); |
| 4160 | if (reserve_flags) |
| 4161 | alloc_flags = reserve_flags; |
Vlastimil Babka | 2377123 | 2016-07-28 15:49:16 -0700 | [diff] [blame] | 4162 | |
| 4163 | /* |
Vlastimil Babka | d6a24df | 2018-08-17 15:45:05 -0700 | [diff] [blame] | 4164 | * Reset the nodemask and zonelist iterators if memory policies can be |
| 4165 | * ignored. These allocations are high priority and system rather than |
| 4166 | * user oriented. |
Mel Gorman | e46e7b7 | 2016-06-03 14:56:01 -0700 | [diff] [blame] | 4167 | */ |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 4168 | if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) { |
Vlastimil Babka | d6a24df | 2018-08-17 15:45:05 -0700 | [diff] [blame] | 4169 | ac->nodemask = NULL; |
Mel Gorman | e46e7b7 | 2016-06-03 14:56:01 -0700 | [diff] [blame] | 4170 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, |
| 4171 | ac->high_zoneidx, ac->nodemask); |
| 4172 | } |
| 4173 | |
Vlastimil Babka | 2377123 | 2016-07-28 15:49:16 -0700 | [diff] [blame] | 4174 | /* Attempt with potentially adjusted zonelist and alloc_flags */ |
Vlastimil Babka | 31a6c19 | 2016-07-28 15:49:13 -0700 | [diff] [blame] | 4175 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
Rohit Seth | 7fb1d9f | 2005-11-13 16:06:43 -0800 | [diff] [blame] | 4176 | if (page) |
| 4177 | goto got_pg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 4179 | /* Caller is not willing to reclaim, we can't balance anything */ |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4180 | if (!can_direct_reclaim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4181 | goto nopage; |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4182 | |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 4183 | /* Avoid recursion of direct reclaim */ |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4184 | if (current->flags & PF_MEMALLOC) |
Peter Zijlstra | 341ce06 | 2009-06-16 15:32:02 -0700 | [diff] [blame] | 4185 | goto nopage; |
David Rientjes | 8fe7804 | 2014-08-06 16:07:54 -0700 | [diff] [blame] | 4186 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4187 | /* Try direct reclaim and then allocating */ |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 4188 | page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac, |
| 4189 | &did_some_progress); |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4190 | if (page) |
| 4191 | goto got_pg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4193 | /* Try direct compaction and then allocating */ |
| 4194 | page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 4195 | compact_priority, &compact_result); |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4196 | if (page) |
| 4197 | goto got_pg; |
| 4198 | |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4199 | /* Do not loop if specifically requested */ |
| 4200 | if (gfp_mask & __GFP_NORETRY) |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4201 | goto nopage; |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4202 | |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4203 | /* |
| 4204 | * Do not retry costly high order allocations unless they are |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 4205 | * __GFP_RETRY_MAYFAIL |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4206 | */ |
Michal Hocko | dcda9b0 | 2017-07-12 14:36:45 -0700 | [diff] [blame] | 4207 | if (costly_order && !(gfp_mask & __GFP_RETRY_MAYFAIL)) |
Vlastimil Babka | a8161d1 | 2016-07-28 15:49:19 -0700 | [diff] [blame] | 4208 | goto nopage; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4209 | |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4210 | if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags, |
Vlastimil Babka | 423b452 | 2016-10-07 17:00:40 -0700 | [diff] [blame] | 4211 | did_some_progress > 0, &no_progress_loops)) |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4212 | goto retry; |
| 4213 | |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 4214 | /* |
| 4215 | * It doesn't make any sense to retry for the compaction if the order-0 |
| 4216 | * reclaim is not able to make any progress because the current |
| 4217 | * implementation of the compaction depends on the sufficient amount |
| 4218 | * of free memory (see __compaction_suitable) |
| 4219 | */ |
| 4220 | if (did_some_progress > 0 && |
Michal Hocko | 86a294a | 2016-05-20 16:57:12 -0700 | [diff] [blame] | 4221 | should_compact_retry(ac, order, alloc_flags, |
Vlastimil Babka | a5508cd | 2016-07-28 15:49:28 -0700 | [diff] [blame] | 4222 | compact_result, &compact_priority, |
Vlastimil Babka | d943649 | 2016-10-07 17:00:31 -0700 | [diff] [blame] | 4223 | &compaction_retries)) |
Michal Hocko | 33c2d21 | 2016-05-20 16:57:06 -0700 | [diff] [blame] | 4224 | goto retry; |
| 4225 | |
Vlastimil Babka | 902b628 | 2017-07-06 15:39:56 -0700 | [diff] [blame] | 4226 | |
| 4227 | /* Deal with possible cpuset update races before we start OOM killing */ |
| 4228 | if (check_retry_cpuset(cpuset_mems_cookie, ac)) |
Vlastimil Babka | e47483b | 2017-01-24 15:18:41 -0800 | [diff] [blame] | 4229 | goto retry_cpuset; |
| 4230 | |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4231 | /* Reclaim has failed us, start killing things */ |
| 4232 | page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress); |
| 4233 | if (page) |
| 4234 | goto got_pg; |
| 4235 | |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4236 | /* Avoid allocations with no watermarks from looping endlessly */ |
Michal Hocko | cd04ae1 | 2017-09-06 16:24:50 -0700 | [diff] [blame] | 4237 | if (tsk_is_oom_victim(current) && |
| 4238 | (alloc_flags == ALLOC_OOM || |
Tetsuo Handa | c288983 | 2017-06-02 14:46:31 -0700 | [diff] [blame] | 4239 | (gfp_mask & __GFP_NOMEMALLOC))) |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4240 | goto nopage; |
| 4241 | |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4242 | /* Retry as long as the OOM killer is making progress */ |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4243 | if (did_some_progress) { |
| 4244 | no_progress_loops = 0; |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4245 | goto retry; |
Michal Hocko | 0a0337e | 2016-05-20 16:57:00 -0700 | [diff] [blame] | 4246 | } |
Johannes Weiner | 9083905 | 2015-06-24 16:57:21 -0700 | [diff] [blame] | 4247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | nopage: |
Vlastimil Babka | 902b628 | 2017-07-06 15:39:56 -0700 | [diff] [blame] | 4249 | /* Deal with possible cpuset update races before we fail */ |
| 4250 | if (check_retry_cpuset(cpuset_mems_cookie, ac)) |
Vlastimil Babka | 5ce9bfe | 2017-01-24 15:18:38 -0800 | [diff] [blame] | 4251 | goto retry_cpuset; |
| 4252 | |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4253 | /* |
| 4254 | * Make sure that __GFP_NOFAIL request doesn't leak out and make sure |
| 4255 | * we always retry |
| 4256 | */ |
| 4257 | if (gfp_mask & __GFP_NOFAIL) { |
| 4258 | /* |
| 4259 | * All existing users of the __GFP_NOFAIL are blockable, so warn |
| 4260 | * of any new users that actually require GFP_NOWAIT |
| 4261 | */ |
| 4262 | if (WARN_ON_ONCE(!can_direct_reclaim)) |
| 4263 | goto fail; |
| 4264 | |
| 4265 | /* |
| 4266 | * PF_MEMALLOC request from this context is rather bizarre |
| 4267 | * because we cannot reclaim anything and only can loop waiting |
| 4268 | * for somebody to do a work for us |
| 4269 | */ |
| 4270 | WARN_ON_ONCE(current->flags & PF_MEMALLOC); |
| 4271 | |
| 4272 | /* |
| 4273 | * non failing costly orders are a hard requirement which we |
| 4274 | * are not prepared for much so let's warn about these users |
| 4275 | * so that we can identify them and convert them to something |
| 4276 | * else. |
| 4277 | */ |
| 4278 | WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER); |
| 4279 | |
Michal Hocko | 6c18ba7 | 2017-02-22 15:46:25 -0800 | [diff] [blame] | 4280 | /* |
| 4281 | * Help non-failing allocations by giving them access to memory |
| 4282 | * reserves but do not use ALLOC_NO_WATERMARKS because this |
| 4283 | * could deplete whole memory reserves which would just make |
| 4284 | * the situation worse |
| 4285 | */ |
| 4286 | page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_HARDER, ac); |
| 4287 | if (page) |
| 4288 | goto got_pg; |
| 4289 | |
Michal Hocko | 9a67f64 | 2017-02-22 15:46:19 -0800 | [diff] [blame] | 4290 | cond_resched(); |
| 4291 | goto retry; |
| 4292 | } |
| 4293 | fail: |
Michal Hocko | a8e9925 | 2017-02-22 15:46:10 -0800 | [diff] [blame] | 4294 | warn_alloc(gfp_mask, ac->nodemask, |
Michal Hocko | 7877cdc | 2016-10-07 17:01:55 -0700 | [diff] [blame] | 4295 | "page allocation failure: order:%u", order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4296 | got_pg: |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 4297 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | } |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4299 | |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4300 | static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, |
Vlastimil Babka | 04ec626 | 2017-07-06 15:40:03 -0700 | [diff] [blame] | 4301 | int preferred_nid, nodemask_t *nodemask, |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4302 | struct alloc_context *ac, gfp_t *alloc_mask, |
| 4303 | unsigned int *alloc_flags) |
| 4304 | { |
| 4305 | ac->high_zoneidx = gfp_zone(gfp_mask); |
Vlastimil Babka | 04ec626 | 2017-07-06 15:40:03 -0700 | [diff] [blame] | 4306 | ac->zonelist = node_zonelist(preferred_nid, gfp_mask); |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4307 | ac->nodemask = nodemask; |
| 4308 | ac->migratetype = gfpflags_to_migratetype(gfp_mask); |
| 4309 | |
| 4310 | if (cpusets_enabled()) { |
| 4311 | *alloc_mask |= __GFP_HARDWALL; |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4312 | if (!ac->nodemask) |
| 4313 | ac->nodemask = &cpuset_current_mems_allowed; |
Vlastimil Babka | 5104782 | 2017-02-24 14:56:53 -0800 | [diff] [blame] | 4314 | else |
| 4315 | *alloc_flags |= ALLOC_CPUSET; |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4316 | } |
| 4317 | |
Peter Zijlstra | d92a8cf | 2017-03-03 10:13:38 +0100 | [diff] [blame] | 4318 | fs_reclaim_acquire(gfp_mask); |
| 4319 | fs_reclaim_release(gfp_mask); |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4320 | |
| 4321 | might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM); |
| 4322 | |
| 4323 | if (should_fail_alloc_page(gfp_mask, order)) |
| 4324 | return false; |
| 4325 | |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 4326 | if (IS_ENABLED(CONFIG_CMA) && ac->migratetype == MIGRATE_MOVABLE) |
| 4327 | *alloc_flags |= ALLOC_CMA; |
| 4328 | |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4329 | return true; |
| 4330 | } |
| 4331 | |
| 4332 | /* Determine whether to spread dirty pages and what the first usable zone */ |
Huaisheng Ye | a380b40 | 2018-06-07 17:07:57 -0700 | [diff] [blame] | 4333 | static inline void finalise_ac(gfp_t gfp_mask, struct alloc_context *ac) |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4334 | { |
| 4335 | /* Dirty zone balancing only done in the fast path */ |
| 4336 | ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE); |
| 4337 | |
| 4338 | /* |
| 4339 | * The preferred zone is used for statistics but crucially it is |
| 4340 | * also used as the starting point for the zonelist iterator. It |
| 4341 | * may get reset for allocations that ignore memory policies. |
| 4342 | */ |
| 4343 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, |
| 4344 | ac->high_zoneidx, ac->nodemask); |
| 4345 | } |
| 4346 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4347 | /* |
| 4348 | * This is the 'heart' of the zoned buddy allocator. |
| 4349 | */ |
| 4350 | struct page * |
Vlastimil Babka | 04ec626 | 2017-07-06 15:40:03 -0700 | [diff] [blame] | 4351 | __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order, int preferred_nid, |
| 4352 | nodemask_t *nodemask) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4353 | { |
Mel Gorman | 5bb1b16 | 2016-05-19 17:13:50 -0700 | [diff] [blame] | 4354 | struct page *page; |
Mel Gorman | e6cbd7f | 2016-07-28 15:46:50 -0700 | [diff] [blame] | 4355 | unsigned int alloc_flags = ALLOC_WMARK_LOW; |
Tetsuo Handa | f19360f | 2017-09-08 16:13:22 -0700 | [diff] [blame] | 4356 | gfp_t alloc_mask; /* The gfp_t that was actually used for allocation */ |
Mel Gorman | 9cd7555 | 2017-02-24 14:56:29 -0800 | [diff] [blame] | 4357 | struct alloc_context ac = { }; |
Mel Gorman | 682a338 | 2016-05-19 17:13:30 -0700 | [diff] [blame] | 4358 | |
Michal Hocko | c63ae43 | 2018-11-16 15:08:53 -0800 | [diff] [blame] | 4359 | /* |
| 4360 | * There are several places where we assume that the order value is sane |
| 4361 | * so bail out early if the request is out of bound. |
| 4362 | */ |
| 4363 | if (unlikely(order >= MAX_ORDER)) { |
| 4364 | WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN)); |
| 4365 | return NULL; |
| 4366 | } |
| 4367 | |
Benjamin Herrenschmidt | dcce284 | 2009-06-18 13:24:12 +1000 | [diff] [blame] | 4368 | gfp_mask &= gfp_allowed_mask; |
Tetsuo Handa | f19360f | 2017-09-08 16:13:22 -0700 | [diff] [blame] | 4369 | alloc_mask = gfp_mask; |
Vlastimil Babka | 04ec626 | 2017-07-06 15:40:03 -0700 | [diff] [blame] | 4370 | if (!prepare_alloc_pages(gfp_mask, order, preferred_nid, nodemask, &ac, &alloc_mask, &alloc_flags)) |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4371 | return NULL; |
| 4372 | |
Huaisheng Ye | a380b40 | 2018-06-07 17:07:57 -0700 | [diff] [blame] | 4373 | finalise_ac(gfp_mask, &ac); |
Mel Gorman | 5bb1b16 | 2016-05-19 17:13:50 -0700 | [diff] [blame] | 4374 | |
Mel Gorman | 5117f45 | 2009-06-16 15:31:59 -0700 | [diff] [blame] | 4375 | /* First allocation attempt */ |
Vlastimil Babka | a926375 | 2015-02-11 15:25:41 -0800 | [diff] [blame] | 4376 | page = get_page_from_freelist(alloc_mask, order, alloc_flags, &ac); |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4377 | if (likely(page)) |
| 4378 | goto out; |
Andrew Morton | 91fbdc0 | 2015-02-11 15:25:04 -0800 | [diff] [blame] | 4379 | |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4380 | /* |
Michal Hocko | 7dea19f | 2017-05-03 14:53:15 -0700 | [diff] [blame] | 4381 | * Apply scoped allocation constraints. This is mainly about GFP_NOFS |
| 4382 | * resp. GFP_NOIO which has to be inherited for all allocation requests |
| 4383 | * from a particular context which has been marked by |
| 4384 | * memalloc_no{fs,io}_{save,restore}. |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4385 | */ |
Michal Hocko | 7dea19f | 2017-05-03 14:53:15 -0700 | [diff] [blame] | 4386 | alloc_mask = current_gfp_context(gfp_mask); |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4387 | ac.spread_dirty_pages = false; |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4388 | |
Mel Gorman | 4741526 | 2016-05-19 17:14:44 -0700 | [diff] [blame] | 4389 | /* |
| 4390 | * Restore the original nodemask if it was potentially replaced with |
| 4391 | * &cpuset_current_mems_allowed to optimize the fast-path attempt. |
| 4392 | */ |
Vlastimil Babka | e47483b | 2017-01-24 15:18:41 -0800 | [diff] [blame] | 4393 | if (unlikely(ac.nodemask != nodemask)) |
Mel Gorman | 4741526 | 2016-05-19 17:14:44 -0700 | [diff] [blame] | 4394 | ac.nodemask = nodemask; |
Vlastimil Babka | 16096c2 | 2017-01-24 15:18:35 -0800 | [diff] [blame] | 4395 | |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4396 | page = __alloc_pages_slowpath(alloc_mask, order, &ac); |
Xishi Qiu | 23f086f | 2015-02-11 15:25:07 -0800 | [diff] [blame] | 4397 | |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4398 | out: |
Vladimir Davydov | c4159a7 | 2016-08-08 23:03:12 +0300 | [diff] [blame] | 4399 | if (memcg_kmem_enabled() && (gfp_mask & __GFP_ACCOUNT) && page && |
| 4400 | unlikely(memcg_kmem_charge(page, gfp_mask, order) != 0)) { |
| 4401 | __free_pages(page, order); |
| 4402 | page = NULL; |
Vladimir Davydov | 4949148 | 2016-07-26 15:24:24 -0700 | [diff] [blame] | 4403 | } |
| 4404 | |
Mel Gorman | 4fcb097 | 2016-05-19 17:14:01 -0700 | [diff] [blame] | 4405 | trace_mm_page_alloc(page, order, alloc_mask, ac.migratetype); |
| 4406 | |
Mel Gorman | 11e33f6 | 2009-06-16 15:31:57 -0700 | [diff] [blame] | 4407 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4408 | } |
Mel Gorman | d239171 | 2009-06-16 15:31:52 -0700 | [diff] [blame] | 4409 | EXPORT_SYMBOL(__alloc_pages_nodemask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4410 | |
| 4411 | /* |
Michal Hocko | 9ea9a68 | 2018-08-17 15:46:01 -0700 | [diff] [blame] | 4412 | * Common helper functions. Never use with __GFP_HIGHMEM because the returned |
| 4413 | * address cannot represent highmem pages. Use alloc_pages and then kmap if |
| 4414 | * you need to access high mem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4415 | */ |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 4416 | unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4417 | { |
Akinobu Mita | 945a111 | 2009-09-21 17:01:47 -0700 | [diff] [blame] | 4418 | struct page *page; |
| 4419 | |
Michal Hocko | 9ea9a68 | 2018-08-17 15:46:01 -0700 | [diff] [blame] | 4420 | page = alloc_pages(gfp_mask & ~__GFP_HIGHMEM, order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4421 | if (!page) |
| 4422 | return 0; |
| 4423 | return (unsigned long) page_address(page); |
| 4424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | EXPORT_SYMBOL(__get_free_pages); |
| 4426 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 4427 | unsigned long get_zeroed_page(gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4428 | { |
Akinobu Mita | 945a111 | 2009-09-21 17:01:47 -0700 | [diff] [blame] | 4429 | return __get_free_pages(gfp_mask | __GFP_ZERO, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | EXPORT_SYMBOL(get_zeroed_page); |
| 4432 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 4433 | void __free_pages(struct page *page, unsigned int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | { |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 4435 | if (put_page_testzero(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | if (order == 0) |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 4437 | free_unref_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4438 | else |
| 4439 | __free_pages_ok(page, order); |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | EXPORT_SYMBOL(__free_pages); |
| 4444 | |
Harvey Harrison | 920c7a5 | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 4445 | void free_pages(unsigned long addr, unsigned int order) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | { |
| 4447 | if (addr != 0) { |
Nick Piggin | 725d704 | 2006-09-25 23:30:55 -0700 | [diff] [blame] | 4448 | VM_BUG_ON(!virt_addr_valid((void *)addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4449 | __free_pages(virt_to_page((void *)addr), order); |
| 4450 | } |
| 4451 | } |
| 4452 | |
| 4453 | EXPORT_SYMBOL(free_pages); |
| 4454 | |
Glauber Costa | 6a1a0d3 | 2012-12-18 14:22:00 -0800 | [diff] [blame] | 4455 | /* |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4456 | * Page Fragment: |
| 4457 | * An arbitrary-length arbitrary-offset area of memory which resides |
| 4458 | * within a 0 or higher order page. Multiple fragments within that page |
| 4459 | * are individually refcounted, in the page's reference counter. |
| 4460 | * |
| 4461 | * The page_frag functions below provide a simple allocation framework for |
| 4462 | * page fragments. This is used by the network stack and network device |
| 4463 | * drivers to provide a backing region of memory for use as either an |
| 4464 | * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. |
| 4465 | */ |
Alexander Duyck | 2976db8 | 2017-01-10 16:58:09 -0800 | [diff] [blame] | 4466 | static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, |
| 4467 | gfp_t gfp_mask) |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4468 | { |
| 4469 | struct page *page = NULL; |
| 4470 | gfp_t gfp = gfp_mask; |
| 4471 | |
| 4472 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) |
| 4473 | gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | |
| 4474 | __GFP_NOMEMALLOC; |
| 4475 | page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, |
| 4476 | PAGE_FRAG_CACHE_MAX_ORDER); |
| 4477 | nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; |
| 4478 | #endif |
| 4479 | if (unlikely(!page)) |
| 4480 | page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); |
| 4481 | |
| 4482 | nc->va = page ? page_address(page) : NULL; |
| 4483 | |
| 4484 | return page; |
| 4485 | } |
| 4486 | |
Alexander Duyck | 2976db8 | 2017-01-10 16:58:09 -0800 | [diff] [blame] | 4487 | void __page_frag_cache_drain(struct page *page, unsigned int count) |
Alexander Duyck | 44fdffd | 2016-12-14 15:05:26 -0800 | [diff] [blame] | 4488 | { |
| 4489 | VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); |
| 4490 | |
| 4491 | if (page_ref_sub_and_test(page, count)) { |
Alexander Duyck | 2976db8 | 2017-01-10 16:58:09 -0800 | [diff] [blame] | 4492 | unsigned int order = compound_order(page); |
| 4493 | |
Alexander Duyck | 44fdffd | 2016-12-14 15:05:26 -0800 | [diff] [blame] | 4494 | if (order == 0) |
Mel Gorman | 2d4894b | 2017-11-15 17:37:59 -0800 | [diff] [blame] | 4495 | free_unref_page(page); |
Alexander Duyck | 44fdffd | 2016-12-14 15:05:26 -0800 | [diff] [blame] | 4496 | else |
| 4497 | __free_pages_ok(page, order); |
| 4498 | } |
| 4499 | } |
Alexander Duyck | 2976db8 | 2017-01-10 16:58:09 -0800 | [diff] [blame] | 4500 | EXPORT_SYMBOL(__page_frag_cache_drain); |
Alexander Duyck | 44fdffd | 2016-12-14 15:05:26 -0800 | [diff] [blame] | 4501 | |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 4502 | void *page_frag_alloc(struct page_frag_cache *nc, |
| 4503 | unsigned int fragsz, gfp_t gfp_mask) |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4504 | { |
| 4505 | unsigned int size = PAGE_SIZE; |
| 4506 | struct page *page; |
| 4507 | int offset; |
| 4508 | |
| 4509 | if (unlikely(!nc->va)) { |
| 4510 | refill: |
Alexander Duyck | 2976db8 | 2017-01-10 16:58:09 -0800 | [diff] [blame] | 4511 | page = __page_frag_cache_refill(nc, gfp_mask); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4512 | if (!page) |
| 4513 | return NULL; |
| 4514 | |
| 4515 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) |
| 4516 | /* if size can vary use size else just use PAGE_SIZE */ |
| 4517 | size = nc->size; |
| 4518 | #endif |
| 4519 | /* Even if we own the page, we do not use atomic_set(). |
| 4520 | * This would break get_page_unless_zero() users. |
| 4521 | */ |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 4522 | page_ref_add(page, size - 1); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4523 | |
| 4524 | /* reset page count bias and offset to start of new frag */ |
Michal Hocko | 2f064f3 | 2015-08-21 14:11:51 -0700 | [diff] [blame] | 4525 | nc->pfmemalloc = page_is_pfmemalloc(page); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4526 | nc->pagecnt_bias = size; |
| 4527 | nc->offset = size; |
| 4528 | } |
| 4529 | |
| 4530 | offset = nc->offset - fragsz; |
| 4531 | if (unlikely(offset < 0)) { |
| 4532 | page = virt_to_page(nc->va); |
| 4533 | |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 4534 | if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4535 | goto refill; |
| 4536 | |
| 4537 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) |
| 4538 | /* if size can vary use size else just use PAGE_SIZE */ |
| 4539 | size = nc->size; |
| 4540 | #endif |
| 4541 | /* OK, page count is 0, we can safely set it */ |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 4542 | set_page_count(page, size); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4543 | |
| 4544 | /* reset page count bias and offset to start of new frag */ |
| 4545 | nc->pagecnt_bias = size; |
| 4546 | offset = size - fragsz; |
| 4547 | } |
| 4548 | |
| 4549 | nc->pagecnt_bias--; |
| 4550 | nc->offset = offset; |
| 4551 | |
| 4552 | return nc->va + offset; |
| 4553 | } |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 4554 | EXPORT_SYMBOL(page_frag_alloc); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4555 | |
| 4556 | /* |
| 4557 | * Frees a page fragment allocated out of either a compound or order 0 page. |
| 4558 | */ |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 4559 | void page_frag_free(void *addr) |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4560 | { |
| 4561 | struct page *page = virt_to_head_page(addr); |
| 4562 | |
| 4563 | if (unlikely(put_page_testzero(page))) |
| 4564 | __free_pages_ok(page, compound_order(page)); |
| 4565 | } |
Alexander Duyck | 8c2dd3e | 2017-01-10 16:58:06 -0800 | [diff] [blame] | 4566 | EXPORT_SYMBOL(page_frag_free); |
Alexander Duyck | b63ae8c | 2015-05-06 21:11:57 -0700 | [diff] [blame] | 4567 | |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 4568 | static void *make_alloc_exact(unsigned long addr, unsigned int order, |
| 4569 | size_t size) |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4570 | { |
| 4571 | if (addr) { |
| 4572 | unsigned long alloc_end = addr + (PAGE_SIZE << order); |
| 4573 | unsigned long used = addr + PAGE_ALIGN(size); |
| 4574 | |
| 4575 | split_page(virt_to_page((void *)addr), order); |
| 4576 | while (used < alloc_end) { |
| 4577 | free_page(used); |
| 4578 | used += PAGE_SIZE; |
| 4579 | } |
| 4580 | } |
| 4581 | return (void *)addr; |
| 4582 | } |
| 4583 | |
Timur Tabi | 2be0ffe | 2008-07-23 21:28:11 -0700 | [diff] [blame] | 4584 | /** |
| 4585 | * alloc_pages_exact - allocate an exact number physically-contiguous pages. |
| 4586 | * @size: the number of bytes to allocate |
| 4587 | * @gfp_mask: GFP flags for the allocation |
| 4588 | * |
| 4589 | * This function is similar to alloc_pages(), except that it allocates the |
| 4590 | * minimum number of pages to satisfy the request. alloc_pages() can only |
| 4591 | * allocate memory in power-of-two pages. |
| 4592 | * |
| 4593 | * This function is also limited by MAX_ORDER. |
| 4594 | * |
| 4595 | * Memory allocated by this function must be released by free_pages_exact(). |
| 4596 | */ |
| 4597 | void *alloc_pages_exact(size_t size, gfp_t gfp_mask) |
| 4598 | { |
| 4599 | unsigned int order = get_order(size); |
| 4600 | unsigned long addr; |
| 4601 | |
| 4602 | addr = __get_free_pages(gfp_mask, order); |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4603 | return make_alloc_exact(addr, order, size); |
Timur Tabi | 2be0ffe | 2008-07-23 21:28:11 -0700 | [diff] [blame] | 4604 | } |
| 4605 | EXPORT_SYMBOL(alloc_pages_exact); |
| 4606 | |
| 4607 | /** |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4608 | * alloc_pages_exact_nid - allocate an exact number of physically-contiguous |
| 4609 | * pages on a node. |
Randy Dunlap | b5e6ab5 | 2011-05-16 13:16:54 -0700 | [diff] [blame] | 4610 | * @nid: the preferred node ID where memory should be allocated |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4611 | * @size: the number of bytes to allocate |
| 4612 | * @gfp_mask: GFP flags for the allocation |
| 4613 | * |
| 4614 | * Like alloc_pages_exact(), but try to allocate on node nid first before falling |
| 4615 | * back. |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4616 | */ |
Fabian Frederick | e193181 | 2014-08-06 16:04:59 -0700 | [diff] [blame] | 4617 | void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask) |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4618 | { |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 4619 | unsigned int order = get_order(size); |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4620 | struct page *p = alloc_pages_node(nid, gfp_mask, order); |
| 4621 | if (!p) |
| 4622 | return NULL; |
| 4623 | return make_alloc_exact((unsigned long)page_address(p), order, size); |
| 4624 | } |
Andi Kleen | ee85c2e | 2011-05-11 15:13:34 -0700 | [diff] [blame] | 4625 | |
| 4626 | /** |
Timur Tabi | 2be0ffe | 2008-07-23 21:28:11 -0700 | [diff] [blame] | 4627 | * free_pages_exact - release memory allocated via alloc_pages_exact() |
| 4628 | * @virt: the value returned by alloc_pages_exact. |
| 4629 | * @size: size of allocation, same value as passed to alloc_pages_exact(). |
| 4630 | * |
| 4631 | * Release the memory allocated by a previous call to alloc_pages_exact. |
| 4632 | */ |
| 4633 | void free_pages_exact(void *virt, size_t size) |
| 4634 | { |
| 4635 | unsigned long addr = (unsigned long)virt; |
| 4636 | unsigned long end = addr + PAGE_ALIGN(size); |
| 4637 | |
| 4638 | while (addr < end) { |
| 4639 | free_page(addr); |
| 4640 | addr += PAGE_SIZE; |
| 4641 | } |
| 4642 | } |
| 4643 | EXPORT_SYMBOL(free_pages_exact); |
| 4644 | |
Zhang Yanfei | e0fb581 | 2013-02-22 16:35:54 -0800 | [diff] [blame] | 4645 | /** |
| 4646 | * nr_free_zone_pages - count number of pages beyond high watermark |
| 4647 | * @offset: The zone index of the highest zone |
| 4648 | * |
| 4649 | * nr_free_zone_pages() counts the number of counts pages which are beyond the |
| 4650 | * high watermark within all zones at or below a given zone index. For each |
| 4651 | * zone, the number of pages is calculated as: |
mchehab@s-opensource.com | 0e056eb | 2017-03-30 17:11:36 -0300 | [diff] [blame] | 4652 | * |
| 4653 | * nr_free_zone_pages = managed_pages - high_pages |
Zhang Yanfei | e0fb581 | 2013-02-22 16:35:54 -0800 | [diff] [blame] | 4654 | */ |
Zhang Yanfei | ebec386 | 2013-02-22 16:35:43 -0800 | [diff] [blame] | 4655 | static unsigned long nr_free_zone_pages(int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4656 | { |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 4657 | struct zoneref *z; |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 4658 | struct zone *zone; |
| 4659 | |
Martin J. Bligh | e310fd4 | 2005-07-29 22:59:18 -0700 | [diff] [blame] | 4660 | /* Just pick one node, since fallback list is circular */ |
Zhang Yanfei | ebec386 | 2013-02-22 16:35:43 -0800 | [diff] [blame] | 4661 | unsigned long sum = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | |
Mel Gorman | 0e88460 | 2008-04-28 02:12:14 -0700 | [diff] [blame] | 4663 | struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4664 | |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 4665 | for_each_zone_zonelist(zone, z, zonelist, offset) { |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 4666 | unsigned long size = zone_managed_pages(zone); |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 4667 | unsigned long high = high_wmark_pages(zone); |
Martin J. Bligh | e310fd4 | 2005-07-29 22:59:18 -0700 | [diff] [blame] | 4668 | if (size > high) |
| 4669 | sum += size - high; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4670 | } |
| 4671 | |
| 4672 | return sum; |
| 4673 | } |
| 4674 | |
Zhang Yanfei | e0fb581 | 2013-02-22 16:35:54 -0800 | [diff] [blame] | 4675 | /** |
| 4676 | * nr_free_buffer_pages - count number of pages beyond high watermark |
| 4677 | * |
| 4678 | * nr_free_buffer_pages() counts the number of pages which are beyond the high |
| 4679 | * watermark within ZONE_DMA and ZONE_NORMAL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | */ |
Zhang Yanfei | ebec386 | 2013-02-22 16:35:43 -0800 | [diff] [blame] | 4681 | unsigned long nr_free_buffer_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4682 | { |
Al Viro | af4ca45 | 2005-10-21 02:55:38 -0400 | [diff] [blame] | 4683 | return nr_free_zone_pages(gfp_zone(GFP_USER)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | } |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 4685 | EXPORT_SYMBOL_GPL(nr_free_buffer_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4686 | |
Zhang Yanfei | e0fb581 | 2013-02-22 16:35:54 -0800 | [diff] [blame] | 4687 | /** |
| 4688 | * nr_free_pagecache_pages - count number of pages beyond high watermark |
| 4689 | * |
| 4690 | * nr_free_pagecache_pages() counts the number of pages which are beyond the |
| 4691 | * high watermark within all zones. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4692 | */ |
Zhang Yanfei | ebec386 | 2013-02-22 16:35:43 -0800 | [diff] [blame] | 4693 | unsigned long nr_free_pagecache_pages(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4694 | { |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 4695 | return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4696 | } |
Christoph Lameter | 08e0f6a | 2006-09-27 01:50:06 -0700 | [diff] [blame] | 4697 | |
| 4698 | static inline void show_node(struct zone *zone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | { |
Kirill A. Shutemov | e5adfff | 2012-12-11 16:00:29 -0800 | [diff] [blame] | 4700 | if (IS_ENABLED(CONFIG_NUMA)) |
Andy Whitcroft | 25ba77c | 2006-12-06 20:33:03 -0800 | [diff] [blame] | 4701 | printk("Node %d ", zone_to_nid(zone)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4702 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4703 | |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4704 | long si_mem_available(void) |
| 4705 | { |
| 4706 | long available; |
| 4707 | unsigned long pagecache; |
| 4708 | unsigned long wmark_low = 0; |
| 4709 | unsigned long pages[NR_LRU_LISTS]; |
Vlastimil Babka | b29940c | 2018-10-26 15:05:46 -0700 | [diff] [blame] | 4710 | unsigned long reclaimable; |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4711 | struct zone *zone; |
| 4712 | int lru; |
| 4713 | |
| 4714 | for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++) |
Mel Gorman | 2f95ff9 | 2016-08-11 15:32:57 -0700 | [diff] [blame] | 4715 | pages[lru] = global_node_page_state(NR_LRU_BASE + lru); |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4716 | |
| 4717 | for_each_zone(zone) |
| 4718 | wmark_low += zone->watermark[WMARK_LOW]; |
| 4719 | |
| 4720 | /* |
| 4721 | * Estimate the amount of memory available for userspace allocations, |
| 4722 | * without causing swapping. |
| 4723 | */ |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 4724 | available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages; |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4725 | |
| 4726 | /* |
| 4727 | * Not all the page cache can be freed, otherwise the system will |
| 4728 | * start swapping. Assume at least half of the page cache, or the |
| 4729 | * low watermark worth of cache, needs to stay. |
| 4730 | */ |
| 4731 | pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE]; |
| 4732 | pagecache -= min(pagecache / 2, wmark_low); |
| 4733 | available += pagecache; |
| 4734 | |
| 4735 | /* |
Vlastimil Babka | b29940c | 2018-10-26 15:05:46 -0700 | [diff] [blame] | 4736 | * Part of the reclaimable slab and other kernel memory consists of |
| 4737 | * items that are in use, and cannot be freed. Cap this estimate at the |
| 4738 | * low watermark. |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4739 | */ |
Vlastimil Babka | b29940c | 2018-10-26 15:05:46 -0700 | [diff] [blame] | 4740 | reclaimable = global_node_page_state(NR_SLAB_RECLAIMABLE) + |
| 4741 | global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE); |
| 4742 | available += reclaimable - min(reclaimable / 2, wmark_low); |
Roman Gushchin | 034ebf6 | 2018-04-10 16:27:40 -0700 | [diff] [blame] | 4743 | |
Igor Redko | d02bd27 | 2016-03-17 14:19:05 -0700 | [diff] [blame] | 4744 | if (available < 0) |
| 4745 | available = 0; |
| 4746 | return available; |
| 4747 | } |
| 4748 | EXPORT_SYMBOL_GPL(si_mem_available); |
| 4749 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | void si_meminfo(struct sysinfo *val) |
| 4751 | { |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 4752 | val->totalram = totalram_pages(); |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4753 | val->sharedram = global_node_page_state(NR_SHMEM); |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 4754 | val->freeram = global_zone_page_state(NR_FREE_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4755 | val->bufferram = nr_blockdev_pages(); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 4756 | val->totalhigh = totalhigh_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4757 | val->freehigh = nr_free_highpages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | val->mem_unit = PAGE_SIZE; |
| 4759 | } |
| 4760 | |
| 4761 | EXPORT_SYMBOL(si_meminfo); |
| 4762 | |
| 4763 | #ifdef CONFIG_NUMA |
| 4764 | void si_meminfo_node(struct sysinfo *val, int nid) |
| 4765 | { |
Jiang Liu | cdd91a7 | 2013-07-03 15:03:27 -0700 | [diff] [blame] | 4766 | int zone_type; /* needs to be signed */ |
| 4767 | unsigned long managed_pages = 0; |
Joonsoo Kim | fc2bd79 | 2016-05-19 17:12:23 -0700 | [diff] [blame] | 4768 | unsigned long managed_highpages = 0; |
| 4769 | unsigned long free_highpages = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | pg_data_t *pgdat = NODE_DATA(nid); |
| 4771 | |
Jiang Liu | cdd91a7 | 2013-07-03 15:03:27 -0700 | [diff] [blame] | 4772 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 4773 | managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]); |
Jiang Liu | cdd91a7 | 2013-07-03 15:03:27 -0700 | [diff] [blame] | 4774 | val->totalram = managed_pages; |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4775 | val->sharedram = node_page_state(pgdat, NR_SHMEM); |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 4776 | val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES); |
Christoph Lameter | 98d2b0e | 2006-09-25 23:31:12 -0700 | [diff] [blame] | 4777 | #ifdef CONFIG_HIGHMEM |
Joonsoo Kim | fc2bd79 | 2016-05-19 17:12:23 -0700 | [diff] [blame] | 4778 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) { |
| 4779 | struct zone *zone = &pgdat->node_zones[zone_type]; |
| 4780 | |
| 4781 | if (is_highmem(zone)) { |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 4782 | managed_highpages += zone_managed_pages(zone); |
Joonsoo Kim | fc2bd79 | 2016-05-19 17:12:23 -0700 | [diff] [blame] | 4783 | free_highpages += zone_page_state(zone, NR_FREE_PAGES); |
| 4784 | } |
| 4785 | } |
| 4786 | val->totalhigh = managed_highpages; |
| 4787 | val->freehigh = free_highpages; |
Christoph Lameter | 98d2b0e | 2006-09-25 23:31:12 -0700 | [diff] [blame] | 4788 | #else |
Joonsoo Kim | fc2bd79 | 2016-05-19 17:12:23 -0700 | [diff] [blame] | 4789 | val->totalhigh = managed_highpages; |
| 4790 | val->freehigh = free_highpages; |
Christoph Lameter | 98d2b0e | 2006-09-25 23:31:12 -0700 | [diff] [blame] | 4791 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | val->mem_unit = PAGE_SIZE; |
| 4793 | } |
| 4794 | #endif |
| 4795 | |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4796 | /* |
David Rientjes | 7bf02ea | 2011-05-24 17:11:16 -0700 | [diff] [blame] | 4797 | * Determine whether the node should be displayed or not, depending on whether |
| 4798 | * SHOW_MEM_FILTER_NODES was passed to show_free_areas(). |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4799 | */ |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4800 | static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask) |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4801 | { |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4802 | if (!(flags & SHOW_MEM_FILTER_NODES)) |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4803 | return false; |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4804 | |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4805 | /* |
| 4806 | * no node mask - aka implicit memory numa policy. Do not bother with |
| 4807 | * the synchronization - read_mems_allowed_begin - because we do not |
| 4808 | * have to be precise here. |
| 4809 | */ |
| 4810 | if (!nodemask) |
| 4811 | nodemask = &cpuset_current_mems_allowed; |
| 4812 | |
| 4813 | return !node_isset(nid, *nodemask); |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4814 | } |
| 4815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4816 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
| 4817 | |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4818 | static void show_migration_types(unsigned char type) |
| 4819 | { |
| 4820 | static const char types[MIGRATE_TYPES] = { |
| 4821 | [MIGRATE_UNMOVABLE] = 'U', |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4822 | [MIGRATE_MOVABLE] = 'M', |
Vlastimil Babka | 475a2f9 | 2015-12-11 13:40:29 -0800 | [diff] [blame] | 4823 | [MIGRATE_RECLAIMABLE] = 'E', |
| 4824 | [MIGRATE_HIGHATOMIC] = 'H', |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4825 | #ifdef CONFIG_CMA |
| 4826 | [MIGRATE_CMA] = 'C', |
| 4827 | #endif |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 4828 | #ifdef CONFIG_MEMORY_ISOLATION |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4829 | [MIGRATE_ISOLATE] = 'I', |
Minchan Kim | 194159f | 2013-02-22 16:33:58 -0800 | [diff] [blame] | 4830 | #endif |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4831 | }; |
| 4832 | char tmp[MIGRATE_TYPES + 1]; |
| 4833 | char *p = tmp; |
| 4834 | int i; |
| 4835 | |
| 4836 | for (i = 0; i < MIGRATE_TYPES; i++) { |
| 4837 | if (type & (1 << i)) |
| 4838 | *p++ = types[i]; |
| 4839 | } |
| 4840 | |
| 4841 | *p = '\0'; |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 4842 | printk(KERN_CONT "(%s) ", tmp); |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 4843 | } |
| 4844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4845 | /* |
| 4846 | * Show free area list (used inside shift_scroll-lock stuff) |
| 4847 | * We also calculate the percentage fragmentation. We do this by counting the |
| 4848 | * memory on each free list with the exception of the first item on the list. |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4849 | * |
| 4850 | * Bits in @filter: |
| 4851 | * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's |
| 4852 | * cpuset. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | */ |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4854 | void show_free_areas(unsigned int filter, nodemask_t *nodemask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4855 | { |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4856 | unsigned long free_pcp = 0; |
Jes Sorensen | c724191 | 2006-09-27 01:50:05 -0700 | [diff] [blame] | 4857 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4858 | struct zone *zone; |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4859 | pg_data_t *pgdat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4860 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 4861 | for_each_populated_zone(zone) { |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4862 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4863 | continue; |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4864 | |
Konstantin Khlebnikov | 761b067 | 2015-04-14 15:45:32 -0700 | [diff] [blame] | 4865 | for_each_online_cpu(cpu) |
| 4866 | free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4867 | } |
| 4868 | |
KOSAKI Motohiro | a731286 | 2009-09-21 17:01:37 -0700 | [diff] [blame] | 4869 | printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n" |
| 4870 | " active_file:%lu inactive_file:%lu isolated_file:%lu\n" |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4871 | " unevictable:%lu dirty:%lu writeback:%lu unstable:%lu\n" |
| 4872 | " slab_reclaimable:%lu slab_unreclaimable:%lu\n" |
Bartlomiej Zolnierkiewicz | d1ce749 | 2012-10-08 16:32:02 -0700 | [diff] [blame] | 4873 | " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n" |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4874 | " free:%lu free_pcp:%lu free_cma:%lu\n", |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4875 | global_node_page_state(NR_ACTIVE_ANON), |
| 4876 | global_node_page_state(NR_INACTIVE_ANON), |
| 4877 | global_node_page_state(NR_ISOLATED_ANON), |
| 4878 | global_node_page_state(NR_ACTIVE_FILE), |
| 4879 | global_node_page_state(NR_INACTIVE_FILE), |
| 4880 | global_node_page_state(NR_ISOLATED_FILE), |
| 4881 | global_node_page_state(NR_UNEVICTABLE), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4882 | global_node_page_state(NR_FILE_DIRTY), |
| 4883 | global_node_page_state(NR_WRITEBACK), |
| 4884 | global_node_page_state(NR_UNSTABLE_NFS), |
Johannes Weiner | d507e2eb | 2017-08-10 15:23:31 -0700 | [diff] [blame] | 4885 | global_node_page_state(NR_SLAB_RECLAIMABLE), |
| 4886 | global_node_page_state(NR_SLAB_UNRECLAIMABLE), |
Mel Gorman | 50658e2 | 2016-07-28 15:46:14 -0700 | [diff] [blame] | 4887 | global_node_page_state(NR_FILE_MAPPED), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4888 | global_node_page_state(NR_SHMEM), |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 4889 | global_zone_page_state(NR_PAGETABLE), |
| 4890 | global_zone_page_state(NR_BOUNCE), |
| 4891 | global_zone_page_state(NR_FREE_PAGES), |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4892 | free_pcp, |
Michal Hocko | c41f012 | 2017-09-06 16:23:36 -0700 | [diff] [blame] | 4893 | global_zone_page_state(NR_FREE_CMA_PAGES)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4895 | for_each_online_pgdat(pgdat) { |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4896 | if (show_mem_node_skip(filter, pgdat->node_id, nodemask)) |
Michal Hocko | c02e50b | 2017-02-22 15:46:07 -0800 | [diff] [blame] | 4897 | continue; |
| 4898 | |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4899 | printk("Node %d" |
| 4900 | " active_anon:%lukB" |
| 4901 | " inactive_anon:%lukB" |
| 4902 | " active_file:%lukB" |
| 4903 | " inactive_file:%lukB" |
| 4904 | " unevictable:%lukB" |
| 4905 | " isolated(anon):%lukB" |
| 4906 | " isolated(file):%lukB" |
Mel Gorman | 50658e2 | 2016-07-28 15:46:14 -0700 | [diff] [blame] | 4907 | " mapped:%lukB" |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4908 | " dirty:%lukB" |
| 4909 | " writeback:%lukB" |
| 4910 | " shmem:%lukB" |
| 4911 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 4912 | " shmem_thp: %lukB" |
| 4913 | " shmem_pmdmapped: %lukB" |
| 4914 | " anon_thp: %lukB" |
| 4915 | #endif |
| 4916 | " writeback_tmp:%lukB" |
| 4917 | " unstable:%lukB" |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4918 | " all_unreclaimable? %s" |
| 4919 | "\n", |
| 4920 | pgdat->node_id, |
| 4921 | K(node_page_state(pgdat, NR_ACTIVE_ANON)), |
| 4922 | K(node_page_state(pgdat, NR_INACTIVE_ANON)), |
| 4923 | K(node_page_state(pgdat, NR_ACTIVE_FILE)), |
| 4924 | K(node_page_state(pgdat, NR_INACTIVE_FILE)), |
| 4925 | K(node_page_state(pgdat, NR_UNEVICTABLE)), |
| 4926 | K(node_page_state(pgdat, NR_ISOLATED_ANON)), |
| 4927 | K(node_page_state(pgdat, NR_ISOLATED_FILE)), |
Mel Gorman | 50658e2 | 2016-07-28 15:46:14 -0700 | [diff] [blame] | 4928 | K(node_page_state(pgdat, NR_FILE_MAPPED)), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4929 | K(node_page_state(pgdat, NR_FILE_DIRTY)), |
| 4930 | K(node_page_state(pgdat, NR_WRITEBACK)), |
Alexander Polakov | 1f06b81 | 2017-04-07 16:04:45 -0700 | [diff] [blame] | 4931 | K(node_page_state(pgdat, NR_SHMEM)), |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4932 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 4933 | K(node_page_state(pgdat, NR_SHMEM_THPS) * HPAGE_PMD_NR), |
| 4934 | K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED) |
| 4935 | * HPAGE_PMD_NR), |
| 4936 | K(node_page_state(pgdat, NR_ANON_THPS) * HPAGE_PMD_NR), |
| 4937 | #endif |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 4938 | K(node_page_state(pgdat, NR_WRITEBACK_TEMP)), |
| 4939 | K(node_page_state(pgdat, NR_UNSTABLE_NFS)), |
Johannes Weiner | c73322d | 2017-05-03 14:51:51 -0700 | [diff] [blame] | 4940 | pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ? |
| 4941 | "yes" : "no"); |
Mel Gorman | 599d0c9 | 2016-07-28 15:45:31 -0700 | [diff] [blame] | 4942 | } |
| 4943 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 4944 | for_each_populated_zone(zone) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | int i; |
| 4946 | |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 4947 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 4948 | continue; |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4949 | |
| 4950 | free_pcp = 0; |
| 4951 | for_each_online_cpu(cpu) |
| 4952 | free_pcp += per_cpu_ptr(zone->pageset, cpu)->pcp.count; |
| 4953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4954 | show_node(zone); |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 4955 | printk(KERN_CONT |
| 4956 | "%s" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | " free:%lukB" |
| 4958 | " min:%lukB" |
| 4959 | " low:%lukB" |
| 4960 | " high:%lukB" |
Minchan Kim | 71c799f | 2016-07-28 15:47:26 -0700 | [diff] [blame] | 4961 | " active_anon:%lukB" |
| 4962 | " inactive_anon:%lukB" |
| 4963 | " active_file:%lukB" |
| 4964 | " inactive_file:%lukB" |
| 4965 | " unevictable:%lukB" |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 4966 | " writepending:%lukB" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | " present:%lukB" |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 4968 | " managed:%lukB" |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4969 | " mlocked:%lukB" |
KOSAKI Motohiro | c6a7f57 | 2009-09-21 17:01:32 -0700 | [diff] [blame] | 4970 | " kernel_stack:%lukB" |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4971 | " pagetables:%lukB" |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4972 | " bounce:%lukB" |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4973 | " free_pcp:%lukB" |
| 4974 | " local_pcp:%ukB" |
Bartlomiej Zolnierkiewicz | d1ce749 | 2012-10-08 16:32:02 -0700 | [diff] [blame] | 4975 | " free_cma:%lukB" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4976 | "\n", |
| 4977 | zone->name, |
Mel Gorman | 88f5acf | 2011-01-13 15:45:41 -0800 | [diff] [blame] | 4978 | K(zone_page_state(zone, NR_FREE_PAGES)), |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 4979 | K(min_wmark_pages(zone)), |
| 4980 | K(low_wmark_pages(zone)), |
| 4981 | K(high_wmark_pages(zone)), |
Minchan Kim | 71c799f | 2016-07-28 15:47:26 -0700 | [diff] [blame] | 4982 | K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)), |
| 4983 | K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)), |
| 4984 | K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)), |
| 4985 | K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)), |
| 4986 | K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)), |
Mel Gorman | 5a1c84b | 2016-07-28 15:47:31 -0700 | [diff] [blame] | 4987 | K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4988 | K(zone->present_pages), |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 4989 | K(zone_managed_pages(zone)), |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4990 | K(zone_page_state(zone, NR_MLOCK)), |
Andy Lutomirski | d30dd8b | 2016-07-28 15:48:14 -0700 | [diff] [blame] | 4991 | zone_page_state(zone, NR_KERNEL_STACK_KB), |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4992 | K(zone_page_state(zone, NR_PAGETABLE)), |
KOSAKI Motohiro | 4a0aa73 | 2009-09-21 17:01:30 -0700 | [diff] [blame] | 4993 | K(zone_page_state(zone, NR_BOUNCE)), |
Konstantin Khlebnikov | d1bfcdb | 2015-04-14 15:45:30 -0700 | [diff] [blame] | 4994 | K(free_pcp), |
| 4995 | K(this_cpu_read(zone->pageset->pcp.count)), |
Minchan Kim | 33e077b | 2016-07-28 15:47:14 -0700 | [diff] [blame] | 4996 | K(zone_page_state(zone, NR_FREE_CMA_PAGES))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4997 | printk("lowmem_reserve[]:"); |
| 4998 | for (i = 0; i < MAX_NR_ZONES; i++) |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 4999 | printk(KERN_CONT " %ld", zone->lowmem_reserve[i]); |
| 5000 | printk(KERN_CONT "\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5001 | } |
| 5002 | |
KOSAKI Motohiro | ee99c71 | 2009-03-31 15:19:31 -0700 | [diff] [blame] | 5003 | for_each_populated_zone(zone) { |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 5004 | unsigned int order; |
| 5005 | unsigned long nr[MAX_ORDER], flags, total = 0; |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 5006 | unsigned char types[MAX_ORDER]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5007 | |
Michal Hocko | 9af744d | 2017-02-22 15:46:16 -0800 | [diff] [blame] | 5008 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
David Rientjes | ddd588b | 2011-03-22 16:30:46 -0700 | [diff] [blame] | 5009 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | show_node(zone); |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 5011 | printk(KERN_CONT "%s: ", zone->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5012 | |
| 5013 | spin_lock_irqsave(&zone->lock, flags); |
| 5014 | for (order = 0; order < MAX_ORDER; order++) { |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 5015 | struct free_area *area = &zone->free_area[order]; |
| 5016 | int type; |
| 5017 | |
| 5018 | nr[order] = area->nr_free; |
Kirill Korotaev | 8f9de51 | 2006-06-23 02:03:50 -0700 | [diff] [blame] | 5019 | total += nr[order] << order; |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 5020 | |
| 5021 | types[order] = 0; |
| 5022 | for (type = 0; type < MIGRATE_TYPES; type++) { |
| 5023 | if (!list_empty(&area->free_list[type])) |
| 5024 | types[order] |= 1 << type; |
| 5025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | } |
| 5027 | spin_unlock_irqrestore(&zone->lock, flags); |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 5028 | for (order = 0; order < MAX_ORDER; order++) { |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 5029 | printk(KERN_CONT "%lu*%lukB ", |
| 5030 | nr[order], K(1UL) << order); |
Rabin Vincent | 377e4f1 | 2012-12-11 16:00:24 -0800 | [diff] [blame] | 5031 | if (nr[order]) |
| 5032 | show_migration_types(types[order]); |
| 5033 | } |
Joe Perches | 1f84a18 | 2016-10-27 17:46:29 -0700 | [diff] [blame] | 5034 | printk(KERN_CONT "= %lukB\n", K(total)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | } |
| 5036 | |
David Rientjes | 949f7ec | 2013-04-29 15:07:48 -0700 | [diff] [blame] | 5037 | hugetlb_show_meminfo(); |
| 5038 | |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 5039 | printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES)); |
Larry Woodman | e6f3602 | 2008-02-04 22:29:30 -0800 | [diff] [blame] | 5040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5041 | show_swap_cache_info(); |
| 5042 | } |
| 5043 | |
Mel Gorman | 19770b3 | 2008-04-28 02:12:18 -0700 | [diff] [blame] | 5044 | static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref) |
| 5045 | { |
| 5046 | zoneref->zone = zone; |
| 5047 | zoneref->zone_idx = zone_idx(zone); |
| 5048 | } |
| 5049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | /* |
| 5051 | * Builds allocation fallback zone lists. |
Christoph Lameter | 1a93205 | 2006-01-06 00:11:16 -0800 | [diff] [blame] | 5052 | * |
| 5053 | * Add all populated zones of a node to the zonelist. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5054 | */ |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5055 | static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | { |
Christoph Lameter | 1a93205 | 2006-01-06 00:11:16 -0800 | [diff] [blame] | 5057 | struct zone *zone; |
Zhang Yanfei | bc732f1 | 2013-07-08 16:00:06 -0700 | [diff] [blame] | 5058 | enum zone_type zone_type = MAX_NR_ZONES; |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5059 | int nr_zones = 0; |
Christoph Lameter | 02a68a5 | 2006-01-06 00:11:18 -0800 | [diff] [blame] | 5060 | |
| 5061 | do { |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 5062 | zone_type--; |
Christoph Lameter | 070f803 | 2006-01-06 00:11:19 -0800 | [diff] [blame] | 5063 | zone = pgdat->node_zones + zone_type; |
Mel Gorman | 6aa303d | 2016-09-01 16:14:55 -0700 | [diff] [blame] | 5064 | if (managed_zone(zone)) { |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5065 | zoneref_set_zone(zone, &zonerefs[nr_zones++]); |
Christoph Lameter | 070f803 | 2006-01-06 00:11:19 -0800 | [diff] [blame] | 5066 | check_highest_zone(zone_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | } |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 5068 | } while (zone_type); |
Zhang Yanfei | bc732f1 | 2013-07-08 16:00:06 -0700 | [diff] [blame] | 5069 | |
Christoph Lameter | 070f803 | 2006-01-06 00:11:19 -0800 | [diff] [blame] | 5070 | return nr_zones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | } |
| 5072 | |
| 5073 | #ifdef CONFIG_NUMA |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5074 | |
| 5075 | static int __parse_numa_zonelist_order(char *s) |
| 5076 | { |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5077 | /* |
| 5078 | * We used to support different zonlists modes but they turned |
| 5079 | * out to be just not useful. Let's keep the warning in place |
| 5080 | * if somebody still use the cmd line parameter so that we do |
| 5081 | * not fail it silently |
| 5082 | */ |
| 5083 | if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) { |
| 5084 | pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s); |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5085 | return -EINVAL; |
| 5086 | } |
| 5087 | return 0; |
| 5088 | } |
| 5089 | |
| 5090 | static __init int setup_numa_zonelist_order(char *s) |
| 5091 | { |
Volodymyr G. Lukiianyk | ecb256f | 2011-01-13 15:46:26 -0800 | [diff] [blame] | 5092 | if (!s) |
| 5093 | return 0; |
| 5094 | |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5095 | return __parse_numa_zonelist_order(s); |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5096 | } |
| 5097 | early_param("numa_zonelist_order", setup_numa_zonelist_order); |
| 5098 | |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5099 | char numa_zonelist_order[] = "Node"; |
| 5100 | |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5101 | /* |
| 5102 | * sysctl handler for numa_zonelist_order |
| 5103 | */ |
Joe Perches | cccad5b | 2014-06-06 14:38:09 -0700 | [diff] [blame] | 5104 | int numa_zonelist_order_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 5105 | void __user *buffer, size_t *length, |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5106 | loff_t *ppos) |
| 5107 | { |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5108 | char *str; |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5109 | int ret; |
| 5110 | |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5111 | if (!write) |
| 5112 | return proc_dostring(table, write, buffer, length, ppos); |
| 5113 | str = memdup_user_nul(buffer, 16); |
| 5114 | if (IS_ERR(str)) |
| 5115 | return PTR_ERR(str); |
Chen Gang | dacbde0 | 2013-07-03 15:02:35 -0700 | [diff] [blame] | 5116 | |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5117 | ret = __parse_numa_zonelist_order(str); |
| 5118 | kfree(str); |
Andi Kleen | 443c6f1 | 2009-12-23 21:00:47 +0100 | [diff] [blame] | 5119 | return ret; |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5120 | } |
| 5121 | |
| 5122 | |
Christoph Lameter | 62bc62a | 2009-06-16 15:32:15 -0700 | [diff] [blame] | 5123 | #define MAX_NODE_LOAD (nr_online_nodes) |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5124 | static int node_load[MAX_NUMNODES]; |
| 5125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | /** |
Pavel Pisa | 4dc3b16 | 2005-05-01 08:59:25 -0700 | [diff] [blame] | 5127 | * find_next_best_node - find the next node that should appear in a given node's fallback list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5128 | * @node: node whose fallback list we're appending |
| 5129 | * @used_node_mask: nodemask_t of already used nodes |
| 5130 | * |
| 5131 | * We use a number of factors to determine which is the next node that should |
| 5132 | * appear on a given node's fallback list. The node should not have appeared |
| 5133 | * already in @node's fallback list, and it should be the next closest node |
| 5134 | * according to the distance array (which contains arbitrary distance values |
| 5135 | * from each node to each node in the system), and should also prefer nodes |
| 5136 | * with no CPUs, since presumably they'll have very little allocation pressure |
| 5137 | * on them otherwise. |
| 5138 | * It returns -1 if no node is found. |
| 5139 | */ |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5140 | static int find_next_best_node(int node, nodemask_t *used_node_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5141 | { |
Linus Torvalds | 4cf808eb | 2006-02-17 20:38:21 +0100 | [diff] [blame] | 5142 | int n, val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5143 | int min_val = INT_MAX; |
David Rientjes | 00ef2d2 | 2013-02-22 16:35:36 -0800 | [diff] [blame] | 5144 | int best_node = NUMA_NO_NODE; |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 5145 | const struct cpumask *tmp = cpumask_of_node(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5146 | |
Linus Torvalds | 4cf808eb | 2006-02-17 20:38:21 +0100 | [diff] [blame] | 5147 | /* Use the local node if we haven't already */ |
| 5148 | if (!node_isset(node, *used_node_mask)) { |
| 5149 | node_set(node, *used_node_mask); |
| 5150 | return node; |
| 5151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 5153 | for_each_node_state(n, N_MEMORY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5154 | |
| 5155 | /* Don't want a node to appear more than once */ |
| 5156 | if (node_isset(n, *used_node_mask)) |
| 5157 | continue; |
| 5158 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | /* Use the distance array to find the distance */ |
| 5160 | val = node_distance(node, n); |
| 5161 | |
Linus Torvalds | 4cf808eb | 2006-02-17 20:38:21 +0100 | [diff] [blame] | 5162 | /* Penalize nodes under us ("prefer the next node") */ |
| 5163 | val += (n < node); |
| 5164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | /* Give preference to headless and unused nodes */ |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 5166 | tmp = cpumask_of_node(n); |
| 5167 | if (!cpumask_empty(tmp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5168 | val += PENALTY_FOR_NODE_WITH_CPUS; |
| 5169 | |
| 5170 | /* Slight preference for less loaded node */ |
| 5171 | val *= (MAX_NODE_LOAD*MAX_NUMNODES); |
| 5172 | val += node_load[n]; |
| 5173 | |
| 5174 | if (val < min_val) { |
| 5175 | min_val = val; |
| 5176 | best_node = n; |
| 5177 | } |
| 5178 | } |
| 5179 | |
| 5180 | if (best_node >= 0) |
| 5181 | node_set(best_node, *used_node_mask); |
| 5182 | |
| 5183 | return best_node; |
| 5184 | } |
| 5185 | |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5186 | |
| 5187 | /* |
| 5188 | * Build zonelists ordered by node and zones within node. |
| 5189 | * This results in maximum locality--normal zone overflows into local |
| 5190 | * DMA zone, if any--but risks exhausting DMA zone. |
| 5191 | */ |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5192 | static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order, |
| 5193 | unsigned nr_nodes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | { |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5195 | struct zoneref *zonerefs; |
| 5196 | int i; |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5197 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5198 | zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; |
| 5199 | |
| 5200 | for (i = 0; i < nr_nodes; i++) { |
| 5201 | int nr_zones; |
| 5202 | |
| 5203 | pg_data_t *node = NODE_DATA(node_order[i]); |
| 5204 | |
| 5205 | nr_zones = build_zonerefs_node(node, zonerefs); |
| 5206 | zonerefs += nr_zones; |
| 5207 | } |
| 5208 | zonerefs->zone = NULL; |
| 5209 | zonerefs->zone_idx = 0; |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5210 | } |
| 5211 | |
| 5212 | /* |
Christoph Lameter | 523b945 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5213 | * Build gfp_thisnode zonelists |
| 5214 | */ |
| 5215 | static void build_thisnode_zonelists(pg_data_t *pgdat) |
| 5216 | { |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5217 | struct zoneref *zonerefs; |
| 5218 | int nr_zones; |
Christoph Lameter | 523b945 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5219 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5220 | zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs; |
| 5221 | nr_zones = build_zonerefs_node(pgdat, zonerefs); |
| 5222 | zonerefs += nr_zones; |
| 5223 | zonerefs->zone = NULL; |
| 5224 | zonerefs->zone_idx = 0; |
Christoph Lameter | 523b945 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5225 | } |
| 5226 | |
| 5227 | /* |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5228 | * Build zonelists ordered by zone and nodes within zones. |
| 5229 | * This results in conserving DMA zone[s] until all Normal memory is |
| 5230 | * exhausted, but results in overflowing to remote node while memory |
| 5231 | * may still exist in local DMA zone. |
| 5232 | */ |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5233 | |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5234 | static void build_zonelists(pg_data_t *pgdat) |
| 5235 | { |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5236 | static int node_order[MAX_NUMNODES]; |
| 5237 | int node, load, nr_nodes = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | nodemask_t used_mask; |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5239 | int local_node, prev_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5240 | |
| 5241 | /* NUMA-aware ordering of nodes */ |
| 5242 | local_node = pgdat->node_id; |
Christoph Lameter | 62bc62a | 2009-06-16 15:32:15 -0700 | [diff] [blame] | 5243 | load = nr_online_nodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | prev_node = local_node; |
| 5245 | nodes_clear(used_mask); |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5246 | |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5247 | memset(node_order, 0, sizeof(node_order)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | while ((node = find_next_best_node(local_node, &used_mask)) >= 0) { |
| 5249 | /* |
| 5250 | * We don't want to pressure a particular node. |
| 5251 | * So adding penalty to the first node in same |
| 5252 | * distance group to make it round-robin. |
| 5253 | */ |
David Rientjes | 957f822 | 2012-10-08 16:33:24 -0700 | [diff] [blame] | 5254 | if (node_distance(local_node, node) != |
| 5255 | node_distance(local_node, prev_node)) |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5256 | node_load[node] = load; |
| 5257 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5258 | node_order[nr_nodes++] = node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | prev_node = node; |
| 5260 | load--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | } |
Christoph Lameter | 523b945 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5262 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5263 | build_zonelists_in_node_order(pgdat, node_order, nr_nodes); |
Christoph Lameter | 523b945 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5264 | build_thisnode_zonelists(pgdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5265 | } |
| 5266 | |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 5267 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
| 5268 | /* |
| 5269 | * Return node id of node used for "local" allocations. |
| 5270 | * I.e., first node id of first zone in arg node's generic zonelist. |
| 5271 | * Used for initializing percpu 'numa_mem', which is used primarily |
| 5272 | * for kernel allocations, so use GFP_KERNEL flags to locate zonelist. |
| 5273 | */ |
| 5274 | int local_memory_node(int node) |
| 5275 | { |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 5276 | struct zoneref *z; |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 5277 | |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 5278 | z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL), |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 5279 | gfp_zone(GFP_KERNEL), |
Mel Gorman | c33d6c0 | 2016-05-19 17:14:10 -0700 | [diff] [blame] | 5280 | NULL); |
Pavel Tatashin | c1093b7 | 2018-08-21 21:53:32 -0700 | [diff] [blame] | 5281 | return zone_to_nid(z->zone); |
Lee Schermerhorn | 7aac789 | 2010-05-26 14:45:00 -0700 | [diff] [blame] | 5282 | } |
| 5283 | #endif |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5284 | |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 5285 | static void setup_min_unmapped_ratio(void); |
| 5286 | static void setup_min_slab_ratio(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | #else /* CONFIG_NUMA */ |
| 5288 | |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5289 | static void build_zonelists(pg_data_t *pgdat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | { |
Christoph Lameter | 19655d3 | 2006-09-25 23:31:19 -0700 | [diff] [blame] | 5291 | int node, local_node; |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5292 | struct zoneref *zonerefs; |
| 5293 | int nr_zones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5294 | |
| 5295 | local_node = pgdat->node_id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5296 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5297 | zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; |
| 5298 | nr_zones = build_zonerefs_node(pgdat, zonerefs); |
| 5299 | zonerefs += nr_zones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 5301 | /* |
| 5302 | * Now we build the zonelist so that it contains the zones |
| 5303 | * of all the other nodes. |
| 5304 | * We don't want to pressure a particular node, so when |
| 5305 | * building the zones for node N, we make sure that the |
| 5306 | * zones coming right after the local ones are those from |
| 5307 | * node N+1 (modulo N) |
| 5308 | */ |
| 5309 | for (node = local_node + 1; node < MAX_NUMNODES; node++) { |
| 5310 | if (!node_online(node)) |
| 5311 | continue; |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5312 | nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs); |
| 5313 | zonerefs += nr_zones; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5314 | } |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 5315 | for (node = 0; node < local_node; node++) { |
| 5316 | if (!node_online(node)) |
| 5317 | continue; |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5318 | nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs); |
| 5319 | zonerefs += nr_zones; |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 5320 | } |
| 5321 | |
Michal Hocko | 9d3be21 | 2017-09-06 16:20:30 -0700 | [diff] [blame] | 5322 | zonerefs->zone = NULL; |
| 5323 | zonerefs->zone_idx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | } |
| 5325 | |
| 5326 | #endif /* CONFIG_NUMA */ |
| 5327 | |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5328 | /* |
| 5329 | * Boot pageset table. One per cpu which is going to be used for all |
| 5330 | * zones and all nodes. The parameters will be set in such a way |
| 5331 | * that an item put on a list will immediately be handed over to |
| 5332 | * the buddy list. This is safe since pageset manipulation is done |
| 5333 | * with interrupts disabled. |
| 5334 | * |
| 5335 | * The boot_pagesets must be kept even after bootup is complete for |
| 5336 | * unused processors and/or zones. They do play a role for bootstrapping |
| 5337 | * hotplugged processors. |
| 5338 | * |
| 5339 | * zoneinfo_show() and maybe other functions do |
| 5340 | * not check if the processor is online before following the pageset pointer. |
| 5341 | * Other parts of the kernel may not check if the zone is available. |
| 5342 | */ |
| 5343 | static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch); |
| 5344 | static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset); |
Johannes Weiner | 385386c | 2017-07-06 15:40:43 -0700 | [diff] [blame] | 5345 | static DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats); |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5346 | |
Michal Hocko | 11cd863 | 2017-09-06 16:20:34 -0700 | [diff] [blame] | 5347 | static void __build_all_zonelists(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | { |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 5349 | int nid; |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5350 | int __maybe_unused cpu; |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 5351 | pg_data_t *self = data; |
Michal Hocko | b93e0f3 | 2017-09-06 16:20:37 -0700 | [diff] [blame] | 5352 | static DEFINE_SPINLOCK(lock); |
| 5353 | |
| 5354 | spin_lock(&lock); |
Paul Jackson | 9276b1bc | 2006-12-06 20:31:48 -0800 | [diff] [blame] | 5355 | |
Bo Liu | 7f9cfb3 | 2009-08-18 14:11:19 -0700 | [diff] [blame] | 5356 | #ifdef CONFIG_NUMA |
| 5357 | memset(node_load, 0, sizeof(node_load)); |
| 5358 | #endif |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 5359 | |
Wei Yang | c115258 | 2017-09-06 16:19:33 -0700 | [diff] [blame] | 5360 | /* |
| 5361 | * This node is hotadded and no memory is yet present. So just |
| 5362 | * building zonelists is fine - no need to touch other nodes. |
| 5363 | */ |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 5364 | if (self && !node_online(self->node_id)) { |
| 5365 | build_zonelists(self); |
Wei Yang | c115258 | 2017-09-06 16:19:33 -0700 | [diff] [blame] | 5366 | } else { |
| 5367 | for_each_online_node(nid) { |
| 5368 | pg_data_t *pgdat = NODE_DATA(nid); |
Jiang Liu | 9adb62a | 2012-07-31 16:43:28 -0700 | [diff] [blame] | 5369 | |
Wei Yang | c115258 | 2017-09-06 16:19:33 -0700 | [diff] [blame] | 5370 | build_zonelists(pgdat); |
| 5371 | } |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5372 | |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5373 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5374 | /* |
| 5375 | * We now know the "local memory node" for each node-- |
| 5376 | * i.e., the node of the first zone in the generic zonelist. |
| 5377 | * Set up numa_mem percpu variable for on-line cpus. During |
| 5378 | * boot, only the boot cpu should be on-line; we'll init the |
| 5379 | * secondary cpus' numa_mem as they come on-line. During |
| 5380 | * node/memory hotplug, we'll fixup all on-line cpus. |
| 5381 | */ |
Michal Hocko | d9c9a0b | 2017-09-06 16:20:20 -0700 | [diff] [blame] | 5382 | for_each_online_cpu(cpu) |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5383 | set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu))); |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5384 | #endif |
Michal Hocko | d9c9a0b | 2017-09-06 16:20:20 -0700 | [diff] [blame] | 5385 | } |
Michal Hocko | b93e0f3 | 2017-09-06 16:20:37 -0700 | [diff] [blame] | 5386 | |
| 5387 | spin_unlock(&lock); |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
| 5390 | static noinline void __init |
| 5391 | build_all_zonelists_init(void) |
| 5392 | { |
| 5393 | int cpu; |
| 5394 | |
| 5395 | __build_all_zonelists(NULL); |
| 5396 | |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5397 | /* |
| 5398 | * Initialize the boot_pagesets that are going to be used |
| 5399 | * for bootstrapping processors. The real pagesets for |
| 5400 | * each zone will be allocated later when the per cpu |
| 5401 | * allocator is available. |
| 5402 | * |
| 5403 | * boot_pagesets are used also for bootstrapping offline |
| 5404 | * cpus if the system is already booted because the pagesets |
| 5405 | * are needed to initialize allocators on a specific cpu too. |
| 5406 | * F.e. the percpu allocator needs the page allocator which |
| 5407 | * needs the percpu allocator in order to allocate its pagesets |
| 5408 | * (a chicken-egg dilemma). |
| 5409 | */ |
Michal Hocko | afb6ebb | 2017-09-06 16:20:17 -0700 | [diff] [blame] | 5410 | for_each_possible_cpu(cpu) |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5411 | setup_pageset(&per_cpu(boot_pageset, cpu), 0); |
| 5412 | |
Rasmus Villemoes | 061f67b | 2015-02-12 15:00:06 -0800 | [diff] [blame] | 5413 | mminit_verify_zonelist(); |
| 5414 | cpuset_init_current_mems_allowed(); |
| 5415 | } |
| 5416 | |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 5417 | /* |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 5418 | * unless system_state == SYSTEM_BOOTING. |
Rasmus Villemoes | 061f67b | 2015-02-12 15:00:06 -0800 | [diff] [blame] | 5419 | * |
Michal Hocko | 72675e1 | 2017-09-06 16:20:24 -0700 | [diff] [blame] | 5420 | * __ref due to call of __init annotated helper build_all_zonelists_init |
Rasmus Villemoes | 061f67b | 2015-02-12 15:00:06 -0800 | [diff] [blame] | 5421 | * [protected by SYSTEM_BOOTING]. |
Haicheng Li | 4eaf3f6 | 2010-05-24 14:32:52 -0700 | [diff] [blame] | 5422 | */ |
Michal Hocko | 72675e1 | 2017-09-06 16:20:24 -0700 | [diff] [blame] | 5423 | void __ref build_all_zonelists(pg_data_t *pgdat) |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 5424 | { |
| 5425 | if (system_state == SYSTEM_BOOTING) { |
Rasmus Villemoes | 061f67b | 2015-02-12 15:00:06 -0800 | [diff] [blame] | 5426 | build_all_zonelists_init(); |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 5427 | } else { |
Michal Hocko | 11cd863 | 2017-09-06 16:20:34 -0700 | [diff] [blame] | 5428 | __build_all_zonelists(pgdat); |
Yasunori Goto | 6811378 | 2006-06-23 02:03:11 -0700 | [diff] [blame] | 5429 | /* cpuset refresh routine should be here */ |
| 5430 | } |
Andrew Morton | bd1e22b | 2006-06-23 02:03:47 -0700 | [diff] [blame] | 5431 | vm_total_pages = nr_free_pagecache_pages(); |
Mel Gorman | 9ef9acb | 2007-10-16 01:25:54 -0700 | [diff] [blame] | 5432 | /* |
| 5433 | * Disable grouping by mobility if the number of pages in the |
| 5434 | * system is too low to allow the mechanism to work. It would be |
| 5435 | * more accurate, but expensive to check per-zone. This check is |
| 5436 | * made on memory-hotadd so a system can start with mobility |
| 5437 | * disabled and enable it later |
| 5438 | */ |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 5439 | if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES)) |
Mel Gorman | 9ef9acb | 2007-10-16 01:25:54 -0700 | [diff] [blame] | 5440 | page_group_by_mobility_disabled = 1; |
| 5441 | else |
| 5442 | page_group_by_mobility_disabled = 0; |
| 5443 | |
Michal Hocko | c9bff3e | 2017-09-06 16:20:13 -0700 | [diff] [blame] | 5444 | pr_info("Built %i zonelists, mobility grouping %s. Total pages: %ld\n", |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 5445 | nr_online_nodes, |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 5446 | page_group_by_mobility_disabled ? "off" : "on", |
| 5447 | vm_total_pages); |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5448 | #ifdef CONFIG_NUMA |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 5449 | pr_info("Policy zone: %s\n", zone_names[policy_zone]); |
KAMEZAWA Hiroyuki | f0c0b2b | 2007-07-15 23:38:01 -0700 | [diff] [blame] | 5450 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5451 | } |
| 5452 | |
Pavel Tatashin | a9a9e77 | 2018-10-26 15:09:40 -0700 | [diff] [blame] | 5453 | /* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */ |
| 5454 | static bool __meminit |
| 5455 | overlap_memmap_init(unsigned long zone, unsigned long *pfn) |
| 5456 | { |
| 5457 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 5458 | static struct memblock_region *r; |
| 5459 | |
| 5460 | if (mirrored_kernelcore && zone == ZONE_MOVABLE) { |
| 5461 | if (!r || *pfn >= memblock_region_memory_end_pfn(r)) { |
| 5462 | for_each_memblock(memory, r) { |
| 5463 | if (*pfn < memblock_region_memory_end_pfn(r)) |
| 5464 | break; |
| 5465 | } |
| 5466 | } |
| 5467 | if (*pfn >= memblock_region_memory_base_pfn(r) && |
| 5468 | memblock_is_mirror(r)) { |
| 5469 | *pfn = memblock_region_memory_end_pfn(r); |
| 5470 | return true; |
| 5471 | } |
| 5472 | } |
| 5473 | #endif |
| 5474 | return false; |
| 5475 | } |
| 5476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | * Initially all pages are reserved - free ones are freed |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 5479 | * up by memblock_free_all() once the early boot process is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5480 | * done. Non-atomic initialization, single-pass. |
| 5481 | */ |
Matt Tolentino | c09b424 | 2006-01-17 07:03:44 +0100 | [diff] [blame] | 5482 | void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone, |
Christoph Hellwig | a99583e | 2017-12-29 08:53:57 +0100 | [diff] [blame] | 5483 | unsigned long start_pfn, enum memmap_context context, |
| 5484 | struct vmem_altmap *altmap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | { |
Pavel Tatashin | a9a9e77 | 2018-10-26 15:09:40 -0700 | [diff] [blame] | 5486 | unsigned long pfn, end_pfn = start_pfn + size; |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 5487 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5488 | |
Hugh Dickins | 22b31ee | 2009-01-06 14:40:09 -0800 | [diff] [blame] | 5489 | if (highest_memmap_pfn < end_pfn - 1) |
| 5490 | highest_memmap_pfn = end_pfn - 1; |
| 5491 | |
Alexander Duyck | 966cf44 | 2018-10-26 15:07:52 -0700 | [diff] [blame] | 5492 | #ifdef CONFIG_ZONE_DEVICE |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 5493 | /* |
| 5494 | * Honor reservation requested by the driver for this ZONE_DEVICE |
Alexander Duyck | 966cf44 | 2018-10-26 15:07:52 -0700 | [diff] [blame] | 5495 | * memory. We limit the total number of pages to initialize to just |
| 5496 | * those that might contain the memory mapping. We will defer the |
| 5497 | * ZONE_DEVICE page initialization until after we have released |
| 5498 | * the hotplug lock. |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 5499 | */ |
Alexander Duyck | 966cf44 | 2018-10-26 15:07:52 -0700 | [diff] [blame] | 5500 | if (zone == ZONE_DEVICE) { |
| 5501 | if (!altmap) |
| 5502 | return; |
| 5503 | |
| 5504 | if (start_pfn == altmap->base_pfn) |
| 5505 | start_pfn += altmap->reserve; |
| 5506 | end_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); |
| 5507 | } |
| 5508 | #endif |
Dan Williams | 4b94ffd | 2016-01-15 16:56:22 -0800 | [diff] [blame] | 5509 | |
Greg Ungerer | cbe8dd4 | 2006-01-12 01:05:24 -0800 | [diff] [blame] | 5510 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 5511 | /* |
Andrew Morton | b72d0ff | 2016-03-15 14:55:25 -0700 | [diff] [blame] | 5512 | * There can be holes in boot-time mem_map[]s handed to this |
| 5513 | * function. They do not exist on hotplugged memory. |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 5514 | */ |
Pavel Tatashin | a9a9e77 | 2018-10-26 15:09:40 -0700 | [diff] [blame] | 5515 | if (context == MEMMAP_EARLY) { |
| 5516 | if (!early_pfn_valid(pfn)) |
Andrew Morton | b72d0ff | 2016-03-15 14:55:25 -0700 | [diff] [blame] | 5517 | continue; |
Pavel Tatashin | a9a9e77 | 2018-10-26 15:09:40 -0700 | [diff] [blame] | 5518 | if (!early_pfn_in_nid(pfn, nid)) |
| 5519 | continue; |
| 5520 | if (overlap_memmap_init(zone, &pfn)) |
| 5521 | continue; |
| 5522 | if (defer_init(nid, pfn, end_pfn)) |
| 5523 | break; |
Dave Hansen | a2f3aa02 | 2007-01-10 23:15:30 -0800 | [diff] [blame] | 5524 | } |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 5525 | |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 5526 | page = pfn_to_page(pfn); |
| 5527 | __init_single_page(page, pfn, zone, nid); |
| 5528 | if (context == MEMMAP_HOTPLUG) |
Alexander Duyck | d483da5 | 2018-10-26 15:07:48 -0700 | [diff] [blame] | 5529 | __SetPageReserved(page); |
Pavel Tatashin | d0dc12e | 2018-04-05 16:23:00 -0700 | [diff] [blame] | 5530 | |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 5531 | /* |
| 5532 | * Mark the block movable so that blocks are reserved for |
| 5533 | * movable at startup. This will force kernel allocations |
| 5534 | * to reserve their blocks rather than leaking throughout |
| 5535 | * the address space during boot when many long-lived |
Mel Gorman | 974a786 | 2015-11-06 16:28:34 -0800 | [diff] [blame] | 5536 | * kernel allocations are made. |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 5537 | * |
| 5538 | * bitmap is created for zone's valid pfn range. but memmap |
| 5539 | * can be created for invalid pages (for alignment) |
| 5540 | * check here not to call set_pageblock_migratetype() against |
| 5541 | * pfn out of zone. |
| 5542 | */ |
| 5543 | if (!(pfn & (pageblock_nr_pages - 1))) { |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 5544 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
Michal Hocko | 9b6e63c | 2017-10-03 16:16:19 -0700 | [diff] [blame] | 5545 | cond_resched(); |
Mel Gorman | ac5d253 | 2015-06-30 14:57:20 -0700 | [diff] [blame] | 5546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5547 | } |
Mikhail Zaslonko | 2830bf6 | 2018-12-21 14:30:46 -0800 | [diff] [blame] | 5548 | #ifdef CONFIG_SPARSEMEM |
| 5549 | /* |
| 5550 | * If the zone does not span the rest of the section then |
| 5551 | * we should at least initialize those pages. Otherwise we |
| 5552 | * could blow up on a poisoned page in some paths which depend |
| 5553 | * on full sections being initialized (e.g. memory hotplug). |
| 5554 | */ |
| 5555 | while (end_pfn % PAGES_PER_SECTION) { |
| 5556 | __init_single_page(pfn_to_page(end_pfn), end_pfn, zone, nid); |
| 5557 | end_pfn++; |
| 5558 | } |
| 5559 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5560 | } |
| 5561 | |
Alexander Duyck | 966cf44 | 2018-10-26 15:07:52 -0700 | [diff] [blame] | 5562 | #ifdef CONFIG_ZONE_DEVICE |
| 5563 | void __ref memmap_init_zone_device(struct zone *zone, |
| 5564 | unsigned long start_pfn, |
| 5565 | unsigned long size, |
| 5566 | struct dev_pagemap *pgmap) |
| 5567 | { |
| 5568 | unsigned long pfn, end_pfn = start_pfn + size; |
| 5569 | struct pglist_data *pgdat = zone->zone_pgdat; |
| 5570 | unsigned long zone_idx = zone_idx(zone); |
| 5571 | unsigned long start = jiffies; |
| 5572 | int nid = pgdat->node_id; |
| 5573 | |
| 5574 | if (WARN_ON_ONCE(!pgmap || !is_dev_zone(zone))) |
| 5575 | return; |
| 5576 | |
| 5577 | /* |
| 5578 | * The call to memmap_init_zone should have already taken care |
| 5579 | * of the pages reserved for the memmap, so we can just jump to |
| 5580 | * the end of that region and start processing the device pages. |
| 5581 | */ |
| 5582 | if (pgmap->altmap_valid) { |
| 5583 | struct vmem_altmap *altmap = &pgmap->altmap; |
| 5584 | |
| 5585 | start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); |
| 5586 | size = end_pfn - start_pfn; |
| 5587 | } |
| 5588 | |
| 5589 | for (pfn = start_pfn; pfn < end_pfn; pfn++) { |
| 5590 | struct page *page = pfn_to_page(pfn); |
| 5591 | |
| 5592 | __init_single_page(page, pfn, zone_idx, nid); |
| 5593 | |
| 5594 | /* |
| 5595 | * Mark page reserved as it will need to wait for onlining |
| 5596 | * phase for it to be fully associated with a zone. |
| 5597 | * |
| 5598 | * We can use the non-atomic __set_bit operation for setting |
| 5599 | * the flag as we are still initializing the pages. |
| 5600 | */ |
| 5601 | __SetPageReserved(page); |
| 5602 | |
| 5603 | /* |
| 5604 | * ZONE_DEVICE pages union ->lru with a ->pgmap back |
| 5605 | * pointer and hmm_data. It is a bug if a ZONE_DEVICE |
| 5606 | * page is ever freed or placed on a driver-private list. |
| 5607 | */ |
| 5608 | page->pgmap = pgmap; |
| 5609 | page->hmm_data = 0; |
| 5610 | |
| 5611 | /* |
| 5612 | * Mark the block movable so that blocks are reserved for |
| 5613 | * movable at startup. This will force kernel allocations |
| 5614 | * to reserve their blocks rather than leaking throughout |
| 5615 | * the address space during boot when many long-lived |
| 5616 | * kernel allocations are made. |
| 5617 | * |
| 5618 | * bitmap is created for zone's valid pfn range. but memmap |
| 5619 | * can be created for invalid pages (for alignment) |
| 5620 | * check here not to call set_pageblock_migratetype() against |
| 5621 | * pfn out of zone. |
| 5622 | * |
| 5623 | * Please note that MEMMAP_HOTPLUG path doesn't clear memmap |
| 5624 | * because this is done early in sparse_add_one_section |
| 5625 | */ |
| 5626 | if (!(pfn & (pageblock_nr_pages - 1))) { |
| 5627 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
| 5628 | cond_resched(); |
| 5629 | } |
| 5630 | } |
| 5631 | |
| 5632 | pr_info("%s initialised, %lu pages in %ums\n", dev_name(pgmap->dev), |
| 5633 | size, jiffies_to_msecs(jiffies - start)); |
| 5634 | } |
| 5635 | |
| 5636 | #endif |
Andi Kleen | 1e548de | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 5637 | static void __meminit zone_init_free_lists(struct zone *zone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | { |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 5639 | unsigned int order, t; |
Mel Gorman | b2a0ac8 | 2007-10-16 01:25:48 -0700 | [diff] [blame] | 5640 | for_each_migratetype_order(order, t) { |
| 5641 | INIT_LIST_HEAD(&zone->free_area[order].free_list[t]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | zone->free_area[order].nr_free = 0; |
| 5643 | } |
| 5644 | } |
| 5645 | |
Pavel Tatashin | dfb3ccd | 2018-10-26 15:09:32 -0700 | [diff] [blame] | 5646 | void __meminit __weak memmap_init(unsigned long size, int nid, |
| 5647 | unsigned long zone, unsigned long start_pfn) |
| 5648 | { |
| 5649 | memmap_init_zone(size, nid, zone, start_pfn, MEMMAP_EARLY, NULL); |
| 5650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 5652 | static int zone_batchsize(struct zone *zone) |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5653 | { |
David Howells | 3a6be87 | 2009-05-06 16:03:03 -0700 | [diff] [blame] | 5654 | #ifdef CONFIG_MMU |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5655 | int batch; |
| 5656 | |
| 5657 | /* |
| 5658 | * The per-cpu-pages pools are set to around 1000th of the |
Aaron Lu | d8a759b | 2018-08-17 15:49:14 -0700 | [diff] [blame] | 5659 | * size of the zone. |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5660 | */ |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 5661 | batch = zone_managed_pages(zone) / 1024; |
Aaron Lu | d8a759b | 2018-08-17 15:49:14 -0700 | [diff] [blame] | 5662 | /* But no more than a meg. */ |
| 5663 | if (batch * PAGE_SIZE > 1024 * 1024) |
| 5664 | batch = (1024 * 1024) / PAGE_SIZE; |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5665 | batch /= 4; /* We effectively *= 4 below */ |
| 5666 | if (batch < 1) |
| 5667 | batch = 1; |
| 5668 | |
| 5669 | /* |
Nick Piggin | 0ceaacc | 2005-12-04 13:55:25 +1100 | [diff] [blame] | 5670 | * Clamp the batch to a 2^n - 1 value. Having a power |
| 5671 | * of 2 value was found to be more likely to have |
| 5672 | * suboptimal cache aliasing properties in some cases. |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5673 | * |
Nick Piggin | 0ceaacc | 2005-12-04 13:55:25 +1100 | [diff] [blame] | 5674 | * For example if 2 tasks are alternately allocating |
| 5675 | * batches of pages, one task can end up with a lot |
| 5676 | * of pages of one half of the possible page colors |
| 5677 | * and the other with pages of the other colors. |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5678 | */ |
David Howells | 9155203 | 2009-05-06 16:03:02 -0700 | [diff] [blame] | 5679 | batch = rounddown_pow_of_two(batch + batch/2) - 1; |
Seth, Rohit | ba56e91 | 2005-10-29 18:15:47 -0700 | [diff] [blame] | 5680 | |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5681 | return batch; |
David Howells | 3a6be87 | 2009-05-06 16:03:03 -0700 | [diff] [blame] | 5682 | |
| 5683 | #else |
| 5684 | /* The deferral and batching of frees should be suppressed under NOMMU |
| 5685 | * conditions. |
| 5686 | * |
| 5687 | * The problem is that NOMMU needs to be able to allocate large chunks |
| 5688 | * of contiguous memory as there's no hardware page translation to |
| 5689 | * assemble apparent contiguous memory from discontiguous pages. |
| 5690 | * |
| 5691 | * Queueing large contiguous runs of pages for batching, however, |
| 5692 | * causes the pages to actually be freed in smaller chunks. As there |
| 5693 | * can be a significant delay between the individual batches being |
| 5694 | * recycled, this leads to the once large chunks of space being |
| 5695 | * fragmented and becoming unavailable for high-order allocations. |
| 5696 | */ |
| 5697 | return 0; |
| 5698 | #endif |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5699 | } |
| 5700 | |
Cody P Schafer | 8d7a8fa | 2013-07-03 15:01:31 -0700 | [diff] [blame] | 5701 | /* |
| 5702 | * pcp->high and pcp->batch values are related and dependent on one another: |
| 5703 | * ->batch must never be higher then ->high. |
| 5704 | * The following function updates them in a safe manner without read side |
| 5705 | * locking. |
| 5706 | * |
| 5707 | * Any new users of pcp->batch and pcp->high should ensure they can cope with |
| 5708 | * those fields changing asynchronously (acording the the above rule). |
| 5709 | * |
| 5710 | * mutex_is_locked(&pcp_batch_high_lock) required when calling this function |
| 5711 | * outside of boot time (or some other assurance that no concurrent updaters |
| 5712 | * exist). |
| 5713 | */ |
| 5714 | static void pageset_update(struct per_cpu_pages *pcp, unsigned long high, |
| 5715 | unsigned long batch) |
| 5716 | { |
| 5717 | /* start with a fail safe value for batch */ |
| 5718 | pcp->batch = 1; |
| 5719 | smp_wmb(); |
| 5720 | |
| 5721 | /* Update high, then batch, in order */ |
| 5722 | pcp->high = high; |
| 5723 | smp_wmb(); |
| 5724 | |
| 5725 | pcp->batch = batch; |
| 5726 | } |
| 5727 | |
Cody P Schafer | 3664033 | 2013-07-03 15:01:40 -0700 | [diff] [blame] | 5728 | /* a companion to pageset_set_high() */ |
Cody P Schafer | 4008bab | 2013-07-03 15:01:28 -0700 | [diff] [blame] | 5729 | static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch) |
| 5730 | { |
Cody P Schafer | 8d7a8fa | 2013-07-03 15:01:31 -0700 | [diff] [blame] | 5731 | pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch)); |
Cody P Schafer | 4008bab | 2013-07-03 15:01:28 -0700 | [diff] [blame] | 5732 | } |
| 5733 | |
Cody P Schafer | 88c90db | 2013-07-03 15:01:35 -0700 | [diff] [blame] | 5734 | static void pageset_init(struct per_cpu_pageset *p) |
Christoph Lameter | 2caaad4 | 2005-06-21 17:15:00 -0700 | [diff] [blame] | 5735 | { |
| 5736 | struct per_cpu_pages *pcp; |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 5737 | int migratetype; |
Christoph Lameter | 2caaad4 | 2005-06-21 17:15:00 -0700 | [diff] [blame] | 5738 | |
Magnus Damm | 1c6fe94 | 2005-10-26 01:58:59 -0700 | [diff] [blame] | 5739 | memset(p, 0, sizeof(*p)); |
| 5740 | |
Christoph Lameter | 3dfa572 | 2008-02-04 22:29:19 -0800 | [diff] [blame] | 5741 | pcp = &p->pcp; |
Mel Gorman | 5f8dcc2 | 2009-09-21 17:03:19 -0700 | [diff] [blame] | 5742 | for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++) |
| 5743 | INIT_LIST_HEAD(&pcp->lists[migratetype]); |
Christoph Lameter | 2caaad4 | 2005-06-21 17:15:00 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
Cody P Schafer | 88c90db | 2013-07-03 15:01:35 -0700 | [diff] [blame] | 5746 | static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch) |
| 5747 | { |
| 5748 | pageset_init(p); |
| 5749 | pageset_set_batch(p, batch); |
| 5750 | } |
| 5751 | |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 5752 | /* |
Cody P Schafer | 3664033 | 2013-07-03 15:01:40 -0700 | [diff] [blame] | 5753 | * pageset_set_high() sets the high water mark for hot per_cpu_pagelist |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 5754 | * to the value high for the pageset p. |
| 5755 | */ |
Cody P Schafer | 3664033 | 2013-07-03 15:01:40 -0700 | [diff] [blame] | 5756 | static void pageset_set_high(struct per_cpu_pageset *p, |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 5757 | unsigned long high) |
| 5758 | { |
Cody P Schafer | 8d7a8fa | 2013-07-03 15:01:31 -0700 | [diff] [blame] | 5759 | unsigned long batch = max(1UL, high / 4); |
| 5760 | if ((high / 4) > (PAGE_SHIFT * 8)) |
| 5761 | batch = PAGE_SHIFT * 8; |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 5762 | |
Cody P Schafer | 8d7a8fa | 2013-07-03 15:01:31 -0700 | [diff] [blame] | 5763 | pageset_update(&p->pcp, high, batch); |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 5764 | } |
| 5765 | |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 5766 | static void pageset_set_high_and_batch(struct zone *zone, |
| 5767 | struct per_cpu_pageset *pcp) |
Cody P Schafer | 56cef2b | 2013-07-03 15:01:38 -0700 | [diff] [blame] | 5768 | { |
Cody P Schafer | 56cef2b | 2013-07-03 15:01:38 -0700 | [diff] [blame] | 5769 | if (percpu_pagelist_fraction) |
Cody P Schafer | 3664033 | 2013-07-03 15:01:40 -0700 | [diff] [blame] | 5770 | pageset_set_high(pcp, |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 5771 | (zone_managed_pages(zone) / |
Cody P Schafer | 56cef2b | 2013-07-03 15:01:38 -0700 | [diff] [blame] | 5772 | percpu_pagelist_fraction)); |
| 5773 | else |
| 5774 | pageset_set_batch(pcp, zone_batchsize(zone)); |
| 5775 | } |
| 5776 | |
Cody P Schafer | 169f6c1 | 2013-07-03 15:01:41 -0700 | [diff] [blame] | 5777 | static void __meminit zone_pageset_init(struct zone *zone, int cpu) |
| 5778 | { |
| 5779 | struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu); |
| 5780 | |
| 5781 | pageset_init(pcp); |
| 5782 | pageset_set_high_and_batch(zone, pcp); |
| 5783 | } |
| 5784 | |
Michal Hocko | 72675e1 | 2017-09-06 16:20:24 -0700 | [diff] [blame] | 5785 | void __meminit setup_zone_pageset(struct zone *zone) |
Wu Fengguang | 319774e | 2010-05-24 14:32:49 -0700 | [diff] [blame] | 5786 | { |
| 5787 | int cpu; |
Wu Fengguang | 319774e | 2010-05-24 14:32:49 -0700 | [diff] [blame] | 5788 | zone->pageset = alloc_percpu(struct per_cpu_pageset); |
Cody P Schafer | 56cef2b | 2013-07-03 15:01:38 -0700 | [diff] [blame] | 5789 | for_each_possible_cpu(cpu) |
| 5790 | zone_pageset_init(zone, cpu); |
Wu Fengguang | 319774e | 2010-05-24 14:32:49 -0700 | [diff] [blame] | 5791 | } |
| 5792 | |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5793 | /* |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5794 | * Allocate per cpu pagesets and initialize them. |
| 5795 | * Before this call only boot pagesets were available. |
Christoph Lameter | 2caaad4 | 2005-06-21 17:15:00 -0700 | [diff] [blame] | 5796 | */ |
Al Viro | 78d9955 | 2005-12-15 09:18:25 +0000 | [diff] [blame] | 5797 | void __init setup_per_cpu_pageset(void) |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5798 | { |
Mel Gorman | b4911ea | 2016-08-04 15:31:49 -0700 | [diff] [blame] | 5799 | struct pglist_data *pgdat; |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5800 | struct zone *zone; |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5801 | |
Wu Fengguang | 319774e | 2010-05-24 14:32:49 -0700 | [diff] [blame] | 5802 | for_each_populated_zone(zone) |
| 5803 | setup_zone_pageset(zone); |
Mel Gorman | b4911ea | 2016-08-04 15:31:49 -0700 | [diff] [blame] | 5804 | |
| 5805 | for_each_online_pgdat(pgdat) |
| 5806 | pgdat->per_cpu_nodestats = |
| 5807 | alloc_percpu(struct per_cpu_nodestat); |
Christoph Lameter | e7c8d5c | 2005-06-21 17:14:47 -0700 | [diff] [blame] | 5808 | } |
| 5809 | |
Matt Tolentino | c09b424 | 2006-01-17 07:03:44 +0100 | [diff] [blame] | 5810 | static __meminit void zone_pcp_init(struct zone *zone) |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5811 | { |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5812 | /* |
| 5813 | * per cpu subsystem is not up at this point. The following code |
| 5814 | * relies on the ability of the linker to provide the |
| 5815 | * offset of a (static) per cpu variable into the per cpu area. |
| 5816 | */ |
| 5817 | zone->pageset = &boot_pageset; |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5818 | |
Xishi Qiu | b38a872 | 2013-11-12 15:07:20 -0800 | [diff] [blame] | 5819 | if (populated_zone(zone)) |
Christoph Lameter | 99dcc3e | 2010-01-05 15:34:51 +0900 | [diff] [blame] | 5820 | printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n", |
| 5821 | zone->name, zone->present_pages, |
| 5822 | zone_batchsize(zone)); |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5823 | } |
| 5824 | |
Michal Hocko | dc0bbf3 | 2017-07-06 15:37:35 -0700 | [diff] [blame] | 5825 | void __meminit init_currently_empty_zone(struct zone *zone, |
Yasunori Goto | 718127c | 2006-06-23 02:03:10 -0700 | [diff] [blame] | 5826 | unsigned long zone_start_pfn, |
Yaowei Bai | b171e40 | 2015-11-05 18:47:06 -0800 | [diff] [blame] | 5827 | unsigned long size) |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5828 | { |
| 5829 | struct pglist_data *pgdat = zone->zone_pgdat; |
Wei Yang | 8f41683 | 2018-11-30 14:09:07 -0800 | [diff] [blame] | 5830 | int zone_idx = zone_idx(zone) + 1; |
Linus Torvalds | 9dcb8b6 | 2016-10-26 10:15:30 -0700 | [diff] [blame] | 5831 | |
Wei Yang | 8f41683 | 2018-11-30 14:09:07 -0800 | [diff] [blame] | 5832 | if (zone_idx > pgdat->nr_zones) |
| 5833 | pgdat->nr_zones = zone_idx; |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5834 | |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5835 | zone->zone_start_pfn = zone_start_pfn; |
| 5836 | |
Mel Gorman | 708614e | 2008-07-23 21:26:51 -0700 | [diff] [blame] | 5837 | mminit_dprintk(MMINIT_TRACE, "memmap_init", |
| 5838 | "Initialising map node %d zone %lu pfns %lu -> %lu\n", |
| 5839 | pgdat->node_id, |
| 5840 | (unsigned long)zone_idx(zone), |
| 5841 | zone_start_pfn, (zone_start_pfn + size)); |
| 5842 | |
Andi Kleen | 1e548de | 2008-02-04 22:29:26 -0800 | [diff] [blame] | 5843 | zone_init_free_lists(zone); |
Linus Torvalds | 9dcb8b6 | 2016-10-26 10:15:30 -0700 | [diff] [blame] | 5844 | zone->initialized = 1; |
Dave Hansen | ed8ece2 | 2005-10-29 18:16:50 -0700 | [diff] [blame] | 5845 | } |
| 5846 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 5847 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5848 | #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID |
Mel Gorman | 8a942fd | 2015-06-30 14:56:55 -0700 | [diff] [blame] | 5849 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5850 | /* |
| 5851 | * Required by SPARSEMEM. Given a PFN, return what node the PFN is on. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5852 | */ |
Mel Gorman | 8a942fd | 2015-06-30 14:56:55 -0700 | [diff] [blame] | 5853 | int __meminit __early_pfn_to_nid(unsigned long pfn, |
| 5854 | struct mminit_pfnnid_cache *state) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5855 | { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5856 | unsigned long start_pfn, end_pfn; |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 5857 | int nid; |
Russ Anderson | 7c243c7 | 2013-04-29 15:07:59 -0700 | [diff] [blame] | 5858 | |
Mel Gorman | 8a942fd | 2015-06-30 14:56:55 -0700 | [diff] [blame] | 5859 | if (state->last_start <= pfn && pfn < state->last_end) |
| 5860 | return state->last_nid; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5861 | |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 5862 | nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn); |
| 5863 | if (nid != -1) { |
Mel Gorman | 8a942fd | 2015-06-30 14:56:55 -0700 | [diff] [blame] | 5864 | state->last_start = start_pfn; |
| 5865 | state->last_end = end_pfn; |
| 5866 | state->last_nid = nid; |
Yinghai Lu | e76b63f | 2013-09-11 14:22:17 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | return nid; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5870 | } |
| 5871 | #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */ |
| 5872 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5873 | /** |
Santosh Shilimkar | 6782832 | 2014-01-21 15:50:25 -0800 | [diff] [blame] | 5874 | * free_bootmem_with_active_regions - Call memblock_free_early_nid for each active range |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 5875 | * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed. |
Santosh Shilimkar | 6782832 | 2014-01-21 15:50:25 -0800 | [diff] [blame] | 5876 | * @max_low_pfn: The highest PFN that will be passed to memblock_free_early_nid |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5877 | * |
Zhang Zhen | 7d01817 | 2014-06-04 16:10:53 -0700 | [diff] [blame] | 5878 | * If an architecture guarantees that all ranges registered contain no holes |
| 5879 | * and may be freed, this this function may be used instead of calling |
| 5880 | * memblock_free_early_nid() manually. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5881 | */ |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5882 | void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5883 | { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5884 | unsigned long start_pfn, end_pfn; |
| 5885 | int i, this_nid; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5886 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5887 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) { |
| 5888 | start_pfn = min(start_pfn, max_low_pfn); |
| 5889 | end_pfn = min(end_pfn, max_low_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5890 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5891 | if (start_pfn < end_pfn) |
Santosh Shilimkar | 6782832 | 2014-01-21 15:50:25 -0800 | [diff] [blame] | 5892 | memblock_free_early_nid(PFN_PHYS(start_pfn), |
| 5893 | (end_pfn - start_pfn) << PAGE_SHIFT, |
| 5894 | this_nid); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5895 | } |
| 5896 | } |
| 5897 | |
| 5898 | /** |
| 5899 | * sparse_memory_present_with_active_regions - Call memory_present for each active range |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 5900 | * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5901 | * |
Zhang Zhen | 7d01817 | 2014-06-04 16:10:53 -0700 | [diff] [blame] | 5902 | * If an architecture guarantees that all ranges registered contain no holes and may |
| 5903 | * be freed, this function may be used instead of calling memory_present() manually. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5904 | */ |
| 5905 | void __init sparse_memory_present_with_active_regions(int nid) |
| 5906 | { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5907 | unsigned long start_pfn, end_pfn; |
| 5908 | int i, this_nid; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5909 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5910 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) |
| 5911 | memory_present(this_nid, start_pfn, end_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
| 5914 | /** |
| 5915 | * get_pfn_range_for_nid - Return the start and end page frames for a node |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 5916 | * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned. |
| 5917 | * @start_pfn: Passed by reference. On return, it will have the node start_pfn. |
| 5918 | * @end_pfn: Passed by reference. On return, it will have the node end_pfn. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5919 | * |
| 5920 | * It returns the start and end page frame of a node based on information |
Zhang Zhen | 7d01817 | 2014-06-04 16:10:53 -0700 | [diff] [blame] | 5921 | * provided by memblock_set_node(). If called for a node |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5922 | * with no available memory, a warning is printed and the start and end |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 5923 | * PFNs will be 0. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5924 | */ |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 5925 | void __meminit get_pfn_range_for_nid(unsigned int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5926 | unsigned long *start_pfn, unsigned long *end_pfn) |
| 5927 | { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5928 | unsigned long this_start_pfn, this_end_pfn; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5929 | int i; |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5930 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5931 | *start_pfn = -1UL; |
| 5932 | *end_pfn = 0; |
| 5933 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 5934 | for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) { |
| 5935 | *start_pfn = min(*start_pfn, this_start_pfn); |
| 5936 | *end_pfn = max(*end_pfn, this_end_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5937 | } |
| 5938 | |
Christoph Lameter | 633c066 | 2007-10-16 01:25:37 -0700 | [diff] [blame] | 5939 | if (*start_pfn == -1UL) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5940 | *start_pfn = 0; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | /* |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 5944 | * This finds a zone that can be used for ZONE_MOVABLE pages. The |
| 5945 | * assumption is made that zones within a node are ordered in monotonic |
| 5946 | * increasing memory addresses so that the "highest" populated zone is used |
| 5947 | */ |
Adrian Bunk | b69a728 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 5948 | static void __init find_usable_zone_for_movable(void) |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 5949 | { |
| 5950 | int zone_index; |
| 5951 | for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) { |
| 5952 | if (zone_index == ZONE_MOVABLE) |
| 5953 | continue; |
| 5954 | |
| 5955 | if (arch_zone_highest_possible_pfn[zone_index] > |
| 5956 | arch_zone_lowest_possible_pfn[zone_index]) |
| 5957 | break; |
| 5958 | } |
| 5959 | |
| 5960 | VM_BUG_ON(zone_index == -1); |
| 5961 | movable_zone = zone_index; |
| 5962 | } |
| 5963 | |
| 5964 | /* |
| 5965 | * The zone ranges provided by the architecture do not include ZONE_MOVABLE |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5966 | * because it is sized independent of architecture. Unlike the other zones, |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 5967 | * the starting point for ZONE_MOVABLE is not fixed. It may be different |
| 5968 | * in each node depending on the size of each node and how evenly kernelcore |
| 5969 | * is distributed. This helper function adjusts the zone ranges |
| 5970 | * provided by the architecture for a given node by using the end of the |
| 5971 | * highest usable zone for ZONE_MOVABLE. This preserves the assumption that |
| 5972 | * zones within a node are in order of monotonic increases memory addresses |
| 5973 | */ |
Adrian Bunk | b69a728 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 5974 | static void __meminit adjust_zone_range_for_zone_movable(int nid, |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 5975 | unsigned long zone_type, |
| 5976 | unsigned long node_start_pfn, |
| 5977 | unsigned long node_end_pfn, |
| 5978 | unsigned long *zone_start_pfn, |
| 5979 | unsigned long *zone_end_pfn) |
| 5980 | { |
| 5981 | /* Only adjust if ZONE_MOVABLE is on this node */ |
| 5982 | if (zone_movable_pfn[nid]) { |
| 5983 | /* Size ZONE_MOVABLE */ |
| 5984 | if (zone_type == ZONE_MOVABLE) { |
| 5985 | *zone_start_pfn = zone_movable_pfn[nid]; |
| 5986 | *zone_end_pfn = min(node_end_pfn, |
| 5987 | arch_zone_highest_possible_pfn[movable_zone]); |
| 5988 | |
Xishi Qiu | e506b99 | 2016-10-07 16:58:06 -0700 | [diff] [blame] | 5989 | /* Adjust for ZONE_MOVABLE starting within this range */ |
| 5990 | } else if (!mirrored_kernelcore && |
| 5991 | *zone_start_pfn < zone_movable_pfn[nid] && |
| 5992 | *zone_end_pfn > zone_movable_pfn[nid]) { |
| 5993 | *zone_end_pfn = zone_movable_pfn[nid]; |
| 5994 | |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 5995 | /* Check if this whole range is within ZONE_MOVABLE */ |
| 5996 | } else if (*zone_start_pfn >= zone_movable_pfn[nid]) |
| 5997 | *zone_start_pfn = *zone_end_pfn; |
| 5998 | } |
| 5999 | } |
| 6000 | |
| 6001 | /* |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6002 | * Return the number of pages a zone spans in a node, including holes |
| 6003 | * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node() |
| 6004 | */ |
Paul Mundt | 6ea6e68 | 2007-07-15 23:38:20 -0700 | [diff] [blame] | 6005 | static unsigned long __meminit zone_spanned_pages_in_node(int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6006 | unsigned long zone_type, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6007 | unsigned long node_start_pfn, |
| 6008 | unsigned long node_end_pfn, |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6009 | unsigned long *zone_start_pfn, |
| 6010 | unsigned long *zone_end_pfn, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6011 | unsigned long *ignored) |
| 6012 | { |
Xishi Qiu | b5685e9 | 2015-09-08 15:04:16 -0700 | [diff] [blame] | 6013 | /* When hotadd a new node from cpu_up(), the node should be empty */ |
Xishi Qiu | f9126ab | 2015-08-14 15:35:16 -0700 | [diff] [blame] | 6014 | if (!node_start_pfn && !node_end_pfn) |
| 6015 | return 0; |
| 6016 | |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6017 | /* Get the start and end of the zone */ |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6018 | *zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type]; |
| 6019 | *zone_end_pfn = arch_zone_highest_possible_pfn[zone_type]; |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6020 | adjust_zone_range_for_zone_movable(nid, zone_type, |
| 6021 | node_start_pfn, node_end_pfn, |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6022 | zone_start_pfn, zone_end_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6023 | |
| 6024 | /* Check that this node has pages within the zone's required range */ |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6025 | if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6026 | return 0; |
| 6027 | |
| 6028 | /* Move the zone boundaries inside the node if necessary */ |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6029 | *zone_end_pfn = min(*zone_end_pfn, node_end_pfn); |
| 6030 | *zone_start_pfn = max(*zone_start_pfn, node_start_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6031 | |
| 6032 | /* Return the spanned pages */ |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6033 | return *zone_end_pfn - *zone_start_pfn; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6034 | } |
| 6035 | |
| 6036 | /* |
| 6037 | * Return the number of holes in a range on a node. If nid is MAX_NUMNODES, |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 6038 | * then all holes in the requested range will be accounted for. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6039 | */ |
Yinghai Lu | 3299625 | 2009-12-15 17:59:02 -0800 | [diff] [blame] | 6040 | unsigned long __meminit __absent_pages_in_range(int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6041 | unsigned long range_start_pfn, |
| 6042 | unsigned long range_end_pfn) |
| 6043 | { |
Tejun Heo | 96e907d | 2011-07-12 10:46:29 +0200 | [diff] [blame] | 6044 | unsigned long nr_absent = range_end_pfn - range_start_pfn; |
| 6045 | unsigned long start_pfn, end_pfn; |
| 6046 | int i; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6047 | |
Tejun Heo | 96e907d | 2011-07-12 10:46:29 +0200 | [diff] [blame] | 6048 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
| 6049 | start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn); |
| 6050 | end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn); |
| 6051 | nr_absent -= end_pfn - start_pfn; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6052 | } |
Tejun Heo | 96e907d | 2011-07-12 10:46:29 +0200 | [diff] [blame] | 6053 | return nr_absent; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6054 | } |
| 6055 | |
| 6056 | /** |
| 6057 | * absent_pages_in_range - Return number of page frames in holes within a range |
| 6058 | * @start_pfn: The start PFN to start searching for holes |
| 6059 | * @end_pfn: The end PFN to stop searching for holes |
| 6060 | * |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 6061 | * It returns the number of pages frames in memory holes within a range. |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6062 | */ |
| 6063 | unsigned long __init absent_pages_in_range(unsigned long start_pfn, |
| 6064 | unsigned long end_pfn) |
| 6065 | { |
| 6066 | return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn); |
| 6067 | } |
| 6068 | |
| 6069 | /* Return the number of page frames in holes in a zone on a node */ |
Paul Mundt | 6ea6e68 | 2007-07-15 23:38:20 -0700 | [diff] [blame] | 6070 | static unsigned long __meminit zone_absent_pages_in_node(int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6071 | unsigned long zone_type, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6072 | unsigned long node_start_pfn, |
| 6073 | unsigned long node_end_pfn, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6074 | unsigned long *ignored) |
| 6075 | { |
Tejun Heo | 96e907d | 2011-07-12 10:46:29 +0200 | [diff] [blame] | 6076 | unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type]; |
| 6077 | unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type]; |
Mel Gorman | 9c7cd68 | 2006-09-27 01:49:58 -0700 | [diff] [blame] | 6078 | unsigned long zone_start_pfn, zone_end_pfn; |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6079 | unsigned long nr_absent; |
Mel Gorman | 9c7cd68 | 2006-09-27 01:49:58 -0700 | [diff] [blame] | 6080 | |
Xishi Qiu | b5685e9 | 2015-09-08 15:04:16 -0700 | [diff] [blame] | 6081 | /* When hotadd a new node from cpu_up(), the node should be empty */ |
Xishi Qiu | f9126ab | 2015-08-14 15:35:16 -0700 | [diff] [blame] | 6082 | if (!node_start_pfn && !node_end_pfn) |
| 6083 | return 0; |
| 6084 | |
Tejun Heo | 96e907d | 2011-07-12 10:46:29 +0200 | [diff] [blame] | 6085 | zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high); |
| 6086 | zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high); |
Mel Gorman | 9c7cd68 | 2006-09-27 01:49:58 -0700 | [diff] [blame] | 6087 | |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6088 | adjust_zone_range_for_zone_movable(nid, zone_type, |
| 6089 | node_start_pfn, node_end_pfn, |
| 6090 | &zone_start_pfn, &zone_end_pfn); |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6091 | nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn); |
| 6092 | |
| 6093 | /* |
| 6094 | * ZONE_MOVABLE handling. |
| 6095 | * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages |
| 6096 | * and vice versa. |
| 6097 | */ |
Xishi Qiu | e506b99 | 2016-10-07 16:58:06 -0700 | [diff] [blame] | 6098 | if (mirrored_kernelcore && zone_movable_pfn[nid]) { |
| 6099 | unsigned long start_pfn, end_pfn; |
| 6100 | struct memblock_region *r; |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6101 | |
Xishi Qiu | e506b99 | 2016-10-07 16:58:06 -0700 | [diff] [blame] | 6102 | for_each_memblock(memory, r) { |
| 6103 | start_pfn = clamp(memblock_region_memory_base_pfn(r), |
| 6104 | zone_start_pfn, zone_end_pfn); |
| 6105 | end_pfn = clamp(memblock_region_memory_end_pfn(r), |
| 6106 | zone_start_pfn, zone_end_pfn); |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6107 | |
Xishi Qiu | e506b99 | 2016-10-07 16:58:06 -0700 | [diff] [blame] | 6108 | if (zone_type == ZONE_MOVABLE && |
| 6109 | memblock_is_mirror(r)) |
| 6110 | nr_absent += end_pfn - start_pfn; |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6111 | |
Xishi Qiu | e506b99 | 2016-10-07 16:58:06 -0700 | [diff] [blame] | 6112 | if (zone_type == ZONE_NORMAL && |
| 6113 | !memblock_is_mirror(r)) |
| 6114 | nr_absent += end_pfn - start_pfn; |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6115 | } |
| 6116 | } |
| 6117 | |
| 6118 | return nr_absent; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6119 | } |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 6120 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 6121 | #else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
Paul Mundt | 6ea6e68 | 2007-07-15 23:38:20 -0700 | [diff] [blame] | 6122 | static inline unsigned long __meminit zone_spanned_pages_in_node(int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6123 | unsigned long zone_type, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6124 | unsigned long node_start_pfn, |
| 6125 | unsigned long node_end_pfn, |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6126 | unsigned long *zone_start_pfn, |
| 6127 | unsigned long *zone_end_pfn, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6128 | unsigned long *zones_size) |
| 6129 | { |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6130 | unsigned int zone; |
| 6131 | |
| 6132 | *zone_start_pfn = node_start_pfn; |
| 6133 | for (zone = 0; zone < zone_type; zone++) |
| 6134 | *zone_start_pfn += zones_size[zone]; |
| 6135 | |
| 6136 | *zone_end_pfn = *zone_start_pfn + zones_size[zone_type]; |
| 6137 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6138 | return zones_size[zone_type]; |
| 6139 | } |
| 6140 | |
Paul Mundt | 6ea6e68 | 2007-07-15 23:38:20 -0700 | [diff] [blame] | 6141 | static inline unsigned long __meminit zone_absent_pages_in_node(int nid, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6142 | unsigned long zone_type, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6143 | unsigned long node_start_pfn, |
| 6144 | unsigned long node_end_pfn, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6145 | unsigned long *zholes_size) |
| 6146 | { |
| 6147 | if (!zholes_size) |
| 6148 | return 0; |
| 6149 | |
| 6150 | return zholes_size[zone_type]; |
| 6151 | } |
Yinghai Lu | 20e6926 | 2013-03-01 14:51:27 -0800 | [diff] [blame] | 6152 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 6153 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6154 | |
Yasunori Goto | a3142c8 | 2007-05-08 00:23:07 -0700 | [diff] [blame] | 6155 | static void __meminit calculate_node_totalpages(struct pglist_data *pgdat, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6156 | unsigned long node_start_pfn, |
| 6157 | unsigned long node_end_pfn, |
| 6158 | unsigned long *zones_size, |
| 6159 | unsigned long *zholes_size) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6160 | { |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6161 | unsigned long realtotalpages = 0, totalpages = 0; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6162 | enum zone_type i; |
| 6163 | |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6164 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 6165 | struct zone *zone = pgdat->node_zones + i; |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6166 | unsigned long zone_start_pfn, zone_end_pfn; |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6167 | unsigned long size, real_size; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6168 | |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6169 | size = zone_spanned_pages_in_node(pgdat->node_id, i, |
| 6170 | node_start_pfn, |
| 6171 | node_end_pfn, |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6172 | &zone_start_pfn, |
| 6173 | &zone_end_pfn, |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6174 | zones_size); |
| 6175 | real_size = size - zone_absent_pages_in_node(pgdat->node_id, i, |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6176 | node_start_pfn, node_end_pfn, |
| 6177 | zholes_size); |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6178 | if (size) |
| 6179 | zone->zone_start_pfn = zone_start_pfn; |
| 6180 | else |
| 6181 | zone->zone_start_pfn = 0; |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6182 | zone->spanned_pages = size; |
| 6183 | zone->present_pages = real_size; |
| 6184 | |
| 6185 | totalpages += size; |
| 6186 | realtotalpages += real_size; |
| 6187 | } |
| 6188 | |
| 6189 | pgdat->node_spanned_pages = totalpages; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6190 | pgdat->node_present_pages = realtotalpages; |
| 6191 | printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id, |
| 6192 | realtotalpages); |
| 6193 | } |
| 6194 | |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6195 | #ifndef CONFIG_SPARSEMEM |
| 6196 | /* |
| 6197 | * Calculate the size of the zone->blockflags rounded to an unsigned long |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6198 | * Start by making sure zonesize is a multiple of pageblock_order by rounding |
| 6199 | * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6200 | * round what is now in bits to nearest long in bits, then return it in |
| 6201 | * bytes. |
| 6202 | */ |
Linus Torvalds | 7c45512 | 2013-02-18 09:58:02 -0800 | [diff] [blame] | 6203 | static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize) |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6204 | { |
| 6205 | unsigned long usemapsize; |
| 6206 | |
Linus Torvalds | 7c45512 | 2013-02-18 09:58:02 -0800 | [diff] [blame] | 6207 | zonesize += zone_start_pfn & (pageblock_nr_pages-1); |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6208 | usemapsize = roundup(zonesize, pageblock_nr_pages); |
| 6209 | usemapsize = usemapsize >> pageblock_order; |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6210 | usemapsize *= NR_PAGEBLOCK_BITS; |
| 6211 | usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long)); |
| 6212 | |
| 6213 | return usemapsize / 8; |
| 6214 | } |
| 6215 | |
Pavel Tatashin | 7cc2a95 | 2018-08-21 21:53:36 -0700 | [diff] [blame] | 6216 | static void __ref setup_usemap(struct pglist_data *pgdat, |
Linus Torvalds | 7c45512 | 2013-02-18 09:58:02 -0800 | [diff] [blame] | 6217 | struct zone *zone, |
| 6218 | unsigned long zone_start_pfn, |
| 6219 | unsigned long zonesize) |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6220 | { |
Linus Torvalds | 7c45512 | 2013-02-18 09:58:02 -0800 | [diff] [blame] | 6221 | unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize); |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6222 | zone->pageblock_flags = NULL; |
Julia Lawall | 58a01a4 | 2009-01-06 14:39:28 -0800 | [diff] [blame] | 6223 | if (usemapsize) |
Santosh Shilimkar | 6782832 | 2014-01-21 15:50:25 -0800 | [diff] [blame] | 6224 | zone->pageblock_flags = |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 6225 | memblock_alloc_node_nopanic(usemapsize, |
Santosh Shilimkar | 6782832 | 2014-01-21 15:50:25 -0800 | [diff] [blame] | 6226 | pgdat->node_id); |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6227 | } |
| 6228 | #else |
Linus Torvalds | 7c45512 | 2013-02-18 09:58:02 -0800 | [diff] [blame] | 6229 | static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone, |
| 6230 | unsigned long zone_start_pfn, unsigned long zonesize) {} |
Mel Gorman | 835c134 | 2007-10-16 01:25:47 -0700 | [diff] [blame] | 6231 | #endif /* CONFIG_SPARSEMEM */ |
| 6232 | |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6233 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
Mel Gorman | ba72cb8 | 2007-11-28 16:21:13 -0800 | [diff] [blame] | 6234 | |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6235 | /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */ |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6236 | void __init set_pageblock_order(void) |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6237 | { |
Andrew Morton | 955c1cd | 2012-05-29 15:06:31 -0700 | [diff] [blame] | 6238 | unsigned int order; |
| 6239 | |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6240 | /* Check that pageblock_nr_pages has not already been setup */ |
| 6241 | if (pageblock_order) |
| 6242 | return; |
| 6243 | |
Andrew Morton | 955c1cd | 2012-05-29 15:06:31 -0700 | [diff] [blame] | 6244 | if (HPAGE_SHIFT > PAGE_SHIFT) |
| 6245 | order = HUGETLB_PAGE_ORDER; |
| 6246 | else |
| 6247 | order = MAX_ORDER - 1; |
| 6248 | |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6249 | /* |
| 6250 | * Assume the largest contiguous order of interest is a huge page. |
Andrew Morton | 955c1cd | 2012-05-29 15:06:31 -0700 | [diff] [blame] | 6251 | * This value may be variable depending on boot parameters on IA64 and |
| 6252 | * powerpc. |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6253 | */ |
| 6254 | pageblock_order = order; |
| 6255 | } |
| 6256 | #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ |
| 6257 | |
Mel Gorman | ba72cb8 | 2007-11-28 16:21:13 -0800 | [diff] [blame] | 6258 | /* |
| 6259 | * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order() |
Andrew Morton | 955c1cd | 2012-05-29 15:06:31 -0700 | [diff] [blame] | 6260 | * is unused as pageblock_order is set at compile-time. See |
| 6261 | * include/linux/pageblock-flags.h for the values of pageblock_order based on |
| 6262 | * the kernel config |
Mel Gorman | ba72cb8 | 2007-11-28 16:21:13 -0800 | [diff] [blame] | 6263 | */ |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6264 | void __init set_pageblock_order(void) |
Mel Gorman | ba72cb8 | 2007-11-28 16:21:13 -0800 | [diff] [blame] | 6265 | { |
Mel Gorman | ba72cb8 | 2007-11-28 16:21:13 -0800 | [diff] [blame] | 6266 | } |
Mel Gorman | d9c2340 | 2007-10-16 01:26:01 -0700 | [diff] [blame] | 6267 | |
| 6268 | #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ |
| 6269 | |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6270 | static unsigned long __init calc_memmap_size(unsigned long spanned_pages, |
Pavel Tatashin | 7cc2a95 | 2018-08-21 21:53:36 -0700 | [diff] [blame] | 6271 | unsigned long present_pages) |
Jiang Liu | 01cefae | 2012-12-12 13:52:19 -0800 | [diff] [blame] | 6272 | { |
| 6273 | unsigned long pages = spanned_pages; |
| 6274 | |
| 6275 | /* |
| 6276 | * Provide a more accurate estimation if there are holes within |
| 6277 | * the zone and SPARSEMEM is in use. If there are holes within the |
| 6278 | * zone, each populated memory region may cost us one or two extra |
| 6279 | * memmap pages due to alignment because memmap pages for each |
Masahiro Yamada | 89d790a | 2017-02-27 14:29:01 -0800 | [diff] [blame] | 6280 | * populated regions may not be naturally aligned on page boundary. |
Jiang Liu | 01cefae | 2012-12-12 13:52:19 -0800 | [diff] [blame] | 6281 | * So the (present_pages >> 4) heuristic is a tradeoff for that. |
| 6282 | */ |
| 6283 | if (spanned_pages > present_pages + (present_pages >> 4) && |
| 6284 | IS_ENABLED(CONFIG_SPARSEMEM)) |
| 6285 | pages = present_pages; |
| 6286 | |
| 6287 | return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT; |
| 6288 | } |
| 6289 | |
Oscar Salvador | ace1db3 | 2018-08-21 21:53:29 -0700 | [diff] [blame] | 6290 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| 6291 | static void pgdat_init_split_queue(struct pglist_data *pgdat) |
| 6292 | { |
| 6293 | spin_lock_init(&pgdat->split_queue_lock); |
| 6294 | INIT_LIST_HEAD(&pgdat->split_queue); |
| 6295 | pgdat->split_queue_len = 0; |
| 6296 | } |
| 6297 | #else |
| 6298 | static void pgdat_init_split_queue(struct pglist_data *pgdat) {} |
| 6299 | #endif |
| 6300 | |
| 6301 | #ifdef CONFIG_COMPACTION |
| 6302 | static void pgdat_init_kcompactd(struct pglist_data *pgdat) |
| 6303 | { |
| 6304 | init_waitqueue_head(&pgdat->kcompactd_wait); |
| 6305 | } |
| 6306 | #else |
| 6307 | static void pgdat_init_kcompactd(struct pglist_data *pgdat) {} |
| 6308 | #endif |
| 6309 | |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6310 | static void __meminit pgdat_init_internals(struct pglist_data *pgdat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6311 | { |
Dave Hansen | 208d54e | 2005-10-29 18:16:52 -0700 | [diff] [blame] | 6312 | pgdat_resize_init(pgdat); |
Oscar Salvador | ace1db3 | 2018-08-21 21:53:29 -0700 | [diff] [blame] | 6313 | |
Oscar Salvador | ace1db3 | 2018-08-21 21:53:29 -0700 | [diff] [blame] | 6314 | pgdat_init_split_queue(pgdat); |
| 6315 | pgdat_init_kcompactd(pgdat); |
| 6316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | init_waitqueue_head(&pgdat->kswapd_wait); |
Mel Gorman | 5515061 | 2012-07-31 16:44:35 -0700 | [diff] [blame] | 6318 | init_waitqueue_head(&pgdat->pfmemalloc_wait); |
Oscar Salvador | ace1db3 | 2018-08-21 21:53:29 -0700 | [diff] [blame] | 6319 | |
Joonsoo Kim | eefa864b | 2014-12-12 16:55:46 -0800 | [diff] [blame] | 6320 | pgdat_page_ext_init(pgdat); |
Mel Gorman | a52633d | 2016-07-28 15:45:28 -0700 | [diff] [blame] | 6321 | spin_lock_init(&pgdat->lru_lock); |
Mel Gorman | a9dd0a8 | 2016-07-28 15:46:02 -0700 | [diff] [blame] | 6322 | lruvec_init(node_lruvec(pgdat)); |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6323 | } |
Michal Nazarewicz | 5f63b72 | 2012-01-11 15:16:11 +0100 | [diff] [blame] | 6324 | |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6325 | static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid, |
| 6326 | unsigned long remaining_pages) |
| 6327 | { |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 6328 | atomic_long_set(&zone->managed_pages, remaining_pages); |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6329 | zone_set_nid(zone, nid); |
| 6330 | zone->name = zone_names[idx]; |
| 6331 | zone->zone_pgdat = NODE_DATA(nid); |
| 6332 | spin_lock_init(&zone->lock); |
| 6333 | zone_seqlock_init(zone); |
| 6334 | zone_pcp_init(zone); |
| 6335 | } |
| 6336 | |
| 6337 | /* |
| 6338 | * Set up the zone data structures |
| 6339 | * - init pgdat internals |
| 6340 | * - init all zones belonging to this node |
| 6341 | * |
| 6342 | * NOTE: this function is only called during memory hotplug |
| 6343 | */ |
| 6344 | #ifdef CONFIG_MEMORY_HOTPLUG |
| 6345 | void __ref free_area_init_core_hotplug(int nid) |
| 6346 | { |
| 6347 | enum zone_type z; |
| 6348 | pg_data_t *pgdat = NODE_DATA(nid); |
| 6349 | |
| 6350 | pgdat_init_internals(pgdat); |
| 6351 | for (z = 0; z < MAX_NR_ZONES; z++) |
| 6352 | zone_init_internals(&pgdat->node_zones[z], z, nid, 0); |
| 6353 | } |
| 6354 | #endif |
| 6355 | |
| 6356 | /* |
| 6357 | * Set up the zone data structures: |
| 6358 | * - mark all pages reserved |
| 6359 | * - mark all memory queues empty |
| 6360 | * - clear the memory bitmaps |
| 6361 | * |
| 6362 | * NOTE: pgdat should get zeroed by caller. |
| 6363 | * NOTE: this function is only called during early init. |
| 6364 | */ |
| 6365 | static void __init free_area_init_core(struct pglist_data *pgdat) |
| 6366 | { |
| 6367 | enum zone_type j; |
| 6368 | int nid = pgdat->node_id; |
| 6369 | |
| 6370 | pgdat_init_internals(pgdat); |
Johannes Weiner | 385386c | 2017-07-06 15:40:43 -0700 | [diff] [blame] | 6371 | pgdat->per_cpu_nodestats = &boot_nodestats; |
| 6372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | for (j = 0; j < MAX_NR_ZONES; j++) { |
| 6374 | struct zone *zone = pgdat->node_zones + j; |
Wei Yang | e694385 | 2018-06-07 17:06:04 -0700 | [diff] [blame] | 6375 | unsigned long size, freesize, memmap_pages; |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6376 | unsigned long zone_start_pfn = zone->zone_start_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | |
Gu Zheng | febd594 | 2015-06-24 16:57:02 -0700 | [diff] [blame] | 6378 | size = zone->spanned_pages; |
Wei Yang | e694385 | 2018-06-07 17:06:04 -0700 | [diff] [blame] | 6379 | freesize = zone->present_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 6381 | /* |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 6382 | * Adjust freesize so that it accounts for how much memory |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 6383 | * is used by this zone for memmap. This affects the watermark |
| 6384 | * and per-cpu initialisations |
| 6385 | */ |
Wei Yang | e694385 | 2018-06-07 17:06:04 -0700 | [diff] [blame] | 6386 | memmap_pages = calc_memmap_size(size, freesize); |
Zhong Hongbo | ba914f4 | 2014-12-12 16:56:21 -0800 | [diff] [blame] | 6387 | if (!is_highmem_idx(j)) { |
| 6388 | if (freesize >= memmap_pages) { |
| 6389 | freesize -= memmap_pages; |
| 6390 | if (memmap_pages) |
| 6391 | printk(KERN_DEBUG |
| 6392 | " %s zone: %lu pages used for memmap\n", |
| 6393 | zone_names[j], memmap_pages); |
| 6394 | } else |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 6395 | pr_warn(" %s zone: %lu pages exceeds freesize %lu\n", |
Zhong Hongbo | ba914f4 | 2014-12-12 16:56:21 -0800 | [diff] [blame] | 6396 | zone_names[j], memmap_pages, freesize); |
| 6397 | } |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 6398 | |
Christoph Lameter | 6267276 | 2007-02-10 01:43:07 -0800 | [diff] [blame] | 6399 | /* Account for reserved pages */ |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 6400 | if (j == 0 && freesize > dma_reserve) { |
| 6401 | freesize -= dma_reserve; |
Yinghai Lu | d903ef9 | 2008-10-18 20:27:06 -0700 | [diff] [blame] | 6402 | printk(KERN_DEBUG " %s zone: %lu pages reserved\n", |
Christoph Lameter | 6267276 | 2007-02-10 01:43:07 -0800 | [diff] [blame] | 6403 | zone_names[0], dma_reserve); |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 6404 | } |
| 6405 | |
Christoph Lameter | 98d2b0e | 2006-09-25 23:31:12 -0700 | [diff] [blame] | 6406 | if (!is_highmem_idx(j)) |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 6407 | nr_kernel_pages += freesize; |
Jiang Liu | 01cefae | 2012-12-12 13:52:19 -0800 | [diff] [blame] | 6408 | /* Charge for highmem memmap if there are enough kernel pages */ |
| 6409 | else if (nr_kernel_pages > memmap_pages * 2) |
| 6410 | nr_kernel_pages -= memmap_pages; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 6411 | nr_all_pages += freesize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6412 | |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 6413 | /* |
| 6414 | * Set an approximate value for lowmem here, it will be adjusted |
| 6415 | * when the bootmem allocator frees pages into the buddy system. |
| 6416 | * And all highmem pages will be managed by the buddy system. |
| 6417 | */ |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6418 | zone_init_internals(zone, j, nid, freesize); |
Johannes Weiner | 81c0a2b | 2013-09-11 14:20:47 -0700 | [diff] [blame] | 6419 | |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 6420 | if (!size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | continue; |
| 6422 | |
Andrew Morton | 955c1cd | 2012-05-29 15:06:31 -0700 | [diff] [blame] | 6423 | set_pageblock_order(); |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 6424 | setup_usemap(pgdat, zone, zone_start_pfn, size); |
| 6425 | init_currently_empty_zone(zone, zone_start_pfn, size); |
Heiko Carstens | 76cdd58 | 2008-05-14 16:05:52 -0700 | [diff] [blame] | 6426 | memmap_init(size, nid, j, zone_start_pfn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6427 | } |
| 6428 | } |
| 6429 | |
Oscar Salvador | 0cd842f | 2017-11-15 17:39:18 -0800 | [diff] [blame] | 6430 | #ifdef CONFIG_FLAT_NODE_MEM_MAP |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 6431 | static void __ref alloc_node_mem_map(struct pglist_data *pgdat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | { |
Tony Luck | b0aeba7 | 2015-11-10 10:09:47 -0800 | [diff] [blame] | 6433 | unsigned long __maybe_unused start = 0; |
Laura Abbott | a1c34a3 | 2015-11-05 18:48:46 -0800 | [diff] [blame] | 6434 | unsigned long __maybe_unused offset = 0; |
| 6435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6436 | /* Skip empty nodes */ |
| 6437 | if (!pgdat->node_spanned_pages) |
| 6438 | return; |
| 6439 | |
Tony Luck | b0aeba7 | 2015-11-10 10:09:47 -0800 | [diff] [blame] | 6440 | start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1); |
| 6441 | offset = pgdat->node_start_pfn - start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | /* ia64 gets its own node_mem_map, before this, without bootmem */ |
| 6443 | if (!pgdat->node_mem_map) { |
Tony Luck | b0aeba7 | 2015-11-10 10:09:47 -0800 | [diff] [blame] | 6444 | unsigned long size, end; |
Andy Whitcroft | d41dee3 | 2005-06-23 00:07:54 -0700 | [diff] [blame] | 6445 | struct page *map; |
| 6446 | |
Bob Picco | e984bb4 | 2006-05-20 15:00:31 -0700 | [diff] [blame] | 6447 | /* |
| 6448 | * The zone's endpoints aren't required to be MAX_ORDER |
| 6449 | * aligned but the node_mem_map endpoints must be in order |
| 6450 | * for the buddy allocator to function correctly. |
| 6451 | */ |
Cody P Schafer | 108bcc9 | 2013-02-22 16:35:23 -0800 | [diff] [blame] | 6452 | end = pgdat_end_pfn(pgdat); |
Bob Picco | e984bb4 | 2006-05-20 15:00:31 -0700 | [diff] [blame] | 6453 | end = ALIGN(end, MAX_ORDER_NR_PAGES); |
| 6454 | size = (end - start) * sizeof(struct page); |
Mike Rapoport | eb31d55 | 2018-10-30 15:08:04 -0700 | [diff] [blame] | 6455 | map = memblock_alloc_node_nopanic(size, pgdat->node_id); |
Laura Abbott | a1c34a3 | 2015-11-05 18:48:46 -0800 | [diff] [blame] | 6456 | pgdat->node_mem_map = map + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | } |
Oscar Salvador | 0cd842f | 2017-11-15 17:39:18 -0800 | [diff] [blame] | 6458 | pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n", |
| 6459 | __func__, pgdat->node_id, (unsigned long)pgdat, |
| 6460 | (unsigned long)pgdat->node_mem_map); |
Roman Zippel | 12d810c | 2007-05-31 00:40:54 -0700 | [diff] [blame] | 6461 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | /* |
| 6463 | * With no DISCONTIG, the global mem_map is just set as node 0's |
| 6464 | */ |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6465 | if (pgdat == NODE_DATA(0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | mem_map = NODE_DATA(0)->node_mem_map; |
Laura Abbott | a1c34a3 | 2015-11-05 18:48:46 -0800 | [diff] [blame] | 6467 | #if defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) || defined(CONFIG_FLATMEM) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6468 | if (page_to_pfn(mem_map) != pgdat->node_start_pfn) |
Laura Abbott | a1c34a3 | 2015-11-05 18:48:46 -0800 | [diff] [blame] | 6469 | mem_map -= offset; |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 6470 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6471 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | #endif |
| 6473 | } |
Oscar Salvador | 0cd842f | 2017-11-15 17:39:18 -0800 | [diff] [blame] | 6474 | #else |
| 6475 | static void __ref alloc_node_mem_map(struct pglist_data *pgdat) { } |
| 6476 | #endif /* CONFIG_FLAT_NODE_MEM_MAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6477 | |
Oscar Salvador | 0188dc9 | 2018-08-21 21:53:39 -0700 | [diff] [blame] | 6478 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| 6479 | static inline void pgdat_set_deferred_range(pg_data_t *pgdat) |
| 6480 | { |
| 6481 | /* |
| 6482 | * We start only with one section of pages, more pages are added as |
| 6483 | * needed until the rest of deferred pages are initialized. |
| 6484 | */ |
| 6485 | pgdat->static_init_pgcnt = min_t(unsigned long, PAGES_PER_SECTION, |
| 6486 | pgdat->node_spanned_pages); |
| 6487 | pgdat->first_deferred_pfn = ULONG_MAX; |
| 6488 | } |
| 6489 | #else |
| 6490 | static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} |
| 6491 | #endif |
| 6492 | |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6493 | void __init free_area_init_node(int nid, unsigned long *zones_size, |
Pavel Tatashin | 7cc2a95 | 2018-08-21 21:53:36 -0700 | [diff] [blame] | 6494 | unsigned long node_start_pfn, |
| 6495 | unsigned long *zholes_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | { |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 6497 | pg_data_t *pgdat = NODE_DATA(nid); |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6498 | unsigned long start_pfn = 0; |
| 6499 | unsigned long end_pfn = 0; |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 6500 | |
Minchan Kim | 88fdf75 | 2012-07-31 16:46:14 -0700 | [diff] [blame] | 6501 | /* pg_data_t should be reset to zero when it's allocated */ |
Mel Gorman | 38087d9 | 2016-07-28 15:45:49 -0700 | [diff] [blame] | 6502 | WARN_ON(pgdat->nr_zones || pgdat->kswapd_classzone_idx); |
Minchan Kim | 88fdf75 | 2012-07-31 16:46:14 -0700 | [diff] [blame] | 6503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | pgdat->node_id = nid; |
| 6505 | pgdat->node_start_pfn = node_start_pfn; |
Mel Gorman | 75ef718 | 2016-07-28 15:45:24 -0700 | [diff] [blame] | 6506 | pgdat->per_cpu_nodestats = NULL; |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6507 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| 6508 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); |
Juergen Gross | 8d29e18 | 2015-02-11 15:26:01 -0800 | [diff] [blame] | 6509 | pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid, |
Zhen Lei | 4ada0c5 | 2015-09-08 15:04:19 -0700 | [diff] [blame] | 6510 | (u64)start_pfn << PAGE_SHIFT, |
| 6511 | end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0); |
Taku Izumi | d91749c | 2016-03-15 14:55:18 -0700 | [diff] [blame] | 6512 | #else |
| 6513 | start_pfn = node_start_pfn; |
Zhang Yanfei | 7960aed | 2013-07-08 15:59:52 -0700 | [diff] [blame] | 6514 | #endif |
| 6515 | calculate_node_totalpages(pgdat, start_pfn, end_pfn, |
| 6516 | zones_size, zholes_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6517 | |
| 6518 | alloc_node_mem_map(pgdat); |
Oscar Salvador | 0188dc9 | 2018-08-21 21:53:39 -0700 | [diff] [blame] | 6519 | pgdat_set_deferred_range(pgdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | |
Wei Yang | 7f3eb55 | 2015-09-08 14:59:50 -0700 | [diff] [blame] | 6521 | free_area_init_core(pgdat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6522 | } |
| 6523 | |
Mike Rapoport | aca52c3 | 2018-10-30 15:07:44 -0700 | [diff] [blame] | 6524 | #if !defined(CONFIG_FLAT_NODE_MEM_MAP) |
Pavel Tatashin | ec393a0f | 2018-10-26 15:10:21 -0700 | [diff] [blame] | 6525 | /* |
| 6526 | * Zero all valid struct pages in range [spfn, epfn), return number of struct |
| 6527 | * pages zeroed |
| 6528 | */ |
| 6529 | static u64 zero_pfn_range(unsigned long spfn, unsigned long epfn) |
| 6530 | { |
| 6531 | unsigned long pfn; |
| 6532 | u64 pgcnt = 0; |
| 6533 | |
| 6534 | for (pfn = spfn; pfn < epfn; pfn++) { |
| 6535 | if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) { |
| 6536 | pfn = ALIGN_DOWN(pfn, pageblock_nr_pages) |
| 6537 | + pageblock_nr_pages - 1; |
| 6538 | continue; |
| 6539 | } |
| 6540 | mm_zero_struct_page(pfn_to_page(pfn)); |
| 6541 | pgcnt++; |
| 6542 | } |
| 6543 | |
| 6544 | return pgcnt; |
| 6545 | } |
| 6546 | |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6547 | /* |
| 6548 | * Only struct pages that are backed by physical memory are zeroed and |
| 6549 | * initialized by going through __init_single_page(). But, there are some |
| 6550 | * struct pages which are reserved in memblock allocator and their fields |
| 6551 | * may be accessed (for example page_to_pfn() on some configuration accesses |
| 6552 | * flags). We must explicitly zero those struct pages. |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6553 | * |
| 6554 | * This function also addresses a similar issue where struct pages are left |
| 6555 | * uninitialized because the physical address range is not covered by |
| 6556 | * memblock.memory or memblock.reserved. That could happen when memblock |
| 6557 | * layout is manually configured via memmap=. |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6558 | */ |
Oscar Salvador | 03e85f9 | 2018-08-21 21:53:43 -0700 | [diff] [blame] | 6559 | void __init zero_resv_unavail(void) |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6560 | { |
| 6561 | phys_addr_t start, end; |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6562 | u64 i, pgcnt; |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6563 | phys_addr_t next = 0; |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6564 | |
| 6565 | /* |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6566 | * Loop through unavailable ranges not covered by memblock.memory. |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6567 | */ |
| 6568 | pgcnt = 0; |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6569 | for_each_mem_range(i, &memblock.memory, NULL, |
| 6570 | NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, NULL) { |
Pavel Tatashin | ec393a0f | 2018-10-26 15:10:21 -0700 | [diff] [blame] | 6571 | if (next < start) |
| 6572 | pgcnt += zero_pfn_range(PFN_DOWN(next), PFN_UP(start)); |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6573 | next = end; |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6574 | } |
Pavel Tatashin | ec393a0f | 2018-10-26 15:10:21 -0700 | [diff] [blame] | 6575 | pgcnt += zero_pfn_range(PFN_DOWN(next), max_pfn); |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6576 | |
| 6577 | /* |
| 6578 | * Struct pages that do not have backing memory. This could be because |
| 6579 | * firmware is using some of this memory, or for some other reasons. |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6580 | */ |
| 6581 | if (pgcnt) |
Naoya Horiguchi | 907ec5f | 2018-10-26 15:10:15 -0700 | [diff] [blame] | 6582 | pr_info("Zeroed struct page in unavailable ranges: %lld pages", pgcnt); |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6583 | } |
Mike Rapoport | aca52c3 | 2018-10-30 15:07:44 -0700 | [diff] [blame] | 6584 | #endif /* !CONFIG_FLAT_NODE_MEM_MAP */ |
Pavel Tatashin | a4a3ede | 2017-11-15 17:36:31 -0800 | [diff] [blame] | 6585 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 6586 | #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 6587 | |
| 6588 | #if MAX_NUMNODES > 1 |
| 6589 | /* |
| 6590 | * Figure out the number of possible node ids. |
| 6591 | */ |
Cody P Schafer | f9872ca | 2013-04-29 15:08:01 -0700 | [diff] [blame] | 6592 | void __init setup_nr_node_ids(void) |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 6593 | { |
Wei Yang | 904a955 | 2015-09-08 14:59:48 -0700 | [diff] [blame] | 6594 | unsigned int highest; |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 6595 | |
Wei Yang | 904a955 | 2015-09-08 14:59:48 -0700 | [diff] [blame] | 6596 | highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES); |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 6597 | nr_node_ids = highest + 1; |
| 6598 | } |
Miklos Szeredi | 418508c | 2007-05-23 13:57:55 -0700 | [diff] [blame] | 6599 | #endif |
| 6600 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6601 | /** |
Tejun Heo | 1e01979 | 2011-07-12 09:45:34 +0200 | [diff] [blame] | 6602 | * node_map_pfn_alignment - determine the maximum internode alignment |
| 6603 | * |
| 6604 | * This function should be called after node map is populated and sorted. |
| 6605 | * It calculates the maximum power of two alignment which can distinguish |
| 6606 | * all the nodes. |
| 6607 | * |
| 6608 | * For example, if all nodes are 1GiB and aligned to 1GiB, the return value |
| 6609 | * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the |
| 6610 | * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is |
| 6611 | * shifted, 1GiB is enough and this function will indicate so. |
| 6612 | * |
| 6613 | * This is used to test whether pfn -> nid mapping of the chosen memory |
| 6614 | * model has fine enough granularity to avoid incorrect mapping for the |
| 6615 | * populated node map. |
| 6616 | * |
| 6617 | * Returns the determined alignment in pfn's. 0 if there is no alignment |
| 6618 | * requirement (single node). |
| 6619 | */ |
| 6620 | unsigned long __init node_map_pfn_alignment(void) |
| 6621 | { |
| 6622 | unsigned long accl_mask = 0, last_end = 0; |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6623 | unsigned long start, end, mask; |
Tejun Heo | 1e01979 | 2011-07-12 09:45:34 +0200 | [diff] [blame] | 6624 | int last_nid = -1; |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6625 | int i, nid; |
Tejun Heo | 1e01979 | 2011-07-12 09:45:34 +0200 | [diff] [blame] | 6626 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6627 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) { |
Tejun Heo | 1e01979 | 2011-07-12 09:45:34 +0200 | [diff] [blame] | 6628 | if (!start || last_nid < 0 || last_nid == nid) { |
| 6629 | last_nid = nid; |
| 6630 | last_end = end; |
| 6631 | continue; |
| 6632 | } |
| 6633 | |
| 6634 | /* |
| 6635 | * Start with a mask granular enough to pin-point to the |
| 6636 | * start pfn and tick off bits one-by-one until it becomes |
| 6637 | * too coarse to separate the current node from the last. |
| 6638 | */ |
| 6639 | mask = ~((1 << __ffs(start)) - 1); |
| 6640 | while (mask && last_end <= (start & (mask << 1))) |
| 6641 | mask <<= 1; |
| 6642 | |
| 6643 | /* accumulate all internode masks */ |
| 6644 | accl_mask |= mask; |
| 6645 | } |
| 6646 | |
| 6647 | /* convert mask to number of pages */ |
| 6648 | return ~accl_mask + 1; |
| 6649 | } |
| 6650 | |
Mel Gorman | a6af2bc | 2007-02-10 01:42:57 -0800 | [diff] [blame] | 6651 | /* Find the lowest pfn for a node */ |
Adrian Bunk | b69a728 | 2008-07-23 21:28:12 -0700 | [diff] [blame] | 6652 | static unsigned long __init find_min_pfn_for_node(int nid) |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6653 | { |
Mel Gorman | a6af2bc | 2007-02-10 01:42:57 -0800 | [diff] [blame] | 6654 | unsigned long min_pfn = ULONG_MAX; |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6655 | unsigned long start_pfn; |
| 6656 | int i; |
Mel Gorman | 1abbfb4 | 2006-11-23 12:01:41 +0000 | [diff] [blame] | 6657 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6658 | for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL) |
| 6659 | min_pfn = min(min_pfn, start_pfn); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6660 | |
Mel Gorman | a6af2bc | 2007-02-10 01:42:57 -0800 | [diff] [blame] | 6661 | if (min_pfn == ULONG_MAX) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 6662 | pr_warn("Could not find start_pfn for node %d\n", nid); |
Mel Gorman | a6af2bc | 2007-02-10 01:42:57 -0800 | [diff] [blame] | 6663 | return 0; |
| 6664 | } |
| 6665 | |
| 6666 | return min_pfn; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6667 | } |
| 6668 | |
| 6669 | /** |
| 6670 | * find_min_pfn_with_active_regions - Find the minimum PFN registered |
| 6671 | * |
| 6672 | * It returns the minimum PFN based on information provided via |
Zhang Zhen | 7d01817 | 2014-06-04 16:10:53 -0700 | [diff] [blame] | 6673 | * memblock_set_node(). |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6674 | */ |
| 6675 | unsigned long __init find_min_pfn_with_active_regions(void) |
| 6676 | { |
| 6677 | return find_min_pfn_for_node(MAX_NUMNODES); |
| 6678 | } |
| 6679 | |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6680 | /* |
| 6681 | * early_calculate_totalpages() |
| 6682 | * Sum pages in active regions for movable zone. |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6683 | * Populate N_MEMORY for calculating usable_nodes. |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6684 | */ |
Adrian Bunk | 484f51f | 2007-10-16 01:26:03 -0700 | [diff] [blame] | 6685 | static unsigned long __init early_calculate_totalpages(void) |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6686 | { |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6687 | unsigned long totalpages = 0; |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6688 | unsigned long start_pfn, end_pfn; |
| 6689 | int i, nid; |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6690 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6691 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { |
| 6692 | unsigned long pages = end_pfn - start_pfn; |
| 6693 | |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6694 | totalpages += pages; |
| 6695 | if (pages) |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6696 | node_set_state(nid, N_MEMORY); |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6697 | } |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 6698 | return totalpages; |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6699 | } |
| 6700 | |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6701 | /* |
| 6702 | * Find the PFN the Movable zone begins in each node. Kernel memory |
| 6703 | * is spread evenly between nodes as long as the nodes have enough |
| 6704 | * memory. When they don't, some nodes will have more kernelcore than |
| 6705 | * others |
| 6706 | */ |
Kautuk Consul | b224ef8 | 2012-03-21 16:34:15 -0700 | [diff] [blame] | 6707 | static void __init find_zone_movable_pfns_for_nodes(void) |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6708 | { |
| 6709 | int i, nid; |
| 6710 | unsigned long usable_startpfn; |
| 6711 | unsigned long kernelcore_node, kernelcore_remaining; |
Yinghai Lu | 66918dc | 2009-06-30 11:41:37 -0700 | [diff] [blame] | 6712 | /* save the state before borrow the nodemask */ |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6713 | nodemask_t saved_node_state = node_states[N_MEMORY]; |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6714 | unsigned long totalpages = early_calculate_totalpages(); |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6715 | int usable_nodes = nodes_weight(node_states[N_MEMORY]); |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 6716 | struct memblock_region *r; |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6717 | |
| 6718 | /* Need to find movable_zone earlier when movable_node is specified. */ |
| 6719 | find_usable_zone_for_movable(); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6720 | |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6721 | /* |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6722 | * If movable_node is specified, ignore kernelcore and movablecore |
| 6723 | * options. |
| 6724 | */ |
| 6725 | if (movable_node_is_enabled()) { |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 6726 | for_each_memblock(memory, r) { |
| 6727 | if (!memblock_is_hotpluggable(r)) |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6728 | continue; |
| 6729 | |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 6730 | nid = r->nid; |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6731 | |
Emil Medve | 136199f | 2014-04-07 15:37:52 -0700 | [diff] [blame] | 6732 | usable_startpfn = PFN_DOWN(r->base); |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6733 | zone_movable_pfn[nid] = zone_movable_pfn[nid] ? |
| 6734 | min(usable_startpfn, zone_movable_pfn[nid]) : |
| 6735 | usable_startpfn; |
| 6736 | } |
| 6737 | |
| 6738 | goto out2; |
| 6739 | } |
| 6740 | |
| 6741 | /* |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 6742 | * If kernelcore=mirror is specified, ignore movablecore option |
| 6743 | */ |
| 6744 | if (mirrored_kernelcore) { |
| 6745 | bool mem_below_4gb_not_mirrored = false; |
| 6746 | |
| 6747 | for_each_memblock(memory, r) { |
| 6748 | if (memblock_is_mirror(r)) |
| 6749 | continue; |
| 6750 | |
| 6751 | nid = r->nid; |
| 6752 | |
| 6753 | usable_startpfn = memblock_region_memory_base_pfn(r); |
| 6754 | |
| 6755 | if (usable_startpfn < 0x100000) { |
| 6756 | mem_below_4gb_not_mirrored = true; |
| 6757 | continue; |
| 6758 | } |
| 6759 | |
| 6760 | zone_movable_pfn[nid] = zone_movable_pfn[nid] ? |
| 6761 | min(usable_startpfn, zone_movable_pfn[nid]) : |
| 6762 | usable_startpfn; |
| 6763 | } |
| 6764 | |
| 6765 | if (mem_below_4gb_not_mirrored) |
| 6766 | pr_warn("This configuration results in unmirrored kernel memory."); |
| 6767 | |
| 6768 | goto out2; |
| 6769 | } |
| 6770 | |
| 6771 | /* |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 6772 | * If kernelcore=nn% or movablecore=nn% was specified, calculate the |
| 6773 | * amount of necessary memory. |
| 6774 | */ |
| 6775 | if (required_kernelcore_percent) |
| 6776 | required_kernelcore = (totalpages * 100 * required_kernelcore_percent) / |
| 6777 | 10000UL; |
| 6778 | if (required_movablecore_percent) |
| 6779 | required_movablecore = (totalpages * 100 * required_movablecore_percent) / |
| 6780 | 10000UL; |
| 6781 | |
| 6782 | /* |
| 6783 | * If movablecore= was specified, calculate what size of |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6784 | * kernelcore that corresponds so that memory usable for |
| 6785 | * any allocation type is evenly spread. If both kernelcore |
| 6786 | * and movablecore are specified, then the value of kernelcore |
| 6787 | * will be used for required_kernelcore if it's greater than |
| 6788 | * what movablecore would have allowed. |
| 6789 | */ |
| 6790 | if (required_movablecore) { |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6791 | unsigned long corepages; |
| 6792 | |
| 6793 | /* |
| 6794 | * Round-up so that ZONE_MOVABLE is at least as large as what |
| 6795 | * was requested by the user |
| 6796 | */ |
| 6797 | required_movablecore = |
| 6798 | roundup(required_movablecore, MAX_ORDER_NR_PAGES); |
Xishi Qiu | 9fd745d | 2015-11-05 18:48:11 -0800 | [diff] [blame] | 6799 | required_movablecore = min(totalpages, required_movablecore); |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 6800 | corepages = totalpages - required_movablecore; |
| 6801 | |
| 6802 | required_kernelcore = max(required_kernelcore, corepages); |
| 6803 | } |
| 6804 | |
Xishi Qiu | bde304b | 2015-11-05 18:48:56 -0800 | [diff] [blame] | 6805 | /* |
| 6806 | * If kernelcore was not specified or kernelcore size is larger |
| 6807 | * than totalpages, there is no ZONE_MOVABLE. |
| 6808 | */ |
| 6809 | if (!required_kernelcore || required_kernelcore >= totalpages) |
Yinghai Lu | 66918dc | 2009-06-30 11:41:37 -0700 | [diff] [blame] | 6810 | goto out; |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6811 | |
| 6812 | /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */ |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6813 | usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone]; |
| 6814 | |
| 6815 | restart: |
| 6816 | /* Spread kernelcore memory as evenly as possible throughout nodes */ |
| 6817 | kernelcore_node = required_kernelcore / usable_nodes; |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6818 | for_each_node_state(nid, N_MEMORY) { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6819 | unsigned long start_pfn, end_pfn; |
| 6820 | |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6821 | /* |
| 6822 | * Recalculate kernelcore_node if the division per node |
| 6823 | * now exceeds what is necessary to satisfy the requested |
| 6824 | * amount of memory for the kernel |
| 6825 | */ |
| 6826 | if (required_kernelcore < kernelcore_node) |
| 6827 | kernelcore_node = required_kernelcore / usable_nodes; |
| 6828 | |
| 6829 | /* |
| 6830 | * As the map is walked, we track how much memory is usable |
| 6831 | * by the kernel using kernelcore_remaining. When it is |
| 6832 | * 0, the rest of the node is usable by ZONE_MOVABLE |
| 6833 | */ |
| 6834 | kernelcore_remaining = kernelcore_node; |
| 6835 | |
| 6836 | /* Go through each range of PFNs within this node */ |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6837 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6838 | unsigned long size_pages; |
| 6839 | |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6840 | start_pfn = max(start_pfn, zone_movable_pfn[nid]); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6841 | if (start_pfn >= end_pfn) |
| 6842 | continue; |
| 6843 | |
| 6844 | /* Account for what is only usable for kernelcore */ |
| 6845 | if (start_pfn < usable_startpfn) { |
| 6846 | unsigned long kernel_pages; |
| 6847 | kernel_pages = min(end_pfn, usable_startpfn) |
| 6848 | - start_pfn; |
| 6849 | |
| 6850 | kernelcore_remaining -= min(kernel_pages, |
| 6851 | kernelcore_remaining); |
| 6852 | required_kernelcore -= min(kernel_pages, |
| 6853 | required_kernelcore); |
| 6854 | |
| 6855 | /* Continue if range is now fully accounted */ |
| 6856 | if (end_pfn <= usable_startpfn) { |
| 6857 | |
| 6858 | /* |
| 6859 | * Push zone_movable_pfn to the end so |
| 6860 | * that if we have to rebalance |
| 6861 | * kernelcore across nodes, we will |
| 6862 | * not double account here |
| 6863 | */ |
| 6864 | zone_movable_pfn[nid] = end_pfn; |
| 6865 | continue; |
| 6866 | } |
| 6867 | start_pfn = usable_startpfn; |
| 6868 | } |
| 6869 | |
| 6870 | /* |
| 6871 | * The usable PFN range for ZONE_MOVABLE is from |
| 6872 | * start_pfn->end_pfn. Calculate size_pages as the |
| 6873 | * number of pages used as kernelcore |
| 6874 | */ |
| 6875 | size_pages = end_pfn - start_pfn; |
| 6876 | if (size_pages > kernelcore_remaining) |
| 6877 | size_pages = kernelcore_remaining; |
| 6878 | zone_movable_pfn[nid] = start_pfn + size_pages; |
| 6879 | |
| 6880 | /* |
| 6881 | * Some kernelcore has been met, update counts and |
| 6882 | * break if the kernelcore for this node has been |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 6883 | * satisfied |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6884 | */ |
| 6885 | required_kernelcore -= min(required_kernelcore, |
| 6886 | size_pages); |
| 6887 | kernelcore_remaining -= size_pages; |
| 6888 | if (!kernelcore_remaining) |
| 6889 | break; |
| 6890 | } |
| 6891 | } |
| 6892 | |
| 6893 | /* |
| 6894 | * If there is still required_kernelcore, we do another pass with one |
| 6895 | * less node in the count. This will push zone_movable_pfn[nid] further |
| 6896 | * along on the nodes that still have memory until kernelcore is |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 6897 | * satisfied |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6898 | */ |
| 6899 | usable_nodes--; |
| 6900 | if (usable_nodes && required_kernelcore > usable_nodes) |
| 6901 | goto restart; |
| 6902 | |
Tang Chen | b2f3eeb | 2014-01-21 15:49:38 -0800 | [diff] [blame] | 6903 | out2: |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6904 | /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */ |
| 6905 | for (nid = 0; nid < MAX_NUMNODES; nid++) |
| 6906 | zone_movable_pfn[nid] = |
| 6907 | roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES); |
Yinghai Lu | 66918dc | 2009-06-30 11:41:37 -0700 | [diff] [blame] | 6908 | |
Yinghai Lu | 20e6926 | 2013-03-01 14:51:27 -0800 | [diff] [blame] | 6909 | out: |
Yinghai Lu | 66918dc | 2009-06-30 11:41:37 -0700 | [diff] [blame] | 6910 | /* restore the node_state */ |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6911 | node_states[N_MEMORY] = saved_node_state; |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6912 | } |
| 6913 | |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6914 | /* Any regular or high memory on that node ? */ |
| 6915 | static void check_for_memory(pg_data_t *pgdat, int nid) |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6916 | { |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6917 | enum zone_type zone_type; |
| 6918 | |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6919 | for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) { |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6920 | struct zone *zone = &pgdat->node_zones[zone_type]; |
Xishi Qiu | b38a872 | 2013-11-12 15:07:20 -0800 | [diff] [blame] | 6921 | if (populated_zone(zone)) { |
Oscar Salvador | 7b0e0c0 | 2018-10-26 15:03:58 -0700 | [diff] [blame] | 6922 | if (IS_ENABLED(CONFIG_HIGHMEM)) |
| 6923 | node_set_state(nid, N_HIGH_MEMORY); |
| 6924 | if (zone_type <= ZONE_NORMAL) |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 6925 | node_set_state(nid, N_NORMAL_MEMORY); |
Bob Liu | d0048b0 | 2012-01-12 17:19:07 -0800 | [diff] [blame] | 6926 | break; |
| 6927 | } |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6928 | } |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 6929 | } |
| 6930 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6931 | /** |
| 6932 | * free_area_init_nodes - Initialise all pg_data_t and zone data |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 6933 | * @max_zone_pfn: an array of max PFNs for each zone |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6934 | * |
| 6935 | * This will call free_area_init_node() for each active node in the system. |
Zhang Zhen | 7d01817 | 2014-06-04 16:10:53 -0700 | [diff] [blame] | 6936 | * Using the page ranges provided by memblock_set_node(), the size of each |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6937 | * zone in each node and their holes is calculated. If the maximum PFN |
| 6938 | * between two adjacent zones match, it is assumed that the zone is empty. |
| 6939 | * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed |
| 6940 | * that arch_max_dma32_pfn has no pages. It is also assumed that a zone |
| 6941 | * starts where the previous one ended. For example, ZONE_DMA32 starts |
| 6942 | * at arch_max_dma_pfn. |
| 6943 | */ |
| 6944 | void __init free_area_init_nodes(unsigned long *max_zone_pfn) |
| 6945 | { |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6946 | unsigned long start_pfn, end_pfn; |
| 6947 | int i, nid; |
Mel Gorman | a6af2bc | 2007-02-10 01:42:57 -0800 | [diff] [blame] | 6948 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6949 | /* Record where the zone boundaries are */ |
| 6950 | memset(arch_zone_lowest_possible_pfn, 0, |
| 6951 | sizeof(arch_zone_lowest_possible_pfn)); |
| 6952 | memset(arch_zone_highest_possible_pfn, 0, |
| 6953 | sizeof(arch_zone_highest_possible_pfn)); |
Oliver O'Halloran | 90cae1f | 2016-07-26 15:22:17 -0700 | [diff] [blame] | 6954 | |
| 6955 | start_pfn = find_min_pfn_with_active_regions(); |
| 6956 | |
| 6957 | for (i = 0; i < MAX_NR_ZONES; i++) { |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6958 | if (i == ZONE_MOVABLE) |
| 6959 | continue; |
Oliver O'Halloran | 90cae1f | 2016-07-26 15:22:17 -0700 | [diff] [blame] | 6960 | |
| 6961 | end_pfn = max(max_zone_pfn[i], start_pfn); |
| 6962 | arch_zone_lowest_possible_pfn[i] = start_pfn; |
| 6963 | arch_zone_highest_possible_pfn[i] = end_pfn; |
| 6964 | |
| 6965 | start_pfn = end_pfn; |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6966 | } |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6967 | |
| 6968 | /* Find the PFNs that ZONE_MOVABLE begins at in each node */ |
| 6969 | memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn)); |
Kautuk Consul | b224ef8 | 2012-03-21 16:34:15 -0700 | [diff] [blame] | 6970 | find_zone_movable_pfns_for_nodes(); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6971 | |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6972 | /* Print out the zone ranges */ |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 6973 | pr_info("Zone ranges:\n"); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6974 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 6975 | if (i == ZONE_MOVABLE) |
| 6976 | continue; |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 6977 | pr_info(" %-8s ", zone_names[i]); |
David Rientjes | 72f0ba0 | 2010-03-05 13:42:14 -0800 | [diff] [blame] | 6978 | if (arch_zone_lowest_possible_pfn[i] == |
| 6979 | arch_zone_highest_possible_pfn[i]) |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 6980 | pr_cont("empty\n"); |
David Rientjes | 72f0ba0 | 2010-03-05 13:42:14 -0800 | [diff] [blame] | 6981 | else |
Juergen Gross | 8d29e18 | 2015-02-11 15:26:01 -0800 | [diff] [blame] | 6982 | pr_cont("[mem %#018Lx-%#018Lx]\n", |
| 6983 | (u64)arch_zone_lowest_possible_pfn[i] |
| 6984 | << PAGE_SHIFT, |
| 6985 | ((u64)arch_zone_highest_possible_pfn[i] |
Bjorn Helgaas | a62e2f4 | 2012-05-29 15:06:30 -0700 | [diff] [blame] | 6986 | << PAGE_SHIFT) - 1); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | /* Print out the PFNs ZONE_MOVABLE begins at in each node */ |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 6990 | pr_info("Movable zone start for each node\n"); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6991 | for (i = 0; i < MAX_NUMNODES; i++) { |
| 6992 | if (zone_movable_pfn[i]) |
Juergen Gross | 8d29e18 | 2015-02-11 15:26:01 -0800 | [diff] [blame] | 6993 | pr_info(" Node %d: %#018Lx\n", i, |
| 6994 | (u64)zone_movable_pfn[i] << PAGE_SHIFT); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 6995 | } |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 6996 | |
Wanpeng Li | f2d52fe | 2012-10-08 16:32:24 -0700 | [diff] [blame] | 6997 | /* Print out the early node map */ |
Anton Blanchard | f88dfff | 2014-12-10 15:42:53 -0800 | [diff] [blame] | 6998 | pr_info("Early memory node ranges\n"); |
Tejun Heo | c13291a | 2011-07-12 10:46:30 +0200 | [diff] [blame] | 6999 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) |
Juergen Gross | 8d29e18 | 2015-02-11 15:26:01 -0800 | [diff] [blame] | 7000 | pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid, |
| 7001 | (u64)start_pfn << PAGE_SHIFT, |
| 7002 | ((u64)end_pfn << PAGE_SHIFT) - 1); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 7003 | |
| 7004 | /* Initialise every node */ |
Mel Gorman | 708614e | 2008-07-23 21:26:51 -0700 | [diff] [blame] | 7005 | mminit_verify_pageflags_layout(); |
Christoph Lameter | 8ef8286 | 2007-02-20 13:57:52 -0800 | [diff] [blame] | 7006 | setup_nr_node_ids(); |
Pavel Tatashin | e181ae0c | 2018-07-14 09:15:07 -0400 | [diff] [blame] | 7007 | zero_resv_unavail(); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 7008 | for_each_online_node(nid) { |
| 7009 | pg_data_t *pgdat = NODE_DATA(nid); |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 7010 | free_area_init_node(nid, NULL, |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 7011 | find_min_pfn_for_node(nid), NULL); |
Lee Schermerhorn | 37b07e4 | 2007-10-16 01:25:39 -0700 | [diff] [blame] | 7012 | |
| 7013 | /* Any memory on that node */ |
| 7014 | if (pgdat->node_present_pages) |
Lai Jiangshan | 4b0ef1fe | 2012-12-12 13:51:46 -0800 | [diff] [blame] | 7015 | node_set_state(nid, N_MEMORY); |
| 7016 | check_for_memory(pgdat, nid); |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 7017 | } |
| 7018 | } |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7019 | |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7020 | static int __init cmdline_parse_core(char *p, unsigned long *core, |
| 7021 | unsigned long *percent) |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7022 | { |
| 7023 | unsigned long long coremem; |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7024 | char *endptr; |
| 7025 | |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7026 | if (!p) |
| 7027 | return -EINVAL; |
| 7028 | |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7029 | /* Value may be a percentage of total memory, otherwise bytes */ |
| 7030 | coremem = simple_strtoull(p, &endptr, 0); |
| 7031 | if (*endptr == '%') { |
| 7032 | /* Paranoid check for percent values greater than 100 */ |
| 7033 | WARN_ON(coremem > 100); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7034 | |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7035 | *percent = coremem; |
| 7036 | } else { |
| 7037 | coremem = memparse(p, &p); |
| 7038 | /* Paranoid check that UL is enough for the coremem value */ |
| 7039 | WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX); |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7040 | |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7041 | *core = coremem >> PAGE_SHIFT; |
| 7042 | *percent = 0UL; |
| 7043 | } |
Mel Gorman | 2a1e274 | 2007-07-17 04:03:12 -0700 | [diff] [blame] | 7044 | return 0; |
| 7045 | } |
Mel Gorman | ed7ed36 | 2007-07-17 04:03:14 -0700 | [diff] [blame] | 7046 | |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 7047 | /* |
| 7048 | * kernelcore=size sets the amount of memory for use for allocations that |
| 7049 | * cannot be reclaimed or migrated. |
| 7050 | */ |
| 7051 | static int __init cmdline_parse_kernelcore(char *p) |
| 7052 | { |
Taku Izumi | 342332e | 2016-03-15 14:55:22 -0700 | [diff] [blame] | 7053 | /* parse kernelcore=mirror */ |
| 7054 | if (parse_option_str(p, "mirror")) { |
| 7055 | mirrored_kernelcore = true; |
| 7056 | return 0; |
| 7057 | } |
| 7058 | |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7059 | return cmdline_parse_core(p, &required_kernelcore, |
| 7060 | &required_kernelcore_percent); |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | /* |
| 7064 | * movablecore=size sets the amount of memory for use for allocations that |
| 7065 | * can be reclaimed or migrated. |
| 7066 | */ |
| 7067 | static int __init cmdline_parse_movablecore(char *p) |
| 7068 | { |
David Rientjes | a5c6d65 | 2018-04-05 16:23:09 -0700 | [diff] [blame] | 7069 | return cmdline_parse_core(p, &required_movablecore, |
| 7070 | &required_movablecore_percent); |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 7071 | } |
| 7072 | |
Mel Gorman | ed7ed36 | 2007-07-17 04:03:14 -0700 | [diff] [blame] | 7073 | early_param("kernelcore", cmdline_parse_kernelcore); |
Mel Gorman | 7e63efef | 2007-07-17 04:03:15 -0700 | [diff] [blame] | 7074 | early_param("movablecore", cmdline_parse_movablecore); |
Mel Gorman | ed7ed36 | 2007-07-17 04:03:14 -0700 | [diff] [blame] | 7075 | |
Tejun Heo | 0ee332c | 2011-12-08 10:22:09 -0800 | [diff] [blame] | 7076 | #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
Mel Gorman | c713216 | 2006-09-27 01:49:43 -0700 | [diff] [blame] | 7077 | |
Jiang Liu | c3d5f5f | 2013-07-03 15:03:14 -0700 | [diff] [blame] | 7078 | void adjust_managed_page_count(struct page *page, long count) |
| 7079 | { |
| 7080 | spin_lock(&managed_page_count_lock); |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7081 | atomic_long_add(count, &page_zone(page)->managed_pages); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7082 | totalram_pages_add(count); |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 7083 | #ifdef CONFIG_HIGHMEM |
| 7084 | if (PageHighMem(page)) |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7085 | totalhigh_pages_add(count); |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 7086 | #endif |
Jiang Liu | c3d5f5f | 2013-07-03 15:03:14 -0700 | [diff] [blame] | 7087 | spin_unlock(&managed_page_count_lock); |
| 7088 | } |
Jiang Liu | 3dcc057 | 2013-07-03 15:03:21 -0700 | [diff] [blame] | 7089 | EXPORT_SYMBOL(adjust_managed_page_count); |
Jiang Liu | c3d5f5f | 2013-07-03 15:03:14 -0700 | [diff] [blame] | 7090 | |
Jiang Liu | 1119969 | 2013-07-03 15:02:48 -0700 | [diff] [blame] | 7091 | unsigned long free_reserved_area(void *start, void *end, int poison, char *s) |
Jiang Liu | 69afade | 2013-04-29 15:06:21 -0700 | [diff] [blame] | 7092 | { |
Jiang Liu | 1119969 | 2013-07-03 15:02:48 -0700 | [diff] [blame] | 7093 | void *pos; |
| 7094 | unsigned long pages = 0; |
Jiang Liu | 69afade | 2013-04-29 15:06:21 -0700 | [diff] [blame] | 7095 | |
Jiang Liu | 1119969 | 2013-07-03 15:02:48 -0700 | [diff] [blame] | 7096 | start = (void *)PAGE_ALIGN((unsigned long)start); |
| 7097 | end = (void *)((unsigned long)end & PAGE_MASK); |
| 7098 | for (pos = start; pos < end; pos += PAGE_SIZE, pages++) { |
Dave Hansen | 0d83432 | 2018-08-02 15:58:26 -0700 | [diff] [blame] | 7099 | struct page *page = virt_to_page(pos); |
| 7100 | void *direct_map_addr; |
| 7101 | |
| 7102 | /* |
| 7103 | * 'direct_map_addr' might be different from 'pos' |
| 7104 | * because some architectures' virt_to_page() |
| 7105 | * work with aliases. Getting the direct map |
| 7106 | * address ensures that we get a _writeable_ |
| 7107 | * alias for the memset(). |
| 7108 | */ |
| 7109 | direct_map_addr = page_address(page); |
Jiang Liu | dbe67df | 2013-07-03 15:02:51 -0700 | [diff] [blame] | 7110 | if ((unsigned int)poison <= 0xFF) |
Dave Hansen | 0d83432 | 2018-08-02 15:58:26 -0700 | [diff] [blame] | 7111 | memset(direct_map_addr, poison, PAGE_SIZE); |
| 7112 | |
| 7113 | free_reserved_page(page); |
Jiang Liu | 69afade | 2013-04-29 15:06:21 -0700 | [diff] [blame] | 7114 | } |
| 7115 | |
| 7116 | if (pages && s) |
Josh Poimboeuf | adb1fe9 | 2016-10-25 09:51:14 -0500 | [diff] [blame] | 7117 | pr_info("Freeing %s memory: %ldK\n", |
| 7118 | s, pages << (PAGE_SHIFT - 10)); |
Jiang Liu | 69afade | 2013-04-29 15:06:21 -0700 | [diff] [blame] | 7119 | |
| 7120 | return pages; |
| 7121 | } |
Jiang Liu | 1119969 | 2013-07-03 15:02:48 -0700 | [diff] [blame] | 7122 | EXPORT_SYMBOL(free_reserved_area); |
Jiang Liu | 69afade | 2013-04-29 15:06:21 -0700 | [diff] [blame] | 7123 | |
Jiang Liu | cfa11e0 | 2013-04-29 15:07:00 -0700 | [diff] [blame] | 7124 | #ifdef CONFIG_HIGHMEM |
| 7125 | void free_highmem_page(struct page *page) |
| 7126 | { |
| 7127 | __free_reserved_page(page); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7128 | totalram_pages_inc(); |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7129 | atomic_long_inc(&page_zone(page)->managed_pages); |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7130 | totalhigh_pages_inc(); |
Jiang Liu | cfa11e0 | 2013-04-29 15:07:00 -0700 | [diff] [blame] | 7131 | } |
| 7132 | #endif |
| 7133 | |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7134 | |
| 7135 | void __init mem_init_print_info(const char *str) |
| 7136 | { |
| 7137 | unsigned long physpages, codesize, datasize, rosize, bss_size; |
| 7138 | unsigned long init_code_size, init_data_size; |
| 7139 | |
| 7140 | physpages = get_num_physpages(); |
| 7141 | codesize = _etext - _stext; |
| 7142 | datasize = _edata - _sdata; |
| 7143 | rosize = __end_rodata - __start_rodata; |
| 7144 | bss_size = __bss_stop - __bss_start; |
| 7145 | init_data_size = __init_end - __init_begin; |
| 7146 | init_code_size = _einittext - _sinittext; |
| 7147 | |
| 7148 | /* |
| 7149 | * Detect special cases and adjust section sizes accordingly: |
| 7150 | * 1) .init.* may be embedded into .data sections |
| 7151 | * 2) .init.text.* may be out of [__init_begin, __init_end], |
| 7152 | * please refer to arch/tile/kernel/vmlinux.lds.S. |
| 7153 | * 3) .rodata.* may be embedded into .text or .data sections. |
| 7154 | */ |
| 7155 | #define adj_init_size(start, end, size, pos, adj) \ |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 7156 | do { \ |
| 7157 | if (start <= pos && pos < end && size > adj) \ |
| 7158 | size -= adj; \ |
| 7159 | } while (0) |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7160 | |
| 7161 | adj_init_size(__init_begin, __init_end, init_data_size, |
| 7162 | _sinittext, init_code_size); |
| 7163 | adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size); |
| 7164 | adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size); |
| 7165 | adj_init_size(_stext, _etext, codesize, __start_rodata, rosize); |
| 7166 | adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize); |
| 7167 | |
| 7168 | #undef adj_init_size |
| 7169 | |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 7170 | pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved" |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7171 | #ifdef CONFIG_HIGHMEM |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 7172 | ", %luK highmem" |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7173 | #endif |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 7174 | "%s%s)\n", |
| 7175 | nr_free_pages() << (PAGE_SHIFT - 10), |
| 7176 | physpages << (PAGE_SHIFT - 10), |
| 7177 | codesize >> 10, datasize >> 10, rosize >> 10, |
| 7178 | (init_data_size + init_code_size) >> 10, bss_size >> 10, |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7179 | (physpages - totalram_pages() - totalcma_pages) << (PAGE_SHIFT - 10), |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 7180 | totalcma_pages << (PAGE_SHIFT - 10), |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7181 | #ifdef CONFIG_HIGHMEM |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame^] | 7182 | totalhigh_pages() << (PAGE_SHIFT - 10), |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7183 | #endif |
Joe Perches | 756a025 | 2016-03-17 14:19:47 -0700 | [diff] [blame] | 7184 | str ? ", " : "", str ? str : ""); |
Jiang Liu | 7ee3d4e | 2013-07-03 15:03:41 -0700 | [diff] [blame] | 7185 | } |
| 7186 | |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 7187 | /** |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 7188 | * set_dma_reserve - set the specified number of pages reserved in the first zone |
| 7189 | * @new_dma_reserve: The number of pages to mark reserved |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 7190 | * |
Yaowei Bai | 013110a | 2015-09-08 15:04:10 -0700 | [diff] [blame] | 7191 | * The per-cpu batchsize and zone watermarks are determined by managed_pages. |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 7192 | * In the DMA zone, a significant percentage may be consumed by kernel image |
| 7193 | * and other unfreeable allocations which can skew the watermarks badly. This |
Randy Dunlap | 88ca3b9 | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 7194 | * function may optionally be used to account for unfreeable pages in the |
| 7195 | * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and |
| 7196 | * smaller per-cpu batchsize. |
Mel Gorman | 0e0b864 | 2006-09-27 01:49:56 -0700 | [diff] [blame] | 7197 | */ |
| 7198 | void __init set_dma_reserve(unsigned long new_dma_reserve) |
| 7199 | { |
| 7200 | dma_reserve = new_dma_reserve; |
| 7201 | } |
| 7202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7203 | void __init free_area_init(unsigned long *zones_size) |
| 7204 | { |
Pavel Tatashin | e181ae0c | 2018-07-14 09:15:07 -0400 | [diff] [blame] | 7205 | zero_resv_unavail(); |
Johannes Weiner | 9109fb7 | 2008-07-23 21:27:20 -0700 | [diff] [blame] | 7206 | free_area_init_node(0, zones_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7207 | __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL); |
| 7208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | |
Sebastian Andrzej Siewior | 005fd4b | 2016-11-03 15:50:02 +0100 | [diff] [blame] | 7210 | static int page_alloc_cpu_dead(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7212 | |
Sebastian Andrzej Siewior | 005fd4b | 2016-11-03 15:50:02 +0100 | [diff] [blame] | 7213 | lru_add_drain_cpu(cpu); |
| 7214 | drain_pages(cpu); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 7215 | |
Sebastian Andrzej Siewior | 005fd4b | 2016-11-03 15:50:02 +0100 | [diff] [blame] | 7216 | /* |
| 7217 | * Spill the event counters of the dead processor |
| 7218 | * into the current processors event counters. |
| 7219 | * This artificially elevates the count of the current |
| 7220 | * processor. |
| 7221 | */ |
| 7222 | vm_events_fold_cpu(cpu); |
Christoph Lameter | 9f8f217 | 2008-02-04 22:29:11 -0800 | [diff] [blame] | 7223 | |
Sebastian Andrzej Siewior | 005fd4b | 2016-11-03 15:50:02 +0100 | [diff] [blame] | 7224 | /* |
| 7225 | * Zero the differential counters of the dead processor |
| 7226 | * so that the vm statistics are consistent. |
| 7227 | * |
| 7228 | * This is only okay since the processor is dead and cannot |
| 7229 | * race with what we are doing. |
| 7230 | */ |
| 7231 | cpu_vm_stats_fold(cpu); |
| 7232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7233 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | |
| 7235 | void __init page_alloc_init(void) |
| 7236 | { |
Sebastian Andrzej Siewior | 005fd4b | 2016-11-03 15:50:02 +0100 | [diff] [blame] | 7237 | int ret; |
| 7238 | |
| 7239 | ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC_DEAD, |
| 7240 | "mm/page_alloc:dead", NULL, |
| 7241 | page_alloc_cpu_dead); |
| 7242 | WARN_ON(ret < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7243 | } |
| 7244 | |
| 7245 | /* |
Yaowei Bai | 34b1006 | 2015-09-08 15:04:13 -0700 | [diff] [blame] | 7246 | * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7247 | * or min_free_kbytes changes. |
| 7248 | */ |
| 7249 | static void calculate_totalreserve_pages(void) |
| 7250 | { |
| 7251 | struct pglist_data *pgdat; |
| 7252 | unsigned long reserve_pages = 0; |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 7253 | enum zone_type i, j; |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7254 | |
| 7255 | for_each_online_pgdat(pgdat) { |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 7256 | |
| 7257 | pgdat->totalreserve_pages = 0; |
| 7258 | |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7259 | for (i = 0; i < MAX_NR_ZONES; i++) { |
| 7260 | struct zone *zone = pgdat->node_zones + i; |
Mel Gorman | 3484b2d | 2014-08-06 16:07:14 -0700 | [diff] [blame] | 7261 | long max = 0; |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7262 | unsigned long managed_pages = zone_managed_pages(zone); |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7263 | |
| 7264 | /* Find valid and maximum lowmem_reserve in the zone */ |
| 7265 | for (j = i; j < MAX_NR_ZONES; j++) { |
| 7266 | if (zone->lowmem_reserve[j] > max) |
| 7267 | max = zone->lowmem_reserve[j]; |
| 7268 | } |
| 7269 | |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 7270 | /* we treat the high watermark as reserved pages. */ |
| 7271 | max += high_wmark_pages(zone); |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7272 | |
Arun KS | 3d6357d | 2018-12-28 00:34:20 -0800 | [diff] [blame] | 7273 | if (max > managed_pages) |
| 7274 | max = managed_pages; |
Johannes Weiner | a8d0143 | 2016-01-14 15:20:15 -0800 | [diff] [blame] | 7275 | |
Mel Gorman | 281e372 | 2016-07-28 15:46:11 -0700 | [diff] [blame] | 7276 | pgdat->totalreserve_pages += max; |
Johannes Weiner | a8d0143 | 2016-01-14 15:20:15 -0800 | [diff] [blame] | 7277 | |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7278 | reserve_pages += max; |
| 7279 | } |
| 7280 | } |
| 7281 | totalreserve_pages = reserve_pages; |
| 7282 | } |
| 7283 | |
| 7284 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | * setup_per_zone_lowmem_reserve - called whenever |
Yaowei Bai | 34b1006 | 2015-09-08 15:04:13 -0700 | [diff] [blame] | 7286 | * sysctl_lowmem_reserve_ratio changes. Ensures that each zone |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | * has a correct pages reserved value, so an adequate number of |
| 7288 | * pages are left in the zone after a successful __alloc_pages(). |
| 7289 | */ |
| 7290 | static void setup_per_zone_lowmem_reserve(void) |
| 7291 | { |
| 7292 | struct pglist_data *pgdat; |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 7293 | enum zone_type j, idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | |
KAMEZAWA Hiroyuki | ec936fc | 2006-03-27 01:15:59 -0800 | [diff] [blame] | 7295 | for_each_online_pgdat(pgdat) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | for (j = 0; j < MAX_NR_ZONES; j++) { |
| 7297 | struct zone *zone = pgdat->node_zones + j; |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7298 | unsigned long managed_pages = zone_managed_pages(zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7299 | |
| 7300 | zone->lowmem_reserve[j] = 0; |
| 7301 | |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 7302 | idx = j; |
| 7303 | while (idx) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7304 | struct zone *lower_zone; |
| 7305 | |
Christoph Lameter | 2f6726e | 2006-09-25 23:31:18 -0700 | [diff] [blame] | 7306 | idx--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7307 | lower_zone = pgdat->node_zones + idx; |
Joonsoo Kim | d3cda23 | 2018-04-10 16:30:11 -0700 | [diff] [blame] | 7308 | |
| 7309 | if (sysctl_lowmem_reserve_ratio[idx] < 1) { |
| 7310 | sysctl_lowmem_reserve_ratio[idx] = 0; |
| 7311 | lower_zone->lowmem_reserve[j] = 0; |
| 7312 | } else { |
| 7313 | lower_zone->lowmem_reserve[j] = |
| 7314 | managed_pages / sysctl_lowmem_reserve_ratio[idx]; |
| 7315 | } |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7316 | managed_pages += zone_managed_pages(lower_zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | } |
| 7318 | } |
| 7319 | } |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7320 | |
| 7321 | /* update totalreserve_pages */ |
| 7322 | calculate_totalreserve_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7323 | } |
| 7324 | |
Mel Gorman | cfd3da1 | 2011-04-25 21:36:42 +0000 | [diff] [blame] | 7325 | static void __setup_per_zone_wmarks(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7326 | { |
| 7327 | unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10); |
| 7328 | unsigned long lowmem_pages = 0; |
| 7329 | struct zone *zone; |
| 7330 | unsigned long flags; |
| 7331 | |
| 7332 | /* Calculate total number of !ZONE_HIGHMEM pages */ |
| 7333 | for_each_zone(zone) { |
| 7334 | if (!is_highmem(zone)) |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7335 | lowmem_pages += zone_managed_pages(zone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | for_each_zone(zone) { |
Andrew Morton | ac924c6 | 2006-05-15 09:43:59 -0700 | [diff] [blame] | 7339 | u64 tmp; |
| 7340 | |
Gerald Schaefer | 1125b4e | 2008-10-18 20:27:11 -0700 | [diff] [blame] | 7341 | spin_lock_irqsave(&zone->lock, flags); |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7342 | tmp = (u64)pages_min * zone_managed_pages(zone); |
Andrew Morton | ac924c6 | 2006-05-15 09:43:59 -0700 | [diff] [blame] | 7343 | do_div(tmp, lowmem_pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7344 | if (is_highmem(zone)) { |
| 7345 | /* |
Nick Piggin | 669ed17 | 2005-11-13 16:06:45 -0800 | [diff] [blame] | 7346 | * __GFP_HIGH and PF_MEMALLOC allocations usually don't |
| 7347 | * need highmem pages, so cap pages_min to a small |
| 7348 | * value here. |
| 7349 | * |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 7350 | * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN) |
Yaowei Bai | 42ff270 | 2015-04-14 15:47:14 -0700 | [diff] [blame] | 7351 | * deltas control asynch page reclaim, and so should |
Nick Piggin | 669ed17 | 2005-11-13 16:06:45 -0800 | [diff] [blame] | 7352 | * not be capped for highmem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7353 | */ |
Andrew Morton | 90ae8d6 | 2013-02-22 16:32:22 -0800 | [diff] [blame] | 7354 | unsigned long min_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7355 | |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7356 | min_pages = zone_managed_pages(zone) / 1024; |
Andrew Morton | 90ae8d6 | 2013-02-22 16:32:22 -0800 | [diff] [blame] | 7357 | min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL); |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 7358 | zone->watermark[WMARK_MIN] = min_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | } else { |
Nick Piggin | 669ed17 | 2005-11-13 16:06:45 -0800 | [diff] [blame] | 7360 | /* |
| 7361 | * If it's a lowmem zone, reserve a number of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7362 | * proportionate to the zone's size. |
| 7363 | */ |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 7364 | zone->watermark[WMARK_MIN] = tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7365 | } |
| 7366 | |
Johannes Weiner | 795ae7a | 2016-03-17 14:19:14 -0700 | [diff] [blame] | 7367 | /* |
| 7368 | * Set the kswapd watermarks distance according to the |
| 7369 | * scale factor in proportion to available memory, but |
| 7370 | * ensure a minimum size on small systems. |
| 7371 | */ |
| 7372 | tmp = max_t(u64, tmp >> 2, |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7373 | mult_frac(zone_managed_pages(zone), |
Johannes Weiner | 795ae7a | 2016-03-17 14:19:14 -0700 | [diff] [blame] | 7374 | watermark_scale_factor, 10000)); |
| 7375 | |
| 7376 | zone->watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp; |
| 7377 | zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) + tmp * 2; |
Marek Szyprowski | 49f223a | 2012-01-25 12:49:24 +0100 | [diff] [blame] | 7378 | |
Gerald Schaefer | 1125b4e | 2008-10-18 20:27:11 -0700 | [diff] [blame] | 7379 | spin_unlock_irqrestore(&zone->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7380 | } |
Hideo AOKI | cb45b0e | 2006-04-10 22:52:59 -0700 | [diff] [blame] | 7381 | |
| 7382 | /* update totalreserve_pages */ |
| 7383 | calculate_totalreserve_pages(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | } |
| 7385 | |
Mel Gorman | cfd3da1 | 2011-04-25 21:36:42 +0000 | [diff] [blame] | 7386 | /** |
| 7387 | * setup_per_zone_wmarks - called when min_free_kbytes changes |
| 7388 | * or when memory is hot-{added|removed} |
| 7389 | * |
| 7390 | * Ensures that the watermark[min,low,high] values for each zone are set |
| 7391 | * correctly with respect to min_free_kbytes. |
| 7392 | */ |
| 7393 | void setup_per_zone_wmarks(void) |
| 7394 | { |
Michal Hocko | b93e0f3 | 2017-09-06 16:20:37 -0700 | [diff] [blame] | 7395 | static DEFINE_SPINLOCK(lock); |
| 7396 | |
| 7397 | spin_lock(&lock); |
Mel Gorman | cfd3da1 | 2011-04-25 21:36:42 +0000 | [diff] [blame] | 7398 | __setup_per_zone_wmarks(); |
Michal Hocko | b93e0f3 | 2017-09-06 16:20:37 -0700 | [diff] [blame] | 7399 | spin_unlock(&lock); |
Mel Gorman | cfd3da1 | 2011-04-25 21:36:42 +0000 | [diff] [blame] | 7400 | } |
| 7401 | |
Randy Dunlap | 55a4462 | 2009-09-21 17:01:20 -0700 | [diff] [blame] | 7402 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7403 | * Initialise min_free_kbytes. |
| 7404 | * |
| 7405 | * For small machines we want it small (128k min). For large machines |
| 7406 | * we want it large (64MB max). But it is not linear, because network |
| 7407 | * bandwidth does not increase linearly with machine size. We use |
| 7408 | * |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 7409 | * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | * min_free_kbytes = sqrt(lowmem_kbytes * 16) |
| 7411 | * |
| 7412 | * which yields |
| 7413 | * |
| 7414 | * 16MB: 512k |
| 7415 | * 32MB: 724k |
| 7416 | * 64MB: 1024k |
| 7417 | * 128MB: 1448k |
| 7418 | * 256MB: 2048k |
| 7419 | * 512MB: 2896k |
| 7420 | * 1024MB: 4096k |
| 7421 | * 2048MB: 5792k |
| 7422 | * 4096MB: 8192k |
| 7423 | * 8192MB: 11584k |
| 7424 | * 16384MB: 16384k |
| 7425 | */ |
KOSAKI Motohiro | 1b79acc | 2011-05-24 17:11:32 -0700 | [diff] [blame] | 7426 | int __meminit init_per_zone_wmark_min(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | { |
| 7428 | unsigned long lowmem_kbytes; |
Michal Hocko | 5f12733 | 2013-07-08 16:00:40 -0700 | [diff] [blame] | 7429 | int new_min_free_kbytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7430 | |
| 7431 | lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10); |
Michal Hocko | 5f12733 | 2013-07-08 16:00:40 -0700 | [diff] [blame] | 7432 | new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | |
Michal Hocko | 5f12733 | 2013-07-08 16:00:40 -0700 | [diff] [blame] | 7434 | if (new_min_free_kbytes > user_min_free_kbytes) { |
| 7435 | min_free_kbytes = new_min_free_kbytes; |
| 7436 | if (min_free_kbytes < 128) |
| 7437 | min_free_kbytes = 128; |
| 7438 | if (min_free_kbytes > 65536) |
| 7439 | min_free_kbytes = 65536; |
| 7440 | } else { |
| 7441 | pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n", |
| 7442 | new_min_free_kbytes, user_min_free_kbytes); |
| 7443 | } |
Minchan Kim | bc75d33 | 2009-06-16 15:32:48 -0700 | [diff] [blame] | 7444 | setup_per_zone_wmarks(); |
KOSAKI Motohiro | a6cccdc | 2011-05-24 17:11:33 -0700 | [diff] [blame] | 7445 | refresh_zone_stat_thresholds(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7446 | setup_per_zone_lowmem_reserve(); |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7447 | |
| 7448 | #ifdef CONFIG_NUMA |
| 7449 | setup_min_unmapped_ratio(); |
| 7450 | setup_min_slab_ratio(); |
| 7451 | #endif |
| 7452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | return 0; |
| 7454 | } |
Jason Baron | bc22af74 | 2016-05-05 16:22:12 -0700 | [diff] [blame] | 7455 | core_initcall(init_per_zone_wmark_min) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7456 | |
| 7457 | /* |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 7458 | * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7459 | * that we can call two helper functions whenever min_free_kbytes |
| 7460 | * changes. |
| 7461 | */ |
Joe Perches | cccad5b | 2014-06-06 14:38:09 -0700 | [diff] [blame] | 7462 | int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7463 | void __user *buffer, size_t *length, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7464 | { |
Han Pingtian | da8c757 | 2014-01-23 15:53:17 -0800 | [diff] [blame] | 7465 | int rc; |
| 7466 | |
| 7467 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 7468 | if (rc) |
| 7469 | return rc; |
| 7470 | |
Michal Hocko | 5f12733 | 2013-07-08 16:00:40 -0700 | [diff] [blame] | 7471 | if (write) { |
| 7472 | user_min_free_kbytes = min_free_kbytes; |
Minchan Kim | bc75d33 | 2009-06-16 15:32:48 -0700 | [diff] [blame] | 7473 | setup_per_zone_wmarks(); |
Michal Hocko | 5f12733 | 2013-07-08 16:00:40 -0700 | [diff] [blame] | 7474 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7475 | return 0; |
| 7476 | } |
| 7477 | |
Johannes Weiner | 795ae7a | 2016-03-17 14:19:14 -0700 | [diff] [blame] | 7478 | int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write, |
| 7479 | void __user *buffer, size_t *length, loff_t *ppos) |
| 7480 | { |
| 7481 | int rc; |
| 7482 | |
| 7483 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 7484 | if (rc) |
| 7485 | return rc; |
| 7486 | |
| 7487 | if (write) |
| 7488 | setup_per_zone_wmarks(); |
| 7489 | |
| 7490 | return 0; |
| 7491 | } |
| 7492 | |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7493 | #ifdef CONFIG_NUMA |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7494 | static void setup_min_unmapped_ratio(void) |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7495 | { |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7496 | pg_data_t *pgdat; |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7497 | struct zone *zone; |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7498 | |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 7499 | for_each_online_pgdat(pgdat) |
Joonsoo Kim | 81cbcbc | 2016-08-10 16:27:46 -0700 | [diff] [blame] | 7500 | pgdat->min_unmapped_pages = 0; |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 7501 | |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7502 | for_each_zone(zone) |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7503 | zone->zone_pgdat->min_unmapped_pages += (zone_managed_pages(zone) * |
| 7504 | sysctl_min_unmapped_ratio) / 100; |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7505 | } |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7506 | |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7507 | |
| 7508 | int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7509 | void __user *buffer, size_t *length, loff_t *ppos) |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7510 | { |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7511 | int rc; |
| 7512 | |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7513 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7514 | if (rc) |
| 7515 | return rc; |
| 7516 | |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7517 | setup_min_unmapped_ratio(); |
| 7518 | |
| 7519 | return 0; |
| 7520 | } |
| 7521 | |
| 7522 | static void setup_min_slab_ratio(void) |
| 7523 | { |
| 7524 | pg_data_t *pgdat; |
| 7525 | struct zone *zone; |
| 7526 | |
Mel Gorman | a5f5f91 | 2016-07-28 15:46:32 -0700 | [diff] [blame] | 7527 | for_each_online_pgdat(pgdat) |
| 7528 | pgdat->min_slab_pages = 0; |
| 7529 | |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7530 | for_each_zone(zone) |
Arun KS | 9705bea | 2018-12-28 00:34:24 -0800 | [diff] [blame] | 7531 | zone->zone_pgdat->min_slab_pages += (zone_managed_pages(zone) * |
| 7532 | sysctl_min_slab_ratio) / 100; |
Joonsoo Kim | 6423aa8 | 2016-08-10 16:27:49 -0700 | [diff] [blame] | 7533 | } |
| 7534 | |
| 7535 | int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write, |
| 7536 | void __user *buffer, size_t *length, loff_t *ppos) |
| 7537 | { |
| 7538 | int rc; |
| 7539 | |
| 7540 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 7541 | if (rc) |
| 7542 | return rc; |
| 7543 | |
| 7544 | setup_min_slab_ratio(); |
| 7545 | |
Christoph Lameter | 0ff3849 | 2006-09-25 23:31:52 -0700 | [diff] [blame] | 7546 | return 0; |
| 7547 | } |
Christoph Lameter | 9614634 | 2006-07-03 00:24:13 -0700 | [diff] [blame] | 7548 | #endif |
| 7549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7550 | /* |
| 7551 | * lowmem_reserve_ratio_sysctl_handler - just a wrapper around |
| 7552 | * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve() |
| 7553 | * whenever sysctl_lowmem_reserve_ratio changes. |
| 7554 | * |
| 7555 | * The reserve ratio obviously has absolutely no relation with the |
Mel Gorman | 4185896 | 2009-06-16 15:32:12 -0700 | [diff] [blame] | 7556 | * minimum watermarks. The lowmem reserve ratio can only make sense |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | * if in function of the boot time zone sizes. |
| 7558 | */ |
Joe Perches | cccad5b | 2014-06-06 14:38:09 -0700 | [diff] [blame] | 7559 | int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7560 | void __user *buffer, size_t *length, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | { |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7562 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | setup_per_zone_lowmem_reserve(); |
| 7564 | return 0; |
| 7565 | } |
| 7566 | |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7567 | /* |
| 7568 | * percpu_pagelist_fraction - changes the pcp->high for each zone on each |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 7569 | * cpu. It is the fraction of total pages in each zone that a hot per cpu |
| 7570 | * pagelist can have before it gets flushed back to buddy allocator. |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7571 | */ |
Joe Perches | cccad5b | 2014-06-06 14:38:09 -0700 | [diff] [blame] | 7572 | int percpu_pagelist_fraction_sysctl_handler(struct ctl_table *table, int write, |
Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 7573 | void __user *buffer, size_t *length, loff_t *ppos) |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7574 | { |
| 7575 | struct zone *zone; |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 7576 | int old_percpu_pagelist_fraction; |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7577 | int ret; |
| 7578 | |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 7579 | mutex_lock(&pcp_batch_high_lock); |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 7580 | old_percpu_pagelist_fraction = percpu_pagelist_fraction; |
| 7581 | |
| 7582 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
| 7583 | if (!write || ret < 0) |
| 7584 | goto out; |
| 7585 | |
| 7586 | /* Sanity checking to avoid pcp imbalance */ |
| 7587 | if (percpu_pagelist_fraction && |
| 7588 | percpu_pagelist_fraction < MIN_PERCPU_PAGELIST_FRACTION) { |
| 7589 | percpu_pagelist_fraction = old_percpu_pagelist_fraction; |
| 7590 | ret = -EINVAL; |
| 7591 | goto out; |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7592 | } |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 7593 | |
| 7594 | /* No change? */ |
| 7595 | if (percpu_pagelist_fraction == old_percpu_pagelist_fraction) |
| 7596 | goto out; |
| 7597 | |
| 7598 | for_each_populated_zone(zone) { |
| 7599 | unsigned int cpu; |
| 7600 | |
| 7601 | for_each_possible_cpu(cpu) |
| 7602 | pageset_set_high_and_batch(zone, |
| 7603 | per_cpu_ptr(zone->pageset, cpu)); |
| 7604 | } |
| 7605 | out: |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 7606 | mutex_unlock(&pcp_batch_high_lock); |
David Rientjes | 7cd2b0a | 2014-06-23 13:22:04 -0700 | [diff] [blame] | 7607 | return ret; |
Rohit Seth | 8ad4b1f | 2006-01-08 01:00:40 -0800 | [diff] [blame] | 7608 | } |
| 7609 | |
Rasmus Villemoes | a9919c7 | 2015-06-24 16:56:28 -0700 | [diff] [blame] | 7610 | #ifdef CONFIG_NUMA |
David S. Miller | f034b5d | 2006-08-24 03:08:07 -0700 | [diff] [blame] | 7611 | int hashdist = HASHDIST_DEFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7613 | static int __init set_hashdist(char *str) |
| 7614 | { |
| 7615 | if (!str) |
| 7616 | return 0; |
| 7617 | hashdist = simple_strtoul(str, &str, 0); |
| 7618 | return 1; |
| 7619 | } |
| 7620 | __setup("hashdist=", set_hashdist); |
| 7621 | #endif |
| 7622 | |
Srikar Dronamraju | f6f34b4 | 2016-10-07 16:59:15 -0700 | [diff] [blame] | 7623 | #ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES |
| 7624 | /* |
| 7625 | * Returns the number of pages that arch has reserved but |
| 7626 | * is not known to alloc_large_system_hash(). |
| 7627 | */ |
| 7628 | static unsigned long __init arch_reserved_kernel_pages(void) |
| 7629 | { |
| 7630 | return 0; |
| 7631 | } |
| 7632 | #endif |
| 7633 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7634 | /* |
Pavel Tatashin | 9017217 | 2017-07-06 15:39:14 -0700 | [diff] [blame] | 7635 | * Adaptive scale is meant to reduce sizes of hash tables on large memory |
| 7636 | * machines. As memory size is increased the scale is also increased but at |
| 7637 | * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory |
| 7638 | * quadruples the scale is increased by one, which means the size of hash table |
| 7639 | * only doubles, instead of quadrupling as well. |
| 7640 | * Because 32-bit systems cannot have large physical memory, where this scaling |
| 7641 | * makes sense, it is disabled on such platforms. |
| 7642 | */ |
| 7643 | #if __BITS_PER_LONG > 32 |
| 7644 | #define ADAPT_SCALE_BASE (64ul << 30) |
| 7645 | #define ADAPT_SCALE_SHIFT 2 |
| 7646 | #define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT) |
| 7647 | #endif |
| 7648 | |
| 7649 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7650 | * allocate a large system hash table from bootmem |
| 7651 | * - it is assumed that the hash table must contain an exact power-of-2 |
| 7652 | * quantity of entries |
| 7653 | * - limit is the number of hash buckets, not the total allocation size |
| 7654 | */ |
| 7655 | void *__init alloc_large_system_hash(const char *tablename, |
| 7656 | unsigned long bucketsize, |
| 7657 | unsigned long numentries, |
| 7658 | int scale, |
| 7659 | int flags, |
| 7660 | unsigned int *_hash_shift, |
| 7661 | unsigned int *_hash_mask, |
Tim Bird | 31fe62b | 2012-05-23 13:33:35 +0000 | [diff] [blame] | 7662 | unsigned long low_limit, |
| 7663 | unsigned long high_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7664 | { |
Tim Bird | 31fe62b | 2012-05-23 13:33:35 +0000 | [diff] [blame] | 7665 | unsigned long long max = high_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7666 | unsigned long log2qty, size; |
| 7667 | void *table = NULL; |
Pavel Tatashin | 3749a8f | 2017-07-06 15:39:08 -0700 | [diff] [blame] | 7668 | gfp_t gfp_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7669 | |
| 7670 | /* allow the kernel cmdline to have a say */ |
| 7671 | if (!numentries) { |
| 7672 | /* round applicable memory size up to nearest megabyte */ |
Andrew Morton | 0490366 | 2006-12-06 20:37:33 -0800 | [diff] [blame] | 7673 | numentries = nr_kernel_pages; |
Srikar Dronamraju | f6f34b4 | 2016-10-07 16:59:15 -0700 | [diff] [blame] | 7674 | numentries -= arch_reserved_kernel_pages(); |
Jerry Zhou | a7e8331 | 2013-09-11 14:20:26 -0700 | [diff] [blame] | 7675 | |
| 7676 | /* It isn't necessary when PAGE_SIZE >= 1MB */ |
| 7677 | if (PAGE_SHIFT < 20) |
| 7678 | numentries = round_up(numentries, (1<<20)/PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7679 | |
Pavel Tatashin | 9017217 | 2017-07-06 15:39:14 -0700 | [diff] [blame] | 7680 | #if __BITS_PER_LONG > 32 |
| 7681 | if (!high_limit) { |
| 7682 | unsigned long adapt; |
| 7683 | |
| 7684 | for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries; |
| 7685 | adapt <<= ADAPT_SCALE_SHIFT) |
| 7686 | scale++; |
| 7687 | } |
| 7688 | #endif |
| 7689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7690 | /* limit to 1 bucket per 2^scale bytes of low memory */ |
| 7691 | if (scale > PAGE_SHIFT) |
| 7692 | numentries >>= (scale - PAGE_SHIFT); |
| 7693 | else |
| 7694 | numentries <<= (PAGE_SHIFT - scale); |
Paul Mundt | 9ab37b8 | 2007-01-05 16:36:30 -0800 | [diff] [blame] | 7695 | |
| 7696 | /* Make sure we've got at least a 0-order allocation.. */ |
Jan Beulich | 2c85f51 | 2009-09-21 17:03:07 -0700 | [diff] [blame] | 7697 | if (unlikely(flags & HASH_SMALL)) { |
| 7698 | /* Makes no sense without HASH_EARLY */ |
| 7699 | WARN_ON(!(flags & HASH_EARLY)); |
| 7700 | if (!(numentries >> *_hash_shift)) { |
| 7701 | numentries = 1UL << *_hash_shift; |
| 7702 | BUG_ON(!numentries); |
| 7703 | } |
| 7704 | } else if (unlikely((numentries * bucketsize) < PAGE_SIZE)) |
Paul Mundt | 9ab37b8 | 2007-01-05 16:36:30 -0800 | [diff] [blame] | 7705 | numentries = PAGE_SIZE / bucketsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7706 | } |
John Hawkes | 6e692ed | 2006-03-25 03:08:02 -0800 | [diff] [blame] | 7707 | numentries = roundup_pow_of_two(numentries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7708 | |
| 7709 | /* limit allocation size to 1/16 total memory by default */ |
| 7710 | if (max == 0) { |
| 7711 | max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4; |
| 7712 | do_div(max, bucketsize); |
| 7713 | } |
Dimitri Sivanich | 074b851 | 2012-02-08 12:39:07 -0800 | [diff] [blame] | 7714 | max = min(max, 0x80000000ULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | |
Tim Bird | 31fe62b | 2012-05-23 13:33:35 +0000 | [diff] [blame] | 7716 | if (numentries < low_limit) |
| 7717 | numentries = low_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | if (numentries > max) |
| 7719 | numentries = max; |
| 7720 | |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 7721 | log2qty = ilog2(numentries); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | |
Pavel Tatashin | 3749a8f | 2017-07-06 15:39:08 -0700 | [diff] [blame] | 7723 | gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7724 | do { |
| 7725 | size = bucketsize << log2qty; |
Pavel Tatashin | ea1f5f3 | 2017-11-15 17:36:27 -0800 | [diff] [blame] | 7726 | if (flags & HASH_EARLY) { |
| 7727 | if (flags & HASH_ZERO) |
Mike Rapoport | 7e1c4e2 | 2018-10-30 15:09:57 -0700 | [diff] [blame] | 7728 | table = memblock_alloc_nopanic(size, |
| 7729 | SMP_CACHE_BYTES); |
Pavel Tatashin | ea1f5f3 | 2017-11-15 17:36:27 -0800 | [diff] [blame] | 7730 | else |
Mike Rapoport | 7e1c4e2 | 2018-10-30 15:09:57 -0700 | [diff] [blame] | 7731 | table = memblock_alloc_raw(size, |
| 7732 | SMP_CACHE_BYTES); |
Pavel Tatashin | ea1f5f3 | 2017-11-15 17:36:27 -0800 | [diff] [blame] | 7733 | } else if (hashdist) { |
Pavel Tatashin | 3749a8f | 2017-07-06 15:39:08 -0700 | [diff] [blame] | 7734 | table = __vmalloc(size, gfp_flags, PAGE_KERNEL); |
Pavel Tatashin | ea1f5f3 | 2017-11-15 17:36:27 -0800 | [diff] [blame] | 7735 | } else { |
Eric Dumazet | 1037b83 | 2007-07-15 23:38:05 -0700 | [diff] [blame] | 7736 | /* |
| 7737 | * If bucketsize is not a power-of-two, we may free |
Mel Gorman | a1dd268 | 2009-06-16 15:32:19 -0700 | [diff] [blame] | 7738 | * some pages at the end of hash table which |
| 7739 | * alloc_pages_exact() automatically does |
Eric Dumazet | 1037b83 | 2007-07-15 23:38:05 -0700 | [diff] [blame] | 7740 | */ |
Catalin Marinas | 264ef8a | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 7741 | if (get_order(size) < MAX_ORDER) { |
Pavel Tatashin | 3749a8f | 2017-07-06 15:39:08 -0700 | [diff] [blame] | 7742 | table = alloc_pages_exact(size, gfp_flags); |
| 7743 | kmemleak_alloc(table, size, 1, gfp_flags); |
Catalin Marinas | 264ef8a | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 7744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | } |
| 7746 | } while (!table && size > PAGE_SIZE && --log2qty); |
| 7747 | |
| 7748 | if (!table) |
| 7749 | panic("Failed to allocate %s hash table\n", tablename); |
| 7750 | |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 7751 | pr_info("%s hash table entries: %ld (order: %d, %lu bytes)\n", |
| 7752 | tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7753 | |
| 7754 | if (_hash_shift) |
| 7755 | *_hash_shift = log2qty; |
| 7756 | if (_hash_mask) |
| 7757 | *_hash_mask = (1 << log2qty) - 1; |
| 7758 | |
| 7759 | return table; |
| 7760 | } |
KAMEZAWA Hiroyuki | a117e66 | 2006-03-27 01:15:25 -0800 | [diff] [blame] | 7761 | |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 7762 | /* |
Minchan Kim | 8093451 | 2012-07-31 16:43:01 -0700 | [diff] [blame] | 7763 | * This function checks whether pageblock includes unmovable pages or not. |
| 7764 | * If @count is not zero, it is okay to include less @count unmovable pages |
| 7765 | * |
Pintu Kumar | b8af294 | 2013-09-11 14:20:34 -0700 | [diff] [blame] | 7766 | * PageLRU check without isolation or lru_lock could race so that |
Yisheng Xie | 0efadf4 | 2017-02-24 14:57:39 -0800 | [diff] [blame] | 7767 | * MIGRATE_MOVABLE block might include unmovable pages. And __PageMovable |
| 7768 | * check without lock_page also may miss some movable non-lru pages at |
| 7769 | * race condition. So you can't expect this function should be exact. |
KAMEZAWA Hiroyuki | a5d76b54 | 2007-10-16 01:26:11 -0700 | [diff] [blame] | 7770 | */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 7771 | bool has_unmovable_pages(struct zone *zone, struct page *page, int count, |
Michal Hocko | d381c54 | 2018-12-28 00:33:56 -0800 | [diff] [blame] | 7772 | int migratetype, int flags) |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7773 | { |
| 7774 | unsigned long pfn, iter, found; |
Michal Nazarewicz | 47118af | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7775 | |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7776 | /* |
Michal Hocko | 15c30bc | 2018-05-25 14:47:42 -0700 | [diff] [blame] | 7777 | * TODO we could make this much more efficient by not checking every |
| 7778 | * page in the range if we know all of them are in MOVABLE_ZONE and |
| 7779 | * that the movable zone guarantees that pages are migratable but |
| 7780 | * the later is not the case right now unfortunatelly. E.g. movablecore |
| 7781 | * can still lead to having bootmem allocations in zone_movable. |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7782 | */ |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7783 | |
Michal Hocko | 4da2ce2 | 2017-11-15 17:33:26 -0800 | [diff] [blame] | 7784 | /* |
| 7785 | * CMA allocations (alloc_contig_range) really need to mark isolate |
| 7786 | * CMA pageblocks even when they are not movable in fact so consider |
| 7787 | * them movable here. |
| 7788 | */ |
| 7789 | if (is_migrate_cma(migratetype) && |
| 7790 | is_migrate_cma(get_pageblock_migratetype(page))) |
| 7791 | return false; |
| 7792 | |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7793 | pfn = page_to_pfn(page); |
| 7794 | for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) { |
| 7795 | unsigned long check = pfn + iter; |
| 7796 | |
Namhyung Kim | 29723fc | 2011-02-25 14:44:25 -0800 | [diff] [blame] | 7797 | if (!pfn_valid_within(check)) |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7798 | continue; |
Namhyung Kim | 29723fc | 2011-02-25 14:44:25 -0800 | [diff] [blame] | 7799 | |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7800 | page = pfn_to_page(check); |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 7801 | |
Michal Hocko | d7ab367 | 2017-11-15 17:33:30 -0800 | [diff] [blame] | 7802 | if (PageReserved(page)) |
Michal Hocko | 15c30bc | 2018-05-25 14:47:42 -0700 | [diff] [blame] | 7803 | goto unmovable; |
Michal Hocko | d7ab367 | 2017-11-15 17:33:30 -0800 | [diff] [blame] | 7804 | |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 7805 | /* |
Michal Hocko | 9d78999 | 2018-11-16 15:08:15 -0800 | [diff] [blame] | 7806 | * If the zone is movable and we have ruled out all reserved |
| 7807 | * pages then it should be reasonably safe to assume the rest |
| 7808 | * is movable. |
| 7809 | */ |
| 7810 | if (zone_idx(zone) == ZONE_MOVABLE) |
| 7811 | continue; |
| 7812 | |
| 7813 | /* |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 7814 | * Hugepages are not in LRU lists, but they're movable. |
| 7815 | * We need not scan over tail pages bacause we don't |
| 7816 | * handle each tail page individually in migration. |
| 7817 | */ |
| 7818 | if (PageHuge(page)) { |
Oscar Salvador | 17e2e7d | 2018-12-21 14:31:00 -0800 | [diff] [blame] | 7819 | struct page *head = compound_head(page); |
| 7820 | unsigned int skip_pages; |
Aneesh Kumar K.V | 464c7ff | 2018-09-04 15:45:59 -0700 | [diff] [blame] | 7821 | |
Oscar Salvador | 17e2e7d | 2018-12-21 14:31:00 -0800 | [diff] [blame] | 7822 | if (!hugepage_migration_supported(page_hstate(head))) |
Aneesh Kumar K.V | 464c7ff | 2018-09-04 15:45:59 -0700 | [diff] [blame] | 7823 | goto unmovable; |
| 7824 | |
Oscar Salvador | 17e2e7d | 2018-12-21 14:31:00 -0800 | [diff] [blame] | 7825 | skip_pages = (1 << compound_order(head)) - (page - head); |
| 7826 | iter += skip_pages - 1; |
Naoya Horiguchi | c8721bb | 2013-09-11 14:22:09 -0700 | [diff] [blame] | 7827 | continue; |
| 7828 | } |
| 7829 | |
Minchan Kim | 97d255c | 2012-07-31 16:42:59 -0700 | [diff] [blame] | 7830 | /* |
| 7831 | * We can't use page_count without pin a page |
| 7832 | * because another CPU can free compound page. |
| 7833 | * This check already skips compound tails of THP |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 7834 | * because their page->_refcount is zero at all time. |
Minchan Kim | 97d255c | 2012-07-31 16:42:59 -0700 | [diff] [blame] | 7835 | */ |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 7836 | if (!page_ref_count(page)) { |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7837 | if (PageBuddy(page)) |
| 7838 | iter += (1 << page_order(page)) - 1; |
| 7839 | continue; |
| 7840 | } |
Minchan Kim | 97d255c | 2012-07-31 16:42:59 -0700 | [diff] [blame] | 7841 | |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 7842 | /* |
| 7843 | * The HWPoisoned page may be not in buddy system, and |
| 7844 | * page_count() is not 0. |
| 7845 | */ |
Michal Hocko | d381c54 | 2018-12-28 00:33:56 -0800 | [diff] [blame] | 7846 | if ((flags & SKIP_HWPOISON) && PageHWPoison(page)) |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 7847 | continue; |
| 7848 | |
Yisheng Xie | 0efadf4 | 2017-02-24 14:57:39 -0800 | [diff] [blame] | 7849 | if (__PageMovable(page)) |
| 7850 | continue; |
| 7851 | |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7852 | if (!PageLRU(page)) |
| 7853 | found++; |
| 7854 | /* |
Johannes Weiner | 6b4f779 | 2014-12-12 16:56:13 -0800 | [diff] [blame] | 7855 | * If there are RECLAIMABLE pages, we need to check |
| 7856 | * it. But now, memory offline itself doesn't call |
| 7857 | * shrink_node_slabs() and it still to be fixed. |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7858 | */ |
| 7859 | /* |
| 7860 | * If the page is not RAM, page_count()should be 0. |
| 7861 | * we don't need more check. This is an _used_ not-movable page. |
| 7862 | * |
| 7863 | * The problematic thing here is PG_reserved pages. PG_reserved |
| 7864 | * is set to both of a memory hole page and a _used_ kernel |
| 7865 | * page at boot. |
| 7866 | */ |
| 7867 | if (found > count) |
Michal Hocko | 15c30bc | 2018-05-25 14:47:42 -0700 | [diff] [blame] | 7868 | goto unmovable; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7869 | } |
Minchan Kim | 8093451 | 2012-07-31 16:43:01 -0700 | [diff] [blame] | 7870 | return false; |
Michal Hocko | 15c30bc | 2018-05-25 14:47:42 -0700 | [diff] [blame] | 7871 | unmovable: |
| 7872 | WARN_ON_ONCE(zone_idx(zone) == ZONE_MOVABLE); |
Michal Hocko | d381c54 | 2018-12-28 00:33:56 -0800 | [diff] [blame] | 7873 | if (flags & REPORT_FAILURE) |
| 7874 | dump_page(pfn_to_page(pfn+iter), "unmovable page"); |
Michal Hocko | 15c30bc | 2018-05-25 14:47:42 -0700 | [diff] [blame] | 7875 | return true; |
KAMEZAWA Hiroyuki | 49ac825 | 2010-10-26 14:21:30 -0700 | [diff] [blame] | 7876 | } |
| 7877 | |
Vlastimil Babka | 080fe20 | 2016-02-05 15:36:41 -0800 | [diff] [blame] | 7878 | #if (defined(CONFIG_MEMORY_ISOLATION) && defined(CONFIG_COMPACTION)) || defined(CONFIG_CMA) |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7879 | |
| 7880 | static unsigned long pfn_max_align_down(unsigned long pfn) |
| 7881 | { |
| 7882 | return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES, |
| 7883 | pageblock_nr_pages) - 1); |
| 7884 | } |
| 7885 | |
| 7886 | static unsigned long pfn_max_align_up(unsigned long pfn) |
| 7887 | { |
| 7888 | return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES, |
| 7889 | pageblock_nr_pages)); |
| 7890 | } |
| 7891 | |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7892 | /* [start, end) must belong to a single zone. */ |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7893 | static int __alloc_contig_migrate_range(struct compact_control *cc, |
| 7894 | unsigned long start, unsigned long end) |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7895 | { |
| 7896 | /* This function is based on compact_zone() from compaction.c. */ |
Minchan Kim | beb51ea | 2012-10-08 16:33:51 -0700 | [diff] [blame] | 7897 | unsigned long nr_reclaimed; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7898 | unsigned long pfn = start; |
| 7899 | unsigned int tries = 0; |
| 7900 | int ret = 0; |
| 7901 | |
Marek Szyprowski | be49a6e | 2012-12-12 13:51:19 -0800 | [diff] [blame] | 7902 | migrate_prep(); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7903 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7904 | while (pfn < end || !list_empty(&cc->migratepages)) { |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7905 | if (fatal_signal_pending(current)) { |
| 7906 | ret = -EINTR; |
| 7907 | break; |
| 7908 | } |
| 7909 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7910 | if (list_empty(&cc->migratepages)) { |
| 7911 | cc->nr_migratepages = 0; |
Vlastimil Babka | edc2ca6 | 2014-10-09 15:27:09 -0700 | [diff] [blame] | 7912 | pfn = isolate_migratepages_range(cc, pfn, end); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7913 | if (!pfn) { |
| 7914 | ret = -EINTR; |
| 7915 | break; |
| 7916 | } |
| 7917 | tries = 0; |
| 7918 | } else if (++tries == 5) { |
| 7919 | ret = ret < 0 ? ret : -EBUSY; |
| 7920 | break; |
| 7921 | } |
| 7922 | |
Minchan Kim | beb51ea | 2012-10-08 16:33:51 -0700 | [diff] [blame] | 7923 | nr_reclaimed = reclaim_clean_pages_from_list(cc->zone, |
| 7924 | &cc->migratepages); |
| 7925 | cc->nr_migratepages -= nr_reclaimed; |
Minchan Kim | 02c6de8 | 2012-10-08 16:31:55 -0700 | [diff] [blame] | 7926 | |
Hugh Dickins | 9c620e2 | 2013-02-22 16:35:14 -0800 | [diff] [blame] | 7927 | ret = migrate_pages(&cc->migratepages, alloc_migrate_target, |
Anshuman Khandual | 3102535 | 2018-04-05 16:22:08 -0700 | [diff] [blame] | 7928 | NULL, 0, cc->mode, MR_CONTIG_RANGE); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7929 | } |
Srinivas Pandruvada | 2a6f512 | 2013-02-22 16:32:09 -0800 | [diff] [blame] | 7930 | if (ret < 0) { |
| 7931 | putback_movable_pages(&cc->migratepages); |
| 7932 | return ret; |
| 7933 | } |
| 7934 | return 0; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7935 | } |
| 7936 | |
| 7937 | /** |
| 7938 | * alloc_contig_range() -- tries to allocate given range of pages |
| 7939 | * @start: start PFN to allocate |
| 7940 | * @end: one-past-the-last PFN to allocate |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 7941 | * @migratetype: migratetype of the underlaying pageblocks (either |
| 7942 | * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks |
| 7943 | * in range must have the same migratetype and it must |
| 7944 | * be either of the two. |
Lucas Stach | ca96b62 | 2017-02-24 14:58:37 -0800 | [diff] [blame] | 7945 | * @gfp_mask: GFP mask to use during compaction |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7946 | * |
| 7947 | * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 7948 | * aligned. The PFN range must belong to a single zone. |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7949 | * |
Mike Kravetz | 2c7452a | 2018-04-05 16:25:26 -0700 | [diff] [blame] | 7950 | * The first thing this routine does is attempt to MIGRATE_ISOLATE all |
| 7951 | * pageblocks in the range. Once isolated, the pageblocks should not |
| 7952 | * be modified by others. |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7953 | * |
| 7954 | * Returns zero on success or negative error code. On success all |
| 7955 | * pages which PFN is in [start, end) are allocated for the caller and |
| 7956 | * need to be freed with free_contig_range(). |
| 7957 | */ |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 7958 | int alloc_contig_range(unsigned long start, unsigned long end, |
Lucas Stach | ca96b62 | 2017-02-24 14:58:37 -0800 | [diff] [blame] | 7959 | unsigned migratetype, gfp_t gfp_mask) |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7960 | { |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7961 | unsigned long outer_start, outer_end; |
Kirill A. Shutemov | d00181b | 2015-11-06 16:29:57 -0800 | [diff] [blame] | 7962 | unsigned int order; |
| 7963 | int ret = 0; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7964 | |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7965 | struct compact_control cc = { |
| 7966 | .nr_migratepages = 0, |
| 7967 | .order = -1, |
| 7968 | .zone = page_zone(pfn_to_page(start)), |
David Rientjes | e0b9dae | 2014-06-04 16:08:28 -0700 | [diff] [blame] | 7969 | .mode = MIGRATE_SYNC, |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7970 | .ignore_skip_hint = true, |
Vlastimil Babka | 2583d67 | 2017-11-17 15:26:38 -0800 | [diff] [blame] | 7971 | .no_set_skip_hint = true, |
Michal Hocko | 7dea19f | 2017-05-03 14:53:15 -0700 | [diff] [blame] | 7972 | .gfp_mask = current_gfp_context(gfp_mask), |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 7973 | }; |
| 7974 | INIT_LIST_HEAD(&cc.migratepages); |
| 7975 | |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 7976 | /* |
| 7977 | * What we do here is we mark all pageblocks in range as |
| 7978 | * MIGRATE_ISOLATE. Because pageblock and max order pages may |
| 7979 | * have different sizes, and due to the way page allocator |
| 7980 | * work, we align the range to biggest of the two pages so |
| 7981 | * that page allocator won't try to merge buddies from |
| 7982 | * different pageblocks and change MIGRATE_ISOLATE to some |
| 7983 | * other migration type. |
| 7984 | * |
| 7985 | * Once the pageblocks are marked as MIGRATE_ISOLATE, we |
| 7986 | * migrate the pages from an unaligned range (ie. pages that |
| 7987 | * we are interested in). This will put all the pages in |
| 7988 | * range back to page allocator as MIGRATE_ISOLATE. |
| 7989 | * |
| 7990 | * When this is done, we take the pages in range from page |
| 7991 | * allocator removing them from the buddy system. This way |
| 7992 | * page allocator will never consider using them. |
| 7993 | * |
| 7994 | * This lets us mark the pageblocks back as |
| 7995 | * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the |
| 7996 | * aligned range but not in the unaligned, original range are |
| 7997 | * put back to page allocator so that buddy can use them. |
| 7998 | */ |
| 7999 | |
| 8000 | ret = start_isolate_page_range(pfn_max_align_down(start), |
Michal Hocko | d381c54 | 2018-12-28 00:33:56 -0800 | [diff] [blame] | 8001 | pfn_max_align_up(end), migratetype, 0); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8002 | if (ret) |
Bob Liu | 86a595f | 2012-10-25 13:37:56 -0700 | [diff] [blame] | 8003 | return ret; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8004 | |
Joonsoo Kim | 8ef5849 | 2016-01-14 15:18:45 -0800 | [diff] [blame] | 8005 | /* |
| 8006 | * In case of -EBUSY, we'd like to know which page causes problem. |
Mike Kravetz | 63cd448 | 2017-11-29 16:10:01 -0800 | [diff] [blame] | 8007 | * So, just fall through. test_pages_isolated() has a tracepoint |
| 8008 | * which will report the busy page. |
| 8009 | * |
| 8010 | * It is possible that busy pages could become available before |
| 8011 | * the call to test_pages_isolated, and the range will actually be |
| 8012 | * allocated. So, if we fall through be sure to clear ret so that |
| 8013 | * -EBUSY is not accidentally used or returned to caller. |
Joonsoo Kim | 8ef5849 | 2016-01-14 15:18:45 -0800 | [diff] [blame] | 8014 | */ |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 8015 | ret = __alloc_contig_migrate_range(&cc, start, end); |
Joonsoo Kim | 8ef5849 | 2016-01-14 15:18:45 -0800 | [diff] [blame] | 8016 | if (ret && ret != -EBUSY) |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8017 | goto done; |
Mike Kravetz | 63cd448 | 2017-11-29 16:10:01 -0800 | [diff] [blame] | 8018 | ret =0; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8019 | |
| 8020 | /* |
| 8021 | * Pages from [start, end) are within a MAX_ORDER_NR_PAGES |
| 8022 | * aligned blocks that are marked as MIGRATE_ISOLATE. What's |
| 8023 | * more, all pages in [start, end) are free in page allocator. |
| 8024 | * What we are going to do is to allocate all pages from |
| 8025 | * [start, end) (that is remove them from page allocator). |
| 8026 | * |
| 8027 | * The only problem is that pages at the beginning and at the |
| 8028 | * end of interesting range may be not aligned with pages that |
| 8029 | * page allocator holds, ie. they can be part of higher order |
| 8030 | * pages. Because of this, we reserve the bigger range and |
| 8031 | * once this is done free the pages we are not interested in. |
| 8032 | * |
| 8033 | * We don't have to hold zone->lock here because the pages are |
| 8034 | * isolated thus they won't get removed from buddy. |
| 8035 | */ |
| 8036 | |
| 8037 | lru_add_drain_all(); |
Vlastimil Babka | 510f550 | 2014-12-10 15:43:07 -0800 | [diff] [blame] | 8038 | drain_all_pages(cc.zone); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8039 | |
| 8040 | order = 0; |
| 8041 | outer_start = start; |
| 8042 | while (!PageBuddy(pfn_to_page(outer_start))) { |
| 8043 | if (++order >= MAX_ORDER) { |
Joonsoo Kim | 8ef5849 | 2016-01-14 15:18:45 -0800 | [diff] [blame] | 8044 | outer_start = start; |
| 8045 | break; |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8046 | } |
| 8047 | outer_start &= ~0UL << order; |
| 8048 | } |
| 8049 | |
Joonsoo Kim | 8ef5849 | 2016-01-14 15:18:45 -0800 | [diff] [blame] | 8050 | if (outer_start != start) { |
| 8051 | order = page_order(pfn_to_page(outer_start)); |
| 8052 | |
| 8053 | /* |
| 8054 | * outer_start page could be small order buddy page and |
| 8055 | * it doesn't include start page. Adjust outer_start |
| 8056 | * in this case to report failed page properly |
| 8057 | * on tracepoint in test_pages_isolated() |
| 8058 | */ |
| 8059 | if (outer_start + (1UL << order) <= start) |
| 8060 | outer_start = start; |
| 8061 | } |
| 8062 | |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8063 | /* Make sure the range is really isolated. */ |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 8064 | if (test_pages_isolated(outer_start, end, false)) { |
Jonathan Toppins | 75dddef | 2017-08-10 15:23:35 -0700 | [diff] [blame] | 8065 | pr_info_ratelimited("%s: [%lx, %lx) PFNs busy\n", |
Michal Nazarewicz | dae803e | 2014-11-13 15:19:27 -0800 | [diff] [blame] | 8066 | __func__, outer_start, end); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8067 | ret = -EBUSY; |
| 8068 | goto done; |
| 8069 | } |
| 8070 | |
Marek Szyprowski | 49f223a | 2012-01-25 12:49:24 +0100 | [diff] [blame] | 8071 | /* Grab isolated pages from freelists. */ |
Mel Gorman | bb13ffe | 2012-10-08 16:32:41 -0700 | [diff] [blame] | 8072 | outer_end = isolate_freepages_range(&cc, outer_start, end); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8073 | if (!outer_end) { |
| 8074 | ret = -EBUSY; |
| 8075 | goto done; |
| 8076 | } |
| 8077 | |
| 8078 | /* Free head and tail (if any) */ |
| 8079 | if (start != outer_start) |
| 8080 | free_contig_range(outer_start, start - outer_start); |
| 8081 | if (end != outer_end) |
| 8082 | free_contig_range(end, outer_end - end); |
| 8083 | |
| 8084 | done: |
| 8085 | undo_isolate_page_range(pfn_max_align_down(start), |
Michal Nazarewicz | 0815f3d | 2012-04-03 15:06:15 +0200 | [diff] [blame] | 8086 | pfn_max_align_up(end), migratetype); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8087 | return ret; |
| 8088 | } |
| 8089 | |
| 8090 | void free_contig_range(unsigned long pfn, unsigned nr_pages) |
| 8091 | { |
Marek Szyprowski | bcc2b02 | 2012-12-20 15:05:18 -0800 | [diff] [blame] | 8092 | unsigned int count = 0; |
| 8093 | |
| 8094 | for (; nr_pages--; pfn++) { |
| 8095 | struct page *page = pfn_to_page(pfn); |
| 8096 | |
| 8097 | count += page_count(page) != 1; |
| 8098 | __free_page(page); |
| 8099 | } |
| 8100 | WARN(count != 0, "%d pages are still in use!\n", count); |
Michal Nazarewicz | 041d3a8 | 2011-12-29 13:09:50 +0100 | [diff] [blame] | 8101 | } |
| 8102 | #endif |
| 8103 | |
Joonsoo Kim | d883c6c | 2018-05-23 10:18:21 +0900 | [diff] [blame] | 8104 | #ifdef CONFIG_MEMORY_HOTPLUG |
Cody P Schafer | 0a647f3 | 2013-07-03 15:01:33 -0700 | [diff] [blame] | 8105 | /* |
| 8106 | * The zone indicated has a new number of managed_pages; batch sizes and percpu |
| 8107 | * page high values need to be recalulated. |
| 8108 | */ |
Jiang Liu | 4ed7e02 | 2012-07-31 16:43:35 -0700 | [diff] [blame] | 8109 | void __meminit zone_pcp_update(struct zone *zone) |
| 8110 | { |
Cody P Schafer | 0a647f3 | 2013-07-03 15:01:33 -0700 | [diff] [blame] | 8111 | unsigned cpu; |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 8112 | mutex_lock(&pcp_batch_high_lock); |
Cody P Schafer | 0a647f3 | 2013-07-03 15:01:33 -0700 | [diff] [blame] | 8113 | for_each_possible_cpu(cpu) |
Cody P Schafer | 169f6c1 | 2013-07-03 15:01:41 -0700 | [diff] [blame] | 8114 | pageset_set_high_and_batch(zone, |
| 8115 | per_cpu_ptr(zone->pageset, cpu)); |
Cody P Schafer | c8e251f | 2013-07-03 15:01:29 -0700 | [diff] [blame] | 8116 | mutex_unlock(&pcp_batch_high_lock); |
Jiang Liu | 4ed7e02 | 2012-07-31 16:43:35 -0700 | [diff] [blame] | 8117 | } |
| 8118 | #endif |
| 8119 | |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 8120 | void zone_pcp_reset(struct zone *zone) |
| 8121 | { |
| 8122 | unsigned long flags; |
Minchan Kim | 5a88381 | 2012-10-08 16:33:39 -0700 | [diff] [blame] | 8123 | int cpu; |
| 8124 | struct per_cpu_pageset *pset; |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 8125 | |
| 8126 | /* avoid races with drain_pages() */ |
| 8127 | local_irq_save(flags); |
| 8128 | if (zone->pageset != &boot_pageset) { |
Minchan Kim | 5a88381 | 2012-10-08 16:33:39 -0700 | [diff] [blame] | 8129 | for_each_online_cpu(cpu) { |
| 8130 | pset = per_cpu_ptr(zone->pageset, cpu); |
| 8131 | drain_zonestat(zone, pset); |
| 8132 | } |
Jiang Liu | 340175b | 2012-07-31 16:43:32 -0700 | [diff] [blame] | 8133 | free_percpu(zone->pageset); |
| 8134 | zone->pageset = &boot_pageset; |
| 8135 | } |
| 8136 | local_irq_restore(flags); |
| 8137 | } |
| 8138 | |
Wen Congyang | 6dcd73d | 2012-12-11 16:01:01 -0800 | [diff] [blame] | 8139 | #ifdef CONFIG_MEMORY_HOTREMOVE |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8140 | /* |
Joonsoo Kim | b9eb631 | 2016-05-19 17:12:06 -0700 | [diff] [blame] | 8141 | * All pages in the range must be in a single zone and isolated |
| 8142 | * before calling this. |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8143 | */ |
| 8144 | void |
| 8145 | __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
| 8146 | { |
| 8147 | struct page *page; |
| 8148 | struct zone *zone; |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 8149 | unsigned int order, i; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8150 | unsigned long pfn; |
| 8151 | unsigned long flags; |
| 8152 | /* find the first valid pfn */ |
| 8153 | for (pfn = start_pfn; pfn < end_pfn; pfn++) |
| 8154 | if (pfn_valid(pfn)) |
| 8155 | break; |
| 8156 | if (pfn == end_pfn) |
| 8157 | return; |
Michal Hocko | 2d070ea | 2017-07-06 15:37:56 -0700 | [diff] [blame] | 8158 | offline_mem_sections(pfn, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8159 | zone = page_zone(pfn_to_page(pfn)); |
| 8160 | spin_lock_irqsave(&zone->lock, flags); |
| 8161 | pfn = start_pfn; |
| 8162 | while (pfn < end_pfn) { |
| 8163 | if (!pfn_valid(pfn)) { |
| 8164 | pfn++; |
| 8165 | continue; |
| 8166 | } |
| 8167 | page = pfn_to_page(pfn); |
Wen Congyang | b023f46 | 2012-12-11 16:00:45 -0800 | [diff] [blame] | 8168 | /* |
| 8169 | * The HWPoisoned page may be not in buddy system, and |
| 8170 | * page_count() is not 0. |
| 8171 | */ |
| 8172 | if (unlikely(!PageBuddy(page) && PageHWPoison(page))) { |
| 8173 | pfn++; |
| 8174 | SetPageReserved(page); |
| 8175 | continue; |
| 8176 | } |
| 8177 | |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8178 | BUG_ON(page_count(page)); |
| 8179 | BUG_ON(!PageBuddy(page)); |
| 8180 | order = page_order(page); |
| 8181 | #ifdef CONFIG_DEBUG_VM |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 8182 | pr_info("remove from free list %lx %d %lx\n", |
| 8183 | pfn, 1 << order, end_pfn); |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8184 | #endif |
| 8185 | list_del(&page->lru); |
| 8186 | rmv_page_order(page); |
| 8187 | zone->free_area[order].nr_free--; |
KAMEZAWA Hiroyuki | 0c0e619 | 2007-10-16 01:26:12 -0700 | [diff] [blame] | 8188 | for (i = 0; i < (1 << order); i++) |
| 8189 | SetPageReserved((page+i)); |
| 8190 | pfn += (1 << order); |
| 8191 | } |
| 8192 | spin_unlock_irqrestore(&zone->lock, flags); |
| 8193 | } |
| 8194 | #endif |
Wu Fengguang | 8d22ba1 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 8195 | |
Wu Fengguang | 8d22ba1 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 8196 | bool is_free_buddy_page(struct page *page) |
| 8197 | { |
| 8198 | struct zone *zone = page_zone(page); |
| 8199 | unsigned long pfn = page_to_pfn(page); |
| 8200 | unsigned long flags; |
Mel Gorman | 7aeb09f | 2014-06-04 16:10:21 -0700 | [diff] [blame] | 8201 | unsigned int order; |
Wu Fengguang | 8d22ba1 | 2009-12-16 12:19:58 +0100 | [diff] [blame] | 8202 | |
| 8203 | spin_lock_irqsave(&zone->lock, flags); |
| 8204 | for (order = 0; order < MAX_ORDER; order++) { |
| 8205 | struct page *page_head = page - (pfn & ((1 << order) - 1)); |
| 8206 | |
| 8207 | if (PageBuddy(page_head) && page_order(page_head) >= order) |
| 8208 | break; |
| 8209 | } |
| 8210 | spin_unlock_irqrestore(&zone->lock, flags); |
| 8211 | |
| 8212 | return order < MAX_ORDER; |
| 8213 | } |
Naoya Horiguchi | d4ae991 | 2018-08-23 17:00:42 -0700 | [diff] [blame] | 8214 | |
| 8215 | #ifdef CONFIG_MEMORY_FAILURE |
| 8216 | /* |
| 8217 | * Set PG_hwpoison flag if a given page is confirmed to be a free page. This |
| 8218 | * test is performed under the zone lock to prevent a race against page |
| 8219 | * allocation. |
| 8220 | */ |
| 8221 | bool set_hwpoison_free_buddy_page(struct page *page) |
| 8222 | { |
| 8223 | struct zone *zone = page_zone(page); |
| 8224 | unsigned long pfn = page_to_pfn(page); |
| 8225 | unsigned long flags; |
| 8226 | unsigned int order; |
| 8227 | bool hwpoisoned = false; |
| 8228 | |
| 8229 | spin_lock_irqsave(&zone->lock, flags); |
| 8230 | for (order = 0; order < MAX_ORDER; order++) { |
| 8231 | struct page *page_head = page - (pfn & ((1 << order) - 1)); |
| 8232 | |
| 8233 | if (PageBuddy(page_head) && page_order(page_head) >= order) { |
| 8234 | if (!TestSetPageHWPoison(page)) |
| 8235 | hwpoisoned = true; |
| 8236 | break; |
| 8237 | } |
| 8238 | } |
| 8239 | spin_unlock_irqrestore(&zone->lock, flags); |
| 8240 | |
| 8241 | return hwpoisoned; |
| 8242 | } |
| 8243 | #endif |