]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/mm/page_alloc.c | |
4 | * | |
5 | * Manages the free list, the system allocates free pages here. | |
6 | * Note that kmalloc() lives in slab.c | |
7 | * | |
8 | * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds | |
9 | * Swap reorganised 29.12.95, Stephen Tweedie | |
10 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 | |
11 | * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999 | |
12 | * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999 | |
13 | * Zone balancing, Kanoj Sarcar, SGI, Jan 2000 | |
14 | * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002 | |
15 | * (lots of bits borrowed from Ingo Molnar & Andrew Morton) | |
16 | */ | |
17 | ||
1da177e4 LT |
18 | #include <linux/stddef.h> |
19 | #include <linux/mm.h> | |
ca79b0c2 | 20 | #include <linux/highmem.h> |
1da177e4 | 21 | #include <linux/swap.h> |
bf181c58 | 22 | #include <linux/swapops.h> |
1da177e4 LT |
23 | #include <linux/interrupt.h> |
24 | #include <linux/pagemap.h> | |
10ed273f | 25 | #include <linux/jiffies.h> |
edbe7d23 | 26 | #include <linux/memblock.h> |
1da177e4 | 27 | #include <linux/compiler.h> |
9f158333 | 28 | #include <linux/kernel.h> |
b8c73fc2 | 29 | #include <linux/kasan.h> |
1da177e4 LT |
30 | #include <linux/module.h> |
31 | #include <linux/suspend.h> | |
32 | #include <linux/pagevec.h> | |
33 | #include <linux/blkdev.h> | |
34 | #include <linux/slab.h> | |
a238ab5b | 35 | #include <linux/ratelimit.h> |
5a3135c2 | 36 | #include <linux/oom.h> |
1da177e4 LT |
37 | #include <linux/topology.h> |
38 | #include <linux/sysctl.h> | |
39 | #include <linux/cpu.h> | |
40 | #include <linux/cpuset.h> | |
bdc8cb98 | 41 | #include <linux/memory_hotplug.h> |
1da177e4 LT |
42 | #include <linux/nodemask.h> |
43 | #include <linux/vmalloc.h> | |
a6cccdc3 | 44 | #include <linux/vmstat.h> |
4be38e35 | 45 | #include <linux/mempolicy.h> |
4b94ffdc | 46 | #include <linux/memremap.h> |
6811378e | 47 | #include <linux/stop_machine.h> |
97500a4a | 48 | #include <linux/random.h> |
c713216d MG |
49 | #include <linux/sort.h> |
50 | #include <linux/pfn.h> | |
3fcfab16 | 51 | #include <linux/backing-dev.h> |
933e312e | 52 | #include <linux/fault-inject.h> |
a5d76b54 | 53 | #include <linux/page-isolation.h> |
3ac7fe5a | 54 | #include <linux/debugobjects.h> |
dbb1f81c | 55 | #include <linux/kmemleak.h> |
56de7263 | 56 | #include <linux/compaction.h> |
0d3d062a | 57 | #include <trace/events/kmem.h> |
d379f01d | 58 | #include <trace/events/oom.h> |
268bb0ce | 59 | #include <linux/prefetch.h> |
6e543d57 | 60 | #include <linux/mm_inline.h> |
f920e413 | 61 | #include <linux/mmu_notifier.h> |
041d3a8c | 62 | #include <linux/migrate.h> |
949f7ec5 | 63 | #include <linux/hugetlb.h> |
8bd75c77 | 64 | #include <linux/sched/rt.h> |
5b3cc15a | 65 | #include <linux/sched/mm.h> |
48c96a36 | 66 | #include <linux/page_owner.h> |
df4e817b | 67 | #include <linux/page_table_check.h> |
0e1cc95b | 68 | #include <linux/kthread.h> |
4949148a | 69 | #include <linux/memcontrol.h> |
42c269c8 | 70 | #include <linux/ftrace.h> |
d92a8cfc | 71 | #include <linux/lockdep.h> |
556b969a | 72 | #include <linux/nmi.h> |
eb414681 | 73 | #include <linux/psi.h> |
e4443149 | 74 | #include <linux/padata.h> |
4aab2be0 | 75 | #include <linux/khugepaged.h> |
ba8f3587 | 76 | #include <linux/buffer_head.h> |
5bf18281 | 77 | #include <linux/delayacct.h> |
7ee3d4e8 | 78 | #include <asm/sections.h> |
1da177e4 | 79 | #include <asm/tlbflush.h> |
ac924c60 | 80 | #include <asm/div64.h> |
1da177e4 | 81 | #include "internal.h" |
e900a918 | 82 | #include "shuffle.h" |
36e66c55 | 83 | #include "page_reporting.h" |
014bb1de | 84 | #include "swap.h" |
1da177e4 | 85 | |
f04a5d5d DH |
86 | /* Free Page Internal flags: for internal, non-pcp variants of free_pages(). */ |
87 | typedef int __bitwise fpi_t; | |
88 | ||
89 | /* No special request */ | |
90 | #define FPI_NONE ((__force fpi_t)0) | |
91 | ||
92 | /* | |
93 | * Skip free page reporting notification for the (possibly merged) page. | |
94 | * This does not hinder free page reporting from grabbing the page, | |
95 | * reporting it and marking it "reported" - it only skips notifying | |
96 | * the free page reporting infrastructure about a newly freed page. For | |
97 | * example, used when temporarily pulling a page from a freelist and | |
98 | * putting it back unmodified. | |
99 | */ | |
100 | #define FPI_SKIP_REPORT_NOTIFY ((__force fpi_t)BIT(0)) | |
101 | ||
47b6a24a DH |
102 | /* |
103 | * Place the (possibly merged) page to the tail of the freelist. Will ignore | |
104 | * page shuffling (relevant code - e.g., memory onlining - is expected to | |
105 | * shuffle the whole zone). | |
106 | * | |
107 | * Note: No code should rely on this flag for correctness - it's purely | |
108 | * to allow for optimizations when handing back either fresh pages | |
109 | * (memory onlining) or untouched pages (page isolation, free page | |
110 | * reporting). | |
111 | */ | |
112 | #define FPI_TO_TAIL ((__force fpi_t)BIT(1)) | |
113 | ||
2c335680 AK |
114 | /* |
115 | * Don't poison memory with KASAN (only for the tag-based modes). | |
116 | * During boot, all non-reserved memblock memory is exposed to page_alloc. | |
117 | * Poisoning all that memory lengthens boot time, especially on systems with | |
118 | * large amount of RAM. This flag is used to skip that poisoning. | |
119 | * This is only done for the tag-based KASAN modes, as those are able to | |
120 | * detect memory corruptions with the memory tags assigned by default. | |
121 | * All memory allocated normally after boot gets poisoned as usual. | |
122 | */ | |
123 | #define FPI_SKIP_KASAN_POISON ((__force fpi_t)BIT(2)) | |
124 | ||
c8e251fa CS |
125 | /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ |
126 | static DEFINE_MUTEX(pcp_batch_high_lock); | |
74f44822 | 127 | #define MIN_PERCPU_PAGELIST_HIGH_FRACTION (8) |
c8e251fa | 128 | |
dbbee9d5 MG |
129 | struct pagesets { |
130 | local_lock_t lock; | |
dbbee9d5 | 131 | }; |
273ba85b | 132 | static DEFINE_PER_CPU(struct pagesets, pagesets) = { |
dbbee9d5 MG |
133 | .lock = INIT_LOCAL_LOCK(lock), |
134 | }; | |
c8e251fa | 135 | |
72812019 LS |
136 | #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID |
137 | DEFINE_PER_CPU(int, numa_node); | |
138 | EXPORT_PER_CPU_SYMBOL(numa_node); | |
139 | #endif | |
140 | ||
4518085e KW |
141 | DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key); |
142 | ||
7aac7898 LS |
143 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
144 | /* | |
145 | * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly. | |
146 | * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined. | |
147 | * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem() | |
148 | * defined in <linux/topology.h>. | |
149 | */ | |
150 | DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */ | |
151 | EXPORT_PER_CPU_SYMBOL(_numa_mem_); | |
152 | #endif | |
153 | ||
bd233f53 | 154 | /* work_structs for global per-cpu drains */ |
d9367bd0 WY |
155 | struct pcpu_drain { |
156 | struct zone *zone; | |
157 | struct work_struct work; | |
158 | }; | |
8b885f53 JY |
159 | static DEFINE_MUTEX(pcpu_drain_mutex); |
160 | static DEFINE_PER_CPU(struct pcpu_drain, pcpu_drain); | |
bd233f53 | 161 | |
38addce8 | 162 | #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY |
58bea414 | 163 | volatile unsigned long latent_entropy __latent_entropy; |
38addce8 ER |
164 | EXPORT_SYMBOL(latent_entropy); |
165 | #endif | |
166 | ||
1da177e4 | 167 | /* |
13808910 | 168 | * Array of node states. |
1da177e4 | 169 | */ |
13808910 CL |
170 | nodemask_t node_states[NR_NODE_STATES] __read_mostly = { |
171 | [N_POSSIBLE] = NODE_MASK_ALL, | |
172 | [N_ONLINE] = { { [0] = 1UL } }, | |
173 | #ifndef CONFIG_NUMA | |
174 | [N_NORMAL_MEMORY] = { { [0] = 1UL } }, | |
175 | #ifdef CONFIG_HIGHMEM | |
176 | [N_HIGH_MEMORY] = { { [0] = 1UL } }, | |
20b2f52b | 177 | #endif |
20b2f52b | 178 | [N_MEMORY] = { { [0] = 1UL } }, |
13808910 CL |
179 | [N_CPU] = { { [0] = 1UL } }, |
180 | #endif /* NUMA */ | |
181 | }; | |
182 | EXPORT_SYMBOL(node_states); | |
183 | ||
ca79b0c2 AK |
184 | atomic_long_t _totalram_pages __read_mostly; |
185 | EXPORT_SYMBOL(_totalram_pages); | |
cb45b0e9 | 186 | unsigned long totalreserve_pages __read_mostly; |
e48322ab | 187 | unsigned long totalcma_pages __read_mostly; |
ab8fabd4 | 188 | |
74f44822 | 189 | int percpu_pagelist_high_fraction; |
dcce284a | 190 | gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; |
51cba1eb | 191 | DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_ALLOC_DEFAULT_ON, init_on_alloc); |
6471384a AP |
192 | EXPORT_SYMBOL(init_on_alloc); |
193 | ||
51cba1eb | 194 | DEFINE_STATIC_KEY_MAYBE(CONFIG_INIT_ON_FREE_DEFAULT_ON, init_on_free); |
6471384a AP |
195 | EXPORT_SYMBOL(init_on_free); |
196 | ||
04013513 VB |
197 | static bool _init_on_alloc_enabled_early __read_mostly |
198 | = IS_ENABLED(CONFIG_INIT_ON_ALLOC_DEFAULT_ON); | |
6471384a AP |
199 | static int __init early_init_on_alloc(char *buf) |
200 | { | |
6471384a | 201 | |
04013513 | 202 | return kstrtobool(buf, &_init_on_alloc_enabled_early); |
6471384a AP |
203 | } |
204 | early_param("init_on_alloc", early_init_on_alloc); | |
205 | ||
04013513 VB |
206 | static bool _init_on_free_enabled_early __read_mostly |
207 | = IS_ENABLED(CONFIG_INIT_ON_FREE_DEFAULT_ON); | |
6471384a AP |
208 | static int __init early_init_on_free(char *buf) |
209 | { | |
04013513 | 210 | return kstrtobool(buf, &_init_on_free_enabled_early); |
6471384a AP |
211 | } |
212 | early_param("init_on_free", early_init_on_free); | |
1da177e4 | 213 | |
bb14c2c7 VB |
214 | /* |
215 | * A cached value of the page's pageblock's migratetype, used when the page is | |
216 | * put on a pcplist. Used to avoid the pageblock migratetype lookup when | |
217 | * freeing from pcplists in most cases, at the cost of possibly becoming stale. | |
218 | * Also the migratetype set in the page does not necessarily match the pcplist | |
219 | * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any | |
220 | * other index - this ensures that it will be put on the correct CMA freelist. | |
221 | */ | |
222 | static inline int get_pcppage_migratetype(struct page *page) | |
223 | { | |
224 | return page->index; | |
225 | } | |
226 | ||
227 | static inline void set_pcppage_migratetype(struct page *page, int migratetype) | |
228 | { | |
229 | page->index = migratetype; | |
230 | } | |
231 | ||
452aa699 RW |
232 | #ifdef CONFIG_PM_SLEEP |
233 | /* | |
234 | * The following functions are used by the suspend/hibernate code to temporarily | |
235 | * change gfp_allowed_mask in order to avoid using I/O during memory allocations | |
236 | * while devices are suspended. To avoid races with the suspend/hibernate code, | |
55f2503c PL |
237 | * they should always be called with system_transition_mutex held |
238 | * (gfp_allowed_mask also should only be modified with system_transition_mutex | |
239 | * held, unless the suspend/hibernate code is guaranteed not to run in parallel | |
240 | * with that modification). | |
452aa699 | 241 | */ |
c9e664f1 RW |
242 | |
243 | static gfp_t saved_gfp_mask; | |
244 | ||
245 | void pm_restore_gfp_mask(void) | |
452aa699 | 246 | { |
55f2503c | 247 | WARN_ON(!mutex_is_locked(&system_transition_mutex)); |
c9e664f1 RW |
248 | if (saved_gfp_mask) { |
249 | gfp_allowed_mask = saved_gfp_mask; | |
250 | saved_gfp_mask = 0; | |
251 | } | |
452aa699 RW |
252 | } |
253 | ||
c9e664f1 | 254 | void pm_restrict_gfp_mask(void) |
452aa699 | 255 | { |
55f2503c | 256 | WARN_ON(!mutex_is_locked(&system_transition_mutex)); |
c9e664f1 RW |
257 | WARN_ON(saved_gfp_mask); |
258 | saved_gfp_mask = gfp_allowed_mask; | |
d0164adc | 259 | gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS); |
452aa699 | 260 | } |
f90ac398 MG |
261 | |
262 | bool pm_suspended_storage(void) | |
263 | { | |
d0164adc | 264 | if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
f90ac398 MG |
265 | return false; |
266 | return true; | |
267 | } | |
452aa699 RW |
268 | #endif /* CONFIG_PM_SLEEP */ |
269 | ||
d9c23400 | 270 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
d00181b9 | 271 | unsigned int pageblock_order __read_mostly; |
d9c23400 MG |
272 | #endif |
273 | ||
7fef431b DH |
274 | static void __free_pages_ok(struct page *page, unsigned int order, |
275 | fpi_t fpi_flags); | |
a226f6c8 | 276 | |
1da177e4 LT |
277 | /* |
278 | * results with 256, 32 in the lowmem_reserve sysctl: | |
279 | * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high) | |
280 | * 1G machine -> (16M dma, 784M normal, 224M high) | |
281 | * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA | |
282 | * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL | |
84109e15 | 283 | * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA |
a2f1b424 AK |
284 | * |
285 | * TBD: should special case ZONE_DMA32 machines here - in those we normally | |
286 | * don't need any ZONE_NORMAL reservation | |
1da177e4 | 287 | */ |
d3cda233 | 288 | int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES] = { |
4b51d669 | 289 | #ifdef CONFIG_ZONE_DMA |
d3cda233 | 290 | [ZONE_DMA] = 256, |
4b51d669 | 291 | #endif |
fb0e7942 | 292 | #ifdef CONFIG_ZONE_DMA32 |
d3cda233 | 293 | [ZONE_DMA32] = 256, |
fb0e7942 | 294 | #endif |
d3cda233 | 295 | [ZONE_NORMAL] = 32, |
e53ef38d | 296 | #ifdef CONFIG_HIGHMEM |
d3cda233 | 297 | [ZONE_HIGHMEM] = 0, |
e53ef38d | 298 | #endif |
d3cda233 | 299 | [ZONE_MOVABLE] = 0, |
2f1b6248 | 300 | }; |
1da177e4 | 301 | |
15ad7cdc | 302 | static char * const zone_names[MAX_NR_ZONES] = { |
4b51d669 | 303 | #ifdef CONFIG_ZONE_DMA |
2f1b6248 | 304 | "DMA", |
4b51d669 | 305 | #endif |
fb0e7942 | 306 | #ifdef CONFIG_ZONE_DMA32 |
2f1b6248 | 307 | "DMA32", |
fb0e7942 | 308 | #endif |
2f1b6248 | 309 | "Normal", |
e53ef38d | 310 | #ifdef CONFIG_HIGHMEM |
2a1e274a | 311 | "HighMem", |
e53ef38d | 312 | #endif |
2a1e274a | 313 | "Movable", |
033fbae9 DW |
314 | #ifdef CONFIG_ZONE_DEVICE |
315 | "Device", | |
316 | #endif | |
2f1b6248 CL |
317 | }; |
318 | ||
c999fbd3 | 319 | const char * const migratetype_names[MIGRATE_TYPES] = { |
60f30350 VB |
320 | "Unmovable", |
321 | "Movable", | |
322 | "Reclaimable", | |
323 | "HighAtomic", | |
324 | #ifdef CONFIG_CMA | |
325 | "CMA", | |
326 | #endif | |
327 | #ifdef CONFIG_MEMORY_ISOLATION | |
328 | "Isolate", | |
329 | #endif | |
330 | }; | |
331 | ||
ae70eddd AK |
332 | compound_page_dtor * const compound_page_dtors[NR_COMPOUND_DTORS] = { |
333 | [NULL_COMPOUND_DTOR] = NULL, | |
334 | [COMPOUND_PAGE_DTOR] = free_compound_page, | |
f1e61557 | 335 | #ifdef CONFIG_HUGETLB_PAGE |
ae70eddd | 336 | [HUGETLB_PAGE_DTOR] = free_huge_page, |
f1e61557 | 337 | #endif |
9a982250 | 338 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
ae70eddd | 339 | [TRANSHUGE_PAGE_DTOR] = free_transhuge_page, |
9a982250 | 340 | #endif |
f1e61557 KS |
341 | }; |
342 | ||
1da177e4 | 343 | int min_free_kbytes = 1024; |
42aa83cb | 344 | int user_min_free_kbytes = -1; |
1c30844d | 345 | int watermark_boost_factor __read_mostly = 15000; |
795ae7a0 | 346 | int watermark_scale_factor = 10; |
1da177e4 | 347 | |
bbe5d993 OS |
348 | static unsigned long nr_kernel_pages __initdata; |
349 | static unsigned long nr_all_pages __initdata; | |
350 | static unsigned long dma_reserve __initdata; | |
1da177e4 | 351 | |
bbe5d993 OS |
352 | static unsigned long arch_zone_lowest_possible_pfn[MAX_NR_ZONES] __initdata; |
353 | static unsigned long arch_zone_highest_possible_pfn[MAX_NR_ZONES] __initdata; | |
7f16f91f | 354 | static unsigned long required_kernelcore __initdata; |
a5c6d650 | 355 | static unsigned long required_kernelcore_percent __initdata; |
7f16f91f | 356 | static unsigned long required_movablecore __initdata; |
a5c6d650 | 357 | static unsigned long required_movablecore_percent __initdata; |
bbe5d993 | 358 | static unsigned long zone_movable_pfn[MAX_NUMNODES] __initdata; |
7f16f91f | 359 | static bool mirrored_kernelcore __meminitdata; |
0ee332c1 TH |
360 | |
361 | /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ | |
362 | int movable_zone; | |
363 | EXPORT_SYMBOL(movable_zone); | |
c713216d | 364 | |
418508c1 | 365 | #if MAX_NUMNODES > 1 |
b9726c26 | 366 | unsigned int nr_node_ids __read_mostly = MAX_NUMNODES; |
ce0725f7 | 367 | unsigned int nr_online_nodes __read_mostly = 1; |
418508c1 | 368 | EXPORT_SYMBOL(nr_node_ids); |
62bc62a8 | 369 | EXPORT_SYMBOL(nr_online_nodes); |
418508c1 MS |
370 | #endif |
371 | ||
9ef9acb0 MG |
372 | int page_group_by_mobility_disabled __read_mostly; |
373 | ||
3a80a7fa | 374 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
3c0c12cc WL |
375 | /* |
376 | * During boot we initialize deferred pages on-demand, as needed, but once | |
377 | * page_alloc_init_late() has finished, the deferred pages are all initialized, | |
378 | * and we can permanently disable that path. | |
379 | */ | |
380 | static DEFINE_STATIC_KEY_TRUE(deferred_pages); | |
381 | ||
94ae8b83 | 382 | static inline bool deferred_pages_enabled(void) |
3c0c12cc | 383 | { |
94ae8b83 | 384 | return static_branch_unlikely(&deferred_pages); |
3c0c12cc WL |
385 | } |
386 | ||
3a80a7fa | 387 | /* Returns true if the struct page for the pfn is uninitialised */ |
0e1cc95b | 388 | static inline bool __meminit early_page_uninitialised(unsigned long pfn) |
3a80a7fa | 389 | { |
ef70b6f4 MG |
390 | int nid = early_pfn_to_nid(pfn); |
391 | ||
392 | if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn) | |
3a80a7fa MG |
393 | return true; |
394 | ||
395 | return false; | |
396 | } | |
397 | ||
398 | /* | |
d3035be4 | 399 | * Returns true when the remaining initialisation should be deferred until |
3a80a7fa MG |
400 | * later in the boot cycle when it can be parallelised. |
401 | */ | |
d3035be4 PT |
402 | static bool __meminit |
403 | defer_init(int nid, unsigned long pfn, unsigned long end_pfn) | |
3a80a7fa | 404 | { |
d3035be4 PT |
405 | static unsigned long prev_end_pfn, nr_initialised; |
406 | ||
407 | /* | |
408 | * prev_end_pfn static that contains the end of previous zone | |
409 | * No need to protect because called very early in boot before smp_init. | |
410 | */ | |
411 | if (prev_end_pfn != end_pfn) { | |
412 | prev_end_pfn = end_pfn; | |
413 | nr_initialised = 0; | |
414 | } | |
415 | ||
3c2c6488 | 416 | /* Always populate low zones for address-constrained allocations */ |
d3035be4 | 417 | if (end_pfn < pgdat_end_pfn(NODE_DATA(nid))) |
3a80a7fa | 418 | return false; |
23b68cfa | 419 | |
dc2da7b4 BH |
420 | if (NODE_DATA(nid)->first_deferred_pfn != ULONG_MAX) |
421 | return true; | |
23b68cfa WY |
422 | /* |
423 | * We start only with one section of pages, more pages are added as | |
424 | * needed until the rest of deferred pages are initialized. | |
425 | */ | |
d3035be4 | 426 | nr_initialised++; |
23b68cfa | 427 | if ((nr_initialised > PAGES_PER_SECTION) && |
d3035be4 PT |
428 | (pfn & (PAGES_PER_SECTION - 1)) == 0) { |
429 | NODE_DATA(nid)->first_deferred_pfn = pfn; | |
430 | return true; | |
3a80a7fa | 431 | } |
d3035be4 | 432 | return false; |
3a80a7fa MG |
433 | } |
434 | #else | |
94ae8b83 | 435 | static inline bool deferred_pages_enabled(void) |
2c335680 | 436 | { |
94ae8b83 | 437 | return false; |
2c335680 | 438 | } |
3c0c12cc | 439 | |
3a80a7fa MG |
440 | static inline bool early_page_uninitialised(unsigned long pfn) |
441 | { | |
442 | return false; | |
443 | } | |
444 | ||
d3035be4 | 445 | static inline bool defer_init(int nid, unsigned long pfn, unsigned long end_pfn) |
3a80a7fa | 446 | { |
d3035be4 | 447 | return false; |
3a80a7fa MG |
448 | } |
449 | #endif | |
450 | ||
0b423ca2 | 451 | /* Return a pointer to the bitmap storing bits affecting a block of pages */ |
ca891f41 | 452 | static inline unsigned long *get_pageblock_bitmap(const struct page *page, |
0b423ca2 MG |
453 | unsigned long pfn) |
454 | { | |
455 | #ifdef CONFIG_SPARSEMEM | |
f1eca35a | 456 | return section_to_usemap(__pfn_to_section(pfn)); |
0b423ca2 MG |
457 | #else |
458 | return page_zone(page)->pageblock_flags; | |
459 | #endif /* CONFIG_SPARSEMEM */ | |
460 | } | |
461 | ||
ca891f41 | 462 | static inline int pfn_to_bitidx(const struct page *page, unsigned long pfn) |
0b423ca2 MG |
463 | { |
464 | #ifdef CONFIG_SPARSEMEM | |
465 | pfn &= (PAGES_PER_SECTION-1); | |
0b423ca2 MG |
466 | #else |
467 | pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages); | |
0b423ca2 | 468 | #endif /* CONFIG_SPARSEMEM */ |
399b795b | 469 | return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
0b423ca2 MG |
470 | } |
471 | ||
535b81e2 | 472 | static __always_inline |
ca891f41 | 473 | unsigned long __get_pfnblock_flags_mask(const struct page *page, |
0b423ca2 | 474 | unsigned long pfn, |
0b423ca2 MG |
475 | unsigned long mask) |
476 | { | |
477 | unsigned long *bitmap; | |
478 | unsigned long bitidx, word_bitidx; | |
479 | unsigned long word; | |
480 | ||
481 | bitmap = get_pageblock_bitmap(page, pfn); | |
482 | bitidx = pfn_to_bitidx(page, pfn); | |
483 | word_bitidx = bitidx / BITS_PER_LONG; | |
484 | bitidx &= (BITS_PER_LONG-1); | |
485 | ||
486 | word = bitmap[word_bitidx]; | |
d93d5ab9 | 487 | return (word >> bitidx) & mask; |
0b423ca2 MG |
488 | } |
489 | ||
a00cda3f MCC |
490 | /** |
491 | * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages | |
492 | * @page: The page within the block of interest | |
493 | * @pfn: The target page frame number | |
494 | * @mask: mask of bits that the caller is interested in | |
495 | * | |
496 | * Return: pageblock_bits flags | |
497 | */ | |
ca891f41 MWO |
498 | unsigned long get_pfnblock_flags_mask(const struct page *page, |
499 | unsigned long pfn, unsigned long mask) | |
0b423ca2 | 500 | { |
535b81e2 | 501 | return __get_pfnblock_flags_mask(page, pfn, mask); |
0b423ca2 MG |
502 | } |
503 | ||
ca891f41 MWO |
504 | static __always_inline int get_pfnblock_migratetype(const struct page *page, |
505 | unsigned long pfn) | |
0b423ca2 | 506 | { |
535b81e2 | 507 | return __get_pfnblock_flags_mask(page, pfn, MIGRATETYPE_MASK); |
0b423ca2 MG |
508 | } |
509 | ||
510 | /** | |
511 | * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages | |
512 | * @page: The page within the block of interest | |
513 | * @flags: The flags to set | |
514 | * @pfn: The target page frame number | |
0b423ca2 MG |
515 | * @mask: mask of bits that the caller is interested in |
516 | */ | |
517 | void set_pfnblock_flags_mask(struct page *page, unsigned long flags, | |
518 | unsigned long pfn, | |
0b423ca2 MG |
519 | unsigned long mask) |
520 | { | |
521 | unsigned long *bitmap; | |
522 | unsigned long bitidx, word_bitidx; | |
523 | unsigned long old_word, word; | |
524 | ||
525 | BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4); | |
125b860b | 526 | BUILD_BUG_ON(MIGRATE_TYPES > (1 << PB_migratetype_bits)); |
0b423ca2 MG |
527 | |
528 | bitmap = get_pageblock_bitmap(page, pfn); | |
529 | bitidx = pfn_to_bitidx(page, pfn); | |
530 | word_bitidx = bitidx / BITS_PER_LONG; | |
531 | bitidx &= (BITS_PER_LONG-1); | |
532 | ||
533 | VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page); | |
534 | ||
d93d5ab9 WY |
535 | mask <<= bitidx; |
536 | flags <<= bitidx; | |
0b423ca2 MG |
537 | |
538 | word = READ_ONCE(bitmap[word_bitidx]); | |
539 | for (;;) { | |
540 | old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags); | |
541 | if (word == old_word) | |
542 | break; | |
543 | word = old_word; | |
544 | } | |
545 | } | |
3a80a7fa | 546 | |
ee6f509c | 547 | void set_pageblock_migratetype(struct page *page, int migratetype) |
b2a0ac88 | 548 | { |
5d0f3f72 KM |
549 | if (unlikely(page_group_by_mobility_disabled && |
550 | migratetype < MIGRATE_PCPTYPES)) | |
49255c61 MG |
551 | migratetype = MIGRATE_UNMOVABLE; |
552 | ||
d93d5ab9 | 553 | set_pfnblock_flags_mask(page, (unsigned long)migratetype, |
535b81e2 | 554 | page_to_pfn(page), MIGRATETYPE_MASK); |
b2a0ac88 MG |
555 | } |
556 | ||
13e7444b | 557 | #ifdef CONFIG_DEBUG_VM |
c6a57e19 | 558 | static int page_outside_zone_boundaries(struct zone *zone, struct page *page) |
1da177e4 | 559 | { |
bdc8cb98 DH |
560 | int ret = 0; |
561 | unsigned seq; | |
562 | unsigned long pfn = page_to_pfn(page); | |
b5e6a5a2 | 563 | unsigned long sp, start_pfn; |
c6a57e19 | 564 | |
bdc8cb98 DH |
565 | do { |
566 | seq = zone_span_seqbegin(zone); | |
b5e6a5a2 CS |
567 | start_pfn = zone->zone_start_pfn; |
568 | sp = zone->spanned_pages; | |
108bcc96 | 569 | if (!zone_spans_pfn(zone, pfn)) |
bdc8cb98 DH |
570 | ret = 1; |
571 | } while (zone_span_seqretry(zone, seq)); | |
572 | ||
b5e6a5a2 | 573 | if (ret) |
613813e8 DH |
574 | pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n", |
575 | pfn, zone_to_nid(zone), zone->name, | |
576 | start_pfn, start_pfn + sp); | |
b5e6a5a2 | 577 | |
bdc8cb98 | 578 | return ret; |
c6a57e19 DH |
579 | } |
580 | ||
581 | static int page_is_consistent(struct zone *zone, struct page *page) | |
582 | { | |
1da177e4 | 583 | if (zone != page_zone(page)) |
c6a57e19 DH |
584 | return 0; |
585 | ||
586 | return 1; | |
587 | } | |
588 | /* | |
589 | * Temporary debugging check for pages not lying within a given zone. | |
590 | */ | |
d73d3c9f | 591 | static int __maybe_unused bad_range(struct zone *zone, struct page *page) |
c6a57e19 DH |
592 | { |
593 | if (page_outside_zone_boundaries(zone, page)) | |
1da177e4 | 594 | return 1; |
c6a57e19 DH |
595 | if (!page_is_consistent(zone, page)) |
596 | return 1; | |
597 | ||
1da177e4 LT |
598 | return 0; |
599 | } | |
13e7444b | 600 | #else |
d73d3c9f | 601 | static inline int __maybe_unused bad_range(struct zone *zone, struct page *page) |
13e7444b NP |
602 | { |
603 | return 0; | |
604 | } | |
605 | #endif | |
606 | ||
82a3241a | 607 | static void bad_page(struct page *page, const char *reason) |
1da177e4 | 608 | { |
d936cf9b HD |
609 | static unsigned long resume; |
610 | static unsigned long nr_shown; | |
611 | static unsigned long nr_unshown; | |
612 | ||
613 | /* | |
614 | * Allow a burst of 60 reports, then keep quiet for that minute; | |
615 | * or allow a steady drip of one report per second. | |
616 | */ | |
617 | if (nr_shown == 60) { | |
618 | if (time_before(jiffies, resume)) { | |
619 | nr_unshown++; | |
620 | goto out; | |
621 | } | |
622 | if (nr_unshown) { | |
ff8e8116 | 623 | pr_alert( |
1e9e6365 | 624 | "BUG: Bad page state: %lu messages suppressed\n", |
d936cf9b HD |
625 | nr_unshown); |
626 | nr_unshown = 0; | |
627 | } | |
628 | nr_shown = 0; | |
629 | } | |
630 | if (nr_shown++ == 0) | |
631 | resume = jiffies + 60 * HZ; | |
632 | ||
ff8e8116 | 633 | pr_alert("BUG: Bad page state in process %s pfn:%05lx\n", |
3dc14741 | 634 | current->comm, page_to_pfn(page)); |
d2f07ec0 | 635 | dump_page(page, reason); |
3dc14741 | 636 | |
4f31888c | 637 | print_modules(); |
1da177e4 | 638 | dump_stack(); |
d936cf9b | 639 | out: |
8cc3b392 | 640 | /* Leave bad fields for debug, except PageBuddy could make trouble */ |
22b751c3 | 641 | page_mapcount_reset(page); /* remove PageBuddy */ |
373d4d09 | 642 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
1da177e4 LT |
643 | } |
644 | ||
44042b44 MG |
645 | static inline unsigned int order_to_pindex(int migratetype, int order) |
646 | { | |
647 | int base = order; | |
648 | ||
649 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
650 | if (order > PAGE_ALLOC_COSTLY_ORDER) { | |
651 | VM_BUG_ON(order != pageblock_order); | |
652 | base = PAGE_ALLOC_COSTLY_ORDER + 1; | |
653 | } | |
654 | #else | |
655 | VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); | |
656 | #endif | |
657 | ||
658 | return (MIGRATE_PCPTYPES * base) + migratetype; | |
659 | } | |
660 | ||
661 | static inline int pindex_to_order(unsigned int pindex) | |
662 | { | |
663 | int order = pindex / MIGRATE_PCPTYPES; | |
664 | ||
665 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
ea808b4e | 666 | if (order > PAGE_ALLOC_COSTLY_ORDER) |
44042b44 | 667 | order = pageblock_order; |
44042b44 MG |
668 | #else |
669 | VM_BUG_ON(order > PAGE_ALLOC_COSTLY_ORDER); | |
670 | #endif | |
671 | ||
672 | return order; | |
673 | } | |
674 | ||
675 | static inline bool pcp_allowed_order(unsigned int order) | |
676 | { | |
677 | if (order <= PAGE_ALLOC_COSTLY_ORDER) | |
678 | return true; | |
679 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
680 | if (order == pageblock_order) | |
681 | return true; | |
682 | #endif | |
683 | return false; | |
684 | } | |
685 | ||
21d02f8f MG |
686 | static inline void free_the_page(struct page *page, unsigned int order) |
687 | { | |
44042b44 MG |
688 | if (pcp_allowed_order(order)) /* Via pcp? */ |
689 | free_unref_page(page, order); | |
21d02f8f MG |
690 | else |
691 | __free_pages_ok(page, order, FPI_NONE); | |
692 | } | |
693 | ||
1da177e4 LT |
694 | /* |
695 | * Higher-order pages are called "compound pages". They are structured thusly: | |
696 | * | |
1d798ca3 | 697 | * The first PAGE_SIZE page is called the "head page" and have PG_head set. |
1da177e4 | 698 | * |
1d798ca3 KS |
699 | * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded |
700 | * in bit 0 of page->compound_head. The rest of bits is pointer to head page. | |
1da177e4 | 701 | * |
1d798ca3 KS |
702 | * The first tail page's ->compound_dtor holds the offset in array of compound |
703 | * page destructors. See compound_page_dtors. | |
1da177e4 | 704 | * |
1d798ca3 | 705 | * The first tail page's ->compound_order holds the order of allocation. |
41d78ba5 | 706 | * This usage means that zero-order pages may not be compound. |
1da177e4 | 707 | */ |
d98c7a09 | 708 | |
9a982250 | 709 | void free_compound_page(struct page *page) |
d98c7a09 | 710 | { |
bbc6b703 | 711 | mem_cgroup_uncharge(page_folio(page)); |
44042b44 | 712 | free_the_page(page, compound_order(page)); |
d98c7a09 HD |
713 | } |
714 | ||
5b24eeef JM |
715 | static void prep_compound_head(struct page *page, unsigned int order) |
716 | { | |
717 | set_compound_page_dtor(page, COMPOUND_PAGE_DTOR); | |
718 | set_compound_order(page, order); | |
719 | atomic_set(compound_mapcount_ptr(page), -1); | |
5232c63f | 720 | atomic_set(compound_pincount_ptr(page), 0); |
5b24eeef JM |
721 | } |
722 | ||
723 | static void prep_compound_tail(struct page *head, int tail_idx) | |
724 | { | |
725 | struct page *p = head + tail_idx; | |
726 | ||
727 | p->mapping = TAIL_MAPPING; | |
728 | set_compound_head(p, head); | |
729 | } | |
730 | ||
d00181b9 | 731 | void prep_compound_page(struct page *page, unsigned int order) |
18229df5 AW |
732 | { |
733 | int i; | |
734 | int nr_pages = 1 << order; | |
735 | ||
18229df5 | 736 | __SetPageHead(page); |
5b24eeef JM |
737 | for (i = 1; i < nr_pages; i++) |
738 | prep_compound_tail(page, i); | |
1378a5ee | 739 | |
5b24eeef | 740 | prep_compound_head(page, order); |
18229df5 AW |
741 | } |
742 | ||
c0a32fc5 SG |
743 | #ifdef CONFIG_DEBUG_PAGEALLOC |
744 | unsigned int _debug_guardpage_minorder; | |
96a2b03f | 745 | |
8e57f8ac VB |
746 | bool _debug_pagealloc_enabled_early __read_mostly |
747 | = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT); | |
748 | EXPORT_SYMBOL(_debug_pagealloc_enabled_early); | |
96a2b03f | 749 | DEFINE_STATIC_KEY_FALSE(_debug_pagealloc_enabled); |
505f6d22 | 750 | EXPORT_SYMBOL(_debug_pagealloc_enabled); |
96a2b03f VB |
751 | |
752 | DEFINE_STATIC_KEY_FALSE(_debug_guardpage_enabled); | |
e30825f1 | 753 | |
031bc574 JK |
754 | static int __init early_debug_pagealloc(char *buf) |
755 | { | |
8e57f8ac | 756 | return kstrtobool(buf, &_debug_pagealloc_enabled_early); |
031bc574 JK |
757 | } |
758 | early_param("debug_pagealloc", early_debug_pagealloc); | |
759 | ||
c0a32fc5 SG |
760 | static int __init debug_guardpage_minorder_setup(char *buf) |
761 | { | |
762 | unsigned long res; | |
763 | ||
764 | if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) { | |
1170532b | 765 | pr_err("Bad debug_guardpage_minorder value\n"); |
c0a32fc5 SG |
766 | return 0; |
767 | } | |
768 | _debug_guardpage_minorder = res; | |
1170532b | 769 | pr_info("Setting debug_guardpage_minorder to %lu\n", res); |
c0a32fc5 SG |
770 | return 0; |
771 | } | |
f1c1e9f7 | 772 | early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup); |
c0a32fc5 | 773 | |
acbc15a4 | 774 | static inline bool set_page_guard(struct zone *zone, struct page *page, |
2847cf95 | 775 | unsigned int order, int migratetype) |
c0a32fc5 | 776 | { |
e30825f1 | 777 | if (!debug_guardpage_enabled()) |
acbc15a4 JK |
778 | return false; |
779 | ||
780 | if (order >= debug_guardpage_minorder()) | |
781 | return false; | |
e30825f1 | 782 | |
3972f6bb | 783 | __SetPageGuard(page); |
2847cf95 JK |
784 | INIT_LIST_HEAD(&page->lru); |
785 | set_page_private(page, order); | |
786 | /* Guard pages are not available for any usage */ | |
787 | __mod_zone_freepage_state(zone, -(1 << order), migratetype); | |
acbc15a4 JK |
788 | |
789 | return true; | |
c0a32fc5 SG |
790 | } |
791 | ||
2847cf95 JK |
792 | static inline void clear_page_guard(struct zone *zone, struct page *page, |
793 | unsigned int order, int migratetype) | |
c0a32fc5 | 794 | { |
e30825f1 JK |
795 | if (!debug_guardpage_enabled()) |
796 | return; | |
797 | ||
3972f6bb | 798 | __ClearPageGuard(page); |
e30825f1 | 799 | |
2847cf95 JK |
800 | set_page_private(page, 0); |
801 | if (!is_migrate_isolate(migratetype)) | |
802 | __mod_zone_freepage_state(zone, (1 << order), migratetype); | |
c0a32fc5 SG |
803 | } |
804 | #else | |
acbc15a4 JK |
805 | static inline bool set_page_guard(struct zone *zone, struct page *page, |
806 | unsigned int order, int migratetype) { return false; } | |
2847cf95 JK |
807 | static inline void clear_page_guard(struct zone *zone, struct page *page, |
808 | unsigned int order, int migratetype) {} | |
c0a32fc5 SG |
809 | #endif |
810 | ||
04013513 VB |
811 | /* |
812 | * Enable static keys related to various memory debugging and hardening options. | |
813 | * Some override others, and depend on early params that are evaluated in the | |
814 | * order of appearance. So we need to first gather the full picture of what was | |
815 | * enabled, and then make decisions. | |
816 | */ | |
817 | void init_mem_debugging_and_hardening(void) | |
818 | { | |
9df65f52 ST |
819 | bool page_poisoning_requested = false; |
820 | ||
821 | #ifdef CONFIG_PAGE_POISONING | |
822 | /* | |
823 | * Page poisoning is debug page alloc for some arches. If | |
824 | * either of those options are enabled, enable poisoning. | |
825 | */ | |
826 | if (page_poisoning_enabled() || | |
827 | (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) && | |
828 | debug_pagealloc_enabled())) { | |
829 | static_branch_enable(&_page_poisoning_enabled); | |
830 | page_poisoning_requested = true; | |
831 | } | |
832 | #endif | |
833 | ||
69e5d322 ST |
834 | if ((_init_on_alloc_enabled_early || _init_on_free_enabled_early) && |
835 | page_poisoning_requested) { | |
836 | pr_info("mem auto-init: CONFIG_PAGE_POISONING is on, " | |
837 | "will take precedence over init_on_alloc and init_on_free\n"); | |
838 | _init_on_alloc_enabled_early = false; | |
839 | _init_on_free_enabled_early = false; | |
04013513 VB |
840 | } |
841 | ||
69e5d322 ST |
842 | if (_init_on_alloc_enabled_early) |
843 | static_branch_enable(&init_on_alloc); | |
844 | else | |
845 | static_branch_disable(&init_on_alloc); | |
846 | ||
847 | if (_init_on_free_enabled_early) | |
848 | static_branch_enable(&init_on_free); | |
849 | else | |
850 | static_branch_disable(&init_on_free); | |
851 | ||
04013513 VB |
852 | #ifdef CONFIG_DEBUG_PAGEALLOC |
853 | if (!debug_pagealloc_enabled()) | |
854 | return; | |
855 | ||
856 | static_branch_enable(&_debug_pagealloc_enabled); | |
857 | ||
858 | if (!debug_guardpage_minorder()) | |
859 | return; | |
860 | ||
861 | static_branch_enable(&_debug_guardpage_enabled); | |
862 | #endif | |
863 | } | |
864 | ||
ab130f91 | 865 | static inline void set_buddy_order(struct page *page, unsigned int order) |
6aa3001b | 866 | { |
4c21e2f2 | 867 | set_page_private(page, order); |
676165a8 | 868 | __SetPageBuddy(page); |
1da177e4 LT |
869 | } |
870 | ||
5e1f0f09 MG |
871 | #ifdef CONFIG_COMPACTION |
872 | static inline struct capture_control *task_capc(struct zone *zone) | |
873 | { | |
874 | struct capture_control *capc = current->capture_control; | |
875 | ||
deba0487 | 876 | return unlikely(capc) && |
5e1f0f09 MG |
877 | !(current->flags & PF_KTHREAD) && |
878 | !capc->page && | |
deba0487 | 879 | capc->cc->zone == zone ? capc : NULL; |
5e1f0f09 MG |
880 | } |
881 | ||
882 | static inline bool | |
883 | compaction_capture(struct capture_control *capc, struct page *page, | |
884 | int order, int migratetype) | |
885 | { | |
886 | if (!capc || order != capc->cc->order) | |
887 | return false; | |
888 | ||
889 | /* Do not accidentally pollute CMA or isolated regions*/ | |
890 | if (is_migrate_cma(migratetype) || | |
891 | is_migrate_isolate(migratetype)) | |
892 | return false; | |
893 | ||
894 | /* | |
f0953a1b | 895 | * Do not let lower order allocations pollute a movable pageblock. |
5e1f0f09 MG |
896 | * This might let an unmovable request use a reclaimable pageblock |
897 | * and vice-versa but no more than normal fallback logic which can | |
898 | * have trouble finding a high-order free page. | |
899 | */ | |
900 | if (order < pageblock_order && migratetype == MIGRATE_MOVABLE) | |
901 | return false; | |
902 | ||
903 | capc->page = page; | |
904 | return true; | |
905 | } | |
906 | ||
907 | #else | |
908 | static inline struct capture_control *task_capc(struct zone *zone) | |
909 | { | |
910 | return NULL; | |
911 | } | |
912 | ||
913 | static inline bool | |
914 | compaction_capture(struct capture_control *capc, struct page *page, | |
915 | int order, int migratetype) | |
916 | { | |
917 | return false; | |
918 | } | |
919 | #endif /* CONFIG_COMPACTION */ | |
920 | ||
6ab01363 AD |
921 | /* Used for pages not on another list */ |
922 | static inline void add_to_free_list(struct page *page, struct zone *zone, | |
923 | unsigned int order, int migratetype) | |
924 | { | |
925 | struct free_area *area = &zone->free_area[order]; | |
926 | ||
927 | list_add(&page->lru, &area->free_list[migratetype]); | |
928 | area->nr_free++; | |
929 | } | |
930 | ||
931 | /* Used for pages not on another list */ | |
932 | static inline void add_to_free_list_tail(struct page *page, struct zone *zone, | |
933 | unsigned int order, int migratetype) | |
934 | { | |
935 | struct free_area *area = &zone->free_area[order]; | |
936 | ||
937 | list_add_tail(&page->lru, &area->free_list[migratetype]); | |
938 | area->nr_free++; | |
939 | } | |
940 | ||
293ffa5e DH |
941 | /* |
942 | * Used for pages which are on another list. Move the pages to the tail | |
943 | * of the list - so the moved pages won't immediately be considered for | |
944 | * allocation again (e.g., optimization for memory onlining). | |
945 | */ | |
6ab01363 AD |
946 | static inline void move_to_free_list(struct page *page, struct zone *zone, |
947 | unsigned int order, int migratetype) | |
948 | { | |
949 | struct free_area *area = &zone->free_area[order]; | |
950 | ||
293ffa5e | 951 | list_move_tail(&page->lru, &area->free_list[migratetype]); |
6ab01363 AD |
952 | } |
953 | ||
954 | static inline void del_page_from_free_list(struct page *page, struct zone *zone, | |
955 | unsigned int order) | |
956 | { | |
36e66c55 AD |
957 | /* clear reported state and update reported page count */ |
958 | if (page_reported(page)) | |
959 | __ClearPageReported(page); | |
960 | ||
6ab01363 AD |
961 | list_del(&page->lru); |
962 | __ClearPageBuddy(page); | |
963 | set_page_private(page, 0); | |
964 | zone->free_area[order].nr_free--; | |
965 | } | |
966 | ||
a2129f24 AD |
967 | /* |
968 | * If this is not the largest possible page, check if the buddy | |
969 | * of the next-highest order is free. If it is, it's possible | |
970 | * that pages are being freed that will coalesce soon. In case, | |
971 | * that is happening, add the free page to the tail of the list | |
972 | * so it's less likely to be used soon and more likely to be merged | |
973 | * as a higher order page | |
974 | */ | |
975 | static inline bool | |
976 | buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, | |
977 | struct page *page, unsigned int order) | |
978 | { | |
8170ac47 ZY |
979 | unsigned long higher_page_pfn; |
980 | struct page *higher_page; | |
a2129f24 AD |
981 | |
982 | if (order >= MAX_ORDER - 2) | |
983 | return false; | |
984 | ||
8170ac47 ZY |
985 | higher_page_pfn = buddy_pfn & pfn; |
986 | higher_page = page + (higher_page_pfn - pfn); | |
a2129f24 | 987 | |
8170ac47 ZY |
988 | return find_buddy_page_pfn(higher_page, higher_page_pfn, order + 1, |
989 | NULL) != NULL; | |
a2129f24 AD |
990 | } |
991 | ||
1da177e4 LT |
992 | /* |
993 | * Freeing function for a buddy system allocator. | |
994 | * | |
995 | * The concept of a buddy system is to maintain direct-mapped table | |
996 | * (containing bit values) for memory blocks of various "orders". | |
997 | * The bottom level table contains the map for the smallest allocatable | |
998 | * units of memory (here, pages), and each level above it describes | |
999 | * pairs of units from the levels below, hence, "buddies". | |
1000 | * At a high level, all that happens here is marking the table entry | |
1001 | * at the bottom level available, and propagating the changes upward | |
1002 | * as necessary, plus some accounting needed to play nicely with other | |
1003 | * parts of the VM system. | |
1004 | * At each level, we keep a list of pages, which are heads of continuous | |
6e292b9b MW |
1005 | * free pages of length of (1 << order) and marked with PageBuddy. |
1006 | * Page's order is recorded in page_private(page) field. | |
1da177e4 | 1007 | * So when we are allocating or freeing one, we can derive the state of the |
5f63b720 MN |
1008 | * other. That is, if we allocate a small block, and both were |
1009 | * free, the remainder of the region must be split into blocks. | |
1da177e4 | 1010 | * If a block is freed, and its buddy is also free, then this |
5f63b720 | 1011 | * triggers coalescing into a block of larger size. |
1da177e4 | 1012 | * |
6d49e352 | 1013 | * -- nyc |
1da177e4 LT |
1014 | */ |
1015 | ||
48db57f8 | 1016 | static inline void __free_one_page(struct page *page, |
dc4b0caf | 1017 | unsigned long pfn, |
ed0ae21d | 1018 | struct zone *zone, unsigned int order, |
f04a5d5d | 1019 | int migratetype, fpi_t fpi_flags) |
1da177e4 | 1020 | { |
a2129f24 | 1021 | struct capture_control *capc = task_capc(zone); |
3f649ab7 | 1022 | unsigned long buddy_pfn; |
a2129f24 | 1023 | unsigned long combined_pfn; |
a2129f24 AD |
1024 | struct page *buddy; |
1025 | bool to_tail; | |
d9dddbf5 | 1026 | |
d29bb978 | 1027 | VM_BUG_ON(!zone_is_initialized(zone)); |
6e9f0d58 | 1028 | VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page); |
1da177e4 | 1029 | |
ed0ae21d | 1030 | VM_BUG_ON(migratetype == -1); |
d9dddbf5 | 1031 | if (likely(!is_migrate_isolate(migratetype))) |
8f82b55d | 1032 | __mod_zone_freepage_state(zone, 1 << order, migratetype); |
ed0ae21d | 1033 | |
76741e77 | 1034 | VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); |
309381fe | 1035 | VM_BUG_ON_PAGE(bad_range(zone, page), page); |
1da177e4 | 1036 | |
bb0e28eb | 1037 | while (order < MAX_ORDER - 1) { |
5e1f0f09 MG |
1038 | if (compaction_capture(capc, page, order, migratetype)) { |
1039 | __mod_zone_freepage_state(zone, -(1 << order), | |
1040 | migratetype); | |
1041 | return; | |
1042 | } | |
13ad59df | 1043 | |
8170ac47 ZY |
1044 | buddy = find_buddy_page_pfn(page, pfn, order, &buddy_pfn); |
1045 | if (!buddy) | |
d9dddbf5 | 1046 | goto done_merging; |
bb0e28eb ZY |
1047 | |
1048 | if (unlikely(order >= pageblock_order)) { | |
1049 | /* | |
1050 | * We want to prevent merge between freepages on pageblock | |
1051 | * without fallbacks and normal pageblock. Without this, | |
1052 | * pageblock isolation could cause incorrect freepage or CMA | |
1053 | * accounting or HIGHATOMIC accounting. | |
1054 | */ | |
1055 | int buddy_mt = get_pageblock_migratetype(buddy); | |
1056 | ||
1057 | if (migratetype != buddy_mt | |
1058 | && (!migratetype_is_mergeable(migratetype) || | |
1059 | !migratetype_is_mergeable(buddy_mt))) | |
1060 | goto done_merging; | |
1061 | } | |
1062 | ||
c0a32fc5 SG |
1063 | /* |
1064 | * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page, | |
1065 | * merge with it and move up one order. | |
1066 | */ | |
b03641af | 1067 | if (page_is_guard(buddy)) |
2847cf95 | 1068 | clear_page_guard(zone, buddy, order, migratetype); |
b03641af | 1069 | else |
6ab01363 | 1070 | del_page_from_free_list(buddy, zone, order); |
76741e77 VB |
1071 | combined_pfn = buddy_pfn & pfn; |
1072 | page = page + (combined_pfn - pfn); | |
1073 | pfn = combined_pfn; | |
1da177e4 LT |
1074 | order++; |
1075 | } | |
d9dddbf5 VB |
1076 | |
1077 | done_merging: | |
ab130f91 | 1078 | set_buddy_order(page, order); |
6dda9d55 | 1079 | |
47b6a24a DH |
1080 | if (fpi_flags & FPI_TO_TAIL) |
1081 | to_tail = true; | |
1082 | else if (is_shuffle_order(order)) | |
a2129f24 | 1083 | to_tail = shuffle_pick_tail(); |
97500a4a | 1084 | else |
a2129f24 | 1085 | to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); |
97500a4a | 1086 | |
a2129f24 | 1087 | if (to_tail) |
6ab01363 | 1088 | add_to_free_list_tail(page, zone, order, migratetype); |
a2129f24 | 1089 | else |
6ab01363 | 1090 | add_to_free_list(page, zone, order, migratetype); |
36e66c55 AD |
1091 | |
1092 | /* Notify page reporting subsystem of freed page */ | |
f04a5d5d | 1093 | if (!(fpi_flags & FPI_SKIP_REPORT_NOTIFY)) |
36e66c55 | 1094 | page_reporting_notify_free(order); |
1da177e4 LT |
1095 | } |
1096 | ||
b2c9e2fb ZY |
1097 | /** |
1098 | * split_free_page() -- split a free page at split_pfn_offset | |
1099 | * @free_page: the original free page | |
1100 | * @order: the order of the page | |
1101 | * @split_pfn_offset: split offset within the page | |
1102 | * | |
1103 | * It is used when the free page crosses two pageblocks with different migratetypes | |
1104 | * at split_pfn_offset within the page. The split free page will be put into | |
1105 | * separate migratetype lists afterwards. Otherwise, the function achieves | |
1106 | * nothing. | |
1107 | */ | |
1108 | void split_free_page(struct page *free_page, | |
1109 | int order, unsigned long split_pfn_offset) | |
1110 | { | |
1111 | struct zone *zone = page_zone(free_page); | |
1112 | unsigned long free_page_pfn = page_to_pfn(free_page); | |
1113 | unsigned long pfn; | |
1114 | unsigned long flags; | |
1115 | int free_page_order; | |
1116 | ||
1117 | spin_lock_irqsave(&zone->lock, flags); | |
1118 | del_page_from_free_list(free_page, zone, order); | |
1119 | for (pfn = free_page_pfn; | |
1120 | pfn < free_page_pfn + (1UL << order);) { | |
1121 | int mt = get_pfnblock_migratetype(pfn_to_page(pfn), pfn); | |
1122 | ||
1123 | free_page_order = ffs(split_pfn_offset) - 1; | |
1124 | __free_one_page(pfn_to_page(pfn), pfn, zone, free_page_order, | |
1125 | mt, FPI_NONE); | |
1126 | pfn += 1UL << free_page_order; | |
1127 | split_pfn_offset -= (1UL << free_page_order); | |
1128 | /* we have done the first part, now switch to second part */ | |
1129 | if (split_pfn_offset == 0) | |
1130 | split_pfn_offset = (1UL << order) - (pfn - free_page_pfn); | |
1131 | } | |
1132 | spin_unlock_irqrestore(&zone->lock, flags); | |
1133 | } | |
7bfec6f4 MG |
1134 | /* |
1135 | * A bad page could be due to a number of fields. Instead of multiple branches, | |
1136 | * try and check multiple fields with one check. The caller must do a detailed | |
1137 | * check if necessary. | |
1138 | */ | |
1139 | static inline bool page_expected_state(struct page *page, | |
1140 | unsigned long check_flags) | |
1141 | { | |
1142 | if (unlikely(atomic_read(&page->_mapcount) != -1)) | |
1143 | return false; | |
1144 | ||
1145 | if (unlikely((unsigned long)page->mapping | | |
1146 | page_ref_count(page) | | |
1147 | #ifdef CONFIG_MEMCG | |
48060834 | 1148 | page->memcg_data | |
7bfec6f4 MG |
1149 | #endif |
1150 | (page->flags & check_flags))) | |
1151 | return false; | |
1152 | ||
1153 | return true; | |
1154 | } | |
1155 | ||
58b7f119 | 1156 | static const char *page_bad_reason(struct page *page, unsigned long flags) |
1da177e4 | 1157 | { |
82a3241a | 1158 | const char *bad_reason = NULL; |
f0b791a3 | 1159 | |
53f9263b | 1160 | if (unlikely(atomic_read(&page->_mapcount) != -1)) |
f0b791a3 DH |
1161 | bad_reason = "nonzero mapcount"; |
1162 | if (unlikely(page->mapping != NULL)) | |
1163 | bad_reason = "non-NULL mapping"; | |
fe896d18 | 1164 | if (unlikely(page_ref_count(page) != 0)) |
0139aa7b | 1165 | bad_reason = "nonzero _refcount"; |
58b7f119 WY |
1166 | if (unlikely(page->flags & flags)) { |
1167 | if (flags == PAGE_FLAGS_CHECK_AT_PREP) | |
1168 | bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag(s) set"; | |
1169 | else | |
1170 | bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set"; | |
f0b791a3 | 1171 | } |
9edad6ea | 1172 | #ifdef CONFIG_MEMCG |
48060834 | 1173 | if (unlikely(page->memcg_data)) |
9edad6ea JW |
1174 | bad_reason = "page still charged to cgroup"; |
1175 | #endif | |
58b7f119 WY |
1176 | return bad_reason; |
1177 | } | |
1178 | ||
1179 | static void check_free_page_bad(struct page *page) | |
1180 | { | |
1181 | bad_page(page, | |
1182 | page_bad_reason(page, PAGE_FLAGS_CHECK_AT_FREE)); | |
bb552ac6 MG |
1183 | } |
1184 | ||
534fe5e3 | 1185 | static inline int check_free_page(struct page *page) |
bb552ac6 | 1186 | { |
da838d4f | 1187 | if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE))) |
bb552ac6 | 1188 | return 0; |
bb552ac6 MG |
1189 | |
1190 | /* Something has gone sideways, find it */ | |
0d0c48a2 | 1191 | check_free_page_bad(page); |
7bfec6f4 | 1192 | return 1; |
1da177e4 LT |
1193 | } |
1194 | ||
4db7548c MG |
1195 | static int free_tail_pages_check(struct page *head_page, struct page *page) |
1196 | { | |
1197 | int ret = 1; | |
1198 | ||
1199 | /* | |
1200 | * We rely page->lru.next never has bit 0 set, unless the page | |
1201 | * is PageTail(). Let's make sure that's true even for poisoned ->lru. | |
1202 | */ | |
1203 | BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1); | |
1204 | ||
1205 | if (!IS_ENABLED(CONFIG_DEBUG_VM)) { | |
1206 | ret = 0; | |
1207 | goto out; | |
1208 | } | |
1209 | switch (page - head_page) { | |
1210 | case 1: | |
4da1984e | 1211 | /* the first tail page: ->mapping may be compound_mapcount() */ |
4db7548c | 1212 | if (unlikely(compound_mapcount(page))) { |
82a3241a | 1213 | bad_page(page, "nonzero compound_mapcount"); |
4db7548c MG |
1214 | goto out; |
1215 | } | |
1216 | break; | |
1217 | case 2: | |
1218 | /* | |
1219 | * the second tail page: ->mapping is | |
fa3015b7 | 1220 | * deferred_list.next -- ignore value. |
4db7548c MG |
1221 | */ |
1222 | break; | |
1223 | default: | |
1224 | if (page->mapping != TAIL_MAPPING) { | |
82a3241a | 1225 | bad_page(page, "corrupted mapping in tail page"); |
4db7548c MG |
1226 | goto out; |
1227 | } | |
1228 | break; | |
1229 | } | |
1230 | if (unlikely(!PageTail(page))) { | |
82a3241a | 1231 | bad_page(page, "PageTail not set"); |
4db7548c MG |
1232 | goto out; |
1233 | } | |
1234 | if (unlikely(compound_head(page) != head_page)) { | |
82a3241a | 1235 | bad_page(page, "compound_head not consistent"); |
4db7548c MG |
1236 | goto out; |
1237 | } | |
1238 | ret = 0; | |
1239 | out: | |
1240 | page->mapping = NULL; | |
1241 | clear_compound_head(page); | |
1242 | return ret; | |
1243 | } | |
1244 | ||
94ae8b83 AK |
1245 | /* |
1246 | * Skip KASAN memory poisoning when either: | |
1247 | * | |
1248 | * 1. Deferred memory initialization has not yet completed, | |
1249 | * see the explanation below. | |
1250 | * 2. Skipping poisoning is requested via FPI_SKIP_KASAN_POISON, | |
1251 | * see the comment next to it. | |
1252 | * 3. Skipping poisoning is requested via __GFP_SKIP_KASAN_POISON, | |
1253 | * see the comment next to it. | |
1254 | * | |
1255 | * Poisoning pages during deferred memory init will greatly lengthen the | |
1256 | * process and cause problem in large memory systems as the deferred pages | |
1257 | * initialization is done with interrupt disabled. | |
1258 | * | |
1259 | * Assuming that there will be no reference to those newly initialized | |
1260 | * pages before they are ever allocated, this should have no effect on | |
1261 | * KASAN memory tracking as the poison will be properly inserted at page | |
1262 | * allocation time. The only corner case is when pages are allocated by | |
1263 | * on-demand allocation and then freed again before the deferred pages | |
1264 | * initialization is done, but this is not likely to happen. | |
1265 | */ | |
1266 | static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags) | |
1267 | { | |
1268 | return deferred_pages_enabled() || | |
1269 | (!IS_ENABLED(CONFIG_KASAN_GENERIC) && | |
1270 | (fpi_flags & FPI_SKIP_KASAN_POISON)) || | |
1271 | PageSkipKASanPoison(page); | |
1272 | } | |
1273 | ||
5b2c0713 | 1274 | static void kernel_init_free_pages(struct page *page, int numpages) |
6471384a AP |
1275 | { |
1276 | int i; | |
1277 | ||
9e15afa5 QC |
1278 | /* s390's use of memset() could override KASAN redzones. */ |
1279 | kasan_disable_current(); | |
aa1ef4d7 | 1280 | for (i = 0; i < numpages; i++) { |
acb35b17 | 1281 | u8 tag = page_kasan_tag(page + i); |
aa1ef4d7 | 1282 | page_kasan_tag_reset(page + i); |
6471384a | 1283 | clear_highpage(page + i); |
acb35b17 | 1284 | page_kasan_tag_set(page + i, tag); |
aa1ef4d7 | 1285 | } |
9e15afa5 | 1286 | kasan_enable_current(); |
6471384a AP |
1287 | } |
1288 | ||
e2769dbd | 1289 | static __always_inline bool free_pages_prepare(struct page *page, |
2c335680 | 1290 | unsigned int order, bool check_free, fpi_t fpi_flags) |
4db7548c | 1291 | { |
e2769dbd | 1292 | int bad = 0; |
c3525330 | 1293 | bool init = want_init_on_free(); |
4db7548c | 1294 | |
4db7548c MG |
1295 | VM_BUG_ON_PAGE(PageTail(page), page); |
1296 | ||
e2769dbd | 1297 | trace_mm_page_free(page, order); |
e2769dbd | 1298 | |
79f5f8fa OS |
1299 | if (unlikely(PageHWPoison(page)) && !order) { |
1300 | /* | |
1301 | * Do not let hwpoison pages hit pcplists/buddy | |
1302 | * Untie memcg state and reset page's owner | |
1303 | */ | |
18b2db3b | 1304 | if (memcg_kmem_enabled() && PageMemcgKmem(page)) |
79f5f8fa OS |
1305 | __memcg_kmem_uncharge_page(page, order); |
1306 | reset_page_owner(page, order); | |
df4e817b | 1307 | page_table_check_free(page, order); |
79f5f8fa OS |
1308 | return false; |
1309 | } | |
1310 | ||
e2769dbd MG |
1311 | /* |
1312 | * Check tail pages before head page information is cleared to | |
1313 | * avoid checking PageCompound for order-0 pages. | |
1314 | */ | |
1315 | if (unlikely(order)) { | |
1316 | bool compound = PageCompound(page); | |
1317 | int i; | |
1318 | ||
1319 | VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); | |
4db7548c | 1320 | |
eac96c3e | 1321 | if (compound) { |
9a73f61b | 1322 | ClearPageDoubleMap(page); |
eac96c3e YS |
1323 | ClearPageHasHWPoisoned(page); |
1324 | } | |
e2769dbd MG |
1325 | for (i = 1; i < (1 << order); i++) { |
1326 | if (compound) | |
1327 | bad += free_tail_pages_check(page, page + i); | |
534fe5e3 | 1328 | if (unlikely(check_free_page(page + i))) { |
e2769dbd MG |
1329 | bad++; |
1330 | continue; | |
1331 | } | |
1332 | (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; | |
1333 | } | |
1334 | } | |
bda807d4 | 1335 | if (PageMappingFlags(page)) |
4db7548c | 1336 | page->mapping = NULL; |
18b2db3b | 1337 | if (memcg_kmem_enabled() && PageMemcgKmem(page)) |
f4b00eab | 1338 | __memcg_kmem_uncharge_page(page, order); |
e2769dbd | 1339 | if (check_free) |
534fe5e3 | 1340 | bad += check_free_page(page); |
e2769dbd MG |
1341 | if (bad) |
1342 | return false; | |
4db7548c | 1343 | |
e2769dbd MG |
1344 | page_cpupid_reset_last(page); |
1345 | page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; | |
1346 | reset_page_owner(page, order); | |
df4e817b | 1347 | page_table_check_free(page, order); |
4db7548c MG |
1348 | |
1349 | if (!PageHighMem(page)) { | |
1350 | debug_check_no_locks_freed(page_address(page), | |
e2769dbd | 1351 | PAGE_SIZE << order); |
4db7548c | 1352 | debug_check_no_obj_freed(page_address(page), |
e2769dbd | 1353 | PAGE_SIZE << order); |
4db7548c | 1354 | } |
6471384a | 1355 | |
8db26a3d VB |
1356 | kernel_poison_pages(page, 1 << order); |
1357 | ||
f9d79e8d | 1358 | /* |
1bb5eab3 | 1359 | * As memory initialization might be integrated into KASAN, |
7c13c163 | 1360 | * KASAN poisoning and memory initialization code must be |
1bb5eab3 AK |
1361 | * kept together to avoid discrepancies in behavior. |
1362 | * | |
f9d79e8d AK |
1363 | * With hardware tag-based KASAN, memory tags must be set before the |
1364 | * page becomes unavailable via debug_pagealloc or arch_free_page. | |
1365 | */ | |
487a32ec | 1366 | if (!should_skip_kasan_poison(page, fpi_flags)) { |
c3525330 | 1367 | kasan_poison_pages(page, order, init); |
f9d79e8d | 1368 | |
db8a0477 AK |
1369 | /* Memory is already initialized if KASAN did it internally. */ |
1370 | if (kasan_has_integrated_init()) | |
1371 | init = false; | |
1372 | } | |
1373 | if (init) | |
1374 | kernel_init_free_pages(page, 1 << order); | |
1375 | ||
234fdce8 QC |
1376 | /* |
1377 | * arch_free_page() can make the page's contents inaccessible. s390 | |
1378 | * does this. So nothing which can access the page's contents should | |
1379 | * happen after this. | |
1380 | */ | |
1381 | arch_free_page(page, order); | |
1382 | ||
77bc7fd6 | 1383 | debug_pagealloc_unmap_pages(page, 1 << order); |
d6332692 | 1384 | |
4db7548c MG |
1385 | return true; |
1386 | } | |
1387 | ||
e2769dbd | 1388 | #ifdef CONFIG_DEBUG_VM |
4462b32c VB |
1389 | /* |
1390 | * With DEBUG_VM enabled, order-0 pages are checked immediately when being freed | |
1391 | * to pcp lists. With debug_pagealloc also enabled, they are also rechecked when | |
1392 | * moved from pcp lists to free lists. | |
1393 | */ | |
44042b44 | 1394 | static bool free_pcp_prepare(struct page *page, unsigned int order) |
e2769dbd | 1395 | { |
44042b44 | 1396 | return free_pages_prepare(page, order, true, FPI_NONE); |
e2769dbd MG |
1397 | } |
1398 | ||
4462b32c | 1399 | static bool bulkfree_pcp_prepare(struct page *page) |
e2769dbd | 1400 | { |
8e57f8ac | 1401 | if (debug_pagealloc_enabled_static()) |
534fe5e3 | 1402 | return check_free_page(page); |
4462b32c VB |
1403 | else |
1404 | return false; | |
e2769dbd MG |
1405 | } |
1406 | #else | |
4462b32c VB |
1407 | /* |
1408 | * With DEBUG_VM disabled, order-0 pages being freed are checked only when | |
1409 | * moving from pcp lists to free list in order to reduce overhead. With | |
1410 | * debug_pagealloc enabled, they are checked also immediately when being freed | |
1411 | * to the pcp lists. | |
1412 | */ | |
44042b44 | 1413 | static bool free_pcp_prepare(struct page *page, unsigned int order) |
e2769dbd | 1414 | { |
8e57f8ac | 1415 | if (debug_pagealloc_enabled_static()) |
44042b44 | 1416 | return free_pages_prepare(page, order, true, FPI_NONE); |
4462b32c | 1417 | else |
44042b44 | 1418 | return free_pages_prepare(page, order, false, FPI_NONE); |
e2769dbd MG |
1419 | } |
1420 | ||
4db7548c MG |
1421 | static bool bulkfree_pcp_prepare(struct page *page) |
1422 | { | |
534fe5e3 | 1423 | return check_free_page(page); |
4db7548c MG |
1424 | } |
1425 | #endif /* CONFIG_DEBUG_VM */ | |
1426 | ||
1da177e4 | 1427 | /* |
5f8dcc21 | 1428 | * Frees a number of pages from the PCP lists |
7cba630b | 1429 | * Assumes all pages on list are in same zone. |
207f36ee | 1430 | * count is the number of pages to free. |
1da177e4 | 1431 | */ |
5f8dcc21 | 1432 | static void free_pcppages_bulk(struct zone *zone, int count, |
fd56eef2 MG |
1433 | struct per_cpu_pages *pcp, |
1434 | int pindex) | |
1da177e4 | 1435 | { |
35b6d770 MG |
1436 | int min_pindex = 0; |
1437 | int max_pindex = NR_PCP_LISTS - 1; | |
44042b44 | 1438 | unsigned int order; |
3777999d | 1439 | bool isolated_pageblocks; |
8b10b465 | 1440 | struct page *page; |
f2260e6b | 1441 | |
88e8ac11 CTR |
1442 | /* |
1443 | * Ensure proper count is passed which otherwise would stuck in the | |
1444 | * below while (list_empty(list)) loop. | |
1445 | */ | |
1446 | count = min(pcp->count, count); | |
d61372bc MG |
1447 | |
1448 | /* Ensure requested pindex is drained first. */ | |
1449 | pindex = pindex - 1; | |
1450 | ||
8b10b465 MG |
1451 | /* |
1452 | * local_lock_irq held so equivalent to spin_lock_irqsave for | |
1453 | * both PREEMPT_RT and non-PREEMPT_RT configurations. | |
1454 | */ | |
1455 | spin_lock(&zone->lock); | |
1456 | isolated_pageblocks = has_isolate_pageblock(zone); | |
1457 | ||
44042b44 | 1458 | while (count > 0) { |
5f8dcc21 | 1459 | struct list_head *list; |
fd56eef2 | 1460 | int nr_pages; |
5f8dcc21 | 1461 | |
fd56eef2 | 1462 | /* Remove pages from lists in a round-robin fashion. */ |
5f8dcc21 | 1463 | do { |
35b6d770 MG |
1464 | if (++pindex > max_pindex) |
1465 | pindex = min_pindex; | |
44042b44 | 1466 | list = &pcp->lists[pindex]; |
35b6d770 MG |
1467 | if (!list_empty(list)) |
1468 | break; | |
1469 | ||
1470 | if (pindex == max_pindex) | |
1471 | max_pindex--; | |
1472 | if (pindex == min_pindex) | |
1473 | min_pindex++; | |
1474 | } while (1); | |
48db57f8 | 1475 | |
44042b44 | 1476 | order = pindex_to_order(pindex); |
fd56eef2 | 1477 | nr_pages = 1 << order; |
44042b44 | 1478 | BUILD_BUG_ON(MAX_ORDER >= (1<<NR_PCP_ORDER_WIDTH)); |
a6f9edd6 | 1479 | do { |
8b10b465 MG |
1480 | int mt; |
1481 | ||
a16601c5 | 1482 | page = list_last_entry(list, struct page, lru); |
8b10b465 MG |
1483 | mt = get_pcppage_migratetype(page); |
1484 | ||
0a5f4e5b | 1485 | /* must delete to avoid corrupting pcp list */ |
a6f9edd6 | 1486 | list_del(&page->lru); |
fd56eef2 MG |
1487 | count -= nr_pages; |
1488 | pcp->count -= nr_pages; | |
aa016d14 | 1489 | |
4db7548c MG |
1490 | if (bulkfree_pcp_prepare(page)) |
1491 | continue; | |
1492 | ||
8b10b465 MG |
1493 | /* MIGRATE_ISOLATE page should not go to pcplists */ |
1494 | VM_BUG_ON_PAGE(is_migrate_isolate(mt), page); | |
1495 | /* Pageblock could have been isolated meanwhile */ | |
1496 | if (unlikely(isolated_pageblocks)) | |
1497 | mt = get_pageblock_migratetype(page); | |
0a5f4e5b | 1498 | |
8b10b465 MG |
1499 | __free_one_page(page, page_to_pfn(page), zone, order, mt, FPI_NONE); |
1500 | trace_mm_page_pcpu_drain(page, order, mt); | |
1501 | } while (count > 0 && !list_empty(list)); | |
0a5f4e5b | 1502 | } |
8b10b465 | 1503 | |
d34b0733 | 1504 | spin_unlock(&zone->lock); |
1da177e4 LT |
1505 | } |
1506 | ||
dc4b0caf MG |
1507 | static void free_one_page(struct zone *zone, |
1508 | struct page *page, unsigned long pfn, | |
7aeb09f9 | 1509 | unsigned int order, |
7fef431b | 1510 | int migratetype, fpi_t fpi_flags) |
1da177e4 | 1511 | { |
df1acc85 MG |
1512 | unsigned long flags; |
1513 | ||
1514 | spin_lock_irqsave(&zone->lock, flags); | |
ad53f92e JK |
1515 | if (unlikely(has_isolate_pageblock(zone) || |
1516 | is_migrate_isolate(migratetype))) { | |
1517 | migratetype = get_pfnblock_migratetype(page, pfn); | |
ad53f92e | 1518 | } |
7fef431b | 1519 | __free_one_page(page, pfn, zone, order, migratetype, fpi_flags); |
df1acc85 | 1520 | spin_unlock_irqrestore(&zone->lock, flags); |
48db57f8 NP |
1521 | } |
1522 | ||
1e8ce83c | 1523 | static void __meminit __init_single_page(struct page *page, unsigned long pfn, |
d0dc12e8 | 1524 | unsigned long zone, int nid) |
1e8ce83c | 1525 | { |
d0dc12e8 | 1526 | mm_zero_struct_page(page); |
1e8ce83c | 1527 | set_page_links(page, zone, nid, pfn); |
1e8ce83c RH |
1528 | init_page_count(page); |
1529 | page_mapcount_reset(page); | |
1530 | page_cpupid_reset_last(page); | |
2813b9c0 | 1531 | page_kasan_tag_reset(page); |
1e8ce83c | 1532 | |
1e8ce83c RH |
1533 | INIT_LIST_HEAD(&page->lru); |
1534 | #ifdef WANT_PAGE_VIRTUAL | |
1535 | /* The shift won't overflow because ZONE_NORMAL is below 4G. */ | |
1536 | if (!is_highmem_idx(zone)) | |
1537 | set_page_address(page, __va(pfn << PAGE_SHIFT)); | |
1538 | #endif | |
1539 | } | |
1540 | ||
7e18adb4 | 1541 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
57148a64 | 1542 | static void __meminit init_reserved_page(unsigned long pfn) |
7e18adb4 MG |
1543 | { |
1544 | pg_data_t *pgdat; | |
1545 | int nid, zid; | |
1546 | ||
1547 | if (!early_page_uninitialised(pfn)) | |
1548 | return; | |
1549 | ||
1550 | nid = early_pfn_to_nid(pfn); | |
1551 | pgdat = NODE_DATA(nid); | |
1552 | ||
1553 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
1554 | struct zone *zone = &pgdat->node_zones[zid]; | |
1555 | ||
86fb05b9 | 1556 | if (zone_spans_pfn(zone, pfn)) |
7e18adb4 MG |
1557 | break; |
1558 | } | |
d0dc12e8 | 1559 | __init_single_page(pfn_to_page(pfn), pfn, zid, nid); |
7e18adb4 MG |
1560 | } |
1561 | #else | |
1562 | static inline void init_reserved_page(unsigned long pfn) | |
1563 | { | |
1564 | } | |
1565 | #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ | |
1566 | ||
92923ca3 NZ |
1567 | /* |
1568 | * Initialised pages do not have PageReserved set. This function is | |
1569 | * called for each range allocated by the bootmem allocator and | |
1570 | * marks the pages PageReserved. The remaining valid pages are later | |
1571 | * sent to the buddy page allocator. | |
1572 | */ | |
4b50bcc7 | 1573 | void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) |
92923ca3 NZ |
1574 | { |
1575 | unsigned long start_pfn = PFN_DOWN(start); | |
1576 | unsigned long end_pfn = PFN_UP(end); | |
1577 | ||
7e18adb4 MG |
1578 | for (; start_pfn < end_pfn; start_pfn++) { |
1579 | if (pfn_valid(start_pfn)) { | |
1580 | struct page *page = pfn_to_page(start_pfn); | |
1581 | ||
1582 | init_reserved_page(start_pfn); | |
1d798ca3 KS |
1583 | |
1584 | /* Avoid false-positive PageTail() */ | |
1585 | INIT_LIST_HEAD(&page->lru); | |
1586 | ||
d483da5b AD |
1587 | /* |
1588 | * no need for atomic set_bit because the struct | |
1589 | * page is not visible yet so nobody should | |
1590 | * access it yet. | |
1591 | */ | |
1592 | __SetPageReserved(page); | |
7e18adb4 MG |
1593 | } |
1594 | } | |
92923ca3 NZ |
1595 | } |
1596 | ||
7fef431b DH |
1597 | static void __free_pages_ok(struct page *page, unsigned int order, |
1598 | fpi_t fpi_flags) | |
ec95f53a | 1599 | { |
d34b0733 | 1600 | unsigned long flags; |
95e34412 | 1601 | int migratetype; |
dc4b0caf | 1602 | unsigned long pfn = page_to_pfn(page); |
56f0e661 | 1603 | struct zone *zone = page_zone(page); |
ec95f53a | 1604 | |
2c335680 | 1605 | if (!free_pages_prepare(page, order, true, fpi_flags)) |
ec95f53a KM |
1606 | return; |
1607 | ||
cfc47a28 | 1608 | migratetype = get_pfnblock_migratetype(page, pfn); |
dbbee9d5 | 1609 | |
56f0e661 | 1610 | spin_lock_irqsave(&zone->lock, flags); |
56f0e661 MG |
1611 | if (unlikely(has_isolate_pageblock(zone) || |
1612 | is_migrate_isolate(migratetype))) { | |
1613 | migratetype = get_pfnblock_migratetype(page, pfn); | |
1614 | } | |
1615 | __free_one_page(page, pfn, zone, order, migratetype, fpi_flags); | |
1616 | spin_unlock_irqrestore(&zone->lock, flags); | |
90249993 | 1617 | |
d34b0733 | 1618 | __count_vm_events(PGFREE, 1 << order); |
1da177e4 LT |
1619 | } |
1620 | ||
a9cd410a | 1621 | void __free_pages_core(struct page *page, unsigned int order) |
a226f6c8 | 1622 | { |
c3993076 | 1623 | unsigned int nr_pages = 1 << order; |
e2d0bd2b | 1624 | struct page *p = page; |
c3993076 | 1625 | unsigned int loop; |
a226f6c8 | 1626 | |
7fef431b DH |
1627 | /* |
1628 | * When initializing the memmap, __init_single_page() sets the refcount | |
1629 | * of all pages to 1 ("allocated"/"not free"). We have to set the | |
1630 | * refcount of all involved pages to 0. | |
1631 | */ | |
e2d0bd2b YL |
1632 | prefetchw(p); |
1633 | for (loop = 0; loop < (nr_pages - 1); loop++, p++) { | |
1634 | prefetchw(p + 1); | |
c3993076 JW |
1635 | __ClearPageReserved(p); |
1636 | set_page_count(p, 0); | |
a226f6c8 | 1637 | } |
e2d0bd2b YL |
1638 | __ClearPageReserved(p); |
1639 | set_page_count(p, 0); | |
c3993076 | 1640 | |
9705bea5 | 1641 | atomic_long_add(nr_pages, &page_zone(page)->managed_pages); |
7fef431b DH |
1642 | |
1643 | /* | |
1644 | * Bypass PCP and place fresh pages right to the tail, primarily | |
1645 | * relevant for memory onlining. | |
1646 | */ | |
2c335680 | 1647 | __free_pages_ok(page, order, FPI_TO_TAIL | FPI_SKIP_KASAN_POISON); |
a226f6c8 DH |
1648 | } |
1649 | ||
a9ee6cf5 | 1650 | #ifdef CONFIG_NUMA |
7ace9917 | 1651 | |
03e92a5e MR |
1652 | /* |
1653 | * During memory init memblocks map pfns to nids. The search is expensive and | |
1654 | * this caches recent lookups. The implementation of __early_pfn_to_nid | |
1655 | * treats start/end as pfns. | |
1656 | */ | |
1657 | struct mminit_pfnnid_cache { | |
1658 | unsigned long last_start; | |
1659 | unsigned long last_end; | |
1660 | int last_nid; | |
1661 | }; | |
75a592a4 | 1662 | |
03e92a5e | 1663 | static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata; |
6f24fbd3 MR |
1664 | |
1665 | /* | |
1666 | * Required by SPARSEMEM. Given a PFN, return what node the PFN is on. | |
1667 | */ | |
03e92a5e | 1668 | static int __meminit __early_pfn_to_nid(unsigned long pfn, |
6f24fbd3 | 1669 | struct mminit_pfnnid_cache *state) |
75a592a4 | 1670 | { |
6f24fbd3 | 1671 | unsigned long start_pfn, end_pfn; |
75a592a4 MG |
1672 | int nid; |
1673 | ||
6f24fbd3 MR |
1674 | if (state->last_start <= pfn && pfn < state->last_end) |
1675 | return state->last_nid; | |
1676 | ||
1677 | nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn); | |
1678 | if (nid != NUMA_NO_NODE) { | |
1679 | state->last_start = start_pfn; | |
1680 | state->last_end = end_pfn; | |
1681 | state->last_nid = nid; | |
1682 | } | |
7ace9917 MG |
1683 | |
1684 | return nid; | |
75a592a4 | 1685 | } |
75a592a4 | 1686 | |
75a592a4 | 1687 | int __meminit early_pfn_to_nid(unsigned long pfn) |
75a592a4 | 1688 | { |
7ace9917 | 1689 | static DEFINE_SPINLOCK(early_pfn_lock); |
75a592a4 MG |
1690 | int nid; |
1691 | ||
7ace9917 | 1692 | spin_lock(&early_pfn_lock); |
56ec43d8 | 1693 | nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache); |
7ace9917 | 1694 | if (nid < 0) |
e4568d38 | 1695 | nid = first_online_node; |
7ace9917 | 1696 | spin_unlock(&early_pfn_lock); |
75a592a4 | 1697 | |
7ace9917 | 1698 | return nid; |
75a592a4 | 1699 | } |
a9ee6cf5 | 1700 | #endif /* CONFIG_NUMA */ |
75a592a4 | 1701 | |
7c2ee349 | 1702 | void __init memblock_free_pages(struct page *page, unsigned long pfn, |
3a80a7fa MG |
1703 | unsigned int order) |
1704 | { | |
1705 | if (early_page_uninitialised(pfn)) | |
1706 | return; | |
a9cd410a | 1707 | __free_pages_core(page, order); |
3a80a7fa MG |
1708 | } |
1709 | ||
7cf91a98 JK |
1710 | /* |
1711 | * Check that the whole (or subset of) a pageblock given by the interval of | |
1712 | * [start_pfn, end_pfn) is valid and within the same zone, before scanning it | |
859a85dd | 1713 | * with the migration of free compaction scanner. |
7cf91a98 JK |
1714 | * |
1715 | * Return struct page pointer of start_pfn, or NULL if checks were not passed. | |
1716 | * | |
1717 | * It's possible on some configurations to have a setup like node0 node1 node0 | |
1718 | * i.e. it's possible that all pages within a zones range of pages do not | |
1719 | * belong to a single zone. We assume that a border between node0 and node1 | |
1720 | * can occur within a single pageblock, but not a node0 node1 node0 | |
1721 | * interleaving within a single pageblock. It is therefore sufficient to check | |
1722 | * the first and last page of a pageblock and avoid checking each individual | |
1723 | * page in a pageblock. | |
1724 | */ | |
1725 | struct page *__pageblock_pfn_to_page(unsigned long start_pfn, | |
1726 | unsigned long end_pfn, struct zone *zone) | |
1727 | { | |
1728 | struct page *start_page; | |
1729 | struct page *end_page; | |
1730 | ||
1731 | /* end_pfn is one past the range we are checking */ | |
1732 | end_pfn--; | |
1733 | ||
1734 | if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) | |
1735 | return NULL; | |
1736 | ||
2d070eab MH |
1737 | start_page = pfn_to_online_page(start_pfn); |
1738 | if (!start_page) | |
1739 | return NULL; | |
7cf91a98 JK |
1740 | |
1741 | if (page_zone(start_page) != zone) | |
1742 | return NULL; | |
1743 | ||
1744 | end_page = pfn_to_page(end_pfn); | |
1745 | ||
1746 | /* This gives a shorter code than deriving page_zone(end_page) */ | |
1747 | if (page_zone_id(start_page) != page_zone_id(end_page)) | |
1748 | return NULL; | |
1749 | ||
1750 | return start_page; | |
1751 | } | |
1752 | ||
1753 | void set_zone_contiguous(struct zone *zone) | |
1754 | { | |
1755 | unsigned long block_start_pfn = zone->zone_start_pfn; | |
1756 | unsigned long block_end_pfn; | |
1757 | ||
1758 | block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages); | |
1759 | for (; block_start_pfn < zone_end_pfn(zone); | |
1760 | block_start_pfn = block_end_pfn, | |
1761 | block_end_pfn += pageblock_nr_pages) { | |
1762 | ||
1763 | block_end_pfn = min(block_end_pfn, zone_end_pfn(zone)); | |
1764 | ||
1765 | if (!__pageblock_pfn_to_page(block_start_pfn, | |
1766 | block_end_pfn, zone)) | |
1767 | return; | |
e84fe99b | 1768 | cond_resched(); |
7cf91a98 JK |
1769 | } |
1770 | ||
1771 | /* We confirm that there is no hole */ | |
1772 | zone->contiguous = true; | |
1773 | } | |
1774 | ||
1775 | void clear_zone_contiguous(struct zone *zone) | |
1776 | { | |
1777 | zone->contiguous = false; | |
1778 | } | |
1779 | ||
7e18adb4 | 1780 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
2f47a91f PT |
1781 | static void __init deferred_free_range(unsigned long pfn, |
1782 | unsigned long nr_pages) | |
a4de83dd | 1783 | { |
2f47a91f PT |
1784 | struct page *page; |
1785 | unsigned long i; | |
a4de83dd | 1786 | |
2f47a91f | 1787 | if (!nr_pages) |
a4de83dd MG |
1788 | return; |
1789 | ||
2f47a91f PT |
1790 | page = pfn_to_page(pfn); |
1791 | ||
a4de83dd | 1792 | /* Free a large naturally-aligned chunk if possible */ |
e780149b XQ |
1793 | if (nr_pages == pageblock_nr_pages && |
1794 | (pfn & (pageblock_nr_pages - 1)) == 0) { | |
ac5d2539 | 1795 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
a9cd410a | 1796 | __free_pages_core(page, pageblock_order); |
a4de83dd MG |
1797 | return; |
1798 | } | |
1799 | ||
e780149b XQ |
1800 | for (i = 0; i < nr_pages; i++, page++, pfn++) { |
1801 | if ((pfn & (pageblock_nr_pages - 1)) == 0) | |
1802 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); | |
a9cd410a | 1803 | __free_pages_core(page, 0); |
e780149b | 1804 | } |
a4de83dd MG |
1805 | } |
1806 | ||
d3cd131d NS |
1807 | /* Completion tracking for deferred_init_memmap() threads */ |
1808 | static atomic_t pgdat_init_n_undone __initdata; | |
1809 | static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp); | |
1810 | ||
1811 | static inline void __init pgdat_init_report_one_done(void) | |
1812 | { | |
1813 | if (atomic_dec_and_test(&pgdat_init_n_undone)) | |
1814 | complete(&pgdat_init_all_done_comp); | |
1815 | } | |
0e1cc95b | 1816 | |
2f47a91f | 1817 | /* |
80b1f41c PT |
1818 | * Returns true if page needs to be initialized or freed to buddy allocator. |
1819 | * | |
1820 | * First we check if pfn is valid on architectures where it is possible to have | |
1821 | * holes within pageblock_nr_pages. On systems where it is not possible, this | |
1822 | * function is optimized out. | |
1823 | * | |
1824 | * Then, we check if a current large page is valid by only checking the validity | |
1825 | * of the head pfn. | |
2f47a91f | 1826 | */ |
56ec43d8 | 1827 | static inline bool __init deferred_pfn_valid(unsigned long pfn) |
2f47a91f | 1828 | { |
80b1f41c PT |
1829 | if (!(pfn & (pageblock_nr_pages - 1)) && !pfn_valid(pfn)) |
1830 | return false; | |
80b1f41c PT |
1831 | return true; |
1832 | } | |
2f47a91f | 1833 | |
80b1f41c PT |
1834 | /* |
1835 | * Free pages to buddy allocator. Try to free aligned pages in | |
1836 | * pageblock_nr_pages sizes. | |
1837 | */ | |
56ec43d8 | 1838 | static void __init deferred_free_pages(unsigned long pfn, |
80b1f41c PT |
1839 | unsigned long end_pfn) |
1840 | { | |
80b1f41c PT |
1841 | unsigned long nr_pgmask = pageblock_nr_pages - 1; |
1842 | unsigned long nr_free = 0; | |
2f47a91f | 1843 | |
80b1f41c | 1844 | for (; pfn < end_pfn; pfn++) { |
56ec43d8 | 1845 | if (!deferred_pfn_valid(pfn)) { |
80b1f41c PT |
1846 | deferred_free_range(pfn - nr_free, nr_free); |
1847 | nr_free = 0; | |
1848 | } else if (!(pfn & nr_pgmask)) { | |
1849 | deferred_free_range(pfn - nr_free, nr_free); | |
1850 | nr_free = 1; | |
80b1f41c PT |
1851 | } else { |
1852 | nr_free++; | |
1853 | } | |
1854 | } | |
1855 | /* Free the last block of pages to allocator */ | |
1856 | deferred_free_range(pfn - nr_free, nr_free); | |
2f47a91f PT |
1857 | } |
1858 | ||
80b1f41c PT |
1859 | /* |
1860 | * Initialize struct pages. We minimize pfn page lookups and scheduler checks | |
1861 | * by performing it only once every pageblock_nr_pages. | |
1862 | * Return number of pages initialized. | |
1863 | */ | |
56ec43d8 | 1864 | static unsigned long __init deferred_init_pages(struct zone *zone, |
80b1f41c PT |
1865 | unsigned long pfn, |
1866 | unsigned long end_pfn) | |
2f47a91f | 1867 | { |
2f47a91f | 1868 | unsigned long nr_pgmask = pageblock_nr_pages - 1; |
56ec43d8 | 1869 | int nid = zone_to_nid(zone); |
2f47a91f | 1870 | unsigned long nr_pages = 0; |
56ec43d8 | 1871 | int zid = zone_idx(zone); |
2f47a91f | 1872 | struct page *page = NULL; |
2f47a91f | 1873 | |
80b1f41c | 1874 | for (; pfn < end_pfn; pfn++) { |
56ec43d8 | 1875 | if (!deferred_pfn_valid(pfn)) { |
80b1f41c | 1876 | page = NULL; |
2f47a91f | 1877 | continue; |
80b1f41c | 1878 | } else if (!page || !(pfn & nr_pgmask)) { |
2f47a91f | 1879 | page = pfn_to_page(pfn); |
80b1f41c PT |
1880 | } else { |
1881 | page++; | |
2f47a91f | 1882 | } |
d0dc12e8 | 1883 | __init_single_page(page, pfn, zid, nid); |
80b1f41c | 1884 | nr_pages++; |
2f47a91f | 1885 | } |
80b1f41c | 1886 | return (nr_pages); |
2f47a91f PT |
1887 | } |
1888 | ||
0e56acae AD |
1889 | /* |
1890 | * This function is meant to pre-load the iterator for the zone init. | |
1891 | * Specifically it walks through the ranges until we are caught up to the | |
1892 | * first_init_pfn value and exits there. If we never encounter the value we | |
1893 | * return false indicating there are no valid ranges left. | |
1894 | */ | |
1895 | static bool __init | |
1896 | deferred_init_mem_pfn_range_in_zone(u64 *i, struct zone *zone, | |
1897 | unsigned long *spfn, unsigned long *epfn, | |
1898 | unsigned long first_init_pfn) | |
1899 | { | |
1900 | u64 j; | |
1901 | ||
1902 | /* | |
1903 | * Start out by walking through the ranges in this zone that have | |
1904 | * already been initialized. We don't need to do anything with them | |
1905 | * so we just need to flush them out of the system. | |
1906 | */ | |
1907 | for_each_free_mem_pfn_range_in_zone(j, zone, spfn, epfn) { | |
1908 | if (*epfn <= first_init_pfn) | |
1909 | continue; | |
1910 | if (*spfn < first_init_pfn) | |
1911 | *spfn = first_init_pfn; | |
1912 | *i = j; | |
1913 | return true; | |
1914 | } | |
1915 | ||
1916 | return false; | |
1917 | } | |
1918 | ||
1919 | /* | |
1920 | * Initialize and free pages. We do it in two loops: first we initialize | |
1921 | * struct page, then free to buddy allocator, because while we are | |
1922 | * freeing pages we can access pages that are ahead (computing buddy | |
1923 | * page in __free_one_page()). | |
1924 | * | |
1925 | * In order to try and keep some memory in the cache we have the loop | |
1926 | * broken along max page order boundaries. This way we will not cause | |
1927 | * any issues with the buddy page computation. | |
1928 | */ | |
1929 | static unsigned long __init | |
1930 | deferred_init_maxorder(u64 *i, struct zone *zone, unsigned long *start_pfn, | |
1931 | unsigned long *end_pfn) | |
1932 | { | |
1933 | unsigned long mo_pfn = ALIGN(*start_pfn + 1, MAX_ORDER_NR_PAGES); | |
1934 | unsigned long spfn = *start_pfn, epfn = *end_pfn; | |
1935 | unsigned long nr_pages = 0; | |
1936 | u64 j = *i; | |
1937 | ||
1938 | /* First we loop through and initialize the page values */ | |
1939 | for_each_free_mem_pfn_range_in_zone_from(j, zone, start_pfn, end_pfn) { | |
1940 | unsigned long t; | |
1941 | ||
1942 | if (mo_pfn <= *start_pfn) | |
1943 | break; | |
1944 | ||
1945 | t = min(mo_pfn, *end_pfn); | |
1946 | nr_pages += deferred_init_pages(zone, *start_pfn, t); | |
1947 | ||
1948 | if (mo_pfn < *end_pfn) { | |
1949 | *start_pfn = mo_pfn; | |
1950 | break; | |
1951 | } | |
1952 | } | |
1953 | ||
1954 | /* Reset values and now loop through freeing pages as needed */ | |
1955 | swap(j, *i); | |
1956 | ||
1957 | for_each_free_mem_pfn_range_in_zone_from(j, zone, &spfn, &epfn) { | |
1958 | unsigned long t; | |
1959 | ||
1960 | if (mo_pfn <= spfn) | |
1961 | break; | |
1962 | ||
1963 | t = min(mo_pfn, epfn); | |
1964 | deferred_free_pages(spfn, t); | |
1965 | ||
1966 | if (mo_pfn <= epfn) | |
1967 | break; | |
1968 | } | |
1969 | ||
1970 | return nr_pages; | |
1971 | } | |
1972 | ||
e4443149 DJ |
1973 | static void __init |
1974 | deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn, | |
1975 | void *arg) | |
1976 | { | |
1977 | unsigned long spfn, epfn; | |
1978 | struct zone *zone = arg; | |
1979 | u64 i; | |
1980 | ||
1981 | deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn, start_pfn); | |
1982 | ||
1983 | /* | |
1984 | * Initialize and free pages in MAX_ORDER sized increments so that we | |
1985 | * can avoid introducing any issues with the buddy allocator. | |
1986 | */ | |
1987 | while (spfn < end_pfn) { | |
1988 | deferred_init_maxorder(&i, zone, &spfn, &epfn); | |
1989 | cond_resched(); | |
1990 | } | |
1991 | } | |
1992 | ||
ecd09650 DJ |
1993 | /* An arch may override for more concurrency. */ |
1994 | __weak int __init | |
1995 | deferred_page_init_max_threads(const struct cpumask *node_cpumask) | |
1996 | { | |
1997 | return 1; | |
1998 | } | |
1999 | ||
7e18adb4 | 2000 | /* Initialise remaining memory on a node */ |
0e1cc95b | 2001 | static int __init deferred_init_memmap(void *data) |
7e18adb4 | 2002 | { |
0e1cc95b | 2003 | pg_data_t *pgdat = data; |
0e56acae | 2004 | const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
89c7c402 | 2005 | unsigned long spfn = 0, epfn = 0; |
0e56acae | 2006 | unsigned long first_init_pfn, flags; |
7e18adb4 | 2007 | unsigned long start = jiffies; |
7e18adb4 | 2008 | struct zone *zone; |
e4443149 | 2009 | int zid, max_threads; |
2f47a91f | 2010 | u64 i; |
7e18adb4 | 2011 | |
3a2d7fa8 PT |
2012 | /* Bind memory initialisation thread to a local node if possible */ |
2013 | if (!cpumask_empty(cpumask)) | |
2014 | set_cpus_allowed_ptr(current, cpumask); | |
2015 | ||
2016 | pgdat_resize_lock(pgdat, &flags); | |
2017 | first_init_pfn = pgdat->first_deferred_pfn; | |
0e1cc95b | 2018 | if (first_init_pfn == ULONG_MAX) { |
3a2d7fa8 | 2019 | pgdat_resize_unlock(pgdat, &flags); |
d3cd131d | 2020 | pgdat_init_report_one_done(); |
0e1cc95b MG |
2021 | return 0; |
2022 | } | |
2023 | ||
7e18adb4 MG |
2024 | /* Sanity check boundaries */ |
2025 | BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn); | |
2026 | BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat)); | |
2027 | pgdat->first_deferred_pfn = ULONG_MAX; | |
2028 | ||
3d060856 PT |
2029 | /* |
2030 | * Once we unlock here, the zone cannot be grown anymore, thus if an | |
2031 | * interrupt thread must allocate this early in boot, zone must be | |
2032 | * pre-grown prior to start of deferred page initialization. | |
2033 | */ | |
2034 | pgdat_resize_unlock(pgdat, &flags); | |
2035 | ||
7e18adb4 MG |
2036 | /* Only the highest zone is deferred so find it */ |
2037 | for (zid = 0; zid < MAX_NR_ZONES; zid++) { | |
2038 | zone = pgdat->node_zones + zid; | |
2039 | if (first_init_pfn < zone_end_pfn(zone)) | |
2040 | break; | |
2041 | } | |
0e56acae AD |
2042 | |
2043 | /* If the zone is empty somebody else may have cleared out the zone */ | |
2044 | if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn, | |
2045 | first_init_pfn)) | |
2046 | goto zone_empty; | |
7e18adb4 | 2047 | |
ecd09650 | 2048 | max_threads = deferred_page_init_max_threads(cpumask); |
7e18adb4 | 2049 | |
117003c3 | 2050 | while (spfn < epfn) { |
e4443149 DJ |
2051 | unsigned long epfn_align = ALIGN(epfn, PAGES_PER_SECTION); |
2052 | struct padata_mt_job job = { | |
2053 | .thread_fn = deferred_init_memmap_chunk, | |
2054 | .fn_arg = zone, | |
2055 | .start = spfn, | |
2056 | .size = epfn_align - spfn, | |
2057 | .align = PAGES_PER_SECTION, | |
2058 | .min_chunk = PAGES_PER_SECTION, | |
2059 | .max_threads = max_threads, | |
2060 | }; | |
2061 | ||
2062 | padata_do_multithreaded(&job); | |
2063 | deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn, | |
2064 | epfn_align); | |
117003c3 | 2065 | } |
0e56acae | 2066 | zone_empty: |
7e18adb4 MG |
2067 | /* Sanity check that the next zone really is unpopulated */ |
2068 | WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone)); | |
2069 | ||
89c7c402 DJ |
2070 | pr_info("node %d deferred pages initialised in %ums\n", |
2071 | pgdat->node_id, jiffies_to_msecs(jiffies - start)); | |
d3cd131d NS |
2072 | |
2073 | pgdat_init_report_one_done(); | |
0e1cc95b MG |
2074 | return 0; |
2075 | } | |
c9e97a19 | 2076 | |
c9e97a19 PT |
2077 | /* |
2078 | * If this zone has deferred pages, try to grow it by initializing enough | |
2079 | * deferred pages to satisfy the allocation specified by order, rounded up to | |
2080 | * the nearest PAGES_PER_SECTION boundary. So we're adding memory in increments | |
2081 | * of SECTION_SIZE bytes by initializing struct pages in increments of | |
2082 | * PAGES_PER_SECTION * sizeof(struct page) bytes. | |
2083 | * | |
2084 | * Return true when zone was grown, otherwise return false. We return true even | |
2085 | * when we grow less than requested, to let the caller decide if there are | |
2086 | * enough pages to satisfy the allocation. | |
2087 | * | |
2088 | * Note: We use noinline because this function is needed only during boot, and | |
2089 | * it is called from a __ref function _deferred_grow_zone. This way we are | |
2090 | * making sure that it is not inlined into permanent text section. | |
2091 | */ | |
2092 | static noinline bool __init | |
2093 | deferred_grow_zone(struct zone *zone, unsigned int order) | |
2094 | { | |
c9e97a19 | 2095 | unsigned long nr_pages_needed = ALIGN(1 << order, PAGES_PER_SECTION); |
837566e7 | 2096 | pg_data_t *pgdat = zone->zone_pgdat; |
c9e97a19 | 2097 | unsigned long first_deferred_pfn = pgdat->first_deferred_pfn; |
0e56acae AD |
2098 | unsigned long spfn, epfn, flags; |
2099 | unsigned long nr_pages = 0; | |
c9e97a19 PT |
2100 | u64 i; |
2101 | ||
2102 | /* Only the last zone may have deferred pages */ | |
2103 | if (zone_end_pfn(zone) != pgdat_end_pfn(pgdat)) | |
2104 | return false; | |
2105 | ||
2106 | pgdat_resize_lock(pgdat, &flags); | |
2107 | ||
c9e97a19 PT |
2108 | /* |
2109 | * If someone grew this zone while we were waiting for spinlock, return | |
2110 | * true, as there might be enough pages already. | |
2111 | */ | |
2112 | if (first_deferred_pfn != pgdat->first_deferred_pfn) { | |
2113 | pgdat_resize_unlock(pgdat, &flags); | |
2114 | return true; | |
2115 | } | |
2116 | ||
0e56acae AD |
2117 | /* If the zone is empty somebody else may have cleared out the zone */ |
2118 | if (!deferred_init_mem_pfn_range_in_zone(&i, zone, &spfn, &epfn, | |
2119 | first_deferred_pfn)) { | |
2120 | pgdat->first_deferred_pfn = ULONG_MAX; | |
c9e97a19 | 2121 | pgdat_resize_unlock(pgdat, &flags); |
b9705d87 JG |
2122 | /* Retry only once. */ |
2123 | return first_deferred_pfn != ULONG_MAX; | |
c9e97a19 PT |
2124 | } |
2125 | ||
0e56acae AD |
2126 | /* |
2127 | * Initialize and free pages in MAX_ORDER sized increments so | |
2128 | * that we can avoid introducing any issues with the buddy | |
2129 | * allocator. | |
2130 | */ | |
2131 | while (spfn < epfn) { | |
2132 | /* update our first deferred PFN for this section */ | |
2133 | first_deferred_pfn = spfn; | |
2134 | ||
2135 | nr_pages += deferred_init_maxorder(&i, zone, &spfn, &epfn); | |
117003c3 | 2136 | touch_nmi_watchdog(); |
c9e97a19 | 2137 | |
0e56acae AD |
2138 | /* We should only stop along section boundaries */ |
2139 | if ((first_deferred_pfn ^ spfn) < PAGES_PER_SECTION) | |
2140 | continue; | |
c9e97a19 | 2141 | |
0e56acae | 2142 | /* If our quota has been met we can stop here */ |
c9e97a19 PT |
2143 | if (nr_pages >= nr_pages_needed) |
2144 | break; | |
2145 | } | |
2146 | ||
0e56acae | 2147 | pgdat->first_deferred_pfn = spfn; |
c9e97a19 PT |
2148 | pgdat_resize_unlock(pgdat, &flags); |
2149 | ||
2150 | return nr_pages > 0; | |
2151 | } | |
2152 | ||
2153 | /* | |
2154 | * deferred_grow_zone() is __init, but it is called from | |
2155 | * get_page_from_freelist() during early boot until deferred_pages permanently | |
2156 | * disables this call. This is why we have refdata wrapper to avoid warning, | |
2157 | * and to ensure that the function body gets unloaded. | |
2158 | */ | |
2159 | static bool __ref | |
2160 | _deferred_grow_zone(struct zone *zone, unsigned int order) | |
2161 | { | |
2162 | return deferred_grow_zone(zone, order); | |
2163 | } | |
2164 | ||
7cf91a98 | 2165 | #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
0e1cc95b MG |
2166 | |
2167 | void __init page_alloc_init_late(void) | |
2168 | { | |
7cf91a98 | 2169 | struct zone *zone; |
e900a918 | 2170 | int nid; |
7cf91a98 JK |
2171 | |
2172 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT | |
0e1cc95b | 2173 | |
d3cd131d NS |
2174 | /* There will be num_node_state(N_MEMORY) threads */ |
2175 | atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY)); | |
0e1cc95b | 2176 | for_each_node_state(nid, N_MEMORY) { |
0e1cc95b MG |
2177 | kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid); |
2178 | } | |
2179 | ||
2180 | /* Block until all are initialised */ | |
d3cd131d | 2181 | wait_for_completion(&pgdat_init_all_done_comp); |
4248b0da | 2182 | |
c9e97a19 PT |
2183 | /* |
2184 | * We initialized the rest of the deferred pages. Permanently disable | |
2185 | * on-demand struct page initialization. | |
2186 | */ | |
2187 | static_branch_disable(&deferred_pages); | |
2188 | ||
4248b0da MG |
2189 | /* Reinit limits that are based on free pages after the kernel is up */ |
2190 | files_maxfiles_init(); | |
7cf91a98 | 2191 | #endif |
350e88ba | 2192 | |
ba8f3587 LF |
2193 | buffer_init(); |
2194 | ||
3010f876 PT |
2195 | /* Discard memblock private memory */ |
2196 | memblock_discard(); | |
7cf91a98 | 2197 | |
e900a918 DW |
2198 | for_each_node_state(nid, N_MEMORY) |
2199 | shuffle_free_memory(NODE_DATA(nid)); | |
2200 | ||
7cf91a98 JK |
2201 | for_each_populated_zone(zone) |
2202 | set_zone_contiguous(zone); | |
7e18adb4 | 2203 | } |
7e18adb4 | 2204 | |
47118af0 | 2205 | #ifdef CONFIG_CMA |
9cf510a5 | 2206 | /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ |
47118af0 MN |
2207 | void __init init_cma_reserved_pageblock(struct page *page) |
2208 | { | |
2209 | unsigned i = pageblock_nr_pages; | |
2210 | struct page *p = page; | |
2211 | ||
2212 | do { | |
2213 | __ClearPageReserved(p); | |
2214 | set_page_count(p, 0); | |
d883c6cf | 2215 | } while (++p, --i); |
47118af0 | 2216 | |
47118af0 | 2217 | set_pageblock_migratetype(page, MIGRATE_CMA); |
b3d40a2b DH |
2218 | set_page_refcounted(page); |
2219 | __free_pages(page, pageblock_order); | |
dc78327c | 2220 | |
3dcc0571 | 2221 | adjust_managed_page_count(page, pageblock_nr_pages); |
3c381db1 | 2222 | page_zone(page)->cma_pages += pageblock_nr_pages; |
47118af0 MN |
2223 | } |
2224 | #endif | |
1da177e4 LT |
2225 | |
2226 | /* | |
2227 | * The order of subdivision here is critical for the IO subsystem. | |
2228 | * Please do not alter this order without good reasons and regression | |
2229 | * testing. Specifically, as large blocks of memory are subdivided, | |
2230 | * the order in which smaller blocks are delivered depends on the order | |
2231 | * they're subdivided in this function. This is the primary factor | |
2232 | * influencing the order in which pages are delivered to the IO | |
2233 | * subsystem according to empirical testing, and this is also justified | |
2234 | * by considering the behavior of a buddy system containing a single | |
2235 | * large block of memory acted on by a series of small allocations. | |
2236 | * This behavior is a critical factor in sglist merging's success. | |
2237 | * | |
6d49e352 | 2238 | * -- nyc |
1da177e4 | 2239 | */ |
085cc7d5 | 2240 | static inline void expand(struct zone *zone, struct page *page, |
6ab01363 | 2241 | int low, int high, int migratetype) |
1da177e4 LT |
2242 | { |
2243 | unsigned long size = 1 << high; | |
2244 | ||
2245 | while (high > low) { | |
1da177e4 LT |
2246 | high--; |
2247 | size >>= 1; | |
309381fe | 2248 | VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); |
c0a32fc5 | 2249 | |
acbc15a4 JK |
2250 | /* |
2251 | * Mark as guard pages (or page), that will allow to | |
2252 | * merge back to allocator when buddy will be freed. | |
2253 | * Corresponding page table entries will not be touched, | |
2254 | * pages will stay not present in virtual address space | |
2255 | */ | |
2256 | if (set_page_guard(zone, &page[size], high, migratetype)) | |
c0a32fc5 | 2257 | continue; |
acbc15a4 | 2258 | |
6ab01363 | 2259 | add_to_free_list(&page[size], zone, high, migratetype); |
ab130f91 | 2260 | set_buddy_order(&page[size], high); |
1da177e4 | 2261 | } |
1da177e4 LT |
2262 | } |
2263 | ||
4e611801 | 2264 | static void check_new_page_bad(struct page *page) |
1da177e4 | 2265 | { |
f4c18e6f | 2266 | if (unlikely(page->flags & __PG_HWPOISON)) { |
e570f56c NH |
2267 | /* Don't complain about hwpoisoned pages */ |
2268 | page_mapcount_reset(page); /* remove PageBuddy */ | |
2269 | return; | |
f4c18e6f | 2270 | } |
58b7f119 WY |
2271 | |
2272 | bad_page(page, | |
2273 | page_bad_reason(page, PAGE_FLAGS_CHECK_AT_PREP)); | |
4e611801 VB |
2274 | } |
2275 | ||
2276 | /* | |
2277 | * This page is about to be returned from the page allocator | |
2278 | */ | |
2279 | static inline int check_new_page(struct page *page) | |
2280 | { | |
2281 | if (likely(page_expected_state(page, | |
2282 | PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON))) | |
2283 | return 0; | |
2284 | ||
2285 | check_new_page_bad(page); | |
2286 | return 1; | |
2a7684a2 WF |
2287 | } |
2288 | ||
77fe7f13 MG |
2289 | static bool check_new_pages(struct page *page, unsigned int order) |
2290 | { | |
2291 | int i; | |
2292 | for (i = 0; i < (1 << order); i++) { | |
2293 | struct page *p = page + i; | |
2294 | ||
2295 | if (unlikely(check_new_page(p))) | |
2296 | return true; | |
2297 | } | |
2298 | ||
2299 | return false; | |
2300 | } | |
2301 | ||
479f854a | 2302 | #ifdef CONFIG_DEBUG_VM |
4462b32c VB |
2303 | /* |
2304 | * With DEBUG_VM enabled, order-0 pages are checked for expected state when | |
2305 | * being allocated from pcp lists. With debug_pagealloc also enabled, they are | |
2306 | * also checked when pcp lists are refilled from the free lists. | |
2307 | */ | |
77fe7f13 | 2308 | static inline bool check_pcp_refill(struct page *page, unsigned int order) |
479f854a | 2309 | { |
8e57f8ac | 2310 | if (debug_pagealloc_enabled_static()) |
77fe7f13 | 2311 | return check_new_pages(page, order); |
4462b32c VB |
2312 | else |
2313 | return false; | |
479f854a MG |
2314 | } |
2315 | ||
77fe7f13 | 2316 | static inline bool check_new_pcp(struct page *page, unsigned int order) |
479f854a | 2317 | { |
77fe7f13 | 2318 | return check_new_pages(page, order); |
479f854a MG |
2319 | } |
2320 | #else | |
4462b32c VB |
2321 | /* |
2322 | * With DEBUG_VM disabled, free order-0 pages are checked for expected state | |
2323 | * when pcp lists are being refilled from the free lists. With debug_pagealloc | |
2324 | * enabled, they are also checked when being allocated from the pcp lists. | |
2325 | */ | |
77fe7f13 | 2326 | static inline bool check_pcp_refill(struct page *page, unsigned int order) |
479f854a | 2327 | { |
77fe7f13 | 2328 | return check_new_pages(page, order); |
479f854a | 2329 | } |
77fe7f13 | 2330 | static inline bool check_new_pcp(struct page *page, unsigned int order) |
479f854a | 2331 | { |
8e57f8ac | 2332 | if (debug_pagealloc_enabled_static()) |
77fe7f13 | 2333 | return check_new_pages(page, order); |
4462b32c VB |
2334 | else |
2335 | return false; | |
479f854a MG |
2336 | } |
2337 | #endif /* CONFIG_DEBUG_VM */ | |
2338 | ||
53ae233c AK |
2339 | static inline bool should_skip_kasan_unpoison(gfp_t flags, bool init_tags) |
2340 | { | |
2341 | /* Don't skip if a software KASAN mode is enabled. */ | |
2342 | if (IS_ENABLED(CONFIG_KASAN_GENERIC) || | |
2343 | IS_ENABLED(CONFIG_KASAN_SW_TAGS)) | |
2344 | return false; | |
2345 | ||
2346 | /* Skip, if hardware tag-based KASAN is not enabled. */ | |
2347 | if (!kasan_hw_tags_enabled()) | |
2348 | return true; | |
2349 | ||
2350 | /* | |
2351 | * With hardware tag-based KASAN enabled, skip if either: | |
2352 | * | |
2353 | * 1. Memory tags have already been cleared via tag_clear_highpage(). | |
2354 | * 2. Skipping has been requested via __GFP_SKIP_KASAN_UNPOISON. | |
2355 | */ | |
2356 | return init_tags || (flags & __GFP_SKIP_KASAN_UNPOISON); | |
2357 | } | |
2358 | ||
9353ffa6 AK |
2359 | static inline bool should_skip_init(gfp_t flags) |
2360 | { | |
2361 | /* Don't skip, if hardware tag-based KASAN is not enabled. */ | |
2362 | if (!kasan_hw_tags_enabled()) | |
2363 | return false; | |
2364 | ||
2365 | /* For hardware tag-based KASAN, skip if requested. */ | |
2366 | return (flags & __GFP_SKIP_ZERO); | |
2367 | } | |
2368 | ||
46f24fd8 JK |
2369 | inline void post_alloc_hook(struct page *page, unsigned int order, |
2370 | gfp_t gfp_flags) | |
2371 | { | |
9353ffa6 AK |
2372 | bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) && |
2373 | !should_skip_init(gfp_flags); | |
b42090ae AK |
2374 | bool init_tags = init && (gfp_flags & __GFP_ZEROTAGS); |
2375 | ||
46f24fd8 JK |
2376 | set_page_private(page, 0); |
2377 | set_page_refcounted(page); | |
2378 | ||
2379 | arch_alloc_page(page, order); | |
77bc7fd6 | 2380 | debug_pagealloc_map_pages(page, 1 << order); |
1bb5eab3 AK |
2381 | |
2382 | /* | |
2383 | * Page unpoisoning must happen before memory initialization. | |
2384 | * Otherwise, the poison pattern will be overwritten for __GFP_ZERO | |
2385 | * allocations and the page unpoisoning code will complain. | |
2386 | */ | |
8db26a3d | 2387 | kernel_unpoison_pages(page, 1 << order); |
862b6dee | 2388 | |
1bb5eab3 AK |
2389 | /* |
2390 | * As memory initialization might be integrated into KASAN, | |
b42090ae | 2391 | * KASAN unpoisoning and memory initializion code must be |
1bb5eab3 AK |
2392 | * kept together to avoid discrepancies in behavior. |
2393 | */ | |
9294b128 AK |
2394 | |
2395 | /* | |
2396 | * If memory tags should be zeroed (which happens only when memory | |
2397 | * should be initialized as well). | |
2398 | */ | |
2399 | if (init_tags) { | |
2400 | int i; | |
2401 | ||
2402 | /* Initialize both memory and tags. */ | |
2403 | for (i = 0; i != 1 << order; ++i) | |
2404 | tag_clear_highpage(page + i); | |
2405 | ||
2406 | /* Note that memory is already initialized by the loop above. */ | |
2407 | init = false; | |
2408 | } | |
53ae233c AK |
2409 | if (!should_skip_kasan_unpoison(gfp_flags, init_tags)) { |
2410 | /* Unpoison shadow memory or set memory tags. */ | |
e9d0ca92 | 2411 | kasan_unpoison_pages(page, order, init); |
7e3cbba6 | 2412 | |
e9d0ca92 AK |
2413 | /* Note that memory is already initialized by KASAN. */ |
2414 | if (kasan_has_integrated_init()) | |
7e3cbba6 | 2415 | init = false; |
7a3b8353 | 2416 | } |
7e3cbba6 AK |
2417 | /* If memory is still not initialized, do it now. */ |
2418 | if (init) | |
2419 | kernel_init_free_pages(page, 1 << order); | |
89b27116 AK |
2420 | /* Propagate __GFP_SKIP_KASAN_POISON to page flags. */ |
2421 | if (kasan_hw_tags_enabled() && (gfp_flags & __GFP_SKIP_KASAN_POISON)) | |
2422 | SetPageSkipKASanPoison(page); | |
1bb5eab3 AK |
2423 | |
2424 | set_page_owner(page, order, gfp_flags); | |
df4e817b | 2425 | page_table_check_alloc(page, order); |
46f24fd8 JK |
2426 | } |
2427 | ||
479f854a | 2428 | static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, |
c603844b | 2429 | unsigned int alloc_flags) |
2a7684a2 | 2430 | { |
46f24fd8 | 2431 | post_alloc_hook(page, order, gfp_flags); |
17cf4406 | 2432 | |
17cf4406 NP |
2433 | if (order && (gfp_flags & __GFP_COMP)) |
2434 | prep_compound_page(page, order); | |
2435 | ||
75379191 | 2436 | /* |
2f064f34 | 2437 | * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to |
75379191 VB |
2438 | * allocate the page. The expectation is that the caller is taking |
2439 | * steps that will free more memory. The caller should avoid the page | |
2440 | * being used for !PFMEMALLOC purposes. | |
2441 | */ | |
2f064f34 MH |
2442 | if (alloc_flags & ALLOC_NO_WATERMARKS) |
2443 | set_page_pfmemalloc(page); | |
2444 | else | |
2445 | clear_page_pfmemalloc(page); | |
1da177e4 LT |
2446 | } |
2447 | ||
56fd56b8 MG |
2448 | /* |
2449 | * Go through the free lists for the given migratetype and remove | |
2450 | * the smallest available page from the freelists | |
2451 | */ | |
85ccc8fa | 2452 | static __always_inline |
728ec980 | 2453 | struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, |
56fd56b8 MG |
2454 | int migratetype) |
2455 | { | |
2456 | unsigned int current_order; | |
b8af2941 | 2457 | struct free_area *area; |
56fd56b8 MG |
2458 | struct page *page; |
2459 | ||
2460 | /* Find a page of the appropriate size in the preferred list */ | |
2461 | for (current_order = order; current_order < MAX_ORDER; ++current_order) { | |
2462 | area = &(zone->free_area[current_order]); | |
b03641af | 2463 | page = get_page_from_free_area(area, migratetype); |
a16601c5 GT |
2464 | if (!page) |
2465 | continue; | |
6ab01363 AD |
2466 | del_page_from_free_list(page, zone, current_order); |
2467 | expand(zone, page, order, current_order, migratetype); | |
bb14c2c7 | 2468 | set_pcppage_migratetype(page, migratetype); |
10e0f753 WY |
2469 | trace_mm_page_alloc_zone_locked(page, order, migratetype, |
2470 | pcp_allowed_order(order) && | |
2471 | migratetype < MIGRATE_PCPTYPES); | |
56fd56b8 MG |
2472 | return page; |
2473 | } | |
2474 | ||
2475 | return NULL; | |
2476 | } | |
2477 | ||
2478 | ||
b2a0ac88 MG |
2479 | /* |
2480 | * This array describes the order lists are fallen back to when | |
2481 | * the free lists for the desirable migrate type are depleted | |
1dd214b8 ZY |
2482 | * |
2483 | * The other migratetypes do not have fallbacks. | |
b2a0ac88 | 2484 | */ |
da415663 | 2485 | static int fallbacks[MIGRATE_TYPES][3] = { |
974a786e | 2486 | [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
974a786e | 2487 | [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES }, |
7ead3342 | 2488 | [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
b2a0ac88 MG |
2489 | }; |
2490 | ||
dc67647b | 2491 | #ifdef CONFIG_CMA |
85ccc8fa | 2492 | static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone, |
dc67647b JK |
2493 | unsigned int order) |
2494 | { | |
2495 | return __rmqueue_smallest(zone, order, MIGRATE_CMA); | |
2496 | } | |
2497 | #else | |
2498 | static inline struct page *__rmqueue_cma_fallback(struct zone *zone, | |
2499 | unsigned int order) { return NULL; } | |
2500 | #endif | |
2501 | ||
c361be55 | 2502 | /* |
293ffa5e | 2503 | * Move the free pages in a range to the freelist tail of the requested type. |
d9c23400 | 2504 | * Note that start_page and end_pages are not aligned on a pageblock |
c361be55 MG |
2505 | * boundary. If alignment is required, use move_freepages_block() |
2506 | */ | |
02aa0cdd | 2507 | static int move_freepages(struct zone *zone, |
39ddb991 | 2508 | unsigned long start_pfn, unsigned long end_pfn, |
02aa0cdd | 2509 | int migratetype, int *num_movable) |
c361be55 MG |
2510 | { |
2511 | struct page *page; | |
39ddb991 | 2512 | unsigned long pfn; |
d00181b9 | 2513 | unsigned int order; |
d100313f | 2514 | int pages_moved = 0; |
c361be55 | 2515 | |
39ddb991 | 2516 | for (pfn = start_pfn; pfn <= end_pfn;) { |
39ddb991 | 2517 | page = pfn_to_page(pfn); |
c361be55 | 2518 | if (!PageBuddy(page)) { |
02aa0cdd VB |
2519 | /* |
2520 | * We assume that pages that could be isolated for | |
2521 | * migration are movable. But we don't actually try | |
2522 | * isolating, as that would be expensive. | |
2523 | */ | |
2524 | if (num_movable && | |
2525 | (PageLRU(page) || __PageMovable(page))) | |
2526 | (*num_movable)++; | |
39ddb991 | 2527 | pfn++; |
c361be55 MG |
2528 | continue; |
2529 | } | |
2530 | ||
cd961038 DR |
2531 | /* Make sure we are not inadvertently changing nodes */ |
2532 | VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page); | |
2533 | VM_BUG_ON_PAGE(page_zone(page) != zone, page); | |
2534 | ||
ab130f91 | 2535 | order = buddy_order(page); |
6ab01363 | 2536 | move_to_free_list(page, zone, order, migratetype); |
39ddb991 | 2537 | pfn += 1 << order; |
d100313f | 2538 | pages_moved += 1 << order; |
c361be55 MG |
2539 | } |
2540 | ||
d100313f | 2541 | return pages_moved; |
c361be55 MG |
2542 | } |
2543 | ||
ee6f509c | 2544 | int move_freepages_block(struct zone *zone, struct page *page, |
02aa0cdd | 2545 | int migratetype, int *num_movable) |
c361be55 | 2546 | { |
39ddb991 | 2547 | unsigned long start_pfn, end_pfn, pfn; |
c361be55 | 2548 | |
4a222127 DR |
2549 | if (num_movable) |
2550 | *num_movable = 0; | |
2551 | ||
39ddb991 KW |
2552 | pfn = page_to_pfn(page); |
2553 | start_pfn = pfn & ~(pageblock_nr_pages - 1); | |
d9c23400 | 2554 | end_pfn = start_pfn + pageblock_nr_pages - 1; |
c361be55 MG |
2555 | |
2556 | /* Do not cross zone boundaries */ | |
108bcc96 | 2557 | if (!zone_spans_pfn(zone, start_pfn)) |
39ddb991 | 2558 | start_pfn = pfn; |
108bcc96 | 2559 | if (!zone_spans_pfn(zone, end_pfn)) |
c361be55 MG |
2560 | return 0; |
2561 | ||
39ddb991 | 2562 | return move_freepages(zone, start_pfn, end_pfn, migratetype, |
02aa0cdd | 2563 | num_movable); |
c361be55 MG |
2564 | } |
2565 | ||
2f66a68f MG |
2566 | static void change_pageblock_range(struct page *pageblock_page, |
2567 | int start_order, int migratetype) | |
2568 | { | |
2569 | int nr_pageblocks = 1 << (start_order - pageblock_order); | |
2570 | ||
2571 | while (nr_pageblocks--) { | |
2572 | set_pageblock_migratetype(pageblock_page, migratetype); | |
2573 | pageblock_page += pageblock_nr_pages; | |
2574 | } | |
2575 | } | |
2576 | ||
fef903ef | 2577 | /* |
9c0415eb VB |
2578 | * When we are falling back to another migratetype during allocation, try to |
2579 | * steal extra free pages from the same pageblocks to satisfy further | |
2580 | * allocations, instead of polluting multiple pageblocks. | |
2581 | * | |
2582 | * If we are stealing a relatively large buddy page, it is likely there will | |
2583 | * be more free pages in the pageblock, so try to steal them all. For | |
2584 | * reclaimable and unmovable allocations, we steal regardless of page size, | |
2585 | * as fragmentation caused by those allocations polluting movable pageblocks | |
2586 | * is worse than movable allocations stealing from unmovable and reclaimable | |
2587 | * pageblocks. | |
fef903ef | 2588 | */ |
4eb7dce6 JK |
2589 | static bool can_steal_fallback(unsigned int order, int start_mt) |
2590 | { | |
2591 | /* | |
2592 | * Leaving this order check is intended, although there is | |
2593 | * relaxed order check in next check. The reason is that | |
2594 | * we can actually steal whole pageblock if this condition met, | |
2595 | * but, below check doesn't guarantee it and that is just heuristic | |
2596 | * so could be changed anytime. | |
2597 | */ | |
2598 | if (order >= pageblock_order) | |
2599 | return true; | |
2600 | ||
2601 | if (order >= pageblock_order / 2 || | |
2602 | start_mt == MIGRATE_RECLAIMABLE || | |
2603 | start_mt == MIGRATE_UNMOVABLE || | |
2604 | page_group_by_mobility_disabled) | |
2605 | return true; | |
2606 | ||
2607 | return false; | |
2608 | } | |
2609 | ||
597c8920 | 2610 | static inline bool boost_watermark(struct zone *zone) |
1c30844d MG |
2611 | { |
2612 | unsigned long max_boost; | |
2613 | ||
2614 | if (!watermark_boost_factor) | |
597c8920 | 2615 | return false; |
14f69140 HW |
2616 | /* |
2617 | * Don't bother in zones that are unlikely to produce results. | |
2618 | * On small machines, including kdump capture kernels running | |
2619 | * in a small area, boosting the watermark can cause an out of | |
2620 | * memory situation immediately. | |
2621 | */ | |
2622 | if ((pageblock_nr_pages * 4) > zone_managed_pages(zone)) | |
597c8920 | 2623 | return false; |
1c30844d MG |
2624 | |
2625 | max_boost = mult_frac(zone->_watermark[WMARK_HIGH], | |
2626 | watermark_boost_factor, 10000); | |
94b3334c MG |
2627 | |
2628 | /* | |
2629 | * high watermark may be uninitialised if fragmentation occurs | |
2630 | * very early in boot so do not boost. We do not fall | |
2631 | * through and boost by pageblock_nr_pages as failing | |
2632 | * allocations that early means that reclaim is not going | |
2633 | * to help and it may even be impossible to reclaim the | |
2634 | * boosted watermark resulting in a hang. | |
2635 | */ | |
2636 | if (!max_boost) | |
597c8920 | 2637 | return false; |
94b3334c | 2638 | |
1c30844d MG |
2639 | max_boost = max(pageblock_nr_pages, max_boost); |
2640 | ||
2641 | zone->watermark_boost = min(zone->watermark_boost + pageblock_nr_pages, | |
2642 | max_boost); | |
597c8920 JW |
2643 | |
2644 | return true; | |
1c30844d MG |
2645 | } |
2646 | ||
4eb7dce6 JK |
2647 | /* |
2648 | * This function implements actual steal behaviour. If order is large enough, | |
2649 | * we can steal whole pageblock. If not, we first move freepages in this | |
02aa0cdd VB |
2650 | * pageblock to our migratetype and determine how many already-allocated pages |
2651 | * are there in the pageblock with a compatible migratetype. If at least half | |
2652 | * of pages are free or compatible, we can change migratetype of the pageblock | |
2653 | * itself, so pages freed in the future will be put on the correct free list. | |
4eb7dce6 JK |
2654 | */ |
2655 | static void steal_suitable_fallback(struct zone *zone, struct page *page, | |
1c30844d | 2656 | unsigned int alloc_flags, int start_type, bool whole_block) |
fef903ef | 2657 | { |
ab130f91 | 2658 | unsigned int current_order = buddy_order(page); |
02aa0cdd VB |
2659 | int free_pages, movable_pages, alike_pages; |
2660 | int old_block_type; | |
2661 | ||
2662 | old_block_type = get_pageblock_migratetype(page); | |
fef903ef | 2663 | |
3bc48f96 VB |
2664 | /* |
2665 | * This can happen due to races and we want to prevent broken | |
2666 | * highatomic accounting. | |
2667 | */ | |
02aa0cdd | 2668 | if (is_migrate_highatomic(old_block_type)) |
3bc48f96 VB |
2669 | goto single_page; |
2670 | ||
fef903ef SB |
2671 | /* Take ownership for orders >= pageblock_order */ |
2672 | if (current_order >= pageblock_order) { | |
2673 | change_pageblock_range(page, current_order, start_type); | |
3bc48f96 | 2674 | goto single_page; |
fef903ef SB |
2675 | } |
2676 | ||
1c30844d MG |
2677 | /* |
2678 | * Boost watermarks to increase reclaim pressure to reduce the | |
2679 | * likelihood of future fallbacks. Wake kswapd now as the node | |
2680 | * may be balanced overall and kswapd will not wake naturally. | |
2681 | */ | |
597c8920 | 2682 | if (boost_watermark(zone) && (alloc_flags & ALLOC_KSWAPD)) |
73444bc4 | 2683 | set_bit(ZONE_BOOSTED_WATERMARK, &zone->flags); |
1c30844d | 2684 | |
3bc48f96 VB |
2685 | /* We are not allowed to try stealing from the whole block */ |
2686 | if (!whole_block) | |
2687 | goto single_page; | |
2688 | ||
02aa0cdd VB |
2689 | free_pages = move_freepages_block(zone, page, start_type, |
2690 | &movable_pages); | |
2691 | /* | |
2692 | * Determine how many pages are compatible with our allocation. | |
2693 | * For movable allocation, it's the number of movable pages which | |
2694 | * we just obtained. For other types it's a bit more tricky. | |
2695 | */ | |
2696 | if (start_type == MIGRATE_MOVABLE) { | |
2697 | alike_pages = movable_pages; | |
2698 | } else { | |
2699 | /* | |
2700 | * If we are falling back a RECLAIMABLE or UNMOVABLE allocation | |
2701 | * to MOVABLE pageblock, consider all non-movable pages as | |
2702 | * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or | |
2703 | * vice versa, be conservative since we can't distinguish the | |
2704 | * exact migratetype of non-movable pages. | |
2705 | */ | |
2706 | if (old_block_type == MIGRATE_MOVABLE) | |
2707 | alike_pages = pageblock_nr_pages | |
2708 | - (free_pages + movable_pages); | |
2709 | else | |
2710 | alike_pages = 0; | |
2711 | } | |
2712 | ||
3bc48f96 | 2713 | /* moving whole block can fail due to zone boundary conditions */ |
02aa0cdd | 2714 | if (!free_pages) |
3bc48f96 | 2715 | goto single_page; |
fef903ef | 2716 | |
02aa0cdd VB |
2717 | /* |
2718 | * If a sufficient number of pages in the block are either free or of | |
2719 | * comparable migratability as our allocation, claim the whole block. | |
2720 | */ | |
2721 | if (free_pages + alike_pages >= (1 << (pageblock_order-1)) || | |
4eb7dce6 JK |
2722 | page_group_by_mobility_disabled) |
2723 | set_pageblock_migratetype(page, start_type); | |
3bc48f96 VB |
2724 | |
2725 | return; | |
2726 | ||
2727 | single_page: | |
6ab01363 | 2728 | move_to_free_list(page, zone, current_order, start_type); |
4eb7dce6 JK |
2729 | } |
2730 | ||
2149cdae JK |
2731 | /* |
2732 | * Check whether there is a suitable fallback freepage with requested order. | |
2733 | * If only_stealable is true, this function returns fallback_mt only if | |
2734 | * we can steal other freepages all together. This would help to reduce | |
2735 | * fragmentation due to mixed migratetype pages in one pageblock. | |
2736 | */ | |
2737 | int find_suitable_fallback(struct free_area *area, unsigned int order, | |
2738 | int migratetype, bool only_stealable, bool *can_steal) | |
4eb7dce6 JK |
2739 | { |
2740 | int i; | |
2741 | int fallback_mt; | |
2742 | ||
2743 | if (area->nr_free == 0) | |
2744 | return -1; | |
2745 | ||
2746 | *can_steal = false; | |
2747 | for (i = 0;; i++) { | |
2748 | fallback_mt = fallbacks[migratetype][i]; | |
974a786e | 2749 | if (fallback_mt == MIGRATE_TYPES) |
4eb7dce6 JK |
2750 | break; |
2751 | ||
b03641af | 2752 | if (free_area_empty(area, fallback_mt)) |
4eb7dce6 | 2753 | continue; |
fef903ef | 2754 | |
4eb7dce6 JK |
2755 | if (can_steal_fallback(order, migratetype)) |
2756 | *can_steal = true; | |
2757 | ||
2149cdae JK |
2758 | if (!only_stealable) |
2759 | return fallback_mt; | |
2760 | ||
2761 | if (*can_steal) | |
2762 | return fallback_mt; | |
fef903ef | 2763 | } |
4eb7dce6 JK |
2764 | |
2765 | return -1; | |
fef903ef SB |
2766 | } |
2767 | ||
0aaa29a5 MG |
2768 | /* |
2769 | * Reserve a pageblock for exclusive use of high-order atomic allocations if | |
2770 | * there are no empty page blocks that contain a page with a suitable order | |
2771 | */ | |
2772 | static void reserve_highatomic_pageblock(struct page *page, struct zone *zone, | |
2773 | unsigned int alloc_order) | |
2774 | { | |
2775 | int mt; | |
2776 | unsigned long max_managed, flags; | |
2777 | ||
2778 | /* | |
2779 | * Limit the number reserved to 1 pageblock or roughly 1% of a zone. | |
2780 | * Check is race-prone but harmless. | |
2781 | */ | |
9705bea5 | 2782 | max_managed = (zone_managed_pages(zone) / 100) + pageblock_nr_pages; |
0aaa29a5 MG |
2783 | if (zone->nr_reserved_highatomic >= max_managed) |
2784 | return; | |
2785 | ||
2786 | spin_lock_irqsave(&zone->lock, flags); | |
2787 | ||
2788 | /* Recheck the nr_reserved_highatomic limit under the lock */ | |
2789 | if (zone->nr_reserved_highatomic >= max_managed) | |
2790 | goto out_unlock; | |
2791 | ||
2792 | /* Yoink! */ | |
2793 | mt = get_pageblock_migratetype(page); | |
1dd214b8 ZY |
2794 | /* Only reserve normal pageblocks (i.e., they can merge with others) */ |
2795 | if (migratetype_is_mergeable(mt)) { | |
0aaa29a5 MG |
2796 | zone->nr_reserved_highatomic += pageblock_nr_pages; |
2797 | set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); | |
02aa0cdd | 2798 | move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL); |
0aaa29a5 MG |
2799 | } |
2800 | ||
2801 | out_unlock: | |
2802 | spin_unlock_irqrestore(&zone->lock, flags); | |
2803 | } | |
2804 | ||
2805 | /* | |
2806 | * Used when an allocation is about to fail under memory pressure. This | |
2807 | * potentially hurts the reliability of high-order allocations when under | |
2808 | * intense memory pressure but failed atomic allocations should be easier | |
2809 | * to recover from than an OOM. | |
29fac03b MK |
2810 | * |
2811 | * If @force is true, try to unreserve a pageblock even though highatomic | |
2812 | * pageblock is exhausted. | |
0aaa29a5 | 2813 | */ |
29fac03b MK |
2814 | static bool unreserve_highatomic_pageblock(const struct alloc_context *ac, |
2815 | bool force) | |
0aaa29a5 MG |
2816 | { |
2817 | struct zonelist *zonelist = ac->zonelist; | |
2818 | unsigned long flags; | |
2819 | struct zoneref *z; | |
2820 | struct zone *zone; | |
2821 | struct page *page; | |
2822 | int order; | |
04c8716f | 2823 | bool ret; |
0aaa29a5 | 2824 | |
97a225e6 | 2825 | for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->highest_zoneidx, |
0aaa29a5 | 2826 | ac->nodemask) { |
29fac03b MK |
2827 | /* |
2828 | * Preserve at least one pageblock unless memory pressure | |
2829 | * is really high. | |
2830 | */ | |
2831 | if (!force && zone->nr_reserved_highatomic <= | |
2832 | pageblock_nr_pages) | |
0aaa29a5 MG |
2833 | continue; |
2834 | ||
2835 | spin_lock_irqsave(&zone->lock, flags); | |
2836 | for (order = 0; order < MAX_ORDER; order++) { | |
2837 | struct free_area *area = &(zone->free_area[order]); | |
2838 | ||
b03641af | 2839 | page = get_page_from_free_area(area, MIGRATE_HIGHATOMIC); |
a16601c5 | 2840 | if (!page) |
0aaa29a5 MG |
2841 | continue; |
2842 | ||
0aaa29a5 | 2843 | /* |
4855e4a7 MK |
2844 | * In page freeing path, migratetype change is racy so |
2845 | * we can counter several free pages in a pageblock | |
f0953a1b | 2846 | * in this loop although we changed the pageblock type |
4855e4a7 MK |
2847 | * from highatomic to ac->migratetype. So we should |
2848 | * adjust the count once. | |
0aaa29a5 | 2849 | */ |
a6ffdc07 | 2850 | if (is_migrate_highatomic_page(page)) { |
4855e4a7 MK |
2851 | /* |
2852 | * It should never happen but changes to | |
2853 | * locking could inadvertently allow a per-cpu | |
2854 | * drain to add pages to MIGRATE_HIGHATOMIC | |
2855 | * while unreserving so be safe and watch for | |
2856 | * underflows. | |
2857 | */ | |
2858 | zone->nr_reserved_highatomic -= min( | |
2859 | pageblock_nr_pages, | |
2860 | zone->nr_reserved_highatomic); | |
2861 | } | |
0aaa29a5 MG |
2862 | |
2863 | /* | |
2864 | * Convert to ac->migratetype and avoid the normal | |
2865 | * pageblock stealing heuristics. Minimally, the caller | |
2866 | * is doing the work and needs the pages. More | |
2867 | * importantly, if the block was always converted to | |
2868 | * MIGRATE_UNMOVABLE or another type then the number | |
2869 | * of pageblocks that cannot be completely freed | |
2870 | * may increase. | |
2871 | */ | |
2872 | set_pageblock_migratetype(page, ac->migratetype); | |
02aa0cdd VB |
2873 | ret = move_freepages_block(zone, page, ac->migratetype, |
2874 | NULL); | |
29fac03b MK |
2875 | if (ret) { |
2876 | spin_unlock_irqrestore(&zone->lock, flags); | |
2877 | return ret; | |
2878 | } | |
0aaa29a5 MG |
2879 | } |
2880 | spin_unlock_irqrestore(&zone->lock, flags); | |
2881 | } | |
04c8716f MK |
2882 | |
2883 | return false; | |
0aaa29a5 MG |
2884 | } |
2885 | ||
3bc48f96 VB |
2886 | /* |
2887 | * Try finding a free buddy page on the fallback list and put it on the free | |
2888 | * list of requested migratetype, possibly along with other pages from the same | |
2889 | * block, depending on fragmentation avoidance heuristics. Returns true if | |
2890 | * fallback was found so that __rmqueue_smallest() can grab it. | |
b002529d RV |
2891 | * |
2892 | * The use of signed ints for order and current_order is a deliberate | |
2893 | * deviation from the rest of this file, to make the for loop | |
2894 | * condition simpler. | |
3bc48f96 | 2895 | */ |
85ccc8fa | 2896 | static __always_inline bool |
6bb15450 MG |
2897 | __rmqueue_fallback(struct zone *zone, int order, int start_migratetype, |
2898 | unsigned int alloc_flags) | |
b2a0ac88 | 2899 | { |
b8af2941 | 2900 | struct free_area *area; |
b002529d | 2901 | int current_order; |
6bb15450 | 2902 | int min_order = order; |
b2a0ac88 | 2903 | struct page *page; |
4eb7dce6 JK |
2904 | int fallback_mt; |
2905 | bool can_steal; | |
b2a0ac88 | 2906 | |
6bb15450 MG |
2907 | /* |
2908 | * Do not steal pages from freelists belonging to other pageblocks | |
2909 | * i.e. orders < pageblock_order. If there are no local zones free, | |
2910 | * the zonelists will be reiterated without ALLOC_NOFRAGMENT. | |
2911 | */ | |
2912 | if (alloc_flags & ALLOC_NOFRAGMENT) | |
2913 | min_order = pageblock_order; | |
2914 | ||
7a8f58f3 VB |
2915 | /* |
2916 | * Find the largest available free page in the other list. This roughly | |
2917 | * approximates finding the pageblock with the most free pages, which | |
2918 | * would be too costly to do exactly. | |
2919 | */ | |
6bb15450 | 2920 | for (current_order = MAX_ORDER - 1; current_order >= min_order; |
7aeb09f9 | 2921 | --current_order) { |
4eb7dce6 JK |
2922 | area = &(zone->free_area[current_order]); |
2923 | fallback_mt = find_suitable_fallback(area, current_order, | |
2149cdae | 2924 | start_migratetype, false, &can_steal); |
4eb7dce6 JK |
2925 | if (fallback_mt == -1) |
2926 | continue; | |
b2a0ac88 | 2927 | |
7a8f58f3 VB |
2928 | /* |
2929 | * We cannot steal all free pages from the pageblock and the | |
2930 | * requested migratetype is movable. In that case it's better to | |
2931 | * steal and split the smallest available page instead of the | |
2932 | * largest available page, because even if the next movable | |
2933 | * allocation falls back into a different pageblock than this | |
2934 | * one, it won't cause permanent fragmentation. | |
2935 | */ | |
2936 | if (!can_steal && start_migratetype == MIGRATE_MOVABLE | |
2937 | && current_order > order) | |
2938 | goto find_smallest; | |
b2a0ac88 | 2939 | |
7a8f58f3 VB |
2940 | goto do_steal; |
2941 | } | |
e0fff1bd | 2942 | |
7a8f58f3 | 2943 | return false; |
e0fff1bd | 2944 | |
7a8f58f3 VB |
2945 | find_smallest: |
2946 | for (current_order = order; current_order < MAX_ORDER; | |
2947 | current_order++) { | |
2948 | area = &(zone->free_area[current_order]); | |
2949 | fallback_mt = find_suitable_fallback(area, current_order, | |
2950 | start_migratetype, false, &can_steal); | |
2951 | if (fallback_mt != -1) | |
2952 | break; | |
b2a0ac88 MG |
2953 | } |
2954 | ||
7a8f58f3 VB |
2955 | /* |
2956 | * This should not happen - we already found a suitable fallback | |
2957 | * when looking for the largest page. | |
2958 | */ | |
2959 | VM_BUG_ON(current_order == MAX_ORDER); | |
2960 | ||
2961 | do_steal: | |
b03641af | 2962 | page = get_page_from_free_area(area, fallback_mt); |
7a8f58f3 | 2963 | |
1c30844d MG |
2964 | steal_suitable_fallback(zone, page, alloc_flags, start_migratetype, |
2965 | can_steal); | |
7a8f58f3 VB |
2966 | |
2967 | trace_mm_page_alloc_extfrag(page, order, current_order, | |
2968 | start_migratetype, fallback_mt); | |
2969 | ||
2970 | return true; | |
2971 | ||
b2a0ac88 MG |
2972 | } |
2973 | ||
56fd56b8 | 2974 | /* |
1da177e4 LT |
2975 | * Do the hard work of removing an element from the buddy allocator. |
2976 | * Call me with the zone->lock already held. | |
2977 | */ | |
85ccc8fa | 2978 | static __always_inline struct page * |
6bb15450 MG |
2979 | __rmqueue(struct zone *zone, unsigned int order, int migratetype, |
2980 | unsigned int alloc_flags) | |
1da177e4 | 2981 | { |
1da177e4 LT |
2982 | struct page *page; |
2983 | ||
ce8f86ee H |
2984 | if (IS_ENABLED(CONFIG_CMA)) { |
2985 | /* | |
2986 | * Balance movable allocations between regular and CMA areas by | |
2987 | * allocating from CMA when over half of the zone's free memory | |
2988 | * is in the CMA area. | |
2989 | */ | |
2990 | if (alloc_flags & ALLOC_CMA && | |
2991 | zone_page_state(zone, NR_FREE_CMA_PAGES) > | |
2992 | zone_page_state(zone, NR_FREE_PAGES) / 2) { | |
2993 | page = __rmqueue_cma_fallback(zone, order); | |
2994 | if (page) | |
10e0f753 | 2995 | return page; |
ce8f86ee | 2996 | } |
16867664 | 2997 | } |
3bc48f96 | 2998 | retry: |
56fd56b8 | 2999 | page = __rmqueue_smallest(zone, order, migratetype); |
974a786e | 3000 | if (unlikely(!page)) { |
8510e69c | 3001 | if (alloc_flags & ALLOC_CMA) |
dc67647b JK |
3002 | page = __rmqueue_cma_fallback(zone, order); |
3003 | ||
6bb15450 MG |
3004 | if (!page && __rmqueue_fallback(zone, order, migratetype, |
3005 | alloc_flags)) | |
3bc48f96 | 3006 | goto retry; |
728ec980 | 3007 | } |
b2a0ac88 | 3008 | return page; |
1da177e4 LT |
3009 | } |
3010 | ||
5f63b720 | 3011 | /* |
1da177e4 LT |
3012 | * Obtain a specified number of elements from the buddy allocator, all under |
3013 | * a single hold of the lock, for efficiency. Add them to the supplied list. | |
3014 | * Returns the number of new pages which were placed at *list. | |
3015 | */ | |
5f63b720 | 3016 | static int rmqueue_bulk(struct zone *zone, unsigned int order, |
b2a0ac88 | 3017 | unsigned long count, struct list_head *list, |
6bb15450 | 3018 | int migratetype, unsigned int alloc_flags) |
1da177e4 | 3019 | { |
cb66bede | 3020 | int i, allocated = 0; |
5f63b720 | 3021 | |
dbbee9d5 MG |
3022 | /* |
3023 | * local_lock_irq held so equivalent to spin_lock_irqsave for | |
3024 | * both PREEMPT_RT and non-PREEMPT_RT configurations. | |
3025 | */ | |
d34b0733 | 3026 | spin_lock(&zone->lock); |
1da177e4 | 3027 | for (i = 0; i < count; ++i) { |
6bb15450 MG |
3028 | struct page *page = __rmqueue(zone, order, migratetype, |
3029 | alloc_flags); | |
085cc7d5 | 3030 | if (unlikely(page == NULL)) |
1da177e4 | 3031 | break; |
81eabcbe | 3032 | |
77fe7f13 | 3033 | if (unlikely(check_pcp_refill(page, order))) |
479f854a MG |
3034 | continue; |
3035 | ||
81eabcbe | 3036 | /* |
0fac3ba5 VB |
3037 | * Split buddy pages returned by expand() are received here in |
3038 | * physical page order. The page is added to the tail of | |
3039 | * caller's list. From the callers perspective, the linked list | |
3040 | * is ordered by page number under some conditions. This is | |
3041 | * useful for IO devices that can forward direction from the | |
3042 | * head, thus also in the physical page order. This is useful | |
3043 | * for IO devices that can merge IO requests if the physical | |
3044 | * pages are ordered properly. | |
81eabcbe | 3045 | */ |
0fac3ba5 | 3046 | list_add_tail(&page->lru, list); |
cb66bede | 3047 | allocated++; |
bb14c2c7 | 3048 | if (is_migrate_cma(get_pcppage_migratetype(page))) |
d1ce749a BZ |
3049 | __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, |
3050 | -(1 << order)); | |
1da177e4 | 3051 | } |
a6de734b MG |
3052 | |
3053 | /* | |
3054 | * i pages were removed from the buddy list even if some leak due | |
3055 | * to check_pcp_refill failing so adjust NR_FREE_PAGES based | |
cb66bede | 3056 | * on i. Do not confuse with 'allocated' which is the number of |
a6de734b MG |
3057 | * pages added to the pcp list. |
3058 | */ | |
f2260e6b | 3059 | __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order)); |
d34b0733 | 3060 | spin_unlock(&zone->lock); |
cb66bede | 3061 | return allocated; |
1da177e4 LT |
3062 | } |
3063 | ||
4ae7c039 | 3064 | #ifdef CONFIG_NUMA |
8fce4d8e | 3065 | /* |
4037d452 CL |
3066 | * Called from the vmstat counter updater to drain pagesets of this |
3067 | * currently executing processor on remote nodes after they have | |
3068 | * expired. | |
3069 | * | |
879336c3 CL |
3070 | * Note that this function must be called with the thread pinned to |
3071 | * a single processor. | |
8fce4d8e | 3072 | */ |
4037d452 | 3073 | void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) |
4ae7c039 | 3074 | { |
4ae7c039 | 3075 | unsigned long flags; |
7be12fc9 | 3076 | int to_drain, batch; |
4ae7c039 | 3077 | |
dbbee9d5 | 3078 | local_lock_irqsave(&pagesets.lock, flags); |
4db0c3c2 | 3079 | batch = READ_ONCE(pcp->batch); |
7be12fc9 | 3080 | to_drain = min(pcp->count, batch); |
77ba9062 | 3081 | if (to_drain > 0) |
fd56eef2 | 3082 | free_pcppages_bulk(zone, to_drain, pcp, 0); |
dbbee9d5 | 3083 | local_unlock_irqrestore(&pagesets.lock, flags); |
4ae7c039 CL |
3084 | } |
3085 | #endif | |
3086 | ||
9f8f2172 | 3087 | /* |
93481ff0 | 3088 | * Drain pcplists of the indicated processor and zone. |
9f8f2172 CL |
3089 | * |
3090 | * The processor must either be the current processor and the | |
3091 | * thread pinned to the current processor or a processor that | |
3092 | * is not online. | |
3093 | */ | |
93481ff0 | 3094 | static void drain_pages_zone(unsigned int cpu, struct zone *zone) |
1da177e4 | 3095 | { |
c54ad30c | 3096 | unsigned long flags; |
93481ff0 | 3097 | struct per_cpu_pages *pcp; |
1da177e4 | 3098 | |
dbbee9d5 | 3099 | local_lock_irqsave(&pagesets.lock, flags); |
1da177e4 | 3100 | |
28f836b6 | 3101 | pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); |
77ba9062 | 3102 | if (pcp->count) |
fd56eef2 | 3103 | free_pcppages_bulk(zone, pcp->count, pcp, 0); |
28f836b6 | 3104 | |
dbbee9d5 | 3105 | local_unlock_irqrestore(&pagesets.lock, flags); |
93481ff0 | 3106 | } |
3dfa5721 | 3107 | |
93481ff0 VB |
3108 | /* |
3109 | * Drain pcplists of all zones on the indicated processor. | |
3110 | * | |
3111 | * The processor must either be the current processor and the | |
3112 | * thread pinned to the current processor or a processor that | |
3113 | * is not online. | |
3114 | */ | |
3115 | static void drain_pages(unsigned int cpu) | |
3116 | { | |
3117 | struct zone *zone; | |
3118 | ||
3119 | for_each_populated_zone(zone) { | |
3120 | drain_pages_zone(cpu, zone); | |
1da177e4 LT |
3121 | } |
3122 | } | |
1da177e4 | 3123 | |
9f8f2172 CL |
3124 | /* |
3125 | * Spill all of this CPU's per-cpu pages back into the buddy allocator. | |
93481ff0 VB |
3126 | * |
3127 | * The CPU has to be pinned. When zone parameter is non-NULL, spill just | |
3128 | * the single zone's pages. | |
9f8f2172 | 3129 | */ |
93481ff0 | 3130 | void drain_local_pages(struct zone *zone) |
9f8f2172 | 3131 | { |
93481ff0 VB |
3132 | int cpu = smp_processor_id(); |
3133 | ||
3134 | if (zone) | |
3135 | drain_pages_zone(cpu, zone); | |
3136 | else | |
3137 | drain_pages(cpu); | |
9f8f2172 CL |
3138 | } |
3139 | ||
0ccce3b9 MG |
3140 | static void drain_local_pages_wq(struct work_struct *work) |
3141 | { | |
d9367bd0 WY |
3142 | struct pcpu_drain *drain; |
3143 | ||
3144 | drain = container_of(work, struct pcpu_drain, work); | |
3145 | ||
a459eeb7 MH |
3146 | /* |
3147 | * drain_all_pages doesn't use proper cpu hotplug protection so | |
3148 | * we can race with cpu offline when the WQ can move this from | |
3149 | * a cpu pinned worker to an unbound one. We can operate on a different | |
f0953a1b | 3150 | * cpu which is alright but we also have to make sure to not move to |
a459eeb7 MH |
3151 | * a different one. |
3152 | */ | |
9c25cbfc | 3153 | migrate_disable(); |
d9367bd0 | 3154 | drain_local_pages(drain->zone); |
9c25cbfc | 3155 | migrate_enable(); |
0ccce3b9 MG |
3156 | } |
3157 | ||
9f8f2172 | 3158 | /* |
ec6e8c7e VB |
3159 | * The implementation of drain_all_pages(), exposing an extra parameter to |
3160 | * drain on all cpus. | |
93481ff0 | 3161 | * |
ec6e8c7e VB |
3162 | * drain_all_pages() is optimized to only execute on cpus where pcplists are |
3163 | * not empty. The check for non-emptiness can however race with a free to | |
3164 | * pcplist that has not yet increased the pcp->count from 0 to 1. Callers | |
3165 | * that need the guarantee that every CPU has drained can disable the | |
3166 | * optimizing racy check. | |
9f8f2172 | 3167 | */ |
3b1f3658 | 3168 | static void __drain_all_pages(struct zone *zone, bool force_all_cpus) |
9f8f2172 | 3169 | { |
74046494 | 3170 | int cpu; |
74046494 GBY |
3171 | |
3172 | /* | |
041711ce | 3173 | * Allocate in the BSS so we won't require allocation in |
74046494 GBY |
3174 | * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y |
3175 | */ | |
3176 | static cpumask_t cpus_with_pcps; | |
3177 | ||
ce612879 MH |
3178 | /* |
3179 | * Make sure nobody triggers this path before mm_percpu_wq is fully | |
3180 | * initialized. | |
3181 | */ | |
3182 | if (WARN_ON_ONCE(!mm_percpu_wq)) | |
3183 | return; | |
3184 | ||
bd233f53 MG |
3185 | /* |
3186 | * Do not drain if one is already in progress unless it's specific to | |
3187 | * a zone. Such callers are primarily CMA and memory hotplug and need | |
3188 | * the drain to be complete when the call returns. | |
3189 | */ | |
3190 | if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) { | |
3191 | if (!zone) | |
3192 | return; | |
3193 | mutex_lock(&pcpu_drain_mutex); | |
3194 | } | |
0ccce3b9 | 3195 | |
74046494 GBY |
3196 | /* |
3197 | * We don't care about racing with CPU hotplug event | |
3198 | * as offline notification will cause the notified | |
3199 | * cpu to drain that CPU pcps and on_each_cpu_mask | |
3200 | * disables preemption as part of its processing | |
3201 | */ | |
3202 | for_each_online_cpu(cpu) { | |
28f836b6 | 3203 | struct per_cpu_pages *pcp; |
93481ff0 | 3204 | struct zone *z; |
74046494 | 3205 | bool has_pcps = false; |
93481ff0 | 3206 | |
ec6e8c7e VB |
3207 | if (force_all_cpus) { |
3208 | /* | |
3209 | * The pcp.count check is racy, some callers need a | |
3210 | * guarantee that no cpu is missed. | |
3211 | */ | |
3212 | has_pcps = true; | |
3213 | } else if (zone) { | |
28f836b6 MG |
3214 | pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); |
3215 | if (pcp->count) | |
74046494 | 3216 | has_pcps = true; |
93481ff0 VB |
3217 | } else { |
3218 | for_each_populated_zone(z) { | |
28f836b6 MG |
3219 | pcp = per_cpu_ptr(z->per_cpu_pageset, cpu); |
3220 | if (pcp->count) { | |
93481ff0 VB |
3221 | has_pcps = true; |
3222 | break; | |
3223 | } | |
74046494 GBY |
3224 | } |
3225 | } | |
93481ff0 | 3226 | |
74046494 GBY |
3227 | if (has_pcps) |
3228 | cpumask_set_cpu(cpu, &cpus_with_pcps); | |
3229 | else | |
3230 | cpumask_clear_cpu(cpu, &cpus_with_pcps); | |
3231 | } | |
0ccce3b9 | 3232 | |
bd233f53 | 3233 | for_each_cpu(cpu, &cpus_with_pcps) { |
d9367bd0 WY |
3234 | struct pcpu_drain *drain = per_cpu_ptr(&pcpu_drain, cpu); |
3235 | ||
3236 | drain->zone = zone; | |
3237 | INIT_WORK(&drain->work, drain_local_pages_wq); | |
3238 | queue_work_on(cpu, mm_percpu_wq, &drain->work); | |
0ccce3b9 | 3239 | } |
bd233f53 | 3240 | for_each_cpu(cpu, &cpus_with_pcps) |
d9367bd0 | 3241 | flush_work(&per_cpu_ptr(&pcpu_drain, cpu)->work); |
bd233f53 MG |
3242 | |
3243 | mutex_unlock(&pcpu_drain_mutex); | |
9f8f2172 CL |
3244 | } |
3245 | ||
ec6e8c7e VB |
3246 | /* |
3247 | * Spill all the per-cpu pages from all CPUs back into the buddy allocator. | |
3248 | * | |
3249 | * When zone parameter is non-NULL, spill just the single zone's pages. | |
3250 | * | |
3251 | * Note that this can be extremely slow as the draining happens in a workqueue. | |
3252 | */ | |
3253 | void drain_all_pages(struct zone *zone) | |
3254 | { | |
3255 | __drain_all_pages(zone, false); | |
3256 | } | |
3257 | ||
296699de | 3258 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 3259 | |
556b969a CY |
3260 | /* |
3261 | * Touch the watchdog for every WD_PAGE_COUNT pages. | |
3262 | */ | |
3263 | #define WD_PAGE_COUNT (128*1024) | |
3264 | ||
1da177e4 LT |
3265 | void mark_free_pages(struct zone *zone) |
3266 | { | |
556b969a | 3267 | unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT; |
f623f0db | 3268 | unsigned long flags; |
7aeb09f9 | 3269 | unsigned int order, t; |
86760a2c | 3270 | struct page *page; |
1da177e4 | 3271 | |
8080fc03 | 3272 | if (zone_is_empty(zone)) |
1da177e4 LT |
3273 | return; |
3274 | ||
3275 | spin_lock_irqsave(&zone->lock, flags); | |
f623f0db | 3276 | |
108bcc96 | 3277 | max_zone_pfn = zone_end_pfn(zone); |
f623f0db RW |
3278 | for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
3279 | if (pfn_valid(pfn)) { | |
86760a2c | 3280 | page = pfn_to_page(pfn); |
ba6b0979 | 3281 | |
556b969a CY |
3282 | if (!--page_count) { |
3283 | touch_nmi_watchdog(); | |
3284 | page_count = WD_PAGE_COUNT; | |
3285 | } | |
3286 | ||
ba6b0979 JK |
3287 | if (page_zone(page) != zone) |
3288 | continue; | |
3289 | ||
7be98234 RW |
3290 | if (!swsusp_page_is_forbidden(page)) |
3291 | swsusp_unset_page_free(page); | |
f623f0db | 3292 | } |
1da177e4 | 3293 | |
b2a0ac88 | 3294 | for_each_migratetype_order(order, t) { |
86760a2c GT |
3295 | list_for_each_entry(page, |
3296 | &zone->free_area[order].free_list[t], lru) { | |
f623f0db | 3297 | unsigned long i; |
1da177e4 | 3298 | |
86760a2c | 3299 | pfn = page_to_pfn(page); |
556b969a CY |
3300 | for (i = 0; i < (1UL << order); i++) { |
3301 | if (!--page_count) { | |
3302 | touch_nmi_watchdog(); | |
3303 | page_count = WD_PAGE_COUNT; | |
3304 | } | |
7be98234 | 3305 | swsusp_set_page_free(pfn_to_page(pfn + i)); |
556b969a | 3306 | } |
f623f0db | 3307 | } |
b2a0ac88 | 3308 | } |
1da177e4 LT |
3309 | spin_unlock_irqrestore(&zone->lock, flags); |
3310 | } | |
e2c55dc8 | 3311 | #endif /* CONFIG_PM */ |
1da177e4 | 3312 | |
44042b44 MG |
3313 | static bool free_unref_page_prepare(struct page *page, unsigned long pfn, |
3314 | unsigned int order) | |
1da177e4 | 3315 | { |
5f8dcc21 | 3316 | int migratetype; |
1da177e4 | 3317 | |
44042b44 | 3318 | if (!free_pcp_prepare(page, order)) |
9cca35d4 | 3319 | return false; |
689bcebf | 3320 | |
dc4b0caf | 3321 | migratetype = get_pfnblock_migratetype(page, pfn); |
bb14c2c7 | 3322 | set_pcppage_migratetype(page, migratetype); |
9cca35d4 MG |
3323 | return true; |
3324 | } | |
3325 | ||
f26b3fa0 MG |
3326 | static int nr_pcp_free(struct per_cpu_pages *pcp, int high, int batch, |
3327 | bool free_high) | |
3b12e7e9 MG |
3328 | { |
3329 | int min_nr_free, max_nr_free; | |
3330 | ||
f26b3fa0 MG |
3331 | /* Free everything if batch freeing high-order pages. */ |
3332 | if (unlikely(free_high)) | |
3333 | return pcp->count; | |
3334 | ||
3b12e7e9 MG |
3335 | /* Check for PCP disabled or boot pageset */ |
3336 | if (unlikely(high < batch)) | |
3337 | return 1; | |
3338 | ||
3339 | /* Leave at least pcp->batch pages on the list */ | |
3340 | min_nr_free = batch; | |
3341 | max_nr_free = high - batch; | |
3342 | ||
3343 | /* | |
3344 | * Double the number of pages freed each time there is subsequent | |
3345 | * freeing of pages without any allocation. | |
3346 | */ | |
3347 | batch <<= pcp->free_factor; | |
3348 | if (batch < max_nr_free) | |
3349 | pcp->free_factor++; | |
3350 | batch = clamp(batch, min_nr_free, max_nr_free); | |
3351 | ||
3352 | return batch; | |
3353 | } | |
3354 | ||
f26b3fa0 MG |
3355 | static int nr_pcp_high(struct per_cpu_pages *pcp, struct zone *zone, |
3356 | bool free_high) | |
c49c2c47 MG |
3357 | { |
3358 | int high = READ_ONCE(pcp->high); | |
3359 | ||
f26b3fa0 | 3360 | if (unlikely(!high || free_high)) |
c49c2c47 MG |
3361 | return 0; |
3362 | ||
3363 | if (!test_bit(ZONE_RECLAIM_ACTIVE, &zone->flags)) | |
3364 | return high; | |
3365 | ||
3366 | /* | |
3367 | * If reclaim is active, limit the number of pages that can be | |
3368 | * stored on pcp lists | |
3369 | */ | |
3370 | return min(READ_ONCE(pcp->batch) << 2, high); | |
3371 | } | |
3372 | ||
56651377 NSJ |
3373 | static void free_unref_page_commit(struct page *page, int migratetype, |
3374 | unsigned int order) | |
9cca35d4 MG |
3375 | { |
3376 | struct zone *zone = page_zone(page); | |
3377 | struct per_cpu_pages *pcp; | |
3b12e7e9 | 3378 | int high; |
44042b44 | 3379 | int pindex; |
f26b3fa0 | 3380 | bool free_high; |
9cca35d4 | 3381 | |
d34b0733 | 3382 | __count_vm_event(PGFREE); |
28f836b6 | 3383 | pcp = this_cpu_ptr(zone->per_cpu_pageset); |
44042b44 MG |
3384 | pindex = order_to_pindex(migratetype, order); |
3385 | list_add(&page->lru, &pcp->lists[pindex]); | |
3386 | pcp->count += 1 << order; | |
f26b3fa0 MG |
3387 | |
3388 | /* | |
3389 | * As high-order pages other than THP's stored on PCP can contribute | |
3390 | * to fragmentation, limit the number stored when PCP is heavily | |
3391 | * freeing without allocation. The remainder after bulk freeing | |
3392 | * stops will be drained from vmstat refresh context. | |
3393 | */ | |
3394 | free_high = (pcp->free_factor && order && order <= PAGE_ALLOC_COSTLY_ORDER); | |
3395 | ||
3396 | high = nr_pcp_high(pcp, zone, free_high); | |
3b12e7e9 MG |
3397 | if (pcp->count >= high) { |
3398 | int batch = READ_ONCE(pcp->batch); | |
3399 | ||
f26b3fa0 | 3400 | free_pcppages_bulk(zone, nr_pcp_free(pcp, high, batch, free_high), pcp, pindex); |
3b12e7e9 | 3401 | } |
9cca35d4 | 3402 | } |
5f8dcc21 | 3403 | |
9cca35d4 | 3404 | /* |
44042b44 | 3405 | * Free a pcp page |
9cca35d4 | 3406 | */ |
44042b44 | 3407 | void free_unref_page(struct page *page, unsigned int order) |
9cca35d4 MG |
3408 | { |
3409 | unsigned long flags; | |
3410 | unsigned long pfn = page_to_pfn(page); | |
df1acc85 | 3411 | int migratetype; |
9cca35d4 | 3412 | |
44042b44 | 3413 | if (!free_unref_page_prepare(page, pfn, order)) |
9cca35d4 | 3414 | return; |
da456f14 | 3415 | |
5f8dcc21 MG |
3416 | /* |
3417 | * We only track unmovable, reclaimable and movable on pcp lists. | |
df1acc85 | 3418 | * Place ISOLATE pages on the isolated list because they are being |
a6ffdc07 | 3419 | * offlined but treat HIGHATOMIC as movable pages so we can get those |
5f8dcc21 MG |
3420 | * areas back if necessary. Otherwise, we may have to free |
3421 | * excessively into the page allocator | |
3422 | */ | |
df1acc85 MG |
3423 | migratetype = get_pcppage_migratetype(page); |
3424 | if (unlikely(migratetype >= MIGRATE_PCPTYPES)) { | |
194159fb | 3425 | if (unlikely(is_migrate_isolate(migratetype))) { |
44042b44 | 3426 | free_one_page(page_zone(page), page, pfn, order, migratetype, FPI_NONE); |
9cca35d4 | 3427 | return; |
5f8dcc21 MG |
3428 | } |
3429 | migratetype = MIGRATE_MOVABLE; | |
3430 | } | |
3431 | ||
dbbee9d5 | 3432 | local_lock_irqsave(&pagesets.lock, flags); |
56651377 | 3433 | free_unref_page_commit(page, migratetype, order); |
dbbee9d5 | 3434 | local_unlock_irqrestore(&pagesets.lock, flags); |
1da177e4 LT |
3435 | } |
3436 | ||
cc59850e KK |
3437 | /* |
3438 | * Free a list of 0-order pages | |
3439 | */ | |
2d4894b5 | 3440 | void free_unref_page_list(struct list_head *list) |
cc59850e KK |
3441 | { |
3442 | struct page *page, *next; | |
56651377 | 3443 | unsigned long flags; |
c24ad77d | 3444 | int batch_count = 0; |
df1acc85 | 3445 | int migratetype; |
9cca35d4 MG |
3446 | |
3447 | /* Prepare pages for freeing */ | |
3448 | list_for_each_entry_safe(page, next, list, lru) { | |
56651377 | 3449 | unsigned long pfn = page_to_pfn(page); |
053cfda1 | 3450 | if (!free_unref_page_prepare(page, pfn, 0)) { |
9cca35d4 | 3451 | list_del(&page->lru); |
053cfda1 ML |
3452 | continue; |
3453 | } | |
df1acc85 MG |
3454 | |
3455 | /* | |
3456 | * Free isolated pages directly to the allocator, see | |
3457 | * comment in free_unref_page. | |
3458 | */ | |
3459 | migratetype = get_pcppage_migratetype(page); | |
47aef601 DB |
3460 | if (unlikely(is_migrate_isolate(migratetype))) { |
3461 | list_del(&page->lru); | |
3462 | free_one_page(page_zone(page), page, pfn, 0, migratetype, FPI_NONE); | |
3463 | continue; | |
df1acc85 | 3464 | } |
9cca35d4 | 3465 | } |
cc59850e | 3466 | |
dbbee9d5 | 3467 | local_lock_irqsave(&pagesets.lock, flags); |
cc59850e | 3468 | list_for_each_entry_safe(page, next, list, lru) { |
47aef601 DB |
3469 | /* |
3470 | * Non-isolated types over MIGRATE_PCPTYPES get added | |
3471 | * to the MIGRATE_MOVABLE pcp list. | |
3472 | */ | |
df1acc85 | 3473 | migratetype = get_pcppage_migratetype(page); |
47aef601 DB |
3474 | if (unlikely(migratetype >= MIGRATE_PCPTYPES)) |
3475 | migratetype = MIGRATE_MOVABLE; | |
3476 | ||
2d4894b5 | 3477 | trace_mm_page_free_batched(page); |
56651377 | 3478 | free_unref_page_commit(page, migratetype, 0); |
c24ad77d LS |
3479 | |
3480 | /* | |
3481 | * Guard against excessive IRQ disabled times when we get | |
3482 | * a large list of pages to free. | |
3483 | */ | |
3484 | if (++batch_count == SWAP_CLUSTER_MAX) { | |
dbbee9d5 | 3485 | local_unlock_irqrestore(&pagesets.lock, flags); |
c24ad77d | 3486 | batch_count = 0; |
dbbee9d5 | 3487 | local_lock_irqsave(&pagesets.lock, flags); |
c24ad77d | 3488 | } |
cc59850e | 3489 | } |
dbbee9d5 | 3490 | local_unlock_irqrestore(&pagesets.lock, flags); |
cc59850e KK |
3491 | } |
3492 | ||
8dfcc9ba NP |
3493 | /* |
3494 | * split_page takes a non-compound higher-order page, and splits it into | |
3495 | * n (1<<order) sub-pages: page[0..n] | |
3496 | * Each sub-page must be freed individually. | |
3497 | * | |
3498 | * Note: this is probably too low level an operation for use in drivers. | |
3499 | * Please consult with lkml before using this in your driver. | |
3500 | */ | |
3501 | void split_page(struct page *page, unsigned int order) | |
3502 | { | |
3503 | int i; | |
3504 | ||
309381fe SL |
3505 | VM_BUG_ON_PAGE(PageCompound(page), page); |
3506 | VM_BUG_ON_PAGE(!page_count(page), page); | |
b1eeab67 | 3507 | |
a9627bc5 | 3508 | for (i = 1; i < (1 << order); i++) |
7835e98b | 3509 | set_page_refcounted(page + i); |
8fb156c9 | 3510 | split_page_owner(page, 1 << order); |
e1baddf8 | 3511 | split_page_memcg(page, 1 << order); |
8dfcc9ba | 3512 | } |
5853ff23 | 3513 | EXPORT_SYMBOL_GPL(split_page); |
8dfcc9ba | 3514 | |
3c605096 | 3515 | int __isolate_free_page(struct page *page, unsigned int order) |
748446bb | 3516 | { |
748446bb MG |
3517 | unsigned long watermark; |
3518 | struct zone *zone; | |
2139cbe6 | 3519 | int mt; |
748446bb MG |
3520 | |
3521 | BUG_ON(!PageBuddy(page)); | |
3522 | ||
3523 | zone = page_zone(page); | |
2e30abd1 | 3524 | mt = get_pageblock_migratetype(page); |
748446bb | 3525 | |
194159fb | 3526 | if (!is_migrate_isolate(mt)) { |
8348faf9 VB |
3527 | /* |
3528 | * Obey watermarks as if the page was being allocated. We can | |
3529 | * emulate a high-order watermark check with a raised order-0 | |
3530 | * watermark, because we already know our high-order page | |
3531 | * exists. | |
3532 | */ | |
fd1444b2 | 3533 | watermark = zone->_watermark[WMARK_MIN] + (1UL << order); |
d883c6cf | 3534 | if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) |
2e30abd1 MS |
3535 | return 0; |
3536 | ||
8fb74b9f | 3537 | __mod_zone_freepage_state(zone, -(1UL << order), mt); |
2e30abd1 | 3538 | } |
748446bb MG |
3539 | |
3540 | /* Remove page from free list */ | |
b03641af | 3541 | |
6ab01363 | 3542 | del_page_from_free_list(page, zone, order); |
2139cbe6 | 3543 | |
400bc7fd | 3544 | /* |
3545 | * Set the pageblock if the isolated page is at least half of a | |
3546 | * pageblock | |
3547 | */ | |
748446bb MG |
3548 | if (order >= pageblock_order - 1) { |
3549 | struct page *endpage = page + (1 << order) - 1; | |
47118af0 MN |
3550 | for (; page < endpage; page += pageblock_nr_pages) { |
3551 | int mt = get_pageblock_migratetype(page); | |
1dd214b8 ZY |
3552 | /* |
3553 | * Only change normal pageblocks (i.e., they can merge | |
3554 | * with others) | |
3555 | */ | |
3556 | if (migratetype_is_mergeable(mt)) | |
47118af0 MN |
3557 | set_pageblock_migratetype(page, |
3558 | MIGRATE_MOVABLE); | |
3559 | } | |
748446bb MG |
3560 | } |
3561 | ||
f3a14ced | 3562 | |
8fb74b9f | 3563 | return 1UL << order; |
1fb3f8ca MG |
3564 | } |
3565 | ||
624f58d8 AD |
3566 | /** |
3567 | * __putback_isolated_page - Return a now-isolated page back where we got it | |
3568 | * @page: Page that was isolated | |
3569 | * @order: Order of the isolated page | |
e6a0a7ad | 3570 | * @mt: The page's pageblock's migratetype |
624f58d8 AD |
3571 | * |
3572 | * This function is meant to return a page pulled from the free lists via | |
3573 | * __isolate_free_page back to the free lists they were pulled from. | |
3574 | */ | |
3575 | void __putback_isolated_page(struct page *page, unsigned int order, int mt) | |
3576 | { | |
3577 | struct zone *zone = page_zone(page); | |
3578 | ||
3579 | /* zone lock should be held when this function is called */ | |
3580 | lockdep_assert_held(&zone->lock); | |
3581 | ||
3582 | /* Return isolated page to tail of freelist. */ | |
f04a5d5d | 3583 | __free_one_page(page, page_to_pfn(page), zone, order, mt, |
47b6a24a | 3584 | FPI_SKIP_REPORT_NOTIFY | FPI_TO_TAIL); |
624f58d8 AD |
3585 | } |
3586 | ||
060e7417 MG |
3587 | /* |
3588 | * Update NUMA hit/miss statistics | |
3589 | * | |
3590 | * Must be called with interrupts disabled. | |
060e7417 | 3591 | */ |
3e23060b MG |
3592 | static inline void zone_statistics(struct zone *preferred_zone, struct zone *z, |
3593 | long nr_account) | |
060e7417 MG |
3594 | { |
3595 | #ifdef CONFIG_NUMA | |
3a321d2a | 3596 | enum numa_stat_item local_stat = NUMA_LOCAL; |
060e7417 | 3597 | |
4518085e KW |
3598 | /* skip numa counters update if numa stats is disabled */ |
3599 | if (!static_branch_likely(&vm_numa_stat_key)) | |
3600 | return; | |
3601 | ||
c1093b74 | 3602 | if (zone_to_nid(z) != numa_node_id()) |
060e7417 | 3603 | local_stat = NUMA_OTHER; |
060e7417 | 3604 | |
c1093b74 | 3605 | if (zone_to_nid(z) == zone_to_nid(preferred_zone)) |
3e23060b | 3606 | __count_numa_events(z, NUMA_HIT, nr_account); |
2df26639 | 3607 | else { |
3e23060b MG |
3608 | __count_numa_events(z, NUMA_MISS, nr_account); |
3609 | __count_numa_events(preferred_zone, NUMA_FOREIGN, nr_account); | |
060e7417 | 3610 | } |
3e23060b | 3611 | __count_numa_events(z, local_stat, nr_account); |
060e7417 MG |
3612 | #endif |
3613 | } | |
3614 | ||
066b2393 | 3615 | /* Remove page from the per-cpu list, caller must protect the list */ |
3b822017 | 3616 | static inline |
44042b44 MG |
3617 | struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order, |
3618 | int migratetype, | |
6bb15450 | 3619 | unsigned int alloc_flags, |
453f85d4 | 3620 | struct per_cpu_pages *pcp, |
066b2393 MG |
3621 | struct list_head *list) |
3622 | { | |
3623 | struct page *page; | |
3624 | ||
3625 | do { | |
3626 | if (list_empty(list)) { | |
44042b44 MG |
3627 | int batch = READ_ONCE(pcp->batch); |
3628 | int alloced; | |
3629 | ||
3630 | /* | |
3631 | * Scale batch relative to order if batch implies | |
3632 | * free pages can be stored on the PCP. Batch can | |
3633 | * be 1 for small zones or for boot pagesets which | |
3634 | * should never store free pages as the pages may | |
3635 | * belong to arbitrary zones. | |
3636 | */ | |
3637 | if (batch > 1) | |
3638 | batch = max(batch >> order, 2); | |
3639 | alloced = rmqueue_bulk(zone, order, | |
3640 | batch, list, | |
6bb15450 | 3641 | migratetype, alloc_flags); |
44042b44 MG |
3642 | |
3643 | pcp->count += alloced << order; | |
066b2393 MG |
3644 | if (unlikely(list_empty(list))) |
3645 | return NULL; | |
3646 | } | |
3647 | ||
453f85d4 | 3648 | page = list_first_entry(list, struct page, lru); |
066b2393 | 3649 | list_del(&page->lru); |
44042b44 | 3650 | pcp->count -= 1 << order; |
77fe7f13 | 3651 | } while (check_new_pcp(page, order)); |
066b2393 MG |
3652 | |
3653 | return page; | |
3654 | } | |
3655 | ||
3656 | /* Lock and remove page from the per-cpu list */ | |
3657 | static struct page *rmqueue_pcplist(struct zone *preferred_zone, | |
44042b44 MG |
3658 | struct zone *zone, unsigned int order, |
3659 | gfp_t gfp_flags, int migratetype, | |
3660 | unsigned int alloc_flags) | |
066b2393 MG |
3661 | { |
3662 | struct per_cpu_pages *pcp; | |
3663 | struct list_head *list; | |
066b2393 | 3664 | struct page *page; |
d34b0733 | 3665 | unsigned long flags; |
066b2393 | 3666 | |
dbbee9d5 | 3667 | local_lock_irqsave(&pagesets.lock, flags); |
3b12e7e9 MG |
3668 | |
3669 | /* | |
3670 | * On allocation, reduce the number of pages that are batch freed. | |
3671 | * See nr_pcp_free() where free_factor is increased for subsequent | |
3672 | * frees. | |
3673 | */ | |
28f836b6 | 3674 | pcp = this_cpu_ptr(zone->per_cpu_pageset); |
3b12e7e9 | 3675 | pcp->free_factor >>= 1; |
44042b44 MG |
3676 | list = &pcp->lists[order_to_pindex(migratetype, order)]; |
3677 | page = __rmqueue_pcplist(zone, order, migratetype, alloc_flags, pcp, list); | |
43c95bcc | 3678 | local_unlock_irqrestore(&pagesets.lock, flags); |
066b2393 | 3679 | if (page) { |
1c52e6d0 | 3680 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1); |
3e23060b | 3681 | zone_statistics(preferred_zone, zone, 1); |
066b2393 | 3682 | } |
066b2393 MG |
3683 | return page; |
3684 | } | |
3685 | ||
1da177e4 | 3686 | /* |
75379191 | 3687 | * Allocate a page from the given zone. Use pcplists for order-0 allocations. |
1da177e4 | 3688 | */ |
0a15c3e9 | 3689 | static inline |
066b2393 | 3690 | struct page *rmqueue(struct zone *preferred_zone, |
7aeb09f9 | 3691 | struct zone *zone, unsigned int order, |
c603844b MG |
3692 | gfp_t gfp_flags, unsigned int alloc_flags, |
3693 | int migratetype) | |
1da177e4 LT |
3694 | { |
3695 | unsigned long flags; | |
689bcebf | 3696 | struct page *page; |
1da177e4 | 3697 | |
44042b44 | 3698 | if (likely(pcp_allowed_order(order))) { |
1d91df85 JK |
3699 | /* |
3700 | * MIGRATE_MOVABLE pcplist could have the pages on CMA area and | |
3701 | * we need to skip it when CMA area isn't allowed. | |
3702 | */ | |
3703 | if (!IS_ENABLED(CONFIG_CMA) || alloc_flags & ALLOC_CMA || | |
3704 | migratetype != MIGRATE_MOVABLE) { | |
44042b44 MG |
3705 | page = rmqueue_pcplist(preferred_zone, zone, order, |
3706 | gfp_flags, migratetype, alloc_flags); | |
1d91df85 JK |
3707 | goto out; |
3708 | } | |
066b2393 | 3709 | } |
83b9355b | 3710 | |
066b2393 MG |
3711 | /* |
3712 | * We most definitely don't want callers attempting to | |
3713 | * allocate greater than order-1 page units with __GFP_NOFAIL. | |
3714 | */ | |
3715 | WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); | |
0aaa29a5 | 3716 | |
066b2393 MG |
3717 | do { |
3718 | page = NULL; | |
3313204c | 3719 | spin_lock_irqsave(&zone->lock, flags); |
1d91df85 JK |
3720 | /* |
3721 | * order-0 request can reach here when the pcplist is skipped | |
3722 | * due to non-CMA allocation context. HIGHATOMIC area is | |
3723 | * reserved for high-order atomic allocation, so order-0 | |
3724 | * request should skip it. | |
3725 | */ | |
10e0f753 | 3726 | if (order > 0 && alloc_flags & ALLOC_HARDER) |
066b2393 | 3727 | page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); |
3313204c | 3728 | if (!page) { |
6bb15450 | 3729 | page = __rmqueue(zone, order, migratetype, alloc_flags); |
3313204c ED |
3730 | if (!page) |
3731 | goto failed; | |
3732 | } | |
3733 | __mod_zone_freepage_state(zone, -(1 << order), | |
3734 | get_pcppage_migratetype(page)); | |
3735 | spin_unlock_irqrestore(&zone->lock, flags); | |
3736 | } while (check_new_pages(page, order)); | |
1da177e4 | 3737 | |
16709d1d | 3738 | __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); |
3e23060b | 3739 | zone_statistics(preferred_zone, zone, 1); |
1da177e4 | 3740 | |
066b2393 | 3741 | out: |
73444bc4 MG |
3742 | /* Separate test+clear to avoid unnecessary atomics */ |
3743 | if (test_bit(ZONE_BOOSTED_WATERMARK, &zone->flags)) { | |
3744 | clear_bit(ZONE_BOOSTED_WATERMARK, &zone->flags); | |
3745 | wakeup_kswapd(zone, 0, 0, zone_idx(zone)); | |
3746 | } | |
3747 | ||
066b2393 | 3748 | VM_BUG_ON_PAGE(page && bad_range(zone, page), page); |
1da177e4 | 3749 | return page; |
a74609fa NP |
3750 | |
3751 | failed: | |
43c95bcc | 3752 | spin_unlock_irqrestore(&zone->lock, flags); |
a74609fa | 3753 | return NULL; |
1da177e4 LT |
3754 | } |
3755 | ||
933e312e AM |
3756 | #ifdef CONFIG_FAIL_PAGE_ALLOC |
3757 | ||
b2588c4b | 3758 | static struct { |
933e312e AM |
3759 | struct fault_attr attr; |
3760 | ||
621a5f7a | 3761 | bool ignore_gfp_highmem; |
71baba4b | 3762 | bool ignore_gfp_reclaim; |
54114994 | 3763 | u32 min_order; |
933e312e AM |
3764 | } fail_page_alloc = { |
3765 | .attr = FAULT_ATTR_INITIALIZER, | |
71baba4b | 3766 | .ignore_gfp_reclaim = true, |
621a5f7a | 3767 | .ignore_gfp_highmem = true, |
54114994 | 3768 | .min_order = 1, |
933e312e AM |
3769 | }; |
3770 | ||
3771 | static int __init setup_fail_page_alloc(char *str) | |
3772 | { | |
3773 | return setup_fault_attr(&fail_page_alloc.attr, str); | |
3774 | } | |
3775 | __setup("fail_page_alloc=", setup_fail_page_alloc); | |
3776 | ||
af3b8544 | 3777 | static bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
933e312e | 3778 | { |
54114994 | 3779 | if (order < fail_page_alloc.min_order) |
deaf386e | 3780 | return false; |
933e312e | 3781 | if (gfp_mask & __GFP_NOFAIL) |
deaf386e | 3782 | return false; |
933e312e | 3783 | if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM)) |
deaf386e | 3784 | return false; |
71baba4b MG |
3785 | if (fail_page_alloc.ignore_gfp_reclaim && |
3786 | (gfp_mask & __GFP_DIRECT_RECLAIM)) | |
deaf386e | 3787 | return false; |
933e312e AM |
3788 | |
3789 | return should_fail(&fail_page_alloc.attr, 1 << order); | |
3790 | } | |
3791 | ||
3792 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS | |
3793 | ||
3794 | static int __init fail_page_alloc_debugfs(void) | |
3795 | { | |
0825a6f9 | 3796 | umode_t mode = S_IFREG | 0600; |
933e312e | 3797 | struct dentry *dir; |
933e312e | 3798 | |
dd48c085 AM |
3799 | dir = fault_create_debugfs_attr("fail_page_alloc", NULL, |
3800 | &fail_page_alloc.attr); | |
b2588c4b | 3801 | |
d9f7979c GKH |
3802 | debugfs_create_bool("ignore-gfp-wait", mode, dir, |
3803 | &fail_page_alloc.ignore_gfp_reclaim); | |
3804 | debugfs_create_bool("ignore-gfp-highmem", mode, dir, | |
3805 | &fail_page_alloc.ignore_gfp_highmem); | |
3806 | debugfs_create_u32("min-order", mode, dir, &fail_page_alloc.min_order); | |
933e312e | 3807 | |
d9f7979c | 3808 | return 0; |
933e312e AM |
3809 | } |
3810 | ||
3811 | late_initcall(fail_page_alloc_debugfs); | |
3812 | ||
3813 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ | |
3814 | ||
3815 | #else /* CONFIG_FAIL_PAGE_ALLOC */ | |
3816 | ||
af3b8544 | 3817 | static inline bool __should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
933e312e | 3818 | { |
deaf386e | 3819 | return false; |
933e312e AM |
3820 | } |
3821 | ||
3822 | #endif /* CONFIG_FAIL_PAGE_ALLOC */ | |
3823 | ||
54aa3866 | 3824 | noinline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
af3b8544 BP |
3825 | { |
3826 | return __should_fail_alloc_page(gfp_mask, order); | |
3827 | } | |
3828 | ALLOW_ERROR_INJECTION(should_fail_alloc_page, TRUE); | |
3829 | ||
f27ce0e1 JK |
3830 | static inline long __zone_watermark_unusable_free(struct zone *z, |
3831 | unsigned int order, unsigned int alloc_flags) | |
3832 | { | |
3833 | const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM)); | |
3834 | long unusable_free = (1 << order) - 1; | |
3835 | ||
3836 | /* | |
3837 | * If the caller does not have rights to ALLOC_HARDER then subtract | |
3838 | * the high-atomic reserves. This will over-estimate the size of the | |
3839 | * atomic reserve but it avoids a search. | |
3840 | */ | |
3841 | if (likely(!alloc_harder)) | |
3842 | unusable_free += z->nr_reserved_highatomic; | |
3843 | ||
3844 | #ifdef CONFIG_CMA | |
3845 | /* If allocation can't use CMA areas don't use free CMA pages */ | |
3846 | if (!(alloc_flags & ALLOC_CMA)) | |
3847 | unusable_free += zone_page_state(z, NR_FREE_CMA_PAGES); | |
3848 | #endif | |
3849 | ||
3850 | return unusable_free; | |
3851 | } | |
3852 | ||
1da177e4 | 3853 | /* |
97a16fc8 MG |
3854 | * Return true if free base pages are above 'mark'. For high-order checks it |
3855 | * will return true of the order-0 watermark is reached and there is at least | |
3856 | * one free page of a suitable size. Checking now avoids taking the zone lock | |
3857 | * to check in the allocation paths if no pages are free. | |
1da177e4 | 3858 | */ |
86a294a8 | 3859 | bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
97a225e6 | 3860 | int highest_zoneidx, unsigned int alloc_flags, |
86a294a8 | 3861 | long free_pages) |
1da177e4 | 3862 | { |
d23ad423 | 3863 | long min = mark; |
1da177e4 | 3864 | int o; |
cd04ae1e | 3865 | const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM)); |
1da177e4 | 3866 | |
0aaa29a5 | 3867 | /* free_pages may go negative - that's OK */ |
f27ce0e1 | 3868 | free_pages -= __zone_watermark_unusable_free(z, order, alloc_flags); |
0aaa29a5 | 3869 | |
7fb1d9fc | 3870 | if (alloc_flags & ALLOC_HIGH) |
1da177e4 | 3871 | min -= min / 2; |
0aaa29a5 | 3872 | |
f27ce0e1 | 3873 | if (unlikely(alloc_harder)) { |
cd04ae1e MH |
3874 | /* |
3875 | * OOM victims can try even harder than normal ALLOC_HARDER | |
3876 | * users on the grounds that it's definitely going to be in | |
3877 | * the exit path shortly and free memory. Any allocation it | |
3878 | * makes during the free path will be small and short-lived. | |
3879 | */ | |
3880 | if (alloc_flags & ALLOC_OOM) | |
3881 | min -= min / 2; | |
3882 | else | |
3883 | min -= min / 4; | |
3884 | } | |
3885 | ||
97a16fc8 MG |
3886 | /* |
3887 | * Check watermarks for an order-0 allocation request. If these | |
3888 | * are not met, then a high-order request also cannot go ahead | |
3889 | * even if a suitable page happened to be free. | |
3890 | */ | |
97a225e6 | 3891 | if (free_pages <= min + z->lowmem_reserve[highest_zoneidx]) |
88f5acf8 | 3892 | return false; |
1da177e4 | 3893 | |
97a16fc8 MG |
3894 | /* If this is an order-0 request then the watermark is fine */ |
3895 | if (!order) | |
3896 | return true; | |
3897 | ||
3898 | /* For a high-order request, check at least one suitable page is free */ | |
3899 | for (o = order; o < MAX_ORDER; o++) { | |
3900 | struct free_area *area = &z->free_area[o]; | |
3901 | int mt; | |
3902 | ||
3903 | if (!area->nr_free) | |
3904 | continue; | |
3905 | ||
97a16fc8 | 3906 | for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) { |
b03641af | 3907 | if (!free_area_empty(area, mt)) |
97a16fc8 MG |
3908 | return true; |
3909 | } | |
3910 | ||
3911 | #ifdef CONFIG_CMA | |
d883c6cf | 3912 | if ((alloc_flags & ALLOC_CMA) && |
b03641af | 3913 | !free_area_empty(area, MIGRATE_CMA)) { |
97a16fc8 | 3914 | return true; |
d883c6cf | 3915 | } |
97a16fc8 | 3916 | #endif |
76089d00 | 3917 | if (alloc_harder && !free_area_empty(area, MIGRATE_HIGHATOMIC)) |
b050e376 | 3918 | return true; |
1da177e4 | 3919 | } |
97a16fc8 | 3920 | return false; |
88f5acf8 MG |
3921 | } |
3922 | ||
7aeb09f9 | 3923 | bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
97a225e6 | 3924 | int highest_zoneidx, unsigned int alloc_flags) |
88f5acf8 | 3925 | { |
97a225e6 | 3926 | return __zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags, |
88f5acf8 MG |
3927 | zone_page_state(z, NR_FREE_PAGES)); |
3928 | } | |
3929 | ||
48ee5f36 | 3930 | static inline bool zone_watermark_fast(struct zone *z, unsigned int order, |
97a225e6 | 3931 | unsigned long mark, int highest_zoneidx, |
f80b08fc | 3932 | unsigned int alloc_flags, gfp_t gfp_mask) |
48ee5f36 | 3933 | { |
f27ce0e1 | 3934 | long free_pages; |
d883c6cf | 3935 | |
f27ce0e1 | 3936 | free_pages = zone_page_state(z, NR_FREE_PAGES); |
48ee5f36 MG |
3937 | |
3938 | /* | |
3939 | * Fast check for order-0 only. If this fails then the reserves | |
f27ce0e1 | 3940 | * need to be calculated. |
48ee5f36 | 3941 | */ |
f27ce0e1 JK |
3942 | if (!order) { |
3943 | long fast_free; | |
3944 | ||
3945 | fast_free = free_pages; | |
3946 | fast_free -= __zone_watermark_unusable_free(z, 0, alloc_flags); | |
3947 | if (fast_free > mark + z->lowmem_reserve[highest_zoneidx]) | |
3948 | return true; | |
3949 | } | |
48ee5f36 | 3950 | |
f80b08fc CTR |
3951 | if (__zone_watermark_ok(z, order, mark, highest_zoneidx, alloc_flags, |
3952 | free_pages)) | |
3953 | return true; | |
3954 | /* | |
3955 | * Ignore watermark boosting for GFP_ATOMIC order-0 allocations | |
3956 | * when checking the min watermark. The min watermark is the | |
3957 | * point where boosting is ignored so that kswapd is woken up | |
3958 | * when below the low watermark. | |
3959 | */ | |
3960 | if (unlikely(!order && (gfp_mask & __GFP_ATOMIC) && z->watermark_boost | |
3961 | && ((alloc_flags & ALLOC_WMARK_MASK) == WMARK_MIN))) { | |
3962 | mark = z->_watermark[WMARK_MIN]; | |
3963 | return __zone_watermark_ok(z, order, mark, highest_zoneidx, | |
3964 | alloc_flags, free_pages); | |
3965 | } | |
3966 | ||
3967 | return false; | |
48ee5f36 MG |
3968 | } |
3969 | ||
7aeb09f9 | 3970 | bool zone_watermark_ok_safe(struct zone *z, unsigned int order, |
97a225e6 | 3971 | unsigned long mark, int highest_zoneidx) |
88f5acf8 MG |
3972 | { |
3973 | long free_pages = zone_page_state(z, NR_FREE_PAGES); | |
3974 | ||
3975 | if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark) | |
3976 | free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES); | |
3977 | ||
97a225e6 | 3978 | return __zone_watermark_ok(z, order, mark, highest_zoneidx, 0, |
88f5acf8 | 3979 | free_pages); |
1da177e4 LT |
3980 | } |
3981 | ||
9276b1bc | 3982 | #ifdef CONFIG_NUMA |
61bb6cd2 GU |
3983 | int __read_mostly node_reclaim_distance = RECLAIM_DISTANCE; |
3984 | ||
957f822a DR |
3985 | static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
3986 | { | |
e02dc017 | 3987 | return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <= |
a55c7454 | 3988 | node_reclaim_distance; |
957f822a | 3989 | } |
9276b1bc | 3990 | #else /* CONFIG_NUMA */ |
957f822a DR |
3991 | static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
3992 | { | |
3993 | return true; | |
3994 | } | |
9276b1bc PJ |
3995 | #endif /* CONFIG_NUMA */ |
3996 | ||
6bb15450 MG |
3997 | /* |
3998 | * The restriction on ZONE_DMA32 as being a suitable zone to use to avoid | |
3999 | * fragmentation is subtle. If the preferred zone was HIGHMEM then | |
4000 | * premature use of a lower zone may cause lowmem pressure problems that | |
4001 | * are worse than fragmentation. If the next zone is ZONE_DMA then it is | |
4002 | * probably too small. It only makes sense to spread allocations to avoid | |
4003 | * fragmentation between the Normal and DMA32 zones. | |
4004 | */ | |
4005 | static inline unsigned int | |
0a79cdad | 4006 | alloc_flags_nofragment(struct zone *zone, gfp_t gfp_mask) |
6bb15450 | 4007 | { |
736838e9 | 4008 | unsigned int alloc_flags; |
0a79cdad | 4009 | |
736838e9 MN |
4010 | /* |
4011 | * __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD | |
4012 | * to save a branch. | |
4013 | */ | |
4014 | alloc_flags = (__force int) (gfp_mask & __GFP_KSWAPD_RECLAIM); | |
0a79cdad MG |
4015 | |
4016 | #ifdef CONFIG_ZONE_DMA32 | |
8139ad04 AR |
4017 | if (!zone) |
4018 | return alloc_flags; | |
4019 | ||
6bb15450 | 4020 | if (zone_idx(zone) != ZONE_NORMAL) |
8118b82e | 4021 | return alloc_flags; |
6bb15450 MG |
4022 | |
4023 | /* | |
4024 | * If ZONE_DMA32 exists, assume it is the one after ZONE_NORMAL and | |
4025 | * the pointer is within zone->zone_pgdat->node_zones[]. Also assume | |
4026 | * on UMA that if Normal is populated then so is DMA32. | |
4027 | */ | |
4028 | BUILD_BUG_ON(ZONE_NORMAL - ZONE_DMA32 != 1); | |
4029 | if (nr_online_nodes > 1 && !populated_zone(--zone)) | |
8118b82e | 4030 | return alloc_flags; |
6bb15450 | 4031 | |
8118b82e | 4032 | alloc_flags |= ALLOC_NOFRAGMENT; |
0a79cdad MG |
4033 | #endif /* CONFIG_ZONE_DMA32 */ |
4034 | return alloc_flags; | |
6bb15450 | 4035 | } |
6bb15450 | 4036 | |
8e3560d9 PT |
4037 | /* Must be called after current_gfp_context() which can change gfp_mask */ |
4038 | static inline unsigned int gfp_to_alloc_flags_cma(gfp_t gfp_mask, | |
4039 | unsigned int alloc_flags) | |
8510e69c JK |
4040 | { |
4041 | #ifdef CONFIG_CMA | |
8e3560d9 | 4042 | if (gfp_migratetype(gfp_mask) == MIGRATE_MOVABLE) |
8510e69c | 4043 | alloc_flags |= ALLOC_CMA; |
8510e69c JK |
4044 | #endif |
4045 | return alloc_flags; | |
4046 | } | |
4047 | ||
7fb1d9fc | 4048 | /* |
0798e519 | 4049 | * get_page_from_freelist goes through the zonelist trying to allocate |
7fb1d9fc RS |
4050 | * a page. |
4051 | */ | |
4052 | static struct page * | |
a9263751 VB |
4053 | get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, |
4054 | const struct alloc_context *ac) | |
753ee728 | 4055 | { |
6bb15450 | 4056 | struct zoneref *z; |
5117f45d | 4057 | struct zone *zone; |
8a87d695 WY |
4058 | struct pglist_data *last_pgdat = NULL; |
4059 | bool last_pgdat_dirty_ok = false; | |
6bb15450 | 4060 | bool no_fallback; |
3b8c0be4 | 4061 | |
6bb15450 | 4062 | retry: |
7fb1d9fc | 4063 | /* |
9276b1bc | 4064 | * Scan zonelist, looking for a zone with enough free. |
344736f2 | 4065 | * See also __cpuset_node_allowed() comment in kernel/cpuset.c. |
7fb1d9fc | 4066 | */ |
6bb15450 MG |
4067 | no_fallback = alloc_flags & ALLOC_NOFRAGMENT; |
4068 | z = ac->preferred_zoneref; | |
30d8ec73 MN |
4069 | for_next_zone_zonelist_nodemask(zone, z, ac->highest_zoneidx, |
4070 | ac->nodemask) { | |
be06af00 | 4071 | struct page *page; |
e085dbc5 JW |
4072 | unsigned long mark; |
4073 | ||
664eedde MG |
4074 | if (cpusets_enabled() && |
4075 | (alloc_flags & ALLOC_CPUSET) && | |
002f2906 | 4076 | !__cpuset_zone_allowed(zone, gfp_mask)) |
cd38b115 | 4077 | continue; |
a756cf59 JW |
4078 | /* |
4079 | * When allocating a page cache page for writing, we | |
281e3726 MG |
4080 | * want to get it from a node that is within its dirty |
4081 | * limit, such that no single node holds more than its | |
a756cf59 | 4082 | * proportional share of globally allowed dirty pages. |
281e3726 | 4083 | * The dirty limits take into account the node's |
a756cf59 JW |
4084 | * lowmem reserves and high watermark so that kswapd |
4085 | * should be able to balance it without having to | |
4086 | * write pages from its LRU list. | |
4087 | * | |
a756cf59 | 4088 | * XXX: For now, allow allocations to potentially |
281e3726 | 4089 | * exceed the per-node dirty limit in the slowpath |
c9ab0c4f | 4090 | * (spread_dirty_pages unset) before going into reclaim, |
a756cf59 | 4091 | * which is important when on a NUMA setup the allowed |
281e3726 | 4092 | * nodes are together not big enough to reach the |
a756cf59 | 4093 | * global limit. The proper fix for these situations |
281e3726 | 4094 | * will require awareness of nodes in the |
a756cf59 JW |
4095 | * dirty-throttling and the flusher threads. |
4096 | */ | |
3b8c0be4 | 4097 | if (ac->spread_dirty_pages) { |
8a87d695 WY |
4098 | if (last_pgdat != zone->zone_pgdat) { |
4099 | last_pgdat = zone->zone_pgdat; | |
4100 | last_pgdat_dirty_ok = node_dirty_ok(zone->zone_pgdat); | |
4101 | } | |
3b8c0be4 | 4102 | |
8a87d695 | 4103 | if (!last_pgdat_dirty_ok) |
3b8c0be4 | 4104 | continue; |
3b8c0be4 | 4105 | } |
7fb1d9fc | 4106 | |
6bb15450 MG |
4107 | if (no_fallback && nr_online_nodes > 1 && |
4108 | zone != ac->preferred_zoneref->zone) { | |
4109 | int local_nid; | |
4110 | ||
4111 | /* | |
4112 | * If moving to a remote node, retry but allow | |
4113 | * fragmenting fallbacks. Locality is more important | |
4114 | * than fragmentation avoidance. | |
4115 | */ | |
4116 | local_nid = zone_to_nid(ac->preferred_zoneref->zone); | |
4117 | if (zone_to_nid(zone) != local_nid) { | |
4118 | alloc_flags &= ~ALLOC_NOFRAGMENT; | |
4119 | goto retry; | |
4120 | } | |
4121 | } | |
4122 | ||
a9214443 | 4123 | mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK); |
48ee5f36 | 4124 | if (!zone_watermark_fast(zone, order, mark, |
f80b08fc CTR |
4125 | ac->highest_zoneidx, alloc_flags, |
4126 | gfp_mask)) { | |
fa5e084e MG |
4127 | int ret; |
4128 | ||
c9e97a19 PT |
4129 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
4130 | /* | |
4131 | * Watermark failed for this zone, but see if we can | |
4132 | * grow this zone if it contains deferred pages. | |
4133 | */ | |
4134 | if (static_branch_unlikely(&deferred_pages)) { | |
4135 | if (_deferred_grow_zone(zone, order)) | |
4136 | goto try_this_zone; | |
4137 | } | |
4138 | #endif | |
5dab2911 MG |
4139 | /* Checked here to keep the fast path fast */ |
4140 | BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK); | |
4141 | if (alloc_flags & ALLOC_NO_WATERMARKS) | |
4142 | goto try_this_zone; | |
4143 | ||
202e35db | 4144 | if (!node_reclaim_enabled() || |
c33d6c06 | 4145 | !zone_allows_reclaim(ac->preferred_zoneref->zone, zone)) |
cd38b115 MG |
4146 | continue; |
4147 | ||
a5f5f91d | 4148 | ret = node_reclaim(zone->zone_pgdat, gfp_mask, order); |
fa5e084e | 4149 | switch (ret) { |
a5f5f91d | 4150 | case NODE_RECLAIM_NOSCAN: |
fa5e084e | 4151 | /* did not scan */ |
cd38b115 | 4152 | continue; |
a5f5f91d | 4153 | case NODE_RECLAIM_FULL: |
fa5e084e | 4154 | /* scanned but unreclaimable */ |
cd38b115 | 4155 | continue; |
fa5e084e MG |
4156 | default: |
4157 | /* did we reclaim enough */ | |
fed2719e | 4158 | if (zone_watermark_ok(zone, order, mark, |
97a225e6 | 4159 | ac->highest_zoneidx, alloc_flags)) |
fed2719e MG |
4160 | goto try_this_zone; |
4161 | ||
fed2719e | 4162 | continue; |
0798e519 | 4163 | } |
7fb1d9fc RS |
4164 | } |
4165 | ||
fa5e084e | 4166 | try_this_zone: |
066b2393 | 4167 | page = rmqueue(ac->preferred_zoneref->zone, zone, order, |
0aaa29a5 | 4168 | gfp_mask, alloc_flags, ac->migratetype); |
75379191 | 4169 | if (page) { |
479f854a | 4170 | prep_new_page(page, order, gfp_mask, alloc_flags); |
0aaa29a5 MG |
4171 | |
4172 | /* | |
4173 | * If this is a high-order atomic allocation then check | |
4174 | * if the pageblock should be reserved for the future | |
4175 | */ | |
4176 | if (unlikely(order && (alloc_flags & ALLOC_HARDER))) | |
4177 | reserve_highatomic_pageblock(page, zone, order); | |
4178 | ||
75379191 | 4179 | return page; |
c9e97a19 PT |
4180 | } else { |
4181 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT | |
4182 | /* Try again if zone has deferred pages */ | |
4183 | if (static_branch_unlikely(&deferred_pages)) { | |
4184 | if (_deferred_grow_zone(zone, order)) | |
4185 | goto try_this_zone; | |
4186 | } | |
4187 | #endif | |
75379191 | 4188 | } |
54a6eb5c | 4189 | } |
9276b1bc | 4190 | |
6bb15450 MG |
4191 | /* |
4192 | * It's possible on a UMA machine to get through all zones that are | |
4193 | * fragmented. If avoiding fragmentation, reset and try again. | |
4194 | */ | |
4195 | if (no_fallback) { | |
4196 | alloc_flags &= ~ALLOC_NOFRAGMENT; | |
4197 | goto retry; | |
4198 | } | |
4199 | ||
4ffeaf35 | 4200 | return NULL; |
753ee728 MH |
4201 | } |
4202 | ||
9af744d7 | 4203 | static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask) |
a238ab5b | 4204 | { |
a238ab5b | 4205 | unsigned int filter = SHOW_MEM_FILTER_NODES; |
a238ab5b DH |
4206 | |
4207 | /* | |
4208 | * This documents exceptions given to allocations in certain | |
4209 | * contexts that are allowed to allocate outside current's set | |
4210 | * of allowed nodes. | |
4211 | */ | |
4212 | if (!(gfp_mask & __GFP_NOMEMALLOC)) | |
cd04ae1e | 4213 | if (tsk_is_oom_victim(current) || |
a238ab5b DH |
4214 | (current->flags & (PF_MEMALLOC | PF_EXITING))) |
4215 | filter &= ~SHOW_MEM_FILTER_NODES; | |
88dc6f20 | 4216 | if (!in_task() || !(gfp_mask & __GFP_DIRECT_RECLAIM)) |
a238ab5b DH |
4217 | filter &= ~SHOW_MEM_FILTER_NODES; |
4218 | ||
9af744d7 | 4219 | show_mem(filter, nodemask); |
aa187507 MH |
4220 | } |
4221 | ||
a8e99259 | 4222 | void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...) |
aa187507 MH |
4223 | { |
4224 | struct va_format vaf; | |
4225 | va_list args; | |
1be334e5 | 4226 | static DEFINE_RATELIMIT_STATE(nopage_rs, 10*HZ, 1); |
aa187507 | 4227 | |
c4dc63f0 BH |
4228 | if ((gfp_mask & __GFP_NOWARN) || |
4229 | !__ratelimit(&nopage_rs) || | |
4230 | ((gfp_mask & __GFP_DMA) && !has_managed_dma())) | |
aa187507 MH |
4231 | return; |
4232 | ||
7877cdcc MH |
4233 | va_start(args, fmt); |
4234 | vaf.fmt = fmt; | |
4235 | vaf.va = &args; | |
ef8444ea | 4236 | pr_warn("%s: %pV, mode:%#x(%pGg), nodemask=%*pbl", |
0205f755 MH |
4237 | current->comm, &vaf, gfp_mask, &gfp_mask, |
4238 | nodemask_pr_args(nodemask)); | |
7877cdcc | 4239 | va_end(args); |
3ee9a4f0 | 4240 | |
a8e99259 | 4241 | cpuset_print_current_mems_allowed(); |
ef8444ea | 4242 | pr_cont("\n"); |
a238ab5b | 4243 | dump_stack(); |
685dbf6f | 4244 | warn_alloc_show_mem(gfp_mask, nodemask); |
a238ab5b DH |
4245 | } |
4246 | ||
6c18ba7a MH |
4247 | static inline struct page * |
4248 | __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order, | |
4249 | unsigned int alloc_flags, | |
4250 | const struct alloc_context *ac) | |
4251 | { | |
4252 | struct page *page; | |
4253 | ||
4254 | page = get_page_from_freelist(gfp_mask, order, | |
4255 | alloc_flags|ALLOC_CPUSET, ac); | |
4256 | /* | |
4257 | * fallback to ignore cpuset restriction if our nodes | |
4258 | * are depleted | |
4259 | */ | |
4260 | if (!page) | |
4261 | page = get_page_from_freelist(gfp_mask, order, | |
4262 | alloc_flags, ac); | |
4263 | ||
4264 | return page; | |
4265 | } | |
4266 | ||
11e33f6a MG |
4267 | static inline struct page * |
4268 | __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, | |
a9263751 | 4269 | const struct alloc_context *ac, unsigned long *did_some_progress) |
11e33f6a | 4270 | { |
6e0fc46d DR |
4271 | struct oom_control oc = { |
4272 | .zonelist = ac->zonelist, | |
4273 | .nodemask = ac->nodemask, | |
2a966b77 | 4274 | .memcg = NULL, |
6e0fc46d DR |
4275 | .gfp_mask = gfp_mask, |
4276 | .order = order, | |
6e0fc46d | 4277 | }; |
11e33f6a MG |
4278 | struct page *page; |
4279 | ||
9879de73 JW |
4280 | *did_some_progress = 0; |
4281 | ||
9879de73 | 4282 | /* |
dc56401f JW |
4283 | * Acquire the oom lock. If that fails, somebody else is |
4284 | * making progress for us. | |
9879de73 | 4285 | */ |
dc56401f | 4286 | if (!mutex_trylock(&oom_lock)) { |
9879de73 | 4287 | *did_some_progress = 1; |
11e33f6a | 4288 | schedule_timeout_uninterruptible(1); |
1da177e4 LT |
4289 | return NULL; |
4290 | } | |
6b1de916 | 4291 | |
11e33f6a MG |
4292 | /* |
4293 | * Go through the zonelist yet one more time, keep very high watermark | |
4294 | * here, this is only to catch a parallel oom killing, we must fail if | |
e746bf73 TH |
4295 | * we're still under heavy pressure. But make sure that this reclaim |
4296 | * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY | |
4297 | * allocation which will never fail due to oom_lock already held. | |
11e33f6a | 4298 | */ |
e746bf73 TH |
4299 | page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) & |
4300 | ~__GFP_DIRECT_RECLAIM, order, | |
4301 | ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac); | |
7fb1d9fc | 4302 | if (page) |
11e33f6a MG |
4303 | goto out; |
4304 | ||
06ad276a MH |
4305 | /* Coredumps can quickly deplete all memory reserves */ |
4306 | if (current->flags & PF_DUMPCORE) | |
4307 | goto out; | |
4308 | /* The OOM killer will not help higher order allocs */ | |
4309 | if (order > PAGE_ALLOC_COSTLY_ORDER) | |
4310 | goto out; | |
dcda9b04 MH |
4311 | /* |
4312 | * We have already exhausted all our reclaim opportunities without any | |
4313 | * success so it is time to admit defeat. We will skip the OOM killer | |
4314 | * because it is very likely that the caller has a more reasonable | |
4315 | * fallback than shooting a random task. | |
cfb4a541 MN |
4316 | * |
4317 | * The OOM killer may not free memory on a specific node. | |
dcda9b04 | 4318 | */ |
cfb4a541 | 4319 | if (gfp_mask & (__GFP_RETRY_MAYFAIL | __GFP_THISNODE)) |
dcda9b04 | 4320 | goto out; |
06ad276a | 4321 | /* The OOM killer does not needlessly kill tasks for lowmem */ |
97a225e6 | 4322 | if (ac->highest_zoneidx < ZONE_NORMAL) |
06ad276a MH |
4323 | goto out; |
4324 | if (pm_suspended_storage()) | |
4325 | goto out; | |
4326 | /* | |
4327 | * XXX: GFP_NOFS allocations should rather fail than rely on | |
4328 | * other request to make a forward progress. | |
4329 | * We are in an unfortunate situation where out_of_memory cannot | |
4330 | * do much for this context but let's try it to at least get | |
4331 | * access to memory reserved if the current task is killed (see | |
4332 | * out_of_memory). Once filesystems are ready to handle allocation | |
4333 | * failures more gracefully we should just bail out here. | |
4334 | */ | |
4335 | ||
3c2c6488 | 4336 | /* Exhausted what can be done so it's blame time */ |
5020e285 | 4337 | if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) { |
c32b3cbe | 4338 | *did_some_progress = 1; |
5020e285 | 4339 | |
6c18ba7a MH |
4340 | /* |
4341 | * Help non-failing allocations by giving them access to memory | |
4342 | * reserves | |
4343 | */ | |
4344 | if (gfp_mask & __GFP_NOFAIL) | |
4345 | page = __alloc_pages_cpuset_fallback(gfp_mask, order, | |
5020e285 | 4346 | ALLOC_NO_WATERMARKS, ac); |
5020e285 | 4347 | } |
11e33f6a | 4348 | out: |
dc56401f | 4349 | mutex_unlock(&oom_lock); |
11e33f6a MG |
4350 | return page; |
4351 | } | |
4352 | ||
33c2d214 | 4353 | /* |
baf2f90b | 4354 | * Maximum number of compaction retries with a progress before OOM |
33c2d214 MH |
4355 | * killer is consider as the only way to move forward. |
4356 | */ | |
4357 | #define MAX_COMPACT_RETRIES 16 | |
4358 | ||
56de7263 MG |
4359 | #ifdef CONFIG_COMPACTION |
4360 | /* Try memory compaction for high-order allocations before reclaim */ | |
4361 | static struct page * | |
4362 | __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, | |
c603844b | 4363 | unsigned int alloc_flags, const struct alloc_context *ac, |
a5508cd8 | 4364 | enum compact_priority prio, enum compact_result *compact_result) |
56de7263 | 4365 | { |
5e1f0f09 | 4366 | struct page *page = NULL; |
eb414681 | 4367 | unsigned long pflags; |
499118e9 | 4368 | unsigned int noreclaim_flag; |
53853e2d VB |
4369 | |
4370 | if (!order) | |
66199712 | 4371 | return NULL; |
66199712 | 4372 | |
eb414681 | 4373 | psi_memstall_enter(&pflags); |
5bf18281 | 4374 | delayacct_compact_start(); |
499118e9 | 4375 | noreclaim_flag = memalloc_noreclaim_save(); |
eb414681 | 4376 | |
c5d01d0d | 4377 | *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac, |
5e1f0f09 | 4378 | prio, &page); |
eb414681 | 4379 | |
499118e9 | 4380 | memalloc_noreclaim_restore(noreclaim_flag); |
eb414681 | 4381 | psi_memstall_leave(&pflags); |
5bf18281 | 4382 | delayacct_compact_end(); |
56de7263 | 4383 | |
06dac2f4 CTR |
4384 | if (*compact_result == COMPACT_SKIPPED) |
4385 | return NULL; | |
98dd3b48 VB |
4386 | /* |
4387 | * At least in one zone compaction wasn't deferred or skipped, so let's | |
4388 | * count a compaction stall | |
4389 | */ | |
4390 | count_vm_event(COMPACTSTALL); | |
8fb74b9f | 4391 | |
5e1f0f09 MG |
4392 | /* Prep a captured page if available */ |
4393 | if (page) | |
4394 | prep_new_page(page, order, gfp_mask, alloc_flags); | |
4395 | ||
4396 | /* Try get a page from the freelist if available */ | |
4397 | if (!page) | |
4398 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); | |
53853e2d | 4399 | |
98dd3b48 VB |
4400 | if (page) { |
4401 | struct zone *zone = page_zone(page); | |
53853e2d | 4402 | |
98dd3b48 VB |
4403 | zone->compact_blockskip_flush = false; |
4404 | compaction_defer_reset(zone, order, true); | |
4405 | count_vm_event(COMPACTSUCCESS); | |
4406 | return page; | |
4407 | } | |
56de7263 | 4408 | |
98dd3b48 VB |
4409 | /* |
4410 | * It's bad if compaction run occurs and fails. The most likely reason | |
4411 | * is that pages exist, but not enough to satisfy watermarks. | |
4412 | */ | |
4413 | count_vm_event(COMPACTFAIL); | |
66199712 | 4414 | |
98dd3b48 | 4415 | cond_resched(); |
56de7263 MG |
4416 | |
4417 | return NULL; | |
4418 | } | |
33c2d214 | 4419 | |
3250845d VB |
4420 | static inline bool |
4421 | should_compact_retry(struct alloc_context *ac, int order, int alloc_flags, | |
4422 | enum compact_result compact_result, | |
4423 | enum compact_priority *compact_priority, | |
d9436498 | 4424 | int *compaction_retries) |
3250845d VB |
4425 | { |
4426 | int max_retries = MAX_COMPACT_RETRIES; | |
c2033b00 | 4427 | int min_priority; |
65190cff MH |
4428 | bool ret = false; |
4429 | int retries = *compaction_retries; | |
4430 | enum compact_priority priority = *compact_priority; | |
3250845d VB |
4431 | |
4432 | if (!order) | |
4433 | return false; | |
4434 | ||
691d9497 AT |
4435 | if (fatal_signal_pending(current)) |
4436 | return false; | |
4437 | ||
d9436498 VB |
4438 | if (compaction_made_progress(compact_result)) |
4439 | (*compaction_retries)++; | |
4440 | ||
3250845d VB |
4441 | /* |
4442 | * compaction considers all the zone as desperately out of memory | |
4443 | * so it doesn't really make much sense to retry except when the | |
4444 | * failure could be caused by insufficient priority | |
4445 | */ | |
d9436498 VB |
4446 | if (compaction_failed(compact_result)) |
4447 | goto check_priority; | |
3250845d | 4448 | |
49433085 VB |
4449 | /* |
4450 | * compaction was skipped because there are not enough order-0 pages | |
4451 | * to work with, so we retry only if it looks like reclaim can help. | |
4452 | */ | |
4453 | if (compaction_needs_reclaim(compact_result)) { | |
4454 | ret = compaction_zonelist_suitable(ac, order, alloc_flags); | |
4455 | goto out; | |
4456 | } | |
4457 | ||
3250845d VB |
4458 | /* |
4459 | * make sure the compaction wasn't deferred or didn't bail out early | |
4460 | * due to locks contention before we declare that we should give up. | |
49433085 VB |
4461 | * But the next retry should use a higher priority if allowed, so |
4462 | * we don't just keep bailing out endlessly. | |
3250845d | 4463 | */ |
65190cff | 4464 | if (compaction_withdrawn(compact_result)) { |
49433085 | 4465 | goto check_priority; |
65190cff | 4466 | } |
3250845d VB |
4467 | |
4468 | /* | |
dcda9b04 | 4469 | * !costly requests are much more important than __GFP_RETRY_MAYFAIL |
3250845d VB |
4470 | * costly ones because they are de facto nofail and invoke OOM |
4471 | * killer to move on while costly can fail and users are ready | |
4472 | * to cope with that. 1/4 retries is rather arbitrary but we | |
4473 | * would need much more detailed feedback from compaction to | |
4474 | * make a better decision. | |
4475 | */ | |
4476 | if (order > PAGE_ALLOC_COSTLY_ORDER) | |
4477 | max_retries /= 4; | |
65190cff MH |
4478 | if (*compaction_retries <= max_retries) { |
4479 | ret = true; | |
4480 | goto out; | |
4481 | } | |
3250845d | 4482 | |
d9436498 VB |
4483 | /* |
4484 | * Make sure there are attempts at the highest priority if we exhausted | |
4485 | * all retries or failed at the lower priorities. | |
4486 | */ | |
4487 | check_priority: | |
c2033b00 VB |
4488 | min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ? |
4489 | MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY; | |
65190cff | 4490 | |
c2033b00 | 4491 | if (*compact_priority > min_priority) { |
d9436498 VB |
4492 | (*compact_priority)--; |
4493 | *compaction_retries = 0; | |
65190cff | 4494 | ret = true; |
d9436498 | 4495 | } |
65190cff MH |
4496 | out: |
4497 | trace_compact_retry(order, priority, compact_result, retries, max_retries, ret); | |
4498 | return ret; | |
3250845d | 4499 | } |
56de7263 MG |
4500 | #else |
4501 | static inline struct page * | |
4502 | __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, | |
c603844b | 4503 | unsigned int alloc_flags, const struct alloc_context *ac, |
a5508cd8 | 4504 | enum compact_priority prio, enum compact_result *compact_result) |
56de7263 | 4505 | { |
33c2d214 | 4506 | *compact_result = COMPACT_SKIPPED; |
56de7263 MG |
4507 | return NULL; |
4508 | } | |
33c2d214 MH |
4509 | |
4510 | static inline bool | |
86a294a8 MH |
4511 | should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags, |
4512 | enum compact_result compact_result, | |
a5508cd8 | 4513 | enum compact_priority *compact_priority, |
d9436498 | 4514 | int *compaction_retries) |
33c2d214 | 4515 | { |
31e49bfd MH |
4516 | struct zone *zone; |
4517 | struct zoneref *z; | |
4518 | ||
4519 | if (!order || order > PAGE_ALLOC_COSTLY_ORDER) | |
4520 | return false; | |
4521 | ||
4522 | /* | |
4523 | * There are setups with compaction disabled which would prefer to loop | |
4524 | * inside the allocator rather than hit the oom killer prematurely. | |
4525 | * Let's give them a good hope and keep retrying while the order-0 | |
4526 | * watermarks are OK. | |
4527 | */ | |
97a225e6 JK |
4528 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
4529 | ac->highest_zoneidx, ac->nodemask) { | |
31e49bfd | 4530 | if (zone_watermark_ok(zone, 0, min_wmark_pages(zone), |
97a225e6 | 4531 | ac->highest_zoneidx, alloc_flags)) |
31e49bfd MH |
4532 | return true; |
4533 | } | |
33c2d214 MH |
4534 | return false; |
4535 | } | |
3250845d | 4536 | #endif /* CONFIG_COMPACTION */ |
56de7263 | 4537 | |
d92a8cfc | 4538 | #ifdef CONFIG_LOCKDEP |
93781325 | 4539 | static struct lockdep_map __fs_reclaim_map = |
d92a8cfc PZ |
4540 | STATIC_LOCKDEP_MAP_INIT("fs_reclaim", &__fs_reclaim_map); |
4541 | ||
f920e413 | 4542 | static bool __need_reclaim(gfp_t gfp_mask) |
d92a8cfc | 4543 | { |
d92a8cfc PZ |
4544 | /* no reclaim without waiting on it */ |
4545 | if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) | |
4546 | return false; | |
4547 | ||
4548 | /* this guy won't enter reclaim */ | |
2e517d68 | 4549 | if (current->flags & PF_MEMALLOC) |
d92a8cfc PZ |
4550 | return false; |
4551 | ||
d92a8cfc PZ |
4552 | if (gfp_mask & __GFP_NOLOCKDEP) |
4553 | return false; | |
4554 | ||
4555 | return true; | |
4556 | } | |
4557 | ||
4f3eaf45 | 4558 | void __fs_reclaim_acquire(unsigned long ip) |
93781325 | 4559 | { |
4f3eaf45 | 4560 | lock_acquire_exclusive(&__fs_reclaim_map, 0, 0, NULL, ip); |
93781325 OS |
4561 | } |
4562 | ||
4f3eaf45 | 4563 | void __fs_reclaim_release(unsigned long ip) |
93781325 | 4564 | { |
4f3eaf45 | 4565 | lock_release(&__fs_reclaim_map, ip); |
93781325 OS |
4566 | } |
4567 | ||
d92a8cfc PZ |
4568 | void fs_reclaim_acquire(gfp_t gfp_mask) |
4569 | { | |
f920e413 SV |
4570 | gfp_mask = current_gfp_context(gfp_mask); |
4571 | ||
4572 | if (__need_reclaim(gfp_mask)) { | |
4573 | if (gfp_mask & __GFP_FS) | |
4f3eaf45 | 4574 | __fs_reclaim_acquire(_RET_IP_); |
f920e413 SV |
4575 | |
4576 | #ifdef CONFIG_MMU_NOTIFIER | |
4577 | lock_map_acquire(&__mmu_notifier_invalidate_range_start_map); | |
4578 | lock_map_release(&__mmu_notifier_invalidate_range_start_map); | |
4579 | #endif | |
4580 | ||
4581 | } | |
d92a8cfc PZ |
4582 | } |
4583 | EXPORT_SYMBOL_GPL(fs_reclaim_acquire); | |
4584 | ||
4585 | void fs_reclaim_release(gfp_t gfp_mask) | |
4586 | { | |
f920e413 SV |
4587 | gfp_mask = current_gfp_context(gfp_mask); |
4588 | ||
4589 | if (__need_reclaim(gfp_mask)) { | |
4590 | if (gfp_mask & __GFP_FS) | |
4f3eaf45 | 4591 | __fs_reclaim_release(_RET_IP_); |
f920e413 | 4592 | } |
d92a8cfc PZ |
4593 | } |
4594 | EXPORT_SYMBOL_GPL(fs_reclaim_release); | |
4595 | #endif | |
4596 | ||
bba90710 | 4597 | /* Perform direct synchronous page reclaim */ |
2187e17b | 4598 | static unsigned long |
a9263751 VB |
4599 | __perform_reclaim(gfp_t gfp_mask, unsigned int order, |
4600 | const struct alloc_context *ac) | |
11e33f6a | 4601 | { |
499118e9 | 4602 | unsigned int noreclaim_flag; |
fa7fc75f | 4603 | unsigned long progress; |
11e33f6a MG |
4604 | |
4605 | cond_resched(); | |
4606 | ||
4607 | /* We now go into synchronous reclaim */ | |
4608 | cpuset_memory_pressure_bump(); | |
d92a8cfc | 4609 | fs_reclaim_acquire(gfp_mask); |
93781325 | 4610 | noreclaim_flag = memalloc_noreclaim_save(); |
11e33f6a | 4611 | |
a9263751 VB |
4612 | progress = try_to_free_pages(ac->zonelist, order, gfp_mask, |
4613 | ac->nodemask); | |
11e33f6a | 4614 | |
499118e9 | 4615 | memalloc_noreclaim_restore(noreclaim_flag); |
93781325 | 4616 | fs_reclaim_release(gfp_mask); |
11e33f6a MG |
4617 | |
4618 | cond_resched(); | |
4619 | ||
bba90710 MS |
4620 | return progress; |
4621 | } | |
4622 | ||
4623 | /* The really slow allocator path where we enter direct reclaim */ | |
4624 | static inline struct page * | |
4625 | __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order, | |
c603844b | 4626 | unsigned int alloc_flags, const struct alloc_context *ac, |
a9263751 | 4627 | unsigned long *did_some_progress) |
bba90710 MS |
4628 | { |
4629 | struct page *page = NULL; | |
fa7fc75f | 4630 | unsigned long pflags; |
bba90710 MS |
4631 | bool drained = false; |
4632 | ||
fa7fc75f | 4633 | psi_memstall_enter(&pflags); |
a9263751 | 4634 | *did_some_progress = __perform_reclaim(gfp_mask, order, ac); |
9ee493ce | 4635 | if (unlikely(!(*did_some_progress))) |
fa7fc75f | 4636 | goto out; |
11e33f6a | 4637 | |
9ee493ce | 4638 | retry: |
31a6c190 | 4639 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
9ee493ce MG |
4640 | |
4641 | /* | |
4642 | * If an allocation failed after direct reclaim, it could be because | |
0aaa29a5 | 4643 | * pages are pinned on the per-cpu lists or in high alloc reserves. |
047b9967 | 4644 | * Shrink them and try again |
9ee493ce MG |
4645 | */ |
4646 | if (!page && !drained) { | |
29fac03b | 4647 | unreserve_highatomic_pageblock(ac, false); |
93481ff0 | 4648 | drain_all_pages(NULL); |
9ee493ce MG |
4649 | drained = true; |
4650 | goto retry; | |
4651 | } | |
fa7fc75f SB |
4652 | out: |
4653 | psi_memstall_leave(&pflags); | |
9ee493ce | 4654 | |
11e33f6a MG |
4655 | return page; |
4656 | } | |
4657 | ||
5ecd9d40 DR |
4658 | static void wake_all_kswapds(unsigned int order, gfp_t gfp_mask, |
4659 | const struct alloc_context *ac) | |
3a025760 JW |
4660 | { |
4661 | struct zoneref *z; | |
4662 | struct zone *zone; | |
e1a55637 | 4663 | pg_data_t *last_pgdat = NULL; |
97a225e6 | 4664 | enum zone_type highest_zoneidx = ac->highest_zoneidx; |
3a025760 | 4665 | |
97a225e6 | 4666 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, highest_zoneidx, |
5ecd9d40 | 4667 | ac->nodemask) { |
bc53008e WY |
4668 | if (!managed_zone(zone)) |
4669 | continue; | |
d137a7cb | 4670 | if (last_pgdat != zone->zone_pgdat) { |
97a225e6 | 4671 | wakeup_kswapd(zone, gfp_mask, order, highest_zoneidx); |
d137a7cb CW |
4672 | last_pgdat = zone->zone_pgdat; |
4673 | } | |
e1a55637 | 4674 | } |
3a025760 JW |
4675 | } |
4676 | ||
c603844b | 4677 | static inline unsigned int |
341ce06f PZ |
4678 | gfp_to_alloc_flags(gfp_t gfp_mask) |
4679 | { | |
c603844b | 4680 | unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET; |
1da177e4 | 4681 | |
736838e9 MN |
4682 | /* |
4683 | * __GFP_HIGH is assumed to be the same as ALLOC_HIGH | |
4684 | * and __GFP_KSWAPD_RECLAIM is assumed to be the same as ALLOC_KSWAPD | |
4685 | * to save two branches. | |
4686 | */ | |
e6223a3b | 4687 | BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH); |
736838e9 | 4688 | BUILD_BUG_ON(__GFP_KSWAPD_RECLAIM != (__force gfp_t) ALLOC_KSWAPD); |
933e312e | 4689 | |
341ce06f PZ |
4690 | /* |
4691 | * The caller may dip into page reserves a bit more if the caller | |
4692 | * cannot run direct reclaim, or if the caller has realtime scheduling | |
4693 | * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will | |
d0164adc | 4694 | * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). |
341ce06f | 4695 | */ |
736838e9 MN |
4696 | alloc_flags |= (__force int) |
4697 | (gfp_mask & (__GFP_HIGH | __GFP_KSWAPD_RECLAIM)); | |
1da177e4 | 4698 | |
d0164adc | 4699 | if (gfp_mask & __GFP_ATOMIC) { |
5c3240d9 | 4700 | /* |
b104a35d DR |
4701 | * Not worth trying to allocate harder for __GFP_NOMEMALLOC even |
4702 | * if it can't schedule. | |
5c3240d9 | 4703 | */ |
b104a35d | 4704 | if (!(gfp_mask & __GFP_NOMEMALLOC)) |
5c3240d9 | 4705 | alloc_flags |= ALLOC_HARDER; |
523b9458 | 4706 | /* |
b104a35d | 4707 | * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the |
344736f2 | 4708 | * comment for __cpuset_node_allowed(). |
523b9458 | 4709 | */ |
341ce06f | 4710 | alloc_flags &= ~ALLOC_CPUSET; |
88dc6f20 | 4711 | } else if (unlikely(rt_task(current)) && in_task()) |
341ce06f PZ |
4712 | alloc_flags |= ALLOC_HARDER; |
4713 | ||
8e3560d9 | 4714 | alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, alloc_flags); |
8510e69c | 4715 | |
341ce06f PZ |
4716 | return alloc_flags; |
4717 | } | |
4718 | ||
cd04ae1e | 4719 | static bool oom_reserves_allowed(struct task_struct *tsk) |
072bb0aa | 4720 | { |
cd04ae1e MH |
4721 | if (!tsk_is_oom_victim(tsk)) |
4722 | return false; | |
4723 | ||
4724 | /* | |
4725 | * !MMU doesn't have oom reaper so give access to memory reserves | |
4726 | * only to the thread with TIF_MEMDIE set | |
4727 | */ | |
4728 | if (!IS_ENABLED(CONFIG_MMU) && !test_thread_flag(TIF_MEMDIE)) | |
31a6c190 VB |
4729 | return false; |
4730 | ||
cd04ae1e MH |
4731 | return true; |
4732 | } | |
4733 | ||
4734 | /* | |
4735 | * Distinguish requests which really need access to full memory | |
4736 | * reserves from oom victims which can live with a portion of it | |
4737 | */ | |
4738 | static inline int __gfp_pfmemalloc_flags(gfp_t gfp_mask) | |
4739 | { | |
4740 | if (unlikely(gfp_mask & __GFP_NOMEMALLOC)) | |
4741 | return 0; | |
31a6c190 | 4742 | if (gfp_mask & __GFP_MEMALLOC) |
cd04ae1e | 4743 | return ALLOC_NO_WATERMARKS; |
31a6c190 | 4744 | if (in_serving_softirq() && (current->flags & PF_MEMALLOC)) |
cd04ae1e MH |
4745 | return ALLOC_NO_WATERMARKS; |
4746 | if (!in_interrupt()) { | |
4747 | if (current->flags & PF_MEMALLOC) | |
4748 | return ALLOC_NO_WATERMARKS; | |
4749 | else if (oom_reserves_allowed(current)) | |
4750 | return ALLOC_OOM; | |
4751 | } | |
31a6c190 | 4752 | |
cd04ae1e MH |
4753 | return 0; |
4754 | } | |
4755 | ||
4756 | bool gfp_pfmemalloc_allowed(gfp_t gfp_mask) | |
4757 | { | |
4758 | return !!__gfp_pfmemalloc_flags(gfp_mask); | |
072bb0aa MG |
4759 | } |
4760 | ||
0a0337e0 MH |
4761 | /* |
4762 | * Checks whether it makes sense to retry the reclaim to make a forward progress | |
4763 | * for the given allocation request. | |
491d79ae JW |
4764 | * |
4765 | * We give up when we either have tried MAX_RECLAIM_RETRIES in a row | |
4766 | * without success, or when we couldn't even meet the watermark if we | |
4767 | * reclaimed all remaining pages on the LRU lists. | |
0a0337e0 MH |
4768 | * |
4769 | * Returns true if a retry is viable or false to enter the oom path. | |
4770 | */ | |
4771 | static inline bool | |
4772 | should_reclaim_retry(gfp_t gfp_mask, unsigned order, | |
4773 | struct alloc_context *ac, int alloc_flags, | |
423b452e | 4774 | bool did_some_progress, int *no_progress_loops) |
0a0337e0 MH |
4775 | { |
4776 | struct zone *zone; | |
4777 | struct zoneref *z; | |
15f570bf | 4778 | bool ret = false; |
0a0337e0 | 4779 | |
423b452e VB |
4780 | /* |
4781 | * Costly allocations might have made a progress but this doesn't mean | |
4782 | * their order will become available due to high fragmentation so | |
4783 | * always increment the no progress counter for them | |
4784 | */ | |
4785 | if (did_some_progress && order <= PAGE_ALLOC_COSTLY_ORDER) | |
4786 | *no_progress_loops = 0; | |
4787 | else | |
4788 | (*no_progress_loops)++; | |
4789 | ||
0a0337e0 MH |
4790 | /* |
4791 | * Make sure we converge to OOM if we cannot make any progress | |
4792 | * several times in the row. | |
4793 | */ | |
04c8716f MK |
4794 | if (*no_progress_loops > MAX_RECLAIM_RETRIES) { |
4795 | /* Before OOM, exhaust highatomic_reserve */ | |
29fac03b | 4796 | return unreserve_highatomic_pageblock(ac, true); |
04c8716f | 4797 | } |
0a0337e0 | 4798 | |
bca67592 MG |
4799 | /* |
4800 | * Keep reclaiming pages while there is a chance this will lead | |
4801 | * somewhere. If none of the target zones can satisfy our allocation | |
4802 | * request even if all reclaimable pages are considered then we are | |
4803 | * screwed and have to go OOM. | |
0a0337e0 | 4804 | */ |
97a225e6 JK |
4805 | for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, |
4806 | ac->highest_zoneidx, ac->nodemask) { | |
0a0337e0 | 4807 | unsigned long available; |
ede37713 | 4808 | unsigned long reclaimable; |
d379f01d MH |
4809 | unsigned long min_wmark = min_wmark_pages(zone); |
4810 | bool wmark; | |
0a0337e0 | 4811 | |
5a1c84b4 | 4812 | available = reclaimable = zone_reclaimable_pages(zone); |
5a1c84b4 | 4813 | available += zone_page_state_snapshot(zone, NR_FREE_PAGES); |
0a0337e0 MH |
4814 | |
4815 | /* | |
491d79ae JW |
4816 | * Would the allocation succeed if we reclaimed all |
4817 | * reclaimable pages? | |
0a0337e0 | 4818 | */ |
d379f01d | 4819 | wmark = __zone_watermark_ok(zone, order, min_wmark, |
97a225e6 | 4820 | ac->highest_zoneidx, alloc_flags, available); |
d379f01d MH |
4821 | trace_reclaim_retry_zone(z, order, reclaimable, |
4822 | available, min_wmark, *no_progress_loops, wmark); | |
4823 | if (wmark) { | |
15f570bf | 4824 | ret = true; |
132b0d21 | 4825 | break; |
0a0337e0 MH |
4826 | } |
4827 | } | |
4828 | ||
15f570bf MH |
4829 | /* |
4830 | * Memory allocation/reclaim might be called from a WQ context and the | |
4831 | * current implementation of the WQ concurrency control doesn't | |
4832 | * recognize that a particular WQ is congested if the worker thread is | |
4833 | * looping without ever sleeping. Therefore we have to do a short sleep | |
4834 | * here rather than calling cond_resched(). | |
4835 | */ | |
4836 | if (current->flags & PF_WQ_WORKER) | |
4837 | schedule_timeout_uninterruptible(1); | |
4838 | else | |
4839 | cond_resched(); | |
4840 | return ret; | |
0a0337e0 MH |
4841 | } |
4842 | ||
902b6281 VB |
4843 | static inline bool |
4844 | check_retry_cpuset(int cpuset_mems_cookie, struct alloc_context *ac) | |
4845 | { | |
4846 | /* | |
4847 | * It's possible that cpuset's mems_allowed and the nodemask from | |
4848 | * mempolicy don't intersect. This should be normally dealt with by | |
4849 | * policy_nodemask(), but it's possible to race with cpuset update in | |
4850 | * such a way the check therein was true, and then it became false | |
4851 | * before we got our cpuset_mems_cookie here. | |
4852 | * This assumes that for all allocations, ac->nodemask can come only | |
4853 | * from MPOL_BIND mempolicy (whose documented semantics is to be ignored | |
4854 | * when it does not intersect with the cpuset restrictions) or the | |
4855 | * caller can deal with a violated nodemask. | |
4856 | */ | |
4857 | if (cpusets_enabled() && ac->nodemask && | |
4858 | !cpuset_nodemask_valid_mems_allowed(ac->nodemask)) { | |
4859 | ac->nodemask = NULL; | |
4860 | return true; | |
4861 | } | |
4862 | ||
4863 | /* | |
4864 | * When updating a task's mems_allowed or mempolicy nodemask, it is | |
4865 | * possible to race with parallel threads in such a way that our | |
4866 | * allocation can fail while the mask is being updated. If we are about | |
4867 | * to fail, check if the cpuset changed during allocation and if so, | |
4868 | * retry. | |
4869 | */ | |
4870 | if (read_mems_allowed_retry(cpuset_mems_cookie)) | |
4871 | return true; | |
4872 | ||
4873 | return false; | |
4874 | } | |
4875 | ||
11e33f6a MG |
4876 | static inline struct page * |
4877 | __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order, | |
a9263751 | 4878 | struct alloc_context *ac) |
11e33f6a | 4879 | { |
d0164adc | 4880 | bool can_direct_reclaim = gfp_mask & __GFP_DIRECT_RECLAIM; |
282722b0 | 4881 | const bool costly_order = order > PAGE_ALLOC_COSTLY_ORDER; |
11e33f6a | 4882 | struct page *page = NULL; |
c603844b | 4883 | unsigned int alloc_flags; |
11e33f6a | 4884 | unsigned long did_some_progress; |
5ce9bfef | 4885 | enum compact_priority compact_priority; |
c5d01d0d | 4886 | enum compact_result compact_result; |
5ce9bfef VB |
4887 | int compaction_retries; |
4888 | int no_progress_loops; | |
5ce9bfef | 4889 | unsigned int cpuset_mems_cookie; |
cd04ae1e | 4890 | int reserve_flags; |
1da177e4 | 4891 | |
d0164adc MG |
4892 | /* |
4893 | * We also sanity check to catch abuse of atomic reserves being used by | |
4894 | * callers that are not in atomic context. | |
4895 | */ | |
4896 | if (WARN_ON_ONCE((gfp_mask & (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM)) == | |
4897 | (__GFP_ATOMIC|__GFP_DIRECT_RECLAIM))) | |
4898 | gfp_mask &= ~__GFP_ATOMIC; | |
4899 | ||
5ce9bfef VB |
4900 | retry_cpuset: |
4901 | compaction_retries = 0; | |
4902 | no_progress_loops = 0; | |
4903 | compact_priority = DEF_COMPACT_PRIORITY; | |
4904 | cpuset_mems_cookie = read_mems_allowed_begin(); | |
9a67f648 MH |
4905 | |
4906 | /* | |
4907 | * The fast path uses conservative alloc_flags to succeed only until | |
4908 | * kswapd needs to be woken up, and to avoid the cost of setting up | |
4909 | * alloc_flags precisely. So we do that now. | |
4910 | */ | |
4911 | alloc_flags = gfp_to_alloc_flags(gfp_mask); | |
4912 | ||
e47483bc VB |
4913 | /* |
4914 | * We need to recalculate the starting point for the zonelist iterator | |
4915 | * because we might have used different nodemask in the fast path, or | |
4916 | * there was a cpuset modification and we are retrying - otherwise we | |
4917 | * could end up iterating over non-eligible zones endlessly. | |
4918 | */ | |
4919 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, | |
97a225e6 | 4920 | ac->highest_zoneidx, ac->nodemask); |
e47483bc VB |
4921 | if (!ac->preferred_zoneref->zone) |
4922 | goto nopage; | |
4923 | ||
8ca1b5a4 FT |
4924 | /* |
4925 | * Check for insane configurations where the cpuset doesn't contain | |
4926 | * any suitable zone to satisfy the request - e.g. non-movable | |
4927 | * GFP_HIGHUSER allocations from MOVABLE nodes only. | |
4928 | */ | |
4929 | if (cpusets_insane_config() && (gfp_mask & __GFP_HARDWALL)) { | |
4930 | struct zoneref *z = first_zones_zonelist(ac->zonelist, | |
4931 | ac->highest_zoneidx, | |
4932 | &cpuset_current_mems_allowed); | |
4933 | if (!z->zone) | |
4934 | goto nopage; | |
4935 | } | |
4936 | ||
0a79cdad | 4937 | if (alloc_flags & ALLOC_KSWAPD) |
5ecd9d40 | 4938 | wake_all_kswapds(order, gfp_mask, ac); |
23771235 VB |
4939 | |
4940 | /* | |
4941 | * The adjusted alloc_flags might result in immediate success, so try | |
4942 | * that first | |
4943 | */ | |
4944 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); | |
4945 | if (page) | |
4946 | goto got_pg; | |
4947 | ||
a8161d1e VB |
4948 | /* |
4949 | * For costly allocations, try direct compaction first, as it's likely | |
282722b0 VB |
4950 | * that we have enough base pages and don't need to reclaim. For non- |
4951 | * movable high-order allocations, do that as well, as compaction will | |
4952 | * try prevent permanent fragmentation by migrating from blocks of the | |
4953 | * same migratetype. | |
4954 | * Don't try this for allocations that are allowed to ignore | |
4955 | * watermarks, as the ALLOC_NO_WATERMARKS attempt didn't yet happen. | |
a8161d1e | 4956 | */ |
282722b0 VB |
4957 | if (can_direct_reclaim && |
4958 | (costly_order || | |
4959 | (order > 0 && ac->migratetype != MIGRATE_MOVABLE)) | |
4960 | && !gfp_pfmemalloc_allowed(gfp_mask)) { | |
a8161d1e VB |
4961 | page = __alloc_pages_direct_compact(gfp_mask, order, |
4962 | alloc_flags, ac, | |
a5508cd8 | 4963 | INIT_COMPACT_PRIORITY, |
a8161d1e VB |
4964 | &compact_result); |
4965 | if (page) | |
4966 | goto got_pg; | |
4967 | ||
cc638f32 VB |
4968 | /* |
4969 | * Checks for costly allocations with __GFP_NORETRY, which | |
4970 | * includes some THP page fault allocations | |
4971 | */ | |
4972 | if (costly_order && (gfp_mask & __GFP_NORETRY)) { | |
b39d0ee2 DR |
4973 | /* |
4974 | * If allocating entire pageblock(s) and compaction | |
4975 | * failed because all zones are below low watermarks | |
4976 | * or is prohibited because it recently failed at this | |
3f36d866 DR |
4977 | * order, fail immediately unless the allocator has |
4978 | * requested compaction and reclaim retry. | |
b39d0ee2 DR |
4979 | * |
4980 | * Reclaim is | |
4981 | * - potentially very expensive because zones are far | |
4982 | * below their low watermarks or this is part of very | |
4983 | * bursty high order allocations, | |
4984 | * - not guaranteed to help because isolate_freepages() | |
4985 | * may not iterate over freed pages as part of its | |
4986 | * linear scan, and | |
4987 | * - unlikely to make entire pageblocks free on its | |
4988 | * own. | |
4989 | */ | |
4990 | if (compact_result == COMPACT_SKIPPED || | |
4991 | compact_result == COMPACT_DEFERRED) | |
4992 | goto nopage; | |
a8161d1e | 4993 | |
a8161d1e | 4994 | /* |
3eb2771b VB |
4995 | * Looks like reclaim/compaction is worth trying, but |
4996 | * sync compaction could be very expensive, so keep | |
25160354 | 4997 | * using async compaction. |
a8161d1e | 4998 | */ |
a5508cd8 | 4999 | compact_priority = INIT_COMPACT_PRIORITY; |
a8161d1e VB |
5000 | } |
5001 | } | |
23771235 | 5002 | |
31a6c190 | 5003 | retry: |
23771235 | 5004 | /* Ensure kswapd doesn't accidentally go to sleep as long as we loop */ |
0a79cdad | 5005 | if (alloc_flags & ALLOC_KSWAPD) |
5ecd9d40 | 5006 | wake_all_kswapds(order, gfp_mask, ac); |
31a6c190 | 5007 | |
cd04ae1e MH |
5008 | reserve_flags = __gfp_pfmemalloc_flags(gfp_mask); |
5009 | if (reserve_flags) | |
8e3560d9 | 5010 | alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, reserve_flags); |
23771235 | 5011 | |
e46e7b77 | 5012 | /* |
d6a24df0 VB |
5013 | * Reset the nodemask and zonelist iterators if memory policies can be |
5014 | * ignored. These allocations are high priority and system rather than | |
5015 | * user oriented. | |
e46e7b77 | 5016 | */ |
cd04ae1e | 5017 | if (!(alloc_flags & ALLOC_CPUSET) || reserve_flags) { |
d6a24df0 | 5018 | ac->nodemask = NULL; |
e46e7b77 | 5019 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, |
97a225e6 | 5020 | ac->highest_zoneidx, ac->nodemask); |
e46e7b77 MG |
5021 | } |
5022 | ||
23771235 | 5023 | /* Attempt with potentially adjusted zonelist and alloc_flags */ |
31a6c190 | 5024 | page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); |
7fb1d9fc RS |
5025 | if (page) |
5026 | goto got_pg; | |
1da177e4 | 5027 | |
d0164adc | 5028 | /* Caller is not willing to reclaim, we can't balance anything */ |
9a67f648 | 5029 | if (!can_direct_reclaim) |
1da177e4 LT |
5030 | goto nopage; |
5031 | ||
9a67f648 MH |
5032 | /* Avoid recursion of direct reclaim */ |
5033 | if (current->flags & PF_MEMALLOC) | |
6583bb64 DR |
5034 | goto nopage; |
5035 | ||
a8161d1e VB |
5036 | /* Try direct reclaim and then allocating */ |
5037 | page = __alloc_pages_direct_reclaim(gfp_mask, order, alloc_flags, ac, | |
5038 | &did_some_progress); | |
5039 | if (page) | |
5040 | goto got_pg; | |
5041 | ||
5042 | /* Try direct compaction and then allocating */ | |
a9263751 | 5043 | page = __alloc_pages_direct_compact(gfp_mask, order, alloc_flags, ac, |
a5508cd8 | 5044 | compact_priority, &compact_result); |
56de7263 MG |
5045 | if (page) |
5046 | goto got_pg; | |
75f30861 | 5047 | |
9083905a JW |
5048 | /* Do not loop if specifically requested */ |
5049 | if (gfp_mask & __GFP_NORETRY) | |
a8161d1e | 5050 | goto nopage; |
9083905a | 5051 | |
0a0337e0 MH |
5052 | /* |
5053 | * Do not retry costly high order allocations unless they are | |
dcda9b04 | 5054 | * __GFP_RETRY_MAYFAIL |
0a0337e0 | 5055 | */ |
dcda9b04 | 5056 | if (costly_order && !(gfp_mask & __GFP_RETRY_MAYFAIL)) |
a8161d1e | 5057 | goto nopage; |
0a0337e0 | 5058 | |
0a0337e0 | 5059 | if (should_reclaim_retry(gfp_mask, order, ac, alloc_flags, |
423b452e | 5060 | did_some_progress > 0, &no_progress_loops)) |
0a0337e0 MH |
5061 | goto retry; |
5062 | ||
33c2d214 MH |
5063 | /* |
5064 | * It doesn't make any sense to retry for the compaction if the order-0 | |
5065 | * reclaim is not able to make any progress because the current | |
5066 | * implementation of the compaction depends on the sufficient amount | |
5067 | * of free memory (see __compaction_suitable) | |
5068 | */ | |
5069 | if (did_some_progress > 0 && | |
86a294a8 | 5070 | should_compact_retry(ac, order, alloc_flags, |
a5508cd8 | 5071 | compact_result, &compact_priority, |
d9436498 | 5072 | &compaction_retries)) |
33c2d214 MH |
5073 | goto retry; |
5074 | ||
902b6281 VB |
5075 | |
5076 | /* Deal with possible cpuset update races before we start OOM killing */ | |
5077 | if (check_retry_cpuset(cpuset_mems_cookie, ac)) | |
e47483bc VB |
5078 | goto retry_cpuset; |
5079 | ||
9083905a JW |
5080 | /* Reclaim has failed us, start killing things */ |
5081 | page = __alloc_pages_may_oom(gfp_mask, order, ac, &did_some_progress); | |
5082 | if (page) | |
5083 | goto got_pg; | |
5084 | ||
9a67f648 | 5085 | /* Avoid allocations with no watermarks from looping endlessly */ |
cd04ae1e | 5086 | if (tsk_is_oom_victim(current) && |
8510e69c | 5087 | (alloc_flags & ALLOC_OOM || |
c288983d | 5088 | (gfp_mask & __GFP_NOMEMALLOC))) |
9a67f648 MH |
5089 | goto nopage; |
5090 | ||
9083905a | 5091 | /* Retry as long as the OOM killer is making progress */ |
0a0337e0 MH |
5092 | if (did_some_progress) { |
5093 | no_progress_loops = 0; | |
9083905a | 5094 | goto retry; |
0a0337e0 | 5095 | } |
9083905a | 5096 | |
1da177e4 | 5097 | nopage: |
902b6281 VB |
5098 | /* Deal with possible cpuset update races before we fail */ |
5099 | if (check_retry_cpuset(cpuset_mems_cookie, ac)) | |
5ce9bfef VB |
5100 | goto retry_cpuset; |
5101 | ||
9a67f648 MH |
5102 | /* |
5103 | * Make sure that __GFP_NOFAIL request doesn't leak out and make sure | |
5104 | * we always retry | |
5105 | */ | |
5106 | if (gfp_mask & __GFP_NOFAIL) { | |
5107 | /* | |
5108 | * All existing users of the __GFP_NOFAIL are blockable, so warn | |
5109 | * of any new users that actually require GFP_NOWAIT | |
5110 | */ | |
5111 | if (WARN_ON_ONCE(!can_direct_reclaim)) | |
5112 | goto fail; | |
5113 | ||
5114 | /* | |
5115 | * PF_MEMALLOC request from this context is rather bizarre | |
5116 | * because we cannot reclaim anything and only can loop waiting | |
5117 | * for somebody to do a work for us | |
5118 | */ | |
5119 | WARN_ON_ONCE(current->flags & PF_MEMALLOC); | |
5120 | ||
5121 | /* | |
5122 | * non failing costly orders are a hard requirement which we | |
5123 | * are not prepared for much so let's warn about these users | |
5124 | * so that we can identify them and convert them to something | |
5125 | * else. | |
5126 | */ | |
5127 | WARN_ON_ONCE(order > PAGE_ALLOC_COSTLY_ORDER); | |
5128 | ||
6c18ba7a MH |
5129 | /* |
5130 | * Help non-failing allocations by giving them access to memory | |
5131 | * reserves but do not use ALLOC_NO_WATERMARKS because this | |
5132 | * could deplete whole memory reserves which would just make | |
5133 | * the situation worse | |
5134 | */ | |
5135 | page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_HARDER, ac); | |
5136 | if (page) | |
5137 | goto got_pg; | |
5138 | ||
9a67f648 MH |
5139 | cond_resched(); |
5140 | goto retry; | |
5141 | } | |
5142 | fail: | |
a8e99259 | 5143 | warn_alloc(gfp_mask, ac->nodemask, |
7877cdcc | 5144 | "page allocation failure: order:%u", order); |
1da177e4 | 5145 | got_pg: |
072bb0aa | 5146 | return page; |
1da177e4 | 5147 | } |
11e33f6a | 5148 | |
9cd75558 | 5149 | static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order, |
04ec6264 | 5150 | int preferred_nid, nodemask_t *nodemask, |
8e6a930b | 5151 | struct alloc_context *ac, gfp_t *alloc_gfp, |
9cd75558 | 5152 | unsigned int *alloc_flags) |
11e33f6a | 5153 | { |
97a225e6 | 5154 | ac->highest_zoneidx = gfp_zone(gfp_mask); |
04ec6264 | 5155 | ac->zonelist = node_zonelist(preferred_nid, gfp_mask); |
9cd75558 | 5156 | ac->nodemask = nodemask; |
01c0bfe0 | 5157 | ac->migratetype = gfp_migratetype(gfp_mask); |
11e33f6a | 5158 | |
682a3385 | 5159 | if (cpusets_enabled()) { |
8e6a930b | 5160 | *alloc_gfp |= __GFP_HARDWALL; |
182f3d7a MS |
5161 | /* |
5162 | * When we are in the interrupt context, it is irrelevant | |
5163 | * to the current task context. It means that any node ok. | |
5164 | */ | |
88dc6f20 | 5165 | if (in_task() && !ac->nodemask) |
9cd75558 | 5166 | ac->nodemask = &cpuset_current_mems_allowed; |
51047820 VB |
5167 | else |
5168 | *alloc_flags |= ALLOC_CPUSET; | |
682a3385 MG |
5169 | } |
5170 | ||
d92a8cfc PZ |
5171 | fs_reclaim_acquire(gfp_mask); |
5172 | fs_reclaim_release(gfp_mask); | |
11e33f6a | 5173 | |
d0164adc | 5174 | might_sleep_if(gfp_mask & __GFP_DIRECT_RECLAIM); |
11e33f6a MG |
5175 | |
5176 | if (should_fail_alloc_page(gfp_mask, order)) | |
9cd75558 | 5177 | return false; |
11e33f6a | 5178 | |
8e3560d9 | 5179 | *alloc_flags = gfp_to_alloc_flags_cma(gfp_mask, *alloc_flags); |
d883c6cf | 5180 | |
c9ab0c4f | 5181 | /* Dirty zone balancing only done in the fast path */ |
9cd75558 | 5182 | ac->spread_dirty_pages = (gfp_mask & __GFP_WRITE); |
c9ab0c4f | 5183 | |
e46e7b77 MG |
5184 | /* |
5185 | * The preferred zone is used for statistics but crucially it is | |
5186 | * also used as the starting point for the zonelist iterator. It | |
5187 | * may get reset for allocations that ignore memory policies. | |
5188 | */ | |
9cd75558 | 5189 | ac->preferred_zoneref = first_zones_zonelist(ac->zonelist, |
97a225e6 | 5190 | ac->highest_zoneidx, ac->nodemask); |
a0622d05 MN |
5191 | |
5192 | return true; | |
9cd75558 MG |
5193 | } |
5194 | ||
387ba26f | 5195 | /* |
0f87d9d3 | 5196 | * __alloc_pages_bulk - Allocate a number of order-0 pages to a list or array |
387ba26f MG |
5197 | * @gfp: GFP flags for the allocation |
5198 | * @preferred_nid: The preferred NUMA node ID to allocate from | |
5199 | * @nodemask: Set of nodes to allocate from, may be NULL | |
0f87d9d3 MG |
5200 | * @nr_pages: The number of pages desired on the list or array |
5201 | * @page_list: Optional list to store the allocated pages | |
5202 | * @page_array: Optional array to store the pages | |
387ba26f MG |
5203 | * |
5204 | * This is a batched version of the page allocator that attempts to | |
0f87d9d3 MG |
5205 | * allocate nr_pages quickly. Pages are added to page_list if page_list |
5206 | * is not NULL, otherwise it is assumed that the page_array is valid. | |
387ba26f | 5207 | * |
0f87d9d3 MG |
5208 | * For lists, nr_pages is the number of pages that should be allocated. |
5209 | * | |
5210 | * For arrays, only NULL elements are populated with pages and nr_pages | |
5211 | * is the maximum number of pages that will be stored in the array. | |
5212 | * | |
5213 | * Returns the number of pages on the list or array. | |
387ba26f MG |
5214 | */ |
5215 | unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, | |
5216 | nodemask_t *nodemask, int nr_pages, | |
0f87d9d3 MG |
5217 | struct list_head *page_list, |
5218 | struct page **page_array) | |
387ba26f MG |
5219 | { |
5220 | struct page *page; | |
5221 | unsigned long flags; | |
5222 | struct zone *zone; | |
5223 | struct zoneref *z; | |
5224 | struct per_cpu_pages *pcp; | |
5225 | struct list_head *pcp_list; | |
5226 | struct alloc_context ac; | |
5227 | gfp_t alloc_gfp; | |
5228 | unsigned int alloc_flags = ALLOC_WMARK_LOW; | |
3e23060b | 5229 | int nr_populated = 0, nr_account = 0; |
387ba26f | 5230 | |
0f87d9d3 MG |
5231 | /* |
5232 | * Skip populated array elements to determine if any pages need | |
5233 | * to be allocated before disabling IRQs. | |
5234 | */ | |
b08e50dd | 5235 | while (page_array && nr_populated < nr_pages && page_array[nr_populated]) |
0f87d9d3 MG |
5236 | nr_populated++; |
5237 | ||
06147843 CL |
5238 | /* No pages requested? */ |
5239 | if (unlikely(nr_pages <= 0)) | |
5240 | goto out; | |
5241 | ||
b3b64ebd MG |
5242 | /* Already populated array? */ |
5243 | if (unlikely(page_array && nr_pages - nr_populated == 0)) | |
06147843 | 5244 | goto out; |
b3b64ebd | 5245 | |
8dcb3060 SB |
5246 | /* Bulk allocator does not support memcg accounting. */ |
5247 | if (memcg_kmem_enabled() && (gfp & __GFP_ACCOUNT)) | |
5248 | goto failed; | |
5249 | ||
387ba26f | 5250 | /* Use the single page allocator for one page. */ |
0f87d9d3 | 5251 | if (nr_pages - nr_populated == 1) |
387ba26f MG |
5252 | goto failed; |
5253 | ||
187ad460 MG |
5254 | #ifdef CONFIG_PAGE_OWNER |
5255 | /* | |
5256 | * PAGE_OWNER may recurse into the allocator to allocate space to | |
5257 | * save the stack with pagesets.lock held. Releasing/reacquiring | |
5258 | * removes much of the performance benefit of bulk allocation so | |
5259 | * force the caller to allocate one page at a time as it'll have | |
5260 | * similar performance to added complexity to the bulk allocator. | |
5261 | */ | |
5262 | if (static_branch_unlikely(&page_owner_inited)) | |
5263 | goto failed; | |
5264 | #endif | |
5265 | ||
387ba26f MG |
5266 | /* May set ALLOC_NOFRAGMENT, fragmentation will return 1 page. */ |
5267 | gfp &= gfp_allowed_mask; | |
5268 | alloc_gfp = gfp; | |
5269 | if (!prepare_alloc_pages(gfp, 0, preferred_nid, nodemask, &ac, &alloc_gfp, &alloc_flags)) | |
06147843 | 5270 | goto out; |
387ba26f MG |
5271 | gfp = alloc_gfp; |
5272 | ||
5273 | /* Find an allowed local zone that meets the low watermark. */ | |
5274 | for_each_zone_zonelist_nodemask(zone, z, ac.zonelist, ac.highest_zoneidx, ac.nodemask) { | |
5275 | unsigned long mark; | |
5276 | ||
5277 | if (cpusets_enabled() && (alloc_flags & ALLOC_CPUSET) && | |
5278 | !__cpuset_zone_allowed(zone, gfp)) { | |
5279 | continue; | |
5280 | } | |
5281 | ||
5282 | if (nr_online_nodes > 1 && zone != ac.preferred_zoneref->zone && | |
5283 | zone_to_nid(zone) != zone_to_nid(ac.preferred_zoneref->zone)) { | |
5284 | goto failed; | |
5285 | } | |
5286 | ||
5287 | mark = wmark_pages(zone, alloc_flags & ALLOC_WMARK_MASK) + nr_pages; | |
5288 | if (zone_watermark_fast(zone, 0, mark, | |
5289 | zonelist_zone_idx(ac.preferred_zoneref), | |
5290 | alloc_flags, gfp)) { | |
5291 | break; | |
5292 | } | |
5293 | } | |
5294 | ||
5295 | /* | |
5296 | * If there are no allowed local zones that meets the watermarks then | |
5297 | * try to allocate a single page and reclaim if necessary. | |
5298 | */ | |
ce76f9a1 | 5299 | if (unlikely(!zone)) |
387ba26f MG |
5300 | goto failed; |
5301 | ||
5302 | /* Attempt the batch allocation */ | |
dbbee9d5 | 5303 | local_lock_irqsave(&pagesets.lock, flags); |
28f836b6 | 5304 | pcp = this_cpu_ptr(zone->per_cpu_pageset); |
44042b44 | 5305 | pcp_list = &pcp->lists[order_to_pindex(ac.migratetype, 0)]; |
387ba26f | 5306 | |
0f87d9d3 MG |
5307 | while (nr_populated < nr_pages) { |
5308 | ||
5309 | /* Skip existing pages */ | |
5310 | if (page_array && page_array[nr_populated]) { | |
5311 | nr_populated++; | |
5312 | continue; | |
5313 | } | |
5314 | ||
44042b44 | 5315 | page = __rmqueue_pcplist(zone, 0, ac.migratetype, alloc_flags, |
387ba26f | 5316 | pcp, pcp_list); |
ce76f9a1 | 5317 | if (unlikely(!page)) { |
387ba26f | 5318 | /* Try and get at least one page */ |
0f87d9d3 | 5319 | if (!nr_populated) |
387ba26f MG |
5320 | goto failed_irq; |
5321 | break; | |
5322 | } | |
3e23060b | 5323 | nr_account++; |
387ba26f MG |
5324 | |
5325 | prep_new_page(page, 0, gfp, 0); | |
0f87d9d3 MG |
5326 | if (page_list) |
5327 | list_add(&page->lru, page_list); | |
5328 | else | |
5329 | page_array[nr_populated] = page; | |
5330 | nr_populated++; | |
387ba26f MG |
5331 | } |
5332 | ||
43c95bcc MG |
5333 | local_unlock_irqrestore(&pagesets.lock, flags); |
5334 | ||
3e23060b MG |
5335 | __count_zid_vm_events(PGALLOC, zone_idx(zone), nr_account); |
5336 | zone_statistics(ac.preferred_zoneref->zone, zone, nr_account); | |
387ba26f | 5337 | |
06147843 | 5338 | out: |
0f87d9d3 | 5339 | return nr_populated; |
387ba26f MG |
5340 | |
5341 | failed_irq: | |
dbbee9d5 | 5342 | local_unlock_irqrestore(&pagesets.lock, flags); |
387ba26f MG |
5343 | |
5344 | failed: | |
5345 | page = __alloc_pages(gfp, 0, preferred_nid, nodemask); | |
5346 | if (page) { | |
0f87d9d3 MG |
5347 | if (page_list) |
5348 | list_add(&page->lru, page_list); | |
5349 | else | |
5350 | page_array[nr_populated] = page; | |
5351 | nr_populated++; | |
387ba26f MG |
5352 | } |
5353 | ||
06147843 | 5354 | goto out; |
387ba26f MG |
5355 | } |
5356 | EXPORT_SYMBOL_GPL(__alloc_pages_bulk); | |
5357 | ||
9cd75558 MG |
5358 | /* |
5359 | * This is the 'heart' of the zoned buddy allocator. | |
5360 | */ | |
84172f4b | 5361 | struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid, |
04ec6264 | 5362 | nodemask_t *nodemask) |
9cd75558 MG |
5363 | { |
5364 | struct page *page; | |
5365 | unsigned int alloc_flags = ALLOC_WMARK_LOW; | |
8e6a930b | 5366 | gfp_t alloc_gfp; /* The gfp_t that was actually used for allocation */ |
9cd75558 MG |
5367 | struct alloc_context ac = { }; |
5368 | ||
c63ae43b MH |
5369 | /* |
5370 | * There are several places where we assume that the order value is sane | |
5371 | * so bail out early if the request is out of bound. | |
5372 | */ | |
5373 | if (unlikely(order >= MAX_ORDER)) { | |
6e5e0f28 | 5374 | WARN_ON_ONCE(!(gfp & __GFP_NOWARN)); |
c63ae43b MH |
5375 | return NULL; |
5376 | } | |
5377 | ||
6e5e0f28 | 5378 | gfp &= gfp_allowed_mask; |
da6df1b0 PT |
5379 | /* |
5380 | * Apply scoped allocation constraints. This is mainly about GFP_NOFS | |
5381 | * resp. GFP_NOIO which has to be inherited for all allocation requests | |
5382 | * from a particular context which has been marked by | |
8e3560d9 PT |
5383 | * memalloc_no{fs,io}_{save,restore}. And PF_MEMALLOC_PIN which ensures |
5384 | * movable zones are not used during allocation. | |
da6df1b0 PT |
5385 | */ |
5386 | gfp = current_gfp_context(gfp); | |
6e5e0f28 MWO |
5387 | alloc_gfp = gfp; |
5388 | if (!prepare_alloc_pages(gfp, order, preferred_nid, nodemask, &ac, | |
8e6a930b | 5389 | &alloc_gfp, &alloc_flags)) |
9cd75558 MG |
5390 | return NULL; |
5391 | ||
6bb15450 MG |
5392 | /* |
5393 | * Forbid the first pass from falling back to types that fragment | |
5394 | * memory until all local zones are considered. | |
5395 | */ | |
6e5e0f28 | 5396 | alloc_flags |= alloc_flags_nofragment(ac.preferred_zoneref->zone, gfp); |
6bb15450 | 5397 | |
5117f45d | 5398 | /* First allocation attempt */ |
8e6a930b | 5399 | page = get_page_from_freelist(alloc_gfp, order, alloc_flags, &ac); |
4fcb0971 MG |
5400 | if (likely(page)) |
5401 | goto out; | |
11e33f6a | 5402 | |
da6df1b0 | 5403 | alloc_gfp = gfp; |
4fcb0971 | 5404 | ac.spread_dirty_pages = false; |
23f086f9 | 5405 | |
4741526b MG |
5406 | /* |
5407 | * Restore the original nodemask if it was potentially replaced with | |
5408 | * &cpuset_current_mems_allowed to optimize the fast-path attempt. | |
5409 | */ | |
97ce86f9 | 5410 | ac.nodemask = nodemask; |
16096c25 | 5411 | |
8e6a930b | 5412 | page = __alloc_pages_slowpath(alloc_gfp, order, &ac); |
cc9a6c87 | 5413 | |
4fcb0971 | 5414 | out: |
6e5e0f28 MWO |
5415 | if (memcg_kmem_enabled() && (gfp & __GFP_ACCOUNT) && page && |
5416 | unlikely(__memcg_kmem_charge_page(page, gfp, order) != 0)) { | |
c4159a75 VD |
5417 | __free_pages(page, order); |
5418 | page = NULL; | |
4949148a VD |
5419 | } |
5420 | ||
8e6a930b | 5421 | trace_mm_page_alloc(page, order, alloc_gfp, ac.migratetype); |
4fcb0971 | 5422 | |
11e33f6a | 5423 | return page; |
1da177e4 | 5424 | } |
84172f4b | 5425 | EXPORT_SYMBOL(__alloc_pages); |
1da177e4 | 5426 | |
cc09cb13 MWO |
5427 | struct folio *__folio_alloc(gfp_t gfp, unsigned int order, int preferred_nid, |
5428 | nodemask_t *nodemask) | |
5429 | { | |
5430 | struct page *page = __alloc_pages(gfp | __GFP_COMP, order, | |
5431 | preferred_nid, nodemask); | |
5432 | ||
5433 | if (page && order > 1) | |
5434 | prep_transhuge_page(page); | |
5435 | return (struct folio *)page; | |
5436 | } | |
5437 | EXPORT_SYMBOL(__folio_alloc); | |
5438 | ||
1da177e4 | 5439 | /* |
9ea9a680 MH |
5440 | * Common helper functions. Never use with __GFP_HIGHMEM because the returned |
5441 | * address cannot represent highmem pages. Use alloc_pages and then kmap if | |
5442 | * you need to access high mem. | |
1da177e4 | 5443 | */ |
920c7a5d | 5444 | unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order) |
1da177e4 | 5445 | { |
945a1113 AM |
5446 | struct page *page; |
5447 | ||
9ea9a680 | 5448 | page = alloc_pages(gfp_mask & ~__GFP_HIGHMEM, order); |
1da177e4 LT |
5449 | if (!page) |
5450 | return 0; | |
5451 | return (unsigned long) page_address(page); | |
5452 | } | |
1da177e4 LT |
5453 | EXPORT_SYMBOL(__get_free_pages); |
5454 | ||
920c7a5d | 5455 | unsigned long get_zeroed_page(gfp_t gfp_mask) |
1da177e4 | 5456 | { |
945a1113 | 5457 | return __get_free_pages(gfp_mask | __GFP_ZERO, 0); |
1da177e4 | 5458 | } |
1da177e4 LT |
5459 | EXPORT_SYMBOL(get_zeroed_page); |
5460 | ||
7f194fbb MWO |
5461 | /** |
5462 | * __free_pages - Free pages allocated with alloc_pages(). | |
5463 | * @page: The page pointer returned from alloc_pages(). | |
5464 | * @order: The order of the allocation. | |
5465 | * | |
5466 | * This function can free multi-page allocations that are not compound | |
5467 | * pages. It does not check that the @order passed in matches that of | |
5468 | * the allocation, so it is easy to leak memory. Freeing more memory | |
5469 | * than was allocated will probably emit a warning. | |
5470 | * | |
5471 | * If the last reference to this page is speculative, it will be released | |
5472 | * by put_page() which only frees the first page of a non-compound | |
5473 | * allocation. To prevent the remaining pages from being leaked, we free | |
5474 | * the subsequent pages here. If you want to use the page's reference | |
5475 | * count to decide when to free the allocation, you should allocate a | |
5476 | * compound page, and use put_page() instead of __free_pages(). | |
5477 | * | |
5478 | * Context: May be called in interrupt context or while holding a normal | |
5479 | * spinlock, but not in NMI context or while holding a raw spinlock. | |
5480 | */ | |
742aa7fb AL |
5481 | void __free_pages(struct page *page, unsigned int order) |
5482 | { | |
5483 | if (put_page_testzero(page)) | |
5484 | free_the_page(page, order); | |
e320d301 MWO |
5485 | else if (!PageHead(page)) |
5486 | while (order-- > 0) | |
5487 | free_the_page(page + (1 << order), order); | |
742aa7fb | 5488 | } |
1da177e4 LT |
5489 | EXPORT_SYMBOL(__free_pages); |
5490 | ||
920c7a5d | 5491 | void free_pages(unsigned long addr, unsigned int order) |
1da177e4 LT |
5492 | { |
5493 | if (addr != 0) { | |
725d704e | 5494 | VM_BUG_ON(!virt_addr_valid((void *)addr)); |
1da177e4 LT |
5495 | __free_pages(virt_to_page((void *)addr), order); |
5496 | } | |
5497 | } | |
5498 | ||
5499 | EXPORT_SYMBOL(free_pages); | |
5500 | ||
b63ae8ca AD |
5501 | /* |
5502 | * Page Fragment: | |
5503 | * An arbitrary-length arbitrary-offset area of memory which resides | |
5504 | * within a 0 or higher order page. Multiple fragments within that page | |
5505 | * are individually refcounted, in the page's reference counter. | |
5506 | * | |
5507 | * The page_frag functions below provide a simple allocation framework for | |
5508 | * page fragments. This is used by the network stack and network device | |
5509 | * drivers to provide a backing region of memory for use as either an | |
5510 | * sk_buff->head, or to be used in the "frags" portion of skb_shared_info. | |
5511 | */ | |
2976db80 AD |
5512 | static struct page *__page_frag_cache_refill(struct page_frag_cache *nc, |
5513 | gfp_t gfp_mask) | |
b63ae8ca AD |
5514 | { |
5515 | struct page *page = NULL; | |
5516 | gfp_t gfp = gfp_mask; | |
5517 | ||
5518 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) | |
5519 | gfp_mask |= __GFP_COMP | __GFP_NOWARN | __GFP_NORETRY | | |
5520 | __GFP_NOMEMALLOC; | |
5521 | page = alloc_pages_node(NUMA_NO_NODE, gfp_mask, | |
5522 | PAGE_FRAG_CACHE_MAX_ORDER); | |
5523 | nc->size = page ? PAGE_FRAG_CACHE_MAX_SIZE : PAGE_SIZE; | |
5524 | #endif | |
5525 | if (unlikely(!page)) | |
5526 | page = alloc_pages_node(NUMA_NO_NODE, gfp, 0); | |
5527 | ||
5528 | nc->va = page ? page_address(page) : NULL; | |
5529 | ||
5530 | return page; | |
5531 | } | |
5532 | ||
2976db80 | 5533 | void __page_frag_cache_drain(struct page *page, unsigned int count) |
44fdffd7 AD |
5534 | { |
5535 | VM_BUG_ON_PAGE(page_ref_count(page) == 0, page); | |
5536 | ||
742aa7fb AL |
5537 | if (page_ref_sub_and_test(page, count)) |
5538 | free_the_page(page, compound_order(page)); | |
44fdffd7 | 5539 | } |
2976db80 | 5540 | EXPORT_SYMBOL(__page_frag_cache_drain); |
44fdffd7 | 5541 | |
b358e212 KH |
5542 | void *page_frag_alloc_align(struct page_frag_cache *nc, |
5543 | unsigned int fragsz, gfp_t gfp_mask, | |
5544 | unsigned int align_mask) | |
b63ae8ca AD |
5545 | { |
5546 | unsigned int size = PAGE_SIZE; | |
5547 | struct page *page; | |
5548 | int offset; | |
5549 | ||
5550 | if (unlikely(!nc->va)) { | |
5551 | refill: | |
2976db80 | 5552 | page = __page_frag_cache_refill(nc, gfp_mask); |
b63ae8ca AD |
5553 | if (!page) |
5554 | return NULL; | |
5555 | ||
5556 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) | |
5557 | /* if size can vary use size else just use PAGE_SIZE */ | |
5558 | size = nc->size; | |
5559 | #endif | |
5560 | /* Even if we own the page, we do not use atomic_set(). | |
5561 | * This would break get_page_unless_zero() users. | |
5562 | */ | |
86447726 | 5563 | page_ref_add(page, PAGE_FRAG_CACHE_MAX_SIZE); |
b63ae8ca AD |
5564 | |
5565 | /* reset page count bias and offset to start of new frag */ | |
2f064f34 | 5566 | nc->pfmemalloc = page_is_pfmemalloc(page); |
86447726 | 5567 | nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; |
b63ae8ca AD |
5568 | nc->offset = size; |
5569 | } | |
5570 | ||
5571 | offset = nc->offset - fragsz; | |
5572 | if (unlikely(offset < 0)) { | |
5573 | page = virt_to_page(nc->va); | |
5574 | ||
fe896d18 | 5575 | if (!page_ref_sub_and_test(page, nc->pagecnt_bias)) |
b63ae8ca AD |
5576 | goto refill; |
5577 | ||
d8c19014 DZ |
5578 | if (unlikely(nc->pfmemalloc)) { |
5579 | free_the_page(page, compound_order(page)); | |
5580 | goto refill; | |
5581 | } | |
5582 | ||
b63ae8ca AD |
5583 | #if (PAGE_SIZE < PAGE_FRAG_CACHE_MAX_SIZE) |
5584 | /* if size can vary use size else just use PAGE_SIZE */ | |
5585 | size = nc->size; | |
5586 | #endif | |
5587 | /* OK, page count is 0, we can safely set it */ | |
86447726 | 5588 | set_page_count(page, PAGE_FRAG_CACHE_MAX_SIZE + 1); |
b63ae8ca AD |
5589 | |
5590 | /* reset page count bias and offset to start of new frag */ | |
86447726 | 5591 | nc->pagecnt_bias = PAGE_FRAG_CACHE_MAX_SIZE + 1; |
b63ae8ca AD |
5592 | offset = size - fragsz; |
5593 | } | |
5594 | ||
5595 | nc->pagecnt_bias--; | |
b358e212 | 5596 | offset &= align_mask; |
b63ae8ca AD |
5597 | nc->offset = offset; |
5598 | ||
5599 | return nc->va + offset; | |
5600 | } | |
b358e212 | 5601 | EXPORT_SYMBOL(page_frag_alloc_align); |
b63ae8ca AD |
5602 | |
5603 | /* | |
5604 | * Frees a page fragment allocated out of either a compound or order 0 page. | |
5605 | */ | |
8c2dd3e4 | 5606 | void page_frag_free(void *addr) |
b63ae8ca AD |
5607 | { |
5608 | struct page *page = virt_to_head_page(addr); | |
5609 | ||
742aa7fb AL |
5610 | if (unlikely(put_page_testzero(page))) |
5611 | free_the_page(page, compound_order(page)); | |
b63ae8ca | 5612 | } |
8c2dd3e4 | 5613 | EXPORT_SYMBOL(page_frag_free); |
b63ae8ca | 5614 | |
d00181b9 KS |
5615 | static void *make_alloc_exact(unsigned long addr, unsigned int order, |
5616 | size_t size) | |
ee85c2e1 AK |
5617 | { |
5618 | if (addr) { | |
5619 | unsigned long alloc_end = addr + (PAGE_SIZE << order); | |
5620 | unsigned long used = addr + PAGE_ALIGN(size); | |
5621 | ||
5622 | split_page(virt_to_page((void *)addr), order); | |
5623 | while (used < alloc_end) { | |
5624 | free_page(used); | |
5625 | used += PAGE_SIZE; | |
5626 | } | |
5627 | } | |
5628 | return (void *)addr; | |
5629 | } | |
5630 | ||
2be0ffe2 TT |
5631 | /** |
5632 | * alloc_pages_exact - allocate an exact number physically-contiguous pages. | |
5633 | * @size: the number of bytes to allocate | |
63931eb9 | 5634 | * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP |
2be0ffe2 TT |
5635 | * |
5636 | * This function is similar to alloc_pages(), except that it allocates the | |
5637 | * minimum number of pages to satisfy the request. alloc_pages() can only | |
5638 | * allocate memory in power-of-two pages. | |
5639 | * | |
5640 | * This function is also limited by MAX_ORDER. | |
5641 | * | |
5642 | * Memory allocated by this function must be released by free_pages_exact(). | |
a862f68a MR |
5643 | * |
5644 | * Return: pointer to the allocated area or %NULL in case of error. | |
2be0ffe2 TT |
5645 | */ |
5646 | void *alloc_pages_exact(size_t size, gfp_t gfp_mask) | |
5647 | { | |
5648 | unsigned int order = get_order(size); | |
5649 | unsigned long addr; | |
5650 | ||
ba7f1b9e ML |
5651 | if (WARN_ON_ONCE(gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) |
5652 | gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); | |
63931eb9 | 5653 | |
2be0ffe2 | 5654 | addr = __get_free_pages(gfp_mask, order); |
ee85c2e1 | 5655 | return make_alloc_exact(addr, order, size); |
2be0ffe2 TT |
5656 | } |
5657 | EXPORT_SYMBOL(alloc_pages_exact); | |
5658 | ||
ee85c2e1 AK |
5659 | /** |
5660 | * alloc_pages_exact_nid - allocate an exact number of physically-contiguous | |
5661 | * pages on a node. | |
b5e6ab58 | 5662 | * @nid: the preferred node ID where memory should be allocated |
ee85c2e1 | 5663 | * @size: the number of bytes to allocate |
63931eb9 | 5664 | * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP |
ee85c2e1 AK |
5665 | * |
5666 | * Like alloc_pages_exact(), but try to allocate on node nid first before falling | |
5667 | * back. | |
a862f68a MR |
5668 | * |
5669 | * Return: pointer to the allocated area or %NULL in case of error. | |
ee85c2e1 | 5670 | */ |
e1931811 | 5671 | void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask) |
ee85c2e1 | 5672 | { |
d00181b9 | 5673 | unsigned int order = get_order(size); |
63931eb9 VB |
5674 | struct page *p; |
5675 | ||
ba7f1b9e ML |
5676 | if (WARN_ON_ONCE(gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) |
5677 | gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); | |
63931eb9 VB |
5678 | |
5679 | p = alloc_pages_node(nid, gfp_mask, order); | |
ee85c2e1 AK |
5680 | if (!p) |
5681 | return NULL; | |
5682 | return make_alloc_exact((unsigned long)page_address(p), order, size); | |
5683 | } | |
ee85c2e1 | 5684 | |
2be0ffe2 TT |
5685 | /** |
5686 | * free_pages_exact - release memory allocated via alloc_pages_exact() | |
5687 | * @virt: the value returned by alloc_pages_exact. | |
5688 | * @size: size of allocation, same value as passed to alloc_pages_exact(). | |
5689 | * | |
5690 | * Release the memory allocated by a previous call to alloc_pages_exact. | |
5691 | */ | |
5692 | void free_pages_exact(void *virt, size_t size) | |
5693 | { | |
5694 | unsigned long addr = (unsigned long)virt; | |
5695 | unsigned long end = addr + PAGE_ALIGN(size); | |
5696 | ||
5697 | while (addr < end) { | |
5698 | free_page(addr); | |
5699 | addr += PAGE_SIZE; | |
5700 | } | |
5701 | } | |
5702 | EXPORT_SYMBOL(free_pages_exact); | |
5703 | ||
e0fb5815 ZY |
5704 | /** |
5705 | * nr_free_zone_pages - count number of pages beyond high watermark | |
5706 | * @offset: The zone index of the highest zone | |
5707 | * | |
a862f68a | 5708 | * nr_free_zone_pages() counts the number of pages which are beyond the |
e0fb5815 ZY |
5709 | * high watermark within all zones at or below a given zone index. For each |
5710 | * zone, the number of pages is calculated as: | |
0e056eb5 MCC |
5711 | * |
5712 | * nr_free_zone_pages = managed_pages - high_pages | |
a862f68a MR |
5713 | * |
5714 | * Return: number of pages beyond high watermark. | |
e0fb5815 | 5715 | */ |
ebec3862 | 5716 | static unsigned long nr_free_zone_pages(int offset) |
1da177e4 | 5717 | { |
dd1a239f | 5718 | struct zoneref *z; |
54a6eb5c MG |
5719 | struct zone *zone; |
5720 | ||
e310fd43 | 5721 | /* Just pick one node, since fallback list is circular */ |
ebec3862 | 5722 | unsigned long sum = 0; |
1da177e4 | 5723 | |
0e88460d | 5724 | struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL); |
1da177e4 | 5725 | |
54a6eb5c | 5726 | for_each_zone_zonelist(zone, z, zonelist, offset) { |
9705bea5 | 5727 | unsigned long size = zone_managed_pages(zone); |
41858966 | 5728 | unsigned long high = high_wmark_pages(zone); |
e310fd43 MB |
5729 | if (size > high) |
5730 | sum += size - high; | |
1da177e4 LT |
5731 | } |
5732 | ||
5733 | return sum; | |
5734 | } | |
5735 | ||
e0fb5815 ZY |
5736 | /** |
5737 | * nr_free_buffer_pages - count number of pages beyond high watermark | |
5738 | * | |
5739 | * nr_free_buffer_pages() counts the number of pages which are beyond the high | |
5740 | * watermark within ZONE_DMA and ZONE_NORMAL. | |
a862f68a MR |
5741 | * |
5742 | * Return: number of pages beyond high watermark within ZONE_DMA and | |
5743 | * ZONE_NORMAL. | |
1da177e4 | 5744 | */ |
ebec3862 | 5745 | unsigned long nr_free_buffer_pages(void) |
1da177e4 | 5746 | { |
af4ca457 | 5747 | return nr_free_zone_pages(gfp_zone(GFP_USER)); |
1da177e4 | 5748 | } |
c2f1a551 | 5749 | EXPORT_SYMBOL_GPL(nr_free_buffer_pages); |
1da177e4 | 5750 | |
08e0f6a9 | 5751 | static inline void show_node(struct zone *zone) |
1da177e4 | 5752 | { |
e5adfffc | 5753 | if (IS_ENABLED(CONFIG_NUMA)) |
25ba77c1 | 5754 | printk("Node %d ", zone_to_nid(zone)); |
1da177e4 | 5755 | } |
1da177e4 | 5756 | |
d02bd27b IR |
5757 | long si_mem_available(void) |
5758 | { | |
5759 | long available; | |
5760 | unsigned long pagecache; | |
5761 | unsigned long wmark_low = 0; | |
5762 | unsigned long pages[NR_LRU_LISTS]; | |
b29940c1 | 5763 | unsigned long reclaimable; |
d02bd27b IR |
5764 | struct zone *zone; |
5765 | int lru; | |
5766 | ||
5767 | for (lru = LRU_BASE; lru < NR_LRU_LISTS; lru++) | |
2f95ff90 | 5768 | pages[lru] = global_node_page_state(NR_LRU_BASE + lru); |
d02bd27b IR |
5769 | |
5770 | for_each_zone(zone) | |
a9214443 | 5771 | wmark_low += low_wmark_pages(zone); |
d02bd27b IR |
5772 | |
5773 | /* | |
5774 | * Estimate the amount of memory available for userspace allocations, | |
5775 | * without causing swapping. | |
5776 | */ | |
c41f012a | 5777 | available = global_zone_page_state(NR_FREE_PAGES) - totalreserve_pages; |
d02bd27b IR |
5778 | |
5779 | /* | |
5780 | * Not all the page cache can be freed, otherwise the system will | |
5781 | * start swapping. Assume at least half of the page cache, or the | |
5782 | * low watermark worth of cache, needs to stay. | |
5783 | */ | |
5784 | pagecache = pages[LRU_ACTIVE_FILE] + pages[LRU_INACTIVE_FILE]; | |
5785 | pagecache -= min(pagecache / 2, wmark_low); | |
5786 | available += pagecache; | |
5787 | ||
5788 | /* | |
b29940c1 VB |
5789 | * Part of the reclaimable slab and other kernel memory consists of |
5790 | * items that are in use, and cannot be freed. Cap this estimate at the | |
5791 | * low watermark. | |
d02bd27b | 5792 | */ |
d42f3245 RG |
5793 | reclaimable = global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B) + |
5794 | global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE); | |
b29940c1 | 5795 | available += reclaimable - min(reclaimable / 2, wmark_low); |
034ebf65 | 5796 | |
d02bd27b IR |
5797 | if (available < 0) |
5798 | available = 0; | |
5799 | return available; | |
5800 | } | |
5801 | EXPORT_SYMBOL_GPL(si_mem_available); | |
5802 | ||
1da177e4 LT |
5803 | void si_meminfo(struct sysinfo *val) |
5804 | { | |
ca79b0c2 | 5805 | val->totalram = totalram_pages(); |
11fb9989 | 5806 | val->sharedram = global_node_page_state(NR_SHMEM); |
c41f012a | 5807 | val->freeram = global_zone_page_state(NR_FREE_PAGES); |
1da177e4 | 5808 | val->bufferram = nr_blockdev_pages(); |
ca79b0c2 | 5809 | val->totalhigh = totalhigh_pages(); |
1da177e4 | 5810 | val->freehigh = nr_free_highpages(); |
1da177e4 LT |
5811 | val->mem_unit = PAGE_SIZE; |
5812 | } | |
5813 | ||
5814 | EXPORT_SYMBOL(si_meminfo); | |
5815 | ||
5816 | #ifdef CONFIG_NUMA | |
5817 | void si_meminfo_node(struct sysinfo *val, int nid) | |
5818 | { | |
cdd91a77 JL |
5819 | int zone_type; /* needs to be signed */ |
5820 | unsigned long managed_pages = 0; | |
fc2bd799 JK |
5821 | unsigned long managed_highpages = 0; |
5822 | unsigned long free_highpages = 0; | |
1da177e4 LT |
5823 | pg_data_t *pgdat = NODE_DATA(nid); |
5824 | ||
cdd91a77 | 5825 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) |
9705bea5 | 5826 | managed_pages += zone_managed_pages(&pgdat->node_zones[zone_type]); |
cdd91a77 | 5827 | val->totalram = managed_pages; |
11fb9989 | 5828 | val->sharedram = node_page_state(pgdat, NR_SHMEM); |
75ef7184 | 5829 | val->freeram = sum_zone_node_page_state(nid, NR_FREE_PAGES); |
98d2b0eb | 5830 | #ifdef CONFIG_HIGHMEM |
fc2bd799 JK |
5831 | for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) { |
5832 | struct zone *zone = &pgdat->node_zones[zone_type]; | |
5833 | ||
5834 | if (is_highmem(zone)) { | |
9705bea5 | 5835 | managed_highpages += zone_managed_pages(zone); |
fc2bd799 JK |
5836 | free_highpages += zone_page_state(zone, NR_FREE_PAGES); |
5837 | } | |
5838 | } | |
5839 | val->totalhigh = managed_highpages; | |
5840 | val->freehigh = free_highpages; | |
98d2b0eb | 5841 | #else |
fc2bd799 JK |
5842 | val->totalhigh = managed_highpages; |
5843 | val->freehigh = free_highpages; | |
98d2b0eb | 5844 | #endif |
1da177e4 LT |
5845 | val->mem_unit = PAGE_SIZE; |
5846 | } | |
5847 | #endif | |
5848 | ||
ddd588b5 | 5849 | /* |
7bf02ea2 DR |
5850 | * Determine whether the node should be displayed or not, depending on whether |
5851 | * SHOW_MEM_FILTER_NODES was passed to show_free_areas(). | |
ddd588b5 | 5852 | */ |
9af744d7 | 5853 | static bool show_mem_node_skip(unsigned int flags, int nid, nodemask_t *nodemask) |
ddd588b5 | 5854 | { |
ddd588b5 | 5855 | if (!(flags & SHOW_MEM_FILTER_NODES)) |
9af744d7 | 5856 | return false; |
ddd588b5 | 5857 | |
9af744d7 MH |
5858 | /* |
5859 | * no node mask - aka implicit memory numa policy. Do not bother with | |
5860 | * the synchronization - read_mems_allowed_begin - because we do not | |
5861 | * have to be precise here. | |
5862 | */ | |
5863 | if (!nodemask) | |
5864 | nodemask = &cpuset_current_mems_allowed; | |
5865 | ||
5866 | return !node_isset(nid, *nodemask); | |
ddd588b5 DR |
5867 | } |
5868 | ||
1da177e4 LT |
5869 | #define K(x) ((x) << (PAGE_SHIFT-10)) |
5870 | ||
377e4f16 RV |
5871 | static void show_migration_types(unsigned char type) |
5872 | { | |
5873 | static const char types[MIGRATE_TYPES] = { | |
5874 | [MIGRATE_UNMOVABLE] = 'U', | |
377e4f16 | 5875 | [MIGRATE_MOVABLE] = 'M', |
475a2f90 VB |
5876 | [MIGRATE_RECLAIMABLE] = 'E', |
5877 | [MIGRATE_HIGHATOMIC] = 'H', | |
377e4f16 RV |
5878 | #ifdef CONFIG_CMA |
5879 | [MIGRATE_CMA] = 'C', | |
5880 | #endif | |
194159fb | 5881 | #ifdef CONFIG_MEMORY_ISOLATION |
377e4f16 | 5882 | [MIGRATE_ISOLATE] = 'I', |
194159fb | 5883 | #endif |
377e4f16 RV |
5884 | }; |
5885 | char tmp[MIGRATE_TYPES + 1]; | |
5886 | char *p = tmp; | |
5887 | int i; | |
5888 | ||
5889 | for (i = 0; i < MIGRATE_TYPES; i++) { | |
5890 | if (type & (1 << i)) | |
5891 | *p++ = types[i]; | |
5892 | } | |
5893 | ||
5894 | *p = '\0'; | |
1f84a18f | 5895 | printk(KERN_CONT "(%s) ", tmp); |
377e4f16 RV |
5896 | } |
5897 | ||
1da177e4 LT |
5898 | /* |
5899 | * Show free area list (used inside shift_scroll-lock stuff) | |
5900 | * We also calculate the percentage fragmentation. We do this by counting the | |
5901 | * memory on each free list with the exception of the first item on the list. | |
d1bfcdb8 KK |
5902 | * |
5903 | * Bits in @filter: | |
5904 | * SHOW_MEM_FILTER_NODES: suppress nodes that are not allowed by current's | |
5905 | * cpuset. | |
1da177e4 | 5906 | */ |
9af744d7 | 5907 | void show_free_areas(unsigned int filter, nodemask_t *nodemask) |
1da177e4 | 5908 | { |
d1bfcdb8 | 5909 | unsigned long free_pcp = 0; |
c7241913 | 5910 | int cpu; |
1da177e4 | 5911 | struct zone *zone; |
599d0c95 | 5912 | pg_data_t *pgdat; |
1da177e4 | 5913 | |
ee99c71c | 5914 | for_each_populated_zone(zone) { |
9af744d7 | 5915 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
ddd588b5 | 5916 | continue; |
d1bfcdb8 | 5917 | |
761b0677 | 5918 | for_each_online_cpu(cpu) |
28f836b6 | 5919 | free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count; |
1da177e4 LT |
5920 | } |
5921 | ||
a731286d KM |
5922 | printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n" |
5923 | " active_file:%lu inactive_file:%lu isolated_file:%lu\n" | |
8d92890b | 5924 | " unevictable:%lu dirty:%lu writeback:%lu\n" |
d1bfcdb8 | 5925 | " slab_reclaimable:%lu slab_unreclaimable:%lu\n" |
d1ce749a | 5926 | " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n" |
eb2169ce | 5927 | " kernel_misc_reclaimable:%lu\n" |
d1bfcdb8 | 5928 | " free:%lu free_pcp:%lu free_cma:%lu\n", |
599d0c95 MG |
5929 | global_node_page_state(NR_ACTIVE_ANON), |
5930 | global_node_page_state(NR_INACTIVE_ANON), | |
5931 | global_node_page_state(NR_ISOLATED_ANON), | |
5932 | global_node_page_state(NR_ACTIVE_FILE), | |
5933 | global_node_page_state(NR_INACTIVE_FILE), | |
5934 | global_node_page_state(NR_ISOLATED_FILE), | |
5935 | global_node_page_state(NR_UNEVICTABLE), | |
11fb9989 MG |
5936 | global_node_page_state(NR_FILE_DIRTY), |
5937 | global_node_page_state(NR_WRITEBACK), | |
d42f3245 RG |
5938 | global_node_page_state_pages(NR_SLAB_RECLAIMABLE_B), |
5939 | global_node_page_state_pages(NR_SLAB_UNRECLAIMABLE_B), | |
50658e2e | 5940 | global_node_page_state(NR_FILE_MAPPED), |
11fb9989 | 5941 | global_node_page_state(NR_SHMEM), |
f0c0c115 | 5942 | global_node_page_state(NR_PAGETABLE), |
c41f012a | 5943 | global_zone_page_state(NR_BOUNCE), |
eb2169ce | 5944 | global_node_page_state(NR_KERNEL_MISC_RECLAIMABLE), |
c41f012a | 5945 | global_zone_page_state(NR_FREE_PAGES), |
d1bfcdb8 | 5946 | free_pcp, |
c41f012a | 5947 | global_zone_page_state(NR_FREE_CMA_PAGES)); |
1da177e4 | 5948 | |
599d0c95 | 5949 | for_each_online_pgdat(pgdat) { |
9af744d7 | 5950 | if (show_mem_node_skip(filter, pgdat->node_id, nodemask)) |
c02e50bb MH |
5951 | continue; |
5952 | ||
599d0c95 MG |
5953 | printk("Node %d" |
5954 | " active_anon:%lukB" | |
5955 | " inactive_anon:%lukB" | |
5956 | " active_file:%lukB" | |
5957 | " inactive_file:%lukB" | |
5958 | " unevictable:%lukB" | |
5959 | " isolated(anon):%lukB" | |
5960 | " isolated(file):%lukB" | |
50658e2e | 5961 | " mapped:%lukB" |
11fb9989 MG |
5962 | " dirty:%lukB" |
5963 | " writeback:%lukB" | |
5964 | " shmem:%lukB" | |
5965 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE | |
5966 | " shmem_thp: %lukB" | |
5967 | " shmem_pmdmapped: %lukB" | |
5968 | " anon_thp: %lukB" | |
5969 | #endif | |
5970 | " writeback_tmp:%lukB" | |
991e7673 SB |
5971 | " kernel_stack:%lukB" |
5972 | #ifdef CONFIG_SHADOW_CALL_STACK | |
5973 | " shadow_call_stack:%lukB" | |
5974 | #endif | |
f0c0c115 | 5975 | " pagetables:%lukB" |
599d0c95 MG |
5976 | " all_unreclaimable? %s" |
5977 | "\n", | |
5978 | pgdat->node_id, | |
5979 | K(node_page_state(pgdat, NR_ACTIVE_ANON)), | |
5980 | K(node_page_state(pgdat, NR_INACTIVE_ANON)), | |
5981 | K(node_page_state(pgdat, NR_ACTIVE_FILE)), | |
5982 | K(node_page_state(pgdat, NR_INACTIVE_FILE)), | |
5983 | K(node_page_state(pgdat, NR_UNEVICTABLE)), | |
5984 | K(node_page_state(pgdat, NR_ISOLATED_ANON)), | |
5985 | K(node_page_state(pgdat, NR_ISOLATED_FILE)), | |
50658e2e | 5986 | K(node_page_state(pgdat, NR_FILE_MAPPED)), |
11fb9989 MG |
5987 | K(node_page_state(pgdat, NR_FILE_DIRTY)), |
5988 | K(node_page_state(pgdat, NR_WRITEBACK)), | |
1f06b81a | 5989 | K(node_page_state(pgdat, NR_SHMEM)), |
11fb9989 | 5990 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
57b2847d | 5991 | K(node_page_state(pgdat, NR_SHMEM_THPS)), |
a1528e21 | 5992 | K(node_page_state(pgdat, NR_SHMEM_PMDMAPPED)), |
69473e5d | 5993 | K(node_page_state(pgdat, NR_ANON_THPS)), |
11fb9989 | 5994 | #endif |
11fb9989 | 5995 | K(node_page_state(pgdat, NR_WRITEBACK_TEMP)), |
991e7673 SB |
5996 | node_page_state(pgdat, NR_KERNEL_STACK_KB), |
5997 | #ifdef CONFIG_SHADOW_CALL_STACK | |
5998 | node_page_state(pgdat, NR_KERNEL_SCS_KB), | |
5999 | #endif | |
f0c0c115 | 6000 | K(node_page_state(pgdat, NR_PAGETABLE)), |
c73322d0 JW |
6001 | pgdat->kswapd_failures >= MAX_RECLAIM_RETRIES ? |
6002 | "yes" : "no"); | |
599d0c95 MG |
6003 | } |
6004 | ||
ee99c71c | 6005 | for_each_populated_zone(zone) { |
1da177e4 LT |
6006 | int i; |
6007 | ||
9af744d7 | 6008 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
ddd588b5 | 6009 | continue; |
d1bfcdb8 KK |
6010 | |
6011 | free_pcp = 0; | |
6012 | for_each_online_cpu(cpu) | |
28f836b6 | 6013 | free_pcp += per_cpu_ptr(zone->per_cpu_pageset, cpu)->count; |
d1bfcdb8 | 6014 | |
1da177e4 | 6015 | show_node(zone); |
1f84a18f JP |
6016 | printk(KERN_CONT |
6017 | "%s" | |
1da177e4 | 6018 | " free:%lukB" |
a6ea8b5b | 6019 | " boost:%lukB" |
1da177e4 LT |
6020 | " min:%lukB" |
6021 | " low:%lukB" | |
6022 | " high:%lukB" | |
e47b346a | 6023 | " reserved_highatomic:%luKB" |
71c799f4 MK |
6024 | " active_anon:%lukB" |
6025 | " inactive_anon:%lukB" | |
6026 | " active_file:%lukB" | |
6027 | " inactive_file:%lukB" | |
6028 | " unevictable:%lukB" | |
5a1c84b4 | 6029 | " writepending:%lukB" |
1da177e4 | 6030 | " present:%lukB" |
9feedc9d | 6031 | " managed:%lukB" |
4a0aa73f | 6032 | " mlocked:%lukB" |
4a0aa73f | 6033 | " bounce:%lukB" |
d1bfcdb8 KK |
6034 | " free_pcp:%lukB" |
6035 | " local_pcp:%ukB" | |
d1ce749a | 6036 | " free_cma:%lukB" |
1da177e4 LT |
6037 | "\n", |
6038 | zone->name, | |
88f5acf8 | 6039 | K(zone_page_state(zone, NR_FREE_PAGES)), |
a6ea8b5b | 6040 | K(zone->watermark_boost), |
41858966 MG |
6041 | K(min_wmark_pages(zone)), |
6042 | K(low_wmark_pages(zone)), | |
6043 | K(high_wmark_pages(zone)), | |
e47b346a | 6044 | K(zone->nr_reserved_highatomic), |
71c799f4 MK |
6045 | K(zone_page_state(zone, NR_ZONE_ACTIVE_ANON)), |
6046 | K(zone_page_state(zone, NR_ZONE_INACTIVE_ANON)), | |
6047 | K(zone_page_state(zone, NR_ZONE_ACTIVE_FILE)), | |
6048 | K(zone_page_state(zone, NR_ZONE_INACTIVE_FILE)), | |
6049 | K(zone_page_state(zone, NR_ZONE_UNEVICTABLE)), | |
5a1c84b4 | 6050 | K(zone_page_state(zone, NR_ZONE_WRITE_PENDING)), |
1da177e4 | 6051 | K(zone->present_pages), |
9705bea5 | 6052 | K(zone_managed_pages(zone)), |
4a0aa73f | 6053 | K(zone_page_state(zone, NR_MLOCK)), |
4a0aa73f | 6054 | K(zone_page_state(zone, NR_BOUNCE)), |
d1bfcdb8 | 6055 | K(free_pcp), |
28f836b6 | 6056 | K(this_cpu_read(zone->per_cpu_pageset->count)), |
33e077bd | 6057 | K(zone_page_state(zone, NR_FREE_CMA_PAGES))); |
1da177e4 LT |
6058 | printk("lowmem_reserve[]:"); |
6059 | for (i = 0; i < MAX_NR_ZONES; i++) | |
1f84a18f JP |
6060 | printk(KERN_CONT " %ld", zone->lowmem_reserve[i]); |
6061 | printk(KERN_CONT "\n"); | |
1da177e4 LT |
6062 | } |
6063 | ||
ee99c71c | 6064 | for_each_populated_zone(zone) { |
d00181b9 KS |
6065 | unsigned int order; |
6066 | unsigned long nr[MAX_ORDER], flags, total = 0; | |
377e4f16 | 6067 | unsigned char types[MAX_ORDER]; |
1da177e4 | 6068 | |
9af744d7 | 6069 | if (show_mem_node_skip(filter, zone_to_nid(zone), nodemask)) |
ddd588b5 | 6070 | continue; |
1da177e4 | 6071 | show_node(zone); |
1f84a18f | 6072 | printk(KERN_CONT "%s: ", zone->name); |
1da177e4 LT |
6073 | |
6074 | spin_lock_irqsave(&zone->lock, flags); | |
6075 | for (order = 0; order < MAX_ORDER; order++) { | |
377e4f16 RV |
6076 | struct free_area *area = &zone->free_area[order]; |
6077 | int type; | |
6078 | ||
6079 | nr[order] = area->nr_free; | |
8f9de51a | 6080 | total += nr[order] << order; |
377e4f16 RV |
6081 | |
6082 | types[order] = 0; | |
6083 | for (type = 0; type < MIGRATE_TYPES; type++) { | |
b03641af | 6084 | if (!free_area_empty(area, type)) |
377e4f16 RV |
6085 | types[order] |= 1 << type; |
6086 | } | |
1da177e4 LT |
6087 | } |
6088 | spin_unlock_irqrestore(&zone->lock, flags); | |
377e4f16 | 6089 | for (order = 0; order < MAX_ORDER; order++) { |
1f84a18f JP |
6090 | printk(KERN_CONT "%lu*%lukB ", |
6091 | nr[order], K(1UL) << order); | |
377e4f16 RV |
6092 | if (nr[order]) |
6093 | show_migration_types(types[order]); | |
6094 | } | |
1f84a18f | 6095 | printk(KERN_CONT "= %lukB\n", K(total)); |
1da177e4 LT |
6096 | } |
6097 | ||
949f7ec5 DR |
6098 | hugetlb_show_meminfo(); |
6099 | ||
11fb9989 | 6100 | printk("%ld total pagecache pages\n", global_node_page_state(NR_FILE_PAGES)); |
e6f3602d | 6101 | |
1da177e4 LT |
6102 | show_swap_cache_info(); |
6103 | } | |
6104 | ||
19770b32 MG |
6105 | static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref) |
6106 | { | |
6107 | zoneref->zone = zone; | |
6108 | zoneref->zone_idx = zone_idx(zone); | |
6109 | } | |
6110 | ||
1da177e4 LT |
6111 | /* |
6112 | * Builds allocation fallback zone lists. | |
1a93205b CL |
6113 | * |
6114 | * Add all populated zones of a node to the zonelist. | |
1da177e4 | 6115 | */ |
9d3be21b | 6116 | static int build_zonerefs_node(pg_data_t *pgdat, struct zoneref *zonerefs) |
1da177e4 | 6117 | { |
1a93205b | 6118 | struct zone *zone; |
bc732f1d | 6119 | enum zone_type zone_type = MAX_NR_ZONES; |
9d3be21b | 6120 | int nr_zones = 0; |
02a68a5e CL |
6121 | |
6122 | do { | |
2f6726e5 | 6123 | zone_type--; |
070f8032 | 6124 | zone = pgdat->node_zones + zone_type; |
e553f62f | 6125 | if (populated_zone(zone)) { |
9d3be21b | 6126 | zoneref_set_zone(zone, &zonerefs[nr_zones++]); |
070f8032 | 6127 | check_highest_zone(zone_type); |
1da177e4 | 6128 | } |
2f6726e5 | 6129 | } while (zone_type); |
bc732f1d | 6130 | |
070f8032 | 6131 | return nr_zones; |
1da177e4 LT |
6132 | } |
6133 | ||
6134 | #ifdef CONFIG_NUMA | |
f0c0b2b8 KH |
6135 | |
6136 | static int __parse_numa_zonelist_order(char *s) | |
6137 | { | |
c9bff3ee | 6138 | /* |
f0953a1b | 6139 | * We used to support different zonelists modes but they turned |
c9bff3ee MH |
6140 | * out to be just not useful. Let's keep the warning in place |
6141 | * if somebody still use the cmd line parameter so that we do | |
6142 | * not fail it silently | |
6143 | */ | |
6144 | if (!(*s == 'd' || *s == 'D' || *s == 'n' || *s == 'N')) { | |
6145 | pr_warn("Ignoring unsupported numa_zonelist_order value: %s\n", s); | |
f0c0b2b8 KH |
6146 | return -EINVAL; |
6147 | } | |
6148 | return 0; | |
6149 | } | |
6150 | ||
c9bff3ee MH |
6151 | char numa_zonelist_order[] = "Node"; |
6152 | ||
f0c0b2b8 KH |
6153 | /* |
6154 | * sysctl handler for numa_zonelist_order | |
6155 | */ | |
cccad5b9 | 6156 | int numa_zonelist_order_handler(struct ctl_table *table, int write, |
32927393 | 6157 | void *buffer, size_t *length, loff_t *ppos) |
f0c0b2b8 | 6158 | { |
32927393 CH |
6159 | if (write) |
6160 | return __parse_numa_zonelist_order(buffer); | |
6161 | return proc_dostring(table, write, buffer, length, ppos); | |
f0c0b2b8 KH |
6162 | } |
6163 | ||
6164 | ||
f0c0b2b8 KH |
6165 | static int node_load[MAX_NUMNODES]; |
6166 | ||
1da177e4 | 6167 | /** |
4dc3b16b | 6168 | * find_next_best_node - find the next node that should appear in a given node's fallback list |
1da177e4 LT |
6169 | * @node: node whose fallback list we're appending |
6170 | * @used_node_mask: nodemask_t of already used nodes | |
6171 | * | |
6172 | * We use a number of factors to determine which is the next node that should | |
6173 | * appear on a given node's fallback list. The node should not have appeared | |
6174 | * already in @node's fallback list, and it should be the next closest node | |
6175 | * according to the distance array (which contains arbitrary distance values | |
6176 | * from each node to each node in the system), and should also prefer nodes | |
6177 | * with no CPUs, since presumably they'll have very little allocation pressure | |
6178 | * on them otherwise. | |
a862f68a MR |
6179 | * |
6180 | * Return: node id of the found node or %NUMA_NO_NODE if no node is found. | |
1da177e4 | 6181 | */ |
79c28a41 | 6182 | int find_next_best_node(int node, nodemask_t *used_node_mask) |
1da177e4 | 6183 | { |
4cf808eb | 6184 | int n, val; |
1da177e4 | 6185 | int min_val = INT_MAX; |
00ef2d2f | 6186 | int best_node = NUMA_NO_NODE; |
1da177e4 | 6187 | |
4cf808eb LT |
6188 | /* Use the local node if we haven't already */ |
6189 | if (!node_isset(node, *used_node_mask)) { | |
6190 | node_set(node, *used_node_mask); | |
6191 | return node; | |
6192 | } | |
1da177e4 | 6193 | |
4b0ef1fe | 6194 | for_each_node_state(n, N_MEMORY) { |
1da177e4 LT |
6195 | |
6196 | /* Don't want a node to appear more than once */ | |
6197 | if (node_isset(n, *used_node_mask)) | |
6198 | continue; | |
6199 | ||
1da177e4 LT |
6200 | /* Use the distance array to find the distance */ |
6201 | val = node_distance(node, n); | |
6202 | ||
4cf808eb LT |
6203 | /* Penalize nodes under us ("prefer the next node") */ |
6204 | val += (n < node); | |
6205 | ||
1da177e4 | 6206 | /* Give preference to headless and unused nodes */ |
b630749f | 6207 | if (!cpumask_empty(cpumask_of_node(n))) |
1da177e4 LT |
6208 | val += PENALTY_FOR_NODE_WITH_CPUS; |
6209 | ||
6210 | /* Slight preference for less loaded node */ | |
37931324 | 6211 | val *= MAX_NUMNODES; |
1da177e4 LT |
6212 | val += node_load[n]; |
6213 | ||
6214 | if (val < min_val) { | |
6215 | min_val = val; | |
6216 | best_node = n; | |
6217 | } | |
6218 | } | |
6219 | ||
6220 | if (best_node >= 0) | |
6221 | node_set(best_node, *used_node_mask); | |
6222 | ||
6223 | return best_node; | |
6224 | } | |
6225 | ||
f0c0b2b8 KH |
6226 | |
6227 | /* | |
6228 | * Build zonelists ordered by node and zones within node. | |
6229 | * This results in maximum locality--normal zone overflows into local | |
6230 | * DMA zone, if any--but risks exhausting DMA zone. | |
6231 | */ | |
9d3be21b MH |
6232 | static void build_zonelists_in_node_order(pg_data_t *pgdat, int *node_order, |
6233 | unsigned nr_nodes) | |
1da177e4 | 6234 | { |
9d3be21b MH |
6235 | struct zoneref *zonerefs; |
6236 | int i; | |
6237 | ||
6238 | zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; | |
6239 | ||
6240 | for (i = 0; i < nr_nodes; i++) { | |
6241 | int nr_zones; | |
6242 | ||
6243 | pg_data_t *node = NODE_DATA(node_order[i]); | |
f0c0b2b8 | 6244 | |
9d3be21b MH |
6245 | nr_zones = build_zonerefs_node(node, zonerefs); |
6246 | zonerefs += nr_zones; | |
6247 | } | |
6248 | zonerefs->zone = NULL; | |
6249 | zonerefs->zone_idx = 0; | |
f0c0b2b8 KH |
6250 | } |
6251 | ||
523b9458 CL |
6252 | /* |
6253 | * Build gfp_thisnode zonelists | |
6254 | */ | |
6255 | static void build_thisnode_zonelists(pg_data_t *pgdat) | |
6256 | { | |
9d3be21b MH |
6257 | struct zoneref *zonerefs; |
6258 | int nr_zones; | |
523b9458 | 6259 | |
9d3be21b MH |
6260 | zonerefs = pgdat->node_zonelists[ZONELIST_NOFALLBACK]._zonerefs; |
6261 | nr_zones = build_zonerefs_node(pgdat, zonerefs); | |
6262 | zonerefs += nr_zones; | |
6263 | zonerefs->zone = NULL; | |
6264 | zonerefs->zone_idx = 0; | |
523b9458 CL |
6265 | } |
6266 | ||
f0c0b2b8 KH |
6267 | /* |
6268 | * Build zonelists ordered by zone and nodes within zones. | |
6269 | * This results in conserving DMA zone[s] until all Normal memory is | |
6270 | * exhausted, but results in overflowing to remote node while memory | |
6271 | * may still exist in local DMA zone. | |
6272 | */ | |
f0c0b2b8 | 6273 | |
f0c0b2b8 KH |
6274 | static void build_zonelists(pg_data_t *pgdat) |
6275 | { | |
9d3be21b | 6276 | static int node_order[MAX_NUMNODES]; |
37931324 | 6277 | int node, nr_nodes = 0; |
d0ddf49b | 6278 | nodemask_t used_mask = NODE_MASK_NONE; |
f0c0b2b8 | 6279 | int local_node, prev_node; |
1da177e4 LT |
6280 | |
6281 | /* NUMA-aware ordering of nodes */ | |
6282 | local_node = pgdat->node_id; | |
1da177e4 | 6283 | prev_node = local_node; |
f0c0b2b8 | 6284 | |
f0c0b2b8 | 6285 | memset(node_order, 0, sizeof(node_order)); |
1da177e4 LT |
6286 | while ((node = find_next_best_node(local_node, &used_mask)) >= 0) { |
6287 | /* | |
6288 | * We don't want to pressure a particular node. | |
6289 | * So adding penalty to the first node in same | |
6290 | * distance group to make it round-robin. | |
6291 | */ | |
957f822a DR |
6292 | if (node_distance(local_node, node) != |
6293 | node_distance(local_node, prev_node)) | |
37931324 | 6294 | node_load[node] += 1; |
f0c0b2b8 | 6295 | |
9d3be21b | 6296 | node_order[nr_nodes++] = node; |
1da177e4 | 6297 | prev_node = node; |
1da177e4 | 6298 | } |
523b9458 | 6299 | |
9d3be21b | 6300 | build_zonelists_in_node_order(pgdat, node_order, nr_nodes); |
523b9458 | 6301 | build_thisnode_zonelists(pgdat); |
6cf25392 BR |
6302 | pr_info("Fallback order for Node %d: ", local_node); |
6303 | for (node = 0; node < nr_nodes; node++) | |
6304 | pr_cont("%d ", node_order[node]); | |
6305 | pr_cont("\n"); | |
1da177e4 LT |
6306 | } |
6307 | ||
7aac7898 LS |
6308 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
6309 | /* | |
6310 | * Return node id of node used for "local" allocations. | |
6311 | * I.e., first node id of first zone in arg node's generic zonelist. | |
6312 | * Used for initializing percpu 'numa_mem', which is used primarily | |
6313 | * for kernel allocations, so use GFP_KERNEL flags to locate zonelist. | |
6314 | */ | |
6315 | int local_memory_node(int node) | |
6316 | { | |
c33d6c06 | 6317 | struct zoneref *z; |
7aac7898 | 6318 | |
c33d6c06 | 6319 | z = first_zones_zonelist(node_zonelist(node, GFP_KERNEL), |
7aac7898 | 6320 | gfp_zone(GFP_KERNEL), |
c33d6c06 | 6321 | NULL); |
c1093b74 | 6322 | return zone_to_nid(z->zone); |
7aac7898 LS |
6323 | } |
6324 | #endif | |
f0c0b2b8 | 6325 | |
6423aa81 JK |
6326 | static void setup_min_unmapped_ratio(void); |
6327 | static void setup_min_slab_ratio(void); | |
1da177e4 LT |
6328 | #else /* CONFIG_NUMA */ |
6329 | ||
f0c0b2b8 | 6330 | static void build_zonelists(pg_data_t *pgdat) |
1da177e4 | 6331 | { |
19655d34 | 6332 | int node, local_node; |
9d3be21b MH |
6333 | struct zoneref *zonerefs; |
6334 | int nr_zones; | |
1da177e4 LT |
6335 | |
6336 | local_node = pgdat->node_id; | |
1da177e4 | 6337 | |
9d3be21b MH |
6338 | zonerefs = pgdat->node_zonelists[ZONELIST_FALLBACK]._zonerefs; |
6339 | nr_zones = build_zonerefs_node(pgdat, zonerefs); | |
6340 | zonerefs += nr_zones; | |
1da177e4 | 6341 | |
54a6eb5c MG |
6342 | /* |
6343 | * Now we build the zonelist so that it contains the zones | |
6344 | * of all the other nodes. | |
6345 | * We don't want to pressure a particular node, so when | |
6346 | * building the zones for node N, we make sure that the | |
6347 | * zones coming right after the local ones are those from | |
6348 | * node N+1 (modulo N) | |
6349 | */ | |
6350 | for (node = local_node + 1; node < MAX_NUMNODES; node++) { | |
6351 | if (!node_online(node)) | |
6352 | continue; | |
9d3be21b MH |
6353 | nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs); |
6354 | zonerefs += nr_zones; | |
1da177e4 | 6355 | } |
54a6eb5c MG |
6356 | for (node = 0; node < local_node; node++) { |
6357 | if (!node_online(node)) | |
6358 | continue; | |
9d3be21b MH |
6359 | nr_zones = build_zonerefs_node(NODE_DATA(node), zonerefs); |
6360 | zonerefs += nr_zones; | |
54a6eb5c MG |
6361 | } |
6362 | ||
9d3be21b MH |
6363 | zonerefs->zone = NULL; |
6364 | zonerefs->zone_idx = 0; | |
1da177e4 LT |
6365 | } |
6366 | ||
6367 | #endif /* CONFIG_NUMA */ | |
6368 | ||
99dcc3e5 CL |
6369 | /* |
6370 | * Boot pageset table. One per cpu which is going to be used for all | |
6371 | * zones and all nodes. The parameters will be set in such a way | |
6372 | * that an item put on a list will immediately be handed over to | |
6373 | * the buddy list. This is safe since pageset manipulation is done | |
6374 | * with interrupts disabled. | |
6375 | * | |
6376 | * The boot_pagesets must be kept even after bootup is complete for | |
6377 | * unused processors and/or zones. They do play a role for bootstrapping | |
6378 | * hotplugged processors. | |
6379 | * | |
6380 | * zoneinfo_show() and maybe other functions do | |
6381 | * not check if the processor is online before following the pageset pointer. | |
6382 | * Other parts of the kernel may not check if the zone is available. | |
6383 | */ | |
28f836b6 | 6384 | static void per_cpu_pages_init(struct per_cpu_pages *pcp, struct per_cpu_zonestat *pzstats); |
952eaf81 VB |
6385 | /* These effectively disable the pcplists in the boot pageset completely */ |
6386 | #define BOOT_PAGESET_HIGH 0 | |
6387 | #define BOOT_PAGESET_BATCH 1 | |
28f836b6 MG |
6388 | static DEFINE_PER_CPU(struct per_cpu_pages, boot_pageset); |
6389 | static DEFINE_PER_CPU(struct per_cpu_zonestat, boot_zonestats); | |
09f49dca | 6390 | DEFINE_PER_CPU(struct per_cpu_nodestat, boot_nodestats); |
99dcc3e5 | 6391 | |
11cd8638 | 6392 | static void __build_all_zonelists(void *data) |
1da177e4 | 6393 | { |
6811378e | 6394 | int nid; |
afb6ebb3 | 6395 | int __maybe_unused cpu; |
9adb62a5 | 6396 | pg_data_t *self = data; |
b93e0f32 MH |
6397 | static DEFINE_SPINLOCK(lock); |
6398 | ||
6399 | spin_lock(&lock); | |
9276b1bc | 6400 | |
7f9cfb31 BL |
6401 | #ifdef CONFIG_NUMA |
6402 | memset(node_load, 0, sizeof(node_load)); | |
6403 | #endif | |
9adb62a5 | 6404 | |
c1152583 WY |
6405 | /* |
6406 | * This node is hotadded and no memory is yet present. So just | |
6407 | * building zonelists is fine - no need to touch other nodes. | |
6408 | */ | |
9adb62a5 JL |
6409 | if (self && !node_online(self->node_id)) { |
6410 | build_zonelists(self); | |
c1152583 | 6411 | } else { |
09f49dca MH |
6412 | /* |
6413 | * All possible nodes have pgdat preallocated | |
6414 | * in free_area_init | |
6415 | */ | |
6416 | for_each_node(nid) { | |
c1152583 | 6417 | pg_data_t *pgdat = NODE_DATA(nid); |
7ea1530a | 6418 | |
c1152583 WY |
6419 | build_zonelists(pgdat); |
6420 | } | |
99dcc3e5 | 6421 | |
7aac7898 LS |
6422 | #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
6423 | /* | |
6424 | * We now know the "local memory node" for each node-- | |
6425 | * i.e., the node of the first zone in the generic zonelist. | |
6426 | * Set up numa_mem percpu variable for on-line cpus. During | |
6427 | * boot, only the boot cpu should be on-line; we'll init the | |
6428 | * secondary cpus' numa_mem as they come on-line. During | |
6429 | * node/memory hotplug, we'll fixup all on-line cpus. | |
6430 | */ | |
d9c9a0b9 | 6431 | for_each_online_cpu(cpu) |
7aac7898 | 6432 | set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu))); |
afb6ebb3 | 6433 | #endif |
d9c9a0b9 | 6434 | } |
b93e0f32 MH |
6435 | |
6436 | spin_unlock(&lock); | |
6811378e YG |
6437 | } |
6438 | ||
061f67bc RV |
6439 | static noinline void __init |
6440 | build_all_zonelists_init(void) | |
6441 | { | |
afb6ebb3 MH |
6442 | int cpu; |
6443 | ||
061f67bc | 6444 | __build_all_zonelists(NULL); |
afb6ebb3 MH |
6445 | |
6446 | /* | |
6447 | * Initialize the boot_pagesets that are going to be used | |
6448 | * for bootstrapping processors. The real pagesets for | |
6449 | * each zone will be allocated later when the per cpu | |
6450 | * allocator is available. | |
6451 | * | |
6452 | * boot_pagesets are used also for bootstrapping offline | |
6453 | * cpus if the system is already booted because the pagesets | |
6454 | * are needed to initialize allocators on a specific cpu too. | |
6455 | * F.e. the percpu allocator needs the page allocator which | |
6456 | * needs the percpu allocator in order to allocate its pagesets | |
6457 | * (a chicken-egg dilemma). | |
6458 | */ | |
6459 | for_each_possible_cpu(cpu) | |
28f836b6 | 6460 | per_cpu_pages_init(&per_cpu(boot_pageset, cpu), &per_cpu(boot_zonestats, cpu)); |
afb6ebb3 | 6461 | |
061f67bc RV |
6462 | mminit_verify_zonelist(); |
6463 | cpuset_init_current_mems_allowed(); | |
6464 | } | |
6465 | ||
4eaf3f64 | 6466 | /* |
4eaf3f64 | 6467 | * unless system_state == SYSTEM_BOOTING. |
061f67bc | 6468 | * |
72675e13 | 6469 | * __ref due to call of __init annotated helper build_all_zonelists_init |
061f67bc | 6470 | * [protected by SYSTEM_BOOTING]. |
4eaf3f64 | 6471 | */ |
72675e13 | 6472 | void __ref build_all_zonelists(pg_data_t *pgdat) |
6811378e | 6473 | { |
0a18e607 DH |
6474 | unsigned long vm_total_pages; |
6475 | ||
6811378e | 6476 | if (system_state == SYSTEM_BOOTING) { |
061f67bc | 6477 | build_all_zonelists_init(); |
6811378e | 6478 | } else { |
11cd8638 | 6479 | __build_all_zonelists(pgdat); |
6811378e YG |
6480 | /* cpuset refresh routine should be here */ |
6481 | } | |
56b9413b DH |
6482 | /* Get the number of free pages beyond high watermark in all zones. */ |
6483 | vm_total_pages = nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE)); | |
9ef9acb0 MG |
6484 | /* |
6485 | * Disable grouping by mobility if the number of pages in the | |
6486 | * system is too low to allow the mechanism to work. It would be | |
6487 | * more accurate, but expensive to check per-zone. This check is | |
6488 | * made on memory-hotadd so a system can start with mobility | |
6489 | * disabled and enable it later | |
6490 | */ | |
d9c23400 | 6491 | if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES)) |
9ef9acb0 MG |
6492 | page_group_by_mobility_disabled = 1; |
6493 | else | |
6494 | page_group_by_mobility_disabled = 0; | |
6495 | ||
ce0725f7 | 6496 | pr_info("Built %u zonelists, mobility grouping %s. Total pages: %ld\n", |
756a025f | 6497 | nr_online_nodes, |
756a025f JP |
6498 | page_group_by_mobility_disabled ? "off" : "on", |
6499 | vm_total_pages); | |
f0c0b2b8 | 6500 | #ifdef CONFIG_NUMA |
f88dfff5 | 6501 | pr_info("Policy zone: %s\n", zone_names[policy_zone]); |
f0c0b2b8 | 6502 | #endif |
1da177e4 LT |
6503 | } |
6504 | ||
a9a9e77f PT |
6505 | /* If zone is ZONE_MOVABLE but memory is mirrored, it is an overlapped init */ |
6506 | static bool __meminit | |
6507 | overlap_memmap_init(unsigned long zone, unsigned long *pfn) | |
6508 | { | |
a9a9e77f PT |
6509 | static struct memblock_region *r; |
6510 | ||
6511 | if (mirrored_kernelcore && zone == ZONE_MOVABLE) { | |
6512 | if (!r || *pfn >= memblock_region_memory_end_pfn(r)) { | |
cc6de168 | 6513 | for_each_mem_region(r) { |
a9a9e77f PT |
6514 | if (*pfn < memblock_region_memory_end_pfn(r)) |
6515 | break; | |
6516 | } | |
6517 | } | |
6518 | if (*pfn >= memblock_region_memory_base_pfn(r) && | |
6519 | memblock_is_mirror(r)) { | |
6520 | *pfn = memblock_region_memory_end_pfn(r); | |
6521 | return true; | |
6522 | } | |
6523 | } | |
a9a9e77f PT |
6524 | return false; |
6525 | } | |
6526 | ||
1da177e4 LT |
6527 | /* |
6528 | * Initially all pages are reserved - free ones are freed | |
c6ffc5ca | 6529 | * up by memblock_free_all() once the early boot process is |
1da177e4 | 6530 | * done. Non-atomic initialization, single-pass. |
d882c006 DH |
6531 | * |
6532 | * All aligned pageblocks are initialized to the specified migratetype | |
6533 | * (usually MIGRATE_MOVABLE). Besides setting the migratetype, no related | |
6534 | * zone stats (e.g., nr_isolate_pageblock) are touched. | |
1da177e4 | 6535 | */ |
ab28cb6e | 6536 | void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone, |
dc2da7b4 | 6537 | unsigned long start_pfn, unsigned long zone_end_pfn, |
d882c006 DH |
6538 | enum meminit_context context, |
6539 | struct vmem_altmap *altmap, int migratetype) | |
1da177e4 | 6540 | { |
a9a9e77f | 6541 | unsigned long pfn, end_pfn = start_pfn + size; |
d0dc12e8 | 6542 | struct page *page; |
1da177e4 | 6543 | |
22b31eec HD |
6544 | if (highest_memmap_pfn < end_pfn - 1) |
6545 | highest_memmap_pfn = end_pfn - 1; | |
6546 | ||
966cf44f | 6547 | #ifdef CONFIG_ZONE_DEVICE |
4b94ffdc DW |
6548 | /* |
6549 | * Honor reservation requested by the driver for this ZONE_DEVICE | |
966cf44f AD |
6550 | * memory. We limit the total number of pages to initialize to just |
6551 | * those that might contain the memory mapping. We will defer the | |
6552 | * ZONE_DEVICE page initialization until after we have released | |
6553 | * the hotplug lock. | |
4b94ffdc | 6554 | */ |
966cf44f AD |
6555 | if (zone == ZONE_DEVICE) { |
6556 | if (!altmap) | |
6557 | return; | |
6558 | ||
6559 | if (start_pfn == altmap->base_pfn) | |
6560 | start_pfn += altmap->reserve; | |
6561 | end_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); | |
6562 | } | |
6563 | #endif | |
4b94ffdc | 6564 | |
948c436e | 6565 | for (pfn = start_pfn; pfn < end_pfn; ) { |
a2f3aa02 | 6566 | /* |
b72d0ffb AM |
6567 | * There can be holes in boot-time mem_map[]s handed to this |
6568 | * function. They do not exist on hotplugged memory. | |
a2f3aa02 | 6569 | */ |
c1d0da83 | 6570 | if (context == MEMINIT_EARLY) { |
a9a9e77f PT |
6571 | if (overlap_memmap_init(zone, &pfn)) |
6572 | continue; | |
dc2da7b4 | 6573 | if (defer_init(nid, pfn, zone_end_pfn)) |
a9a9e77f | 6574 | break; |
a2f3aa02 | 6575 | } |
ac5d2539 | 6576 | |
d0dc12e8 PT |
6577 | page = pfn_to_page(pfn); |
6578 | __init_single_page(page, pfn, zone, nid); | |
c1d0da83 | 6579 | if (context == MEMINIT_HOTPLUG) |
d483da5b | 6580 | __SetPageReserved(page); |
d0dc12e8 | 6581 | |
ac5d2539 | 6582 | /* |
d882c006 DH |
6583 | * Usually, we want to mark the pageblock MIGRATE_MOVABLE, |
6584 | * such that unmovable allocations won't be scattered all | |
6585 | * over the place during system boot. | |
ac5d2539 | 6586 | */ |
4eb29bd9 | 6587 | if (IS_ALIGNED(pfn, pageblock_nr_pages)) { |
d882c006 | 6588 | set_pageblock_migratetype(page, migratetype); |
9b6e63cb | 6589 | cond_resched(); |
ac5d2539 | 6590 | } |
948c436e | 6591 | pfn++; |
1da177e4 LT |
6592 | } |
6593 | } | |
6594 | ||
966cf44f | 6595 | #ifdef CONFIG_ZONE_DEVICE |
46487e00 JM |
6596 | static void __ref __init_zone_device_page(struct page *page, unsigned long pfn, |
6597 | unsigned long zone_idx, int nid, | |
6598 | struct dev_pagemap *pgmap) | |
6599 | { | |
6600 | ||
6601 | __init_single_page(page, pfn, zone_idx, nid); | |
6602 | ||
6603 | /* | |
6604 | * Mark page reserved as it will need to wait for onlining | |
6605 | * phase for it to be fully associated with a zone. | |
6606 | * | |
6607 | * We can use the non-atomic __set_bit operation for setting | |
6608 | * the flag as we are still initializing the pages. | |
6609 | */ | |
6610 | __SetPageReserved(page); | |
6611 | ||
6612 | /* | |
6613 | * ZONE_DEVICE pages union ->lru with a ->pgmap back pointer | |
6614 | * and zone_device_data. It is a bug if a ZONE_DEVICE page is | |
6615 | * ever freed or placed on a driver-private list. | |
6616 | */ | |
6617 | page->pgmap = pgmap; | |
6618 | page->zone_device_data = NULL; | |
6619 | ||
6620 | /* | |
6621 | * Mark the block movable so that blocks are reserved for | |
6622 | * movable at startup. This will force kernel allocations | |
6623 | * to reserve their blocks rather than leaking throughout | |
6624 | * the address space during boot when many long-lived | |
6625 | * kernel allocations are made. | |
6626 | * | |
6627 | * Please note that MEMINIT_HOTPLUG path doesn't clear memmap | |
6628 | * because this is done early in section_activate() | |
6629 | */ | |
6630 | if (IS_ALIGNED(pfn, pageblock_nr_pages)) { | |
6631 | set_pageblock_migratetype(page, MIGRATE_MOVABLE); | |
6632 | cond_resched(); | |
6633 | } | |
6634 | } | |
6635 | ||
6fd3620b JM |
6636 | /* |
6637 | * With compound page geometry and when struct pages are stored in ram most | |
6638 | * tail pages are reused. Consequently, the amount of unique struct pages to | |
6639 | * initialize is a lot smaller that the total amount of struct pages being | |
6640 | * mapped. This is a paired / mild layering violation with explicit knowledge | |
6641 | * of how the sparse_vmemmap internals handle compound pages in the lack | |
6642 | * of an altmap. See vmemmap_populate_compound_pages(). | |
6643 | */ | |
6644 | static inline unsigned long compound_nr_pages(struct vmem_altmap *altmap, | |
6645 | unsigned long nr_pages) | |
6646 | { | |
6647 | return is_power_of_2(sizeof(struct page)) && | |
6648 | !altmap ? 2 * (PAGE_SIZE / sizeof(struct page)) : nr_pages; | |
6649 | } | |
6650 | ||
c4386bd8 JM |
6651 | static void __ref memmap_init_compound(struct page *head, |
6652 | unsigned long head_pfn, | |
6653 | unsigned long zone_idx, int nid, | |
6654 | struct dev_pagemap *pgmap, | |
6655 | unsigned long nr_pages) | |
6656 | { | |
6657 | unsigned long pfn, end_pfn = head_pfn + nr_pages; | |
6658 | unsigned int order = pgmap->vmemmap_shift; | |
6659 | ||
6660 | __SetPageHead(head); | |
6661 | for (pfn = head_pfn + 1; pfn < end_pfn; pfn++) { | |
6662 | struct page *page = pfn_to_page(pfn); | |
6663 | ||
6664 | __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); | |
6665 | prep_compound_tail(head, pfn - head_pfn); | |
6666 | set_page_count(page, 0); | |
6667 | ||
6668 | /* | |
6669 | * The first tail page stores compound_mapcount_ptr() and | |
6670 | * compound_order() and the second tail page stores | |
6671 | * compound_pincount_ptr(). Call prep_compound_head() after | |
6672 | * the first and second tail pages have been initialized to | |
6673 | * not have the data overwritten. | |
6674 | */ | |
6675 | if (pfn == head_pfn + 2) | |
6676 | prep_compound_head(head, order); | |
6677 | } | |
6678 | } | |
6679 | ||
966cf44f AD |
6680 | void __ref memmap_init_zone_device(struct zone *zone, |
6681 | unsigned long start_pfn, | |
1f8d75c1 | 6682 | unsigned long nr_pages, |
966cf44f AD |
6683 | struct dev_pagemap *pgmap) |
6684 | { | |
1f8d75c1 | 6685 | unsigned long pfn, end_pfn = start_pfn + nr_pages; |
966cf44f | 6686 | struct pglist_data *pgdat = zone->zone_pgdat; |
514caf23 | 6687 | struct vmem_altmap *altmap = pgmap_altmap(pgmap); |
c4386bd8 | 6688 | unsigned int pfns_per_compound = pgmap_vmemmap_nr(pgmap); |
966cf44f AD |
6689 | unsigned long zone_idx = zone_idx(zone); |
6690 | unsigned long start = jiffies; | |
6691 | int nid = pgdat->node_id; | |
6692 | ||
46d945ae | 6693 | if (WARN_ON_ONCE(!pgmap || zone_idx(zone) != ZONE_DEVICE)) |
966cf44f AD |
6694 | return; |
6695 | ||
6696 | /* | |
122e093c | 6697 | * The call to memmap_init should have already taken care |
966cf44f AD |
6698 | * of the pages reserved for the memmap, so we can just jump to |
6699 | * the end of that region and start processing the device pages. | |
6700 | */ | |
514caf23 | 6701 | if (altmap) { |
966cf44f | 6702 | start_pfn = altmap->base_pfn + vmem_altmap_offset(altmap); |
1f8d75c1 | 6703 | nr_pages = end_pfn - start_pfn; |
966cf44f AD |
6704 | } |
6705 | ||
c4386bd8 | 6706 | for (pfn = start_pfn; pfn < end_pfn; pfn += pfns_per_compound) { |
966cf44f AD |
6707 | struct page *page = pfn_to_page(pfn); |
6708 | ||
46487e00 | 6709 | __init_zone_device_page(page, pfn, zone_idx, nid, pgmap); |
c4386bd8 JM |
6710 | |
6711 | if (pfns_per_compound == 1) | |
6712 | continue; | |
6713 | ||
6714 | memmap_init_compound(page, pfn, zone_idx, nid, pgmap, | |
6fd3620b | 6715 | compound_nr_pages(altmap, pfns_per_compound)); |
966cf44f AD |
6716 | } |
6717 | ||
fdc029b1 | 6718 | pr_info("%s initialised %lu pages in %ums\n", __func__, |
1f8d75c1 | 6719 | nr_pages, jiffies_to_msecs(jiffies - start)); |
966cf44f AD |
6720 | } |
6721 | ||
6722 | #endif | |
1e548deb | 6723 | static void __meminit zone_init_free_lists(struct zone *zone) |
1da177e4 | 6724 | { |
7aeb09f9 | 6725 | unsigned int order, t; |
b2a0ac88 MG |
6726 | for_each_migratetype_order(order, t) { |
6727 | INIT_LIST_HEAD(&zone->free_area[order].free_list[t]); | |
1da177e4 LT |
6728 | zone->free_area[order].nr_free = 0; |
6729 | } | |
6730 | } | |
6731 | ||
0740a50b MR |
6732 | /* |
6733 | * Only struct pages that correspond to ranges defined by memblock.memory | |
6734 | * are zeroed and initialized by going through __init_single_page() during | |
122e093c | 6735 | * memmap_init_zone_range(). |
0740a50b MR |
6736 | * |
6737 | * But, there could be struct pages that correspond to holes in | |
6738 | * memblock.memory. This can happen because of the following reasons: | |
6739 | * - physical memory bank size is not necessarily the exact multiple of the | |
6740 | * arbitrary section size | |
6741 | * - early reserved memory may not be listed in memblock.memory | |
6742 | * - memory layouts defined with memmap= kernel parameter may not align | |
6743 | * nicely with memmap sections | |
6744 | * | |
6745 | * Explicitly initialize those struct pages so that: | |
6746 | * - PG_Reserved is set | |
6747 | * - zone and node links point to zone and node that span the page if the | |
6748 | * hole is in the middle of a zone | |
6749 | * - zone and node links point to adjacent zone/node if the hole falls on | |
6750 | * the zone boundary; the pages in such holes will be prepended to the | |
6751 | * zone/node above the hole except for the trailing pages in the last | |
6752 | * section that will be appended to the zone/node below. | |
6753 | */ | |
122e093c MR |
6754 | static void __init init_unavailable_range(unsigned long spfn, |
6755 | unsigned long epfn, | |
6756 | int zone, int node) | |
0740a50b MR |
6757 | { |
6758 | unsigned long pfn; | |
6759 | u64 pgcnt = 0; | |
6760 | ||
6761 | for (pfn = spfn; pfn < epfn; pfn++) { | |
6762 | if (!pfn_valid(ALIGN_DOWN(pfn, pageblock_nr_pages))) { | |
6763 | pfn = ALIGN_DOWN(pfn, pageblock_nr_pages) | |
6764 | + pageblock_nr_pages - 1; | |
6765 | continue; | |
6766 | } | |
6767 | __init_single_page(pfn_to_page(pfn), pfn, zone, node); | |
6768 | __SetPageReserved(pfn_to_page(pfn)); | |
6769 | pgcnt++; | |
6770 | } | |
6771 | ||
122e093c MR |
6772 | if (pgcnt) |
6773 | pr_info("On node %d, zone %s: %lld pages in unavailable ranges", | |
6774 | node, zone_names[zone], pgcnt); | |
0740a50b | 6775 | } |
0740a50b | 6776 | |
122e093c MR |
6777 | static void __init memmap_init_zone_range(struct zone *zone, |
6778 | unsigned long start_pfn, | |
6779 | unsigned long end_pfn, | |
6780 | unsigned long *hole_pfn) | |
dfb3ccd0 | 6781 | { |
3256ff83 BH |
6782 | unsigned long zone_start_pfn = zone->zone_start_pfn; |
6783 | unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages; | |
122e093c MR |
6784 | int nid = zone_to_nid(zone), zone_id = zone_idx(zone); |
6785 | ||
6786 | start_pfn = clamp(start_pfn, zone_start_pfn, zone_end_pfn); | |
6787 | end_pfn = clamp(end_pfn, zone_start_pfn, zone_end_pfn); | |
6788 | ||
6789 | if (start_pfn >= end_pfn) | |
6790 | return; | |
6791 | ||
6792 | memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, | |
6793 | zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE); | |
6794 | ||
6795 | if (*hole_pfn < start_pfn) | |
6796 | init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid); | |
6797 | ||
6798 | *hole_pfn = end_pfn; | |
6799 | } | |
6800 | ||
6801 | static void __init memmap_init(void) | |
6802 | { | |
73a6e474 | 6803 | unsigned long start_pfn, end_pfn; |
122e093c | 6804 | unsigned long hole_pfn = 0; |
b346075f | 6805 | int i, j, zone_id = 0, nid; |
73a6e474 | 6806 | |
122e093c MR |
6807 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { |
6808 | struct pglist_data *node = NODE_DATA(nid); | |
73a6e474 | 6809 | |
122e093c MR |
6810 | for (j = 0; j < MAX_NR_ZONES; j++) { |
6811 | struct zone *zone = node->node_zones + j; | |
0740a50b | 6812 | |
122e093c MR |
6813 | if (!populated_zone(zone)) |
6814 | continue; | |
0740a50b | 6815 | |
122e093c MR |
6816 | memmap_init_zone_range(zone, start_pfn, end_pfn, |
6817 | &hole_pfn); | |
6818 | zone_id = j; | |
6819 | } | |
73a6e474 | 6820 | } |
0740a50b MR |
6821 | |
6822 | #ifdef CONFIG_SPARSEMEM | |
6823 | /* | |
122e093c MR |
6824 | * Initialize the memory map for hole in the range [memory_end, |
6825 | * section_end]. | |
6826 | * Append the pages in this hole to the highest zone in the last | |
6827 | * node. | |
6828 | * The call to init_unavailable_range() is outside the ifdef to | |
6829 | * silence the compiler warining about zone_id set but not used; | |
6830 | * for FLATMEM it is a nop anyway | |
0740a50b | 6831 | */ |
122e093c | 6832 | end_pfn = round_up(end_pfn, PAGES_PER_SECTION); |
0740a50b | 6833 | if (hole_pfn < end_pfn) |
0740a50b | 6834 | #endif |
122e093c | 6835 | init_unavailable_range(hole_pfn, end_pfn, zone_id, nid); |
dfb3ccd0 | 6836 | } |
1da177e4 | 6837 | |
c803b3c8 MR |
6838 | void __init *memmap_alloc(phys_addr_t size, phys_addr_t align, |
6839 | phys_addr_t min_addr, int nid, bool exact_nid) | |
6840 | { | |
6841 | void *ptr; | |
6842 | ||
6843 | if (exact_nid) | |
6844 | ptr = memblock_alloc_exact_nid_raw(size, align, min_addr, | |
6845 | MEMBLOCK_ALLOC_ACCESSIBLE, | |
6846 | nid); | |
6847 | else | |
6848 | ptr = memblock_alloc_try_nid_raw(size, align, min_addr, | |
6849 | MEMBLOCK_ALLOC_ACCESSIBLE, | |
6850 | nid); | |
6851 | ||
6852 | if (ptr && size > 0) | |
6853 | page_init_poison(ptr, size); | |
6854 | ||
6855 | return ptr; | |
6856 | } | |
6857 | ||
7cd2b0a3 | 6858 | static int zone_batchsize(struct zone *zone) |
e7c8d5c9 | 6859 | { |
3a6be87f | 6860 | #ifdef CONFIG_MMU |
e7c8d5c9 CL |
6861 | int batch; |
6862 | ||
6863 | /* | |
b92ca18e MG |
6864 | * The number of pages to batch allocate is either ~0.1% |
6865 | * of the zone or 1MB, whichever is smaller. The batch | |
6866 | * size is striking a balance between allocation latency | |
6867 | * and zone lock contention. | |
e7c8d5c9 | 6868 | */ |
b92ca18e | 6869 | batch = min(zone_managed_pages(zone) >> 10, (1024 * 1024) / PAGE_SIZE); |
e7c8d5c9 CL |
6870 | batch /= 4; /* We effectively *= 4 below */ |
6871 | if (batch < 1) | |
6872 | batch = 1; | |
6873 | ||
6874 | /* | |
0ceaacc9 NP |
6875 | * Clamp the batch to a 2^n - 1 value. Having a power |
6876 | * of 2 value was found to be more likely to have | |
6877 | * suboptimal cache aliasing properties in some cases. | |
e7c8d5c9 | 6878 | * |
0ceaacc9 NP |
6879 | * For example if 2 tasks are alternately allocating |
6880 | * batches of pages, one task can end up with a lot | |
6881 | * of pages of one half of the possible page colors | |
6882 | * and the other with pages of the other colors. | |
e7c8d5c9 | 6883 | */ |
9155203a | 6884 | batch = rounddown_pow_of_two(batch + batch/2) - 1; |
ba56e91c | 6885 | |
e7c8d5c9 | 6886 | return batch; |
3a6be87f DH |
6887 | |
6888 | #else | |
6889 | /* The deferral and batching of frees should be suppressed under NOMMU | |
6890 | * conditions. | |
6891 | * | |
6892 | * The problem is that NOMMU needs to be able to allocate large chunks | |
6893 | * of contiguous memory as there's no hardware page translation to | |
6894 | * assemble apparent contiguous memory from discontiguous pages. | |
6895 | * | |
6896 | * Queueing large contiguous runs of pages for batching, however, | |
6897 | * causes the pages to actually be freed in smaller chunks. As there | |
6898 | * can be a significant delay between the individual batches being | |
6899 | * recycled, this leads to the once large chunks of space being | |
6900 | * fragmented and becoming unavailable for high-order allocations. | |
6901 | */ | |
6902 | return 0; | |
6903 | #endif | |
e7c8d5c9 CL |
6904 | } |
6905 | ||
04f8cfea | 6906 | static int zone_highsize(struct zone *zone, int batch, int cpu_online) |
b92ca18e MG |
6907 | { |
6908 | #ifdef CONFIG_MMU | |
6909 | int high; | |
203c06ee | 6910 | int nr_split_cpus; |
74f44822 MG |
6911 | unsigned long total_pages; |
6912 | ||
6913 | if (!percpu_pagelist_high_fraction) { | |
6914 | /* | |
6915 | * By default, the high value of the pcp is based on the zone | |
6916 | * low watermark so that if they are full then background | |
6917 | * reclaim will not be started prematurely. | |
6918 | */ | |
6919 | total_pages = low_wmark_pages(zone); | |
6920 | } else { | |
6921 | /* | |
6922 | * If percpu_pagelist_high_fraction is configured, the high | |
6923 | * value is based on a fraction of the managed pages in the | |
6924 | * zone. | |
6925 | */ | |
6926 | total_pages = zone_managed_pages(zone) / percpu_pagelist_high_fraction; | |
6927 | } | |
b92ca18e MG |
6928 | |
6929 | /* | |
74f44822 MG |
6930 | * Split the high value across all online CPUs local to the zone. Note |
6931 | * that early in boot that CPUs may not be online yet and that during | |
6932 | * CPU hotplug that the cpumask is not yet updated when a CPU is being | |
203c06ee MG |
6933 | * onlined. For memory nodes that have no CPUs, split pcp->high across |
6934 | * all online CPUs to mitigate the risk that reclaim is triggered | |
6935 | * prematurely due to pages stored on pcp lists. | |
b92ca18e | 6936 | */ |
203c06ee MG |
6937 | nr_split_cpus = cpumask_weight(cpumask_of_node(zone_to_nid(zone))) + cpu_online; |
6938 | if (!nr_split_cpus) | |
6939 | nr_split_cpus = num_online_cpus(); | |
6940 | high = total_pages / nr_split_cpus; | |
b92ca18e MG |
6941 | |
6942 | /* | |
6943 | * Ensure high is at least batch*4. The multiple is based on the | |
6944 | * historical relationship between high and batch. | |
6945 | */ | |
6946 | high = max(high, batch << 2); | |
6947 | ||
6948 | return high; | |
6949 | #else | |
6950 | return 0; | |
6951 | #endif | |
6952 | } | |
6953 | ||
8d7a8fa9 | 6954 | /* |
5c3ad2eb VB |
6955 | * pcp->high and pcp->batch values are related and generally batch is lower |
6956 | * than high. They are also related to pcp->count such that count is lower | |
6957 | * than high, and as soon as it reaches high, the pcplist is flushed. | |
8d7a8fa9 | 6958 | * |
5c3ad2eb VB |
6959 | * However, guaranteeing these relations at all times would require e.g. write |
6960 | * barriers here but also careful usage of read barriers at the read side, and | |
6961 | * thus be prone to error and bad for performance. Thus the update only prevents | |
6962 | * store tearing. Any new users of pcp->batch and pcp->high should ensure they | |
6963 | * can cope with those fields changing asynchronously, and fully trust only the | |
6964 | * pcp->count field on the local CPU with interrupts disabled. | |
8d7a8fa9 CS |
6965 | * |
6966 | * mutex_is_locked(&pcp_batch_high_lock) required when calling this function | |
6967 | * outside of boot time (or some other assurance that no concurrent updaters | |
6968 | * exist). | |
6969 | */ | |
6970 | static void pageset_update(struct per_cpu_pages *pcp, unsigned long high, | |
6971 | unsigned long batch) | |
6972 | { | |
5c3ad2eb VB |
6973 | WRITE_ONCE(pcp->batch, batch); |
6974 | WRITE_ONCE(pcp->high, high); | |
8d7a8fa9 CS |
6975 | } |
6976 | ||
28f836b6 | 6977 | static void per_cpu_pages_init(struct per_cpu_pages *pcp, struct per_cpu_zonestat *pzstats) |
2caaad41 | 6978 | { |
44042b44 | 6979 | int pindex; |
2caaad41 | 6980 | |
28f836b6 MG |
6981 | memset(pcp, 0, sizeof(*pcp)); |
6982 | memset(pzstats, 0, sizeof(*pzstats)); | |
1c6fe946 | 6983 | |
44042b44 MG |
6984 | for (pindex = 0; pindex < NR_PCP_LISTS; pindex++) |
6985 | INIT_LIST_HEAD(&pcp->lists[pindex]); | |
2caaad41 | 6986 | |
69a8396a VB |
6987 | /* |
6988 | * Set batch and high values safe for a boot pageset. A true percpu | |
6989 | * pageset's initialization will update them subsequently. Here we don't | |
6990 | * need to be as careful as pageset_update() as nobody can access the | |
6991 | * pageset yet. | |
6992 | */ | |
952eaf81 VB |
6993 | pcp->high = BOOT_PAGESET_HIGH; |
6994 | pcp->batch = BOOT_PAGESET_BATCH; | |
3b12e7e9 | 6995 | pcp->free_factor = 0; |
88c90dbc CS |
6996 | } |
6997 | ||
3b1f3658 | 6998 | static void __zone_set_pageset_high_and_batch(struct zone *zone, unsigned long high, |
ec6e8c7e VB |
6999 | unsigned long batch) |
7000 | { | |
28f836b6 | 7001 | struct per_cpu_pages *pcp; |
ec6e8c7e VB |
7002 | int cpu; |
7003 | ||
7004 | for_each_possible_cpu(cpu) { | |
28f836b6 MG |
7005 | pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); |
7006 | pageset_update(pcp, high, batch); | |
ec6e8c7e VB |
7007 | } |
7008 | } | |
7009 | ||
8ad4b1fb | 7010 | /* |
0a8b4f1d | 7011 | * Calculate and set new high and batch values for all per-cpu pagesets of a |
bbbecb35 | 7012 | * zone based on the zone's size. |
8ad4b1fb | 7013 | */ |
04f8cfea | 7014 | static void zone_set_pageset_high_and_batch(struct zone *zone, int cpu_online) |
56cef2b8 | 7015 | { |
b92ca18e | 7016 | int new_high, new_batch; |
7115ac6e | 7017 | |
b92ca18e | 7018 | new_batch = max(1, zone_batchsize(zone)); |
04f8cfea | 7019 | new_high = zone_highsize(zone, new_batch, cpu_online); |
169f6c19 | 7020 | |
952eaf81 VB |
7021 | if (zone->pageset_high == new_high && |
7022 | zone->pageset_batch == new_batch) | |
7023 | return; | |
7024 | ||
7025 | zone->pageset_high = new_high; | |
7026 | zone->pageset_batch = new_batch; | |
7027 | ||
ec6e8c7e | 7028 | __zone_set_pageset_high_and_batch(zone, new_high, new_batch); |
169f6c19 CS |
7029 | } |
7030 | ||
72675e13 | 7031 | void __meminit setup_zone_pageset(struct zone *zone) |
319774e2 WF |
7032 | { |
7033 | int cpu; | |
0a8b4f1d | 7034 | |
28f836b6 MG |
7035 | /* Size may be 0 on !SMP && !NUMA */ |
7036 | if (sizeof(struct per_cpu_zonestat) > 0) | |
7037 | zone->per_cpu_zonestats = alloc_percpu(struct per_cpu_zonestat); | |
7038 | ||
7039 | zone->per_cpu_pageset = alloc_percpu(struct per_cpu_pages); | |
0a8b4f1d | 7040 | for_each_possible_cpu(cpu) { |
28f836b6 MG |
7041 | struct per_cpu_pages *pcp; |
7042 | struct per_cpu_zonestat *pzstats; | |
7043 | ||
7044 | pcp = per_cpu_ptr(zone->per_cpu_pageset, cpu); | |
7045 | pzstats = per_cpu_ptr(zone->per_cpu_zonestats, cpu); | |
7046 | per_cpu_pages_init(pcp, pzstats); | |
0a8b4f1d VB |
7047 | } |
7048 | ||
04f8cfea | 7049 | zone_set_pageset_high_and_batch(zone, 0); |
319774e2 WF |
7050 | } |
7051 | ||
2caaad41 | 7052 | /* |
99dcc3e5 CL |
7053 | * Allocate per cpu pagesets and initialize them. |
7054 | * Before this call only boot pagesets were available. | |
e7c8d5c9 | 7055 | */ |
99dcc3e5 | 7056 | void __init setup_per_cpu_pageset(void) |
e7c8d5c9 | 7057 | { |
b4911ea2 | 7058 | struct pglist_data *pgdat; |
99dcc3e5 | 7059 | struct zone *zone; |
b418a0f9 | 7060 | int __maybe_unused cpu; |
e7c8d5c9 | 7061 | |
319774e2 WF |
7062 | for_each_populated_zone(zone) |
7063 | setup_zone_pageset(zone); | |
b4911ea2 | 7064 | |
b418a0f9 SD |
7065 | #ifdef CONFIG_NUMA |
7066 | /* | |
7067 | * Unpopulated zones continue using the boot pagesets. | |
7068 | * The numa stats for these pagesets need to be reset. | |
7069 | * Otherwise, they will end up skewing the stats of | |
7070 | * the nodes these zones are associated with. | |
7071 | */ | |
7072 | for_each_possible_cpu(cpu) { | |
28f836b6 | 7073 | struct per_cpu_zonestat *pzstats = &per_cpu(boot_zonestats, cpu); |
f19298b9 MG |
7074 | memset(pzstats->vm_numa_event, 0, |
7075 | sizeof(pzstats->vm_numa_event)); | |
b418a0f9 SD |
7076 | } |
7077 | #endif | |
7078 | ||
b4911ea2 MG |
7079 | for_each_online_pgdat(pgdat) |
7080 | pgdat->per_cpu_nodestats = | |
7081 | alloc_percpu(struct per_cpu_nodestat); | |
e7c8d5c9 CL |
7082 | } |
7083 | ||
c09b4240 | 7084 | static __meminit void zone_pcp_init(struct zone *zone) |
ed8ece2e | 7085 | { |
99dcc3e5 CL |
7086 | /* |
7087 | * per cpu subsystem is not up at this point. The following code | |
7088 | * relies on the ability of the linker to provide the | |
7089 | * offset of a (static) per cpu variable into the per cpu area. | |
7090 | */ | |
28f836b6 MG |
7091 | zone->per_cpu_pageset = &boot_pageset; |
7092 | zone->per_cpu_zonestats = &boot_zonestats; | |
952eaf81 VB |
7093 | zone->pageset_high = BOOT_PAGESET_HIGH; |
7094 | zone->pageset_batch = BOOT_PAGESET_BATCH; | |
ed8ece2e | 7095 | |
b38a8725 | 7096 | if (populated_zone(zone)) |
9660ecaa HK |
7097 | pr_debug(" %s zone: %lu pages, LIFO batch:%u\n", zone->name, |
7098 | zone->present_pages, zone_batchsize(zone)); | |
ed8ece2e DH |
7099 | } |
7100 | ||
dc0bbf3b | 7101 | void __meminit init_currently_empty_zone(struct zone *zone, |
718127cc | 7102 | unsigned long zone_start_pfn, |
b171e409 | 7103 | unsigned long size) |
ed8ece2e DH |
7104 | { |
7105 | struct pglist_data *pgdat = zone->zone_pgdat; | |
8f416836 | 7106 | int zone_idx = zone_idx(zone) + 1; |
9dcb8b68 | 7107 | |
8f416836 WY |
7108 | if (zone_idx > pgdat->nr_zones) |
7109 | pgdat->nr_zones = zone_idx; | |
ed8ece2e | 7110 | |
ed8ece2e DH |
7111 | zone->zone_start_pfn = zone_start_pfn; |
7112 | ||
708614e6 MG |
7113 | mminit_dprintk(MMINIT_TRACE, "memmap_init", |
7114 | "Initialising map node %d zone %lu pfns %lu -> %lu\n", | |
7115 | pgdat->node_id, | |
7116 | (unsigned long)zone_idx(zone), | |
7117 | zone_start_pfn, (zone_start_pfn + size)); | |
7118 | ||
1e548deb | 7119 | zone_init_free_lists(zone); |
9dcb8b68 | 7120 | zone->initialized = 1; |
ed8ece2e DH |
7121 | } |
7122 | ||
c713216d MG |
7123 | /** |
7124 | * get_pfn_range_for_nid - Return the start and end page frames for a node | |
88ca3b94 RD |
7125 | * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned. |
7126 | * @start_pfn: Passed by reference. On return, it will have the node start_pfn. | |
7127 | * @end_pfn: Passed by reference. On return, it will have the node end_pfn. | |
c713216d MG |
7128 | * |
7129 | * It returns the start and end page frame of a node based on information | |
7d018176 | 7130 | * provided by memblock_set_node(). If called for a node |
c713216d | 7131 | * with no available memory, a warning is printed and the start and end |
88ca3b94 | 7132 | * PFNs will be 0. |
c713216d | 7133 | */ |
bbe5d993 | 7134 | void __init get_pfn_range_for_nid(unsigned int nid, |
c713216d MG |
7135 | unsigned long *start_pfn, unsigned long *end_pfn) |
7136 | { | |
c13291a5 | 7137 | unsigned long this_start_pfn, this_end_pfn; |
c713216d | 7138 | int i; |
c13291a5 | 7139 | |
c713216d MG |
7140 | *start_pfn = -1UL; |
7141 | *end_pfn = 0; | |
7142 | ||
c13291a5 TH |
7143 | for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) { |
7144 | *start_pfn = min(*start_pfn, this_start_pfn); | |
7145 | *end_pfn = max(*end_pfn, this_end_pfn); | |
c713216d MG |
7146 | } |
7147 | ||
633c0666 | 7148 | if (*start_pfn == -1UL) |
c713216d | 7149 | *start_pfn = 0; |
c713216d MG |
7150 | } |
7151 | ||
2a1e274a MG |
7152 | /* |
7153 | * This finds a zone that can be used for ZONE_MOVABLE pages. The | |
7154 | * assumption is made that zones within a node are ordered in monotonic | |
7155 | * increasing memory addresses so that the "highest" populated zone is used | |
7156 | */ | |
b69a7288 | 7157 | static void __init find_usable_zone_for_movable(void) |
2a1e274a MG |
7158 | { |
7159 | int zone_index; | |
7160 | for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) { | |
7161 | if (zone_index == ZONE_MOVABLE) | |
7162 | continue; | |
7163 | ||
7164 | if (arch_zone_highest_possible_pfn[zone_index] > | |
7165 | arch_zone_lowest_possible_pfn[zone_index]) | |
7166 | break; | |
7167 | } | |
7168 | ||
7169 | VM_BUG_ON(zone_index == -1); | |
7170 | movable_zone = zone_index; | |
7171 | } | |
7172 | ||
7173 | /* | |
7174 | * The zone ranges provided by the architecture do not include ZONE_MOVABLE | |
25985edc | 7175 | * because it is sized independent of architecture. Unlike the other zones, |
2a1e274a MG |
7176 | * the starting point for ZONE_MOVABLE is not fixed. It may be different |
7177 | * in each node depending on the size of each node and how evenly kernelcore | |
7178 | * is distributed. This helper function adjusts the zone ranges | |
7179 | * provided by the architecture for a given node by using the end of the | |
7180 | * highest usable zone for ZONE_MOVABLE. This preserves the assumption that | |
7181 | * zones within a node are in order of monotonic increases memory addresses | |
7182 | */ | |
bbe5d993 | 7183 | static void __init adjust_zone_range_for_zone_movable(int nid, |
2a1e274a MG |
7184 | unsigned long zone_type, |
7185 | unsigned long node_start_pfn, | |
7186 | unsigned long node_end_pfn, | |
7187 | unsigned long *zone_start_pfn, | |
7188 | unsigned long *zone_end_pfn) | |
7189 | { | |
7190 | /* Only adjust if ZONE_MOVABLE is on this node */ | |
7191 | if (zone_movable_pfn[nid]) { | |
7192 | /* Size ZONE_MOVABLE */ | |
7193 | if (zone_type == ZONE_MOVABLE) { | |
7194 | *zone_start_pfn = zone_movable_pfn[nid]; | |
7195 | *zone_end_pfn = min(node_end_pfn, | |
7196 | arch_zone_highest_possible_pfn[movable_zone]); | |
7197 | ||
e506b996 XQ |
7198 | /* Adjust for ZONE_MOVABLE starting within this range */ |
7199 | } else if (!mirrored_kernelcore && | |
7200 | *zone_start_pfn < zone_movable_pfn[nid] && | |
7201 | *zone_end_pfn > zone_movable_pfn[nid]) { | |
7202 | *zone_end_pfn = zone_movable_pfn[nid]; | |
7203 | ||
2a1e274a MG |
7204 | /* Check if this whole range is within ZONE_MOVABLE */ |
7205 | } else if (*zone_start_pfn >= zone_movable_pfn[nid]) | |
7206 | *zone_start_pfn = *zone_end_pfn; | |
7207 | } | |
7208 | } | |
7209 | ||
c713216d MG |
7210 | /* |
7211 | * Return the number of pages a zone spans in a node, including holes | |
7212 | * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node() | |
7213 | */ | |
bbe5d993 | 7214 | static unsigned long __init zone_spanned_pages_in_node(int nid, |
c713216d | 7215 | unsigned long zone_type, |
7960aedd ZY |
7216 | unsigned long node_start_pfn, |
7217 | unsigned long node_end_pfn, | |
d91749c1 | 7218 | unsigned long *zone_start_pfn, |
854e8848 | 7219 | unsigned long *zone_end_pfn) |
c713216d | 7220 | { |
299c83dc LF |
7221 | unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type]; |
7222 | unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type]; | |
b5685e92 | 7223 | /* When hotadd a new node from cpu_up(), the node should be empty */ |
f9126ab9 XQ |
7224 | if (!node_start_pfn && !node_end_pfn) |
7225 | return 0; | |
7226 | ||
7960aedd | 7227 | /* Get the start and end of the zone */ |
299c83dc LF |
7228 | *zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high); |
7229 | *zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high); | |
2a1e274a MG |
7230 | adjust_zone_range_for_zone_movable(nid, zone_type, |
7231 | node_start_pfn, node_end_pfn, | |
d91749c1 | 7232 | zone_start_pfn, zone_end_pfn); |
c713216d MG |
7233 | |
7234 | /* Check that this node has pages within the zone's required range */ | |
d91749c1 | 7235 | if (*zone_end_pfn < node_start_pfn || *zone_start_pfn > node_end_pfn) |
c713216d MG |
7236 | return 0; |
7237 | ||
7238 | /* Move the zone boundaries inside the node if necessary */ | |
d91749c1 TI |
7239 | *zone_end_pfn = min(*zone_end_pfn, node_end_pfn); |
7240 | *zone_start_pfn = max(*zone_start_pfn, node_start_pfn); | |
c713216d MG |
7241 | |
7242 | /* Return the spanned pages */ | |
d91749c1 | 7243 | return *zone_end_pfn - *zone_start_pfn; |
c713216d MG |
7244 | } |
7245 | ||
7246 | /* | |
7247 | * Return the number of holes in a range on a node. If nid is MAX_NUMNODES, | |
88ca3b94 | 7248 | * then all holes in the requested range will be accounted for. |
c713216d | 7249 | */ |
bbe5d993 | 7250 | unsigned long __init __absent_pages_in_range(int nid, |
c713216d MG |
7251 | unsigned long range_start_pfn, |
7252 | unsigned long range_end_pfn) | |
7253 | { | |
96e907d1 TH |
7254 | unsigned long nr_absent = range_end_pfn - range_start_pfn; |
7255 | unsigned long start_pfn, end_pfn; | |
7256 | int i; | |
c713216d | 7257 | |
96e907d1 TH |
7258 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
7259 | start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn); | |
7260 | end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn); | |
7261 | nr_absent -= end_pfn - start_pfn; | |
c713216d | 7262 | } |
96e907d1 | 7263 | return nr_absent; |
c713216d MG |
7264 | } |
7265 | ||
7266 | /** | |
7267 | * absent_pages_in_range - Return number of page frames in holes within a range | |
7268 | * @start_pfn: The start PFN to start searching for holes | |
7269 | * @end_pfn: The end PFN to stop searching for holes | |
7270 | * | |
a862f68a | 7271 | * Return: the number of pages frames in memory holes within a range. |
c713216d MG |
7272 | */ |
7273 | unsigned long __init absent_pages_in_range(unsigned long start_pfn, | |
7274 | unsigned long end_pfn) | |
7275 | { | |
7276 | return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn); | |
7277 | } | |
7278 | ||
7279 | /* Return the number of page frames in holes in a zone on a node */ | |
bbe5d993 | 7280 | static unsigned long __init zone_absent_pages_in_node(int nid, |
c713216d | 7281 | unsigned long zone_type, |
7960aedd | 7282 | unsigned long node_start_pfn, |
854e8848 | 7283 | unsigned long node_end_pfn) |
c713216d | 7284 | { |
96e907d1 TH |
7285 | unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type]; |
7286 | unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type]; | |
9c7cd687 | 7287 | unsigned long zone_start_pfn, zone_end_pfn; |
342332e6 | 7288 | unsigned long nr_absent; |
9c7cd687 | 7289 | |
b5685e92 | 7290 | /* When hotadd a new node from cpu_up(), the node should be empty */ |
f9126ab9 XQ |
7291 | if (!node_start_pfn && !node_end_pfn) |
7292 | return 0; | |
7293 | ||
96e907d1 TH |
7294 | zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high); |
7295 | zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high); | |
9c7cd687 | 7296 | |
2a1e274a MG |
7297 | adjust_zone_range_for_zone_movable(nid, zone_type, |
7298 | node_start_pfn, node_end_pfn, | |
7299 | &zone_start_pfn, &zone_end_pfn); | |
342332e6 TI |
7300 | nr_absent = __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn); |
7301 | ||
7302 | /* | |
7303 | * ZONE_MOVABLE handling. | |
7304 | * Treat pages to be ZONE_MOVABLE in ZONE_NORMAL as absent pages | |
7305 | * and vice versa. | |
7306 | */ | |
e506b996 XQ |
7307 | if (mirrored_kernelcore && zone_movable_pfn[nid]) { |
7308 | unsigned long start_pfn, end_pfn; | |
7309 | struct memblock_region *r; | |
7310 | ||
cc6de168 | 7311 | for_each_mem_region(r) { |
e506b996 XQ |
7312 | start_pfn = clamp(memblock_region_memory_base_pfn(r), |
7313 | zone_start_pfn, zone_end_pfn); | |
7314 | end_pfn = clamp(memblock_region_memory_end_pfn(r), | |
7315 | zone_start_pfn, zone_end_pfn); | |
7316 | ||
7317 | if (zone_type == ZONE_MOVABLE && | |
7318 | memblock_is_mirror(r)) | |
7319 | nr_absent += end_pfn - start_pfn; | |
7320 | ||
7321 | if (zone_type == ZONE_NORMAL && | |
7322 | !memblock_is_mirror(r)) | |
7323 | nr_absent += end_pfn - start_pfn; | |
342332e6 TI |
7324 | } |
7325 | } | |
7326 | ||
7327 | return nr_absent; | |
c713216d | 7328 | } |
0e0b864e | 7329 | |
bbe5d993 | 7330 | static void __init calculate_node_totalpages(struct pglist_data *pgdat, |
7960aedd | 7331 | unsigned long node_start_pfn, |
854e8848 | 7332 | unsigned long node_end_pfn) |
c713216d | 7333 | { |
febd5949 | 7334 | unsigned long realtotalpages = 0, totalpages = 0; |
c713216d MG |
7335 | enum zone_type i; |
7336 | ||
febd5949 GZ |
7337 | for (i = 0; i < MAX_NR_ZONES; i++) { |
7338 | struct zone *zone = pgdat->node_zones + i; | |
d91749c1 | 7339 | unsigned long zone_start_pfn, zone_end_pfn; |
3f08a302 | 7340 | unsigned long spanned, absent; |
febd5949 | 7341 | unsigned long size, real_size; |
c713216d | 7342 | |
854e8848 MR |
7343 | spanned = zone_spanned_pages_in_node(pgdat->node_id, i, |
7344 | node_start_pfn, | |
7345 | node_end_pfn, | |
7346 | &zone_start_pfn, | |
7347 | &zone_end_pfn); | |
7348 | absent = zone_absent_pages_in_node(pgdat->node_id, i, | |
7349 | node_start_pfn, | |
7350 | node_end_pfn); | |
3f08a302 MR |
7351 | |
7352 | size = spanned; | |
7353 | real_size = size - absent; | |
7354 | ||
d91749c1 TI |
7355 | if (size) |
7356 | zone->zone_start_pfn = zone_start_pfn; | |
7357 | else | |
7358 | zone->zone_start_pfn = 0; | |
febd5949 GZ |
7359 | zone->spanned_pages = size; |
7360 | zone->present_pages = real_size; | |
4b097002 DH |
7361 | #if defined(CONFIG_MEMORY_HOTPLUG) |
7362 | zone->present_early_pages = real_size; | |
7363 | #endif | |
febd5949 GZ |
7364 | |
7365 | totalpages += size; | |
7366 | realtotalpages += real_size; | |
7367 | } | |
7368 | ||
7369 | pgdat->node_spanned_pages = totalpages; | |
c713216d | 7370 | pgdat->node_present_pages = realtotalpages; |
9660ecaa | 7371 | pr_debug("On node %d totalpages: %lu\n", pgdat->node_id, realtotalpages); |
c713216d MG |
7372 | } |
7373 | ||
835c134e MG |
7374 | #ifndef CONFIG_SPARSEMEM |
7375 | /* | |
7376 | * Calculate the size of the zone->blockflags rounded to an unsigned long | |
d9c23400 MG |
7377 | * Start by making sure zonesize is a multiple of pageblock_order by rounding |
7378 | * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally | |
835c134e MG |
7379 | * round what is now in bits to nearest long in bits, then return it in |
7380 | * bytes. | |
7381 | */ | |
7c45512d | 7382 | static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize) |
835c134e MG |
7383 | { |
7384 | unsigned long usemapsize; | |
7385 | ||
7c45512d | 7386 | zonesize += zone_start_pfn & (pageblock_nr_pages-1); |
d9c23400 MG |
7387 | usemapsize = roundup(zonesize, pageblock_nr_pages); |
7388 | usemapsize = usemapsize >> pageblock_order; | |
835c134e MG |
7389 | usemapsize *= NR_PAGEBLOCK_BITS; |
7390 | usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long)); | |
7391 | ||
7392 | return usemapsize / 8; | |
7393 | } | |
7394 | ||
7010a6ec | 7395 | static void __ref setup_usemap(struct zone *zone) |
835c134e | 7396 | { |
7010a6ec BH |
7397 | unsigned long usemapsize = usemap_size(zone->zone_start_pfn, |
7398 | zone->spanned_pages); | |
835c134e | 7399 | zone->pageblock_flags = NULL; |
23a7052a | 7400 | if (usemapsize) { |
6782832e | 7401 | zone->pageblock_flags = |
26fb3dae | 7402 | memblock_alloc_node(usemapsize, SMP_CACHE_BYTES, |
7010a6ec | 7403 | zone_to_nid(zone)); |
23a7052a MR |
7404 | if (!zone->pageblock_flags) |
7405 | panic("Failed to allocate %ld bytes for zone %s pageblock flags on node %d\n", | |
7010a6ec | 7406 | usemapsize, zone->name, zone_to_nid(zone)); |
23a7052a | 7407 | } |
835c134e MG |
7408 | } |
7409 | #else | |
7010a6ec | 7410 | static inline void setup_usemap(struct zone *zone) {} |
835c134e MG |
7411 | #endif /* CONFIG_SPARSEMEM */ |
7412 | ||
d9c23400 | 7413 | #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
ba72cb8c | 7414 | |
d9c23400 | 7415 | /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */ |
03e85f9d | 7416 | void __init set_pageblock_order(void) |
d9c23400 | 7417 | { |
b3d40a2b | 7418 | unsigned int order = MAX_ORDER - 1; |
955c1cd7 | 7419 | |
d9c23400 MG |
7420 | /* Check that pageblock_nr_pages has not already been setup */ |
7421 | if (pageblock_order) | |
7422 | return; | |
7423 | ||
b3d40a2b DH |
7424 | /* Don't let pageblocks exceed the maximum allocation granularity. */ |
7425 | if (HPAGE_SHIFT > PAGE_SHIFT && HUGETLB_PAGE_ORDER < order) | |
955c1cd7 | 7426 | order = HUGETLB_PAGE_ORDER; |
955c1cd7 | 7427 | |
d9c23400 MG |
7428 | /* |
7429 | * Assume the largest contiguous order of interest is a huge page. | |
955c1cd7 AM |
7430 | * This value may be variable depending on boot parameters on IA64 and |
7431 | * powerpc. | |
d9c23400 MG |
7432 | */ |
7433 | pageblock_order = order; | |
7434 | } | |
7435 | #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ | |
7436 | ||
ba72cb8c MG |
7437 | /* |
7438 | * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order() | |
955c1cd7 AM |
7439 | * is unused as pageblock_order is set at compile-time. See |
7440 | * include/linux/pageblock-flags.h for the values of pageblock_order based on | |
7441 | * the kernel config | |
ba72cb8c | 7442 | */ |
03e85f9d | 7443 | void __init set_pageblock_order(void) |
ba72cb8c | 7444 | { |
ba72cb8c | 7445 | } |
d9c23400 MG |
7446 | |
7447 | #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */ | |
7448 | ||
03e85f9d | 7449 | static unsigned long __init calc_memmap_size(unsigned long spanned_pages, |
7cc2a959 | 7450 | unsigned long present_pages) |
01cefaef JL |
7451 | { |
7452 | unsigned long pages = spanned_pages; | |
7453 | ||
7454 | /* | |
7455 | * Provide a more accurate estimation if there are holes within | |
7456 | * the zone and SPARSEMEM is in use. If there are holes within the | |
7457 | * zone, each populated memory region may cost us one or two extra | |
7458 | * memmap pages due to alignment because memmap pages for each | |
89d790ab | 7459 | * populated regions may not be naturally aligned on page boundary. |
01cefaef JL |
7460 | * So the (present_pages >> 4) heuristic is a tradeoff for that. |
7461 | */ | |
7462 | if (spanned_pages > present_pages + (present_pages >> 4) && | |
7463 | IS_ENABLED(CONFIG_SPARSEMEM)) | |
7464 | pages = present_pages; | |
7465 | ||
7466 | return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT; | |
7467 | } | |
7468 | ||
ace1db39 OS |
7469 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
7470 | static void pgdat_init_split_queue(struct pglist_data *pgdat) | |
7471 | { | |
364c1eeb YS |
7472 | struct deferred_split *ds_queue = &pgdat->deferred_split_queue; |
7473 | ||
7474 | spin_lock_init(&ds_queue->split_queue_lock); | |
7475 | INIT_LIST_HEAD(&ds_queue->split_queue); | |
7476 | ds_queue->split_queue_len = 0; | |
ace1db39 OS |
7477 | } |
7478 | #else | |
7479 | static void pgdat_init_split_queue(struct pglist_data *pgdat) {} | |
7480 | #endif | |
7481 | ||
7482 | #ifdef CONFIG_COMPACTION | |
7483 | static void pgdat_init_kcompactd(struct pglist_data *pgdat) | |
7484 | { | |
7485 | init_waitqueue_head(&pgdat->kcompactd_wait); | |
7486 | } | |
7487 | #else | |
7488 | static void pgdat_init_kcompactd(struct pglist_data *pgdat) {} | |
7489 | #endif | |
7490 | ||
03e85f9d | 7491 | static void __meminit pgdat_init_internals(struct pglist_data *pgdat) |
1da177e4 | 7492 | { |
8cd7c588 MG |
7493 | int i; |
7494 | ||
208d54e5 | 7495 | pgdat_resize_init(pgdat); |
ace1db39 | 7496 | |
ace1db39 OS |
7497 | pgdat_init_split_queue(pgdat); |
7498 | pgdat_init_kcompactd(pgdat); | |
7499 | ||
1da177e4 | 7500 | init_waitqueue_head(&pgdat->kswapd_wait); |
5515061d | 7501 | init_waitqueue_head(&pgdat->pfmemalloc_wait); |
ace1db39 | 7502 | |
8cd7c588 MG |
7503 | for (i = 0; i < NR_VMSCAN_THROTTLE; i++) |
7504 | init_waitqueue_head(&pgdat->reclaim_wait[i]); | |
7505 | ||
eefa864b | 7506 | pgdat_page_ext_init(pgdat); |
867e5e1d | 7507 | lruvec_init(&pgdat->__lruvec); |
03e85f9d OS |
7508 | } |
7509 | ||
7510 | static void __meminit zone_init_internals(struct zone *zone, enum zone_type idx, int nid, | |
7511 | unsigned long remaining_pages) | |
7512 | { | |
9705bea5 | 7513 | atomic_long_set(&zone->managed_pages, remaining_pages); |
03e85f9d OS |
7514 | zone_set_nid(zone, nid); |
7515 | zone->name = zone_names[idx]; | |
7516 | zone->zone_pgdat = NODE_DATA(nid); | |
7517 | spin_lock_init(&zone->lock); | |
7518 | zone_seqlock_init(zone); | |
7519 | zone_pcp_init(zone); | |
7520 | } | |
7521 | ||
7522 | /* | |
7523 | * Set up the zone data structures | |
7524 | * - init pgdat internals | |
7525 | * - init all zones belonging to this node | |
7526 | * | |
7527 | * NOTE: this function is only called during memory hotplug | |
7528 | */ | |
7529 | #ifdef CONFIG_MEMORY_HOTPLUG | |
70b5b46a | 7530 | void __ref free_area_init_core_hotplug(struct pglist_data *pgdat) |
03e85f9d | 7531 | { |
70b5b46a | 7532 | int nid = pgdat->node_id; |
03e85f9d | 7533 | enum zone_type z; |
70b5b46a | 7534 | int cpu; |
03e85f9d OS |
7535 | |
7536 | pgdat_init_internals(pgdat); | |
70b5b46a MH |
7537 | |
7538 | if (pgdat->per_cpu_nodestats == &boot_nodestats) | |
7539 | pgdat->per_cpu_nodestats = alloc_percpu(struct per_cpu_nodestat); | |
7540 | ||
7541 | /* | |
7542 | * Reset the nr_zones, order and highest_zoneidx before reuse. | |
7543 | * Note that kswapd will init kswapd_highest_zoneidx properly | |
7544 | * when it starts in the near future. | |
7545 | */ | |
7546 | pgdat->nr_zones = 0; | |
7547 | pgdat->kswapd_order = 0; | |
7548 | pgdat->kswapd_highest_zoneidx = 0; | |
7549 | pgdat->node_start_pfn = 0; | |
7550 | for_each_online_cpu(cpu) { | |
7551 | struct per_cpu_nodestat *p; | |
7552 | ||
7553 | p = per_cpu_ptr(pgdat->per_cpu_nodestats, cpu); | |
7554 | memset(p, 0, sizeof(*p)); | |
7555 | } | |
7556 | ||
03e85f9d OS |
7557 | for (z = 0; z < MAX_NR_ZONES; z++) |
7558 | zone_init_internals(&pgdat->node_zones[z], z, nid, 0); | |
7559 | } | |
7560 | #endif | |
7561 | ||
7562 | /* | |
7563 | * Set up the zone data structures: | |
7564 | * - mark all pages reserved | |
7565 | * - mark all memory queues empty | |
7566 | * - clear the memory bitmaps | |
7567 | * | |
7568 | * NOTE: pgdat should get zeroed by caller. | |
7569 | * NOTE: this function is only called during early init. | |
7570 | */ | |
7571 | static void __init free_area_init_core(struct pglist_data *pgdat) | |
7572 | { | |
7573 | enum zone_type j; | |
7574 | int nid = pgdat->node_id; | |
5f63b720 | 7575 | |
03e85f9d | 7576 | pgdat_init_internals(pgdat); |
385386cf JW |
7577 | pgdat->per_cpu_nodestats = &boot_nodestats; |
7578 | ||
1da177e4 LT |
7579 | for (j = 0; j < MAX_NR_ZONES; j++) { |
7580 | struct zone *zone = pgdat->node_zones + j; | |
e6943859 | 7581 | unsigned long size, freesize, memmap_pages; |
1da177e4 | 7582 | |
febd5949 | 7583 | size = zone->spanned_pages; |
e6943859 | 7584 | freesize = zone->present_pages; |
1da177e4 | 7585 | |
0e0b864e | 7586 | /* |
9feedc9d | 7587 | * Adjust freesize so that it accounts for how much memory |
0e0b864e MG |
7588 | * is used by this zone for memmap. This affects the watermark |
7589 | * and per-cpu initialisations | |
7590 | */ | |
e6943859 | 7591 | memmap_pages = calc_memmap_size(size, freesize); |
ba914f48 ZH |
7592 | if (!is_highmem_idx(j)) { |
7593 | if (freesize >= memmap_pages) { | |
7594 | freesize -= memmap_pages; | |
7595 | if (memmap_pages) | |
9660ecaa HK |
7596 | pr_debug(" %s zone: %lu pages used for memmap\n", |
7597 | zone_names[j], memmap_pages); | |
ba914f48 | 7598 | } else |
e47aa905 | 7599 | pr_warn(" %s zone: %lu memmap pages exceeds freesize %lu\n", |
ba914f48 ZH |
7600 | zone_names[j], memmap_pages, freesize); |
7601 | } | |
0e0b864e | 7602 | |
6267276f | 7603 | /* Account for reserved pages */ |
9feedc9d JL |
7604 | if (j == 0 && freesize > dma_reserve) { |
7605 | freesize -= dma_reserve; | |
9660ecaa | 7606 | pr_debug(" %s zone: %lu pages reserved\n", zone_names[0], dma_reserve); |
0e0b864e MG |
7607 | } |
7608 | ||
98d2b0eb | 7609 | if (!is_highmem_idx(j)) |
9feedc9d | 7610 | nr_kernel_pages += freesize; |
01cefaef JL |
7611 | /* Charge for highmem memmap if there are enough kernel pages */ |
7612 | else if (nr_kernel_pages > memmap_pages * 2) | |
7613 | nr_kernel_pages -= memmap_pages; | |
9feedc9d | 7614 | nr_all_pages += freesize; |
1da177e4 | 7615 | |
9feedc9d JL |
7616 | /* |
7617 | * Set an approximate value for lowmem here, it will be adjusted | |
7618 | * when the bootmem allocator frees pages into the buddy system. | |
7619 | * And all highmem pages will be managed by the buddy system. | |
7620 | */ | |
03e85f9d | 7621 | zone_init_internals(zone, j, nid, freesize); |
81c0a2bb | 7622 | |
d883c6cf | 7623 | if (!size) |
1da177e4 LT |
7624 | continue; |
7625 | ||
955c1cd7 | 7626 | set_pageblock_order(); |
7010a6ec | 7627 | setup_usemap(zone); |
9699ee7b | 7628 | init_currently_empty_zone(zone, zone->zone_start_pfn, size); |
1da177e4 LT |
7629 | } |
7630 | } | |
7631 | ||
43b02ba9 | 7632 | #ifdef CONFIG_FLATMEM |
3b446da6 | 7633 | static void __init alloc_node_mem_map(struct pglist_data *pgdat) |
1da177e4 | 7634 | { |
b0aeba74 | 7635 | unsigned long __maybe_unused start = 0; |
a1c34a3b LA |
7636 | unsigned long __maybe_unused offset = 0; |
7637 | ||
1da177e4 LT |
7638 | /* Skip empty nodes */ |
7639 | if (!pgdat->node_spanned_pages) | |
7640 | return; | |
7641 | ||
b0aeba74 TL |
7642 | start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1); |
7643 | offset = pgdat->node_start_pfn - start; | |
1da177e4 LT |
7644 | /* ia64 gets its own node_mem_map, before this, without bootmem */ |
7645 | if (!pgdat->node_mem_map) { | |
b0aeba74 | 7646 | unsigned long size, end; |
d41dee36 AW |
7647 | struct page *map; |
7648 | ||
e984bb43 BP |
7649 | /* |
7650 | * The zone's endpoints aren't required to be MAX_ORDER | |
7651 | * aligned but the node_mem_map endpoints must be in order | |
7652 | * for the buddy allocator to function correctly. | |
7653 | */ | |
108bcc96 | 7654 | end = pgdat_end_pfn(pgdat); |
e984bb43 BP |
7655 | end = ALIGN(end, MAX_ORDER_NR_PAGES); |
7656 | size = (end - start) * sizeof(struct page); | |
c803b3c8 MR |
7657 | map = memmap_alloc(size, SMP_CACHE_BYTES, MEMBLOCK_LOW_LIMIT, |
7658 | pgdat->node_id, false); | |
23a7052a MR |
7659 | if (!map) |
7660 | panic("Failed to allocate %ld bytes for node %d memory map\n", | |
7661 | size, pgdat->node_id); | |
a1c34a3b | 7662 | pgdat->node_mem_map = map + offset; |
1da177e4 | 7663 | } |
0cd842f9 OS |
7664 | pr_debug("%s: node %d, pgdat %08lx, node_mem_map %08lx\n", |
7665 | __func__, pgdat->node_id, (unsigned long)pgdat, | |
7666 | (unsigned long)pgdat->node_mem_map); | |
a9ee6cf5 | 7667 | #ifndef CONFIG_NUMA |
1da177e4 LT |
7668 | /* |
7669 | * With no DISCONTIG, the global mem_map is just set as node 0's | |
7670 | */ | |
c713216d | 7671 | if (pgdat == NODE_DATA(0)) { |
1da177e4 | 7672 | mem_map = NODE_DATA(0)->node_mem_map; |
c713216d | 7673 | if (page_to_pfn(mem_map) != pgdat->node_start_pfn) |
a1c34a3b | 7674 | mem_map -= offset; |
c713216d | 7675 | } |
1da177e4 LT |
7676 | #endif |
7677 | } | |
0cd842f9 | 7678 | #else |
3b446da6 | 7679 | static inline void alloc_node_mem_map(struct pglist_data *pgdat) { } |
43b02ba9 | 7680 | #endif /* CONFIG_FLATMEM */ |
1da177e4 | 7681 | |
0188dc98 OS |
7682 | #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
7683 | static inline void pgdat_set_deferred_range(pg_data_t *pgdat) | |
7684 | { | |
0188dc98 OS |
7685 | pgdat->first_deferred_pfn = ULONG_MAX; |
7686 | } | |
7687 | #else | |
7688 | static inline void pgdat_set_deferred_range(pg_data_t *pgdat) {} | |
7689 | #endif | |
7690 | ||
854e8848 | 7691 | static void __init free_area_init_node(int nid) |
1da177e4 | 7692 | { |
9109fb7b | 7693 | pg_data_t *pgdat = NODE_DATA(nid); |
7960aedd ZY |
7694 | unsigned long start_pfn = 0; |
7695 | unsigned long end_pfn = 0; | |
9109fb7b | 7696 | |
88fdf75d | 7697 | /* pg_data_t should be reset to zero when it's allocated */ |
97a225e6 | 7698 | WARN_ON(pgdat->nr_zones || pgdat->kswapd_highest_zoneidx); |
88fdf75d | 7699 | |
854e8848 | 7700 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); |
88fdf75d | 7701 | |
1da177e4 | 7702 | pgdat->node_id = nid; |
854e8848 | 7703 | pgdat->node_start_pfn = start_pfn; |
75ef7184 | 7704 | pgdat->per_cpu_nodestats = NULL; |
854e8848 | 7705 | |
7c30daac MH |
7706 | if (start_pfn != end_pfn) { |
7707 | pr_info("Initmem setup node %d [mem %#018Lx-%#018Lx]\n", nid, | |
7708 | (u64)start_pfn << PAGE_SHIFT, | |
7709 | end_pfn ? ((u64)end_pfn << PAGE_SHIFT) - 1 : 0); | |
7710 | } else { | |
7711 | pr_info("Initmem setup node %d as memoryless\n", nid); | |
7712 | } | |
7713 | ||
854e8848 | 7714 | calculate_node_totalpages(pgdat, start_pfn, end_pfn); |
1da177e4 LT |
7715 | |
7716 | alloc_node_mem_map(pgdat); | |
0188dc98 | 7717 | pgdat_set_deferred_range(pgdat); |
1da177e4 | 7718 | |
7f3eb55b | 7719 | free_area_init_core(pgdat); |
1da177e4 LT |
7720 | } |
7721 | ||
1ca75fa7 | 7722 | static void __init free_area_init_memoryless_node(int nid) |
3f08a302 | 7723 | { |
854e8848 | 7724 | free_area_init_node(nid); |
3f08a302 MR |
7725 | } |
7726 | ||
418508c1 MS |
7727 | #if MAX_NUMNODES > 1 |
7728 | /* | |
7729 | * Figure out the number of possible node ids. | |
7730 | */ | |
f9872caf | 7731 | void __init setup_nr_node_ids(void) |
418508c1 | 7732 | { |
904a9553 | 7733 | unsigned int highest; |
418508c1 | 7734 | |
904a9553 | 7735 | highest = find_last_bit(node_possible_map.bits, MAX_NUMNODES); |
418508c1 MS |
7736 | nr_node_ids = highest + 1; |
7737 | } | |
418508c1 MS |
7738 | #endif |
7739 | ||
1e01979c TH |
7740 | /** |
7741 | * node_map_pfn_alignment - determine the maximum internode alignment | |
7742 | * | |
7743 | * This function should be called after node map is populated and sorted. | |
7744 | * It calculates the maximum power of two alignment which can distinguish | |
7745 | * all the nodes. | |
7746 | * | |
7747 | * For example, if all nodes are 1GiB and aligned to 1GiB, the return value | |
7748 | * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the | |
7749 | * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is | |
7750 | * shifted, 1GiB is enough and this function will indicate so. | |
7751 | * | |
7752 | * This is used to test whether pfn -> nid mapping of the chosen memory | |
7753 | * model has fine enough granularity to avoid incorrect mapping for the | |
7754 | * populated node map. | |
7755 | * | |
a862f68a | 7756 | * Return: the determined alignment in pfn's. 0 if there is no alignment |
1e01979c TH |
7757 | * requirement (single node). |
7758 | */ | |
7759 | unsigned long __init node_map_pfn_alignment(void) | |
7760 | { | |
7761 | unsigned long accl_mask = 0, last_end = 0; | |
c13291a5 | 7762 | unsigned long start, end, mask; |
98fa15f3 | 7763 | int last_nid = NUMA_NO_NODE; |
c13291a5 | 7764 | int i, nid; |
1e01979c | 7765 | |
c13291a5 | 7766 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) { |
1e01979c TH |
7767 | if (!start || last_nid < 0 || last_nid == nid) { |
7768 | last_nid = nid; | |
7769 | last_end = end; | |
7770 | continue; | |
7771 | } | |
7772 | ||
7773 | /* | |
7774 | * Start with a mask granular enough to pin-point to the | |
7775 | * start pfn and tick off bits one-by-one until it becomes | |
7776 | * too coarse to separate the current node from the last. | |
7777 | */ | |
7778 | mask = ~((1 << __ffs(start)) - 1); | |
7779 | while (mask && last_end <= (start & (mask << 1))) | |
7780 | mask <<= 1; | |
7781 | ||
7782 | /* accumulate all internode masks */ | |
7783 | accl_mask |= mask; | |
7784 | } | |
7785 | ||
7786 | /* convert mask to number of pages */ | |
7787 | return ~accl_mask + 1; | |
7788 | } | |
7789 | ||
c713216d MG |
7790 | /** |
7791 | * find_min_pfn_with_active_regions - Find the minimum PFN registered | |
7792 | * | |
a862f68a | 7793 | * Return: the minimum PFN based on information provided via |
7d018176 | 7794 | * memblock_set_node(). |
c713216d MG |
7795 | */ |
7796 | unsigned long __init find_min_pfn_with_active_regions(void) | |
7797 | { | |
8a1b25fe | 7798 | return PHYS_PFN(memblock_start_of_DRAM()); |
c713216d MG |
7799 | } |
7800 | ||
37b07e41 LS |
7801 | /* |
7802 | * early_calculate_totalpages() | |
7803 | * Sum pages in active regions for movable zone. | |
4b0ef1fe | 7804 | * Populate N_MEMORY for calculating usable_nodes. |
37b07e41 | 7805 | */ |
484f51f8 | 7806 | static unsigned long __init early_calculate_totalpages(void) |
7e63efef | 7807 | { |
7e63efef | 7808 | unsigned long totalpages = 0; |
c13291a5 TH |
7809 | unsigned long start_pfn, end_pfn; |
7810 | int i, nid; | |
7811 | ||
7812 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { | |
7813 | unsigned long pages = end_pfn - start_pfn; | |
7e63efef | 7814 | |
37b07e41 LS |
7815 | totalpages += pages; |
7816 | if (pages) | |
4b0ef1fe | 7817 | node_set_state(nid, N_MEMORY); |
37b07e41 | 7818 | } |
b8af2941 | 7819 | return totalpages; |
7e63efef MG |
7820 | } |
7821 | ||
2a1e274a MG |
7822 | /* |
7823 | * Find the PFN the Movable zone begins in each node. Kernel memory | |
7824 | * is spread evenly between nodes as long as the nodes have enough | |
7825 | * memory. When they don't, some nodes will have more kernelcore than | |
7826 | * others | |
7827 | */ | |
b224ef85 | 7828 | static void __init find_zone_movable_pfns_for_nodes(void) |
2a1e274a MG |
7829 | { |
7830 | int i, nid; | |
7831 | unsigned long usable_startpfn; | |
7832 | unsigned long kernelcore_node, kernelcore_remaining; | |
66918dcd | 7833 | /* save the state before borrow the nodemask */ |
4b0ef1fe | 7834 | nodemask_t saved_node_state = node_states[N_MEMORY]; |
37b07e41 | 7835 | unsigned long totalpages = early_calculate_totalpages(); |
4b0ef1fe | 7836 | int usable_nodes = nodes_weight(node_states[N_MEMORY]); |
136199f0 | 7837 | struct memblock_region *r; |
b2f3eebe TC |
7838 | |
7839 | /* Need to find movable_zone earlier when movable_node is specified. */ | |
7840 | find_usable_zone_for_movable(); | |
7841 | ||
7842 | /* | |
7843 | * If movable_node is specified, ignore kernelcore and movablecore | |
7844 | * options. | |
7845 | */ | |
7846 | if (movable_node_is_enabled()) { | |
cc6de168 | 7847 | for_each_mem_region(r) { |
136199f0 | 7848 | if (!memblock_is_hotpluggable(r)) |
b2f3eebe TC |
7849 | continue; |
7850 | ||
d622abf7 | 7851 | nid = memblock_get_region_node(r); |
b2f3eebe | 7852 | |
136199f0 | 7853 | usable_startpfn = PFN_DOWN(r->base); |
b2f3eebe TC |
7854 | zone_movable_pfn[nid] = zone_movable_pfn[nid] ? |
7855 | min(usable_startpfn, zone_movable_pfn[nid]) : | |
7856 | usable_startpfn; | |
7857 | } | |
7858 | ||
7859 | goto out2; | |
7860 | } | |
2a1e274a | 7861 | |
342332e6 TI |
7862 | /* |
7863 | * If kernelcore=mirror is specified, ignore movablecore option | |
7864 | */ | |
7865 | if (mirrored_kernelcore) { | |
7866 | bool mem_below_4gb_not_mirrored = false; | |
7867 | ||
cc6de168 | 7868 | for_each_mem_region(r) { |
342332e6 TI |
7869 | if (memblock_is_mirror(r)) |
7870 | continue; | |
7871 | ||
d622abf7 | 7872 | nid = memblock_get_region_node(r); |
342332e6 TI |
7873 | |
7874 | usable_startpfn = memblock_region_memory_base_pfn(r); | |
7875 | ||
aa282a15 | 7876 | if (usable_startpfn < PHYS_PFN(SZ_4G)) { |
342332e6 TI |
7877 | mem_below_4gb_not_mirrored = true; |
7878 | continue; | |
7879 | } | |
7880 | ||
7881 | zone_movable_pfn[nid] = zone_movable_pfn[nid] ? | |
7882 | min(usable_startpfn, zone_movable_pfn[nid]) : | |
7883 | usable_startpfn; | |
7884 | } | |
7885 | ||
7886 | if (mem_below_4gb_not_mirrored) | |
633bf2fe | 7887 | pr_warn("This configuration results in unmirrored kernel memory.\n"); |
342332e6 TI |
7888 | |
7889 | goto out2; | |
7890 | } | |
7891 | ||
7e63efef | 7892 | /* |
a5c6d650 DR |
7893 | * If kernelcore=nn% or movablecore=nn% was specified, calculate the |
7894 | * amount of necessary memory. | |
7895 | */ | |
7896 | if (required_kernelcore_percent) | |
7897 | required_kernelcore = (totalpages * 100 * required_kernelcore_percent) / | |
7898 | 10000UL; | |
7899 | if (required_movablecore_percent) | |
7900 | required_movablecore = (totalpages * 100 * required_movablecore_percent) / | |
7901 | 10000UL; | |
7902 | ||
7903 | /* | |
7904 | * If movablecore= was specified, calculate what size of | |
7e63efef MG |
7905 | * kernelcore that corresponds so that memory usable for |
7906 | * any allocation type is evenly spread. If both kernelcore | |
7907 | * and movablecore are specified, then the value of kernelcore | |
7908 | * will be used for required_kernelcore if it's greater than | |
7909 | * what movablecore would have allowed. | |
7910 | */ | |
7911 | if (required_movablecore) { | |
7e63efef MG |
7912 | unsigned long corepages; |
7913 | ||
7914 | /* | |
7915 | * Round-up so that ZONE_MOVABLE is at least as large as what | |
7916 | * was requested by the user | |
7917 | */ | |
7918 | required_movablecore = | |
7919 | roundup(required_movablecore, MAX_ORDER_NR_PAGES); | |
9fd745d4 | 7920 | required_movablecore = min(totalpages, required_movablecore); |
7e63efef MG |
7921 | corepages = totalpages - required_movablecore; |
7922 | ||
7923 | required_kernelcore = max(required_kernelcore, corepages); | |
7924 | } | |
7925 | ||
bde304bd XQ |
7926 | /* |
7927 | * If kernelcore was not specified or kernelcore size is larger | |
7928 | * than totalpages, there is no ZONE_MOVABLE. | |
7929 | */ | |
7930 | if (!required_kernelcore || required_kernelcore >= totalpages) | |
66918dcd | 7931 | goto out; |
2a1e274a MG |
7932 | |
7933 | /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */ | |
2a1e274a MG |
7934 | usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone]; |
7935 | ||
7936 | restart: | |
7937 | /* Spread kernelcore memory as evenly as possible throughout nodes */ | |
7938 | kernelcore_node = required_kernelcore / usable_nodes; | |
4b0ef1fe | 7939 | for_each_node_state(nid, N_MEMORY) { |
c13291a5 TH |
7940 | unsigned long start_pfn, end_pfn; |
7941 | ||
2a1e274a MG |
7942 | /* |
7943 | * Recalculate kernelcore_node if the division per node | |
7944 | * now exceeds what is necessary to satisfy the requested | |
7945 | * amount of memory for the kernel | |
7946 | */ | |
7947 | if (required_kernelcore < kernelcore_node) | |
7948 | kernelcore_node = required_kernelcore / usable_nodes; | |
7949 | ||
7950 | /* | |
7951 | * As the map is walked, we track how much memory is usable | |
7952 | * by the kernel using kernelcore_remaining. When it is | |
7953 | * 0, the rest of the node is usable by ZONE_MOVABLE | |
7954 | */ | |
7955 | kernelcore_remaining = kernelcore_node; | |
7956 | ||
7957 | /* Go through each range of PFNs within this node */ | |
c13291a5 | 7958 | for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) { |
2a1e274a MG |
7959 | unsigned long size_pages; |
7960 | ||
c13291a5 | 7961 | start_pfn = max(start_pfn, zone_movable_pfn[nid]); |
2a1e274a MG |
7962 | if (start_pfn >= end_pfn) |
7963 | continue; | |
7964 | ||
7965 | /* Account for what is only usable for kernelcore */ | |
7966 | if (start_pfn < usable_startpfn) { | |
7967 | unsigned long kernel_pages; | |
7968 | kernel_pages = min(end_pfn, usable_startpfn) | |
7969 | - start_pfn; | |
7970 | ||
7971 | kernelcore_remaining -= min(kernel_pages, | |
7972 | kernelcore_remaining); | |
7973 | required_kernelcore -= min(kernel_pages, | |
7974 | required_kernelcore); | |
7975 | ||
7976 | /* Continue if range is now fully accounted */ | |
7977 | if (end_pfn <= usable_startpfn) { | |
7978 | ||
7979 | /* | |
7980 | * Push zone_movable_pfn to the end so | |
7981 | * that if we have to rebalance | |
7982 | * kernelcore across nodes, we will | |
7983 | * not double account here | |
7984 | */ | |
7985 | zone_movable_pfn[nid] = end_pfn; | |
7986 | continue; | |
7987 | } | |
7988 | start_pfn = usable_startpfn; | |
7989 | } | |
7990 | ||
7991 | /* | |
7992 | * The usable PFN range for ZONE_MOVABLE is from | |
7993 | * start_pfn->end_pfn. Calculate size_pages as the | |
7994 | * number of pages used as kernelcore | |
7995 | */ | |
7996 | size_pages = end_pfn - start_pfn; | |
7997 | if (size_pages > kernelcore_remaining) | |
7998 | size_pages = kernelcore_remaining; | |
7999 | zone_movable_pfn[nid] = start_pfn + size_pages; | |
8000 | ||
8001 | /* | |
8002 | * Some kernelcore has been met, update counts and | |
8003 | * break if the kernelcore for this node has been | |
b8af2941 | 8004 | * satisfied |
2a1e274a MG |
8005 | */ |
8006 | required_kernelcore -= min(required_kernelcore, | |
8007 | size_pages); | |
8008 | kernelcore_remaining -= size_pages; | |
8009 | if (!kernelcore_remaining) | |
8010 | break; | |
8011 | } | |
8012 | } | |
8013 | ||
8014 | /* | |
8015 | * If there is still required_kernelcore, we do another pass with one | |
8016 | * less node in the count. This will push zone_movable_pfn[nid] further | |
8017 | * along on the nodes that still have memory until kernelcore is | |
b8af2941 | 8018 | * satisfied |
2a1e274a MG |
8019 | */ |
8020 | usable_nodes--; | |
8021 | if (usable_nodes && required_kernelcore > usable_nodes) | |
8022 | goto restart; | |
8023 | ||
b2f3eebe | 8024 | out2: |
2a1e274a | 8025 | /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */ |
ddbc84f3 AP |
8026 | for (nid = 0; nid < MAX_NUMNODES; nid++) { |
8027 | unsigned long start_pfn, end_pfn; | |
8028 | ||
2a1e274a MG |
8029 | zone_movable_pfn[nid] = |
8030 | roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES); | |
66918dcd | 8031 | |
ddbc84f3 AP |
8032 | get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); |
8033 | if (zone_movable_pfn[nid] >= end_pfn) | |
8034 | zone_movable_pfn[nid] = 0; | |
8035 | } | |
8036 | ||
20e6926d | 8037 | out: |
66918dcd | 8038 | /* restore the node_state */ |
4b0ef1fe | 8039 | node_states[N_MEMORY] = saved_node_state; |
2a1e274a MG |
8040 | } |
8041 | ||
4b0ef1fe LJ |
8042 | /* Any regular or high memory on that node ? */ |
8043 | static void check_for_memory(pg_data_t *pgdat, int nid) | |
37b07e41 | 8044 | { |
37b07e41 LS |
8045 | enum zone_type zone_type; |
8046 | ||
4b0ef1fe | 8047 | for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) { |
37b07e41 | 8048 | struct zone *zone = &pgdat->node_zones[zone_type]; |
b38a8725 | 8049 | if (populated_zone(zone)) { |
7b0e0c0e OS |
8050 | if (IS_ENABLED(CONFIG_HIGHMEM)) |
8051 | node_set_state(nid, N_HIGH_MEMORY); | |
8052 | if (zone_type <= ZONE_NORMAL) | |
4b0ef1fe | 8053 | node_set_state(nid, N_NORMAL_MEMORY); |
d0048b0e BL |
8054 | break; |
8055 | } | |
37b07e41 | 8056 | } |
37b07e41 LS |
8057 | } |
8058 | ||
51930df5 | 8059 | /* |
f0953a1b | 8060 | * Some architectures, e.g. ARC may have ZONE_HIGHMEM below ZONE_NORMAL. For |
51930df5 MR |
8061 | * such cases we allow max_zone_pfn sorted in the descending order |
8062 | */ | |
8063 | bool __weak arch_has_descending_max_zone_pfns(void) | |
8064 | { | |
8065 | return false; | |
8066 | } | |
8067 | ||
c713216d | 8068 | /** |
9691a071 | 8069 | * free_area_init - Initialise all pg_data_t and zone data |
88ca3b94 | 8070 | * @max_zone_pfn: an array of max PFNs for each zone |
c713216d MG |
8071 | * |
8072 | * This will call free_area_init_node() for each active node in the system. | |
7d018176 | 8073 | * Using the page ranges provided by memblock_set_node(), the size of each |
c713216d MG |
8074 | * zone in each node and their holes is calculated. If the maximum PFN |
8075 | * between two adjacent zones match, it is assumed that the zone is empty. | |
8076 | * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed | |
8077 | * that arch_max_dma32_pfn has no pages. It is also assumed that a zone | |
8078 | * starts where the previous one ended. For example, ZONE_DMA32 starts | |
8079 | * at arch_max_dma_pfn. | |
8080 | */ | |
9691a071 | 8081 | void __init free_area_init(unsigned long *max_zone_pfn) |
c713216d | 8082 | { |
c13291a5 | 8083 | unsigned long start_pfn, end_pfn; |
51930df5 MR |
8084 | int i, nid, zone; |
8085 | bool descending; | |
a6af2bc3 | 8086 | |
c713216d MG |
8087 | /* Record where the zone boundaries are */ |
8088 | memset(arch_zone_lowest_possible_pfn, 0, | |
8089 | sizeof(arch_zone_lowest_possible_pfn)); | |
8090 | memset(arch_zone_highest_possible_pfn, 0, | |
8091 | sizeof(arch_zone_highest_possible_pfn)); | |
90cae1fe OH |
8092 | |
8093 | start_pfn = find_min_pfn_with_active_regions(); | |
51930df5 | 8094 | descending = arch_has_descending_max_zone_pfns(); |
90cae1fe OH |
8095 | |
8096 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
51930df5 MR |
8097 | if (descending) |
8098 | zone = MAX_NR_ZONES - i - 1; | |
8099 | else | |
8100 | zone = i; | |
8101 | ||
8102 | if (zone == ZONE_MOVABLE) | |
2a1e274a | 8103 | continue; |
90cae1fe | 8104 | |
51930df5 MR |
8105 | end_pfn = max(max_zone_pfn[zone], start_pfn); |
8106 | arch_zone_lowest_possible_pfn[zone] = start_pfn; | |
8107 | arch_zone_highest_possible_pfn[zone] = end_pfn; | |
90cae1fe OH |
8108 | |
8109 | start_pfn = end_pfn; | |
c713216d | 8110 | } |
2a1e274a MG |
8111 | |
8112 | /* Find the PFNs that ZONE_MOVABLE begins at in each node */ | |
8113 | memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn)); | |
b224ef85 | 8114 | find_zone_movable_pfns_for_nodes(); |
c713216d | 8115 | |
c713216d | 8116 | /* Print out the zone ranges */ |
f88dfff5 | 8117 | pr_info("Zone ranges:\n"); |
2a1e274a MG |
8118 | for (i = 0; i < MAX_NR_ZONES; i++) { |
8119 | if (i == ZONE_MOVABLE) | |
8120 | continue; | |
f88dfff5 | 8121 | pr_info(" %-8s ", zone_names[i]); |
72f0ba02 DR |
8122 | if (arch_zone_lowest_possible_pfn[i] == |
8123 | arch_zone_highest_possible_pfn[i]) | |
f88dfff5 | 8124 | pr_cont("empty\n"); |
72f0ba02 | 8125 | else |
8d29e18a JG |
8126 | pr_cont("[mem %#018Lx-%#018Lx]\n", |
8127 | (u64)arch_zone_lowest_possible_pfn[i] | |
8128 | << PAGE_SHIFT, | |
8129 | ((u64)arch_zone_highest_possible_pfn[i] | |
a62e2f4f | 8130 | << PAGE_SHIFT) - 1); |
2a1e274a MG |
8131 | } |
8132 | ||
8133 | /* Print out the PFNs ZONE_MOVABLE begins at in each node */ | |
f88dfff5 | 8134 | pr_info("Movable zone start for each node\n"); |
2a1e274a MG |
8135 | for (i = 0; i < MAX_NUMNODES; i++) { |
8136 | if (zone_movable_pfn[i]) | |
8d29e18a JG |
8137 | pr_info(" Node %d: %#018Lx\n", i, |
8138 | (u64)zone_movable_pfn[i] << PAGE_SHIFT); | |
2a1e274a | 8139 | } |
c713216d | 8140 | |
f46edbd1 DW |
8141 | /* |
8142 | * Print out the early node map, and initialize the | |
8143 | * subsection-map relative to active online memory ranges to | |
8144 | * enable future "sub-section" extensions of the memory map. | |
8145 | */ | |
f88dfff5 | 8146 | pr_info("Early memory node ranges\n"); |
f46edbd1 | 8147 | for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { |
8d29e18a JG |
8148 | pr_info(" node %3d: [mem %#018Lx-%#018Lx]\n", nid, |
8149 | (u64)start_pfn << PAGE_SHIFT, | |
8150 | ((u64)end_pfn << PAGE_SHIFT) - 1); | |
f46edbd1 DW |
8151 | subsection_map_init(start_pfn, end_pfn - start_pfn); |
8152 | } | |
c713216d MG |
8153 | |
8154 | /* Initialise every node */ | |
708614e6 | 8155 | mminit_verify_pageflags_layout(); |
8ef82866 | 8156 | setup_nr_node_ids(); |
09f49dca MH |
8157 | for_each_node(nid) { |
8158 | pg_data_t *pgdat; | |
8159 | ||
8160 | if (!node_online(nid)) { | |
8161 | pr_info("Initializing node %d as memoryless\n", nid); | |
8162 | ||
8163 | /* Allocator not initialized yet */ | |
8164 | pgdat = arch_alloc_nodedata(nid); | |
8165 | if (!pgdat) { | |
8166 | pr_err("Cannot allocate %zuB for node %d.\n", | |
8167 | sizeof(*pgdat), nid); | |
8168 | continue; | |
8169 | } | |
8170 | arch_refresh_nodedata(nid, pgdat); | |
8171 | free_area_init_memoryless_node(nid); | |
8172 | ||
8173 | /* | |
8174 | * We do not want to confuse userspace by sysfs | |
8175 | * files/directories for node without any memory | |
8176 | * attached to it, so this node is not marked as | |
8177 | * N_MEMORY and not marked online so that no sysfs | |
8178 | * hierarchy will be created via register_one_node for | |
8179 | * it. The pgdat will get fully initialized by | |
8180 | * hotadd_init_pgdat() when memory is hotplugged into | |
8181 | * this node. | |
8182 | */ | |
8183 | continue; | |
8184 | } | |
8185 | ||
8186 | pgdat = NODE_DATA(nid); | |
854e8848 | 8187 | free_area_init_node(nid); |
37b07e41 LS |
8188 | |
8189 | /* Any memory on that node */ | |
8190 | if (pgdat->node_present_pages) | |
4b0ef1fe LJ |
8191 | node_set_state(nid, N_MEMORY); |
8192 | check_for_memory(pgdat, nid); | |
c713216d | 8193 | } |
122e093c MR |
8194 | |
8195 | memmap_init(); | |
c713216d | 8196 | } |
2a1e274a | 8197 | |
a5c6d650 DR |
8198 | static int __init cmdline_parse_core(char *p, unsigned long *core, |
8199 | unsigned long *percent) | |
2a1e274a MG |
8200 | { |
8201 | unsigned long long coremem; | |
a5c6d650 DR |
8202 | char *endptr; |
8203 | ||
2a1e274a MG |
8204 | if (!p) |
8205 | return -EINVAL; | |
8206 | ||
a5c6d650 DR |
8207 | /* Value may be a percentage of total memory, otherwise bytes */ |
8208 | coremem = simple_strtoull(p, &endptr, 0); | |
8209 | if (*endptr == '%') { | |
8210 | /* Paranoid check for percent values greater than 100 */ | |
8211 | WARN_ON(coremem > 100); | |
2a1e274a | 8212 | |
a5c6d650 DR |
8213 | *percent = coremem; |
8214 | } else { | |
8215 | coremem = memparse(p, &p); | |
8216 | /* Paranoid check that UL is enough for the coremem value */ | |
8217 | WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX); | |
2a1e274a | 8218 | |
a5c6d650 DR |
8219 | *core = coremem >> PAGE_SHIFT; |
8220 | *percent = 0UL; | |
8221 | } | |
2a1e274a MG |
8222 | return 0; |
8223 | } | |
ed7ed365 | 8224 | |
7e63efef MG |
8225 | /* |
8226 | * kernelcore=size sets the amount of memory for use for allocations that | |
8227 | * cannot be reclaimed or migrated. | |
8228 | */ | |
8229 | static int __init cmdline_parse_kernelcore(char *p) | |
8230 | { | |
342332e6 TI |
8231 | /* parse kernelcore=mirror */ |
8232 | if (parse_option_str(p, "mirror")) { | |
8233 | mirrored_kernelcore = true; | |
8234 | return 0; | |
8235 | } | |
8236 | ||
a5c6d650 DR |
8237 | return cmdline_parse_core(p, &required_kernelcore, |
8238 | &required_kernelcore_percent); | |
7e63efef MG |
8239 | } |
8240 | ||
8241 | /* | |
8242 | * movablecore=size sets the amount of memory for use for allocations that | |
8243 | * can be reclaimed or migrated. | |
8244 | */ | |
8245 | static int __init cmdline_parse_movablecore(char *p) | |
8246 | { | |
a5c6d650 DR |
8247 | return cmdline_parse_core(p, &required_movablecore, |
8248 | &required_movablecore_percent); | |
7e63efef MG |
8249 | } |
8250 | ||
ed7ed365 | 8251 | early_param("kernelcore", cmdline_parse_kernelcore); |
7e63efef | 8252 | early_param("movablecore", cmdline_parse_movablecore); |
ed7ed365 | 8253 | |
c3d5f5f0 JL |
8254 | void adjust_managed_page_count(struct page *page, long count) |
8255 | { | |
9705bea5 | 8256 | atomic_long_add(count, &page_zone(page)->managed_pages); |
ca79b0c2 | 8257 | totalram_pages_add(count); |
3dcc0571 JL |
8258 | #ifdef CONFIG_HIGHMEM |
8259 | if (PageHighMem(page)) | |
ca79b0c2 | 8260 | totalhigh_pages_add(count); |
3dcc0571 | 8261 | #endif |
c3d5f5f0 | 8262 | } |
3dcc0571 | 8263 | EXPORT_SYMBOL(adjust_managed_page_count); |
c3d5f5f0 | 8264 | |
e5cb113f | 8265 | unsigned long free_reserved_area(void *start, void *end, int poison, const char *s) |
69afade7 | 8266 | { |
11199692 JL |
8267 | void *pos; |
8268 | unsigned long pages = 0; | |
69afade7 | 8269 | |
11199692 JL |
8270 | start = (void *)PAGE_ALIGN((unsigned long)start); |
8271 | end = (void *)((unsigned long)end & PAGE_MASK); | |
8272 | for (pos = start; pos < end; pos += PAGE_SIZE, pages++) { | |
0d834328 DH |
8273 | struct page *page = virt_to_page(pos); |
8274 | void *direct_map_addr; | |
8275 | ||
8276 | /* | |
8277 | * 'direct_map_addr' might be different from 'pos' | |
8278 | * because some architectures' virt_to_page() | |
8279 | * work with aliases. Getting the direct map | |
8280 | * address ensures that we get a _writeable_ | |
8281 | * alias for the memset(). | |
8282 | */ | |
8283 | direct_map_addr = page_address(page); | |
c746170d VF |
8284 | /* |
8285 | * Perform a kasan-unchecked memset() since this memory | |
8286 | * has not been initialized. | |
8287 | */ | |
8288 | direct_map_addr = kasan_reset_tag(direct_map_addr); | |
dbe67df4 | 8289 | if ((unsigned int)poison <= 0xFF) |
0d834328 DH |
8290 | memset(direct_map_addr, poison, PAGE_SIZE); |
8291 | ||
8292 | free_reserved_page(page); | |
69afade7 JL |
8293 | } |
8294 | ||
8295 | if (pages && s) | |
ff7ed9e4 | 8296 | pr_info("Freeing %s memory: %ldK\n", s, K(pages)); |
69afade7 JL |
8297 | |
8298 | return pages; | |
8299 | } | |
8300 | ||
1f9d03c5 | 8301 | void __init mem_init_print_info(void) |
7ee3d4e8 JL |
8302 | { |
8303 | unsigned long physpages, codesize, datasize, rosize, bss_size; | |
8304 | unsigned long init_code_size, init_data_size; | |
8305 | ||
8306 | physpages = get_num_physpages(); | |
8307 | codesize = _etext - _stext; | |
8308 | datasize = _edata - _sdata; | |
8309 | rosize = __end_rodata - __start_rodata; | |
8310 | bss_size = __bss_stop - __bss_start; | |
8311 | init_data_size = __init_end - __init_begin; | |
8312 | init_code_size = _einittext - _sinittext; | |
8313 | ||
8314 | /* | |
8315 | * Detect special cases and adjust section sizes accordingly: | |
8316 | * 1) .init.* may be embedded into .data sections | |
8317 | * 2) .init.text.* may be out of [__init_begin, __init_end], | |
8318 | * please refer to arch/tile/kernel/vmlinux.lds.S. | |
8319 | * 3) .rodata.* may be embedded into .text or .data sections. | |
8320 | */ | |
8321 | #define adj_init_size(start, end, size, pos, adj) \ | |
b8af2941 | 8322 | do { \ |
ca831f29 | 8323 | if (&start[0] <= &pos[0] && &pos[0] < &end[0] && size > adj) \ |
b8af2941 PK |
8324 | size -= adj; \ |
8325 | } while (0) | |
7ee3d4e8 JL |
8326 | |
8327 | adj_init_size(__init_begin, __init_end, init_data_size, | |
8328 | _sinittext, init_code_size); | |
8329 | adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size); | |
8330 | adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size); | |
8331 | adj_init_size(_stext, _etext, codesize, __start_rodata, rosize); | |
8332 | adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize); | |
8333 | ||
8334 | #undef adj_init_size | |
8335 | ||
756a025f | 8336 | pr_info("Memory: %luK/%luK available (%luK kernel code, %luK rwdata, %luK rodata, %luK init, %luK bss, %luK reserved, %luK cma-reserved" |
7ee3d4e8 | 8337 | #ifdef CONFIG_HIGHMEM |
756a025f | 8338 | ", %luK highmem" |
7ee3d4e8 | 8339 | #endif |
1f9d03c5 | 8340 | ")\n", |
ff7ed9e4 | 8341 | K(nr_free_pages()), K(physpages), |
756a025f JP |
8342 | codesize >> 10, datasize >> 10, rosize >> 10, |
8343 | (init_data_size + init_code_size) >> 10, bss_size >> 10, | |
ff7ed9e4 ML |
8344 | K(physpages - totalram_pages() - totalcma_pages), |
8345 | K(totalcma_pages) | |
7ee3d4e8 | 8346 | #ifdef CONFIG_HIGHMEM |
ff7ed9e4 | 8347 | , K(totalhigh_pages()) |
7ee3d4e8 | 8348 | #endif |
1f9d03c5 | 8349 | ); |
7ee3d4e8 JL |
8350 | } |
8351 | ||
0e0b864e | 8352 | /** |
88ca3b94 RD |
8353 | * set_dma_reserve - set the specified number of pages reserved in the first zone |
8354 | * @new_dma_reserve: The number of pages to mark reserved | |
0e0b864e | 8355 | * |
013110a7 | 8356 | * The per-cpu batchsize and zone watermarks are determined by managed_pages. |
0e0b864e MG |
8357 | * In the DMA zone, a significant percentage may be consumed by kernel image |
8358 | * and other unfreeable allocations which can skew the watermarks badly. This | |
88ca3b94 RD |
8359 | * function may optionally be used to account for unfreeable pages in the |
8360 | * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and | |
8361 | * smaller per-cpu batchsize. | |
0e0b864e MG |
8362 | */ |
8363 | void __init set_dma_reserve(unsigned long new_dma_reserve) | |
8364 | { | |
8365 | dma_reserve = new_dma_reserve; | |
8366 | } | |
8367 | ||
005fd4bb | 8368 | static int page_alloc_cpu_dead(unsigned int cpu) |
1da177e4 | 8369 | { |
04f8cfea | 8370 | struct zone *zone; |
1da177e4 | 8371 | |
005fd4bb | 8372 | lru_add_drain_cpu(cpu); |
adb11e78 | 8373 | mlock_page_drain_remote(cpu); |
005fd4bb | 8374 | drain_pages(cpu); |
9f8f2172 | 8375 | |
005fd4bb SAS |
8376 | /* |
8377 | * Spill the event counters of the dead processor | |
8378 | * into the current processors event counters. | |
8379 | * This artificially elevates the count of the current | |
8380 | * processor. | |
8381 | */ | |
8382 | vm_events_fold_cpu(cpu); | |
9f8f2172 | 8383 | |
005fd4bb SAS |
8384 | /* |
8385 | * Zero the differential counters of the dead processor | |
8386 | * so that the vm statistics are consistent. | |
8387 | * | |
8388 | * This is only okay since the processor is dead and cannot | |
8389 | * race with what we are doing. | |
8390 | */ | |
8391 | cpu_vm_stats_fold(cpu); | |
04f8cfea MG |
8392 | |
8393 | for_each_populated_zone(zone) | |
8394 | zone_pcp_update(zone, 0); | |
8395 | ||
8396 | return 0; | |
8397 | } | |
8398 | ||
8399 | static int page_alloc_cpu_online(unsigned int cpu) | |
8400 | { | |
8401 | struct zone *zone; | |
8402 | ||
8403 | for_each_populated_zone(zone) | |
8404 | zone_pcp_update(zone, 1); | |
005fd4bb | 8405 | return 0; |
1da177e4 | 8406 | } |
1da177e4 | 8407 | |
e03a5125 NP |
8408 | #ifdef CONFIG_NUMA |
8409 | int hashdist = HASHDIST_DEFAULT; | |
8410 | ||
8411 | static int __init set_hashdist(char *str) | |
8412 | { | |
8413 | if (!str) | |
8414 | return 0; | |
8415 | hashdist = simple_strtoul(str, &str, 0); | |
8416 | return 1; | |
8417 | } | |
8418 | __setup("hashdist=", set_hashdist); | |
8419 | #endif | |
8420 | ||
1da177e4 LT |
8421 | void __init page_alloc_init(void) |
8422 | { | |
005fd4bb SAS |
8423 | int ret; |
8424 | ||
e03a5125 NP |
8425 | #ifdef CONFIG_NUMA |
8426 | if (num_node_state(N_MEMORY) == 1) | |
8427 | hashdist = 0; | |
8428 | #endif | |
8429 | ||
04f8cfea MG |
8430 | ret = cpuhp_setup_state_nocalls(CPUHP_PAGE_ALLOC, |
8431 | "mm/page_alloc:pcp", | |
8432 | page_alloc_cpu_online, | |
005fd4bb SAS |
8433 | page_alloc_cpu_dead); |
8434 | WARN_ON(ret < 0); | |
1da177e4 LT |
8435 | } |
8436 | ||
cb45b0e9 | 8437 | /* |
34b10060 | 8438 | * calculate_totalreserve_pages - called when sysctl_lowmem_reserve_ratio |
cb45b0e9 HA |
8439 | * or min_free_kbytes changes. |
8440 | */ | |
8441 | static void calculate_totalreserve_pages(void) | |
8442 | { | |
8443 | struct pglist_data *pgdat; | |
8444 | unsigned long reserve_pages = 0; | |
2f6726e5 | 8445 | enum zone_type i, j; |
cb45b0e9 HA |
8446 | |
8447 | for_each_online_pgdat(pgdat) { | |
281e3726 MG |
8448 | |
8449 | pgdat->totalreserve_pages = 0; | |
8450 | ||
cb45b0e9 HA |
8451 | for (i = 0; i < MAX_NR_ZONES; i++) { |
8452 | struct zone *zone = pgdat->node_zones + i; | |
3484b2de | 8453 | long max = 0; |
9705bea5 | 8454 | unsigned long managed_pages = zone_managed_pages(zone); |
cb45b0e9 HA |
8455 | |
8456 | /* Find valid and maximum lowmem_reserve in the zone */ | |
8457 | for (j = i; j < MAX_NR_ZONES; j++) { | |
8458 | if (zone->lowmem_reserve[j] > max) | |
8459 | max = zone->lowmem_reserve[j]; | |
8460 | } | |
8461 | ||
41858966 MG |
8462 | /* we treat the high watermark as reserved pages. */ |
8463 | max += high_wmark_pages(zone); | |
cb45b0e9 | 8464 | |
3d6357de AK |
8465 | if (max > managed_pages) |
8466 | max = managed_pages; | |
a8d01437 | 8467 | |
281e3726 | 8468 | pgdat->totalreserve_pages += max; |
a8d01437 | 8469 | |
cb45b0e9 HA |
8470 | reserve_pages += max; |
8471 | } | |
8472 | } | |
8473 | totalreserve_pages = reserve_pages; | |
8474 | } | |
8475 | ||
1da177e4 LT |
8476 | /* |
8477 | * setup_per_zone_lowmem_reserve - called whenever | |
34b10060 | 8478 | * sysctl_lowmem_reserve_ratio changes. Ensures that each zone |
1da177e4 LT |
8479 | * has a correct pages reserved value, so an adequate number of |
8480 | * pages are left in the zone after a successful __alloc_pages(). | |
8481 | */ | |
8482 | static void setup_per_zone_lowmem_reserve(void) | |
8483 | { | |
8484 | struct pglist_data *pgdat; | |
470c61d7 | 8485 | enum zone_type i, j; |
1da177e4 | 8486 | |
ec936fc5 | 8487 | for_each_online_pgdat(pgdat) { |
470c61d7 LS |
8488 | for (i = 0; i < MAX_NR_ZONES - 1; i++) { |
8489 | struct zone *zone = &pgdat->node_zones[i]; | |
8490 | int ratio = sysctl_lowmem_reserve_ratio[i]; | |
8491 | bool clear = !ratio || !zone_managed_pages(zone); | |
8492 | unsigned long managed_pages = 0; | |
8493 | ||
8494 | for (j = i + 1; j < MAX_NR_ZONES; j++) { | |
f7ec1044 LS |
8495 | struct zone *upper_zone = &pgdat->node_zones[j]; |
8496 | ||
8497 | managed_pages += zone_managed_pages(upper_zone); | |
470c61d7 | 8498 | |
f7ec1044 LS |
8499 | if (clear) |
8500 | zone->lowmem_reserve[j] = 0; | |
8501 | else | |
470c61d7 | 8502 | zone->lowmem_reserve[j] = managed_pages / ratio; |
1da177e4 LT |
8503 | } |
8504 | } | |
8505 | } | |
cb45b0e9 HA |
8506 | |
8507 | /* update totalreserve_pages */ | |
8508 | calculate_totalreserve_pages(); | |
1da177e4 LT |
8509 | } |
8510 | ||
cfd3da1e | 8511 | static void __setup_per_zone_wmarks(void) |
1da177e4 LT |
8512 | { |
8513 | unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10); | |
8514 | unsigned long lowmem_pages = 0; | |
8515 | struct zone *zone; | |
8516 | unsigned long flags; | |
8517 | ||
8518 | /* Calculate total number of !ZONE_HIGHMEM pages */ | |
8519 | for_each_zone(zone) { | |
8520 | if (!is_highmem(zone)) | |
9705bea5 | 8521 | lowmem_pages += zone_managed_pages(zone); |
1da177e4 LT |
8522 | } |
8523 | ||
8524 | for_each_zone(zone) { | |
ac924c60 AM |
8525 | u64 tmp; |
8526 | ||
1125b4e3 | 8527 | spin_lock_irqsave(&zone->lock, flags); |
9705bea5 | 8528 | tmp = (u64)pages_min * zone_managed_pages(zone); |
ac924c60 | 8529 | do_div(tmp, lowmem_pages); |
1da177e4 LT |
8530 | if (is_highmem(zone)) { |
8531 | /* | |
669ed175 NP |
8532 | * __GFP_HIGH and PF_MEMALLOC allocations usually don't |
8533 | * need highmem pages, so cap pages_min to a small | |
8534 | * value here. | |
8535 | * | |
41858966 | 8536 | * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN) |
8bb4e7a2 | 8537 | * deltas control async page reclaim, and so should |
669ed175 | 8538 | * not be capped for highmem. |
1da177e4 | 8539 | */ |
90ae8d67 | 8540 | unsigned long min_pages; |
1da177e4 | 8541 | |
9705bea5 | 8542 | min_pages = zone_managed_pages(zone) / 1024; |
90ae8d67 | 8543 | min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL); |
a9214443 | 8544 | zone->_watermark[WMARK_MIN] = min_pages; |
1da177e4 | 8545 | } else { |
669ed175 NP |
8546 | /* |
8547 | * If it's a lowmem zone, reserve a number of pages | |
1da177e4 LT |
8548 | * proportionate to the zone's size. |
8549 | */ | |
a9214443 | 8550 | zone->_watermark[WMARK_MIN] = tmp; |
1da177e4 LT |
8551 | } |
8552 | ||
795ae7a0 JW |
8553 | /* |
8554 | * Set the kswapd watermarks distance according to the | |
8555 | * scale factor in proportion to available memory, but | |
8556 | * ensure a minimum size on small systems. | |
8557 | */ | |
8558 | tmp = max_t(u64, tmp >> 2, | |
9705bea5 | 8559 | mult_frac(zone_managed_pages(zone), |
795ae7a0 JW |
8560 | watermark_scale_factor, 10000)); |
8561 | ||
aa092591 | 8562 | zone->watermark_boost = 0; |
a9214443 | 8563 | zone->_watermark[WMARK_LOW] = min_wmark_pages(zone) + tmp; |
c574bbe9 HY |
8564 | zone->_watermark[WMARK_HIGH] = low_wmark_pages(zone) + tmp; |
8565 | zone->_watermark[WMARK_PROMO] = high_wmark_pages(zone) + tmp; | |
49f223a9 | 8566 | |
1125b4e3 | 8567 | spin_unlock_irqrestore(&zone->lock, flags); |
1da177e4 | 8568 | } |
cb45b0e9 HA |
8569 | |
8570 | /* update totalreserve_pages */ | |
8571 | calculate_totalreserve_pages(); | |
1da177e4 LT |
8572 | } |
8573 | ||
cfd3da1e MG |
8574 | /** |
8575 | * setup_per_zone_wmarks - called when min_free_kbytes changes | |
8576 | * or when memory is hot-{added|removed} | |
8577 | * | |
8578 | * Ensures that the watermark[min,low,high] values for each zone are set | |
8579 | * correctly with respect to min_free_kbytes. | |
8580 | */ | |
8581 | void setup_per_zone_wmarks(void) | |
8582 | { | |
b92ca18e | 8583 | struct zone *zone; |
b93e0f32 MH |
8584 | static DEFINE_SPINLOCK(lock); |
8585 | ||
8586 | spin_lock(&lock); | |
cfd3da1e | 8587 | __setup_per_zone_wmarks(); |
b93e0f32 | 8588 | spin_unlock(&lock); |
b92ca18e MG |
8589 | |
8590 | /* | |
8591 | * The watermark size have changed so update the pcpu batch | |
8592 | * and high limits or the limits may be inappropriate. | |
8593 | */ | |
8594 | for_each_zone(zone) | |
04f8cfea | 8595 | zone_pcp_update(zone, 0); |
cfd3da1e MG |
8596 | } |
8597 | ||
1da177e4 LT |
8598 | /* |
8599 | * Initialise min_free_kbytes. | |
8600 | * | |
8601 | * For small machines we want it small (128k min). For large machines | |
8beeae86 | 8602 | * we want it large (256MB max). But it is not linear, because network |
1da177e4 LT |
8603 | * bandwidth does not increase linearly with machine size. We use |
8604 | * | |
b8af2941 | 8605 | * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy: |
1da177e4 LT |
8606 | * min_free_kbytes = sqrt(lowmem_kbytes * 16) |
8607 | * | |
8608 | * which yields | |
8609 | * | |
8610 | * 16MB: 512k | |
8611 | * 32MB: 724k | |
8612 | * 64MB: 1024k | |
8613 | * 128MB: 1448k | |
8614 | * 256MB: 2048k | |
8615 | * 512MB: 2896k | |
8616 | * 1024MB: 4096k | |
8617 | * 2048MB: 5792k | |
8618 | * 4096MB: 8192k | |
8619 | * 8192MB: 11584k | |
8620 | * 16384MB: 16384k | |
8621 | */ | |
bd3400ea | 8622 | void calculate_min_free_kbytes(void) |
1da177e4 LT |
8623 | { |
8624 | unsigned long lowmem_kbytes; | |
5f12733e | 8625 | int new_min_free_kbytes; |
1da177e4 LT |
8626 | |
8627 | lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10); | |
5f12733e MH |
8628 | new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16); |
8629 | ||
59d336bd WS |
8630 | if (new_min_free_kbytes > user_min_free_kbytes) |
8631 | min_free_kbytes = clamp(new_min_free_kbytes, 128, 262144); | |
8632 | else | |
5f12733e MH |
8633 | pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n", |
8634 | new_min_free_kbytes, user_min_free_kbytes); | |
59d336bd | 8635 | |
bd3400ea LF |
8636 | } |
8637 | ||
8638 | int __meminit init_per_zone_wmark_min(void) | |
8639 | { | |
8640 | calculate_min_free_kbytes(); | |
bc75d33f | 8641 | setup_per_zone_wmarks(); |
a6cccdc3 | 8642 | refresh_zone_stat_thresholds(); |
1da177e4 | 8643 | setup_per_zone_lowmem_reserve(); |
6423aa81 JK |
8644 | |
8645 | #ifdef CONFIG_NUMA | |
8646 | setup_min_unmapped_ratio(); | |
8647 | setup_min_slab_ratio(); | |
8648 | #endif | |
8649 | ||
4aab2be0 VB |
8650 | khugepaged_min_free_kbytes_update(); |
8651 | ||
1da177e4 LT |
8652 | return 0; |
8653 | } | |
e08d3fdf | 8654 | postcore_initcall(init_per_zone_wmark_min) |
1da177e4 LT |
8655 | |
8656 | /* | |
b8af2941 | 8657 | * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so |
1da177e4 LT |
8658 | * that we can call two helper functions whenever min_free_kbytes |
8659 | * changes. | |
8660 | */ | |
cccad5b9 | 8661 | int min_free_kbytes_sysctl_handler(struct ctl_table *table, int write, |
32927393 | 8662 | void *buffer, size_t *length, loff_t *ppos) |
1da177e4 | 8663 | { |
da8c757b HP |
8664 | int rc; |
8665 | ||
8666 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
8667 | if (rc) | |
8668 | return rc; | |
8669 | ||
5f12733e MH |
8670 | if (write) { |
8671 | user_min_free_kbytes = min_free_kbytes; | |
bc75d33f | 8672 | setup_per_zone_wmarks(); |
5f12733e | 8673 | } |
1da177e4 LT |
8674 | return 0; |
8675 | } | |
8676 | ||
795ae7a0 | 8677 | int watermark_scale_factor_sysctl_handler(struct ctl_table *table, int write, |
32927393 | 8678 | void *buffer, size_t *length, loff_t *ppos) |
795ae7a0 JW |
8679 | { |
8680 | int rc; | |
8681 | ||
8682 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
8683 | if (rc) | |
8684 | return rc; | |
8685 | ||
8686 | if (write) | |
8687 | setup_per_zone_wmarks(); | |
8688 | ||
8689 | return 0; | |
8690 | } | |
8691 | ||
9614634f | 8692 | #ifdef CONFIG_NUMA |
6423aa81 | 8693 | static void setup_min_unmapped_ratio(void) |
9614634f | 8694 | { |
6423aa81 | 8695 | pg_data_t *pgdat; |
9614634f | 8696 | struct zone *zone; |
9614634f | 8697 | |
a5f5f91d | 8698 | for_each_online_pgdat(pgdat) |
81cbcbc2 | 8699 | pgdat->min_unmapped_pages = 0; |
a5f5f91d | 8700 | |
9614634f | 8701 | for_each_zone(zone) |
9705bea5 AK |
8702 | zone->zone_pgdat->min_unmapped_pages += (zone_managed_pages(zone) * |
8703 | sysctl_min_unmapped_ratio) / 100; | |
9614634f | 8704 | } |
0ff38490 | 8705 | |
6423aa81 JK |
8706 | |
8707 | int sysctl_min_unmapped_ratio_sysctl_handler(struct ctl_table *table, int write, | |
32927393 | 8708 | void *buffer, size_t *length, loff_t *ppos) |
0ff38490 | 8709 | { |
0ff38490 CL |
8710 | int rc; |
8711 | ||
8d65af78 | 8712 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); |
0ff38490 CL |
8713 | if (rc) |
8714 | return rc; | |
8715 | ||
6423aa81 JK |
8716 | setup_min_unmapped_ratio(); |
8717 | ||
8718 | return 0; | |
8719 | } | |
8720 | ||
8721 | static void setup_min_slab_ratio(void) | |
8722 | { | |
8723 | pg_data_t *pgdat; | |
8724 | struct zone *zone; | |
8725 | ||
a5f5f91d MG |
8726 | for_each_online_pgdat(pgdat) |
8727 | pgdat->min_slab_pages = 0; | |
8728 | ||
0ff38490 | 8729 | for_each_zone(zone) |
9705bea5 AK |
8730 | zone->zone_pgdat->min_slab_pages += (zone_managed_pages(zone) * |
8731 | sysctl_min_slab_ratio) / 100; | |
6423aa81 JK |
8732 | } |
8733 | ||
8734 | int sysctl_min_slab_ratio_sysctl_handler(struct ctl_table *table, int write, | |
32927393 | 8735 | void *buffer, size_t *length, loff_t *ppos) |
6423aa81 JK |
8736 | { |
8737 | int rc; | |
8738 | ||
8739 | rc = proc_dointvec_minmax(table, write, buffer, length, ppos); | |
8740 | if (rc) | |
8741 | return rc; | |
8742 | ||
8743 | setup_min_slab_ratio(); | |
8744 | ||
0ff38490 CL |
8745 | return 0; |
8746 | } | |
9614634f CL |
8747 | #endif |
8748 | ||
1da177e4 LT |
8749 | /* |
8750 | * lowmem_reserve_ratio_sysctl_handler - just a wrapper around | |
8751 | * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve() | |
8752 | * whenever sysctl_lowmem_reserve_ratio changes. | |
8753 | * | |
8754 | * The reserve ratio obviously has absolutely no relation with the | |
41858966 | 8755 | * minimum watermarks. The lowmem reserve ratio can only make sense |
1da177e4 LT |
8756 | * if in function of the boot time zone sizes. |
8757 | */ | |
cccad5b9 | 8758 | int lowmem_reserve_ratio_sysctl_handler(struct ctl_table *table, int write, |
32927393 | 8759 | void *buffer, size_t *length, loff_t *ppos) |
1da177e4 | 8760 | { |
86aaf255 BH |
8761 | int i; |
8762 | ||
8d65af78 | 8763 | proc_dointvec_minmax(table, write, buffer, length, ppos); |
86aaf255 BH |
8764 | |
8765 | for (i = 0; i < MAX_NR_ZONES; i++) { | |
8766 | if (sysctl_lowmem_reserve_ratio[i] < 1) | |
8767 | sysctl_lowmem_reserve_ratio[i] = 0; | |
8768 | } | |
8769 | ||
1da177e4 LT |
8770 | setup_per_zone_lowmem_reserve(); |
8771 | return 0; | |
8772 | } | |
8773 | ||
8ad4b1fb | 8774 | /* |
74f44822 MG |
8775 | * percpu_pagelist_high_fraction - changes the pcp->high for each zone on each |
8776 | * cpu. It is the fraction of total pages in each zone that a hot per cpu | |
b8af2941 | 8777 | * pagelist can have before it gets flushed back to buddy allocator. |
8ad4b1fb | 8778 | */ |
74f44822 MG |
8779 | int percpu_pagelist_high_fraction_sysctl_handler(struct ctl_table *table, |
8780 | int write, void *buffer, size_t *length, loff_t *ppos) | |
8ad4b1fb RS |
8781 | { |
8782 | struct zone *zone; | |
74f44822 | 8783 | int old_percpu_pagelist_high_fraction; |
8ad4b1fb RS |
8784 | int ret; |
8785 | ||
7cd2b0a3 | 8786 | mutex_lock(&pcp_batch_high_lock); |
74f44822 | 8787 | old_percpu_pagelist_high_fraction = percpu_pagelist_high_fraction; |
7cd2b0a3 | 8788 | |
8d65af78 | 8789 | ret = proc_dointvec_minmax(table, write, buffer, length, ppos); |
7cd2b0a3 DR |
8790 | if (!write || ret < 0) |
8791 | goto out; | |
8792 | ||
8793 | /* Sanity checking to avoid pcp imbalance */ | |
74f44822 MG |
8794 | if (percpu_pagelist_high_fraction && |
8795 | percpu_pagelist_high_fraction < MIN_PERCPU_PAGELIST_HIGH_FRACTION) { | |
8796 | percpu_pagelist_high_fraction = old_percpu_pagelist_high_fraction; | |
7cd2b0a3 DR |
8797 | ret = -EINVAL; |
8798 | goto out; | |
8799 | } | |
8800 | ||
8801 | /* No change? */ | |
74f44822 | 8802 | if (percpu_pagelist_high_fraction == old_percpu_pagelist_high_fraction) |
7cd2b0a3 | 8803 | goto out; |
c8e251fa | 8804 | |
cb1ef534 | 8805 | for_each_populated_zone(zone) |
74f44822 | 8806 | zone_set_pageset_high_and_batch(zone, 0); |
7cd2b0a3 | 8807 | out: |
c8e251fa | 8808 | mutex_unlock(&pcp_batch_high_lock); |
7cd2b0a3 | 8809 | return ret; |
8ad4b1fb RS |
8810 | } |
8811 | ||
f6f34b43 SD |
8812 | #ifndef __HAVE_ARCH_RESERVED_KERNEL_PAGES |
8813 | /* | |
8814 | * Returns the number of pages that arch has reserved but | |
8815 | * is not known to alloc_large_system_hash(). | |
8816 | */ | |
8817 | static unsigned long __init arch_reserved_kernel_pages(void) | |
8818 | { | |
8819 | return 0; | |
8820 | } | |
8821 | #endif | |
8822 | ||
9017217b PT |
8823 | /* |
8824 | * Adaptive scale is meant to reduce sizes of hash tables on large memory | |
8825 | * machines. As memory size is increased the scale is also increased but at | |
8826 | * slower pace. Starting from ADAPT_SCALE_BASE (64G), every time memory | |
8827 | * quadruples the scale is increased by one, which means the size of hash table | |
8828 | * only doubles, instead of quadrupling as well. | |
8829 | * Because 32-bit systems cannot have large physical memory, where this scaling | |
8830 | * makes sense, it is disabled on such platforms. | |
8831 | */ | |
8832 | #if __BITS_PER_LONG > 32 | |
8833 | #define ADAPT_SCALE_BASE (64ul << 30) | |
8834 | #define ADAPT_SCALE_SHIFT 2 | |
8835 | #define ADAPT_SCALE_NPAGES (ADAPT_SCALE_BASE >> PAGE_SHIFT) | |
8836 | #endif | |
8837 | ||
1da177e4 LT |
8838 | /* |
8839 | * allocate a large system hash table from bootmem | |
8840 | * - it is assumed that the hash table must contain an exact power-of-2 | |
8841 | * quantity of entries | |
8842 | * - limit is the number of hash buckets, not the total allocation size | |
8843 | */ | |
8844 | void *__init alloc_large_system_hash(const char *tablename, | |
8845 | unsigned long bucketsize, | |
8846 | unsigned long numentries, | |
8847 | int scale, | |
8848 | int flags, | |
8849 | unsigned int *_hash_shift, | |
8850 | unsigned int *_hash_mask, | |
31fe62b9 TB |
8851 | unsigned long low_limit, |
8852 | unsigned long high_limit) | |
1da177e4 | 8853 | { |
31fe62b9 | 8854 | unsigned long long max = high_limit; |
1da177e4 LT |
8855 | unsigned long log2qty, size; |
8856 | void *table = NULL; | |
3749a8f0 | 8857 | gfp_t gfp_flags; |
ec11408a | 8858 | bool virt; |
121e6f32 | 8859 | bool huge; |
1da177e4 LT |
8860 | |
8861 | /* allow the kernel cmdline to have a say */ | |
8862 | if (!numentries) { | |
8863 | /* round applicable memory size up to nearest megabyte */ | |
04903664 | 8864 | numentries = nr_kernel_pages; |
f6f34b43 | 8865 | numentries -= arch_reserved_kernel_pages(); |
a7e83318 JZ |
8866 | |
8867 | /* It isn't necessary when PAGE_SIZE >= 1MB */ | |
8868 | if (PAGE_SHIFT < 20) | |
8869 | numentries = round_up(numentries, (1<<20)/PAGE_SIZE); | |
1da177e4 | 8870 | |
9017217b PT |
8871 | #if __BITS_PER_LONG > 32 |
8872 | if (!high_limit) { | |
8873 | unsigned long adapt; | |
8874 | ||
8875 | for (adapt = ADAPT_SCALE_NPAGES; adapt < numentries; | |
8876 | adapt <<= ADAPT_SCALE_SHIFT) | |
8877 | scale++; | |
8878 | } | |
8879 | #endif | |
8880 | ||
1da177e4 LT |
8881 | /* limit to 1 bucket per 2^scale bytes of low memory */ |
8882 | if (scale > PAGE_SHIFT) | |
8883 | numentries >>= (scale - PAGE_SHIFT); | |
8884 | else | |
8885 | numentries <<= (PAGE_SHIFT - scale); | |
9ab37b8f PM |
8886 | |
8887 | /* Make sure we've got at least a 0-order allocation.. */ | |
2c85f51d JB |
8888 | if (unlikely(flags & HASH_SMALL)) { |
8889 | /* Makes no sense without HASH_EARLY */ | |
8890 | WARN_ON(!(flags & HASH_EARLY)); | |
8891 | if (!(numentries >> *_hash_shift)) { | |
8892 | numentries = 1UL << *_hash_shift; | |
8893 | BUG_ON(!numentries); | |
8894 | } | |
8895 | } else if (unlikely((numentries * bucketsize) < PAGE_SIZE)) | |
9ab37b8f | 8896 | numentries = PAGE_SIZE / bucketsize; |
1da177e4 | 8897 | } |
6e692ed3 | 8898 | numentries = roundup_pow_of_two(numentries); |
1da177e4 LT |
8899 | |
8900 | /* limit allocation size to 1/16 total memory by default */ | |
8901 | if (max == 0) { | |
8902 | max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4; | |
8903 | do_div(max, bucketsize); | |
8904 | } | |
074b8517 | 8905 | max = min(max, 0x80000000ULL); |
1da177e4 | 8906 | |
31fe62b9 TB |
8907 | if (numentries < low_limit) |
8908 | numentries = low_limit; | |
1da177e4 LT |
8909 | if (numentries > max) |
8910 | numentries = max; | |
8911 | ||
f0d1b0b3 | 8912 | log2qty = ilog2(numentries); |
1da177e4 | 8913 | |
3749a8f0 | 8914 | gfp_flags = (flags & HASH_ZERO) ? GFP_ATOMIC | __GFP_ZERO : GFP_ATOMIC; |
1da177e4 | 8915 | do { |
ec11408a | 8916 | virt = false; |
1da177e4 | 8917 | size = bucketsize << log2qty; |
ea1f5f37 PT |
8918 | if (flags & HASH_EARLY) { |
8919 | if (flags & HASH_ZERO) | |
26fb3dae | 8920 | table = memblock_alloc(size, SMP_CACHE_BYTES); |
ea1f5f37 | 8921 | else |
7e1c4e27 MR |
8922 | table = memblock_alloc_raw(size, |
8923 | SMP_CACHE_BYTES); | |
ec11408a | 8924 | } else if (get_order(size) >= MAX_ORDER || hashdist) { |
f2edd118 | 8925 | table = vmalloc_huge(size, gfp_flags); |
ec11408a | 8926 | virt = true; |
084f7e23 ED |
8927 | if (table) |
8928 | huge = is_vm_area_hugepages(table); | |
ea1f5f37 | 8929 | } else { |
1037b83b ED |
8930 | /* |
8931 | * If bucketsize is not a power-of-two, we may free | |
a1dd268c MG |
8932 | * some pages at the end of hash table which |
8933 | * alloc_pages_exact() automatically does | |
1037b83b | 8934 | */ |
ec11408a NP |
8935 | table = alloc_pages_exact(size, gfp_flags); |
8936 | kmemleak_alloc(table, size, 1, gfp_flags); | |
1da177e4 LT |
8937 | } |
8938 | } while (!table && size > PAGE_SIZE && --log2qty); | |
8939 | ||
8940 | if (!table) | |
8941 | panic("Failed to allocate %s hash table\n", tablename); | |
8942 | ||
ec11408a NP |
8943 | pr_info("%s hash table entries: %ld (order: %d, %lu bytes, %s)\n", |
8944 | tablename, 1UL << log2qty, ilog2(size) - PAGE_SHIFT, size, | |
121e6f32 | 8945 | virt ? (huge ? "vmalloc hugepage" : "vmalloc") : "linear"); |
1da177e4 LT |
8946 | |
8947 | if (_hash_shift) | |
8948 | *_hash_shift = log2qty; | |
8949 | if (_hash_mask) | |
8950 | *_hash_mask = (1 << log2qty) - 1; | |
8951 | ||
8952 | return table; | |
8953 | } | |
a117e66e | 8954 | |
8df995f6 | 8955 | #ifdef CONFIG_CONTIG_ALLOC |
a1394bdd MK |
8956 | #if defined(CONFIG_DYNAMIC_DEBUG) || \ |
8957 | (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE)) | |
8958 | /* Usage: See admin-guide/dynamic-debug-howto.rst */ | |
8959 | static void alloc_contig_dump_pages(struct list_head *page_list) | |
8960 | { | |
8961 | DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, "migrate failure"); | |
8962 | ||
8963 | if (DYNAMIC_DEBUG_BRANCH(descriptor)) { | |
8964 | struct page *page; | |
8965 | ||
8966 | dump_stack(); | |
8967 | list_for_each_entry(page, page_list, lru) | |
8968 | dump_page(page, "migration failure"); | |
8969 | } | |
8970 | } | |
8971 | #else | |
8972 | static inline void alloc_contig_dump_pages(struct list_head *page_list) | |
8973 | { | |
8974 | } | |
8975 | #endif | |
8976 | ||
041d3a8c | 8977 | /* [start, end) must belong to a single zone. */ |
b2c9e2fb | 8978 | int __alloc_contig_migrate_range(struct compact_control *cc, |
bb13ffeb | 8979 | unsigned long start, unsigned long end) |
041d3a8c MN |
8980 | { |
8981 | /* This function is based on compact_zone() from compaction.c. */ | |
730ec8c0 | 8982 | unsigned int nr_reclaimed; |
041d3a8c MN |
8983 | unsigned long pfn = start; |
8984 | unsigned int tries = 0; | |
8985 | int ret = 0; | |
8b94e0b8 JK |
8986 | struct migration_target_control mtc = { |
8987 | .nid = zone_to_nid(cc->zone), | |
8988 | .gfp_mask = GFP_USER | __GFP_MOVABLE | __GFP_RETRY_MAYFAIL, | |
8989 | }; | |
041d3a8c | 8990 | |
361a2a22 | 8991 | lru_cache_disable(); |
041d3a8c | 8992 | |
bb13ffeb | 8993 | while (pfn < end || !list_empty(&cc->migratepages)) { |
041d3a8c MN |
8994 | if (fatal_signal_pending(current)) { |
8995 | ret = -EINTR; | |
8996 | break; | |
8997 | } | |
8998 | ||
bb13ffeb MG |
8999 | if (list_empty(&cc->migratepages)) { |
9000 | cc->nr_migratepages = 0; | |
c2ad7a1f OS |
9001 | ret = isolate_migratepages_range(cc, pfn, end); |
9002 | if (ret && ret != -EAGAIN) | |
041d3a8c | 9003 | break; |
c2ad7a1f | 9004 | pfn = cc->migrate_pfn; |
041d3a8c MN |
9005 | tries = 0; |
9006 | } else if (++tries == 5) { | |
c8e28b47 | 9007 | ret = -EBUSY; |
041d3a8c MN |
9008 | break; |
9009 | } | |
9010 | ||
beb51eaa MK |
9011 | nr_reclaimed = reclaim_clean_pages_from_list(cc->zone, |
9012 | &cc->migratepages); | |
9013 | cc->nr_migratepages -= nr_reclaimed; | |
02c6de8d | 9014 | |
8b94e0b8 | 9015 | ret = migrate_pages(&cc->migratepages, alloc_migration_target, |
5ac95884 | 9016 | NULL, (unsigned long)&mtc, cc->mode, MR_CONTIG_RANGE, NULL); |
c8e28b47 OS |
9017 | |
9018 | /* | |
9019 | * On -ENOMEM, migrate_pages() bails out right away. It is pointless | |
9020 | * to retry again over this error, so do the same here. | |
9021 | */ | |
9022 | if (ret == -ENOMEM) | |
9023 | break; | |
041d3a8c | 9024 | } |
d479960e | 9025 | |
361a2a22 | 9026 | lru_cache_enable(); |
2a6f5124 | 9027 | if (ret < 0) { |
151e084a MK |
9028 | if (ret == -EBUSY) |
9029 | alloc_contig_dump_pages(&cc->migratepages); | |
2a6f5124 SP |
9030 | putback_movable_pages(&cc->migratepages); |
9031 | return ret; | |
9032 | } | |
9033 | return 0; | |
041d3a8c MN |
9034 | } |
9035 | ||
9036 | /** | |
9037 | * alloc_contig_range() -- tries to allocate given range of pages | |
9038 | * @start: start PFN to allocate | |
9039 | * @end: one-past-the-last PFN to allocate | |
f0953a1b | 9040 | * @migratetype: migratetype of the underlying pageblocks (either |
0815f3d8 MN |
9041 | * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks |
9042 | * in range must have the same migratetype and it must | |
9043 | * be either of the two. | |
ca96b625 | 9044 | * @gfp_mask: GFP mask to use during compaction |
041d3a8c | 9045 | * |
11ac3e87 ZY |
9046 | * The PFN range does not have to be pageblock aligned. The PFN range must |
9047 | * belong to a single zone. | |
041d3a8c | 9048 | * |
2c7452a0 MK |
9049 | * The first thing this routine does is attempt to MIGRATE_ISOLATE all |
9050 | * pageblocks in the range. Once isolated, the pageblocks should not | |
9051 | * be modified by others. | |
041d3a8c | 9052 | * |
a862f68a | 9053 | * Return: zero on success or negative error code. On success all |
041d3a8c MN |
9054 | * pages which PFN is in [start, end) are allocated for the caller and |
9055 | * need to be freed with free_contig_range(). | |
9056 | */ | |
0815f3d8 | 9057 | int alloc_contig_range(unsigned long start, unsigned long end, |
ca96b625 | 9058 | unsigned migratetype, gfp_t gfp_mask) |
041d3a8c | 9059 | { |
041d3a8c | 9060 | unsigned long outer_start, outer_end; |
b2c9e2fb | 9061 | int order; |
d00181b9 | 9062 | int ret = 0; |
041d3a8c | 9063 | |
bb13ffeb MG |
9064 | struct compact_control cc = { |
9065 | .nr_migratepages = 0, | |
9066 | .order = -1, | |
9067 | .zone = page_zone(pfn_to_page(start)), | |
e0b9daeb | 9068 | .mode = MIGRATE_SYNC, |
bb13ffeb | 9069 | .ignore_skip_hint = true, |
2583d671 | 9070 | .no_set_skip_hint = true, |
7dea19f9 | 9071 | .gfp_mask = current_gfp_context(gfp_mask), |
b06eda09 | 9072 | .alloc_contig = true, |
bb13ffeb MG |
9073 | }; |
9074 | INIT_LIST_HEAD(&cc.migratepages); | |
9075 | ||
041d3a8c MN |
9076 | /* |
9077 | * What we do here is we mark all pageblocks in range as | |
9078 | * MIGRATE_ISOLATE. Because pageblock and max order pages may | |
9079 | * have different sizes, and due to the way page allocator | |
b2c9e2fb | 9080 | * work, start_isolate_page_range() has special handlings for this. |
041d3a8c MN |
9081 | * |
9082 | * Once the pageblocks are marked as MIGRATE_ISOLATE, we | |
9083 | * migrate the pages from an unaligned range (ie. pages that | |
b2c9e2fb | 9084 | * we are interested in). This will put all the pages in |
041d3a8c MN |
9085 | * range back to page allocator as MIGRATE_ISOLATE. |
9086 | * | |
9087 | * When this is done, we take the pages in range from page | |
9088 | * allocator removing them from the buddy system. This way | |
9089 | * page allocator will never consider using them. | |
9090 | * | |
9091 | * This lets us mark the pageblocks back as | |
9092 | * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the | |
9093 | * aligned range but not in the unaligned, original range are | |
9094 | * put back to page allocator so that buddy can use them. | |
9095 | */ | |
9096 | ||
6e263fff | 9097 | ret = start_isolate_page_range(start, end, migratetype, 0, gfp_mask); |
3fa0c7c7 | 9098 | if (ret) |
b2c9e2fb | 9099 | goto done; |
041d3a8c | 9100 | |
7612921f VB |
9101 | drain_all_pages(cc.zone); |
9102 | ||
8ef5849f JK |
9103 | /* |
9104 | * In case of -EBUSY, we'd like to know which page causes problem. | |
63cd4489 MK |
9105 | * So, just fall through. test_pages_isolated() has a tracepoint |
9106 | * which will report the busy page. | |
9107 | * | |
9108 | * It is possible that busy pages could become available before | |
9109 | * the call to test_pages_isolated, and the range will actually be | |
9110 | * allocated. So, if we fall through be sure to clear ret so that | |
9111 | * -EBUSY is not accidentally used or returned to caller. | |
8ef5849f | 9112 | */ |
bb13ffeb | 9113 | ret = __alloc_contig_migrate_range(&cc, start, end); |
8ef5849f | 9114 | if (ret && ret != -EBUSY) |
041d3a8c | 9115 | goto done; |
68d68ff6 | 9116 | ret = 0; |
041d3a8c MN |
9117 | |
9118 | /* | |
b2c9e2fb | 9119 | * Pages from [start, end) are within a pageblock_nr_pages |
041d3a8c MN |
9120 | * aligned blocks that are marked as MIGRATE_ISOLATE. What's |
9121 | * more, all pages in [start, end) are free in page allocator. | |
9122 | * What we are going to do is to allocate all pages from | |
9123 | * [start, end) (that is remove them from page allocator). | |
9124 | * | |
9125 | * The only problem is that pages at the beginning and at the | |
9126 | * end of interesting range may be not aligned with pages that | |
9127 | * page allocator holds, ie. they can be part of higher order | |
9128 | * pages. Because of this, we reserve the bigger range and | |
9129 | * once this is done free the pages we are not interested in. | |
9130 | * | |
9131 | * We don't have to hold zone->lock here because the pages are | |
9132 | * isolated thus they won't get removed from buddy. | |
9133 | */ | |
9134 | ||
041d3a8c MN |
9135 | order = 0; |
9136 | outer_start = start; | |
9137 | while (!PageBuddy(pfn_to_page(outer_start))) { | |
9138 | if (++order >= MAX_ORDER) { | |
8ef5849f JK |
9139 | outer_start = start; |
9140 | break; | |
041d3a8c MN |
9141 | } |
9142 | outer_start &= ~0UL << order; | |
9143 | } | |
9144 | ||
8ef5849f | 9145 | if (outer_start != start) { |
ab130f91 | 9146 | order = buddy_order(pfn_to_page(outer_start)); |
8ef5849f JK |
9147 | |
9148 | /* | |
9149 | * outer_start page could be small order buddy page and | |
9150 | * it doesn't include start page. Adjust outer_start | |
9151 | * in this case to report failed page properly | |
9152 | * on tracepoint in test_pages_isolated() | |
9153 | */ | |
9154 | if (outer_start + (1UL << order) <= start) | |
9155 | outer_start = start; | |
9156 | } | |
9157 | ||
041d3a8c | 9158 | /* Make sure the range is really isolated. */ |
756d25be | 9159 | if (test_pages_isolated(outer_start, end, 0)) { |
041d3a8c MN |
9160 | ret = -EBUSY; |
9161 | goto done; | |
9162 | } | |
9163 | ||
49f223a9 | 9164 | /* Grab isolated pages from freelists. */ |
bb13ffeb | 9165 | outer_end = isolate_freepages_range(&cc, outer_start, end); |
041d3a8c MN |
9166 | if (!outer_end) { |
9167 | ret = -EBUSY; | |
9168 | goto done; | |
9169 | } | |
9170 | ||
9171 | /* Free head and tail (if any) */ | |
9172 | if (start != outer_start) | |
9173 | free_contig_range(outer_start, start - outer_start); | |
9174 | if (end != outer_end) | |
9175 | free_contig_range(end, outer_end - end); | |
9176 | ||
9177 | done: | |
6e263fff | 9178 | undo_isolate_page_range(start, end, migratetype); |
041d3a8c MN |
9179 | return ret; |
9180 | } | |
255f5985 | 9181 | EXPORT_SYMBOL(alloc_contig_range); |
5e27a2df AK |
9182 | |
9183 | static int __alloc_contig_pages(unsigned long start_pfn, | |
9184 | unsigned long nr_pages, gfp_t gfp_mask) | |
9185 | { | |
9186 | unsigned long end_pfn = start_pfn + nr_pages; | |
9187 | ||
9188 | return alloc_contig_range(start_pfn, end_pfn, MIGRATE_MOVABLE, | |
9189 | gfp_mask); | |
9190 | } | |
9191 | ||
9192 | static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn, | |
9193 | unsigned long nr_pages) | |
9194 | { | |
9195 | unsigned long i, end_pfn = start_pfn + nr_pages; | |
9196 | struct page *page; | |
9197 | ||
9198 | for (i = start_pfn; i < end_pfn; i++) { | |
9199 | page = pfn_to_online_page(i); | |
9200 | if (!page) | |
9201 | return false; | |
9202 | ||
9203 | if (page_zone(page) != z) | |
9204 | return false; | |
9205 | ||
9206 | if (PageReserved(page)) | |
9207 | return false; | |
5e27a2df AK |
9208 | } |
9209 | return true; | |
9210 | } | |
9211 | ||
9212 | static bool zone_spans_last_pfn(const struct zone *zone, | |
9213 | unsigned long start_pfn, unsigned long nr_pages) | |
9214 | { | |
9215 | unsigned long last_pfn = start_pfn + nr_pages - 1; | |
9216 | ||
9217 | return zone_spans_pfn(zone, last_pfn); | |
9218 | } | |
9219 | ||
9220 | /** | |
9221 | * alloc_contig_pages() -- tries to find and allocate contiguous range of pages | |
9222 | * @nr_pages: Number of contiguous pages to allocate | |
9223 | * @gfp_mask: GFP mask to limit search and used during compaction | |
9224 | * @nid: Target node | |
9225 | * @nodemask: Mask for other possible nodes | |
9226 | * | |
9227 | * This routine is a wrapper around alloc_contig_range(). It scans over zones | |
9228 | * on an applicable zonelist to find a contiguous pfn range which can then be | |
9229 | * tried for allocation with alloc_contig_range(). This routine is intended | |
9230 | * for allocation requests which can not be fulfilled with the buddy allocator. | |
9231 | * | |
9232 | * The allocated memory is always aligned to a page boundary. If nr_pages is a | |
eaab8e75 AK |
9233 | * power of two, then allocated range is also guaranteed to be aligned to same |
9234 | * nr_pages (e.g. 1GB request would be aligned to 1GB). | |
5e27a2df AK |
9235 | * |
9236 | * Allocated pages can be freed with free_contig_range() or by manually calling | |
9237 | * __free_page() on each allocated page. | |
9238 | * | |
9239 | * Return: pointer to contiguous pages on success, or NULL if not successful. | |
9240 | */ | |
9241 | struct page *alloc_contig_pages(unsigned long nr_pages, gfp_t gfp_mask, | |
9242 | int nid, nodemask_t *nodemask) | |
9243 | { | |
9244 | unsigned long ret, pfn, flags; | |
9245 | struct zonelist *zonelist; | |
9246 | struct zone *zone; | |
9247 | struct zoneref *z; | |
9248 | ||
9249 | zonelist = node_zonelist(nid, gfp_mask); | |
9250 | for_each_zone_zonelist_nodemask(zone, z, zonelist, | |
9251 | gfp_zone(gfp_mask), nodemask) { | |
9252 | spin_lock_irqsave(&zone->lock, flags); | |
9253 | ||
9254 | pfn = ALIGN(zone->zone_start_pfn, nr_pages); | |
9255 | while (zone_spans_last_pfn(zone, pfn, nr_pages)) { | |
9256 | if (pfn_range_valid_contig(zone, pfn, nr_pages)) { | |
9257 | /* | |
9258 | * We release the zone lock here because | |
9259 | * alloc_contig_range() will also lock the zone | |
9260 | * at some point. If there's an allocation | |
9261 | * spinning on this lock, it may win the race | |
9262 | * and cause alloc_contig_range() to fail... | |
9263 | */ | |
9264 | spin_unlock_irqrestore(&zone->lock, flags); | |
9265 | ret = __alloc_contig_pages(pfn, nr_pages, | |
9266 | gfp_mask); | |
9267 | if (!ret) | |
9268 | return pfn_to_page(pfn); | |
9269 | spin_lock_irqsave(&zone->lock, flags); | |
9270 | } | |
9271 | pfn += nr_pages; | |
9272 | } | |
9273 | spin_unlock_irqrestore(&zone->lock, flags); | |
9274 | } | |
9275 | return NULL; | |
9276 | } | |
4eb0716e | 9277 | #endif /* CONFIG_CONTIG_ALLOC */ |
041d3a8c | 9278 | |
78fa5150 | 9279 | void free_contig_range(unsigned long pfn, unsigned long nr_pages) |
041d3a8c | 9280 | { |
78fa5150 | 9281 | unsigned long count = 0; |
bcc2b02f MS |
9282 | |
9283 | for (; nr_pages--; pfn++) { | |
9284 | struct page *page = pfn_to_page(pfn); | |
9285 | ||
9286 | count += page_count(page) != 1; | |
9287 | __free_page(page); | |
9288 | } | |
78fa5150 | 9289 | WARN(count != 0, "%lu pages are still in use!\n", count); |
041d3a8c | 9290 | } |
255f5985 | 9291 | EXPORT_SYMBOL(free_contig_range); |
041d3a8c | 9292 | |
0a647f38 CS |
9293 | /* |
9294 | * The zone indicated has a new number of managed_pages; batch sizes and percpu | |
f0953a1b | 9295 | * page high values need to be recalculated. |
0a647f38 | 9296 | */ |
04f8cfea | 9297 | void zone_pcp_update(struct zone *zone, int cpu_online) |
4ed7e022 | 9298 | { |
c8e251fa | 9299 | mutex_lock(&pcp_batch_high_lock); |
04f8cfea | 9300 | zone_set_pageset_high_and_batch(zone, cpu_online); |
c8e251fa | 9301 | mutex_unlock(&pcp_batch_high_lock); |
4ed7e022 | 9302 | } |
4ed7e022 | 9303 | |
ec6e8c7e VB |
9304 | /* |
9305 | * Effectively disable pcplists for the zone by setting the high limit to 0 | |
9306 | * and draining all cpus. A concurrent page freeing on another CPU that's about | |
9307 | * to put the page on pcplist will either finish before the drain and the page | |
9308 | * will be drained, or observe the new high limit and skip the pcplist. | |
9309 | * | |
9310 | * Must be paired with a call to zone_pcp_enable(). | |
9311 | */ | |
9312 | void zone_pcp_disable(struct zone *zone) | |
9313 | { | |
9314 | mutex_lock(&pcp_batch_high_lock); | |
9315 | __zone_set_pageset_high_and_batch(zone, 0, 1); | |
9316 | __drain_all_pages(zone, true); | |
9317 | } | |
9318 | ||
9319 | void zone_pcp_enable(struct zone *zone) | |
9320 | { | |
9321 | __zone_set_pageset_high_and_batch(zone, zone->pageset_high, zone->pageset_batch); | |
9322 | mutex_unlock(&pcp_batch_high_lock); | |
9323 | } | |
9324 | ||
340175b7 JL |
9325 | void zone_pcp_reset(struct zone *zone) |
9326 | { | |
5a883813 | 9327 | int cpu; |
28f836b6 | 9328 | struct per_cpu_zonestat *pzstats; |
340175b7 | 9329 | |
28f836b6 | 9330 | if (zone->per_cpu_pageset != &boot_pageset) { |
5a883813 | 9331 | for_each_online_cpu(cpu) { |
28f836b6 MG |
9332 | pzstats = per_cpu_ptr(zone->per_cpu_zonestats, cpu); |
9333 | drain_zonestat(zone, pzstats); | |
5a883813 | 9334 | } |
28f836b6 MG |
9335 | free_percpu(zone->per_cpu_pageset); |
9336 | free_percpu(zone->per_cpu_zonestats); | |
9337 | zone->per_cpu_pageset = &boot_pageset; | |
9338 | zone->per_cpu_zonestats = &boot_zonestats; | |
340175b7 | 9339 | } |
340175b7 JL |
9340 | } |
9341 | ||
6dcd73d7 | 9342 | #ifdef CONFIG_MEMORY_HOTREMOVE |
0c0e6195 | 9343 | /* |
257bea71 DH |
9344 | * All pages in the range must be in a single zone, must not contain holes, |
9345 | * must span full sections, and must be isolated before calling this function. | |
0c0e6195 | 9346 | */ |
257bea71 | 9347 | void __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn) |
0c0e6195 | 9348 | { |
257bea71 | 9349 | unsigned long pfn = start_pfn; |
0c0e6195 KH |
9350 | struct page *page; |
9351 | struct zone *zone; | |
0ee5f4f3 | 9352 | unsigned int order; |
0c0e6195 | 9353 | unsigned long flags; |
5557c766 | 9354 | |
2d070eab | 9355 | offline_mem_sections(pfn, end_pfn); |
0c0e6195 KH |
9356 | zone = page_zone(pfn_to_page(pfn)); |
9357 | spin_lock_irqsave(&zone->lock, flags); | |
0c0e6195 | 9358 | while (pfn < end_pfn) { |
0c0e6195 | 9359 | page = pfn_to_page(pfn); |
b023f468 WC |
9360 | /* |
9361 | * The HWPoisoned page may be not in buddy system, and | |
9362 | * page_count() is not 0. | |
9363 | */ | |
9364 | if (unlikely(!PageBuddy(page) && PageHWPoison(page))) { | |
9365 | pfn++; | |
b023f468 WC |
9366 | continue; |
9367 | } | |
aa218795 DH |
9368 | /* |
9369 | * At this point all remaining PageOffline() pages have a | |
9370 | * reference count of 0 and can simply be skipped. | |
9371 | */ | |
9372 | if (PageOffline(page)) { | |
9373 | BUG_ON(page_count(page)); | |
9374 | BUG_ON(PageBuddy(page)); | |
9375 | pfn++; | |
aa218795 DH |
9376 | continue; |
9377 | } | |
b023f468 | 9378 | |
0c0e6195 KH |
9379 | BUG_ON(page_count(page)); |
9380 | BUG_ON(!PageBuddy(page)); | |
ab130f91 | 9381 | order = buddy_order(page); |
6ab01363 | 9382 | del_page_from_free_list(page, zone, order); |
0c0e6195 KH |
9383 | pfn += (1 << order); |
9384 | } | |
9385 | spin_unlock_irqrestore(&zone->lock, flags); | |
9386 | } | |
9387 | #endif | |
8d22ba1b | 9388 | |
8446b59b ED |
9389 | /* |
9390 | * This function returns a stable result only if called under zone lock. | |
9391 | */ | |
8d22ba1b WF |
9392 | bool is_free_buddy_page(struct page *page) |
9393 | { | |
8d22ba1b | 9394 | unsigned long pfn = page_to_pfn(page); |
7aeb09f9 | 9395 | unsigned int order; |
8d22ba1b | 9396 | |
8d22ba1b WF |
9397 | for (order = 0; order < MAX_ORDER; order++) { |
9398 | struct page *page_head = page - (pfn & ((1 << order) - 1)); | |
9399 | ||
8446b59b ED |
9400 | if (PageBuddy(page_head) && |
9401 | buddy_order_unsafe(page_head) >= order) | |
8d22ba1b WF |
9402 | break; |
9403 | } | |
8d22ba1b WF |
9404 | |
9405 | return order < MAX_ORDER; | |
9406 | } | |
a581865e | 9407 | EXPORT_SYMBOL(is_free_buddy_page); |
d4ae9916 NH |
9408 | |
9409 | #ifdef CONFIG_MEMORY_FAILURE | |
9410 | /* | |
06be6ff3 OS |
9411 | * Break down a higher-order page in sub-pages, and keep our target out of |
9412 | * buddy allocator. | |
d4ae9916 | 9413 | */ |
06be6ff3 OS |
9414 | static void break_down_buddy_pages(struct zone *zone, struct page *page, |
9415 | struct page *target, int low, int high, | |
9416 | int migratetype) | |
9417 | { | |
9418 | unsigned long size = 1 << high; | |
9419 | struct page *current_buddy, *next_page; | |
9420 | ||
9421 | while (high > low) { | |
9422 | high--; | |
9423 | size >>= 1; | |
9424 | ||
9425 | if (target >= &page[size]) { | |
9426 | next_page = page + size; | |
9427 | current_buddy = page; | |
9428 | } else { | |
9429 | next_page = page; | |
9430 | current_buddy = page + size; | |
9431 | } | |
9432 | ||
9433 | if (set_page_guard(zone, current_buddy, high, migratetype)) | |
9434 | continue; | |
9435 | ||
9436 | if (current_buddy != target) { | |
9437 | add_to_free_list(current_buddy, zone, high, migratetype); | |
ab130f91 | 9438 | set_buddy_order(current_buddy, high); |
06be6ff3 OS |
9439 | page = next_page; |
9440 | } | |
9441 | } | |
9442 | } | |
9443 | ||
9444 | /* | |
9445 | * Take a page that will be marked as poisoned off the buddy allocator. | |
9446 | */ | |
9447 | bool take_page_off_buddy(struct page *page) | |
d4ae9916 NH |
9448 | { |
9449 | struct zone *zone = page_zone(page); | |
9450 | unsigned long pfn = page_to_pfn(page); | |
9451 | unsigned long flags; | |
9452 | unsigned int order; | |
06be6ff3 | 9453 | bool ret = false; |
d4ae9916 NH |
9454 | |
9455 | spin_lock_irqsave(&zone->lock, flags); | |
9456 | for (order = 0; order < MAX_ORDER; order++) { | |
9457 | struct page *page_head = page - (pfn & ((1 << order) - 1)); | |
ab130f91 | 9458 | int page_order = buddy_order(page_head); |
d4ae9916 | 9459 | |
ab130f91 | 9460 | if (PageBuddy(page_head) && page_order >= order) { |
06be6ff3 OS |
9461 | unsigned long pfn_head = page_to_pfn(page_head); |
9462 | int migratetype = get_pfnblock_migratetype(page_head, | |
9463 | pfn_head); | |
9464 | ||
ab130f91 | 9465 | del_page_from_free_list(page_head, zone, page_order); |
06be6ff3 | 9466 | break_down_buddy_pages(zone, page_head, page, 0, |
ab130f91 | 9467 | page_order, migratetype); |
bf181c58 | 9468 | SetPageHWPoisonTakenOff(page); |
bac9c6fa DH |
9469 | if (!is_migrate_isolate(migratetype)) |
9470 | __mod_zone_freepage_state(zone, -1, migratetype); | |
06be6ff3 | 9471 | ret = true; |
d4ae9916 NH |
9472 | break; |
9473 | } | |
06be6ff3 OS |
9474 | if (page_count(page_head) > 0) |
9475 | break; | |
d4ae9916 NH |
9476 | } |
9477 | spin_unlock_irqrestore(&zone->lock, flags); | |
06be6ff3 | 9478 | return ret; |
d4ae9916 | 9479 | } |
bf181c58 NH |
9480 | |
9481 | /* | |
9482 | * Cancel takeoff done by take_page_off_buddy(). | |
9483 | */ | |
9484 | bool put_page_back_buddy(struct page *page) | |
9485 | { | |
9486 | struct zone *zone = page_zone(page); | |
9487 | unsigned long pfn = page_to_pfn(page); | |
9488 | unsigned long flags; | |
9489 | int migratetype = get_pfnblock_migratetype(page, pfn); | |
9490 | bool ret = false; | |
9491 | ||
9492 | spin_lock_irqsave(&zone->lock, flags); | |
9493 | if (put_page_testzero(page)) { | |
9494 | ClearPageHWPoisonTakenOff(page); | |
9495 | __free_one_page(page, pfn, zone, 0, migratetype, FPI_NONE); | |
9496 | if (TestClearPageHWPoison(page)) { | |
bf181c58 NH |
9497 | ret = true; |
9498 | } | |
9499 | } | |
9500 | spin_unlock_irqrestore(&zone->lock, flags); | |
9501 | ||
9502 | return ret; | |
9503 | } | |
d4ae9916 | 9504 | #endif |
62b31070 BH |
9505 | |
9506 | #ifdef CONFIG_ZONE_DMA | |
9507 | bool has_managed_dma(void) | |
9508 | { | |
9509 | struct pglist_data *pgdat; | |
9510 | ||
9511 | for_each_online_pgdat(pgdat) { | |
9512 | struct zone *zone = &pgdat->node_zones[ZONE_DMA]; | |
9513 | ||
9514 | if (managed_zone(zone)) | |
9515 | return true; | |
9516 | } | |
9517 | return false; | |
9518 | } | |
9519 | #endif /* CONFIG_ZONE_DMA */ |