Branch data Line data Source code
1 : : /*
2 : : * linux/mm/page_alloc.c
3 : : *
4 : : * Manages the free list, the system allocates free pages here.
5 : : * Note that kmalloc() lives in slab.c
6 : : *
7 : : * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
8 : : * Swap reorganised 29.12.95, Stephen Tweedie
9 : : * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
10 : : * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999
11 : : * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999
12 : : * Zone balancing, Kanoj Sarcar, SGI, Jan 2000
13 : : * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002
14 : : * (lots of bits borrowed from Ingo Molnar & Andrew Morton)
15 : : */
16 : :
17 : : #include <linux/stddef.h>
18 : : #include <linux/mm.h>
19 : : #include <linux/swap.h>
20 : : #include <linux/interrupt.h>
21 : : #include <linux/pagemap.h>
22 : : #include <linux/jiffies.h>
23 : : #include <linux/bootmem.h>
24 : : #include <linux/memblock.h>
25 : : #include <linux/compiler.h>
26 : : #include <linux/kernel.h>
27 : : #include <linux/kmemcheck.h>
28 : : #include <linux/module.h>
29 : : #include <linux/suspend.h>
30 : : #include <linux/pagevec.h>
31 : : #include <linux/blkdev.h>
32 : : #include <linux/slab.h>
33 : : #include <linux/ratelimit.h>
34 : : #include <linux/oom.h>
35 : : #include <linux/notifier.h>
36 : : #include <linux/topology.h>
37 : : #include <linux/sysctl.h>
38 : : #include <linux/cpu.h>
39 : : #include <linux/cpuset.h>
40 : : #include <linux/memory_hotplug.h>
41 : : #include <linux/nodemask.h>
42 : : #include <linux/vmalloc.h>
43 : : #include <linux/vmstat.h>
44 : : #include <linux/mempolicy.h>
45 : : #include <linux/stop_machine.h>
46 : : #include <linux/sort.h>
47 : : #include <linux/pfn.h>
48 : : #include <linux/backing-dev.h>
49 : : #include <linux/fault-inject.h>
50 : : #include <linux/page-isolation.h>
51 : : #include <linux/page_cgroup.h>
52 : : #include <linux/debugobjects.h>
53 : : #include <linux/kmemleak.h>
54 : : #include <linux/compaction.h>
55 : : #include <trace/events/kmem.h>
56 : : #include <linux/ftrace_event.h>
57 : : #include <linux/memcontrol.h>
58 : : #include <linux/prefetch.h>
59 : : #include <linux/mm_inline.h>
60 : : #include <linux/migrate.h>
61 : : #include <linux/page-debug-flags.h>
62 : : #include <linux/hugetlb.h>
63 : : #include <linux/sched/rt.h>
64 : :
65 : : #include <asm/sections.h>
66 : : #include <asm/tlbflush.h>
67 : : #include <asm/div64.h>
68 : : #include "internal.h"
69 : :
70 : : /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
71 : : static DEFINE_MUTEX(pcp_batch_high_lock);
72 : :
73 : : #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
74 : : DEFINE_PER_CPU(int, numa_node);
75 : : EXPORT_PER_CPU_SYMBOL(numa_node);
76 : : #endif
77 : :
78 : : #ifdef CONFIG_HAVE_MEMORYLESS_NODES
79 : : /*
80 : : * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
81 : : * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
82 : : * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
83 : : * defined in <linux/topology.h>.
84 : : */
85 : : DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
86 : : EXPORT_PER_CPU_SYMBOL(_numa_mem_);
87 : : #endif
88 : :
89 : : /*
90 : : * Array of node states.
91 : : */
92 : : nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
93 : : [N_POSSIBLE] = NODE_MASK_ALL,
94 : : [N_ONLINE] = { { [0] = 1UL } },
95 : : #ifndef CONFIG_NUMA
96 : : [N_NORMAL_MEMORY] = { { [0] = 1UL } },
97 : : #ifdef CONFIG_HIGHMEM
98 : : [N_HIGH_MEMORY] = { { [0] = 1UL } },
99 : : #endif
100 : : #ifdef CONFIG_MOVABLE_NODE
101 : : [N_MEMORY] = { { [0] = 1UL } },
102 : : #endif
103 : : [N_CPU] = { { [0] = 1UL } },
104 : : #endif /* NUMA */
105 : : };
106 : : EXPORT_SYMBOL(node_states);
107 : :
108 : : /* Protect totalram_pages and zone->managed_pages */
109 : : static DEFINE_SPINLOCK(managed_page_count_lock);
110 : :
111 : : unsigned long totalram_pages __read_mostly;
112 : : unsigned long totalreserve_pages __read_mostly;
113 : : /*
114 : : * When calculating the number of globally allowed dirty pages, there
115 : : * is a certain number of per-zone reserves that should not be
116 : : * considered dirtyable memory. This is the sum of those reserves
117 : : * over all existing zones that contribute dirtyable memory.
118 : : */
119 : : unsigned long dirty_balance_reserve __read_mostly;
120 : :
121 : : int percpu_pagelist_fraction;
122 : : gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
123 : :
124 : : #ifdef CONFIG_PM_SLEEP
125 : : /*
126 : : * The following functions are used by the suspend/hibernate code to temporarily
127 : : * change gfp_allowed_mask in order to avoid using I/O during memory allocations
128 : : * while devices are suspended. To avoid races with the suspend/hibernate code,
129 : : * they should always be called with pm_mutex held (gfp_allowed_mask also should
130 : : * only be modified with pm_mutex held, unless the suspend/hibernate code is
131 : : * guaranteed not to run in parallel with that modification).
132 : : */
133 : :
134 : : static gfp_t saved_gfp_mask;
135 : :
136 : 0 : void pm_restore_gfp_mask(void)
137 : : {
138 [ # # ]: 0 : WARN_ON(!mutex_is_locked(&pm_mutex));
139 [ # # ]: 0 : if (saved_gfp_mask) {
140 : 0 : gfp_allowed_mask = saved_gfp_mask;
141 : 0 : saved_gfp_mask = 0;
142 : : }
143 : 0 : }
144 : :
145 : 0 : void pm_restrict_gfp_mask(void)
146 : : {
147 [ # # ]: 0 : WARN_ON(!mutex_is_locked(&pm_mutex));
148 [ # # ]: 0 : WARN_ON(saved_gfp_mask);
149 : 0 : saved_gfp_mask = gfp_allowed_mask;
150 : 0 : gfp_allowed_mask &= ~GFP_IOFS;
151 : 0 : }
152 : :
153 : 0 : bool pm_suspended_storage(void)
154 : : {
155 [ # # ]: 0 : if ((gfp_allowed_mask & GFP_IOFS) == GFP_IOFS)
156 : : return false;
157 : 0 : return true;
158 : : }
159 : : #endif /* CONFIG_PM_SLEEP */
160 : :
161 : : #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
162 : : int pageblock_order __read_mostly;
163 : : #endif
164 : :
165 : : static void __free_pages_ok(struct page *page, unsigned int order);
166 : :
167 : : /*
168 : : * results with 256, 32 in the lowmem_reserve sysctl:
169 : : * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
170 : : * 1G machine -> (16M dma, 784M normal, 224M high)
171 : : * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
172 : : * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
173 : : * HIGHMEM allocation will (224M+784M)/256 of ram reserved in ZONE_DMA
174 : : *
175 : : * TBD: should special case ZONE_DMA32 machines here - in those we normally
176 : : * don't need any ZONE_NORMAL reservation
177 : : */
178 : : int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
179 : : #ifdef CONFIG_ZONE_DMA
180 : : 256,
181 : : #endif
182 : : #ifdef CONFIG_ZONE_DMA32
183 : : 256,
184 : : #endif
185 : : #ifdef CONFIG_HIGHMEM
186 : : 32,
187 : : #endif
188 : : 32,
189 : : };
190 : :
191 : : EXPORT_SYMBOL(totalram_pages);
192 : :
193 : : static char * const zone_names[MAX_NR_ZONES] = {
194 : : #ifdef CONFIG_ZONE_DMA
195 : : "DMA",
196 : : #endif
197 : : #ifdef CONFIG_ZONE_DMA32
198 : : "DMA32",
199 : : #endif
200 : : "Normal",
201 : : #ifdef CONFIG_HIGHMEM
202 : : "HighMem",
203 : : #endif
204 : : "Movable",
205 : : };
206 : :
207 : : /*
208 : : * Try to keep at least this much lowmem free. Do not allow normal
209 : : * allocations below this point, only high priority ones. Automatically
210 : : * tuned according to the amount of memory in the system.
211 : : */
212 : : int min_free_kbytes = 1024;
213 : : int user_min_free_kbytes = -1;
214 : : int min_free_order_shift = 1;
215 : :
216 : : /*
217 : : * Extra memory for the system to try freeing. Used to temporarily
218 : : * free memory, to make space for new workloads. Anyone can allocate
219 : : * down to the min watermarks controlled by min_free_kbytes above.
220 : : */
221 : : int extra_free_kbytes = 0;
222 : :
223 : : static unsigned long __meminitdata nr_kernel_pages;
224 : : static unsigned long __meminitdata nr_all_pages;
225 : : static unsigned long __meminitdata dma_reserve;
226 : :
227 : : #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
228 : : static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
229 : : static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
230 : : static unsigned long __initdata required_kernelcore;
231 : : static unsigned long __initdata required_movablecore;
232 : : static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
233 : :
234 : : /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
235 : : int movable_zone;
236 : : EXPORT_SYMBOL(movable_zone);
237 : : #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
238 : :
239 : : #if MAX_NUMNODES > 1
240 : : int nr_node_ids __read_mostly = MAX_NUMNODES;
241 : : int nr_online_nodes __read_mostly = 1;
242 : : EXPORT_SYMBOL(nr_node_ids);
243 : : EXPORT_SYMBOL(nr_online_nodes);
244 : : #endif
245 : :
246 : : int page_group_by_mobility_disabled __read_mostly;
247 : :
248 : 0 : void set_pageblock_migratetype(struct page *page, int migratetype)
249 : : {
250 [ - + ][ # # ]: 48 : if (unlikely(page_group_by_mobility_disabled &&
251 : : migratetype < MIGRATE_PCPTYPES))
252 : : migratetype = MIGRATE_UNMOVABLE;
253 : :
254 : 48 : set_pageblock_flags_group(page, (unsigned long)migratetype,
255 : : PB_migrate, PB_migrate_end);
256 : 48 : }
257 : :
258 : : bool oom_killer_disabled __read_mostly;
259 : :
260 : : #ifdef CONFIG_DEBUG_VM
261 : : static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
262 : : {
263 : : int ret = 0;
264 : : unsigned seq;
265 : : unsigned long pfn = page_to_pfn(page);
266 : : unsigned long sp, start_pfn;
267 : :
268 : : do {
269 : : seq = zone_span_seqbegin(zone);
270 : : start_pfn = zone->zone_start_pfn;
271 : : sp = zone->spanned_pages;
272 : : if (!zone_spans_pfn(zone, pfn))
273 : : ret = 1;
274 : : } while (zone_span_seqretry(zone, seq));
275 : :
276 : : if (ret)
277 : : pr_err("page %lu outside zone [ %lu - %lu ]\n",
278 : : pfn, start_pfn, start_pfn + sp);
279 : :
280 : : return ret;
281 : : }
282 : :
283 : : static int page_is_consistent(struct zone *zone, struct page *page)
284 : : {
285 : : if (!pfn_valid_within(page_to_pfn(page)))
286 : : return 0;
287 : : if (zone != page_zone(page))
288 : : return 0;
289 : :
290 : : return 1;
291 : : }
292 : : /*
293 : : * Temporary debugging check for pages not lying within a given zone.
294 : : */
295 : : static int bad_range(struct zone *zone, struct page *page)
296 : : {
297 : : if (page_outside_zone_boundaries(zone, page))
298 : : return 1;
299 : : if (!page_is_consistent(zone, page))
300 : : return 1;
301 : :
302 : : return 0;
303 : : }
304 : : #else
305 : : static inline int bad_range(struct zone *zone, struct page *page)
306 : : {
307 : : return 0;
308 : : }
309 : : #endif
310 : :
311 : 0 : static void bad_page(struct page *page, char *reason, unsigned long bad_flags)
312 : : {
313 : : static unsigned long resume;
314 : : static unsigned long nr_shown;
315 : : static unsigned long nr_unshown;
316 : :
317 : : /* Don't complain about poisoned pages */
318 : : if (PageHWPoison(page)) {
319 : : page_mapcount_reset(page); /* remove PageBuddy */
320 : 0 : return;
321 : : }
322 : :
323 : : /*
324 : : * Allow a burst of 60 reports, then keep quiet for that minute;
325 : : * or allow a steady drip of one report per second.
326 : : */
327 [ # # ]: 0 : if (nr_shown == 60) {
328 [ # # ]: 0 : if (time_before(jiffies, resume)) {
329 : 0 : nr_unshown++;
330 : 0 : goto out;
331 : : }
332 [ # # ]: 0 : if (nr_unshown) {
333 : 0 : printk(KERN_ALERT
334 : : "BUG: Bad page state: %lu messages suppressed\n",
335 : : nr_unshown);
336 : 0 : nr_unshown = 0;
337 : : }
338 : 0 : nr_shown = 0;
339 : : }
340 [ # # ]: 0 : if (nr_shown++ == 0)
341 : 0 : resume = jiffies + 60 * HZ;
342 : :
343 : 0 : printk(KERN_ALERT "BUG: Bad page state in process %s pfn:%05lx\n",
344 : 0 : current->comm, page_to_pfn(page));
345 : 0 : dump_page_badflags(page, reason, bad_flags);
346 : :
347 : 0 : print_modules();
348 : 0 : dump_stack();
349 : : out:
350 : : /* Leave bad fields for debug, except PageBuddy could make trouble */
351 : : page_mapcount_reset(page); /* remove PageBuddy */
352 : 0 : add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
353 : : }
354 : :
355 : : /*
356 : : * Higher-order pages are called "compound pages". They are structured thusly:
357 : : *
358 : : * The first PAGE_SIZE page is called the "head page".
359 : : *
360 : : * The remaining PAGE_SIZE pages are called "tail pages".
361 : : *
362 : : * All pages have PG_compound set. All tail pages have their ->first_page
363 : : * pointing at the head page.
364 : : *
365 : : * The first tail page's ->lru.next holds the address of the compound page's
366 : : * put_page() function. Its ->lru.prev holds the order of allocation.
367 : : * This usage means that zero-order pages may not be compound.
368 : : */
369 : :
370 : 0 : static void free_compound_page(struct page *page)
371 : : {
372 : 13 : __free_pages_ok(page, compound_order(page));
373 : 13 : }
374 : :
375 : 0 : void prep_compound_page(struct page *page, unsigned long order)
376 : : {
377 : : int i;
378 : 1270 : int nr_pages = 1 << order;
379 : :
380 : : set_compound_page_dtor(page, free_compound_page);
381 : : set_compound_order(page, order);
382 : : __SetPageHead(page);
383 [ + + ]: 2738 : for (i = 1; i < nr_pages; i++) {
384 : 1468 : struct page *p = page + i;
385 : : set_page_count(p, 0);
386 : 1468 : p->first_page = page;
387 : : /* Make sure p->first_page is always valid for PageTail() */
388 : 1468 : smp_wmb();
389 : : __SetPageTail(p);
390 : : }
391 : 1270 : }
392 : :
393 : : /* update __split_huge_page_refcount if you change this function */
394 : 0 : static int destroy_compound_page(struct page *page, unsigned long order)
395 : : {
396 : : int i;
397 : 0 : int nr_pages = 1 << order;
398 : : int bad = 0;
399 : :
400 [ # # ]: 0 : if (unlikely(compound_order(page) != order)) {
401 : 0 : bad_page(page, "wrong compound order", 0);
402 : : bad++;
403 : : }
404 : :
405 : : __ClearPageHead(page);
406 : :
407 [ # # ]: 0 : for (i = 1; i < nr_pages; i++) {
408 : 0 : struct page *p = page + i;
409 : :
410 [ # # ]: 0 : if (unlikely(!PageTail(p))) {
411 : 0 : bad_page(page, "PageTail not set", 0);
412 : 0 : bad++;
413 [ # # ]: 0 : } else if (unlikely(p->first_page != page)) {
414 : 0 : bad_page(page, "first_page not consistent", 0);
415 : 0 : bad++;
416 : : }
417 : : __ClearPageTail(p);
418 : : }
419 : :
420 : 0 : return bad;
421 : : }
422 : :
423 : : static inline void prep_zero_page(struct page *page, int order, gfp_t gfp_flags)
424 : : {
425 : : int i;
426 : :
427 : : /*
428 : : * clear_highpage() will use KM_USER0, so it's a bug to use __GFP_ZERO
429 : : * and __GFP_HIGHMEM from hard or soft interrupt context.
430 : : */
431 : : VM_BUG_ON((gfp_flags & __GFP_HIGHMEM) && in_interrupt());
432 [ + + ]: 7242812 : for (i = 0; i < (1 << order); i++)
433 : 3621415 : clear_highpage(page + i);
434 : : }
435 : :
436 : : #ifdef CONFIG_DEBUG_PAGEALLOC
437 : : unsigned int _debug_guardpage_minorder;
438 : :
439 : : static int __init debug_guardpage_minorder_setup(char *buf)
440 : : {
441 : : unsigned long res;
442 : :
443 : : if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
444 : : printk(KERN_ERR "Bad debug_guardpage_minorder value\n");
445 : : return 0;
446 : : }
447 : : _debug_guardpage_minorder = res;
448 : : printk(KERN_INFO "Setting debug_guardpage_minorder to %lu\n", res);
449 : : return 0;
450 : : }
451 : : __setup("debug_guardpage_minorder=", debug_guardpage_minorder_setup);
452 : :
453 : : static inline void set_page_guard_flag(struct page *page)
454 : : {
455 : : __set_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
456 : : }
457 : :
458 : : static inline void clear_page_guard_flag(struct page *page)
459 : : {
460 : : __clear_bit(PAGE_DEBUG_FLAG_GUARD, &page->debug_flags);
461 : : }
462 : : #else
463 : : static inline void set_page_guard_flag(struct page *page) { }
464 : : static inline void clear_page_guard_flag(struct page *page) { }
465 : : #endif
466 : :
467 : : static inline void set_page_order(struct page *page, int order)
468 : : {
469 : 28018229 : set_page_private(page, order);
470 : : __SetPageBuddy(page);
471 : : }
472 : :
473 : : static inline void rmv_page_order(struct page *page)
474 : : {
475 : : __ClearPageBuddy(page);
476 : 28016198 : set_page_private(page, 0);
477 : : }
478 : :
479 : : /*
480 : : * Locate the struct page for both the matching buddy in our
481 : : * pair (buddy1) and the combined O(n+1) page they form (page).
482 : : *
483 : : * 1) Any buddy B1 will have an order O twin B2 which satisfies
484 : : * the following equation:
485 : : * B2 = B1 ^ (1 << O)
486 : : * For example, if the starting buddy (buddy2) is #8 its order
487 : : * 1 buddy is #10:
488 : : * B2 = 8 ^ (1 << 1) = 8 ^ 2 = 10
489 : : *
490 : : * 2) Any buddy B will have an order O+1 parent P which
491 : : * satisfies the following equation:
492 : : * P = B & ~(1 << O)
493 : : *
494 : : * Assumption: *_mem_map is contiguous at least up to MAX_ORDER
495 : : */
496 : : static inline unsigned long
497 : : __find_buddy_index(unsigned long page_idx, unsigned int order)
498 : : {
499 : 47094382 : return page_idx ^ (1 << order);
500 : : }
501 : :
502 : : /*
503 : : * This function checks whether a page is free && is the buddy
504 : : * we can do coalesce a page and its buddy if
505 : : * (a) the buddy is not in a hole &&
506 : : * (b) the buddy is in the buddy system &&
507 : : * (c) a page and its buddy have the same order &&
508 : : * (d) a page and its buddy are in the same zone.
509 : : *
510 : : * For recording whether a page is in the buddy system, we set ->_mapcount
511 : : * PAGE_BUDDY_MAPCOUNT_VALUE.
512 : : * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is
513 : : * serialized by zone->lock.
514 : : *
515 : : * For recording page's order, we use page_private(page).
516 : : */
517 : 109630451 : static inline int page_is_buddy(struct page *page, struct page *buddy,
518 : : int order)
519 : : {
520 : : if (!pfn_valid_within(page_to_pfn(buddy)))
521 : : return 0;
522 : :
523 [ + - ][ + - ]: 47094382 : if (page_zone_id(page) != page_zone_id(buddy))
[ + + ][ + + ]
524 : : return 0;
525 : :
526 : : if (page_is_guard(buddy) && page_order(buddy) == order) {
527 : : VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
528 : : return 1;
529 : : }
530 : :
531 [ + + ][ + + ]: 47092548 : if (PageBuddy(buddy) && page_order(buddy) == order) {
[ + + ][ + + ]
[ + + ][ + + ]
[ + + ][ + + ]
532 : : VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
533 : : return 1;
534 : : }
535 : : return 0;
536 : : }
537 : :
538 : : /*
539 : : * Freeing function for a buddy system allocator.
540 : : *
541 : : * The concept of a buddy system is to maintain direct-mapped table
542 : : * (containing bit values) for memory blocks of various "orders".
543 : : * The bottom level table contains the map for the smallest allocatable
544 : : * units of memory (here, pages), and each level above it describes
545 : : * pairs of units from the levels below, hence, "buddies".
546 : : * At a high level, all that happens here is marking the table entry
547 : : * at the bottom level available, and propagating the changes upward
548 : : * as necessary, plus some accounting needed to play nicely with other
549 : : * parts of the VM system.
550 : : * At each level, we keep a list of pages, which are heads of continuous
551 : : * free pages of length of (1 << order) and marked with _mapcount
552 : : * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
553 : : * field.
554 : : * So when we are allocating or freeing one, we can derive the state of the
555 : : * other. That is, if we allocate a small block, and both were
556 : : * free, the remainder of the region must be split into blocks.
557 : : * If a block is freed, and its buddy is also free, then this
558 : : * triggers coalescing into a block of larger size.
559 : : *
560 : : * -- nyc
561 : : */
562 : :
563 : 18596572 : static inline void __free_one_page(struct page *page,
564 : : struct zone *zone, unsigned int order,
565 : : int migratetype)
566 : : {
567 : : unsigned long page_idx;
568 : : unsigned long combined_idx;
569 : : unsigned long uninitialized_var(buddy_idx);
570 : : struct page *buddy;
571 : :
572 : : VM_BUG_ON(!zone_is_initialized(zone));
573 : :
574 [ - + ][ - + ]: 19141759 : if (unlikely(PageCompound(page)))
575 [ # # ][ # # ]: 0 : if (unlikely(destroy_compound_page(page, order)))
576 : : return;
577 : :
578 : : VM_BUG_ON(migratetype == -1);
579 : :
580 : 21374917 : page_idx = page_to_pfn(page) & ((1 << MAX_ORDER) - 1);
581 : :
582 : : VM_BUG_ON_PAGE(page_idx & ((1 << order) - 1), page);
583 : : VM_BUG_ON_PAGE(bad_range(zone, page), page);
584 : :
585 [ + + ][ + + ]: 30208822 : while (order < MAX_ORDER-1) {
586 : : buddy_idx = __find_buddy_index(page_idx, order);
587 : 27962132 : buddy = page + (buddy_idx - page_idx);
588 [ + + ][ + + ]: 27962132 : if (!page_is_buddy(page, buddy, order))
589 : : break;
590 : : /*
591 : : * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
592 : : * merge with it and move up one order.
593 : : */
594 : : if (page_is_guard(buddy)) {
595 : : clear_page_guard_flag(buddy);
596 : : set_page_private(page, 0);
597 : : __mod_zone_freepage_state(zone, 1 << order,
598 : : migratetype);
599 : : } else {
600 : : list_del(&buddy->lru);
601 : 8833905 : zone->free_area[order].nr_free--;
602 : : rmv_page_order(buddy);
603 : : }
604 : 8833905 : combined_idx = buddy_idx & page_idx;
605 : 8833905 : page = page + (combined_idx - page_idx);
606 : : page_idx = combined_idx;
607 : 8833905 : order++;
608 : : }
609 : : set_page_order(page, order);
610 : :
611 : : /*
612 : : * If this is not the largest possible page, check if the buddy
613 : : * of the next-highest order is free. If it is, it's possible
614 : : * that pages are being freed that will coalesce soon. In case,
615 : : * that is happening, add the free page to the tail of the list
616 : : * so it's less likely to be used soon and more likely to be merged
617 : : * as a higher order page
618 : : */
619 [ + + ][ + + ]: 19141636 : if ((order < MAX_ORDER-2) && pfn_valid_within(page_to_pfn(buddy))) {
620 : : struct page *higher_page, *higher_buddy;
621 : 19132250 : combined_idx = buddy_idx & page_idx;
622 : 19132250 : higher_page = page + (combined_idx - page_idx);
623 : 19132250 : buddy_idx = __find_buddy_index(combined_idx, order + 1);
624 : 19132250 : higher_buddy = higher_page + (buddy_idx - combined_idx);
625 [ + + ][ + + ]: 19132250 : if (page_is_buddy(higher_page, higher_buddy, order + 1)) {
626 : 3515882 : list_add_tail(&page->lru,
627 : : &zone->free_area[order].free_list[migratetype]);
628 : : goto out;
629 : : }
630 : : }
631 : :
632 : 15625754 : list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
633 : : out:
634 : 19141636 : zone->free_area[order].nr_free++;
635 : : }
636 : :
637 : : static inline int free_pages_check(struct page *page)
638 : : {
639 : : char *bad_reason = NULL;
640 : : unsigned long bad_flags = 0;
641 : :
642 [ - + ]: 33899935 : if (unlikely(page_mapcount(page)))
643 : : bad_reason = "nonzero mapcount";
644 [ - + ]: 33899935 : if (unlikely(page->mapping != NULL))
645 : : bad_reason = "non-NULL mapping";
646 [ - + ]: 33899935 : if (unlikely(atomic_read(&page->_count) != 0))
647 : : bad_reason = "nonzero _count";
648 [ - + ]: 33899935 : if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
649 : : bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
650 : : bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
651 : : }
652 : : if (unlikely(mem_cgroup_bad_page_check(page)))
653 : : bad_reason = "cgroup check failed";
654 [ + ]: 33899935 : if (unlikely(bad_reason)) {
655 : 0 : bad_page(page, bad_reason, bad_flags);
656 : : return 1;
657 : : }
658 : : page_cpupid_reset_last(page);
659 [ + + ]: 33901597 : if (page->flags & PAGE_FLAGS_CHECK_AT_PREP)
660 : 20408169 : page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
661 : : return 0;
662 : : }
663 : :
664 : : /*
665 : : * Frees a number of pages from the PCP lists
666 : : * Assumes all pages on list are in same zone, and of same order.
667 : : * count is the number of pages to free.
668 : : *
669 : : * If the zone was previously in an "all pages pinned" state then look to
670 : : * see if this freeing clears that state.
671 : : *
672 : : * And clear the zone's pages_scanned counter, to hold off the "all pages are
673 : : * pinned" detection logic.
674 : : */
675 : 0 : static void free_pcppages_bulk(struct zone *zone, int count,
676 : : struct per_cpu_pages *pcp)
677 : : {
678 : : int migratetype = 0;
679 : : int batch_free = 0;
680 : : int to_free = count;
681 : :
682 : : spin_lock(&zone->lock);
683 : 545192 : zone->pages_scanned = 0;
684 : :
685 [ + + ]: 5789998 : while (to_free) {
686 : 16362554 : struct page *page;
687 : : struct list_head *list;
688 : :
689 : : /*
690 : : * Remove pages from lists in a round-robin fashion. A
691 : : * batch_free count is maintained that is incremented when an
692 : : * empty list is encountered. This is so more pages are freed
693 : : * off fuller lists instead of spinning excessively around empty
694 : : * lists
695 : : */
696 : : do {
697 : 8630380 : batch_free++;
698 [ + + ]: 8630380 : if (++migratetype == MIGRATE_PCPTYPES)
699 : : migratetype = 0;
700 : 8630380 : list = &pcp->lists[migratetype];
701 [ + + ]: 8630380 : } while (list_empty(list));
702 : :
703 : : /* This is the only non-empty list. Free them all. */
704 [ + + ]: 5244803 : if (batch_free == MIGRATE_PCPTYPES)
705 : : batch_free = to_free;
706 : :
707 : : do {
708 : : int mt; /* migratetype of the to-be-freed page */
709 : :
710 : 16362554 : page = list_entry(list->prev, struct page, lru);
711 : : /* must delete as __free_one_page list manipulates */
712 : : list_del(&page->lru);
713 : : mt = get_freepage_migratetype(page);
714 : : /* MIGRATE_MOVABLE list may include MIGRATE_RESERVEs */
715 : : __free_one_page(page, zone, 0, mt);
716 : : trace_mm_page_pcpu_drain(page, 0, mt);
717 : : if (likely(!is_migrate_isolate_page(page))) {
718 : 16362554 : __mod_zone_page_state(zone, NR_FREE_PAGES, 1);
719 : : if (is_migrate_cma(mt))
720 : : __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, 1);
721 : : }
722 [ + + ][ + + ]: 16907744 : } while (--to_free && --batch_free && !list_empty(list));
[ + + ]
723 : : }
724 : : spin_unlock(&zone->lock);
725 : 545194 : }
726 : :
727 : 0 : static void free_one_page(struct zone *zone, struct page *page, int order,
728 : : int migratetype)
729 : : {
730 : : spin_lock(&zone->lock);
731 : 2234018 : zone->pages_scanned = 0;
732 : :
733 : 2234018 : __free_one_page(page, zone, order, migratetype);
734 : : if (unlikely(!is_migrate_isolate(migratetype)))
735 : 737 : __mod_zone_freepage_state(zone, 1 << order, migratetype);
736 : : spin_unlock(&zone->lock);
737 : 2234017 : }
738 : :
739 : 0 : static bool free_pages_prepare(struct page *page, unsigned int order)
740 : : {
741 : : int i;
742 : : int bad = 0;
743 : :
744 : : trace_mm_page_free(page, order);
745 : : kmemcheck_free_shadow(page, order);
746 : :
747 [ + + ]: 29415729 : if (PageAnon(page))
748 : 29415729 : page->mapping = NULL;
749 [ + + ]: 63319071 : for (i = 0; i < (1 << order); i++)
750 : 67803277 : bad += free_pages_check(page + i);
751 [ + + ]: 29419136 : if (bad)
752 : : return false;
753 : :
754 [ + + ]: 29418422 : if (!PageHighMem(page)) {
755 : 12308017 : debug_check_no_locks_freed(page_address(page),
756 : : PAGE_SIZE << order);
757 : 12308946 : debug_check_no_obj_freed(page_address(page),
758 : : PAGE_SIZE << order);
759 : : }
760 : : arch_free_page(page, order);
761 : : kernel_map_pages(page, 1 << order, 0);
762 : :
763 : : return true;
764 : : }
765 : :
766 : 0 : static void __free_pages_ok(struct page *page, unsigned int order)
767 : : {
768 : : unsigned long flags;
769 : : int migratetype;
770 : :
771 [ + + ]: 2233464 : if (!free_pages_prepare(page, order))
772 : 688 : return;
773 : :
774 : : local_irq_save(flags);
775 : 2233460 : __count_vm_events(PGFREE, 1 << order);
776 : : migratetype = get_pageblock_migratetype(page);
777 : : set_freepage_migratetype(page, migratetype);
778 : 2233279 : free_one_page(page_zone(page), page, order, migratetype);
779 [ + + ]: 2234015 : local_irq_restore(flags);
780 : : }
781 : :
782 : 0 : void __init __free_pages_bootmem(struct page *page, unsigned int order)
783 : : {
784 : 0 : unsigned int nr_pages = 1 << order;
785 : : struct page *p = page;
786 : : unsigned int loop;
787 : :
788 : : prefetchw(p);
789 [ # # ]: 0 : for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
790 : 0 : prefetchw(p + 1);
791 : : __ClearPageReserved(p);
792 : : set_page_count(p, 0);
793 : : }
794 : : __ClearPageReserved(p);
795 : : set_page_count(p, 0);
796 : :
797 : 0 : page_zone(page)->managed_pages += nr_pages;
798 : : set_page_refcounted(page);
799 : 0 : __free_pages(page, order);
800 : 0 : }
801 : :
802 : : #ifdef CONFIG_CMA
803 : : /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
804 : : void __init init_cma_reserved_pageblock(struct page *page)
805 : : {
806 : : unsigned i = pageblock_nr_pages;
807 : : struct page *p = page;
808 : :
809 : : do {
810 : : __ClearPageReserved(p);
811 : : set_page_count(p, 0);
812 : : } while (++p, --i);
813 : :
814 : : set_page_refcounted(page);
815 : : set_pageblock_migratetype(page, MIGRATE_CMA);
816 : : __free_pages(page, pageblock_order);
817 : : adjust_managed_page_count(page, pageblock_nr_pages);
818 : : }
819 : : #endif
820 : :
821 : : /*
822 : : * The order of subdivision here is critical for the IO subsystem.
823 : : * Please do not alter this order without good reasons and regression
824 : : * testing. Specifically, as large blocks of memory are subdivided,
825 : : * the order in which smaller blocks are delivered depends on the order
826 : : * they're subdivided in this function. This is the primary factor
827 : : * influencing the order in which pages are delivered to the IO
828 : : * subsystem according to empirical testing, and this is also justified
829 : : * by considering the behavior of a buddy system containing a single
830 : : * large block of memory acted on by a series of small allocations.
831 : : * This behavior is a critical factor in sglist merging's success.
832 : : *
833 : : * -- nyc
834 : : */
835 : : static inline void expand(struct zone *zone, struct page *page,
836 : : int low, int high, struct free_area *area,
837 : : int migratetype)
838 : : {
839 : 19182293 : unsigned long size = 1 << high;
840 : :
841 [ + + ][ + + ]: 28058886 : while (high > low) {
842 : 8876593 : area--;
843 : 8876593 : high--;
844 : 8876593 : size >>= 1;
845 : : VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
846 : :
847 : : #ifdef CONFIG_DEBUG_PAGEALLOC
848 : : if (high < debug_guardpage_minorder()) {
849 : : /*
850 : : * Mark as guard pages (or page), that will allow to
851 : : * merge back to allocator when buddy will be freed.
852 : : * Corresponding page table entries will not be touched,
853 : : * pages will stay not present in virtual address space
854 : : */
855 : : INIT_LIST_HEAD(&page[size].lru);
856 : : set_page_guard_flag(&page[size]);
857 : : set_page_private(&page[size], high);
858 : : /* Guard pages are not available for any usage */
859 : : __mod_zone_freepage_state(zone, -(1 << high),
860 : : migratetype);
861 : : continue;
862 : : }
863 : : #endif
864 : 8876593 : list_add(&page[size].lru, &area->free_list[migratetype]);
865 : 8876593 : area->nr_free++;
866 : : set_page_order(&page[size], high);
867 : : }
868 : : }
869 : :
870 : : /*
871 : : * This page is about to be returned from the page allocator
872 : : */
873 : : static inline int check_new_page(struct page *page)
874 : : {
875 : : char *bad_reason = NULL;
876 : : unsigned long bad_flags = 0;
877 : :
878 [ - + ]: 33951086 : if (unlikely(page_mapcount(page)))
879 : : bad_reason = "nonzero mapcount";
880 [ - + ]: 33951086 : if (unlikely(page->mapping != NULL))
881 : : bad_reason = "non-NULL mapping";
882 [ - + ]: 33951086 : if (unlikely(atomic_read(&page->_count) != 0))
883 : : bad_reason = "nonzero _count";
884 [ - + ]: 33951086 : if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
885 : : bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
886 : : bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
887 : : }
888 : : if (unlikely(mem_cgroup_bad_page_check(page)))
889 : : bad_reason = "cgroup check failed";
890 [ - + ]: 33951086 : if (unlikely(bad_reason)) {
891 : 0 : bad_page(page, bad_reason, bad_flags);
892 : : return 1;
893 : : }
894 : : return 0;
895 : : }
896 : :
897 : 0 : static int prep_new_page(struct page *page, int order, gfp_t gfp_flags)
898 : : {
899 : : int i;
900 : :
901 [ + + ]: 63412792 : for (i = 0; i < (1 << order); i++) {
902 : 33951086 : struct page *p = page + i;
903 [ + - ]: 33952805 : if (unlikely(check_new_page(p)))
904 : : return 1;
905 : : }
906 : :
907 : 29461706 : set_page_private(page, 0);
908 : : set_page_refcounted(page);
909 : :
910 : : arch_alloc_page(page, order);
911 : : kernel_map_pages(page, 1 << order, 1);
912 : :
913 [ + + ]: 29461706 : if (gfp_flags & __GFP_ZERO)
914 : : prep_zero_page(page, order, gfp_flags);
915 : :
916 [ + + ][ + + ]: 29461689 : if (order && (gfp_flags & __GFP_COMP))
917 : 1270 : prep_compound_page(page, order);
918 : :
919 : : return 0;
920 : : }
921 : :
922 : : /*
923 : : * Go through the free lists for the given migratetype and remove
924 : : * the smallest available page from the freelists
925 : : */
926 : : static inline
927 : : struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
928 : : int migratetype)
929 : : {
930 : : unsigned int current_order;
931 : : struct free_area *area;
932 : : struct page *page;
933 : :
934 : : /* Find a page of the appropriate size in the preferred list */
935 [ + + ]: 28092765 : for (current_order = order; current_order < MAX_ORDER; ++current_order) {
936 : 28089887 : area = &(zone->free_area[current_order]);
937 [ + + ]: 28089887 : if (list_empty(&area->free_list[migratetype]))
938 : 8907716 : continue;
939 : :
940 : 19182171 : page = list_entry(area->free_list[migratetype].next,
941 : : struct page, lru);
942 : : list_del(&page->lru);
943 : : rmv_page_order(page);
944 : 19182171 : area->nr_free--;
945 : 19182171 : expand(zone, page, order, current_order, area, migratetype);
946 : : return page;
947 : : }
948 : :
949 : : return NULL;
950 : : }
951 : :
952 : :
953 : : /*
954 : : * This array describes the order lists are fallen back to when
955 : : * the free lists for the desirable migrate type are depleted
956 : : */
957 : : static int fallbacks[MIGRATE_TYPES][4] = {
958 : : [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
959 : : [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_RESERVE },
960 : : #ifdef CONFIG_CMA
961 : : [MIGRATE_MOVABLE] = { MIGRATE_CMA, MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
962 : : [MIGRATE_CMA] = { MIGRATE_RESERVE }, /* Never used */
963 : : #else
964 : : [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_RESERVE },
965 : : #endif
966 : : [MIGRATE_RESERVE] = { MIGRATE_RESERVE }, /* Never used */
967 : : #ifdef CONFIG_MEMORY_ISOLATION
968 : : [MIGRATE_ISOLATE] = { MIGRATE_RESERVE }, /* Never used */
969 : : #endif
970 : : };
971 : :
972 : : /*
973 : : * Move the free pages in a range to the free lists of the requested type.
974 : : * Note that start_page and end_pages are not aligned on a pageblock
975 : : * boundary. If alignment is required, use move_freepages_block()
976 : : */
977 : 0 : int move_freepages(struct zone *zone,
978 : : struct page *start_page, struct page *end_page,
979 : : int migratetype)
980 : : {
981 : 741 : struct page *page;
982 : : unsigned long order;
983 : : int pages_moved = 0;
984 : :
985 : : #ifndef CONFIG_HOLES_IN_ZONE
986 : : /*
987 : : * page_zone is not safe to call in this context when
988 : : * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
989 : : * anyway as we check zone boundaries in move_freepages_block().
990 : : * Remove at a later date when no bug reports exist related to
991 : : * grouping pages by mobility
992 : : */
993 [ + - ]: 19 : BUG_ON(page_zone(start_page) != page_zone(end_page));
994 : : #endif
995 : :
996 [ + + ]: 5247 : for (page = start_page; page <= end_page;) {
997 : : /* Make sure we are not inadvertently changing nodes */
998 : : VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
999 : :
1000 : : if (!pfn_valid_within(page_to_pfn(page))) {
1001 : : page++;
1002 : : continue;
1003 : : }
1004 : :
1005 [ + + ]: 5228 : if (!PageBuddy(page)) {
1006 : 4487 : page++;
1007 : 4487 : continue;
1008 : : }
1009 : :
1010 : : order = page_order(page);
1011 : 741 : list_move(&page->lru,
1012 : : &zone->free_area[order].free_list[migratetype]);
1013 : : set_freepage_migratetype(page, migratetype);
1014 : 741 : page += 1 << order;
1015 : 5228 : pages_moved += 1 << order;
1016 : : }
1017 : :
1018 : 19 : return pages_moved;
1019 : : }
1020 : :
1021 : 0 : int move_freepages_block(struct zone *zone, struct page *page,
1022 : : int migratetype)
1023 : : {
1024 : : unsigned long start_pfn, end_pfn;
1025 : : struct page *start_page, *end_page;
1026 : :
1027 : 19 : start_pfn = page_to_pfn(page);
1028 : 19 : start_pfn = start_pfn & ~(pageblock_nr_pages-1);
1029 : 19 : start_page = pfn_to_page(start_pfn);
1030 : 19 : end_page = start_page + pageblock_nr_pages - 1;
1031 : 19 : end_pfn = start_pfn + pageblock_nr_pages - 1;
1032 : :
1033 : : /* Do not cross zone boundaries */
1034 [ - + ]: 19 : if (!zone_spans_pfn(zone, start_pfn))
1035 : : start_page = page;
1036 [ # # ]: 19 : if (!zone_spans_pfn(zone, end_pfn))
1037 : : return 0;
1038 : :
1039 : 19 : return move_freepages(zone, start_page, end_page, migratetype);
1040 : : }
1041 : :
1042 : : static void change_pageblock_range(struct page *pageblock_page,
1043 : : int start_order, int migratetype)
1044 : : {
1045 : 33 : int nr_pageblocks = 1 << (start_order - pageblock_order);
1046 : :
1047 [ + + ]: 66 : while (nr_pageblocks--) {
1048 : 33 : set_pageblock_migratetype(pageblock_page, migratetype);
1049 : 33 : pageblock_page += pageblock_nr_pages;
1050 : : }
1051 : : }
1052 : :
1053 : : /*
1054 : : * If breaking a large block of pages, move all free pages to the preferred
1055 : : * allocation list. If falling back for a reclaimable kernel allocation, be
1056 : : * more aggressive about taking ownership of free pages.
1057 : : *
1058 : : * On the other hand, never change migration type of MIGRATE_CMA pageblocks
1059 : : * nor move CMA pages to different free lists. We don't want unmovable pages
1060 : : * to be allocated from MIGRATE_CMA areas.
1061 : : *
1062 : : * Returns the new migratetype of the pageblock (or the same old migratetype
1063 : : * if it was unchanged).
1064 : : */
1065 : 0 : static int try_to_steal_freepages(struct zone *zone, struct page *page,
1066 : : int start_type, int fallback_type)
1067 : : {
1068 : 122 : int current_order = page_order(page);
1069 : :
1070 : : /*
1071 : : * When borrowing from MIGRATE_CMA, we need to release the excess
1072 : : * buddy pages to CMA itself.
1073 : : */
1074 : : if (is_migrate_cma(fallback_type))
1075 : : return fallback_type;
1076 : :
1077 : : /* Take ownership for orders >= pageblock_order */
1078 [ + + ]: 122 : if (current_order >= pageblock_order) {
1079 : : change_pageblock_range(page, current_order, start_type);
1080 : : return start_type;
1081 : : }
1082 : :
1083 [ + + ]: 89 : if (current_order >= pageblock_order / 2 ||
1084 [ - + ]: 70 : start_type == MIGRATE_RECLAIMABLE ||
1085 : : page_group_by_mobility_disabled) {
1086 : : int pages;
1087 : :
1088 : 19 : pages = move_freepages_block(zone, page, start_type);
1089 : :
1090 : : /* Claim the whole block if over half of it is free */
1091 [ + + ][ - + ]: 19 : if (pages >= (1 << (pageblock_order-1)) ||
1092 : : page_group_by_mobility_disabled) {
1093 : :
1094 : 15 : set_pageblock_migratetype(page, start_type);
1095 : 15 : return start_type;
1096 : : }
1097 : :
1098 : : }
1099 : :
1100 : 74 : return fallback_type;
1101 : : }
1102 : :
1103 : : /* Remove an element from the buddy allocator from the fallback list */
1104 : : static inline struct page *
1105 : : __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
1106 : : {
1107 : : struct free_area *area;
1108 : : int current_order;
1109 : : struct page *page;
1110 : : int migratetype, new_type, i;
1111 : :
1112 : : /* Find the largest possible block of pages in the other list */
1113 [ + + ]: 33873 : for (current_order = MAX_ORDER-1; current_order >= order;
1114 : 30997 : --current_order) {
1115 : 62110 : for (i = 0;; i++) {
1116 : 93229 : migratetype = fallbacks[start_migratetype][i];
1117 : :
1118 : : /* MIGRATE_RESERVE handled later if necessary */
1119 [ + + ]: 93229 : if (migratetype == MIGRATE_RESERVE)
1120 : : break;
1121 : :
1122 : 62232 : area = &(zone->free_area[current_order]);
1123 [ + + ]: 62232 : if (list_empty(&area->free_list[migratetype]))
1124 : 62110 : continue;
1125 : :
1126 : 122 : page = list_entry(area->free_list[migratetype].next,
1127 : : struct page, lru);
1128 : 122 : area->nr_free--;
1129 : :
1130 : 122 : new_type = try_to_steal_freepages(zone, page,
1131 : : start_migratetype,
1132 : : migratetype);
1133 : :
1134 : : /* Remove the page from the freelists */
1135 : : list_del(&page->lru);
1136 : : rmv_page_order(page);
1137 : :
1138 : : expand(zone, page, order, current_order, area,
1139 : : new_type);
1140 : :
1141 : : trace_mm_page_alloc_extfrag(page, order, current_order,
1142 : : start_migratetype, migratetype, new_type);
1143 : :
1144 : : return page;
1145 : : }
1146 : : }
1147 : :
1148 : : return NULL;
1149 : : }
1150 : :
1151 : : /*
1152 : : * Do the hard work of removing an element from the buddy allocator.
1153 : : * Call me with the zone->lock already held.
1154 : : */
1155 : 19185049 : static struct page *__rmqueue(struct zone *zone, unsigned int order,
1156 : : int migratetype)
1157 : : {
1158 : : struct page *page;
1159 : :
1160 : : retry_reserve:
1161 : : page = __rmqueue_smallest(zone, order, migratetype);
1162 : :
1163 [ + + ][ + - ]: 19185049 : if (unlikely(!page) && migratetype != MIGRATE_RESERVE) {
1164 : 2876 : page = __rmqueue_fallback(zone, order, migratetype);
1165 : :
1166 : : /*
1167 : : * Use MIGRATE_RESERVE rather than fail an allocation. goto
1168 : : * is used because __rmqueue_smallest is an inline function
1169 : : * and we want just one call site
1170 : : */
1171 [ + + ]: 19185174 : if (!page) {
1172 : : migratetype = MIGRATE_RESERVE;
1173 : : goto retry_reserve;
1174 : : }
1175 : : }
1176 : :
1177 : : trace_mm_page_alloc_zone_locked(page, order, migratetype);
1178 : 19182298 : return page;
1179 : : }
1180 : :
1181 : : /*
1182 : : * Obtain a specified number of elements from the buddy allocator, all under
1183 : : * a single hold of the lock, for efficiency. Add them to the supplied list.
1184 : : * Returns the number of new pages which were placed at *list.
1185 : : */
1186 : 0 : static int rmqueue_bulk(struct zone *zone, unsigned int order,
1187 : : unsigned long count, struct list_head *list,
1188 : : int migratetype, int cold)
1189 : : {
1190 : : int mt = migratetype, i;
1191 : :
1192 : : spin_lock(&zone->lock);
1193 [ + + ]: 17494956 : for (i = 0; i < count; ++i) {
1194 : 16948239 : struct page *page = __rmqueue(zone, order, migratetype);
1195 [ + ]: 16948237 : if (unlikely(page == NULL))
1196 : : break;
1197 : :
1198 : : /*
1199 : : * Split buddy pages returned by expand() are received here
1200 : : * in physical page order. The page is added to the callers and
1201 : : * list and the list head then moves forward. From the callers
1202 : : * perspective, the linked list is ordered by page number in
1203 : : * some conditions. This is useful for IO devices that can
1204 : : * merge IO requests if the physical pages are ordered
1205 : : * properly.
1206 : : */
1207 [ + + ]: 17494950 : if (likely(cold == 0))
1208 : 16755015 : list_add(&page->lru, list);
1209 : : else
1210 : 193223 : list_add_tail(&page->lru, list);
1211 : : if (IS_ENABLED(CONFIG_CMA)) {
1212 : : mt = get_pageblock_migratetype(page);
1213 : : if (!is_migrate_cma(mt) && !is_migrate_isolate(mt))
1214 : : mt = migratetype;
1215 : : }
1216 : : set_freepage_migratetype(page, mt);
1217 : 16948238 : list = &page->lru;
1218 : : if (is_migrate_cma(mt))
1219 : : __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
1220 : : -(1 << order));
1221 : : }
1222 : 4 : __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
1223 : : spin_unlock(&zone->lock);
1224 : 546718 : return i;
1225 : : }
1226 : :
1227 : : #ifdef CONFIG_NUMA
1228 : : /*
1229 : : * Called from the vmstat counter updater to drain pagesets of this
1230 : : * currently executing processor on remote nodes after they have
1231 : : * expired.
1232 : : *
1233 : : * Note that this function must be called with the thread pinned to
1234 : : * a single processor.
1235 : : */
1236 : : void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
1237 : : {
1238 : : unsigned long flags;
1239 : : int to_drain;
1240 : : unsigned long batch;
1241 : :
1242 : : local_irq_save(flags);
1243 : : batch = ACCESS_ONCE(pcp->batch);
1244 : : if (pcp->count >= batch)
1245 : : to_drain = batch;
1246 : : else
1247 : : to_drain = pcp->count;
1248 : : if (to_drain > 0) {
1249 : : free_pcppages_bulk(zone, to_drain, pcp);
1250 : : pcp->count -= to_drain;
1251 : : }
1252 : : local_irq_restore(flags);
1253 : : }
1254 : : static bool gfp_thisnode_allocation(gfp_t gfp_mask)
1255 : : {
1256 : : return (gfp_mask & GFP_THISNODE) == GFP_THISNODE;
1257 : : }
1258 : : #else
1259 : 29495122 : static bool gfp_thisnode_allocation(gfp_t gfp_mask)
1260 : : {
1261 : 29495122 : return false;
1262 : : }
1263 : : #endif
1264 : :
1265 : : /*
1266 : : * Drain pages of the indicated processor.
1267 : : *
1268 : : * The processor must either be the current processor and the
1269 : : * thread pinned to the current processor or a processor that
1270 : : * is not online.
1271 : : */
1272 : 0 : static void drain_pages(unsigned int cpu)
1273 : : {
1274 : : unsigned long flags;
1275 : 1137 : struct zone *zone;
1276 : :
1277 [ + + ][ + + ]: 1515 : for_each_populated_zone(zone) {
1278 : : struct per_cpu_pageset *pset;
1279 : : struct per_cpu_pages *pcp;
1280 : :
1281 : : local_irq_save(flags);
1282 : 756 : pset = per_cpu_ptr(zone->pageset, cpu);
1283 : :
1284 : 756 : pcp = &pset->pcp;
1285 [ + + ]: 1131 : if (pcp->count) {
1286 : 497 : free_pcppages_bulk(zone, pcp->count, pcp);
1287 : 501 : pcp->count = 0;
1288 : : }
1289 [ + + ]: 760 : local_irq_restore(flags);
1290 : : }
1291 : 380 : }
1292 : :
1293 : : /*
1294 : : * Spill all of this CPU's per-cpu pages back into the buddy allocator.
1295 : : */
1296 : 0 : void drain_local_pages(void *arg)
1297 : : {
1298 : 299 : drain_pages(smp_processor_id());
1299 : 302 : }
1300 : :
1301 : : /*
1302 : : * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
1303 : : *
1304 : : * Note that this code is protected against sending an IPI to an offline
1305 : : * CPU but does not guarantee sending an IPI to newly hotplugged CPUs:
1306 : : * on_each_cpu_mask() blocks hotplug and won't talk to offlined CPUs but
1307 : : * nothing keeps CPUs from showing up after we populated the cpumask and
1308 : : * before the call to on_each_cpu_mask().
1309 : : */
1310 : 0 : void drain_all_pages(void)
1311 : : {
1312 : : int cpu;
1313 : : struct per_cpu_pageset *pcp;
1314 : 73085 : struct zone *zone;
1315 : :
1316 : : /*
1317 : : * Allocate in the BSS so we wont require allocation in
1318 : : * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
1319 : : */
1320 : : static cpumask_t cpus_with_pcps;
1321 : :
1322 : : /*
1323 : : * We don't care about racing with CPU hotplug event
1324 : : * as offline notification will cause the notified
1325 : : * cpu to drain that CPU pcps and on_each_cpu_mask
1326 : : * disables preemption as part of its processing
1327 : : */
1328 [ + + ]: 34377 : for_each_online_cpu(cpu) {
1329 : : bool has_pcps = false;
1330 [ + + ][ + + ]: 97338 : for_each_populated_zone(zone) {
1331 : 48832 : pcp = per_cpu_ptr(zone->pageset, cpu);
1332 [ + + ]: 48832 : if (pcp->pcp.count) {
1333 : : has_pcps = true;
1334 : : break;
1335 : : }
1336 : : }
1337 [ + + ]: 24555 : if (has_pcps)
1338 : : cpumask_set_cpu(cpu, &cpus_with_pcps);
1339 : : else
1340 : : cpumask_clear_cpu(cpu, &cpus_with_pcps);
1341 : : }
1342 : 4911 : on_each_cpu_mask(&cpus_with_pcps, drain_local_pages, NULL, 1);
1343 : 4911 : }
1344 : :
1345 : : #ifdef CONFIG_HIBERNATION
1346 : :
1347 : : void mark_free_pages(struct zone *zone)
1348 : : {
1349 : : unsigned long pfn, max_zone_pfn;
1350 : : unsigned long flags;
1351 : : int order, t;
1352 : : struct list_head *curr;
1353 : :
1354 : : if (zone_is_empty(zone))
1355 : : return;
1356 : :
1357 : : spin_lock_irqsave(&zone->lock, flags);
1358 : :
1359 : : max_zone_pfn = zone_end_pfn(zone);
1360 : : for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
1361 : : if (pfn_valid(pfn)) {
1362 : : struct page *page = pfn_to_page(pfn);
1363 : :
1364 : : if (!swsusp_page_is_forbidden(page))
1365 : : swsusp_unset_page_free(page);
1366 : : }
1367 : :
1368 : : for_each_migratetype_order(order, t) {
1369 : : list_for_each(curr, &zone->free_area[order].free_list[t]) {
1370 : : unsigned long i;
1371 : :
1372 : : pfn = page_to_pfn(list_entry(curr, struct page, lru));
1373 : : for (i = 0; i < (1UL << order); i++)
1374 : : swsusp_set_page_free(pfn_to_page(pfn + i));
1375 : : }
1376 : : }
1377 : : spin_unlock_irqrestore(&zone->lock, flags);
1378 : : }
1379 : : #endif /* CONFIG_PM */
1380 : :
1381 : : /*
1382 : : * Free a 0-order page
1383 : : * cold == 1 ? free a cold page : free a hot page
1384 : : */
1385 : 0 : void free_hot_cold_page(struct page *page, int cold)
1386 : : {
1387 : 27183552 : struct zone *zone = page_zone(page);
1388 : : struct per_cpu_pages *pcp;
1389 : : unsigned long flags;
1390 : : int migratetype;
1391 : :
1392 [ + ]: 27183552 : if (!free_pages_prepare(page, 0))
1393 : 27186220 : return;
1394 : :
1395 : : migratetype = get_pageblock_migratetype(page);
1396 : : set_freepage_migratetype(page, migratetype);
1397 : : local_irq_save(flags);
1398 : : __count_vm_event(PGFREE);
1399 : :
1400 : : /*
1401 : : * We only track unmovable, reclaimable and movable on pcp lists.
1402 : : * Free ISOLATE pages back to the allocator because they are being
1403 : : * offlined but treat RESERVE as movable pages so we can get those
1404 : : * areas back if necessary. Otherwise, we may have to free
1405 : : * excessively into the page allocator
1406 : : */
1407 [ + + ]: 54371507 : if (migratetype >= MIGRATE_PCPTYPES) {
1408 : : if (unlikely(is_migrate_isolate(migratetype))) {
1409 : : free_one_page(zone, page, 0, migratetype);
1410 : : goto out;
1411 : : }
1412 : : migratetype = MIGRATE_MOVABLE;
1413 : : }
1414 : :
1415 : 54375910 : pcp = &this_cpu_ptr(zone->pageset)->pcp;
1416 [ + + ]: 54371507 : if (cold)
1417 : 82618 : list_add_tail(&page->lru, &pcp->lists[migratetype]);
1418 : : else
1419 : 27105337 : list_add(&page->lru, &pcp->lists[migratetype]);
1420 : 27187955 : pcp->count++;
1421 [ + + ]: 27187955 : if (pcp->count >= pcp->high) {
1422 : 544692 : unsigned long batch = ACCESS_ONCE(pcp->batch);
1423 : 544692 : free_pcppages_bulk(zone, batch, pcp);
1424 : 544693 : pcp->count -= batch;
1425 : : }
1426 : :
1427 : : out:
1428 [ + + ]: 27187956 : local_irq_restore(flags);
1429 : : }
1430 : :
1431 : : /*
1432 : : * Free a list of 0-order pages
1433 : : */
1434 : 0 : void free_hot_cold_page_list(struct list_head *list, int cold)
1435 : : {
1436 : : struct page *page, *next;
1437 : :
1438 [ + + ]: 30706780 : list_for_each_entry_safe(page, next, list, lru) {
1439 : : trace_mm_page_free_batched(page, cold);
1440 : 22607676 : free_hot_cold_page(page, cold);
1441 : : }
1442 : 8099104 : }
1443 : :
1444 : : /*
1445 : : * split_page takes a non-compound higher-order page, and splits it into
1446 : : * n (1<<order) sub-pages: page[0..n]
1447 : : * Each sub-page must be freed individually.
1448 : : *
1449 : : * Note: this is probably too low level an operation for use in drivers.
1450 : : * Please consult with lkml before using this in your driver.
1451 : : */
1452 : 0 : void split_page(struct page *page, unsigned int order)
1453 : : {
1454 : : int i;
1455 : :
1456 : : VM_BUG_ON_PAGE(PageCompound(page), page);
1457 : : VM_BUG_ON_PAGE(!page_count(page), page);
1458 : :
1459 : : #ifdef CONFIG_KMEMCHECK
1460 : : /*
1461 : : * Split shadow pages too, because free(page[0]) would
1462 : : * otherwise free the whole shadow.
1463 : : */
1464 : : if (kmemcheck_page_is_tracked(page))
1465 : : split_page(virt_to_page(page[0].shadow), order);
1466 : : #endif
1467 : :
1468 [ # # ][ # # ]: 0 : for (i = 1; i < (1 << order); i++)
[ # # ]
1469 : 0 : set_page_refcounted(page + i);
1470 : 0 : }
1471 : : EXPORT_SYMBOL_GPL(split_page);
1472 : :
1473 : 0 : static int __isolate_free_page(struct page *page, unsigned int order)
1474 : : {
1475 : : unsigned long watermark;
1476 : : struct zone *zone;
1477 : : int mt;
1478 : :
1479 [ # # ]: 0 : BUG_ON(!PageBuddy(page));
1480 : :
1481 : 0 : zone = page_zone(page);
1482 : : mt = get_pageblock_migratetype(page);
1483 : :
1484 : : if (!is_migrate_isolate(mt)) {
1485 : : /* Obey watermarks as if the page was being allocated */
1486 : 0 : watermark = low_wmark_pages(zone) + (1 << order);
1487 [ # # ]: 0 : if (!zone_watermark_ok(zone, 0, watermark, 0, 0))
1488 : : return 0;
1489 : :
1490 : 0 : __mod_zone_freepage_state(zone, -(1UL << order), mt);
1491 : : }
1492 : :
1493 : : /* Remove page from free list */
1494 : : list_del(&page->lru);
1495 : 0 : zone->free_area[order].nr_free--;
1496 : : rmv_page_order(page);
1497 : :
1498 : : /* Set the pageblock if the isolated page is at least a pageblock */
1499 [ # # ]: 0 : if (order >= pageblock_order - 1) {
1500 : 0 : struct page *endpage = page + (1 << order) - 1;
1501 [ # # ]: 0 : for (; page < endpage; page += pageblock_nr_pages) {
1502 : : int mt = get_pageblock_migratetype(page);
1503 : : if (!is_migrate_isolate(mt) && !is_migrate_cma(mt))
1504 : 0 : set_pageblock_migratetype(page,
1505 : : MIGRATE_MOVABLE);
1506 : : }
1507 : : }
1508 : :
1509 : 0 : return 1UL << order;
1510 : : }
1511 : :
1512 : : /*
1513 : : * Similar to split_page except the page is already free. As this is only
1514 : : * being used for migration, the migratetype of the block also changes.
1515 : : * As this is called with interrupts disabled, the caller is responsible
1516 : : * for calling arch_alloc_page() and kernel_map_page() after interrupts
1517 : : * are enabled.
1518 : : *
1519 : : * Note: this is probably too low level an operation for use in drivers.
1520 : : * Please consult with lkml before using this in your driver.
1521 : : */
1522 : 0 : int split_free_page(struct page *page)
1523 : : {
1524 : : unsigned int order;
1525 : : int nr_pages;
1526 : :
1527 : : order = page_order(page);
1528 : :
1529 : 0 : nr_pages = __isolate_free_page(page, order);
1530 [ # # ]: 0 : if (!nr_pages)
1531 : : return 0;
1532 : :
1533 : : /* Split into individual pages */
1534 : : set_page_refcounted(page);
1535 : : split_page(page, order);
1536 : : return nr_pages;
1537 : : }
1538 : :
1539 : : /*
1540 : : * Really, prep_compound_page() should be called from __rmqueue_bulk(). But
1541 : : * we cheat by calling it from here, in the order > 0 path. Saves a branch
1542 : : * or two.
1543 : : */
1544 : : static inline
1545 : : struct page *buffered_rmqueue(struct zone *preferred_zone,
1546 : : struct zone *zone, int order, gfp_t gfp_flags,
1547 : : int migratetype)
1548 : : {
1549 : : unsigned long flags;
1550 : : struct page *page;
1551 : 29460881 : int cold = !!(gfp_flags & __GFP_COLD);
1552 : :
1553 : : again:
1554 [ + + ]: 29460719 : if (likely(order == 0)) {
1555 : : struct per_cpu_pages *pcp;
1556 : : struct list_head *list;
1557 : :
1558 : : local_irq_save(flags);
1559 : 54456408 : pcp = &this_cpu_ptr(zone->pageset)->pcp;
1560 : 27228204 : list = &pcp->lists[migratetype];
1561 [ + + ]: 56741330 : if (list_empty(list)) {
1562 : 546709 : pcp->count += rmqueue_bulk(zone, 0,
1563 : 546709 : pcp->batch, list,
1564 : : migratetype, cold);
1565 [ + - ]: 546718 : if (unlikely(list_empty(list)))
1566 : : goto failed;
1567 : : }
1568 : :
1569 [ + + ]: 27228213 : if (cold)
1570 : 424876 : page = list_entry(list->prev, struct page, lru);
1571 : : else
1572 : 26803337 : page = list_entry(list->next, struct page, lru);
1573 : :
1574 : : list_del(&page->lru);
1575 : 27228213 : pcp->count--;
1576 : : } else {
1577 [ - + ]: 2234065 : if (unlikely(gfp_flags & __GFP_NOFAIL)) {
1578 : : /*
1579 : : * __GFP_NOFAIL is not to be used in new code.
1580 : : *
1581 : : * All __GFP_NOFAIL callers should be fixed so that they
1582 : : * properly detect and handle allocation failures.
1583 : : *
1584 : : * We most definitely don't want callers attempting to
1585 : : * allocate greater than order-1 page units with
1586 : : * __GFP_NOFAIL.
1587 : : */
1588 [ # # ][ # # ]: 0 : WARN_ON_ONCE(order > 1);
[ # # ]
1589 : : }
1590 : 2234065 : spin_lock_irqsave(&zone->lock, flags);
1591 : 2234075 : page = __rmqueue(zone, order, migratetype);
1592 : : spin_unlock(&zone->lock);
1593 [ + - ]: 2234075 : if (!page)
1594 : : goto failed;
1595 : 2234075 : __mod_zone_freepage_state(zone, -(1 << order),
1596 : : get_pageblock_migratetype(page));
1597 : : }
1598 : :
1599 : : /*
1600 : : * NOTE: GFP_THISNODE allocations do not partake in the kswapd
1601 : : * aging protocol, so they can't be fair.
1602 : : */
1603 [ + ]: 29462288 : if (!gfp_thisnode_allocation(gfp_flags))
1604 : 29458389 : __mod_zone_page_state(zone, NR_ALLOC_BATCH, -(1 << order));
1605 : :
1606 : 29461139 : __count_zone_vm_events(PGALLOC, zone, 1 << order);
1607 : : zone_statistics(preferred_zone, zone, gfp_flags);
1608 [ + + ]: 29461139 : local_irq_restore(flags);
1609 : :
1610 : : VM_BUG_ON_PAGE(bad_range(zone, page), page);
1611 [ + ]: 29462279 : if (prep_new_page(page, order, gfp_flags))
1612 : : goto again;
1613 : : return page;
1614 : :
1615 : : failed:
1616 [ # # ]: 0 : local_irq_restore(flags);
1617 : : return NULL;
1618 : : }
1619 : :
1620 : : #ifdef CONFIG_FAIL_PAGE_ALLOC
1621 : :
1622 : : static struct {
1623 : : struct fault_attr attr;
1624 : :
1625 : : u32 ignore_gfp_highmem;
1626 : : u32 ignore_gfp_wait;
1627 : : u32 min_order;
1628 : : } fail_page_alloc = {
1629 : : .attr = FAULT_ATTR_INITIALIZER,
1630 : : .ignore_gfp_wait = 1,
1631 : : .ignore_gfp_highmem = 1,
1632 : : .min_order = 1,
1633 : : };
1634 : :
1635 : : static int __init setup_fail_page_alloc(char *str)
1636 : : {
1637 : : return setup_fault_attr(&fail_page_alloc.attr, str);
1638 : : }
1639 : : __setup("fail_page_alloc=", setup_fail_page_alloc);
1640 : :
1641 : : static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1642 : : {
1643 : : if (order < fail_page_alloc.min_order)
1644 : : return false;
1645 : : if (gfp_mask & __GFP_NOFAIL)
1646 : : return false;
1647 : : if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
1648 : : return false;
1649 : : if (fail_page_alloc.ignore_gfp_wait && (gfp_mask & __GFP_WAIT))
1650 : : return false;
1651 : :
1652 : : return should_fail(&fail_page_alloc.attr, 1 << order);
1653 : : }
1654 : :
1655 : : #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
1656 : :
1657 : : static int __init fail_page_alloc_debugfs(void)
1658 : : {
1659 : : umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
1660 : : struct dentry *dir;
1661 : :
1662 : : dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
1663 : : &fail_page_alloc.attr);
1664 : : if (IS_ERR(dir))
1665 : : return PTR_ERR(dir);
1666 : :
1667 : : if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
1668 : : &fail_page_alloc.ignore_gfp_wait))
1669 : : goto fail;
1670 : : if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
1671 : : &fail_page_alloc.ignore_gfp_highmem))
1672 : : goto fail;
1673 : : if (!debugfs_create_u32("min-order", mode, dir,
1674 : : &fail_page_alloc.min_order))
1675 : : goto fail;
1676 : :
1677 : : return 0;
1678 : : fail:
1679 : : debugfs_remove_recursive(dir);
1680 : :
1681 : : return -ENOMEM;
1682 : : }
1683 : :
1684 : : late_initcall(fail_page_alloc_debugfs);
1685 : :
1686 : : #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
1687 : :
1688 : : #else /* CONFIG_FAIL_PAGE_ALLOC */
1689 : :
1690 : : static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
1691 : : {
1692 : : return false;
1693 : : }
1694 : :
1695 : : #endif /* CONFIG_FAIL_PAGE_ALLOC */
1696 : :
1697 : : /*
1698 : : * Return true if free pages are above 'mark'. This takes into account the order
1699 : : * of the allocation.
1700 : : */
1701 : 0 : static bool __zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1702 : : int classzone_idx, int alloc_flags, long free_pages)
1703 : : {
1704 : : /* free_pages my go negative - that's OK */
1705 : 29784670 : long min = mark;
1706 : 29784670 : long lowmem_reserve = z->lowmem_reserve[classzone_idx];
1707 : : int o;
1708 : : long free_cma = 0;
1709 : :
1710 : 29784670 : free_pages -= (1 << order) - 1;
1711 [ + + ]: 29784670 : if (alloc_flags & ALLOC_HIGH)
1712 : 3 : min -= min / 2;
1713 [ + + ]: 29784670 : if (alloc_flags & ALLOC_HARDER)
1714 : 2923 : min -= min / 4;
1715 : : #ifdef CONFIG_CMA
1716 : : /* If allocation can't use CMA areas don't use free CMA pages */
1717 : : if (!(alloc_flags & ALLOC_CMA))
1718 : : free_cma = zone_page_state(z, NR_FREE_CMA_PAGES);
1719 : : #endif
1720 : :
1721 [ # # ]: 29784670 : if (free_pages - free_cma <= min + lowmem_reserve)
1722 : : return false;
1723 [ + + ]: 32885626 : for (o = 0; o < order; o++) {
1724 : : /* At the next order, this order's pages become unavailable */
1725 : 3372092 : free_pages -= z->free_area[o].nr_free << o;
1726 : :
1727 : : /* Require fewer higher order pages to be free */
1728 : 3372092 : min >>= min_free_order_shift;
1729 : :
1730 [ + + ]: 3372092 : if (free_pages <= min)
1731 : : return false;
1732 : : }
1733 : : return true;
1734 : : }
1735 : :
1736 : 0 : bool zone_watermark_ok(struct zone *z, int order, unsigned long mark,
1737 : : int classzone_idx, int alloc_flags)
1738 : : {
1739 : 29575607 : return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1740 : : zone_page_state(z, NR_FREE_PAGES));
1741 : : }
1742 : :
1743 : 0 : bool zone_watermark_ok_safe(struct zone *z, int order, unsigned long mark,
1744 : : int classzone_idx, int alloc_flags)
1745 : : {
1746 : : long free_pages = zone_page_state(z, NR_FREE_PAGES);
1747 : :
1748 [ - + ][ # # ]: 211222 : if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
1749 : : free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
1750 : :
1751 : 211222 : return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
1752 : : free_pages);
1753 : : }
1754 : :
1755 : : #ifdef CONFIG_NUMA
1756 : : /*
1757 : : * zlc_setup - Setup for "zonelist cache". Uses cached zone data to
1758 : : * skip over zones that are not allowed by the cpuset, or that have
1759 : : * been recently (in last second) found to be nearly full. See further
1760 : : * comments in mmzone.h. Reduces cache footprint of zonelist scans
1761 : : * that have to skip over a lot of full or unallowed zones.
1762 : : *
1763 : : * If the zonelist cache is present in the passed zonelist, then
1764 : : * returns a pointer to the allowed node mask (either the current
1765 : : * tasks mems_allowed, or node_states[N_MEMORY].)
1766 : : *
1767 : : * If the zonelist cache is not available for this zonelist, does
1768 : : * nothing and returns NULL.
1769 : : *
1770 : : * If the fullzones BITMAP in the zonelist cache is stale (more than
1771 : : * a second since last zap'd) then we zap it out (clear its bits.)
1772 : : *
1773 : : * We hold off even calling zlc_setup, until after we've checked the
1774 : : * first zone in the zonelist, on the theory that most allocations will
1775 : : * be satisfied from that first zone, so best to examine that zone as
1776 : : * quickly as we can.
1777 : : */
1778 : : static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1779 : : {
1780 : : struct zonelist_cache *zlc; /* cached zonelist speedup info */
1781 : : nodemask_t *allowednodes; /* zonelist_cache approximation */
1782 : :
1783 : : zlc = zonelist->zlcache_ptr;
1784 : : if (!zlc)
1785 : : return NULL;
1786 : :
1787 : : if (time_after(jiffies, zlc->last_full_zap + HZ)) {
1788 : : bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1789 : : zlc->last_full_zap = jiffies;
1790 : : }
1791 : :
1792 : : allowednodes = !in_interrupt() && (alloc_flags & ALLOC_CPUSET) ?
1793 : : &cpuset_current_mems_allowed :
1794 : : &node_states[N_MEMORY];
1795 : : return allowednodes;
1796 : : }
1797 : :
1798 : : /*
1799 : : * Given 'z' scanning a zonelist, run a couple of quick checks to see
1800 : : * if it is worth looking at further for free memory:
1801 : : * 1) Check that the zone isn't thought to be full (doesn't have its
1802 : : * bit set in the zonelist_cache fullzones BITMAP).
1803 : : * 2) Check that the zones node (obtained from the zonelist_cache
1804 : : * z_to_n[] mapping) is allowed in the passed in allowednodes mask.
1805 : : * Return true (non-zero) if zone is worth looking at further, or
1806 : : * else return false (zero) if it is not.
1807 : : *
1808 : : * This check -ignores- the distinction between various watermarks,
1809 : : * such as GFP_HIGH, GFP_ATOMIC, PF_MEMALLOC, ... If a zone is
1810 : : * found to be full for any variation of these watermarks, it will
1811 : : * be considered full for up to one second by all requests, unless
1812 : : * we are so low on memory on all allowed nodes that we are forced
1813 : : * into the second scan of the zonelist.
1814 : : *
1815 : : * In the second scan we ignore this zonelist cache and exactly
1816 : : * apply the watermarks to all zones, even it is slower to do so.
1817 : : * We are low on memory in the second scan, and should leave no stone
1818 : : * unturned looking for a free page.
1819 : : */
1820 : : static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
1821 : : nodemask_t *allowednodes)
1822 : : {
1823 : : struct zonelist_cache *zlc; /* cached zonelist speedup info */
1824 : : int i; /* index of *z in zonelist zones */
1825 : : int n; /* node that zone *z is on */
1826 : :
1827 : : zlc = zonelist->zlcache_ptr;
1828 : : if (!zlc)
1829 : : return 1;
1830 : :
1831 : : i = z - zonelist->_zonerefs;
1832 : : n = zlc->z_to_n[i];
1833 : :
1834 : : /* This zone is worth trying if it is allowed but not full */
1835 : : return node_isset(n, *allowednodes) && !test_bit(i, zlc->fullzones);
1836 : : }
1837 : :
1838 : : /*
1839 : : * Given 'z' scanning a zonelist, set the corresponding bit in
1840 : : * zlc->fullzones, so that subsequent attempts to allocate a page
1841 : : * from that zone don't waste time re-examining it.
1842 : : */
1843 : : static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
1844 : : {
1845 : : struct zonelist_cache *zlc; /* cached zonelist speedup info */
1846 : : int i; /* index of *z in zonelist zones */
1847 : :
1848 : : zlc = zonelist->zlcache_ptr;
1849 : : if (!zlc)
1850 : : return;
1851 : :
1852 : : i = z - zonelist->_zonerefs;
1853 : :
1854 : : set_bit(i, zlc->fullzones);
1855 : : }
1856 : :
1857 : : /*
1858 : : * clear all zones full, called after direct reclaim makes progress so that
1859 : : * a zone that was recently full is not skipped over for up to a second
1860 : : */
1861 : : static void zlc_clear_zones_full(struct zonelist *zonelist)
1862 : : {
1863 : : struct zonelist_cache *zlc; /* cached zonelist speedup info */
1864 : :
1865 : : zlc = zonelist->zlcache_ptr;
1866 : : if (!zlc)
1867 : : return;
1868 : :
1869 : : bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
1870 : : }
1871 : :
1872 : : static bool zone_local(struct zone *local_zone, struct zone *zone)
1873 : : {
1874 : : return local_zone->node == zone->node;
1875 : : }
1876 : :
1877 : : static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1878 : : {
1879 : : return node_isset(local_zone->node, zone->zone_pgdat->reclaim_nodes);
1880 : : }
1881 : :
1882 : : static void __paginginit init_zone_allows_reclaim(int nid)
1883 : : {
1884 : : int i;
1885 : :
1886 : : for_each_online_node(i)
1887 : : if (node_distance(nid, i) <= RECLAIM_DISTANCE)
1888 : : node_set(i, NODE_DATA(nid)->reclaim_nodes);
1889 : : else
1890 : : zone_reclaim_mode = 1;
1891 : : }
1892 : :
1893 : : #else /* CONFIG_NUMA */
1894 : :
1895 : : static nodemask_t *zlc_setup(struct zonelist *zonelist, int alloc_flags)
1896 : : {
1897 : : return NULL;
1898 : : }
1899 : :
1900 : : static int zlc_zone_worth_trying(struct zonelist *zonelist, struct zoneref *z,
1901 : : nodemask_t *allowednodes)
1902 : : {
1903 : : return 1;
1904 : : }
1905 : :
1906 : : static void zlc_mark_zone_full(struct zonelist *zonelist, struct zoneref *z)
1907 : : {
1908 : : }
1909 : :
1910 : : static void zlc_clear_zones_full(struct zonelist *zonelist)
1911 : : {
1912 : : }
1913 : :
1914 : : static bool zone_local(struct zone *local_zone, struct zone *zone)
1915 : : {
1916 : : return true;
1917 : : }
1918 : :
1919 : : static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
1920 : : {
1921 : : return true;
1922 : : }
1923 : :
1924 : : static inline void init_zone_allows_reclaim(int nid)
1925 : : {
1926 : : }
1927 : : #endif /* CONFIG_NUMA */
1928 : :
1929 : : /*
1930 : : * get_page_from_freelist goes through the zonelist trying to allocate
1931 : : * a page.
1932 : : */
1933 : : static struct page *
1934 : 0 : get_page_from_freelist(gfp_t gfp_mask, nodemask_t *nodemask, unsigned int order,
1935 : : struct zonelist *zonelist, int high_zoneidx, int alloc_flags,
1936 : : struct zone *preferred_zone, int migratetype)
1937 : : {
1938 : : struct zoneref *z;
1939 : : struct page *page = NULL;
1940 : : int classzone_idx;
1941 : : struct zone *zone;
1942 : : nodemask_t *allowednodes = NULL;/* zonelist_cache approximation */
1943 : : int zlc_active = 0; /* set if using zonelist_cache */
1944 : : int did_zlc_setup = 0; /* just call zlc_setup() one time */
1945 : :
1946 : 29513126 : classzone_idx = zone_idx(preferred_zone);
1947 : : zonelist_scan:
1948 : : /*
1949 : : * Scan zonelist, looking for a zone with enough free.
1950 : : * See also __cpuset_node_allowed_softwall() comment in kernel/cpuset.c.
1951 : : */
1952 [ + + ]: 36578454 : for_each_zone_zonelist_nodemask(zone, z, zonelist,
1953 : : high_zoneidx, nodemask) {
1954 : : unsigned long mark;
1955 : :
1956 : : if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
1957 : : !zlc_zone_worth_trying(zonelist, z, allowednodes))
1958 : : continue;
1959 : : if ((alloc_flags & ALLOC_CPUSET) &&
1960 : : !cpuset_zone_allowed_softwall(zone, gfp_mask))
1961 : : continue;
1962 : : BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
1963 [ + ]: 36527516 : if (unlikely(alloc_flags & ALLOC_NO_WATERMARKS))
1964 : : goto try_this_zone;
1965 : : /*
1966 : : * Distribute pages in proportion to the individual
1967 : : * zone size to ensure fair page aging. The zone a
1968 : : * page was allocated in should have no effect on the
1969 : : * time the page has in memory before being reclaimed.
1970 : : *
1971 : : * Try to stay in local zones in the fastpath. If
1972 : : * that fails, the slowpath is entered, which will do
1973 : : * another pass starting with the local zones, but
1974 : : * ultimately fall back to remote zones that do not
1975 : : * partake in the fairness round-robin cycle of this
1976 : : * zonelist.
1977 : : *
1978 : : * NOTE: GFP_THISNODE allocations do not partake in
1979 : : * the kswapd aging protocol, so they can't be fair.
1980 : : */
1981 [ + + ]: 36527778 : if ((alloc_flags & ALLOC_WMARK_LOW) &&
1982 : : !gfp_thisnode_allocation(gfp_mask)) {
1983 [ + + ]: 36447930 : if (zone_page_state(zone, NR_ALLOC_BATCH) <= 0)
1984 : 6959393 : continue;
1985 : : if (!zone_local(preferred_zone, zone))
1986 : : continue;
1987 : : }
1988 : : /*
1989 : : * When allocating a page cache page for writing, we
1990 : : * want to get it from a zone that is within its dirty
1991 : : * limit, such that no single zone holds more than its
1992 : : * proportional share of globally allowed dirty pages.
1993 : : * The dirty limits take into account the zone's
1994 : : * lowmem reserves and high watermark so that kswapd
1995 : : * should be able to balance it without having to
1996 : : * write pages from its LRU list.
1997 : : *
1998 : : * This may look like it could increase pressure on
1999 : : * lower zones by failing allocations in higher zones
2000 : : * before they are full. But the pages that do spill
2001 : : * over are limited as the lower zones are protected
2002 : : * by this very same mechanism. It should not become
2003 : : * a practical burden to them.
2004 : : *
2005 : : * XXX: For now, allow allocations to potentially
2006 : : * exceed the per-zone dirty limit in the slowpath
2007 : : * (ALLOC_WMARK_LOW unset) before going into reclaim,
2008 : : * which is important when on a NUMA setup the allowed
2009 : : * zones are together not big enough to reach the
2010 : : * global limit. The proper fix for these situations
2011 : : * will require awareness of zones in the
2012 : : * dirty-throttling and the flusher threads.
2013 : : */
2014 [ + + ][ + + ]: 29568385 : if ((alloc_flags & ALLOC_WMARK_LOW) &&
2015 [ + ]: 1634284 : (gfp_mask & __GFP_WRITE) && !zone_dirty_ok(zone))
2016 : : goto this_zone_full;
2017 : :
2018 : 29570026 : mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
2019 [ + + ]: 29568160 : if (!zone_watermark_ok(zone, order, mark,
2020 : : classzone_idx, alloc_flags)) {
2021 : : int ret;
2022 : :
2023 : : if (IS_ENABLED(CONFIG_NUMA) &&
2024 : : !did_zlc_setup && nr_online_nodes > 1) {
2025 : : /*
2026 : : * we do zlc_setup if there are multiple nodes
2027 : : * and before considering the first zone allowed
2028 : : * by the cpuset.
2029 : : */
2030 : : allowednodes = zlc_setup(zonelist, alloc_flags);
2031 : : zlc_active = 1;
2032 : : did_zlc_setup = 1;
2033 : : }
2034 : :
2035 : : if (zone_reclaim_mode == 0 ||
2036 : : !zone_allows_reclaim(preferred_zone, zone))
2037 : : goto this_zone_full;
2038 : :
2039 : : /*
2040 : : * As we may have just activated ZLC, check if the first
2041 : : * eligible zone has failed zone_reclaim recently.
2042 : : */
2043 : : if (IS_ENABLED(CONFIG_NUMA) && zlc_active &&
2044 : : !zlc_zone_worth_trying(zonelist, z, allowednodes))
2045 : : continue;
2046 : :
2047 : : ret = zone_reclaim(zone, gfp_mask, order);
2048 : : switch (ret) {
2049 : : case ZONE_RECLAIM_NOSCAN:
2050 : : /* did not scan */
2051 : : continue;
2052 : : case ZONE_RECLAIM_FULL:
2053 : : /* scanned but unreclaimable */
2054 : : continue;
2055 : : default:
2056 : : /* did we reclaim enough */
2057 : : if (zone_watermark_ok(zone, order, mark,
2058 : : classzone_idx, alloc_flags))
2059 : : goto try_this_zone;
2060 : :
2061 : : /*
2062 : : * Failed to reclaim enough to meet watermark.
2063 : : * Only mark the zone full if checking the min
2064 : : * watermark or if we failed to reclaim just
2065 : : * 1<<order pages or else the page allocator
2066 : : * fastpath will prematurely mark zones full
2067 : : * when the watermark is between the low and
2068 : : * min watermarks.
2069 : : */
2070 : : if (((alloc_flags & ALLOC_WMARK_MASK) == ALLOC_WMARK_MIN) ||
2071 : : ret == ZONE_RECLAIM_SOME)
2072 : : goto this_zone_full;
2073 : :
2074 : : continue;
2075 : : }
2076 : : }
2077 : :
2078 : : try_this_zone:
2079 : 29460881 : page = buffered_rmqueue(preferred_zone, zone, order,
2080 : : gfp_mask, migratetype);
2081 [ - + ]: 29460687 : if (page)
2082 : : break;
2083 : : this_zone_full:
2084 : : if (IS_ENABLED(CONFIG_NUMA))
2085 : : zlc_mark_zone_full(zonelist, z);
2086 : : }
2087 : :
2088 : : if (unlikely(IS_ENABLED(CONFIG_NUMA) && page == NULL && zlc_active)) {
2089 : : /* Disable zlc cache for second zonelist scan */
2090 : : zlc_active = 0;
2091 : : goto zonelist_scan;
2092 : : }
2093 : :
2094 [ + + ]: 29514018 : if (page)
2095 : : /*
2096 : : * page->pfmemalloc is set when ALLOC_NO_WATERMARKS was
2097 : : * necessary to allocate the page. The expectation is
2098 : : * that the caller is taking steps that will free more
2099 : : * memory. The caller should avoid the page being used
2100 : : * for !PFMEMALLOC purposes.
2101 : : */
2102 : 29460687 : page->pfmemalloc = !!(alloc_flags & ALLOC_NO_WATERMARKS);
2103 : :
2104 : 29514018 : return page;
2105 : : }
2106 : :
2107 : : /*
2108 : : * Large machines with many possible nodes should not always dump per-node
2109 : : * meminfo in irq context.
2110 : : */
2111 : : static inline bool should_suppress_show_mem(void)
2112 : : {
2113 : : bool ret = false;
2114 : :
2115 : : #if NODES_SHIFT > 8
2116 : : ret = in_interrupt();
2117 : : #endif
2118 : : return ret;
2119 : : }
2120 : :
2121 : : static DEFINE_RATELIMIT_STATE(nopage_rs,
2122 : : DEFAULT_RATELIMIT_INTERVAL,
2123 : : DEFAULT_RATELIMIT_BURST);
2124 : :
2125 : 0 : void warn_alloc_failed(gfp_t gfp_mask, int order, const char *fmt, ...)
2126 : : {
2127 : : unsigned int filter = SHOW_MEM_FILTER_NODES;
2128 : :
2129 [ + + ][ + - ]: 36 : if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs) ||
2130 : : debug_guardpage_minorder() > 0)
2131 : 36 : return;
2132 : :
2133 : : /*
2134 : : * This documents exceptions given to allocations in certain
2135 : : * contexts that are allowed to allocate outside current's set
2136 : : * of allowed nodes.
2137 : : */
2138 [ + - ]: 1 : if (!(gfp_mask & __GFP_NOMEMALLOC))
2139 [ - + ][ # # ]: 1 : if (test_thread_flag(TIF_MEMDIE) ||
2140 : 0 : (current->flags & (PF_MEMALLOC | PF_EXITING)))
2141 : : filter &= ~SHOW_MEM_FILTER_NODES;
2142 [ + - ][ - + ]: 1 : if (in_interrupt() || !(gfp_mask & __GFP_WAIT))
2143 : : filter &= ~SHOW_MEM_FILTER_NODES;
2144 : :
2145 [ - ]: 1 : if (fmt) {
2146 : : struct va_format vaf;
2147 : : va_list args;
2148 : :
2149 : 0 : va_start(args, fmt);
2150 : :
2151 : 0 : vaf.fmt = fmt;
2152 : 0 : vaf.va = &args;
2153 : :
2154 : 0 : pr_warn("%pV", &vaf);
2155 : :
2156 : 0 : va_end(args);
2157 : : }
2158 : :
2159 : 0 : pr_warn("%s: page allocation failure: order:%d, mode:0x%x\n",
2160 : : current->comm, order, gfp_mask);
2161 : :
2162 : 1 : dump_stack();
2163 : : if (!should_suppress_show_mem())
2164 : 1 : show_mem(filter);
2165 : : }
2166 : :
2167 : : static inline int
2168 : : should_alloc_retry(gfp_t gfp_mask, unsigned int order,
2169 : : unsigned long did_some_progress,
2170 : : unsigned long pages_reclaimed)
2171 : : {
2172 : : /* Do not loop if specifically requested */
2173 [ + + ]: 4748 : if (gfp_mask & __GFP_NORETRY)
2174 : : return 0;
2175 : :
2176 : : /* Always retry if specifically requested */
2177 [ + - ]: 4713 : if (gfp_mask & __GFP_NOFAIL)
2178 : : return 1;
2179 : :
2180 : : /*
2181 : : * Suspend converts GFP_KERNEL to __GFP_WAIT which can prevent reclaim
2182 : : * making forward progress without invoking OOM. Suspend also disables
2183 : : * storage devices so kswapd will not help. Bail if we are suspending.
2184 : : */
2185 [ - + ][ # # ]: 4713 : if (!did_some_progress && pm_suspended_storage())
2186 : : return 0;
2187 : :
2188 : : /*
2189 : : * In this implementation, order <= PAGE_ALLOC_COSTLY_ORDER
2190 : : * means __GFP_NOFAIL, but that may not be true in other
2191 : : * implementations.
2192 : : */
2193 [ - + ]: 4713 : if (order <= PAGE_ALLOC_COSTLY_ORDER)
2194 : : return 1;
2195 : :
2196 : : /*
2197 : : * For order > PAGE_ALLOC_COSTLY_ORDER, if __GFP_REPEAT is
2198 : : * specified, then we retry until we no longer reclaim any pages
2199 : : * (above), or we've reclaimed an order of pages at least as
2200 : : * large as the allocation's order. In both cases, if the
2201 : : * allocation still fails, we stop retrying.
2202 : : */
2203 [ # # ][ # # ]: 0 : if (gfp_mask & __GFP_REPEAT && pages_reclaimed < (1 << order))
2204 : : return 1;
2205 : :
2206 : : return 0;
2207 : : }
2208 : :
2209 : : static inline struct page *
2210 : : __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
2211 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2212 : : nodemask_t *nodemask, struct zone *preferred_zone,
2213 : : int migratetype)
2214 : : {
2215 : : struct page *page;
2216 : :
2217 : : /* Acquire the OOM killer lock for the zones in zonelist */
2218 [ + + ]: 905 : if (!try_set_zonelist_oom(zonelist, gfp_mask)) {
2219 : 332 : schedule_timeout_uninterruptible(1);
2220 : : return NULL;
2221 : : }
2222 : :
2223 : : /*
2224 : : * Go through the zonelist yet one more time, keep very high watermark
2225 : : * here, this is only to catch a parallel oom killing, we must fail if
2226 : : * we're still under heavy pressure.
2227 : : */
2228 : 573 : page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask,
2229 : : order, zonelist, high_zoneidx,
2230 : : ALLOC_WMARK_HIGH|ALLOC_CPUSET,
2231 : : preferred_zone, migratetype);
2232 [ + - ]: 573 : if (page)
2233 : : goto out;
2234 : :
2235 [ + - ]: 573 : if (!(gfp_mask & __GFP_NOFAIL)) {
2236 : : /* The OOM killer will not help higher order allocs */
2237 [ + - ]: 573 : if (order > PAGE_ALLOC_COSTLY_ORDER)
2238 : : goto out;
2239 : : /* The OOM killer does not needlessly kill tasks for lowmem */
2240 : : if (high_zoneidx < ZONE_NORMAL)
2241 : : goto out;
2242 : : /*
2243 : : * GFP_THISNODE contains __GFP_NORETRY and we never hit this.
2244 : : * Sanity check for bare calls of __GFP_THISNODE, not real OOM.
2245 : : * The caller should handle page allocation failure by itself if
2246 : : * it specifies __GFP_THISNODE.
2247 : : * Note: Hugepage uses it but will hit PAGE_ALLOC_COSTLY_ORDER.
2248 : : */
2249 [ + - ]: 573 : if (gfp_mask & __GFP_THISNODE)
2250 : : goto out;
2251 : : }
2252 : : /* Exhausted what can be done so it's blamo time */
2253 : 573 : out_of_memory(zonelist, gfp_mask, order, nodemask, false);
2254 : :
2255 : : out:
2256 : 573 : clear_zonelist_oom(zonelist, gfp_mask);
2257 : : return page;
2258 : : }
2259 : :
2260 : : #ifdef CONFIG_COMPACTION
2261 : : /* Try memory compaction for high-order allocations before reclaim */
2262 : : static struct page *
2263 : 0 : __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2264 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2265 : : nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2266 : : int migratetype, bool sync_migration,
2267 : : bool *contended_compaction, bool *deferred_compaction,
2268 : : unsigned long *did_some_progress)
2269 : : {
2270 [ - + ]: 5891 : if (!order)
2271 : : return NULL;
2272 : :
2273 [ # # ]: 0 : if (compaction_deferred(preferred_zone, order)) {
2274 : 0 : *deferred_compaction = true;
2275 : 0 : return NULL;
2276 : : }
2277 : :
2278 : 0 : current->flags |= PF_MEMALLOC;
2279 : 0 : *did_some_progress = try_to_compact_pages(zonelist, order, gfp_mask,
2280 : : nodemask, sync_migration,
2281 : : contended_compaction);
2282 : 0 : current->flags &= ~PF_MEMALLOC;
2283 : :
2284 [ # # ]: 0 : if (*did_some_progress != COMPACT_SKIPPED) {
2285 : : struct page *page;
2286 : :
2287 : : /* Page migration frees to the PCP lists but we want merging */
2288 : 0 : drain_pages(get_cpu());
2289 : 0 : put_cpu();
2290 : :
2291 : 0 : page = get_page_from_freelist(gfp_mask, nodemask,
2292 : : order, zonelist, high_zoneidx,
2293 : : alloc_flags & ~ALLOC_NO_WATERMARKS,
2294 : : preferred_zone, migratetype);
2295 [ # # ]: 0 : if (page) {
2296 : 0 : preferred_zone->compact_blockskip_flush = false;
2297 : : compaction_defer_reset(preferred_zone, order, true);
2298 : : count_vm_event(COMPACTSUCCESS);
2299 : 0 : return page;
2300 : : }
2301 : :
2302 : : /*
2303 : : * It's bad if compaction run occurs and fails.
2304 : : * The most likely reason is that pages exist,
2305 : : * but not enough to satisfy watermarks.
2306 : : */
2307 : : count_vm_event(COMPACTFAIL);
2308 : :
2309 : : /*
2310 : : * As async compaction considers a subset of pageblocks, only
2311 : : * defer if the failure was a sync compaction failure.
2312 : : */
2313 [ # # ]: 0 : if (sync_migration)
2314 : : defer_compaction(preferred_zone, order);
2315 : :
2316 : 0 : cond_resched();
2317 : : }
2318 : :
2319 : : return NULL;
2320 : : }
2321 : : #else
2322 : : static inline struct page *
2323 : : __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order,
2324 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2325 : : nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2326 : : int migratetype, bool sync_migration,
2327 : : bool *contended_compaction, bool *deferred_compaction,
2328 : : unsigned long *did_some_progress)
2329 : : {
2330 : : return NULL;
2331 : : }
2332 : : #endif /* CONFIG_COMPACTION */
2333 : :
2334 : : /* Perform direct synchronous page reclaim */
2335 : : static int
2336 : 0 : __perform_reclaim(gfp_t gfp_mask, unsigned int order, struct zonelist *zonelist,
2337 : : nodemask_t *nodemask)
2338 : : {
2339 : : struct reclaim_state reclaim_state;
2340 : : int progress;
2341 : :
2342 : 5852 : cond_resched();
2343 : :
2344 : : /* We now go into synchronous reclaim */
2345 : : cpuset_memory_pressure_bump();
2346 : 5855 : current->flags |= PF_MEMALLOC;
2347 : : lockdep_set_current_reclaim_state(gfp_mask);
2348 : 5855 : reclaim_state.reclaimed_slab = 0;
2349 : 5855 : current->reclaim_state = &reclaim_state;
2350 : :
2351 : 5855 : progress = try_to_free_pages(zonelist, order, gfp_mask, nodemask);
2352 : :
2353 : 5855 : current->reclaim_state = NULL;
2354 : : lockdep_clear_current_reclaim_state();
2355 : 5855 : current->flags &= ~PF_MEMALLOC;
2356 : :
2357 : 5855 : cond_resched();
2358 : :
2359 : 5856 : return progress;
2360 : : }
2361 : :
2362 : : /* The really slow allocator path where we enter direct reclaim */
2363 : : static inline struct page *
2364 : : __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order,
2365 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2366 : : nodemask_t *nodemask, int alloc_flags, struct zone *preferred_zone,
2367 : : int migratetype, unsigned long *did_some_progress)
2368 : : {
2369 : : struct page *page = NULL;
2370 : : bool drained = false;
2371 : :
2372 : 5847 : *did_some_progress = __perform_reclaim(gfp_mask, order, zonelist,
2373 : : nodemask);
2374 [ + + ]: 5856 : if (unlikely(!(*did_some_progress)))
2375 : : return NULL;
2376 : :
2377 : : /* After successful reclaim, reconsider all zones for allocation */
2378 : : if (IS_ENABLED(CONFIG_NUMA))
2379 : : zlc_clear_zones_full(zonelist);
2380 : :
2381 : : retry:
2382 : 9858 : page = get_page_from_freelist(gfp_mask, nodemask, order,
2383 : : zonelist, high_zoneidx,
2384 : : alloc_flags & ~ALLOC_NO_WATERMARKS,
2385 : : preferred_zone, migratetype);
2386 : :
2387 : : /*
2388 : : * If an allocation failed after direct reclaim, it could be because
2389 : : * pages are pinned on the per-cpu lists. Drain them and try again
2390 : : */
2391 [ + + ]: 9858 : if (!page && !drained) {
2392 : 4911 : drain_all_pages();
2393 : : drained = true;
2394 : : goto retry;
2395 : : }
2396 : :
2397 : : return page;
2398 : : }
2399 : :
2400 : : /*
2401 : : * This is called in the allocator slow-path if the allocation request is of
2402 : : * sufficient urgency to ignore watermarks and take other desperate measures
2403 : : */
2404 : : static inline struct page *
2405 : : __alloc_pages_high_priority(gfp_t gfp_mask, unsigned int order,
2406 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2407 : : nodemask_t *nodemask, struct zone *preferred_zone,
2408 : : int migratetype)
2409 : : {
2410 : : struct page *page;
2411 : :
2412 : : do {
2413 : 1 : page = get_page_from_freelist(gfp_mask, nodemask, order,
2414 : : zonelist, high_zoneidx, ALLOC_NO_WATERMARKS,
2415 : : preferred_zone, migratetype);
2416 : :
2417 [ - + ][ # # ]: 1 : if (!page && gfp_mask & __GFP_NOFAIL)
2418 : 0 : wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
2419 [ - + ][ # # ]: 1 : } while (!page && (gfp_mask & __GFP_NOFAIL));
2420 : :
2421 : : return page;
2422 : : }
2423 : :
2424 : 0 : static void prepare_slowpath(gfp_t gfp_mask, unsigned int order,
2425 : : struct zonelist *zonelist,
2426 : : enum zone_type high_zoneidx,
2427 : : struct zone *preferred_zone)
2428 : : {
2429 : : struct zoneref *z;
2430 : : struct zone *zone;
2431 : :
2432 [ + + ]: 107452 : for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
2433 [ + + ]: 69303 : if (!(gfp_mask & __GFP_NO_KSWAPD))
2434 : 69291 : wakeup_kswapd(zone, order, zone_idx(preferred_zone));
2435 : : /*
2436 : : * Only reset the batches of zones that were actually
2437 : : * considered in the fast path, we don't want to
2438 : : * thrash fairness information for zones that are not
2439 : : * actually part of this zonelist's round-robin cycle.
2440 : : */
2441 : : if (!zone_local(preferred_zone, zone))
2442 : : continue;
2443 : 69287 : mod_zone_page_state(zone, NR_ALLOC_BATCH,
2444 : 138574 : high_wmark_pages(zone) -
2445 : 138574 : low_wmark_pages(zone) -
2446 : : zone_page_state(zone, NR_ALLOC_BATCH));
2447 : : }
2448 : 38157 : }
2449 : :
2450 : : static inline int
2451 : : gfp_to_alloc_flags(gfp_t gfp_mask)
2452 : : {
2453 : : int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET;
2454 : 0 : const gfp_t wait = gfp_mask & __GFP_WAIT;
2455 : :
2456 : : /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */
2457 : : BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH);
2458 : :
2459 : : /*
2460 : : * The caller may dip into page reserves a bit more if the caller
2461 : : * cannot run direct reclaim, or if the caller has realtime scheduling
2462 : : * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will
2463 : : * set both ALLOC_HARDER (!wait) and ALLOC_HIGH (__GFP_HIGH).
2464 : : */
2465 : 38154 : alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH);
2466 : :
2467 [ + + ][ # # ]: 38154 : if (!wait) {
2468 : : /*
2469 : : * Not worth trying to allocate harder for
2470 : : * __GFP_NOMEMALLOC even if it can't schedule.
2471 : : */
2472 [ + + ][ # # ]: 3879 : if (!(gfp_mask & __GFP_NOMEMALLOC))
2473 : 2923 : alloc_flags |= ALLOC_HARDER;
2474 : : /*
2475 : : * Ignore cpuset if GFP_ATOMIC (!wait) rather than fail alloc.
2476 : : * See also cpuset_zone_allowed() comment in kernel/cpuset.c.
2477 : : */
2478 : 3879 : alloc_flags &= ~ALLOC_CPUSET;
2479 [ - + ][ # # ]: 34275 : } else if (unlikely(rt_task(current)) && !in_interrupt())
[ # # ][ # # ]
2480 : 0 : alloc_flags |= ALLOC_HARDER;
2481 : :
2482 [ + + ][ # # ]: 38154 : if (likely(!(gfp_mask & __GFP_NOMEMALLOC))) {
2483 [ - + ][ # # ]: 37176 : if (gfp_mask & __GFP_MEMALLOC)
2484 : 0 : alloc_flags |= ALLOC_NO_WATERMARKS;
2485 [ - + ][ # # ]: 37176 : else if (in_serving_softirq() && (current->flags & PF_MEMALLOC))
[ # # ][ # # ]
2486 : 0 : alloc_flags |= ALLOC_NO_WATERMARKS;
2487 [ + ][ + + ]: 37176 : else if (!in_interrupt() &&
[ # # ][ # # ]
2488 [ + + ][ # # ]: 37180 : ((current->flags & PF_MEMALLOC) ||
2489 : 37180 : unlikely(test_thread_flag(TIF_MEMDIE))))
2490 : 2 : alloc_flags |= ALLOC_NO_WATERMARKS;
2491 : : }
2492 : : #ifdef CONFIG_CMA
2493 : : if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2494 : : alloc_flags |= ALLOC_CMA;
2495 : : #endif
2496 : : return alloc_flags;
2497 : : }
2498 : :
2499 : 0 : bool gfp_pfmemalloc_allowed(gfp_t gfp_mask)
2500 : : {
2501 : 0 : return !!(gfp_to_alloc_flags(gfp_mask) & ALLOC_NO_WATERMARKS);
2502 : : }
2503 : :
2504 : : static inline struct page *
2505 : : __alloc_pages_slowpath(gfp_t gfp_mask, unsigned int order,
2506 : : struct zonelist *zonelist, enum zone_type high_zoneidx,
2507 : : nodemask_t *nodemask, struct zone *preferred_zone,
2508 : : int migratetype)
2509 : : {
2510 : 37243 : const gfp_t wait = gfp_mask & __GFP_WAIT;
2511 : : struct page *page = NULL;
2512 : : int alloc_flags;
2513 : : unsigned long pages_reclaimed = 0;
2514 : : unsigned long did_some_progress;
2515 : : bool sync_migration = false;
2516 : 37243 : bool deferred_compaction = false;
2517 : 37243 : bool contended_compaction = false;
2518 : :
2519 : : /*
2520 : : * In the slowpath, we sanity check order to avoid ever trying to
2521 : : * reclaim >= MAX_ORDER areas which will never succeed. Callers may
2522 : : * be using allocators in order of preference for an area that is
2523 : : * too large.
2524 : : */
2525 [ - + ]: 37243 : if (order >= MAX_ORDER) {
2526 [ # # ][ # # ]: 0 : WARN_ON_ONCE(!(gfp_mask & __GFP_NOWARN));
[ - ]
2527 : : return NULL;
2528 : : }
2529 : :
2530 : : /*
2531 : : * GFP_THISNODE (meaning __GFP_THISNODE, __GFP_NORETRY and
2532 : : * __GFP_NOWARN set) should not cause reclaim since the subsystem
2533 : : * (f.e. slab) using GFP_THISNODE may choose to trigger reclaim
2534 : : * using a larger set of nodes after it has established that the
2535 : : * allowed per node queues are empty and that nodes are
2536 : : * over allocated.
2537 : : */
2538 [ + ]: 38148 : if (gfp_thisnode_allocation(gfp_mask))
2539 : : goto nopage;
2540 : :
2541 : : restart:
2542 : 38145 : prepare_slowpath(gfp_mask, order, zonelist,
2543 : : high_zoneidx, preferred_zone);
2544 : :
2545 : : /*
2546 : : * OK, we're below the kswapd watermark and have kicked background
2547 : : * reclaim. Now things get more complex, so set up alloc_flags according
2548 : : * to how we want to proceed.
2549 : : */
2550 : : alloc_flags = gfp_to_alloc_flags(gfp_mask);
2551 : :
2552 : : /*
2553 : : * Find the true preferred zone if the allocation is unconstrained by
2554 : : * cpusets.
2555 : : */
2556 [ + + ][ + - ]: 38154 : if (!(alloc_flags & ALLOC_CPUSET) && !nodemask)
2557 : : first_zones_zonelist(zonelist, high_zoneidx, NULL,
2558 : : &preferred_zone);
2559 : :
2560 : : rebalance:
2561 : : /* This is the last chance, in general, before the goto nopage. */
2562 : 42859 : page = get_page_from_freelist(gfp_mask, nodemask, order, zonelist,
2563 : : high_zoneidx, alloc_flags & ~ALLOC_NO_WATERMARKS,
2564 : : preferred_zone, migratetype);
2565 [ + + ]: 42869 : if (page)
2566 : : goto got_pg;
2567 : :
2568 : : /* Allocate without watermarks if the context allows */
2569 [ + + ]: 5858 : if (alloc_flags & ALLOC_NO_WATERMARKS) {
2570 : : /*
2571 : : * Ignore mempolicies if ALLOC_NO_WATERMARKS on the grounds
2572 : : * the allocation is high priority and these type of
2573 : : * allocations are system rather than user orientated
2574 : : */
2575 : : zonelist = node_zonelist(numa_node_id(), gfp_mask);
2576 : :
2577 : 1 : page = __alloc_pages_high_priority(gfp_mask, order,
2578 : : zonelist, high_zoneidx, nodemask,
2579 : : preferred_zone, migratetype);
2580 [ - + ]: 1 : if (page) {
2581 : : goto got_pg;
2582 : : }
2583 : : }
2584 : :
2585 : : /* Atomic allocations - we can't balance anything */
2586 [ - + ]: 5857 : if (!wait) {
2587 : : /*
2588 : : * All existing users of the deprecated __GFP_NOFAIL are
2589 : : * blockable, so warn of any new users that actually allow this
2590 : : * type of allocation to fail.
2591 : : */
2592 [ # # ][ # # ]: 0 : WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL);
[ # # ]
2593 : : goto nopage;
2594 : : }
2595 : :
2596 : : /* Avoid recursion of direct reclaim */
2597 [ + - ]: 5857 : if (current->flags & PF_MEMALLOC)
2598 : : goto nopage;
2599 : :
2600 : : /* Avoid allocations with no watermarks from looping endlessly */
2601 [ + + ][ - + ]: 5857 : if (test_thread_flag(TIF_MEMDIE) && !(gfp_mask & __GFP_NOFAIL))
2602 : : goto nopage;
2603 : :
2604 : : /*
2605 : : * Try direct compaction. The first pass is asynchronous. Subsequent
2606 : : * attempts after direct reclaim are synchronous
2607 : : */
2608 : 5856 : page = __alloc_pages_direct_compact(gfp_mask, order,
2609 : : zonelist, high_zoneidx,
2610 : : nodemask,
2611 : : alloc_flags, preferred_zone,
2612 : : migratetype, sync_migration,
2613 : : &contended_compaction,
2614 : : &deferred_compaction,
2615 : : &did_some_progress);
2616 [ + - ]: 5847 : if (page)
2617 : : goto got_pg;
2618 : : sync_migration = true;
2619 : :
2620 : : /*
2621 : : * If compaction is deferred for high-order allocations, it is because
2622 : : * sync compaction recently failed. In this is the case and the caller
2623 : : * requested a movable allocation that does not heavily disrupt the
2624 : : * system then fail the allocation instead of entering direct reclaim.
2625 : : */
2626 [ + - ][ - + ]: 5847 : if ((deferred_compaction || contended_compaction) &&
[ # # ]
2627 : 0 : (gfp_mask & __GFP_NO_KSWAPD))
2628 : : goto nopage;
2629 : :
2630 : : /* Try direct reclaim and then allocating */
2631 : 5847 : page = __alloc_pages_direct_reclaim(gfp_mask, order,
2632 : : zonelist, high_zoneidx,
2633 : : nodemask,
2634 : : alloc_flags, preferred_zone,
2635 : : migratetype, &did_some_progress);
2636 [ + + ]: 5856 : if (page)
2637 : : goto got_pg;
2638 : :
2639 : : /*
2640 : : * If we failed to make any progress reclaiming, then we are
2641 : : * running out of options and have to consider going OOM
2642 : : */
2643 [ + + ]: 5653 : if (!did_some_progress) {
2644 [ + + ]: 909 : if (oom_gfp_allowed(gfp_mask)) {
2645 [ + - ]: 905 : if (oom_killer_disabled)
2646 : : goto nopage;
2647 : : /* Coredumps can quickly deplete all memory reserves */
2648 [ - + ][ # # ]: 905 : if ((current->flags & PF_DUMPCORE) &&
2649 : 0 : !(gfp_mask & __GFP_NOFAIL))
2650 : : goto nopage;
2651 : 905 : page = __alloc_pages_may_oom(gfp_mask, order,
2652 : : zonelist, high_zoneidx,
2653 : : nodemask, preferred_zone,
2654 : : migratetype);
2655 [ + - ]: 905 : if (page)
2656 : : goto got_pg;
2657 : :
2658 [ + + ]: 905 : if (!(gfp_mask & __GFP_NOFAIL)) {
2659 : : /*
2660 : : * The oom killer is not called for high-order
2661 : : * allocations that may fail, so if no progress
2662 : : * is being made, there are no other options and
2663 : : * retrying is unlikely to help.
2664 : : */
2665 [ + - ]: 904 : if (order > PAGE_ALLOC_COSTLY_ORDER)
2666 : : goto nopage;
2667 : : /*
2668 : : * The oom killer is not called for lowmem
2669 : : * allocations to prevent needlessly killing
2670 : : * innocent tasks.
2671 : : */
2672 : : if (high_zoneidx < ZONE_NORMAL)
2673 : : goto nopage;
2674 : : }
2675 : :
2676 : : goto restart;
2677 : : }
2678 : : }
2679 : :
2680 : : /* Check if we should retry the allocation */
2681 : 4748 : pages_reclaimed += did_some_progress;
2682 [ + + ]: 4748 : if (should_alloc_retry(gfp_mask, order, did_some_progress,
2683 : : pages_reclaimed)) {
2684 : : /* Wait for some write requests to complete then retry */
2685 : 4713 : wait_iff_congested(preferred_zone, BLK_RW_ASYNC, HZ/50);
2686 : : goto rebalance;
2687 : : } else {
2688 : : /*
2689 : : * High-order allocations do not necessarily loop after
2690 : : * direct reclaim and reclaim/compaction depends on compaction
2691 : : * being called after reclaim so call directly if necessary
2692 : : */
2693 : 35 : page = __alloc_pages_direct_compact(gfp_mask, order,
2694 : : zonelist, high_zoneidx,
2695 : : nodemask,
2696 : : alloc_flags, preferred_zone,
2697 : : migratetype, sync_migration,
2698 : : &contended_compaction,
2699 : : &deferred_compaction,
2700 : : &did_some_progress);
2701 [ + - ]: 35 : if (page)
2702 : : goto got_pg;
2703 : : }
2704 : :
2705 : : nopage:
2706 : 37237 : warn_alloc_failed(gfp_mask, order, NULL);
2707 : : return page;
2708 : : got_pg:
2709 : : if (kmemcheck_enabled)
2710 : : kmemcheck_pagealloc_alloc(page, order, gfp_mask);
2711 : :
2712 : : return page;
2713 : : }
2714 : :
2715 : : /*
2716 : : * This is the 'heart' of the zoned buddy allocator.
2717 : : */
2718 : : struct page *
2719 : 0 : __alloc_pages_nodemask(gfp_t gfp_mask, unsigned int order,
2720 : : struct zonelist *zonelist, nodemask_t *nodemask)
2721 : : {
2722 : : enum zone_type high_zoneidx = gfp_zone(gfp_mask);
2723 : : struct zone *preferred_zone;
2724 : : struct page *page = NULL;
2725 : : int migratetype = allocflags_to_migratetype(gfp_mask);
2726 : : unsigned int cpuset_mems_cookie;
2727 : : int alloc_flags = ALLOC_WMARK_LOW|ALLOC_CPUSET;
2728 : : struct mem_cgroup *memcg = NULL;
2729 : :
2730 : 29462096 : gfp_mask &= gfp_allowed_mask;
2731 : :
2732 : : lockdep_trace_alloc(gfp_mask);
2733 : :
2734 : : might_sleep_if(gfp_mask & __GFP_WAIT);
2735 : :
2736 : : if (should_fail_alloc_page(gfp_mask, order))
2737 : : return NULL;
2738 : :
2739 : : /*
2740 : : * Check the zones suitable for the gfp_mask contain at least one
2741 : : * valid zone. It's possible to have an empty zonelist as a result
2742 : : * of GFP_THISNODE and a memoryless node
2743 : : */
2744 [ + - ]: 29462096 : if (unlikely(!zonelist->_zonerefs->zone))
2745 : : return NULL;
2746 : :
2747 : : /*
2748 : : * Will only have any effect when __GFP_KMEMCG is set. This is
2749 : : * verified in the (always inline) callee
2750 : : */
2751 : : if (!memcg_kmem_newpage_charge(gfp_mask, &memcg, order))
2752 : : return NULL;
2753 : :
2754 : : retry_cpuset:
2755 : : cpuset_mems_cookie = get_mems_allowed();
2756 : :
2757 : : /* The preferred zone is used for statistics later */
2758 [ - + ]: 29462096 : first_zones_zonelist(zonelist, high_zoneidx,
2759 : : nodemask ? : &cpuset_current_mems_allowed,
2760 : : &preferred_zone);
2761 [ + ]: 29460169 : if (!preferred_zone)
2762 : : goto out;
2763 : :
2764 : : #ifdef CONFIG_CMA
2765 : : if (allocflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE)
2766 : : alloc_flags |= ALLOC_CMA;
2767 : : #endif
2768 : : /* First allocation attempt */
2769 : 29460214 : page = get_page_from_freelist(gfp_mask|__GFP_HARDWALL, nodemask, order,
2770 : : zonelist, high_zoneidx, alloc_flags,
2771 : : preferred_zone, migratetype);
2772 [ + + ]: 29461182 : if (unlikely(!page)) {
2773 : : /*
2774 : : * Runtime PM, block IO and its error handling path
2775 : : * can deadlock because I/O on the device might not
2776 : : * complete.
2777 : : */
2778 : : gfp_mask = memalloc_noio_flags(gfp_mask);
2779 : 37243 : page = __alloc_pages_slowpath(gfp_mask, order,
2780 : : zonelist, high_zoneidx, nodemask,
2781 : : preferred_zone, migratetype);
2782 : : }
2783 : :
2784 : : trace_mm_page_alloc(page, order, gfp_mask, migratetype);
2785 : :
2786 : : out:
2787 : : /*
2788 : : * When updating a task's mems_allowed, it is possible to race with
2789 : : * parallel threads in such a way that an allocation can fail while
2790 : : * the mask is being updated. If a page allocation is about to fail,
2791 : : * check if the cpuset changed during allocation and if so, retry.
2792 : : */
2793 : : if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !page))
2794 : : goto retry_cpuset;
2795 : :
2796 : : memcg_kmem_commit_charge(page, memcg, order);
2797 : :
2798 : 29459624 : return page;
2799 : : }
2800 : : EXPORT_SYMBOL(__alloc_pages_nodemask);
2801 : :
2802 : : /*
2803 : : * Common helper functions.
2804 : : */
2805 : 0 : unsigned long __get_free_pages(gfp_t gfp_mask, unsigned int order)
2806 : : {
2807 : : struct page *page;
2808 : :
2809 : : /*
2810 : : * __get_free_pages() returns a 32-bit address, which cannot represent
2811 : : * a highmem page
2812 : : */
2813 : : VM_BUG_ON((gfp_mask & __GFP_HIGHMEM) != 0);
2814 : :
2815 : : page = alloc_pages(gfp_mask, order);
2816 [ + ]: 2706755 : if (!page)
2817 : : return 0;
2818 : 2706756 : return (unsigned long) page_address(page);
2819 : : }
2820 : : EXPORT_SYMBOL(__get_free_pages);
2821 : :
2822 : 0 : unsigned long get_zeroed_page(gfp_t gfp_mask)
2823 : : {
2824 : 466 : return __get_free_pages(gfp_mask | __GFP_ZERO, 0);
2825 : : }
2826 : : EXPORT_SYMBOL(get_zeroed_page);
2827 : :
2828 : 0 : void __free_pages(struct page *page, unsigned int order)
2829 : : {
2830 [ + ]: 6799644 : if (put_page_testzero(page)) {
2831 [ + + ]: 6800013 : if (order == 0)
2832 : 4566596 : free_hot_cold_page(page, 0);
2833 : : else
2834 : 2233417 : __free_pages_ok(page, order);
2835 : : }
2836 : 900 : }
2837 : :
2838 : : EXPORT_SYMBOL(__free_pages);
2839 : :
2840 : 0 : void free_pages(unsigned long addr, unsigned int order)
2841 : : {
2842 [ + + ]: 2712825 : if (addr != 0) {
2843 : : VM_BUG_ON(!virt_addr_valid((void *)addr));
2844 : 2706713 : __free_pages(virt_to_page((void *)addr), order);
2845 : : }
2846 : 0 : }
2847 : :
2848 : : EXPORT_SYMBOL(free_pages);
2849 : :
2850 : : /*
2851 : : * __free_memcg_kmem_pages and free_memcg_kmem_pages will free
2852 : : * pages allocated with __GFP_KMEMCG.
2853 : : *
2854 : : * Those pages are accounted to a particular memcg, embedded in the
2855 : : * corresponding page_cgroup. To avoid adding a hit in the allocator to search
2856 : : * for that information only to find out that it is NULL for users who have no
2857 : : * interest in that whatsoever, we provide these functions.
2858 : : *
2859 : : * The caller knows better which flags it relies on.
2860 : : */
2861 : 0 : void __free_memcg_kmem_pages(struct page *page, unsigned int order)
2862 : : {
2863 : : memcg_kmem_uncharge_pages(page, order);
2864 : 1382669 : __free_pages(page, order);
2865 : 278930 : }
2866 : :
2867 : 0 : void free_memcg_kmem_pages(unsigned long addr, unsigned int order)
2868 : : {
2869 [ + ]: 1103532 : if (addr != 0) {
2870 : : VM_BUG_ON(!virt_addr_valid((void *)addr));
2871 : 2207444 : __free_memcg_kmem_pages(virt_to_page((void *)addr), order);
2872 : : }
2873 : 511 : }
2874 : :
2875 : 0 : static void *make_alloc_exact(unsigned long addr, unsigned order, size_t size)
2876 : : {
2877 [ # # ]: 0 : if (addr) {
2878 : 0 : unsigned long alloc_end = addr + (PAGE_SIZE << order);
2879 : 0 : unsigned long used = addr + PAGE_ALIGN(size);
2880 : :
2881 : 0 : split_page(virt_to_page((void *)addr), order);
2882 [ # # ]: 0 : while (used < alloc_end) {
2883 : 0 : free_page(used);
2884 : 0 : used += PAGE_SIZE;
2885 : : }
2886 : : }
2887 : 0 : return (void *)addr;
2888 : : }
2889 : :
2890 : : /**
2891 : : * alloc_pages_exact - allocate an exact number physically-contiguous pages.
2892 : : * @size: the number of bytes to allocate
2893 : : * @gfp_mask: GFP flags for the allocation
2894 : : *
2895 : : * This function is similar to alloc_pages(), except that it allocates the
2896 : : * minimum number of pages to satisfy the request. alloc_pages() can only
2897 : : * allocate memory in power-of-two pages.
2898 : : *
2899 : : * This function is also limited by MAX_ORDER.
2900 : : *
2901 : : * Memory allocated by this function must be released by free_pages_exact().
2902 : : */
2903 : 0 : void *alloc_pages_exact(size_t size, gfp_t gfp_mask)
2904 : : {
2905 [ # # ][ # # ]: 0 : unsigned int order = get_order(size);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
2906 : : unsigned long addr;
2907 : :
2908 : 0 : addr = __get_free_pages(gfp_mask, order);
2909 : 0 : return make_alloc_exact(addr, order, size);
2910 : : }
2911 : : EXPORT_SYMBOL(alloc_pages_exact);
2912 : :
2913 : : /**
2914 : : * alloc_pages_exact_nid - allocate an exact number of physically-contiguous
2915 : : * pages on a node.
2916 : : * @nid: the preferred node ID where memory should be allocated
2917 : : * @size: the number of bytes to allocate
2918 : : * @gfp_mask: GFP flags for the allocation
2919 : : *
2920 : : * Like alloc_pages_exact(), but try to allocate on node nid first before falling
2921 : : * back.
2922 : : * Note this is not alloc_pages_exact_node() which allocates on a specific node,
2923 : : * but is not exact.
2924 : : */
2925 : 0 : void *alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)
2926 : : {
2927 [ # # ][ # # ]: 0 : unsigned order = get_order(size);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
2928 : : struct page *p = alloc_pages_node(nid, gfp_mask, order);
2929 [ # # ]: 0 : if (!p)
2930 : : return NULL;
2931 : 0 : return make_alloc_exact((unsigned long)page_address(p), order, size);
2932 : : }
2933 : : EXPORT_SYMBOL(alloc_pages_exact_nid);
2934 : :
2935 : : /**
2936 : : * free_pages_exact - release memory allocated via alloc_pages_exact()
2937 : : * @virt: the value returned by alloc_pages_exact.
2938 : : * @size: size of allocation, same value as passed to alloc_pages_exact().
2939 : : *
2940 : : * Release the memory allocated by a previous call to alloc_pages_exact.
2941 : : */
2942 : 0 : void free_pages_exact(void *virt, size_t size)
2943 : : {
2944 : 0 : unsigned long addr = (unsigned long)virt;
2945 : 0 : unsigned long end = addr + PAGE_ALIGN(size);
2946 : :
2947 [ # # ]: 0 : while (addr < end) {
2948 : 0 : free_page(addr);
2949 : 0 : addr += PAGE_SIZE;
2950 : : }
2951 : 0 : }
2952 : : EXPORT_SYMBOL(free_pages_exact);
2953 : :
2954 : : /**
2955 : : * nr_free_zone_pages - count number of pages beyond high watermark
2956 : : * @offset: The zone index of the highest zone
2957 : : *
2958 : : * nr_free_zone_pages() counts the number of counts pages which are beyond the
2959 : : * high watermark within all zones at or below a given zone index. For each
2960 : : * zone, the number of pages is calculated as:
2961 : : * managed_pages - high_pages
2962 : : */
2963 : 0 : static unsigned long nr_free_zone_pages(int offset)
2964 : : {
2965 : : struct zoneref *z;
2966 : : struct zone *zone;
2967 : :
2968 : : /* Just pick one node, since fallback list is circular */
2969 : : unsigned long sum = 0;
2970 : :
2971 : : struct zonelist *zonelist = node_zonelist(numa_node_id(), GFP_KERNEL);
2972 : :
2973 [ # # ]: 0 : for_each_zone_zonelist(zone, z, zonelist, offset) {
2974 : 0 : unsigned long size = zone->managed_pages;
2975 : 0 : unsigned long high = high_wmark_pages(zone);
2976 [ # # ]: 0 : if (size > high)
2977 : 0 : sum += size - high;
2978 : : }
2979 : :
2980 : 0 : return sum;
2981 : : }
2982 : :
2983 : : /**
2984 : : * nr_free_buffer_pages - count number of pages beyond high watermark
2985 : : *
2986 : : * nr_free_buffer_pages() counts the number of pages which are beyond the high
2987 : : * watermark within ZONE_DMA and ZONE_NORMAL.
2988 : : */
2989 : 0 : unsigned long nr_free_buffer_pages(void)
2990 : : {
2991 : 0 : return nr_free_zone_pages(gfp_zone(GFP_USER));
2992 : : }
2993 : : EXPORT_SYMBOL_GPL(nr_free_buffer_pages);
2994 : :
2995 : : /**
2996 : : * nr_free_pagecache_pages - count number of pages beyond high watermark
2997 : : *
2998 : : * nr_free_pagecache_pages() counts the number of pages which are beyond the
2999 : : * high watermark within all zones.
3000 : : */
3001 : 0 : unsigned long nr_free_pagecache_pages(void)
3002 : : {
3003 : 0 : return nr_free_zone_pages(gfp_zone(GFP_HIGHUSER_MOVABLE));
3004 : : }
3005 : :
3006 : : static inline void show_node(struct zone *zone)
3007 : : {
3008 : : if (IS_ENABLED(CONFIG_NUMA))
3009 : : printk("Node %d ", zone_to_nid(zone));
3010 : : }
3011 : :
3012 : 0 : void si_meminfo(struct sysinfo *val)
3013 : : {
3014 : 604 : val->totalram = totalram_pages;
3015 : 604 : val->sharedram = 0;
3016 : 0 : val->freeram = global_page_state(NR_FREE_PAGES);
3017 : 604 : val->bufferram = nr_blockdev_pages();
3018 : 604 : val->totalhigh = totalhigh_pages;
3019 : 604 : val->freehigh = nr_free_highpages();
3020 : 604 : val->mem_unit = PAGE_SIZE;
3021 : 604 : }
3022 : :
3023 : : EXPORT_SYMBOL(si_meminfo);
3024 : :
3025 : : #ifdef CONFIG_NUMA
3026 : : void si_meminfo_node(struct sysinfo *val, int nid)
3027 : : {
3028 : : int zone_type; /* needs to be signed */
3029 : : unsigned long managed_pages = 0;
3030 : : pg_data_t *pgdat = NODE_DATA(nid);
3031 : :
3032 : : for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++)
3033 : : managed_pages += pgdat->node_zones[zone_type].managed_pages;
3034 : : val->totalram = managed_pages;
3035 : : val->freeram = node_page_state(nid, NR_FREE_PAGES);
3036 : : #ifdef CONFIG_HIGHMEM
3037 : : val->totalhigh = pgdat->node_zones[ZONE_HIGHMEM].managed_pages;
3038 : : val->freehigh = zone_page_state(&pgdat->node_zones[ZONE_HIGHMEM],
3039 : : NR_FREE_PAGES);
3040 : : #else
3041 : : val->totalhigh = 0;
3042 : : val->freehigh = 0;
3043 : : #endif
3044 : : val->mem_unit = PAGE_SIZE;
3045 : : }
3046 : : #endif
3047 : :
3048 : : /*
3049 : : * Determine whether the node should be displayed or not, depending on whether
3050 : : * SHOW_MEM_FILTER_NODES was passed to show_free_areas().
3051 : : */
3052 : 0 : bool skip_free_areas_node(unsigned int flags, int nid)
3053 : : {
3054 : : bool ret = false;
3055 : : unsigned int cpuset_mems_cookie;
3056 : :
3057 [ + + ][ + + ]: 24 : if (!(flags & SHOW_MEM_FILTER_NODES))
[ + + ][ # # ]
3058 : : goto out;
3059 : :
3060 : : do {
3061 : : cpuset_mems_cookie = get_mems_allowed();
3062 : 18 : ret = !node_isset(nid, cpuset_current_mems_allowed);
3063 : : } while (!put_mems_allowed(cpuset_mems_cookie));
3064 : : out:
3065 : 0 : return ret;
3066 : : }
3067 : :
3068 : : #define K(x) ((x) << (PAGE_SHIFT-10))
3069 : :
3070 : 0 : static void show_migration_types(unsigned char type)
3071 : : {
3072 : : static const char types[MIGRATE_TYPES] = {
3073 : : [MIGRATE_UNMOVABLE] = 'U',
3074 : : [MIGRATE_RECLAIMABLE] = 'E',
3075 : : [MIGRATE_MOVABLE] = 'M',
3076 : : [MIGRATE_RESERVE] = 'R',
3077 : : #ifdef CONFIG_CMA
3078 : : [MIGRATE_CMA] = 'C',
3079 : : #endif
3080 : : #ifdef CONFIG_MEMORY_ISOLATION
3081 : : [MIGRATE_ISOLATE] = 'I',
3082 : : #endif
3083 : : };
3084 : : char tmp[MIGRATE_TYPES + 1];
3085 : : char *p = tmp;
3086 : : int i;
3087 : :
3088 [ + + ]: 325 : for (i = 0; i < MIGRATE_TYPES; i++) {
3089 [ + + ]: 260 : if (type & (1 << i))
3090 : 137 : *p++ = types[i];
3091 : : }
3092 : :
3093 : 65 : *p = '\0';
3094 : 65 : printk("(%s) ", tmp);
3095 : 65 : }
3096 : :
3097 : : /*
3098 : : * Show free area list (used inside shift_scroll-lock stuff)
3099 : : * We also calculate the percentage fragmentation. We do this by counting the
3100 : : * memory on each free list with the exception of the first item on the list.
3101 : : * Suppresses nodes that are not allowed by current's cpuset if
3102 : : * SHOW_MEM_FILTER_NODES is passed.
3103 : : */
3104 : 0 : void show_free_areas(unsigned int filter)
3105 : : {
3106 : : int cpu;
3107 : 36 : struct zone *zone;
3108 : :
3109 [ + + ][ + + ]: 16 : for_each_populated_zone(zone) {
3110 [ - + ]: 8 : if (skip_free_areas_node(filter, zone_to_nid(zone)))
3111 : 0 : continue;
3112 : : show_node(zone);
3113 : 8 : printk("%s per-cpu:\n", zone->name);
3114 : :
3115 [ + + ]: 56 : for_each_online_cpu(cpu) {
3116 : : struct per_cpu_pageset *pageset;
3117 : :
3118 : 40 : pageset = per_cpu_ptr(zone->pageset, cpu);
3119 : :
3120 : 40 : printk("CPU %4d: hi:%5d, btch:%4d usd:%4d\n",
3121 : : cpu, pageset->pcp.high,
3122 : : pageset->pcp.batch, pageset->pcp.count);
3123 : : }
3124 : : }
3125 : :
3126 : 4 : printk("active_anon:%lu inactive_anon:%lu isolated_anon:%lu\n"
3127 : : " active_file:%lu inactive_file:%lu isolated_file:%lu\n"
3128 : : " unevictable:%lu"
3129 : : " dirty:%lu writeback:%lu unstable:%lu\n"
3130 : : " free:%lu slab_reclaimable:%lu slab_unreclaimable:%lu\n"
3131 : : " mapped:%lu shmem:%lu pagetables:%lu bounce:%lu\n"
3132 : : " free_cma:%lu\n",
3133 : : global_page_state(NR_ACTIVE_ANON),
3134 : : global_page_state(NR_INACTIVE_ANON),
3135 : : global_page_state(NR_ISOLATED_ANON),
3136 : : global_page_state(NR_ACTIVE_FILE),
3137 : : global_page_state(NR_INACTIVE_FILE),
3138 : : global_page_state(NR_ISOLATED_FILE),
3139 : : global_page_state(NR_UNEVICTABLE),
3140 : : global_page_state(NR_FILE_DIRTY),
3141 : : global_page_state(NR_WRITEBACK),
3142 : : global_page_state(NR_UNSTABLE_NFS),
3143 : : global_page_state(NR_FREE_PAGES),
3144 : : global_page_state(NR_SLAB_RECLAIMABLE),
3145 : : global_page_state(NR_SLAB_UNRECLAIMABLE),
3146 : : global_page_state(NR_FILE_MAPPED),
3147 : : global_page_state(NR_SHMEM),
3148 : : global_page_state(NR_PAGETABLE),
3149 : : global_page_state(NR_BOUNCE),
3150 : : global_page_state(NR_FREE_CMA_PAGES));
3151 : :
3152 [ + + ][ + + ]: 16 : for_each_populated_zone(zone) {
3153 : : int i;
3154 : :
3155 [ - + ]: 8 : if (skip_free_areas_node(filter, zone_to_nid(zone)))
3156 : 0 : continue;
3157 : : show_node(zone);
3158 [ - + ]: 8 : printk("%s"
3159 : : " free:%lukB"
3160 : : " min:%lukB"
3161 : : " low:%lukB"
3162 : : " high:%lukB"
3163 : : " active_anon:%lukB"
3164 : : " inactive_anon:%lukB"
3165 : : " active_file:%lukB"
3166 : : " inactive_file:%lukB"
3167 : : " unevictable:%lukB"
3168 : : " isolated(anon):%lukB"
3169 : : " isolated(file):%lukB"
3170 : : " present:%lukB"
3171 : : " managed:%lukB"
3172 : : " mlocked:%lukB"
3173 : : " dirty:%lukB"
3174 : : " writeback:%lukB"
3175 : : " mapped:%lukB"
3176 : : " shmem:%lukB"
3177 : : " slab_reclaimable:%lukB"
3178 : : " slab_unreclaimable:%lukB"
3179 : : " kernel_stack:%lukB"
3180 : : " pagetables:%lukB"
3181 : : " unstable:%lukB"
3182 : : " bounce:%lukB"
3183 : : " free_cma:%lukB"
3184 : : " writeback_tmp:%lukB"
3185 : : " pages_scanned:%lu"
3186 : : " all_unreclaimable? %s"
3187 : : "\n",
3188 : : zone->name,
3189 : : K(zone_page_state(zone, NR_FREE_PAGES)),
3190 : 8 : K(min_wmark_pages(zone)),
3191 : 8 : K(low_wmark_pages(zone)),
3192 : 8 : K(high_wmark_pages(zone)),
3193 : : K(zone_page_state(zone, NR_ACTIVE_ANON)),
3194 : : K(zone_page_state(zone, NR_INACTIVE_ANON)),
3195 : : K(zone_page_state(zone, NR_ACTIVE_FILE)),
3196 : : K(zone_page_state(zone, NR_INACTIVE_FILE)),
3197 : : K(zone_page_state(zone, NR_UNEVICTABLE)),
3198 : : K(zone_page_state(zone, NR_ISOLATED_ANON)),
3199 : : K(zone_page_state(zone, NR_ISOLATED_FILE)),
3200 : : K(zone->present_pages),
3201 : 8 : K(zone->managed_pages),
3202 : : K(zone_page_state(zone, NR_MLOCK)),
3203 : : K(zone_page_state(zone, NR_FILE_DIRTY)),
3204 : : K(zone_page_state(zone, NR_WRITEBACK)),
3205 : : K(zone_page_state(zone, NR_FILE_MAPPED)),
3206 : : K(zone_page_state(zone, NR_SHMEM)),
3207 : : K(zone_page_state(zone, NR_SLAB_RECLAIMABLE)),
3208 : : K(zone_page_state(zone, NR_SLAB_UNRECLAIMABLE)),
3209 : 8 : zone_page_state(zone, NR_KERNEL_STACK) *
3210 : : THREAD_SIZE / 1024,
3211 : : K(zone_page_state(zone, NR_PAGETABLE)),
3212 : : K(zone_page_state(zone, NR_UNSTABLE_NFS)),
3213 : : K(zone_page_state(zone, NR_BOUNCE)),
3214 : : K(zone_page_state(zone, NR_FREE_CMA_PAGES)),
3215 : : K(zone_page_state(zone, NR_WRITEBACK_TEMP)),
3216 : : zone->pages_scanned,
3217 : 4 : (!zone_reclaimable(zone) ? "yes" : "no")
3218 : : );
3219 : 8 : printk("lowmem_reserve[]:");
3220 [ + + ]: 32 : for (i = 0; i < MAX_NR_ZONES; i++)
3221 : 24 : printk(" %lu", zone->lowmem_reserve[i]);
3222 : 8 : printk("\n");
3223 : : }
3224 : :
3225 [ + + ][ + + ]: 16 : for_each_populated_zone(zone) {
3226 : : unsigned long nr[MAX_ORDER], flags, order, total = 0;
3227 : : unsigned char types[MAX_ORDER];
3228 : :
3229 [ - + ]: 8 : if (skip_free_areas_node(filter, zone_to_nid(zone)))
3230 : 0 : continue;
3231 : : show_node(zone);
3232 : 8 : printk("%s: ", zone->name);
3233 : :
3234 : 8 : spin_lock_irqsave(&zone->lock, flags);
3235 [ + + ]: 96 : for (order = 0; order < MAX_ORDER; order++) {
3236 : 88 : struct free_area *area = &zone->free_area[order];
3237 : : int type;
3238 : :
3239 : 88 : nr[order] = area->nr_free;
3240 : 88 : total += nr[order] << order;
3241 : :
3242 : 88 : types[order] = 0;
3243 [ + + ]: 440 : for (type = 0; type < MIGRATE_TYPES; type++) {
3244 [ + + ]: 352 : if (!list_empty(&area->free_list[type]))
3245 : 137 : types[order] |= 1 << type;
3246 : : }
3247 : : }
3248 : : spin_unlock_irqrestore(&zone->lock, flags);
3249 [ + + ]: 96 : for (order = 0; order < MAX_ORDER; order++) {
3250 : 88 : printk("%lu*%lukB ", nr[order], K(1UL) << order);
3251 [ + + ]: 88 : if (nr[order])
3252 : 65 : show_migration_types(types[order]);
3253 : : }
3254 : 8 : printk("= %lukB\n", K(total));
3255 : : }
3256 : :
3257 : : hugetlb_show_meminfo();
3258 : :
3259 : 4 : printk("%ld total pagecache pages\n", global_page_state(NR_FILE_PAGES));
3260 : :
3261 : 4 : show_swap_cache_info();
3262 : 4 : }
3263 : :
3264 : : static void zoneref_set_zone(struct zone *zone, struct zoneref *zoneref)
3265 : : {
3266 : 0 : zoneref->zone = zone;
3267 : 0 : zoneref->zone_idx = zone_idx(zone);
3268 : : }
3269 : :
3270 : : /*
3271 : : * Builds allocation fallback zone lists.
3272 : : *
3273 : : * Add all populated zones of a node to the zonelist.
3274 : : */
3275 : : static int build_zonelists_node(pg_data_t *pgdat, struct zonelist *zonelist,
3276 : : int nr_zones)
3277 : : {
3278 : 0 : struct zone *zone;
3279 : : enum zone_type zone_type = MAX_NR_ZONES;
3280 : :
3281 : : do {
3282 : 0 : zone_type--;
3283 : 0 : zone = pgdat->node_zones + zone_type;
3284 [ # # ][ # # ]: 0 : if (populated_zone(zone)) {
[ # # ]
3285 : 0 : zoneref_set_zone(zone,
3286 : 0 : &zonelist->_zonerefs[nr_zones++]);
3287 : : check_highest_zone(zone_type);
3288 : : }
3289 [ # # ][ # # ]: 0 : } while (zone_type);
[ # # ]
3290 : :
3291 : : return nr_zones;
3292 : : }
3293 : :
3294 : :
3295 : : /*
3296 : : * zonelist_order:
3297 : : * 0 = automatic detection of better ordering.
3298 : : * 1 = order by ([node] distance, -zonetype)
3299 : : * 2 = order by (-zonetype, [node] distance)
3300 : : *
3301 : : * If not NUMA, ZONELIST_ORDER_ZONE and ZONELIST_ORDER_NODE will create
3302 : : * the same zonelist. So only NUMA can configure this param.
3303 : : */
3304 : : #define ZONELIST_ORDER_DEFAULT 0
3305 : : #define ZONELIST_ORDER_NODE 1
3306 : : #define ZONELIST_ORDER_ZONE 2
3307 : :
3308 : : /* zonelist order in the kernel.
3309 : : * set_zonelist_order() will set this to NODE or ZONE.
3310 : : */
3311 : : static int current_zonelist_order = ZONELIST_ORDER_DEFAULT;
3312 : : static char zonelist_order_name[3][8] = {"Default", "Node", "Zone"};
3313 : :
3314 : :
3315 : : #ifdef CONFIG_NUMA
3316 : : /* The value user specified ....changed by config */
3317 : : static int user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3318 : : /* string for sysctl */
3319 : : #define NUMA_ZONELIST_ORDER_LEN 16
3320 : : char numa_zonelist_order[16] = "default";
3321 : :
3322 : : /*
3323 : : * interface for configure zonelist ordering.
3324 : : * command line option "numa_zonelist_order"
3325 : : * = "[dD]efault - default, automatic configuration.
3326 : : * = "[nN]ode - order by node locality, then by zone within node
3327 : : * = "[zZ]one - order by zone, then by locality within zone
3328 : : */
3329 : :
3330 : : static int __parse_numa_zonelist_order(char *s)
3331 : : {
3332 : : if (*s == 'd' || *s == 'D') {
3333 : : user_zonelist_order = ZONELIST_ORDER_DEFAULT;
3334 : : } else if (*s == 'n' || *s == 'N') {
3335 : : user_zonelist_order = ZONELIST_ORDER_NODE;
3336 : : } else if (*s == 'z' || *s == 'Z') {
3337 : : user_zonelist_order = ZONELIST_ORDER_ZONE;
3338 : : } else {
3339 : : printk(KERN_WARNING
3340 : : "Ignoring invalid numa_zonelist_order value: "
3341 : : "%s\n", s);
3342 : : return -EINVAL;
3343 : : }
3344 : : return 0;
3345 : : }
3346 : :
3347 : : static __init int setup_numa_zonelist_order(char *s)
3348 : : {
3349 : : int ret;
3350 : :
3351 : : if (!s)
3352 : : return 0;
3353 : :
3354 : : ret = __parse_numa_zonelist_order(s);
3355 : : if (ret == 0)
3356 : : strlcpy(numa_zonelist_order, s, NUMA_ZONELIST_ORDER_LEN);
3357 : :
3358 : : return ret;
3359 : : }
3360 : : early_param("numa_zonelist_order", setup_numa_zonelist_order);
3361 : :
3362 : : /*
3363 : : * sysctl handler for numa_zonelist_order
3364 : : */
3365 : : int numa_zonelist_order_handler(ctl_table *table, int write,
3366 : : void __user *buffer, size_t *length,
3367 : : loff_t *ppos)
3368 : : {
3369 : : char saved_string[NUMA_ZONELIST_ORDER_LEN];
3370 : : int ret;
3371 : : static DEFINE_MUTEX(zl_order_mutex);
3372 : :
3373 : : mutex_lock(&zl_order_mutex);
3374 : : if (write) {
3375 : : if (strlen((char *)table->data) >= NUMA_ZONELIST_ORDER_LEN) {
3376 : : ret = -EINVAL;
3377 : : goto out;
3378 : : }
3379 : : strcpy(saved_string, (char *)table->data);
3380 : : }
3381 : : ret = proc_dostring(table, write, buffer, length, ppos);
3382 : : if (ret)
3383 : : goto out;
3384 : : if (write) {
3385 : : int oldval = user_zonelist_order;
3386 : :
3387 : : ret = __parse_numa_zonelist_order((char *)table->data);
3388 : : if (ret) {
3389 : : /*
3390 : : * bogus value. restore saved string
3391 : : */
3392 : : strncpy((char *)table->data, saved_string,
3393 : : NUMA_ZONELIST_ORDER_LEN);
3394 : : user_zonelist_order = oldval;
3395 : : } else if (oldval != user_zonelist_order) {
3396 : : mutex_lock(&zonelists_mutex);
3397 : : build_all_zonelists(NULL, NULL);
3398 : : mutex_unlock(&zonelists_mutex);
3399 : : }
3400 : : }
3401 : : out:
3402 : : mutex_unlock(&zl_order_mutex);
3403 : : return ret;
3404 : : }
3405 : :
3406 : :
3407 : : #define MAX_NODE_LOAD (nr_online_nodes)
3408 : : static int node_load[MAX_NUMNODES];
3409 : :
3410 : : /**
3411 : : * find_next_best_node - find the next node that should appear in a given node's fallback list
3412 : : * @node: node whose fallback list we're appending
3413 : : * @used_node_mask: nodemask_t of already used nodes
3414 : : *
3415 : : * We use a number of factors to determine which is the next node that should
3416 : : * appear on a given node's fallback list. The node should not have appeared
3417 : : * already in @node's fallback list, and it should be the next closest node
3418 : : * according to the distance array (which contains arbitrary distance values
3419 : : * from each node to each node in the system), and should also prefer nodes
3420 : : * with no CPUs, since presumably they'll have very little allocation pressure
3421 : : * on them otherwise.
3422 : : * It returns -1 if no node is found.
3423 : : */
3424 : : static int find_next_best_node(int node, nodemask_t *used_node_mask)
3425 : : {
3426 : : int n, val;
3427 : : int min_val = INT_MAX;
3428 : : int best_node = NUMA_NO_NODE;
3429 : : const struct cpumask *tmp = cpumask_of_node(0);
3430 : :
3431 : : /* Use the local node if we haven't already */
3432 : : if (!node_isset(node, *used_node_mask)) {
3433 : : node_set(node, *used_node_mask);
3434 : : return node;
3435 : : }
3436 : :
3437 : : for_each_node_state(n, N_MEMORY) {
3438 : :
3439 : : /* Don't want a node to appear more than once */
3440 : : if (node_isset(n, *used_node_mask))
3441 : : continue;
3442 : :
3443 : : /* Use the distance array to find the distance */
3444 : : val = node_distance(node, n);
3445 : :
3446 : : /* Penalize nodes under us ("prefer the next node") */
3447 : : val += (n < node);
3448 : :
3449 : : /* Give preference to headless and unused nodes */
3450 : : tmp = cpumask_of_node(n);
3451 : : if (!cpumask_empty(tmp))
3452 : : val += PENALTY_FOR_NODE_WITH_CPUS;
3453 : :
3454 : : /* Slight preference for less loaded node */
3455 : : val *= (MAX_NODE_LOAD*MAX_NUMNODES);
3456 : : val += node_load[n];
3457 : :
3458 : : if (val < min_val) {
3459 : : min_val = val;
3460 : : best_node = n;
3461 : : }
3462 : : }
3463 : :
3464 : : if (best_node >= 0)
3465 : : node_set(best_node, *used_node_mask);
3466 : :
3467 : : return best_node;
3468 : : }
3469 : :
3470 : :
3471 : : /*
3472 : : * Build zonelists ordered by node and zones within node.
3473 : : * This results in maximum locality--normal zone overflows into local
3474 : : * DMA zone, if any--but risks exhausting DMA zone.
3475 : : */
3476 : : static void build_zonelists_in_node_order(pg_data_t *pgdat, int node)
3477 : : {
3478 : : int j;
3479 : : struct zonelist *zonelist;
3480 : :
3481 : : zonelist = &pgdat->node_zonelists[0];
3482 : : for (j = 0; zonelist->_zonerefs[j].zone != NULL; j++)
3483 : : ;
3484 : : j = build_zonelists_node(NODE_DATA(node), zonelist, j);
3485 : : zonelist->_zonerefs[j].zone = NULL;
3486 : : zonelist->_zonerefs[j].zone_idx = 0;
3487 : : }
3488 : :
3489 : : /*
3490 : : * Build gfp_thisnode zonelists
3491 : : */
3492 : : static void build_thisnode_zonelists(pg_data_t *pgdat)
3493 : : {
3494 : : int j;
3495 : : struct zonelist *zonelist;
3496 : :
3497 : : zonelist = &pgdat->node_zonelists[1];
3498 : : j = build_zonelists_node(pgdat, zonelist, 0);
3499 : : zonelist->_zonerefs[j].zone = NULL;
3500 : : zonelist->_zonerefs[j].zone_idx = 0;
3501 : : }
3502 : :
3503 : : /*
3504 : : * Build zonelists ordered by zone and nodes within zones.
3505 : : * This results in conserving DMA zone[s] until all Normal memory is
3506 : : * exhausted, but results in overflowing to remote node while memory
3507 : : * may still exist in local DMA zone.
3508 : : */
3509 : : static int node_order[MAX_NUMNODES];
3510 : :
3511 : : static void build_zonelists_in_zone_order(pg_data_t *pgdat, int nr_nodes)
3512 : : {
3513 : : int pos, j, node;
3514 : : int zone_type; /* needs to be signed */
3515 : : struct zone *z;
3516 : : struct zonelist *zonelist;
3517 : :
3518 : : zonelist = &pgdat->node_zonelists[0];
3519 : : pos = 0;
3520 : : for (zone_type = MAX_NR_ZONES - 1; zone_type >= 0; zone_type--) {
3521 : : for (j = 0; j < nr_nodes; j++) {
3522 : : node = node_order[j];
3523 : : z = &NODE_DATA(node)->node_zones[zone_type];
3524 : : if (populated_zone(z)) {
3525 : : zoneref_set_zone(z,
3526 : : &zonelist->_zonerefs[pos++]);
3527 : : check_highest_zone(zone_type);
3528 : : }
3529 : : }
3530 : : }
3531 : : zonelist->_zonerefs[pos].zone = NULL;
3532 : : zonelist->_zonerefs[pos].zone_idx = 0;
3533 : : }
3534 : :
3535 : : static int default_zonelist_order(void)
3536 : : {
3537 : : int nid, zone_type;
3538 : : unsigned long low_kmem_size, total_size;
3539 : : struct zone *z;
3540 : : int average_size;
3541 : : /*
3542 : : * ZONE_DMA and ZONE_DMA32 can be very small area in the system.
3543 : : * If they are really small and used heavily, the system can fall
3544 : : * into OOM very easily.
3545 : : * This function detect ZONE_DMA/DMA32 size and configures zone order.
3546 : : */
3547 : : /* Is there ZONE_NORMAL ? (ex. ppc has only DMA zone..) */
3548 : : low_kmem_size = 0;
3549 : : total_size = 0;
3550 : : for_each_online_node(nid) {
3551 : : for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3552 : : z = &NODE_DATA(nid)->node_zones[zone_type];
3553 : : if (populated_zone(z)) {
3554 : : if (zone_type < ZONE_NORMAL)
3555 : : low_kmem_size += z->managed_pages;
3556 : : total_size += z->managed_pages;
3557 : : } else if (zone_type == ZONE_NORMAL) {
3558 : : /*
3559 : : * If any node has only lowmem, then node order
3560 : : * is preferred to allow kernel allocations
3561 : : * locally; otherwise, they can easily infringe
3562 : : * on other nodes when there is an abundance of
3563 : : * lowmem available to allocate from.
3564 : : */
3565 : : return ZONELIST_ORDER_NODE;
3566 : : }
3567 : : }
3568 : : }
3569 : : if (!low_kmem_size || /* there are no DMA area. */
3570 : : low_kmem_size > total_size/2) /* DMA/DMA32 is big. */
3571 : : return ZONELIST_ORDER_NODE;
3572 : : /*
3573 : : * look into each node's config.
3574 : : * If there is a node whose DMA/DMA32 memory is very big area on
3575 : : * local memory, NODE_ORDER may be suitable.
3576 : : */
3577 : : average_size = total_size /
3578 : : (nodes_weight(node_states[N_MEMORY]) + 1);
3579 : : for_each_online_node(nid) {
3580 : : low_kmem_size = 0;
3581 : : total_size = 0;
3582 : : for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) {
3583 : : z = &NODE_DATA(nid)->node_zones[zone_type];
3584 : : if (populated_zone(z)) {
3585 : : if (zone_type < ZONE_NORMAL)
3586 : : low_kmem_size += z->present_pages;
3587 : : total_size += z->present_pages;
3588 : : }
3589 : : }
3590 : : if (low_kmem_size &&
3591 : : total_size > average_size && /* ignore small node */
3592 : : low_kmem_size > total_size * 70/100)
3593 : : return ZONELIST_ORDER_NODE;
3594 : : }
3595 : : return ZONELIST_ORDER_ZONE;
3596 : : }
3597 : :
3598 : : static void set_zonelist_order(void)
3599 : : {
3600 : : if (user_zonelist_order == ZONELIST_ORDER_DEFAULT)
3601 : : current_zonelist_order = default_zonelist_order();
3602 : : else
3603 : : current_zonelist_order = user_zonelist_order;
3604 : : }
3605 : :
3606 : : static void build_zonelists(pg_data_t *pgdat)
3607 : : {
3608 : : int j, node, load;
3609 : : enum zone_type i;
3610 : : nodemask_t used_mask;
3611 : : int local_node, prev_node;
3612 : : struct zonelist *zonelist;
3613 : : int order = current_zonelist_order;
3614 : :
3615 : : /* initialize zonelists */
3616 : : for (i = 0; i < MAX_ZONELISTS; i++) {
3617 : : zonelist = pgdat->node_zonelists + i;
3618 : : zonelist->_zonerefs[0].zone = NULL;
3619 : : zonelist->_zonerefs[0].zone_idx = 0;
3620 : : }
3621 : :
3622 : : /* NUMA-aware ordering of nodes */
3623 : : local_node = pgdat->node_id;
3624 : : load = nr_online_nodes;
3625 : : prev_node = local_node;
3626 : : nodes_clear(used_mask);
3627 : :
3628 : : memset(node_order, 0, sizeof(node_order));
3629 : : j = 0;
3630 : :
3631 : : while ((node = find_next_best_node(local_node, &used_mask)) >= 0) {
3632 : : /*
3633 : : * We don't want to pressure a particular node.
3634 : : * So adding penalty to the first node in same
3635 : : * distance group to make it round-robin.
3636 : : */
3637 : : if (node_distance(local_node, node) !=
3638 : : node_distance(local_node, prev_node))
3639 : : node_load[node] = load;
3640 : :
3641 : : prev_node = node;
3642 : : load--;
3643 : : if (order == ZONELIST_ORDER_NODE)
3644 : : build_zonelists_in_node_order(pgdat, node);
3645 : : else
3646 : : node_order[j++] = node; /* remember order */
3647 : : }
3648 : :
3649 : : if (order == ZONELIST_ORDER_ZONE) {
3650 : : /* calculate node order -- i.e., DMA last! */
3651 : : build_zonelists_in_zone_order(pgdat, j);
3652 : : }
3653 : :
3654 : : build_thisnode_zonelists(pgdat);
3655 : : }
3656 : :
3657 : : /* Construct the zonelist performance cache - see further mmzone.h */
3658 : : static void build_zonelist_cache(pg_data_t *pgdat)
3659 : : {
3660 : : struct zonelist *zonelist;
3661 : : struct zonelist_cache *zlc;
3662 : : struct zoneref *z;
3663 : :
3664 : : zonelist = &pgdat->node_zonelists[0];
3665 : : zonelist->zlcache_ptr = zlc = &zonelist->zlcache;
3666 : : bitmap_zero(zlc->fullzones, MAX_ZONES_PER_ZONELIST);
3667 : : for (z = zonelist->_zonerefs; z->zone; z++)
3668 : : zlc->z_to_n[z - zonelist->_zonerefs] = zonelist_node_idx(z);
3669 : : }
3670 : :
3671 : : #ifdef CONFIG_HAVE_MEMORYLESS_NODES
3672 : : /*
3673 : : * Return node id of node used for "local" allocations.
3674 : : * I.e., first node id of first zone in arg node's generic zonelist.
3675 : : * Used for initializing percpu 'numa_mem', which is used primarily
3676 : : * for kernel allocations, so use GFP_KERNEL flags to locate zonelist.
3677 : : */
3678 : : int local_memory_node(int node)
3679 : : {
3680 : : struct zone *zone;
3681 : :
3682 : : (void)first_zones_zonelist(node_zonelist(node, GFP_KERNEL),
3683 : : gfp_zone(GFP_KERNEL),
3684 : : NULL,
3685 : : &zone);
3686 : : return zone->node;
3687 : : }
3688 : : #endif
3689 : :
3690 : : #else /* CONFIG_NUMA */
3691 : :
3692 : : static void set_zonelist_order(void)
3693 : : {
3694 : 0 : current_zonelist_order = ZONELIST_ORDER_ZONE;
3695 : : }
3696 : :
3697 : 0 : static void build_zonelists(pg_data_t *pgdat)
3698 : : {
3699 : : int node, local_node;
3700 : : enum zone_type j;
3701 : : struct zonelist *zonelist;
3702 : :
3703 : 0 : local_node = pgdat->node_id;
3704 : :
3705 : : zonelist = &pgdat->node_zonelists[0];
3706 : 0 : j = build_zonelists_node(pgdat, zonelist, 0);
3707 : :
3708 : : /*
3709 : : * Now we build the zonelist so that it contains the zones
3710 : : * of all the other nodes.
3711 : : * We don't want to pressure a particular node, so when
3712 : : * building the zones for node N, we make sure that the
3713 : : * zones coming right after the local ones are those from
3714 : : * node N+1 (modulo N)
3715 : : */
3716 [ # # ]: 0 : for (node = local_node + 1; node < MAX_NUMNODES; node++) {
3717 [ # # ]: 0 : if (!node_online(node))
3718 : 0 : continue;
3719 : 0 : j = build_zonelists_node(NODE_DATA(node), zonelist, j);
3720 : : }
3721 [ # # ]: 0 : for (node = 0; node < local_node; node++) {
3722 [ # # ]: 0 : if (!node_online(node))
3723 : 0 : continue;
3724 : 0 : j = build_zonelists_node(NODE_DATA(node), zonelist, j);
3725 : : }
3726 : :
3727 : 0 : zonelist->_zonerefs[j].zone = NULL;
3728 : 0 : zonelist->_zonerefs[j].zone_idx = 0;
3729 : 0 : }
3730 : :
3731 : : /* non-NUMA variant of zonelist performance cache - just NULL zlcache_ptr */
3732 : : static void build_zonelist_cache(pg_data_t *pgdat)
3733 : : {
3734 : 0 : pgdat->node_zonelists[0].zlcache_ptr = NULL;
3735 : : }
3736 : :
3737 : : #endif /* CONFIG_NUMA */
3738 : :
3739 : : /*
3740 : : * Boot pageset table. One per cpu which is going to be used for all
3741 : : * zones and all nodes. The parameters will be set in such a way
3742 : : * that an item put on a list will immediately be handed over to
3743 : : * the buddy list. This is safe since pageset manipulation is done
3744 : : * with interrupts disabled.
3745 : : *
3746 : : * The boot_pagesets must be kept even after bootup is complete for
3747 : : * unused processors and/or zones. They do play a role for bootstrapping
3748 : : * hotplugged processors.
3749 : : *
3750 : : * zoneinfo_show() and maybe other functions do
3751 : : * not check if the processor is online before following the pageset pointer.
3752 : : * Other parts of the kernel may not check if the zone is available.
3753 : : */
3754 : : static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch);
3755 : : static DEFINE_PER_CPU(struct per_cpu_pageset, boot_pageset);
3756 : : static void setup_zone_pageset(struct zone *zone);
3757 : :
3758 : : /*
3759 : : * Global mutex to protect against size modification of zonelists
3760 : : * as well as to serialize pageset setup for the new populated zone.
3761 : : */
3762 : : DEFINE_MUTEX(zonelists_mutex);
3763 : :
3764 : : /* return values int ....just for stop_machine() */
3765 : 0 : static int __build_all_zonelists(void *data)
3766 : : {
3767 : : int nid;
3768 : : int cpu;
3769 : : pg_data_t *self = data;
3770 : :
3771 : : #ifdef CONFIG_NUMA
3772 : : memset(node_load, 0, sizeof(node_load));
3773 : : #endif
3774 : :
3775 [ # # ][ # # ]: 0 : if (self && !node_online(self->node_id)) {
3776 : 0 : build_zonelists(self);
3777 : : build_zonelist_cache(self);
3778 : : }
3779 : :
3780 [ # # ]: 0 : for_each_online_node(nid) {
3781 : : pg_data_t *pgdat = NODE_DATA(nid);
3782 : :
3783 : 0 : build_zonelists(pgdat);
3784 : : build_zonelist_cache(pgdat);
3785 : : }
3786 : :
3787 : : /*
3788 : : * Initialize the boot_pagesets that are going to be used
3789 : : * for bootstrapping processors. The real pagesets for
3790 : : * each zone will be allocated later when the per cpu
3791 : : * allocator is available.
3792 : : *
3793 : : * boot_pagesets are used also for bootstrapping offline
3794 : : * cpus if the system is already booted because the pagesets
3795 : : * are needed to initialize allocators on a specific cpu too.
3796 : : * F.e. the percpu allocator needs the page allocator which
3797 : : * needs the percpu allocator in order to allocate its pagesets
3798 : : * (a chicken-egg dilemma).
3799 : : */
3800 [ # # ]: 0 : for_each_possible_cpu(cpu) {
3801 : 0 : setup_pageset(&per_cpu(boot_pageset, cpu), 0);
3802 : :
3803 : : #ifdef CONFIG_HAVE_MEMORYLESS_NODES
3804 : : /*
3805 : : * We now know the "local memory node" for each node--
3806 : : * i.e., the node of the first zone in the generic zonelist.
3807 : : * Set up numa_mem percpu variable for on-line cpus. During
3808 : : * boot, only the boot cpu should be on-line; we'll init the
3809 : : * secondary cpus' numa_mem as they come on-line. During
3810 : : * node/memory hotplug, we'll fixup all on-line cpus.
3811 : : */
3812 : : if (cpu_online(cpu))
3813 : : set_cpu_numa_mem(cpu, local_memory_node(cpu_to_node(cpu)));
3814 : : #endif
3815 : : }
3816 : :
3817 : 0 : return 0;
3818 : : }
3819 : :
3820 : : /*
3821 : : * Called with zonelists_mutex held always
3822 : : * unless system_state == SYSTEM_BOOTING.
3823 : : */
3824 : 0 : void __ref build_all_zonelists(pg_data_t *pgdat, struct zone *zone)
3825 : : {
3826 : : set_zonelist_order();
3827 : :
3828 [ # # ]: 0 : if (system_state == SYSTEM_BOOTING) {
3829 : 0 : __build_all_zonelists(NULL);
3830 : : mminit_verify_zonelist();
3831 : : cpuset_init_current_mems_allowed();
3832 : : } else {
3833 : : #ifdef CONFIG_MEMORY_HOTPLUG
3834 : : if (zone)
3835 : : setup_zone_pageset(zone);
3836 : : #endif
3837 : : /* we have to stop all cpus to guarantee there is no user
3838 : : of zonelist */
3839 : 0 : stop_machine(__build_all_zonelists, pgdat, NULL);
3840 : : /* cpuset refresh routine should be here */
3841 : : }
3842 : 0 : vm_total_pages = nr_free_pagecache_pages();
3843 : : /*
3844 : : * Disable grouping by mobility if the number of pages in the
3845 : : * system is too low to allow the mechanism to work. It would be
3846 : : * more accurate, but expensive to check per-zone. This check is
3847 : : * made on memory-hotadd so a system can start with mobility
3848 : : * disabled and enable it later
3849 : : */
3850 [ # # ]: 0 : if (vm_total_pages < (pageblock_nr_pages * MIGRATE_TYPES))
3851 : 0 : page_group_by_mobility_disabled = 1;
3852 : : else
3853 : 0 : page_group_by_mobility_disabled = 0;
3854 : :
3855 [ # # ]: 0 : printk("Built %i zonelists in %s order, mobility grouping %s. "
3856 : : "Total pages: %ld\n",
3857 : : nr_online_nodes,
3858 : 0 : zonelist_order_name[current_zonelist_order],
3859 : 0 : page_group_by_mobility_disabled ? "off" : "on",
3860 : : vm_total_pages);
3861 : : #ifdef CONFIG_NUMA
3862 : : printk("Policy zone: %s\n", zone_names[policy_zone]);
3863 : : #endif
3864 : 0 : }
3865 : :
3866 : : /*
3867 : : * Helper functions to size the waitqueue hash table.
3868 : : * Essentially these want to choose hash table sizes sufficiently
3869 : : * large so that collisions trying to wait on pages are rare.
3870 : : * But in fact, the number of active page waitqueues on typical
3871 : : * systems is ridiculously low, less than 200. So this is even
3872 : : * conservative, even though it seems large.
3873 : : *
3874 : : * The constant PAGES_PER_WAITQUEUE specifies the ratio of pages to
3875 : : * waitqueues, i.e. the size of the waitq table given the number of pages.
3876 : : */
3877 : : #define PAGES_PER_WAITQUEUE 256
3878 : :
3879 : : #ifndef CONFIG_MEMORY_HOTPLUG
3880 : : static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3881 : : {
3882 : : unsigned long size = 1;
3883 : :
3884 : 0 : pages /= PAGES_PER_WAITQUEUE;
3885 : :
3886 [ # # ]: 0 : while (size < pages)
3887 : 0 : size <<= 1;
3888 : :
3889 : : /*
3890 : : * Once we have dozens or even hundreds of threads sleeping
3891 : : * on IO we've got bigger problems than wait queue collision.
3892 : : * Limit the size of the wait table to a reasonable size.
3893 : : */
3894 : 0 : size = min(size, 4096UL);
3895 : :
3896 : 0 : return max(size, 4UL);
3897 : : }
3898 : : #else
3899 : : /*
3900 : : * A zone's size might be changed by hot-add, so it is not possible to determine
3901 : : * a suitable size for its wait_table. So we use the maximum size now.
3902 : : *
3903 : : * The max wait table size = 4096 x sizeof(wait_queue_head_t). ie:
3904 : : *
3905 : : * i386 (preemption config) : 4096 x 16 = 64Kbyte.
3906 : : * ia64, x86-64 (no preemption): 4096 x 20 = 80Kbyte.
3907 : : * ia64, x86-64 (preemption) : 4096 x 24 = 96Kbyte.
3908 : : *
3909 : : * The maximum entries are prepared when a zone's memory is (512K + 256) pages
3910 : : * or more by the traditional way. (See above). It equals:
3911 : : *
3912 : : * i386, x86-64, powerpc(4K page size) : = ( 2G + 1M)byte.
3913 : : * ia64(16K page size) : = ( 8G + 4M)byte.
3914 : : * powerpc (64K page size) : = (32G +16M)byte.
3915 : : */
3916 : : static inline unsigned long wait_table_hash_nr_entries(unsigned long pages)
3917 : : {
3918 : : return 4096UL;
3919 : : }
3920 : : #endif
3921 : :
3922 : : /*
3923 : : * This is an integer logarithm so that shifts can be used later
3924 : : * to extract the more random high bits from the multiplicative
3925 : : * hash function before the remainder is taken.
3926 : : */
3927 : : static inline unsigned long wait_table_bits(unsigned long size)
3928 : : {
3929 : : return ffz(~size);
3930 : : }
3931 : :
3932 : : /*
3933 : : * Check if a pageblock contains reserved pages
3934 : : */
3935 : : static int pageblock_is_reserved(unsigned long start_pfn, unsigned long end_pfn)
3936 : : {
3937 : : unsigned long pfn;
3938 : :
3939 [ # # ]: 0 : for (pfn = start_pfn; pfn < end_pfn; pfn++) {
3940 [ # # ]: 0 : if (!pfn_valid_within(pfn) || PageReserved(pfn_to_page(pfn)))
3941 : : return 1;
3942 : : }
3943 : : return 0;
3944 : : }
3945 : :
3946 : : /*
3947 : : * Mark a number of pageblocks as MIGRATE_RESERVE. The number
3948 : : * of blocks reserved is based on min_wmark_pages(zone). The memory within
3949 : : * the reserve will tend to store contiguous free pages. Setting min_free_kbytes
3950 : : * higher will lead to a bigger reserve which will get freed as contiguous
3951 : : * blocks as reclaim kicks in
3952 : : */
3953 : 0 : static void setup_zone_migrate_reserve(struct zone *zone)
3954 : : {
3955 : : unsigned long start_pfn, pfn, end_pfn, block_end_pfn;
3956 : : struct page *page;
3957 : : unsigned long block_migratetype;
3958 : : int reserve;
3959 : : int old_reserve;
3960 : :
3961 : : /*
3962 : : * Get the start pfn, end pfn and the number of blocks to reserve
3963 : : * We have to be careful to be aligned to pageblock_nr_pages to
3964 : : * make sure that we always check pfn_valid for the first page in
3965 : : * the block.
3966 : : */
3967 : 30 : start_pfn = zone->zone_start_pfn;
3968 : : end_pfn = zone_end_pfn(zone);
3969 : 30 : start_pfn = roundup(start_pfn, pageblock_nr_pages);
3970 : 30 : reserve = roundup(min_wmark_pages(zone), pageblock_nr_pages) >>
3971 : : pageblock_order;
3972 : :
3973 : : /*
3974 : : * Reserve blocks are generally in place to help high-order atomic
3975 : : * allocations that are short-lived. A min_free_kbytes value that
3976 : : * would result in more than 2 reserve blocks for atomic allocations
3977 : : * is assumed to be in place to help anti-fragmentation for the
3978 : : * future allocation of hugepages at runtime.
3979 : : */
3980 : 30 : reserve = min(2, reserve);
3981 : 30 : old_reserve = zone->nr_migrate_reserve_block;
3982 : :
3983 : : /* When memory hot-add, we almost always need to do nothing */
3984 [ - + ]: 30 : if (reserve == old_reserve)
3985 : 30 : return;
3986 : 0 : zone->nr_migrate_reserve_block = reserve;
3987 : :
3988 [ # # ]: 0 : for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) {
3989 [ # # ]: 0 : if (!pfn_valid(pfn))
3990 : 0 : continue;
3991 : 0 : page = pfn_to_page(pfn);
3992 : :
3993 : : /* Watch out for overlapping nodes */
3994 : : if (page_to_nid(page) != zone_to_nid(zone))
3995 : : continue;
3996 : :
3997 : : block_migratetype = get_pageblock_migratetype(page);
3998 : :
3999 : : /* Only test what is necessary when the reserves are not met */
4000 [ + - ]: 30 : if (reserve > 0) {
4001 : : /*
4002 : : * Blocks with reserved pages will never free, skip
4003 : : * them.
4004 : : */
4005 : 30 : block_end_pfn = min(pfn + pageblock_nr_pages, end_pfn);
4006 [ # # ]: 0 : if (pageblock_is_reserved(pfn, block_end_pfn))
4007 : 0 : continue;
4008 : :
4009 : : /* If this block is reserved, account for it */
4010 [ # # ]: 0 : if (block_migratetype == MIGRATE_RESERVE) {
4011 : 0 : reserve--;
4012 : 0 : continue;
4013 : : }
4014 : :
4015 : : /* Suitable for reserving if this block is movable */
4016 [ # # ]: 0 : if (block_migratetype == MIGRATE_MOVABLE) {
4017 : 0 : set_pageblock_migratetype(page,
4018 : : MIGRATE_RESERVE);
4019 : 0 : move_freepages_block(zone, page,
4020 : : MIGRATE_RESERVE);
4021 : 0 : reserve--;
4022 : 0 : continue;
4023 : : }
4024 [ # # ]: 0 : } else if (!old_reserve) {
4025 : : /*
4026 : : * At boot time we don't need to scan the whole zone
4027 : : * for turning off MIGRATE_RESERVE.
4028 : : */
4029 : : break;
4030 : : }
4031 : :
4032 : : /*
4033 : : * If the reserve is met and this is a previous reserved block,
4034 : : * take it back
4035 : : */
4036 [ # # ]: 0 : if (block_migratetype == MIGRATE_RESERVE) {
4037 : 0 : set_pageblock_migratetype(page, MIGRATE_MOVABLE);
4038 : 0 : move_freepages_block(zone, page, MIGRATE_MOVABLE);
4039 : : }
4040 : : }
4041 : : }
4042 : :
4043 : : /*
4044 : : * Initially all pages are reserved - free ones are freed
4045 : : * up by free_all_bootmem() once the early boot process is
4046 : : * done. Non-atomic initialization, single-pass.
4047 : : */
4048 : 0 : void __meminit memmap_init_zone(unsigned long size, int nid, unsigned long zone,
4049 : : unsigned long start_pfn, enum memmap_context context)
4050 : : {
4051 : : struct page *page;
4052 : 0 : unsigned long end_pfn = start_pfn + size;
4053 : : unsigned long pfn;
4054 : 0 : struct zone *z;
4055 : :
4056 [ # # ]: 0 : if (highest_memmap_pfn < end_pfn - 1)
4057 : 0 : highest_memmap_pfn = end_pfn - 1;
4058 : :
4059 : 0 : z = &NODE_DATA(nid)->node_zones[zone];
4060 [ # # ]: 0 : for (pfn = start_pfn; pfn < end_pfn; pfn++) {
4061 : : /*
4062 : : * There can be holes in boot-time mem_map[]s
4063 : : * handed to this function. They do not
4064 : : * exist on hotplugged memory.
4065 : : */
4066 : : if (context == MEMMAP_EARLY) {
4067 : : if (!early_pfn_valid(pfn))
4068 : : continue;
4069 : : if (!early_pfn_in_nid(pfn, nid))
4070 : : continue;
4071 : : }
4072 : 0 : page = pfn_to_page(pfn);
4073 : : set_page_links(page, zone, nid, pfn);
4074 : : mminit_verify_page_links(page, zone, nid, pfn);
4075 : : init_page_count(page);
4076 : : page_mapcount_reset(page);
4077 : : page_cpupid_reset_last(page);
4078 : : SetPageReserved(page);
4079 : : /*
4080 : : * Mark the block movable so that blocks are reserved for
4081 : : * movable at startup. This will force kernel allocations
4082 : : * to reserve their blocks rather than leaking throughout
4083 : : * the address space during boot when many long-lived
4084 : : * kernel allocations are made. Later some blocks near
4085 : : * the start are marked MIGRATE_RESERVE by
4086 : : * setup_zone_migrate_reserve()
4087 : : *
4088 : : * bitmap is created for zone's valid pfn range. but memmap
4089 : : * can be created for invalid pages (for alignment)
4090 : : * check here not to call set_pageblock_migratetype() against
4091 : : * pfn out of zone.
4092 : : */
4093 [ # # ]: 0 : if ((z->zone_start_pfn <= pfn)
4094 [ # # ]: 0 : && (pfn < zone_end_pfn(z))
4095 [ # # ]: 0 : && !(pfn & (pageblock_nr_pages - 1)))
4096 : 0 : set_pageblock_migratetype(page, MIGRATE_MOVABLE);
4097 : :
4098 : 0 : INIT_LIST_HEAD(&page->lru);
4099 : : #ifdef WANT_PAGE_VIRTUAL
4100 : : /* The shift won't overflow because ZONE_NORMAL is below 4G. */
4101 : : if (!is_highmem_idx(zone))
4102 : : set_page_address(page, __va(pfn << PAGE_SHIFT));
4103 : : #endif
4104 : : }
4105 : 0 : }
4106 : :
4107 : 0 : static void __meminit zone_init_free_lists(struct zone *zone)
4108 : : {
4109 : : int order, t;
4110 [ # # ][ # # ]: 0 : for_each_migratetype_order(order, t) {
4111 : 0 : INIT_LIST_HEAD(&zone->free_area[order].free_list[t]);
4112 : 0 : zone->free_area[order].nr_free = 0;
4113 : : }
4114 : 0 : }
4115 : :
4116 : : #ifndef __HAVE_ARCH_MEMMAP_INIT
4117 : : #define memmap_init(size, nid, zone, start_pfn) \
4118 : : memmap_init_zone((size), (nid), (zone), (start_pfn), MEMMAP_EARLY)
4119 : : #endif
4120 : :
4121 : 0 : static int __meminit zone_batchsize(struct zone *zone)
4122 : : {
4123 : : #ifdef CONFIG_MMU
4124 : : int batch;
4125 : :
4126 : : /*
4127 : : * The per-cpu-pages pools are set to around 1000th of the
4128 : : * size of the zone. But no more than 1/2 of a meg.
4129 : : *
4130 : : * OK, so we don't know how big the cache is. So guess.
4131 : : */
4132 : 0 : batch = zone->managed_pages / 1024;
4133 [ # # ]: 0 : if (batch * PAGE_SIZE > 512 * 1024)
4134 : : batch = (512 * 1024) / PAGE_SIZE;
4135 : 0 : batch /= 4; /* We effectively *= 4 below */
4136 [ # # ]: 0 : if (batch < 1)
4137 : : batch = 1;
4138 : :
4139 : : /*
4140 : : * Clamp the batch to a 2^n - 1 value. Having a power
4141 : : * of 2 value was found to be more likely to have
4142 : : * suboptimal cache aliasing properties in some cases.
4143 : : *
4144 : : * For example if 2 tasks are alternately allocating
4145 : : * batches of pages, one task can end up with a lot
4146 : : * of pages of one half of the possible page colors
4147 : : * and the other with pages of the other colors.
4148 : : */
4149 [ # # ][ # # ]: 0 : batch = rounddown_pow_of_two(batch + batch/2) - 1;
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
4150 : :
4151 : 0 : return batch;
4152 : :
4153 : : #else
4154 : : /* The deferral and batching of frees should be suppressed under NOMMU
4155 : : * conditions.
4156 : : *
4157 : : * The problem is that NOMMU needs to be able to allocate large chunks
4158 : : * of contiguous memory as there's no hardware page translation to
4159 : : * assemble apparent contiguous memory from discontiguous pages.
4160 : : *
4161 : : * Queueing large contiguous runs of pages for batching, however,
4162 : : * causes the pages to actually be freed in smaller chunks. As there
4163 : : * can be a significant delay between the individual batches being
4164 : : * recycled, this leads to the once large chunks of space being
4165 : : * fragmented and becoming unavailable for high-order allocations.
4166 : : */
4167 : : return 0;
4168 : : #endif
4169 : : }
4170 : :
4171 : : /*
4172 : : * pcp->high and pcp->batch values are related and dependent on one another:
4173 : : * ->batch must never be higher then ->high.
4174 : : * The following function updates them in a safe manner without read side
4175 : : * locking.
4176 : : *
4177 : : * Any new users of pcp->batch and pcp->high should ensure they can cope with
4178 : : * those fields changing asynchronously (acording the the above rule).
4179 : : *
4180 : : * mutex_is_locked(&pcp_batch_high_lock) required when calling this function
4181 : : * outside of boot time (or some other assurance that no concurrent updaters
4182 : : * exist).
4183 : : */
4184 : : static void pageset_update(struct per_cpu_pages *pcp, unsigned long high,
4185 : : unsigned long batch)
4186 : : {
4187 : : /* start with a fail safe value for batch */
4188 : 0 : pcp->batch = 1;
4189 : 0 : smp_wmb();
4190 : :
4191 : : /* Update high, then batch, in order */
4192 : 0 : pcp->high = high;
4193 : 0 : smp_wmb();
4194 : :
4195 : 0 : pcp->batch = batch;
4196 : : }
4197 : :
4198 : : /* a companion to pageset_set_high() */
4199 : : static void pageset_set_batch(struct per_cpu_pageset *p, unsigned long batch)
4200 : : {
4201 : 0 : pageset_update(&p->pcp, 6 * batch, max(1UL, 1 * batch));
4202 : : }
4203 : :
4204 : 0 : static void pageset_init(struct per_cpu_pageset *p)
4205 : : {
4206 : : struct per_cpu_pages *pcp;
4207 : : int migratetype;
4208 : :
4209 : 0 : memset(p, 0, sizeof(*p));
4210 : :
4211 : : pcp = &p->pcp;
4212 : 0 : pcp->count = 0;
4213 [ # # ]: 0 : for (migratetype = 0; migratetype < MIGRATE_PCPTYPES; migratetype++)
4214 : 0 : INIT_LIST_HEAD(&pcp->lists[migratetype]);
4215 : 0 : }
4216 : :
4217 : : static void setup_pageset(struct per_cpu_pageset *p, unsigned long batch)
4218 : : {
4219 : 0 : pageset_init(p);
4220 : : pageset_set_batch(p, batch);
4221 : : }
4222 : :
4223 : : /*
4224 : : * pageset_set_high() sets the high water mark for hot per_cpu_pagelist
4225 : : * to the value high for the pageset p.
4226 : : */
4227 : : static void pageset_set_high(struct per_cpu_pageset *p,
4228 : : unsigned long high)
4229 : : {
4230 : 0 : unsigned long batch = max(1UL, high / 4);
4231 [ # # ][ # # ]: 0 : if ((high / 4) > (PAGE_SHIFT * 8))
4232 : : batch = PAGE_SHIFT * 8;
4233 : :
4234 : : pageset_update(&p->pcp, high, batch);
4235 : : }
4236 : :
4237 : 0 : static void __meminit pageset_set_high_and_batch(struct zone *zone,
4238 : : struct per_cpu_pageset *pcp)
4239 : : {
4240 [ # # ]: 0 : if (percpu_pagelist_fraction)
4241 : 0 : pageset_set_high(pcp,
4242 : 0 : (zone->managed_pages /
4243 : : percpu_pagelist_fraction));
4244 : : else
4245 : 0 : pageset_set_batch(pcp, zone_batchsize(zone));
4246 : 0 : }
4247 : :
4248 : 0 : static void __meminit zone_pageset_init(struct zone *zone, int cpu)
4249 : : {
4250 : 0 : struct per_cpu_pageset *pcp = per_cpu_ptr(zone->pageset, cpu);
4251 : :
4252 : 0 : pageset_init(pcp);
4253 : 0 : pageset_set_high_and_batch(zone, pcp);
4254 : 0 : }
4255 : :
4256 : 0 : static void __meminit setup_zone_pageset(struct zone *zone)
4257 : : {
4258 : : int cpu;
4259 : 0 : zone->pageset = alloc_percpu(struct per_cpu_pageset);
4260 [ # # ]: 0 : for_each_possible_cpu(cpu)
4261 : 0 : zone_pageset_init(zone, cpu);
4262 : 0 : }
4263 : :
4264 : : /*
4265 : : * Allocate per cpu pagesets and initialize them.
4266 : : * Before this call only boot pagesets were available.
4267 : : */
4268 : 0 : void __init setup_per_cpu_pageset(void)
4269 : : {
4270 : 0 : struct zone *zone;
4271 : :
4272 [ # # ][ # # ]: 0 : for_each_populated_zone(zone)
4273 : 0 : setup_zone_pageset(zone);
4274 : 0 : }
4275 : :
4276 : : static noinline __init_refok
4277 : 0 : int zone_wait_table_init(struct zone *zone, unsigned long zone_size_pages)
4278 : : {
4279 : : int i;
4280 : : size_t alloc_size;
4281 : :
4282 : : /*
4283 : : * The per-page waitqueue mechanism uses hashed waitqueues
4284 : : * per zone.
4285 : : */
4286 : 0 : zone->wait_table_hash_nr_entries =
4287 : : wait_table_hash_nr_entries(zone_size_pages);
4288 : 0 : zone->wait_table_bits =
4289 : : wait_table_bits(zone->wait_table_hash_nr_entries);
4290 : 0 : alloc_size = zone->wait_table_hash_nr_entries
4291 : : * sizeof(wait_queue_head_t);
4292 : :
4293 [ # # ]: 0 : if (!slab_is_available()) {
4294 : 0 : zone->wait_table = (wait_queue_head_t *)
4295 : 0 : memblock_virt_alloc_node_nopanic(
4296 : 0 : alloc_size, zone->zone_pgdat->node_id);
4297 : : } else {
4298 : : /*
4299 : : * This case means that a zone whose size was 0 gets new memory
4300 : : * via memory hot-add.
4301 : : * But it may be the case that a new node was hot-added. In
4302 : : * this case vmalloc() will not be able to use this new node's
4303 : : * memory - this wait_table must be initialized to use this new
4304 : : * node itself as well.
4305 : : * To use this new node's memory, further consideration will be
4306 : : * necessary.
4307 : : */
4308 : 0 : zone->wait_table = vmalloc(alloc_size);
4309 : : }
4310 [ # # ]: 0 : if (!zone->wait_table)
4311 : : return -ENOMEM;
4312 : :
4313 [ # # ]: 0 : for (i = 0; i < zone->wait_table_hash_nr_entries; ++i)
4314 : 0 : init_waitqueue_head(zone->wait_table + i);
4315 : :
4316 : : return 0;
4317 : : }
4318 : :
4319 : 0 : static __meminit void zone_pcp_init(struct zone *zone)
4320 : : {
4321 : : /*
4322 : : * per cpu subsystem is not up at this point. The following code
4323 : : * relies on the ability of the linker to provide the
4324 : : * offset of a (static) per cpu variable into the per cpu area.
4325 : : */
4326 : 0 : zone->pageset = &boot_pageset;
4327 : :
4328 [ # # ]: 0 : if (populated_zone(zone))
4329 : 0 : printk(KERN_DEBUG " %s zone: %lu pages, LIFO batch:%u\n",
4330 : : zone->name, zone->present_pages,
4331 : : zone_batchsize(zone));
4332 : 0 : }
4333 : :
4334 : 0 : int __meminit init_currently_empty_zone(struct zone *zone,
4335 : : unsigned long zone_start_pfn,
4336 : : unsigned long size,
4337 : : enum memmap_context context)
4338 : : {
4339 : 0 : struct pglist_data *pgdat = zone->zone_pgdat;
4340 : : int ret;
4341 : 0 : ret = zone_wait_table_init(zone, size);
4342 [ # # ]: 0 : if (ret)
4343 : : return ret;
4344 : 0 : pgdat->nr_zones = zone_idx(zone) + 1;
4345 : :
4346 : 0 : zone->zone_start_pfn = zone_start_pfn;
4347 : :
4348 : : mminit_dprintk(MMINIT_TRACE, "memmap_init",
4349 : : "Initialising map node %d zone %lu pfns %lu -> %lu\n",
4350 : : pgdat->node_id,
4351 : : (unsigned long)zone_idx(zone),
4352 : : zone_start_pfn, (zone_start_pfn + size));
4353 : :
4354 : 0 : zone_init_free_lists(zone);
4355 : :
4356 : 0 : return 0;
4357 : : }
4358 : :
4359 : : #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
4360 : : #ifndef CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID
4361 : : /*
4362 : : * Required by SPARSEMEM. Given a PFN, return what node the PFN is on.
4363 : : * Architectures may implement their own version but if add_active_range()
4364 : : * was used and there are no special requirements, this is a convenient
4365 : : * alternative
4366 : : */
4367 : : int __meminit __early_pfn_to_nid(unsigned long pfn)
4368 : : {
4369 : : unsigned long start_pfn, end_pfn;
4370 : : int nid;
4371 : : /*
4372 : : * NOTE: The following SMP-unsafe globals are only used early in boot
4373 : : * when the kernel is running single-threaded.
4374 : : */
4375 : : static unsigned long __meminitdata last_start_pfn, last_end_pfn;
4376 : : static int __meminitdata last_nid;
4377 : :
4378 : : if (last_start_pfn <= pfn && pfn < last_end_pfn)
4379 : : return last_nid;
4380 : :
4381 : : nid = memblock_search_pfn_nid(pfn, &start_pfn, &end_pfn);
4382 : : if (nid != -1) {
4383 : : last_start_pfn = start_pfn;
4384 : : last_end_pfn = end_pfn;
4385 : : last_nid = nid;
4386 : : }
4387 : :
4388 : : return nid;
4389 : : }
4390 : : #endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
4391 : :
4392 : : int __meminit early_pfn_to_nid(unsigned long pfn)
4393 : : {
4394 : : int nid;
4395 : :
4396 : : nid = __early_pfn_to_nid(pfn);
4397 : : if (nid >= 0)
4398 : : return nid;
4399 : : /* just returns 0 */
4400 : : return 0;
4401 : : }
4402 : :
4403 : : #ifdef CONFIG_NODES_SPAN_OTHER_NODES
4404 : : bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
4405 : : {
4406 : : int nid;
4407 : :
4408 : : nid = __early_pfn_to_nid(pfn);
4409 : : if (nid >= 0 && nid != node)
4410 : : return false;
4411 : : return true;
4412 : : }
4413 : : #endif
4414 : :
4415 : : /**
4416 : : * free_bootmem_with_active_regions - Call memblock_free_early_nid for each active range
4417 : : * @nid: The node to free memory on. If MAX_NUMNODES, all nodes are freed.
4418 : : * @max_low_pfn: The highest PFN that will be passed to memblock_free_early_nid
4419 : : *
4420 : : * If an architecture guarantees that all ranges registered with
4421 : : * add_active_ranges() contain no holes and may be freed, this
4422 : : * this function may be used instead of calling memblock_free_early_nid()
4423 : : * manually.
4424 : : */
4425 : : void __init free_bootmem_with_active_regions(int nid, unsigned long max_low_pfn)
4426 : : {
4427 : : unsigned long start_pfn, end_pfn;
4428 : : int i, this_nid;
4429 : :
4430 : : for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid) {
4431 : : start_pfn = min(start_pfn, max_low_pfn);
4432 : : end_pfn = min(end_pfn, max_low_pfn);
4433 : :
4434 : : if (start_pfn < end_pfn)
4435 : : memblock_free_early_nid(PFN_PHYS(start_pfn),
4436 : : (end_pfn - start_pfn) << PAGE_SHIFT,
4437 : : this_nid);
4438 : : }
4439 : : }
4440 : :
4441 : : /**
4442 : : * sparse_memory_present_with_active_regions - Call memory_present for each active range
4443 : : * @nid: The node to call memory_present for. If MAX_NUMNODES, all nodes will be used.
4444 : : *
4445 : : * If an architecture guarantees that all ranges registered with
4446 : : * add_active_ranges() contain no holes and may be freed, this
4447 : : * function may be used instead of calling memory_present() manually.
4448 : : */
4449 : : void __init sparse_memory_present_with_active_regions(int nid)
4450 : : {
4451 : : unsigned long start_pfn, end_pfn;
4452 : : int i, this_nid;
4453 : :
4454 : : for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, &this_nid)
4455 : : memory_present(this_nid, start_pfn, end_pfn);
4456 : : }
4457 : :
4458 : : /**
4459 : : * get_pfn_range_for_nid - Return the start and end page frames for a node
4460 : : * @nid: The nid to return the range for. If MAX_NUMNODES, the min and max PFN are returned.
4461 : : * @start_pfn: Passed by reference. On return, it will have the node start_pfn.
4462 : : * @end_pfn: Passed by reference. On return, it will have the node end_pfn.
4463 : : *
4464 : : * It returns the start and end page frame of a node based on information
4465 : : * provided by an arch calling add_active_range(). If called for a node
4466 : : * with no available memory, a warning is printed and the start and end
4467 : : * PFNs will be 0.
4468 : : */
4469 : : void __meminit get_pfn_range_for_nid(unsigned int nid,
4470 : : unsigned long *start_pfn, unsigned long *end_pfn)
4471 : : {
4472 : : unsigned long this_start_pfn, this_end_pfn;
4473 : : int i;
4474 : :
4475 : : *start_pfn = -1UL;
4476 : : *end_pfn = 0;
4477 : :
4478 : : for_each_mem_pfn_range(i, nid, &this_start_pfn, &this_end_pfn, NULL) {
4479 : : *start_pfn = min(*start_pfn, this_start_pfn);
4480 : : *end_pfn = max(*end_pfn, this_end_pfn);
4481 : : }
4482 : :
4483 : : if (*start_pfn == -1UL)
4484 : : *start_pfn = 0;
4485 : : }
4486 : :
4487 : : /*
4488 : : * This finds a zone that can be used for ZONE_MOVABLE pages. The
4489 : : * assumption is made that zones within a node are ordered in monotonic
4490 : : * increasing memory addresses so that the "highest" populated zone is used
4491 : : */
4492 : : static void __init find_usable_zone_for_movable(void)
4493 : : {
4494 : : int zone_index;
4495 : : for (zone_index = MAX_NR_ZONES - 1; zone_index >= 0; zone_index--) {
4496 : : if (zone_index == ZONE_MOVABLE)
4497 : : continue;
4498 : :
4499 : : if (arch_zone_highest_possible_pfn[zone_index] >
4500 : : arch_zone_lowest_possible_pfn[zone_index])
4501 : : break;
4502 : : }
4503 : :
4504 : : VM_BUG_ON(zone_index == -1);
4505 : : movable_zone = zone_index;
4506 : : }
4507 : :
4508 : : /*
4509 : : * The zone ranges provided by the architecture do not include ZONE_MOVABLE
4510 : : * because it is sized independent of architecture. Unlike the other zones,
4511 : : * the starting point for ZONE_MOVABLE is not fixed. It may be different
4512 : : * in each node depending on the size of each node and how evenly kernelcore
4513 : : * is distributed. This helper function adjusts the zone ranges
4514 : : * provided by the architecture for a given node by using the end of the
4515 : : * highest usable zone for ZONE_MOVABLE. This preserves the assumption that
4516 : : * zones within a node are in order of monotonic increases memory addresses
4517 : : */
4518 : : static void __meminit adjust_zone_range_for_zone_movable(int nid,
4519 : : unsigned long zone_type,
4520 : : unsigned long node_start_pfn,
4521 : : unsigned long node_end_pfn,
4522 : : unsigned long *zone_start_pfn,
4523 : : unsigned long *zone_end_pfn)
4524 : : {
4525 : : /* Only adjust if ZONE_MOVABLE is on this node */
4526 : : if (zone_movable_pfn[nid]) {
4527 : : /* Size ZONE_MOVABLE */
4528 : : if (zone_type == ZONE_MOVABLE) {
4529 : : *zone_start_pfn = zone_movable_pfn[nid];
4530 : : *zone_end_pfn = min(node_end_pfn,
4531 : : arch_zone_highest_possible_pfn[movable_zone]);
4532 : :
4533 : : /* Adjust for ZONE_MOVABLE starting within this range */
4534 : : } else if (*zone_start_pfn < zone_movable_pfn[nid] &&
4535 : : *zone_end_pfn > zone_movable_pfn[nid]) {
4536 : : *zone_end_pfn = zone_movable_pfn[nid];
4537 : :
4538 : : /* Check if this whole range is within ZONE_MOVABLE */
4539 : : } else if (*zone_start_pfn >= zone_movable_pfn[nid])
4540 : : *zone_start_pfn = *zone_end_pfn;
4541 : : }
4542 : : }
4543 : :
4544 : : /*
4545 : : * Return the number of pages a zone spans in a node, including holes
4546 : : * present_pages = zone_spanned_pages_in_node() - zone_absent_pages_in_node()
4547 : : */
4548 : : static unsigned long __meminit zone_spanned_pages_in_node(int nid,
4549 : : unsigned long zone_type,
4550 : : unsigned long node_start_pfn,
4551 : : unsigned long node_end_pfn,
4552 : : unsigned long *ignored)
4553 : : {
4554 : : unsigned long zone_start_pfn, zone_end_pfn;
4555 : :
4556 : : /* Get the start and end of the zone */
4557 : : zone_start_pfn = arch_zone_lowest_possible_pfn[zone_type];
4558 : : zone_end_pfn = arch_zone_highest_possible_pfn[zone_type];
4559 : : adjust_zone_range_for_zone_movable(nid, zone_type,
4560 : : node_start_pfn, node_end_pfn,
4561 : : &zone_start_pfn, &zone_end_pfn);
4562 : :
4563 : : /* Check that this node has pages within the zone's required range */
4564 : : if (zone_end_pfn < node_start_pfn || zone_start_pfn > node_end_pfn)
4565 : : return 0;
4566 : :
4567 : : /* Move the zone boundaries inside the node if necessary */
4568 : : zone_end_pfn = min(zone_end_pfn, node_end_pfn);
4569 : : zone_start_pfn = max(zone_start_pfn, node_start_pfn);
4570 : :
4571 : : /* Return the spanned pages */
4572 : : return zone_end_pfn - zone_start_pfn;
4573 : : }
4574 : :
4575 : : /*
4576 : : * Return the number of holes in a range on a node. If nid is MAX_NUMNODES,
4577 : : * then all holes in the requested range will be accounted for.
4578 : : */
4579 : : unsigned long __meminit __absent_pages_in_range(int nid,
4580 : : unsigned long range_start_pfn,
4581 : : unsigned long range_end_pfn)
4582 : : {
4583 : : unsigned long nr_absent = range_end_pfn - range_start_pfn;
4584 : : unsigned long start_pfn, end_pfn;
4585 : : int i;
4586 : :
4587 : : for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
4588 : : start_pfn = clamp(start_pfn, range_start_pfn, range_end_pfn);
4589 : : end_pfn = clamp(end_pfn, range_start_pfn, range_end_pfn);
4590 : : nr_absent -= end_pfn - start_pfn;
4591 : : }
4592 : : return nr_absent;
4593 : : }
4594 : :
4595 : : /**
4596 : : * absent_pages_in_range - Return number of page frames in holes within a range
4597 : : * @start_pfn: The start PFN to start searching for holes
4598 : : * @end_pfn: The end PFN to stop searching for holes
4599 : : *
4600 : : * It returns the number of pages frames in memory holes within a range.
4601 : : */
4602 : : unsigned long __init absent_pages_in_range(unsigned long start_pfn,
4603 : : unsigned long end_pfn)
4604 : : {
4605 : : return __absent_pages_in_range(MAX_NUMNODES, start_pfn, end_pfn);
4606 : : }
4607 : :
4608 : : /* Return the number of page frames in holes in a zone on a node */
4609 : : static unsigned long __meminit zone_absent_pages_in_node(int nid,
4610 : : unsigned long zone_type,
4611 : : unsigned long node_start_pfn,
4612 : : unsigned long node_end_pfn,
4613 : : unsigned long *ignored)
4614 : : {
4615 : : unsigned long zone_low = arch_zone_lowest_possible_pfn[zone_type];
4616 : : unsigned long zone_high = arch_zone_highest_possible_pfn[zone_type];
4617 : : unsigned long zone_start_pfn, zone_end_pfn;
4618 : :
4619 : : zone_start_pfn = clamp(node_start_pfn, zone_low, zone_high);
4620 : : zone_end_pfn = clamp(node_end_pfn, zone_low, zone_high);
4621 : :
4622 : : adjust_zone_range_for_zone_movable(nid, zone_type,
4623 : : node_start_pfn, node_end_pfn,
4624 : : &zone_start_pfn, &zone_end_pfn);
4625 : : return __absent_pages_in_range(nid, zone_start_pfn, zone_end_pfn);
4626 : : }
4627 : :
4628 : : #else /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
4629 : : static inline unsigned long __meminit zone_spanned_pages_in_node(int nid,
4630 : : unsigned long zone_type,
4631 : : unsigned long node_start_pfn,
4632 : : unsigned long node_end_pfn,
4633 : : unsigned long *zones_size)
4634 : : {
4635 : 0 : return zones_size[zone_type];
4636 : : }
4637 : :
4638 : : static inline unsigned long __meminit zone_absent_pages_in_node(int nid,
4639 : : unsigned long zone_type,
4640 : : unsigned long node_start_pfn,
4641 : : unsigned long node_end_pfn,
4642 : : unsigned long *zholes_size)
4643 : : {
4644 [ # # ][ # # ]: 0 : if (!zholes_size)
4645 : : return 0;
4646 : :
4647 : 0 : return zholes_size[zone_type];
4648 : : }
4649 : :
4650 : : #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
4651 : :
4652 : 0 : static void __meminit calculate_node_totalpages(struct pglist_data *pgdat,
4653 : : unsigned long node_start_pfn,
4654 : : unsigned long node_end_pfn,
4655 : : unsigned long *zones_size,
4656 : : unsigned long *zholes_size)
4657 : : {
4658 : : unsigned long realtotalpages, totalpages = 0;
4659 : : enum zone_type i;
4660 : :
4661 [ # # ]: 0 : for (i = 0; i < MAX_NR_ZONES; i++)
4662 : 0 : totalpages += zone_spanned_pages_in_node(pgdat->node_id, i,
4663 : : node_start_pfn,
4664 : : node_end_pfn,
4665 : : zones_size);
4666 : 0 : pgdat->node_spanned_pages = totalpages;
4667 : :
4668 : : realtotalpages = totalpages;
4669 [ # # ]: 0 : for (i = 0; i < MAX_NR_ZONES; i++)
4670 : 0 : realtotalpages -=
4671 : : zone_absent_pages_in_node(pgdat->node_id, i,
4672 : : node_start_pfn, node_end_pfn,
4673 : : zholes_size);
4674 : 0 : pgdat->node_present_pages = realtotalpages;
4675 : 0 : printk(KERN_DEBUG "On node %d totalpages: %lu\n", pgdat->node_id,
4676 : : realtotalpages);
4677 : 0 : }
4678 : :
4679 : : #ifndef CONFIG_SPARSEMEM
4680 : : /*
4681 : : * Calculate the size of the zone->blockflags rounded to an unsigned long
4682 : : * Start by making sure zonesize is a multiple of pageblock_order by rounding
4683 : : * up. Then use 1 NR_PAGEBLOCK_BITS worth of bits per pageblock, finally
4684 : : * round what is now in bits to nearest long in bits, then return it in
4685 : : * bytes.
4686 : : */
4687 : 0 : static unsigned long __init usemap_size(unsigned long zone_start_pfn, unsigned long zonesize)
4688 : : {
4689 : : unsigned long usemapsize;
4690 : :
4691 : 0 : zonesize += zone_start_pfn & (pageblock_nr_pages-1);
4692 : 0 : usemapsize = roundup(zonesize, pageblock_nr_pages);
4693 : 0 : usemapsize = usemapsize >> pageblock_order;
4694 : 0 : usemapsize *= NR_PAGEBLOCK_BITS;
4695 : 0 : usemapsize = roundup(usemapsize, 8 * sizeof(unsigned long));
4696 : :
4697 : 0 : return usemapsize / 8;
4698 : : }
4699 : :
4700 : 0 : static void __init setup_usemap(struct pglist_data *pgdat,
4701 : : struct zone *zone,
4702 : : unsigned long zone_start_pfn,
4703 : : unsigned long zonesize)
4704 : : {
4705 : 0 : unsigned long usemapsize = usemap_size(zone_start_pfn, zonesize);
4706 : 0 : zone->pageblock_flags = NULL;
4707 [ # # ]: 0 : if (usemapsize)
4708 : 0 : zone->pageblock_flags =
4709 : 0 : memblock_virt_alloc_node_nopanic(usemapsize,
4710 : : pgdat->node_id);
4711 : 0 : }
4712 : : #else
4713 : : static inline void setup_usemap(struct pglist_data *pgdat, struct zone *zone,
4714 : : unsigned long zone_start_pfn, unsigned long zonesize) {}
4715 : : #endif /* CONFIG_SPARSEMEM */
4716 : :
4717 : : #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
4718 : :
4719 : : /* Initialise the number of pages represented by NR_PAGEBLOCK_BITS */
4720 : : void __paginginit set_pageblock_order(void)
4721 : : {
4722 : : unsigned int order;
4723 : :
4724 : : /* Check that pageblock_nr_pages has not already been setup */
4725 : : if (pageblock_order)
4726 : : return;
4727 : :
4728 : : if (HPAGE_SHIFT > PAGE_SHIFT)
4729 : : order = HUGETLB_PAGE_ORDER;
4730 : : else
4731 : : order = MAX_ORDER - 1;
4732 : :
4733 : : /*
4734 : : * Assume the largest contiguous order of interest is a huge page.
4735 : : * This value may be variable depending on boot parameters on IA64 and
4736 : : * powerpc.
4737 : : */
4738 : : pageblock_order = order;
4739 : : }
4740 : : #else /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4741 : :
4742 : : /*
4743 : : * When CONFIG_HUGETLB_PAGE_SIZE_VARIABLE is not set, set_pageblock_order()
4744 : : * is unused as pageblock_order is set at compile-time. See
4745 : : * include/linux/pageblock-flags.h for the values of pageblock_order based on
4746 : : * the kernel config
4747 : : */
4748 : 0 : void __paginginit set_pageblock_order(void)
4749 : : {
4750 : 0 : }
4751 : :
4752 : : #endif /* CONFIG_HUGETLB_PAGE_SIZE_VARIABLE */
4753 : :
4754 : : static unsigned long __paginginit calc_memmap_size(unsigned long spanned_pages,
4755 : : unsigned long present_pages)
4756 : : {
4757 : : unsigned long pages = spanned_pages;
4758 : :
4759 : : /*
4760 : : * Provide a more accurate estimation if there are holes within
4761 : : * the zone and SPARSEMEM is in use. If there are holes within the
4762 : : * zone, each populated memory region may cost us one or two extra
4763 : : * memmap pages due to alignment because memmap pages for each
4764 : : * populated regions may not naturally algined on page boundary.
4765 : : * So the (present_pages >> 4) heuristic is a tradeoff for that.
4766 : : */
4767 : : if (spanned_pages > present_pages + (present_pages >> 4) &&
4768 : : IS_ENABLED(CONFIG_SPARSEMEM))
4769 : : pages = present_pages;
4770 : :
4771 : 0 : return PAGE_ALIGN(pages * sizeof(struct page)) >> PAGE_SHIFT;
4772 : : }
4773 : :
4774 : : /*
4775 : : * Set up the zone data structures:
4776 : : * - mark all pages reserved
4777 : : * - mark all memory queues empty
4778 : : * - clear the memory bitmaps
4779 : : *
4780 : : * NOTE: pgdat should get zeroed by caller.
4781 : : */
4782 : 0 : static void __paginginit free_area_init_core(struct pglist_data *pgdat,
4783 : : unsigned long node_start_pfn, unsigned long node_end_pfn,
4784 : : unsigned long *zones_size, unsigned long *zholes_size)
4785 : : {
4786 : : enum zone_type j;
4787 : 0 : int nid = pgdat->node_id;
4788 : 0 : unsigned long zone_start_pfn = pgdat->node_start_pfn;
4789 : : int ret;
4790 : :
4791 : : pgdat_resize_init(pgdat);
4792 : : #ifdef CONFIG_NUMA_BALANCING
4793 : : spin_lock_init(&pgdat->numabalancing_migrate_lock);
4794 : : pgdat->numabalancing_migrate_nr_pages = 0;
4795 : : pgdat->numabalancing_migrate_next_window = jiffies;
4796 : : #endif
4797 : 0 : init_waitqueue_head(&pgdat->kswapd_wait);
4798 : 0 : init_waitqueue_head(&pgdat->pfmemalloc_wait);
4799 : : pgdat_page_cgroup_init(pgdat);
4800 : :
4801 [ # # ]: 0 : for (j = 0; j < MAX_NR_ZONES; j++) {
4802 : 0 : struct zone *zone = pgdat->node_zones + j;
4803 : : unsigned long size, realsize, freesize, memmap_pages;
4804 : :
4805 : : size = zone_spanned_pages_in_node(nid, j, node_start_pfn,
4806 : : node_end_pfn, zones_size);
4807 : 0 : realsize = freesize = size - zone_absent_pages_in_node(nid, j,
4808 : : node_start_pfn,
4809 : : node_end_pfn,
4810 : : zholes_size);
4811 : :
4812 : : /*
4813 : : * Adjust freesize so that it accounts for how much memory
4814 : : * is used by this zone for memmap. This affects the watermark
4815 : : * and per-cpu initialisations
4816 : : */
4817 : : memmap_pages = calc_memmap_size(size, realsize);
4818 [ # # ]: 0 : if (freesize >= memmap_pages) {
4819 : 0 : freesize -= memmap_pages;
4820 [ # # ]: 0 : if (memmap_pages)
4821 : 0 : printk(KERN_DEBUG
4822 : : " %s zone: %lu pages used for memmap\n",
4823 : : zone_names[j], memmap_pages);
4824 : : } else
4825 : 0 : printk(KERN_WARNING
4826 : : " %s zone: %lu pages exceeds freesize %lu\n",
4827 : : zone_names[j], memmap_pages, freesize);
4828 : :
4829 : : /* Account for reserved pages */
4830 [ # # ][ # # ]: 0 : if (j == 0 && freesize > dma_reserve) {
4831 : 0 : freesize -= dma_reserve;
4832 : 0 : printk(KERN_DEBUG " %s zone: %lu pages reserved\n",
4833 : : zone_names[0], dma_reserve);
4834 : : }
4835 : :
4836 [ # # ]: 0 : if (!is_highmem_idx(j))
4837 : 0 : nr_kernel_pages += freesize;
4838 : : /* Charge for highmem memmap if there are enough kernel pages */
4839 [ # # ]: 0 : else if (nr_kernel_pages > memmap_pages * 2)
4840 : 0 : nr_kernel_pages -= memmap_pages;
4841 : 0 : nr_all_pages += freesize;
4842 : :
4843 : 0 : zone->spanned_pages = size;
4844 : 0 : zone->present_pages = realsize;
4845 : : /*
4846 : : * Set an approximate value for lowmem here, it will be adjusted
4847 : : * when the bootmem allocator frees pages into the buddy system.
4848 : : * And all highmem pages will be managed by the buddy system.
4849 : : */
4850 [ # # ]: 0 : zone->managed_pages = is_highmem_idx(j) ? realsize : freesize;
4851 : : #ifdef CONFIG_NUMA
4852 : : zone->node = nid;
4853 : : zone->min_unmapped_pages = (freesize*sysctl_min_unmapped_ratio)
4854 : : / 100;
4855 : : zone->min_slab_pages = (freesize * sysctl_min_slab_ratio) / 100;
4856 : : #endif
4857 : 0 : zone->name = zone_names[j];
4858 : 0 : spin_lock_init(&zone->lock);
4859 : 0 : spin_lock_init(&zone->lru_lock);
4860 : : zone_seqlock_init(zone);
4861 : 0 : zone->zone_pgdat = pgdat;
4862 : 0 : zone_pcp_init(zone);
4863 : :
4864 : : /* For bootup, initialized properly in watermark setup */
4865 : 0 : mod_zone_page_state(zone, NR_ALLOC_BATCH, zone->managed_pages);
4866 : :
4867 : 0 : lruvec_init(&zone->lruvec);
4868 [ # # ]: 0 : if (!size)
4869 : 0 : continue;
4870 : :
4871 : : set_pageblock_order();
4872 : 0 : setup_usemap(pgdat, zone, zone_start_pfn, size);
4873 : 0 : ret = init_currently_empty_zone(zone, zone_start_pfn,
4874 : : size, MEMMAP_EARLY);
4875 [ # # ]: 0 : BUG_ON(ret);
4876 : 0 : memmap_init(size, nid, j, zone_start_pfn);
4877 : 0 : zone_start_pfn += size;
4878 : : }
4879 : 0 : }
4880 : :
4881 : 0 : static void __init_refok alloc_node_mem_map(struct pglist_data *pgdat)
4882 : : {
4883 : : /* Skip empty nodes */
4884 [ # # ]: 0 : if (!pgdat->node_spanned_pages)
4885 : 0 : return;
4886 : :
4887 : : #ifdef CONFIG_FLAT_NODE_MEM_MAP
4888 : : /* ia64 gets its own node_mem_map, before this, without bootmem */
4889 [ # # ]: 0 : if (!pgdat->node_mem_map) {
4890 : : unsigned long size, start, end;
4891 : : struct page *map;
4892 : :
4893 : : /*
4894 : : * The zone's endpoints aren't required to be MAX_ORDER
4895 : : * aligned but the node_mem_map endpoints must be in order
4896 : : * for the buddy allocator to function correctly.
4897 : : */
4898 : 0 : start = pgdat->node_start_pfn & ~(MAX_ORDER_NR_PAGES - 1);
4899 : : end = pgdat_end_pfn(pgdat);
4900 : 0 : end = ALIGN(end, MAX_ORDER_NR_PAGES);
4901 : 0 : size = (end - start) * sizeof(struct page);
4902 : : map = alloc_remap(pgdat->node_id, size);
4903 : : if (!map)
4904 : 0 : map = memblock_virt_alloc_node_nopanic(size,
4905 : : pgdat->node_id);
4906 : 0 : pgdat->node_mem_map = map + (pgdat->node_start_pfn - start);
4907 : : }
4908 : : #ifndef CONFIG_NEED_MULTIPLE_NODES
4909 : : /*
4910 : : * With no DISCONTIG, the global mem_map is just set as node 0's
4911 : : */
4912 [ # # ]: 0 : if (pgdat == NODE_DATA(0)) {
4913 : 0 : mem_map = NODE_DATA(0)->node_mem_map;
4914 : : #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
4915 : : if (page_to_pfn(mem_map) != pgdat->node_start_pfn)
4916 : : mem_map -= (pgdat->node_start_pfn - ARCH_PFN_OFFSET);
4917 : : #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
4918 : : }
4919 : : #endif
4920 : : #endif /* CONFIG_FLAT_NODE_MEM_MAP */
4921 : : }
4922 : :
4923 : 0 : void __paginginit free_area_init_node(int nid, unsigned long *zones_size,
4924 : : unsigned long node_start_pfn, unsigned long *zholes_size)
4925 : : {
4926 : : pg_data_t *pgdat = NODE_DATA(nid);
4927 : : unsigned long start_pfn = 0;
4928 : : unsigned long end_pfn = 0;
4929 : :
4930 : : /* pg_data_t should be reset to zero when it's allocated */
4931 [ # # ][ # # ]: 0 : WARN_ON(pgdat->nr_zones || pgdat->classzone_idx);
[ # # ]
4932 : :
4933 : 0 : pgdat->node_id = nid;
4934 : 0 : pgdat->node_start_pfn = node_start_pfn;
4935 : : init_zone_allows_reclaim(nid);
4936 : : #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
4937 : : get_pfn_range_for_nid(nid, &start_pfn, &end_pfn);
4938 : : #endif
4939 : 0 : calculate_node_totalpages(pgdat, start_pfn, end_pfn,
4940 : : zones_size, zholes_size);
4941 : :
4942 : 0 : alloc_node_mem_map(pgdat);
4943 : : #ifdef CONFIG_FLAT_NODE_MEM_MAP
4944 : 0 : printk(KERN_DEBUG "free_area_init_node: node %d, pgdat %08lx, node_mem_map %08lx\n",
4945 : : nid, (unsigned long)pgdat,
4946 : 0 : (unsigned long)pgdat->node_mem_map);
4947 : : #endif
4948 : :
4949 : 0 : free_area_init_core(pgdat, start_pfn, end_pfn,
4950 : : zones_size, zholes_size);
4951 : 0 : }
4952 : :
4953 : : #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
4954 : :
4955 : : #if MAX_NUMNODES > 1
4956 : : /*
4957 : : * Figure out the number of possible node ids.
4958 : : */
4959 : : void __init setup_nr_node_ids(void)
4960 : : {
4961 : : unsigned int node;
4962 : : unsigned int highest = 0;
4963 : :
4964 : : for_each_node_mask(node, node_possible_map)
4965 : : highest = node;
4966 : : nr_node_ids = highest + 1;
4967 : : }
4968 : : #endif
4969 : :
4970 : : /**
4971 : : * node_map_pfn_alignment - determine the maximum internode alignment
4972 : : *
4973 : : * This function should be called after node map is populated and sorted.
4974 : : * It calculates the maximum power of two alignment which can distinguish
4975 : : * all the nodes.
4976 : : *
4977 : : * For example, if all nodes are 1GiB and aligned to 1GiB, the return value
4978 : : * would indicate 1GiB alignment with (1 << (30 - PAGE_SHIFT)). If the
4979 : : * nodes are shifted by 256MiB, 256MiB. Note that if only the last node is
4980 : : * shifted, 1GiB is enough and this function will indicate so.
4981 : : *
4982 : : * This is used to test whether pfn -> nid mapping of the chosen memory
4983 : : * model has fine enough granularity to avoid incorrect mapping for the
4984 : : * populated node map.
4985 : : *
4986 : : * Returns the determined alignment in pfn's. 0 if there is no alignment
4987 : : * requirement (single node).
4988 : : */
4989 : : unsigned long __init node_map_pfn_alignment(void)
4990 : : {
4991 : : unsigned long accl_mask = 0, last_end = 0;
4992 : : unsigned long start, end, mask;
4993 : : int last_nid = -1;
4994 : : int i, nid;
4995 : :
4996 : : for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid) {
4997 : : if (!start || last_nid < 0 || last_nid == nid) {
4998 : : last_nid = nid;
4999 : : last_end = end;
5000 : : continue;
5001 : : }
5002 : :
5003 : : /*
5004 : : * Start with a mask granular enough to pin-point to the
5005 : : * start pfn and tick off bits one-by-one until it becomes
5006 : : * too coarse to separate the current node from the last.
5007 : : */
5008 : : mask = ~((1 << __ffs(start)) - 1);
5009 : : while (mask && last_end <= (start & (mask << 1)))
5010 : : mask <<= 1;
5011 : :
5012 : : /* accumulate all internode masks */
5013 : : accl_mask |= mask;
5014 : : }
5015 : :
5016 : : /* convert mask to number of pages */
5017 : : return ~accl_mask + 1;
5018 : : }
5019 : :
5020 : : /* Find the lowest pfn for a node */
5021 : : static unsigned long __init find_min_pfn_for_node(int nid)
5022 : : {
5023 : : unsigned long min_pfn = ULONG_MAX;
5024 : : unsigned long start_pfn;
5025 : : int i;
5026 : :
5027 : : for_each_mem_pfn_range(i, nid, &start_pfn, NULL, NULL)
5028 : : min_pfn = min(min_pfn, start_pfn);
5029 : :
5030 : : if (min_pfn == ULONG_MAX) {
5031 : : printk(KERN_WARNING
5032 : : "Could not find start_pfn for node %d\n", nid);
5033 : : return 0;
5034 : : }
5035 : :
5036 : : return min_pfn;
5037 : : }
5038 : :
5039 : : /**
5040 : : * find_min_pfn_with_active_regions - Find the minimum PFN registered
5041 : : *
5042 : : * It returns the minimum PFN based on information provided via
5043 : : * add_active_range().
5044 : : */
5045 : : unsigned long __init find_min_pfn_with_active_regions(void)
5046 : : {
5047 : : return find_min_pfn_for_node(MAX_NUMNODES);
5048 : : }
5049 : :
5050 : : /*
5051 : : * early_calculate_totalpages()
5052 : : * Sum pages in active regions for movable zone.
5053 : : * Populate N_MEMORY for calculating usable_nodes.
5054 : : */
5055 : : static unsigned long __init early_calculate_totalpages(void)
5056 : : {
5057 : : unsigned long totalpages = 0;
5058 : : unsigned long start_pfn, end_pfn;
5059 : : int i, nid;
5060 : :
5061 : : for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
5062 : : unsigned long pages = end_pfn - start_pfn;
5063 : :
5064 : : totalpages += pages;
5065 : : if (pages)
5066 : : node_set_state(nid, N_MEMORY);
5067 : : }
5068 : : return totalpages;
5069 : : }
5070 : :
5071 : : /*
5072 : : * Find the PFN the Movable zone begins in each node. Kernel memory
5073 : : * is spread evenly between nodes as long as the nodes have enough
5074 : : * memory. When they don't, some nodes will have more kernelcore than
5075 : : * others
5076 : : */
5077 : : static void __init find_zone_movable_pfns_for_nodes(void)
5078 : : {
5079 : : int i, nid;
5080 : : unsigned long usable_startpfn;
5081 : : unsigned long kernelcore_node, kernelcore_remaining;
5082 : : /* save the state before borrow the nodemask */
5083 : : nodemask_t saved_node_state = node_states[N_MEMORY];
5084 : : unsigned long totalpages = early_calculate_totalpages();
5085 : : int usable_nodes = nodes_weight(node_states[N_MEMORY]);
5086 : : struct memblock_type *type = &memblock.memory;
5087 : :
5088 : : /* Need to find movable_zone earlier when movable_node is specified. */
5089 : : find_usable_zone_for_movable();
5090 : :
5091 : : /*
5092 : : * If movable_node is specified, ignore kernelcore and movablecore
5093 : : * options.
5094 : : */
5095 : : if (movable_node_is_enabled()) {
5096 : : for (i = 0; i < type->cnt; i++) {
5097 : : if (!memblock_is_hotpluggable(&type->regions[i]))
5098 : : continue;
5099 : :
5100 : : nid = type->regions[i].nid;
5101 : :
5102 : : usable_startpfn = PFN_DOWN(type->regions[i].base);
5103 : : zone_movable_pfn[nid] = zone_movable_pfn[nid] ?
5104 : : min(usable_startpfn, zone_movable_pfn[nid]) :
5105 : : usable_startpfn;
5106 : : }
5107 : :
5108 : : goto out2;
5109 : : }
5110 : :
5111 : : /*
5112 : : * If movablecore=nn[KMG] was specified, calculate what size of
5113 : : * kernelcore that corresponds so that memory usable for
5114 : : * any allocation type is evenly spread. If both kernelcore
5115 : : * and movablecore are specified, then the value of kernelcore
5116 : : * will be used for required_kernelcore if it's greater than
5117 : : * what movablecore would have allowed.
5118 : : */
5119 : : if (required_movablecore) {
5120 : : unsigned long corepages;
5121 : :
5122 : : /*
5123 : : * Round-up so that ZONE_MOVABLE is at least as large as what
5124 : : * was requested by the user
5125 : : */
5126 : : required_movablecore =
5127 : : roundup(required_movablecore, MAX_ORDER_NR_PAGES);
5128 : : corepages = totalpages - required_movablecore;
5129 : :
5130 : : required_kernelcore = max(required_kernelcore, corepages);
5131 : : }
5132 : :
5133 : : /* If kernelcore was not specified, there is no ZONE_MOVABLE */
5134 : : if (!required_kernelcore)
5135 : : goto out;
5136 : :
5137 : : /* usable_startpfn is the lowest possible pfn ZONE_MOVABLE can be at */
5138 : : usable_startpfn = arch_zone_lowest_possible_pfn[movable_zone];
5139 : :
5140 : : restart:
5141 : : /* Spread kernelcore memory as evenly as possible throughout nodes */
5142 : : kernelcore_node = required_kernelcore / usable_nodes;
5143 : : for_each_node_state(nid, N_MEMORY) {
5144 : : unsigned long start_pfn, end_pfn;
5145 : :
5146 : : /*
5147 : : * Recalculate kernelcore_node if the division per node
5148 : : * now exceeds what is necessary to satisfy the requested
5149 : : * amount of memory for the kernel
5150 : : */
5151 : : if (required_kernelcore < kernelcore_node)
5152 : : kernelcore_node = required_kernelcore / usable_nodes;
5153 : :
5154 : : /*
5155 : : * As the map is walked, we track how much memory is usable
5156 : : * by the kernel using kernelcore_remaining. When it is
5157 : : * 0, the rest of the node is usable by ZONE_MOVABLE
5158 : : */
5159 : : kernelcore_remaining = kernelcore_node;
5160 : :
5161 : : /* Go through each range of PFNs within this node */
5162 : : for_each_mem_pfn_range(i, nid, &start_pfn, &end_pfn, NULL) {
5163 : : unsigned long size_pages;
5164 : :
5165 : : start_pfn = max(start_pfn, zone_movable_pfn[nid]);
5166 : : if (start_pfn >= end_pfn)
5167 : : continue;
5168 : :
5169 : : /* Account for what is only usable for kernelcore */
5170 : : if (start_pfn < usable_startpfn) {
5171 : : unsigned long kernel_pages;
5172 : : kernel_pages = min(end_pfn, usable_startpfn)
5173 : : - start_pfn;
5174 : :
5175 : : kernelcore_remaining -= min(kernel_pages,
5176 : : kernelcore_remaining);
5177 : : required_kernelcore -= min(kernel_pages,
5178 : : required_kernelcore);
5179 : :
5180 : : /* Continue if range is now fully accounted */
5181 : : if (end_pfn <= usable_startpfn) {
5182 : :
5183 : : /*
5184 : : * Push zone_movable_pfn to the end so
5185 : : * that if we have to rebalance
5186 : : * kernelcore across nodes, we will
5187 : : * not double account here
5188 : : */
5189 : : zone_movable_pfn[nid] = end_pfn;
5190 : : continue;
5191 : : }
5192 : : start_pfn = usable_startpfn;
5193 : : }
5194 : :
5195 : : /*
5196 : : * The usable PFN range for ZONE_MOVABLE is from
5197 : : * start_pfn->end_pfn. Calculate size_pages as the
5198 : : * number of pages used as kernelcore
5199 : : */
5200 : : size_pages = end_pfn - start_pfn;
5201 : : if (size_pages > kernelcore_remaining)
5202 : : size_pages = kernelcore_remaining;
5203 : : zone_movable_pfn[nid] = start_pfn + size_pages;
5204 : :
5205 : : /*
5206 : : * Some kernelcore has been met, update counts and
5207 : : * break if the kernelcore for this node has been
5208 : : * satisfied
5209 : : */
5210 : : required_kernelcore -= min(required_kernelcore,
5211 : : size_pages);
5212 : : kernelcore_remaining -= size_pages;
5213 : : if (!kernelcore_remaining)
5214 : : break;
5215 : : }
5216 : : }
5217 : :
5218 : : /*
5219 : : * If there is still required_kernelcore, we do another pass with one
5220 : : * less node in the count. This will push zone_movable_pfn[nid] further
5221 : : * along on the nodes that still have memory until kernelcore is
5222 : : * satisfied
5223 : : */
5224 : : usable_nodes--;
5225 : : if (usable_nodes && required_kernelcore > usable_nodes)
5226 : : goto restart;
5227 : :
5228 : : out2:
5229 : : /* Align start of ZONE_MOVABLE on all nids to MAX_ORDER_NR_PAGES */
5230 : : for (nid = 0; nid < MAX_NUMNODES; nid++)
5231 : : zone_movable_pfn[nid] =
5232 : : roundup(zone_movable_pfn[nid], MAX_ORDER_NR_PAGES);
5233 : :
5234 : : out:
5235 : : /* restore the node_state */
5236 : : node_states[N_MEMORY] = saved_node_state;
5237 : : }
5238 : :
5239 : : /* Any regular or high memory on that node ? */
5240 : : static void check_for_memory(pg_data_t *pgdat, int nid)
5241 : : {
5242 : : enum zone_type zone_type;
5243 : :
5244 : : if (N_MEMORY == N_NORMAL_MEMORY)
5245 : : return;
5246 : :
5247 : : for (zone_type = 0; zone_type <= ZONE_MOVABLE - 1; zone_type++) {
5248 : : struct zone *zone = &pgdat->node_zones[zone_type];
5249 : : if (populated_zone(zone)) {
5250 : : node_set_state(nid, N_HIGH_MEMORY);
5251 : : if (N_NORMAL_MEMORY != N_HIGH_MEMORY &&
5252 : : zone_type <= ZONE_NORMAL)
5253 : : node_set_state(nid, N_NORMAL_MEMORY);
5254 : : break;
5255 : : }
5256 : : }
5257 : : }
5258 : :
5259 : : /**
5260 : : * free_area_init_nodes - Initialise all pg_data_t and zone data
5261 : : * @max_zone_pfn: an array of max PFNs for each zone
5262 : : *
5263 : : * This will call free_area_init_node() for each active node in the system.
5264 : : * Using the page ranges provided by add_active_range(), the size of each
5265 : : * zone in each node and their holes is calculated. If the maximum PFN
5266 : : * between two adjacent zones match, it is assumed that the zone is empty.
5267 : : * For example, if arch_max_dma_pfn == arch_max_dma32_pfn, it is assumed
5268 : : * that arch_max_dma32_pfn has no pages. It is also assumed that a zone
5269 : : * starts where the previous one ended. For example, ZONE_DMA32 starts
5270 : : * at arch_max_dma_pfn.
5271 : : */
5272 : : void __init free_area_init_nodes(unsigned long *max_zone_pfn)
5273 : : {
5274 : : unsigned long start_pfn, end_pfn;
5275 : : int i, nid;
5276 : :
5277 : : /* Record where the zone boundaries are */
5278 : : memset(arch_zone_lowest_possible_pfn, 0,
5279 : : sizeof(arch_zone_lowest_possible_pfn));
5280 : : memset(arch_zone_highest_possible_pfn, 0,
5281 : : sizeof(arch_zone_highest_possible_pfn));
5282 : : arch_zone_lowest_possible_pfn[0] = find_min_pfn_with_active_regions();
5283 : : arch_zone_highest_possible_pfn[0] = max_zone_pfn[0];
5284 : : for (i = 1; i < MAX_NR_ZONES; i++) {
5285 : : if (i == ZONE_MOVABLE)
5286 : : continue;
5287 : : arch_zone_lowest_possible_pfn[i] =
5288 : : arch_zone_highest_possible_pfn[i-1];
5289 : : arch_zone_highest_possible_pfn[i] =
5290 : : max(max_zone_pfn[i], arch_zone_lowest_possible_pfn[i]);
5291 : : }
5292 : : arch_zone_lowest_possible_pfn[ZONE_MOVABLE] = 0;
5293 : : arch_zone_highest_possible_pfn[ZONE_MOVABLE] = 0;
5294 : :
5295 : : /* Find the PFNs that ZONE_MOVABLE begins at in each node */
5296 : : memset(zone_movable_pfn, 0, sizeof(zone_movable_pfn));
5297 : : find_zone_movable_pfns_for_nodes();
5298 : :
5299 : : /* Print out the zone ranges */
5300 : : printk("Zone ranges:\n");
5301 : : for (i = 0; i < MAX_NR_ZONES; i++) {
5302 : : if (i == ZONE_MOVABLE)
5303 : : continue;
5304 : : printk(KERN_CONT " %-8s ", zone_names[i]);
5305 : : if (arch_zone_lowest_possible_pfn[i] ==
5306 : : arch_zone_highest_possible_pfn[i])
5307 : : printk(KERN_CONT "empty\n");
5308 : : else
5309 : : printk(KERN_CONT "[mem %0#10lx-%0#10lx]\n",
5310 : : arch_zone_lowest_possible_pfn[i] << PAGE_SHIFT,
5311 : : (arch_zone_highest_possible_pfn[i]
5312 : : << PAGE_SHIFT) - 1);
5313 : : }
5314 : :
5315 : : /* Print out the PFNs ZONE_MOVABLE begins at in each node */
5316 : : printk("Movable zone start for each node\n");
5317 : : for (i = 0; i < MAX_NUMNODES; i++) {
5318 : : if (zone_movable_pfn[i])
5319 : : printk(" Node %d: %#010lx\n", i,
5320 : : zone_movable_pfn[i] << PAGE_SHIFT);
5321 : : }
5322 : :
5323 : : /* Print out the early node map */
5324 : : printk("Early memory node ranges\n");
5325 : : for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid)
5326 : : printk(" node %3d: [mem %#010lx-%#010lx]\n", nid,
5327 : : start_pfn << PAGE_SHIFT, (end_pfn << PAGE_SHIFT) - 1);
5328 : :
5329 : : /* Initialise every node */
5330 : : mminit_verify_pageflags_layout();
5331 : : setup_nr_node_ids();
5332 : : for_each_online_node(nid) {
5333 : : pg_data_t *pgdat = NODE_DATA(nid);
5334 : : free_area_init_node(nid, NULL,
5335 : : find_min_pfn_for_node(nid), NULL);
5336 : :
5337 : : /* Any memory on that node */
5338 : : if (pgdat->node_present_pages)
5339 : : node_set_state(nid, N_MEMORY);
5340 : : check_for_memory(pgdat, nid);
5341 : : }
5342 : : }
5343 : :
5344 : : static int __init cmdline_parse_core(char *p, unsigned long *core)
5345 : : {
5346 : : unsigned long long coremem;
5347 : : if (!p)
5348 : : return -EINVAL;
5349 : :
5350 : : coremem = memparse(p, &p);
5351 : : *core = coremem >> PAGE_SHIFT;
5352 : :
5353 : : /* Paranoid check that UL is enough for the coremem value */
5354 : : WARN_ON((coremem >> PAGE_SHIFT) > ULONG_MAX);
5355 : :
5356 : : return 0;
5357 : : }
5358 : :
5359 : : /*
5360 : : * kernelcore=size sets the amount of memory for use for allocations that
5361 : : * cannot be reclaimed or migrated.
5362 : : */
5363 : : static int __init cmdline_parse_kernelcore(char *p)
5364 : : {
5365 : : return cmdline_parse_core(p, &required_kernelcore);
5366 : : }
5367 : :
5368 : : /*
5369 : : * movablecore=size sets the amount of memory for use for allocations that
5370 : : * can be reclaimed or migrated.
5371 : : */
5372 : : static int __init cmdline_parse_movablecore(char *p)
5373 : : {
5374 : : return cmdline_parse_core(p, &required_movablecore);
5375 : : }
5376 : :
5377 : : early_param("kernelcore", cmdline_parse_kernelcore);
5378 : : early_param("movablecore", cmdline_parse_movablecore);
5379 : :
5380 : : #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
5381 : :
5382 : 0 : void adjust_managed_page_count(struct page *page, long count)
5383 : : {
5384 : : spin_lock(&managed_page_count_lock);
5385 : 0 : page_zone(page)->managed_pages += count;
5386 : 0 : totalram_pages += count;
5387 : : #ifdef CONFIG_HIGHMEM
5388 [ # # ]: 0 : if (PageHighMem(page))
5389 : 0 : totalhigh_pages += count;
5390 : : #endif
5391 : : spin_unlock(&managed_page_count_lock);
5392 : 0 : }
5393 : : EXPORT_SYMBOL(adjust_managed_page_count);
5394 : :
5395 : 0 : unsigned long free_reserved_area(void *start, void *end, int poison, char *s)
5396 : : {
5397 : : void *pos;
5398 : : unsigned long pages = 0;
5399 : :
5400 : 0 : start = (void *)PAGE_ALIGN((unsigned long)start);
5401 : 0 : end = (void *)((unsigned long)end & PAGE_MASK);
5402 [ # # ]: 0 : for (pos = start; pos < end; pos += PAGE_SIZE, pages++) {
5403 [ # # ]: 0 : if ((unsigned int)poison <= 0xFF)
5404 [ # # ][ # # ]: 0 : memset(pos, poison, PAGE_SIZE);
5405 : 0 : free_reserved_page(virt_to_page(pos));
5406 : : }
5407 : :
5408 [ # # ]: 0 : if (pages && s)
5409 : 0 : pr_info("Freeing %s memory: %ldK (%p - %p)\n",
5410 : : s, pages << (PAGE_SHIFT - 10), start, end);
5411 : :
5412 : 0 : return pages;
5413 : : }
5414 : : EXPORT_SYMBOL(free_reserved_area);
5415 : :
5416 : : #ifdef CONFIG_HIGHMEM
5417 : 0 : void free_highmem_page(struct page *page)
5418 : : {
5419 : : __free_reserved_page(page);
5420 : 0 : totalram_pages++;
5421 : 0 : page_zone(page)->managed_pages++;
5422 : 0 : totalhigh_pages++;
5423 : 0 : }
5424 : : #endif
5425 : :
5426 : :
5427 : 0 : void __init mem_init_print_info(const char *str)
5428 : : {
5429 : : unsigned long physpages, codesize, datasize, rosize, bss_size;
5430 : : unsigned long init_code_size, init_data_size;
5431 : :
5432 : : physpages = get_num_physpages();
5433 : 0 : codesize = _etext - _stext;
5434 : 0 : datasize = _edata - _sdata;
5435 : 0 : rosize = __end_rodata - __start_rodata;
5436 : 0 : bss_size = __bss_stop - __bss_start;
5437 : 0 : init_data_size = __init_end - __init_begin;
5438 : 0 : init_code_size = _einittext - _sinittext;
5439 : :
5440 : : /*
5441 : : * Detect special cases and adjust section sizes accordingly:
5442 : : * 1) .init.* may be embedded into .data sections
5443 : : * 2) .init.text.* may be out of [__init_begin, __init_end],
5444 : : * please refer to arch/tile/kernel/vmlinux.lds.S.
5445 : : * 3) .rodata.* may be embedded into .text or .data sections.
5446 : : */
5447 : : #define adj_init_size(start, end, size, pos, adj) \
5448 : : do { \
5449 : : if (start <= pos && pos < end && size > adj) \
5450 : : size -= adj; \
5451 : : } while (0)
5452 : :
5453 [ # # ][ # # ]: 0 : adj_init_size(__init_begin, __init_end, init_data_size,
[ # # ]
5454 : : _sinittext, init_code_size);
5455 [ # # ][ # # ]: 0 : adj_init_size(_stext, _etext, codesize, _sinittext, init_code_size);
[ # # ]
5456 [ # # ][ # # ]: 0 : adj_init_size(_sdata, _edata, datasize, __init_begin, init_data_size);
[ # # ]
5457 [ # # ][ # # ]: 0 : adj_init_size(_stext, _etext, codesize, __start_rodata, rosize);
[ # # ]
5458 [ # # ][ # # ]: 0 : adj_init_size(_sdata, _edata, datasize, __start_rodata, rosize);
[ # # ]
5459 : :
5460 : : #undef adj_init_size
5461 : :
5462 [ # # ][ # # ]: 0 : printk("Memory: %luK/%luK available "
5463 : : "(%luK kernel code, %luK rwdata, %luK rodata, "
5464 : : "%luK init, %luK bss, %luK reserved"
5465 : : #ifdef CONFIG_HIGHMEM
5466 : : ", %luK highmem"
5467 : : #endif
5468 : : "%s%s)\n",
5469 : : nr_free_pages() << (PAGE_SHIFT-10), physpages << (PAGE_SHIFT-10),
5470 : : codesize >> 10, datasize >> 10, rosize >> 10,
5471 : 0 : (init_data_size + init_code_size) >> 10, bss_size >> 10,
5472 : 0 : (physpages - totalram_pages) << (PAGE_SHIFT-10),
5473 : : #ifdef CONFIG_HIGHMEM
5474 : : totalhigh_pages << (PAGE_SHIFT-10),
5475 : : #endif
5476 : : str ? ", " : "", str ? str : "");
5477 : 0 : }
5478 : :
5479 : : /**
5480 : : * set_dma_reserve - set the specified number of pages reserved in the first zone
5481 : : * @new_dma_reserve: The number of pages to mark reserved
5482 : : *
5483 : : * The per-cpu batchsize and zone watermarks are determined by present_pages.
5484 : : * In the DMA zone, a significant percentage may be consumed by kernel image
5485 : : * and other unfreeable allocations which can skew the watermarks badly. This
5486 : : * function may optionally be used to account for unfreeable pages in the
5487 : : * first zone (e.g., ZONE_DMA). The effect will be lower watermarks and
5488 : : * smaller per-cpu batchsize.
5489 : : */
5490 : 0 : void __init set_dma_reserve(unsigned long new_dma_reserve)
5491 : : {
5492 : 0 : dma_reserve = new_dma_reserve;
5493 : 0 : }
5494 : :
5495 : 0 : void __init free_area_init(unsigned long *zones_size)
5496 : : {
5497 : 0 : free_area_init_node(0, zones_size,
5498 : 0 : __pa(PAGE_OFFSET) >> PAGE_SHIFT, NULL);
5499 : 0 : }
5500 : :
5501 : 0 : static int page_alloc_cpu_notify(struct notifier_block *self,
5502 : : unsigned long action, void *hcpu)
5503 : : {
5504 : 555 : int cpu = (unsigned long)hcpu;
5505 : :
5506 [ + + ]: 555 : if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
5507 : 78 : lru_add_drain_cpu(cpu);
5508 : 78 : drain_pages(cpu);
5509 : :
5510 : : /*
5511 : : * Spill the event counters of the dead processor
5512 : : * into the current processors event counters.
5513 : : * This artificially elevates the count of the current
5514 : : * processor.
5515 : : */
5516 : 78 : vm_events_fold_cpu(cpu);
5517 : :
5518 : : /*
5519 : : * Zero the differential counters of the dead processor
5520 : : * so that the vm statistics are consistent.
5521 : : *
5522 : : * This is only okay since the processor is dead and cannot
5523 : : * race with what we are doing.
5524 : : */
5525 : 78 : cpu_vm_stats_fold(cpu);
5526 : : }
5527 : 0 : return NOTIFY_OK;
5528 : : }
5529 : :
5530 : 0 : void __init page_alloc_init(void)
5531 : : {
5532 : 0 : hotcpu_notifier(page_alloc_cpu_notify, 0);
5533 : 0 : }
5534 : :
5535 : : /*
5536 : : * calculate_totalreserve_pages - called when sysctl_lower_zone_reserve_ratio
5537 : : * or min_free_kbytes changes.
5538 : : */
5539 : 0 : static void calculate_totalreserve_pages(void)
5540 : : {
5541 : : struct pglist_data *pgdat;
5542 : : unsigned long reserve_pages = 0;
5543 : : enum zone_type i, j;
5544 : :
5545 [ + + ]: 12 : for_each_online_pgdat(pgdat) {
5546 [ + - ]: 48 : for (i = 0; i < MAX_NR_ZONES; i++) {
5547 : 36 : struct zone *zone = pgdat->node_zones + i;
5548 : : unsigned long max = 0;
5549 : :
5550 : : /* Find valid and maximum lowmem_reserve in the zone */
5551 [ + + ]: 108 : for (j = i; j < MAX_NR_ZONES; j++) {
5552 [ + + ]: 72 : if (zone->lowmem_reserve[j] > max)
5553 : : max = zone->lowmem_reserve[j];
5554 : : }
5555 : :
5556 : : /* we treat the high watermark as reserved pages. */
5557 : 36 : max += high_wmark_pages(zone);
5558 : :
5559 [ - + ]: 36 : if (max > zone->managed_pages)
5560 : : max = zone->managed_pages;
5561 : 24 : reserve_pages += max;
5562 : : /*
5563 : : * Lowmem reserves are not available to
5564 : : * GFP_HIGHUSER page cache allocations and
5565 : : * kswapd tries to balance zones to their high
5566 : : * watermark. As a result, neither should be
5567 : : * regarded as dirtyable memory, to prevent a
5568 : : * situation where reclaim has to clean pages
5569 : : * in order to balance the zones.
5570 : : */
5571 : 24 : zone->dirty_balance_reserve = max;
5572 : : }
5573 : : }
5574 : 12 : dirty_balance_reserve = reserve_pages;
5575 : 12 : totalreserve_pages = reserve_pages;
5576 : 12 : }
5577 : :
5578 : : /*
5579 : : * setup_per_zone_lowmem_reserve - called whenever
5580 : : * sysctl_lower_zone_reserve_ratio changes. Ensures that each zone
5581 : : * has a correct pages reserved value, so an adequate number of
5582 : : * pages are left in the zone after a successful __alloc_pages().
5583 : : */
5584 : 0 : static void setup_per_zone_lowmem_reserve(void)
5585 : : {
5586 : : struct pglist_data *pgdat;
5587 : : enum zone_type j, idx;
5588 : :
5589 [ + + ]: 2 : for_each_online_pgdat(pgdat) {
5590 [ + - ]: 8 : for (j = 0; j < MAX_NR_ZONES; j++) {
5591 : 6 : struct zone *zone = pgdat->node_zones + j;
5592 : 6 : unsigned long managed_pages = zone->managed_pages;
5593 : :
5594 : 6 : zone->lowmem_reserve[j] = 0;
5595 : :
5596 : : idx = j;
5597 [ + + ]: 12 : while (idx) {
5598 : : struct zone *lower_zone;
5599 : :
5600 : 6 : idx--;
5601 : :
5602 [ - + ]: 6 : if (sysctl_lowmem_reserve_ratio[idx] < 1)
5603 : 0 : sysctl_lowmem_reserve_ratio[idx] = 1;
5604 : :
5605 : 6 : lower_zone = pgdat->node_zones + idx;
5606 : 6 : lower_zone->lowmem_reserve[j] = managed_pages /
5607 : 6 : sysctl_lowmem_reserve_ratio[idx];
5608 : 6 : managed_pages += lower_zone->managed_pages;
5609 : : }
5610 : : }
5611 : : }
5612 : :
5613 : : /* update totalreserve_pages */
5614 : 2 : calculate_totalreserve_pages();
5615 : 2 : }
5616 : :
5617 : 0 : static void __setup_per_zone_wmarks(void)
5618 : : {
5619 : 10 : unsigned long pages_min = min_free_kbytes >> (PAGE_SHIFT - 10);
5620 : 10 : unsigned long pages_low = extra_free_kbytes >> (PAGE_SHIFT - 10);
5621 : : unsigned long lowmem_pages = 0;
5622 : : struct zone *zone;
5623 : : unsigned long flags;
5624 : :
5625 : : /* Calculate total number of !ZONE_HIGHMEM pages */
5626 [ + + ]: 40 : for_each_zone(zone) {
5627 [ + + ]: 30 : if (!is_highmem(zone))
5628 : 20 : lowmem_pages += zone->managed_pages;
5629 : : }
5630 : :
5631 [ + + ]: 40 : for_each_zone(zone) {
5632 : : u64 min, low;
5633 : :
5634 : 30 : spin_lock_irqsave(&zone->lock, flags);
5635 : 40 : min = (u64)pages_min * zone->managed_pages;
5636 [ - + ][ # # ]: 40 : do_div(min, lowmem_pages);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
5637 : 30 : low = (u64)pages_low * zone->managed_pages;
5638 [ - + ][ # # ]: 40 : do_div(low, vm_total_pages);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
5639 : :
5640 [ + + ]: 30 : if (is_highmem(zone)) {
5641 : : /*
5642 : : * __GFP_HIGH and PF_MEMALLOC allocations usually don't
5643 : : * need highmem pages, so cap pages_min to a small
5644 : : * value here.
5645 : : *
5646 : : * The WMARK_HIGH-WMARK_LOW and (WMARK_LOW-WMARK_MIN)
5647 : : * deltas controls asynch page reclaim, and so should
5648 : : * not be capped for highmem.
5649 : : */
5650 : : unsigned long min_pages;
5651 : :
5652 : 10 : min_pages = zone->managed_pages / 1024;
5653 : 10 : min_pages = clamp(min_pages, SWAP_CLUSTER_MAX, 128UL);
5654 : 10 : zone->watermark[WMARK_MIN] = min_pages;
5655 : : } else {
5656 : : /*
5657 : : * If it's a lowmem zone, reserve a number of pages
5658 : : * proportionate to the zone's size.
5659 : : */
5660 : 20 : zone->watermark[WMARK_MIN] = min;
5661 : : }
5662 : :
5663 : 60 : zone->watermark[WMARK_LOW] = min_wmark_pages(zone) +
5664 : 30 : low + (min >> 2);
5665 : 30 : zone->watermark[WMARK_HIGH] = min_wmark_pages(zone) +
5666 : 30 : low + (min >> 1);
5667 : :
5668 : 30 : __mod_zone_page_state(zone, NR_ALLOC_BATCH,
5669 : 30 : high_wmark_pages(zone) -
5670 : 30 : low_wmark_pages(zone) -
5671 : : zone_page_state(zone, NR_ALLOC_BATCH));
5672 : :
5673 : 30 : setup_zone_migrate_reserve(zone);
5674 : : spin_unlock_irqrestore(&zone->lock, flags);
5675 : : }
5676 : :
5677 : : /* update totalreserve_pages */
5678 : 10 : calculate_totalreserve_pages();
5679 : 10 : }
5680 : :
5681 : : /**
5682 : : * setup_per_zone_wmarks - called when min_free_kbytes changes
5683 : : * or when memory is hot-{added|removed}
5684 : : *
5685 : : * Ensures that the watermark[min,low,high] values for each zone are set
5686 : : * correctly with respect to min_free_kbytes.
5687 : : */
5688 : 0 : void setup_per_zone_wmarks(void)
5689 : : {
5690 : 10 : mutex_lock(&zonelists_mutex);
5691 : 10 : __setup_per_zone_wmarks();
5692 : 10 : mutex_unlock(&zonelists_mutex);
5693 : 10 : }
5694 : :
5695 : : /*
5696 : : * The inactive anon list should be small enough that the VM never has to
5697 : : * do too much work, but large enough that each inactive page has a chance
5698 : : * to be referenced again before it is swapped out.
5699 : : *
5700 : : * The inactive_anon ratio is the target ratio of ACTIVE_ANON to
5701 : : * INACTIVE_ANON pages on this zone's LRU, maintained by the
5702 : : * pageout code. A zone->inactive_ratio of 3 means 3:1 or 25% of
5703 : : * the anonymous pages are kept on the inactive list.
5704 : : *
5705 : : * total target max
5706 : : * memory ratio inactive anon
5707 : : * -------------------------------------
5708 : : * 10MB 1 5MB
5709 : : * 100MB 1 50MB
5710 : : * 1GB 3 250MB
5711 : : * 10GB 10 0.9GB
5712 : : * 100GB 31 3GB
5713 : : * 1TB 101 10GB
5714 : : * 10TB 320 32GB
5715 : : */
5716 : 0 : static void __meminit calculate_zone_inactive_ratio(struct zone *zone)
5717 : : {
5718 : : unsigned int gb, ratio;
5719 : :
5720 : : /* Zone size in gigabytes */
5721 : 0 : gb = zone->managed_pages >> (30 - PAGE_SHIFT);
5722 [ # # ]: 0 : if (gb)
5723 : 0 : ratio = int_sqrt(10 * gb);
5724 : : else
5725 : : ratio = 1;
5726 : :
5727 : 0 : zone->inactive_ratio = ratio;
5728 : 0 : }
5729 : :
5730 : 0 : static void __meminit setup_per_zone_inactive_ratio(void)
5731 : : {
5732 : 0 : struct zone *zone;
5733 : :
5734 [ # # ]: 0 : for_each_zone(zone)
5735 : 0 : calculate_zone_inactive_ratio(zone);
5736 : 0 : }
5737 : :
5738 : : /*
5739 : : * Initialise min_free_kbytes.
5740 : : *
5741 : : * For small machines we want it small (128k min). For large machines
5742 : : * we want it large (64MB max). But it is not linear, because network
5743 : : * bandwidth does not increase linearly with machine size. We use
5744 : : *
5745 : : * min_free_kbytes = 4 * sqrt(lowmem_kbytes), for better accuracy:
5746 : : * min_free_kbytes = sqrt(lowmem_kbytes * 16)
5747 : : *
5748 : : * which yields
5749 : : *
5750 : : * 16MB: 512k
5751 : : * 32MB: 724k
5752 : : * 64MB: 1024k
5753 : : * 128MB: 1448k
5754 : : * 256MB: 2048k
5755 : : * 512MB: 2896k
5756 : : * 1024MB: 4096k
5757 : : * 2048MB: 5792k
5758 : : * 4096MB: 8192k
5759 : : * 8192MB: 11584k
5760 : : * 16384MB: 16384k
5761 : : */
5762 : 0 : int __meminit init_per_zone_wmark_min(void)
5763 : : {
5764 : : unsigned long lowmem_kbytes;
5765 : : int new_min_free_kbytes;
5766 : :
5767 : 0 : lowmem_kbytes = nr_free_buffer_pages() * (PAGE_SIZE >> 10);
5768 : 0 : new_min_free_kbytes = int_sqrt(lowmem_kbytes * 16);
5769 : :
5770 [ # # ]: 0 : if (new_min_free_kbytes > user_min_free_kbytes) {
5771 : 0 : min_free_kbytes = new_min_free_kbytes;
5772 [ # # ]: 0 : if (min_free_kbytes < 128)
5773 : 0 : min_free_kbytes = 128;
5774 [ # # ]: 0 : if (min_free_kbytes > 65536)
5775 : 0 : min_free_kbytes = 65536;
5776 : : } else {
5777 : 0 : pr_warn("min_free_kbytes is not updated to %d because user defined value %d is preferred\n",
5778 : : new_min_free_kbytes, user_min_free_kbytes);
5779 : : }
5780 : 0 : setup_per_zone_wmarks();
5781 : 0 : refresh_zone_stat_thresholds();
5782 : 0 : setup_per_zone_lowmem_reserve();
5783 : 0 : setup_per_zone_inactive_ratio();
5784 : 0 : return 0;
5785 : : }
5786 : : module_init(init_per_zone_wmark_min)
5787 : :
5788 : : /*
5789 : : * min_free_kbytes_sysctl_handler - just a wrapper around proc_dointvec() so
5790 : : * that we can call two helper functions whenever min_free_kbytes
5791 : : * or extra_free_kbytes changes.
5792 : : */
5793 : 0 : int min_free_kbytes_sysctl_handler(ctl_table *table, int write,
5794 : : void __user *buffer, size_t *length, loff_t *ppos)
5795 : : {
5796 : : int rc;
5797 : :
5798 : 24 : rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
5799 [ + - ]: 24 : if (rc)
5800 : : return rc;
5801 : :
5802 [ + + ]: 24 : if (write) {
5803 : 10 : user_min_free_kbytes = min_free_kbytes;
5804 : 10 : setup_per_zone_wmarks();
5805 : : }
5806 : : return 0;
5807 : : }
5808 : :
5809 : : #ifdef CONFIG_NUMA
5810 : : int sysctl_min_unmapped_ratio_sysctl_handler(ctl_table *table, int write,
5811 : : void __user *buffer, size_t *length, loff_t *ppos)
5812 : : {
5813 : : struct zone *zone;
5814 : : int rc;
5815 : :
5816 : : rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
5817 : : if (rc)
5818 : : return rc;
5819 : :
5820 : : for_each_zone(zone)
5821 : : zone->min_unmapped_pages = (zone->managed_pages *
5822 : : sysctl_min_unmapped_ratio) / 100;
5823 : : return 0;
5824 : : }
5825 : :
5826 : : int sysctl_min_slab_ratio_sysctl_handler(ctl_table *table, int write,
5827 : : void __user *buffer, size_t *length, loff_t *ppos)
5828 : : {
5829 : : struct zone *zone;
5830 : : int rc;
5831 : :
5832 : : rc = proc_dointvec_minmax(table, write, buffer, length, ppos);
5833 : : if (rc)
5834 : : return rc;
5835 : :
5836 : : for_each_zone(zone)
5837 : : zone->min_slab_pages = (zone->managed_pages *
5838 : : sysctl_min_slab_ratio) / 100;
5839 : : return 0;
5840 : : }
5841 : : #endif
5842 : :
5843 : : /*
5844 : : * lowmem_reserve_ratio_sysctl_handler - just a wrapper around
5845 : : * proc_dointvec() so that we can call setup_per_zone_lowmem_reserve()
5846 : : * whenever sysctl_lowmem_reserve_ratio changes.
5847 : : *
5848 : : * The reserve ratio obviously has absolutely no relation with the
5849 : : * minimum watermarks. The lowmem reserve ratio can only make sense
5850 : : * if in function of the boot time zone sizes.
5851 : : */
5852 : 0 : int lowmem_reserve_ratio_sysctl_handler(ctl_table *table, int write,
5853 : : void __user *buffer, size_t *length, loff_t *ppos)
5854 : : {
5855 : 2 : proc_dointvec_minmax(table, write, buffer, length, ppos);
5856 : 2 : setup_per_zone_lowmem_reserve();
5857 : 2 : return 0;
5858 : : }
5859 : :
5860 : : /*
5861 : : * percpu_pagelist_fraction - changes the pcp->high for each zone on each
5862 : : * cpu. It is the fraction of total pages in each zone that a hot per cpu
5863 : : * pagelist can have before it gets flushed back to buddy allocator.
5864 : : */
5865 : 0 : int percpu_pagelist_fraction_sysctl_handler(ctl_table *table, int write,
5866 : : void __user *buffer, size_t *length, loff_t *ppos)
5867 : : {
5868 : 0 : struct zone *zone;
5869 : : unsigned int cpu;
5870 : : int ret;
5871 : :
5872 : 2 : ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
5873 [ - + ]: 2 : if (!write || (ret < 0))
5874 : : return ret;
5875 : :
5876 : 0 : mutex_lock(&pcp_batch_high_lock);
5877 [ # # ][ # # ]: 0 : for_each_populated_zone(zone) {
5878 : : unsigned long high;
5879 : 0 : high = zone->managed_pages / percpu_pagelist_fraction;
5880 [ # # ]: 0 : for_each_possible_cpu(cpu)
5881 : 0 : pageset_set_high(per_cpu_ptr(zone->pageset, cpu),
5882 : : high);
5883 : : }
5884 : 0 : mutex_unlock(&pcp_batch_high_lock);
5885 : 0 : return 0;
5886 : : }
5887 : :
5888 : : int hashdist = HASHDIST_DEFAULT;
5889 : :
5890 : : #ifdef CONFIG_NUMA
5891 : : static int __init set_hashdist(char *str)
5892 : : {
5893 : : if (!str)
5894 : : return 0;
5895 : : hashdist = simple_strtoul(str, &str, 0);
5896 : : return 1;
5897 : : }
5898 : : __setup("hashdist=", set_hashdist);
5899 : : #endif
5900 : :
5901 : : /*
5902 : : * allocate a large system hash table from bootmem
5903 : : * - it is assumed that the hash table must contain an exact power-of-2
5904 : : * quantity of entries
5905 : : * - limit is the number of hash buckets, not the total allocation size
5906 : : */
5907 : 0 : void *__init alloc_large_system_hash(const char *tablename,
5908 : : unsigned long bucketsize,
5909 : : unsigned long numentries,
5910 : : int scale,
5911 : : int flags,
5912 : : unsigned int *_hash_shift,
5913 : : unsigned int *_hash_mask,
5914 : : unsigned long low_limit,
5915 : : unsigned long high_limit)
5916 : : {
5917 : 0 : unsigned long long max = high_limit;
5918 : : unsigned long log2qty, size;
5919 : : void *table = NULL;
5920 : :
5921 : : /* allow the kernel cmdline to have a say */
5922 [ # # ]: 0 : if (!numentries) {
5923 : : /* round applicable memory size up to nearest megabyte */
5924 : 0 : numentries = nr_kernel_pages;
5925 : :
5926 : : /* It isn't necessary when PAGE_SIZE >= 1MB */
5927 : : if (PAGE_SHIFT < 20)
5928 : 0 : numentries = round_up(numentries, (1<<20)/PAGE_SIZE);
5929 : :
5930 : : /* limit to 1 bucket per 2^scale bytes of low memory */
5931 [ # # ]: 0 : if (scale > PAGE_SHIFT)
5932 : 0 : numentries >>= (scale - PAGE_SHIFT);
5933 : : else
5934 : 0 : numentries <<= (PAGE_SHIFT - scale);
5935 : :
5936 : : /* Make sure we've got at least a 0-order allocation.. */
5937 [ # # ]: 0 : if (unlikely(flags & HASH_SMALL)) {
5938 : : /* Makes no sense without HASH_EARLY */
5939 [ # # ]: 0 : WARN_ON(!(flags & HASH_EARLY));
5940 [ # # ]: 0 : if (!(numentries >> *_hash_shift)) {
5941 : 0 : numentries = 1UL << *_hash_shift;
5942 [ # # ]: 0 : BUG_ON(!numentries);
5943 : : }
5944 [ # # ]: 0 : } else if (unlikely((numentries * bucketsize) < PAGE_SIZE))
5945 : 0 : numentries = PAGE_SIZE / bucketsize;
5946 : : }
5947 [ # # ][ # # ]: 0 : numentries = roundup_pow_of_two(numentries);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
5948 : :
5949 : : /* limit allocation size to 1/16 total memory by default */
5950 [ # # ]: 0 : if (max == 0) {
5951 : 0 : max = ((unsigned long long)nr_all_pages << PAGE_SHIFT) >> 4;
5952 [ # # ][ # # ]: 0 : do_div(max, bucketsize);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
5953 : : }
5954 : 0 : max = min(max, 0x80000000ULL);
5955 : :
5956 [ # # ]: 0 : if (numentries < low_limit)
5957 : : numentries = low_limit;
5958 [ # # ]: 0 : if (numentries > max)
5959 : 0 : numentries = max;
5960 : :
5961 [ # # ][ # # ]: 0 : log2qty = ilog2(numentries);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
5962 : :
5963 : : do {
5964 : 0 : size = bucketsize << log2qty;
5965 [ # # ]: 0 : if (flags & HASH_EARLY)
5966 : : table = memblock_virt_alloc_nopanic(size, 0);
5967 [ # # ]: 0 : else if (hashdist)
5968 : 0 : table = __vmalloc(size, GFP_ATOMIC, PAGE_KERNEL);
5969 : : else {
5970 : : /*
5971 : : * If bucketsize is not a power-of-two, we may free
5972 : : * some pages at the end of hash table which
5973 : : * alloc_pages_exact() automatically does
5974 : : */
5975 [ # # ][ # # ]: 0 : if (get_order(size) < MAX_ORDER) {
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
5976 : 0 : table = alloc_pages_exact(size, GFP_ATOMIC);
5977 : : kmemleak_alloc(table, size, 1, GFP_ATOMIC);
5978 : : }
5979 : : }
5980 [ # # ][ # # ]: 0 : } while (!table && size > PAGE_SIZE && --log2qty);
5981 : :
5982 [ # # ]: 0 : if (!table)
5983 : 0 : panic("Failed to allocate %s hash table\n", tablename);
5984 : :
5985 [ # # ]: 0 : printk(KERN_INFO "%s hash table entries: %ld (order: %d, %lu bytes)\n",
5986 : : tablename,
5987 : : (1UL << log2qty),
5988 [ # # ][ # # ]: 0 : ilog2(size) - PAGE_SHIFT,
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
5989 : : size);
5990 : :
5991 [ # # ]: 0 : if (_hash_shift)
5992 : 0 : *_hash_shift = log2qty;
5993 [ # # ]: 0 : if (_hash_mask)
5994 : 0 : *_hash_mask = (1 << log2qty) - 1;
5995 : :
5996 : 0 : return table;
5997 : : }
5998 : :
5999 : : /* Return a pointer to the bitmap storing bits affecting a block of pages */
6000 : : static inline unsigned long *get_pageblock_bitmap(struct zone *zone,
6001 : : unsigned long pfn)
6002 : : {
6003 : : #ifdef CONFIG_SPARSEMEM
6004 : : return __pfn_to_section(pfn)->pageblock_flags;
6005 : : #else
6006 : : return zone->pageblock_flags;
6007 : : #endif /* CONFIG_SPARSEMEM */
6008 : : }
6009 : :
6010 : : static inline int pfn_to_bitidx(struct zone *zone, unsigned long pfn)
6011 : : {
6012 : : #ifdef CONFIG_SPARSEMEM
6013 : : pfn &= (PAGES_PER_SECTION-1);
6014 : : return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
6015 : : #else
6016 : 31653133 : pfn = pfn - round_down(zone->zone_start_pfn, pageblock_nr_pages);
6017 : 31653133 : return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
6018 : : #endif /* CONFIG_SPARSEMEM */
6019 : : }
6020 : :
6021 : : /**
6022 : : * get_pageblock_flags_group - Return the requested group of flags for the pageblock_nr_pages block of pages
6023 : : * @page: The page within the block of interest
6024 : : * @start_bitidx: The first bit of interest to retrieve
6025 : : * @end_bitidx: The last bit of interest
6026 : : * returns pageblock_bits flags
6027 : : */
6028 : 0 : unsigned long get_pageblock_flags_group(struct page *page,
6029 : : int start_bitidx, int end_bitidx)
6030 : : {
6031 : 31653085 : struct zone *zone;
6032 : : unsigned long *bitmap;
6033 : : unsigned long pfn, bitidx;
6034 : : unsigned long flags = 0;
6035 : : unsigned long value = 1;
6036 : :
6037 : 31653085 : zone = page_zone(page);
6038 : 31653085 : pfn = page_to_pfn(page);
6039 : : bitmap = get_pageblock_bitmap(zone, pfn);
6040 : : bitidx = pfn_to_bitidx(zone, pfn);
6041 : :
6042 [ + + ]: 126601075 : for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
6043 [ + + ]: 94947990 : if (test_bit(bitidx + start_bitidx, bitmap))
6044 : 20223983 : flags |= value;
6045 : :
6046 : 31653085 : return flags;
6047 : : }
6048 : :
6049 : : /**
6050 : : * set_pageblock_flags_group - Set the requested group of flags for a pageblock_nr_pages block of pages
6051 : : * @page: The page within the block of interest
6052 : : * @start_bitidx: The first bit of interest
6053 : : * @end_bitidx: The last bit of interest
6054 : : * @flags: The flags to set
6055 : : */
6056 : 0 : void set_pageblock_flags_group(struct page *page, unsigned long flags,
6057 : : int start_bitidx, int end_bitidx)
6058 : : {
6059 : 48 : struct zone *zone;
6060 : : unsigned long *bitmap;
6061 : : unsigned long pfn, bitidx;
6062 : : unsigned long value = 1;
6063 : :
6064 : 48 : zone = page_zone(page);
6065 : 48 : pfn = page_to_pfn(page);
6066 : : bitmap = get_pageblock_bitmap(zone, pfn);
6067 : : bitidx = pfn_to_bitidx(zone, pfn);
6068 : : VM_BUG_ON_PAGE(!zone_spans_pfn(zone, pfn), page);
6069 : :
6070 [ + + ]: 192 : for (; start_bitidx <= end_bitidx; start_bitidx++, value <<= 1)
6071 [ + + ]: 144 : if (flags & value)
6072 : 22 : __set_bit(bitidx + start_bitidx, bitmap);
6073 : : else
6074 : 122 : __clear_bit(bitidx + start_bitidx, bitmap);
6075 : 48 : }
6076 : :
6077 : : /*
6078 : : * This function checks whether pageblock includes unmovable pages or not.
6079 : : * If @count is not zero, it is okay to include less @count unmovable pages
6080 : : *
6081 : : * PageLRU check without isolation or lru_lock could race so that
6082 : : * MIGRATE_MOVABLE block might include unmovable pages. It means you can't
6083 : : * expect this function should be exact.
6084 : : */
6085 : 0 : bool has_unmovable_pages(struct zone *zone, struct page *page, int count,
6086 : : bool skip_hwpoisoned_pages)
6087 : : {
6088 : : unsigned long pfn, iter, found;
6089 : : int mt;
6090 : :
6091 : : /*
6092 : : * For avoiding noise data, lru_add_drain_all() should be called
6093 : : * If ZONE_MOVABLE, the zone never contains unmovable pages
6094 : : */
6095 [ # # ]: 0 : if (zone_idx(zone) == ZONE_MOVABLE)
6096 : : return false;
6097 : : mt = get_pageblock_migratetype(page);
6098 [ # # ]: 0 : if (mt == MIGRATE_MOVABLE || is_migrate_cma(mt))
6099 : : return false;
6100 : :
6101 : 0 : pfn = page_to_pfn(page);
6102 [ # # ]: 0 : for (found = 0, iter = 0; iter < pageblock_nr_pages; iter++) {
6103 : 0 : unsigned long check = pfn + iter;
6104 : :
6105 : : if (!pfn_valid_within(check))
6106 : : continue;
6107 : :
6108 : 0 : page = pfn_to_page(check);
6109 : :
6110 : : /*
6111 : : * Hugepages are not in LRU lists, but they're movable.
6112 : : * We need not scan over tail pages bacause we don't
6113 : : * handle each tail page individually in migration.
6114 : : */
6115 : : if (PageHuge(page)) {
6116 : : iter = round_up(iter + 1, 1<<compound_order(page)) - 1;
6117 : : continue;
6118 : : }
6119 : :
6120 : : /*
6121 : : * We can't use page_count without pin a page
6122 : : * because another CPU can free compound page.
6123 : : * This check already skips compound tails of THP
6124 : : * because their page->_count is zero at all time.
6125 : : */
6126 [ # # ]: 0 : if (!atomic_read(&page->_count)) {
6127 [ # # ]: 0 : if (PageBuddy(page))
6128 : 0 : iter += (1 << page_order(page)) - 1;
6129 : 0 : continue;
6130 : : }
6131 : :
6132 : : /*
6133 : : * The HWPoisoned page may be not in buddy system, and
6134 : : * page_count() is not 0.
6135 : : */
6136 : : if (skip_hwpoisoned_pages && PageHWPoison(page))
6137 : : continue;
6138 : :
6139 [ # # ]: 0 : if (!PageLRU(page))
6140 : 0 : found++;
6141 : : /*
6142 : : * If there are RECLAIMABLE pages, we need to check it.
6143 : : * But now, memory offline itself doesn't call shrink_slab()
6144 : : * and it still to be fixed.
6145 : : */
6146 : : /*
6147 : : * If the page is not RAM, page_count()should be 0.
6148 : : * we don't need more check. This is an _used_ not-movable page.
6149 : : *
6150 : : * The problematic thing here is PG_reserved pages. PG_reserved
6151 : : * is set to both of a memory hole page and a _used_ kernel
6152 : : * page at boot.
6153 : : */
6154 [ # # ]: 0 : if (found > count)
6155 : : return true;
6156 : : }
6157 : : return false;
6158 : : }
6159 : :
6160 : 0 : bool is_pageblock_removable_nolock(struct page *page)
6161 : : {
6162 : : struct zone *zone;
6163 : : unsigned long pfn;
6164 : :
6165 : : /*
6166 : : * We have to be careful here because we are iterating over memory
6167 : : * sections which are not zone aware so we might end up outside of
6168 : : * the zone but still within the section.
6169 : : * We have to take care about the node as well. If the node is offline
6170 : : * its NODE_DATA will be NULL - see page_zone.
6171 : : */
6172 : : if (!node_online(page_to_nid(page)))
6173 : : return false;
6174 : :
6175 : 0 : zone = page_zone(page);
6176 : 0 : pfn = page_to_pfn(page);
6177 [ # # ]: 0 : if (!zone_spans_pfn(zone, pfn))
6178 : : return false;
6179 : :
6180 : 0 : return !has_unmovable_pages(zone, page, 0, true);
6181 : : }
6182 : :
6183 : : #ifdef CONFIG_CMA
6184 : :
6185 : : static unsigned long pfn_max_align_down(unsigned long pfn)
6186 : : {
6187 : : return pfn & ~(max_t(unsigned long, MAX_ORDER_NR_PAGES,
6188 : : pageblock_nr_pages) - 1);
6189 : : }
6190 : :
6191 : : static unsigned long pfn_max_align_up(unsigned long pfn)
6192 : : {
6193 : : return ALIGN(pfn, max_t(unsigned long, MAX_ORDER_NR_PAGES,
6194 : : pageblock_nr_pages));
6195 : : }
6196 : :
6197 : : /* [start, end) must belong to a single zone. */
6198 : : static int __alloc_contig_migrate_range(struct compact_control *cc,
6199 : : unsigned long start, unsigned long end)
6200 : : {
6201 : : /* This function is based on compact_zone() from compaction.c. */
6202 : : unsigned long nr_reclaimed;
6203 : : unsigned long pfn = start;
6204 : : unsigned int tries = 0;
6205 : : int ret = 0;
6206 : :
6207 : : migrate_prep();
6208 : :
6209 : : while (pfn < end || !list_empty(&cc->migratepages)) {
6210 : : if (fatal_signal_pending(current)) {
6211 : : ret = -EINTR;
6212 : : break;
6213 : : }
6214 : :
6215 : : if (list_empty(&cc->migratepages)) {
6216 : : cc->nr_migratepages = 0;
6217 : : pfn = isolate_migratepages_range(cc->zone, cc,
6218 : : pfn, end, true);
6219 : : if (!pfn) {
6220 : : ret = -EINTR;
6221 : : break;
6222 : : }
6223 : : tries = 0;
6224 : : } else if (++tries == 5) {
6225 : : ret = ret < 0 ? ret : -EBUSY;
6226 : : break;
6227 : : }
6228 : :
6229 : : nr_reclaimed = reclaim_clean_pages_from_list(cc->zone,
6230 : : &cc->migratepages);
6231 : : cc->nr_migratepages -= nr_reclaimed;
6232 : :
6233 : : ret = migrate_pages(&cc->migratepages, alloc_migrate_target,
6234 : : 0, MIGRATE_SYNC, MR_CMA);
6235 : : }
6236 : : if (ret < 0) {
6237 : : putback_movable_pages(&cc->migratepages);
6238 : : return ret;
6239 : : }
6240 : : return 0;
6241 : : }
6242 : :
6243 : : /**
6244 : : * alloc_contig_range() -- tries to allocate given range of pages
6245 : : * @start: start PFN to allocate
6246 : : * @end: one-past-the-last PFN to allocate
6247 : : * @migratetype: migratetype of the underlaying pageblocks (either
6248 : : * #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks
6249 : : * in range must have the same migratetype and it must
6250 : : * be either of the two.
6251 : : *
6252 : : * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES
6253 : : * aligned, however it's the caller's responsibility to guarantee that
6254 : : * we are the only thread that changes migrate type of pageblocks the
6255 : : * pages fall in.
6256 : : *
6257 : : * The PFN range must belong to a single zone.
6258 : : *
6259 : : * Returns zero on success or negative error code. On success all
6260 : : * pages which PFN is in [start, end) are allocated for the caller and
6261 : : * need to be freed with free_contig_range().
6262 : : */
6263 : : int alloc_contig_range(unsigned long start, unsigned long end,
6264 : : unsigned migratetype)
6265 : : {
6266 : : unsigned long outer_start, outer_end;
6267 : : int ret = 0, order;
6268 : :
6269 : : struct compact_control cc = {
6270 : : .nr_migratepages = 0,
6271 : : .order = -1,
6272 : : .zone = page_zone(pfn_to_page(start)),
6273 : : .sync = true,
6274 : : .ignore_skip_hint = true,
6275 : : };
6276 : : INIT_LIST_HEAD(&cc.migratepages);
6277 : :
6278 : : /*
6279 : : * What we do here is we mark all pageblocks in range as
6280 : : * MIGRATE_ISOLATE. Because pageblock and max order pages may
6281 : : * have different sizes, and due to the way page allocator
6282 : : * work, we align the range to biggest of the two pages so
6283 : : * that page allocator won't try to merge buddies from
6284 : : * different pageblocks and change MIGRATE_ISOLATE to some
6285 : : * other migration type.
6286 : : *
6287 : : * Once the pageblocks are marked as MIGRATE_ISOLATE, we
6288 : : * migrate the pages from an unaligned range (ie. pages that
6289 : : * we are interested in). This will put all the pages in
6290 : : * range back to page allocator as MIGRATE_ISOLATE.
6291 : : *
6292 : : * When this is done, we take the pages in range from page
6293 : : * allocator removing them from the buddy system. This way
6294 : : * page allocator will never consider using them.
6295 : : *
6296 : : * This lets us mark the pageblocks back as
6297 : : * MIGRATE_CMA/MIGRATE_MOVABLE so that free pages in the
6298 : : * aligned range but not in the unaligned, original range are
6299 : : * put back to page allocator so that buddy can use them.
6300 : : */
6301 : :
6302 : : ret = start_isolate_page_range(pfn_max_align_down(start),
6303 : : pfn_max_align_up(end), migratetype,
6304 : : false);
6305 : : if (ret)
6306 : : return ret;
6307 : :
6308 : : ret = __alloc_contig_migrate_range(&cc, start, end);
6309 : : if (ret)
6310 : : goto done;
6311 : :
6312 : : /*
6313 : : * Pages from [start, end) are within a MAX_ORDER_NR_PAGES
6314 : : * aligned blocks that are marked as MIGRATE_ISOLATE. What's
6315 : : * more, all pages in [start, end) are free in page allocator.
6316 : : * What we are going to do is to allocate all pages from
6317 : : * [start, end) (that is remove them from page allocator).
6318 : : *
6319 : : * The only problem is that pages at the beginning and at the
6320 : : * end of interesting range may be not aligned with pages that
6321 : : * page allocator holds, ie. they can be part of higher order
6322 : : * pages. Because of this, we reserve the bigger range and
6323 : : * once this is done free the pages we are not interested in.
6324 : : *
6325 : : * We don't have to hold zone->lock here because the pages are
6326 : : * isolated thus they won't get removed from buddy.
6327 : : */
6328 : :
6329 : : lru_add_drain_all();
6330 : : drain_all_pages();
6331 : :
6332 : : order = 0;
6333 : : outer_start = start;
6334 : : while (!PageBuddy(pfn_to_page(outer_start))) {
6335 : : if (++order >= MAX_ORDER) {
6336 : : ret = -EBUSY;
6337 : : goto done;
6338 : : }
6339 : : outer_start &= ~0UL << order;
6340 : : }
6341 : :
6342 : : /* Make sure the range is really isolated. */
6343 : : if (test_pages_isolated(outer_start, end, false)) {
6344 : : pr_warn("alloc_contig_range test_pages_isolated(%lx, %lx) failed\n",
6345 : : outer_start, end);
6346 : : ret = -EBUSY;
6347 : : goto done;
6348 : : }
6349 : :
6350 : :
6351 : : /* Grab isolated pages from freelists. */
6352 : : outer_end = isolate_freepages_range(&cc, outer_start, end);
6353 : : if (!outer_end) {
6354 : : ret = -EBUSY;
6355 : : goto done;
6356 : : }
6357 : :
6358 : : /* Free head and tail (if any) */
6359 : : if (start != outer_start)
6360 : : free_contig_range(outer_start, start - outer_start);
6361 : : if (end != outer_end)
6362 : : free_contig_range(end, outer_end - end);
6363 : :
6364 : : done:
6365 : : undo_isolate_page_range(pfn_max_align_down(start),
6366 : : pfn_max_align_up(end), migratetype);
6367 : : return ret;
6368 : : }
6369 : :
6370 : : void free_contig_range(unsigned long pfn, unsigned nr_pages)
6371 : : {
6372 : : unsigned int count = 0;
6373 : :
6374 : : for (; nr_pages--; pfn++) {
6375 : : struct page *page = pfn_to_page(pfn);
6376 : :
6377 : : count += page_count(page) != 1;
6378 : : __free_page(page);
6379 : : }
6380 : : WARN(count != 0, "%d pages are still in use!\n", count);
6381 : : }
6382 : : #endif
6383 : :
6384 : : #ifdef CONFIG_MEMORY_HOTPLUG
6385 : : /*
6386 : : * The zone indicated has a new number of managed_pages; batch sizes and percpu
6387 : : * page high values need to be recalulated.
6388 : : */
6389 : : void __meminit zone_pcp_update(struct zone *zone)
6390 : : {
6391 : : unsigned cpu;
6392 : : mutex_lock(&pcp_batch_high_lock);
6393 : : for_each_possible_cpu(cpu)
6394 : : pageset_set_high_and_batch(zone,
6395 : : per_cpu_ptr(zone->pageset, cpu));
6396 : : mutex_unlock(&pcp_batch_high_lock);
6397 : : }
6398 : : #endif
6399 : :
6400 : 0 : void zone_pcp_reset(struct zone *zone)
6401 : : {
6402 : : unsigned long flags;
6403 : : int cpu;
6404 : : struct per_cpu_pageset *pset;
6405 : :
6406 : : /* avoid races with drain_pages() */
6407 : : local_irq_save(flags);
6408 [ # # ]: 0 : if (zone->pageset != &boot_pageset) {
6409 [ # # ]: 0 : for_each_online_cpu(cpu) {
6410 : 0 : pset = per_cpu_ptr(zone->pageset, cpu);
6411 : 0 : drain_zonestat(zone, pset);
6412 : : }
6413 : 0 : free_percpu(zone->pageset);
6414 : 0 : zone->pageset = &boot_pageset;
6415 : : }
6416 [ # # ]: 0 : local_irq_restore(flags);
6417 : 0 : }
6418 : :
6419 : : #ifdef CONFIG_MEMORY_HOTREMOVE
6420 : : /*
6421 : : * All pages in the range must be isolated before calling this.
6422 : : */
6423 : : void
6424 : : __offline_isolated_pages(unsigned long start_pfn, unsigned long end_pfn)
6425 : : {
6426 : : struct page *page;
6427 : : struct zone *zone;
6428 : : int order, i;
6429 : : unsigned long pfn;
6430 : : unsigned long flags;
6431 : : /* find the first valid pfn */
6432 : : for (pfn = start_pfn; pfn < end_pfn; pfn++)
6433 : : if (pfn_valid(pfn))
6434 : : break;
6435 : : if (pfn == end_pfn)
6436 : : return;
6437 : : zone = page_zone(pfn_to_page(pfn));
6438 : : spin_lock_irqsave(&zone->lock, flags);
6439 : : pfn = start_pfn;
6440 : : while (pfn < end_pfn) {
6441 : : if (!pfn_valid(pfn)) {
6442 : : pfn++;
6443 : : continue;
6444 : : }
6445 : : page = pfn_to_page(pfn);
6446 : : /*
6447 : : * The HWPoisoned page may be not in buddy system, and
6448 : : * page_count() is not 0.
6449 : : */
6450 : : if (unlikely(!PageBuddy(page) && PageHWPoison(page))) {
6451 : : pfn++;
6452 : : SetPageReserved(page);
6453 : : continue;
6454 : : }
6455 : :
6456 : : BUG_ON(page_count(page));
6457 : : BUG_ON(!PageBuddy(page));
6458 : : order = page_order(page);
6459 : : #ifdef CONFIG_DEBUG_VM
6460 : : printk(KERN_INFO "remove from free list %lx %d %lx\n",
6461 : : pfn, 1 << order, end_pfn);
6462 : : #endif
6463 : : list_del(&page->lru);
6464 : : rmv_page_order(page);
6465 : : zone->free_area[order].nr_free--;
6466 : : for (i = 0; i < (1 << order); i++)
6467 : : SetPageReserved((page+i));
6468 : : pfn += (1 << order);
6469 : : }
6470 : : spin_unlock_irqrestore(&zone->lock, flags);
6471 : : }
6472 : : #endif
6473 : :
6474 : : #ifdef CONFIG_MEMORY_FAILURE
6475 : : bool is_free_buddy_page(struct page *page)
6476 : : {
6477 : : struct zone *zone = page_zone(page);
6478 : : unsigned long pfn = page_to_pfn(page);
6479 : : unsigned long flags;
6480 : : int order;
6481 : :
6482 : : spin_lock_irqsave(&zone->lock, flags);
6483 : : for (order = 0; order < MAX_ORDER; order++) {
6484 : : struct page *page_head = page - (pfn & ((1 << order) - 1));
6485 : :
6486 : : if (PageBuddy(page_head) && page_order(page_head) >= order)
6487 : : break;
6488 : : }
6489 : : spin_unlock_irqrestore(&zone->lock, flags);
6490 : :
6491 : : return order < MAX_ORDER;
6492 : : }
6493 : : #endif
6494 : :
6495 : : static const struct trace_print_flags pageflag_names[] = {
6496 : : {1UL << PG_locked, "locked" },
6497 : : {1UL << PG_error, "error" },
6498 : : {1UL << PG_referenced, "referenced" },
6499 : : {1UL << PG_uptodate, "uptodate" },
6500 : : {1UL << PG_dirty, "dirty" },
6501 : : {1UL << PG_lru, "lru" },
6502 : : {1UL << PG_active, "active" },
6503 : : {1UL << PG_slab, "slab" },
6504 : : {1UL << PG_owner_priv_1, "owner_priv_1" },
6505 : : {1UL << PG_arch_1, "arch_1" },
6506 : : {1UL << PG_reserved, "reserved" },
6507 : : {1UL << PG_private, "private" },
6508 : : {1UL << PG_private_2, "private_2" },
6509 : : {1UL << PG_writeback, "writeback" },
6510 : : #ifdef CONFIG_PAGEFLAGS_EXTENDED
6511 : : {1UL << PG_head, "head" },
6512 : : {1UL << PG_tail, "tail" },
6513 : : #else
6514 : : {1UL << PG_compound, "compound" },
6515 : : #endif
6516 : : {1UL << PG_swapcache, "swapcache" },
6517 : : {1UL << PG_mappedtodisk, "mappedtodisk" },
6518 : : {1UL << PG_reclaim, "reclaim" },
6519 : : {1UL << PG_swapbacked, "swapbacked" },
6520 : : {1UL << PG_unevictable, "unevictable" },
6521 : : #ifdef CONFIG_MMU
6522 : : {1UL << PG_mlocked, "mlocked" },
6523 : : #endif
6524 : : #ifdef CONFIG_ARCH_USES_PG_UNCACHED
6525 : : {1UL << PG_uncached, "uncached" },
6526 : : #endif
6527 : : #ifdef CONFIG_MEMORY_FAILURE
6528 : : {1UL << PG_hwpoison, "hwpoison" },
6529 : : #endif
6530 : : #ifdef CONFIG_TRANSPARENT_HUGEPAGE
6531 : : {1UL << PG_compound_lock, "compound_lock" },
6532 : : #endif
6533 : : };
6534 : :
6535 : 0 : static void dump_page_flags(unsigned long flags)
6536 : : {
6537 : : const char *delim = "";
6538 : : unsigned long mask;
6539 : : int i;
6540 : :
6541 : : BUILD_BUG_ON(ARRAY_SIZE(pageflag_names) != __NR_PAGEFLAGS);
6542 : :
6543 : 0 : printk(KERN_ALERT "page flags: %#lx(", flags);
6544 : :
6545 : : /* remove zone id */
6546 : 0 : flags &= (1UL << NR_PAGEFLAGS) - 1;
6547 : :
6548 [ # # ]: 0 : for (i = 0; i < ARRAY_SIZE(pageflag_names) && flags; i++) {
6549 : :
6550 : 0 : mask = pageflag_names[i].mask;
6551 [ # # ]: 0 : if ((flags & mask) != mask)
6552 : 0 : continue;
6553 : :
6554 : 0 : flags &= ~mask;
6555 : 0 : printk("%s%s", delim, pageflag_names[i].name);
6556 : : delim = "|";
6557 : : }
6558 : :
6559 : : /* check for left over flags */
6560 [ # # ]: 0 : if (flags)
6561 : 0 : printk("%s%#lx", delim, flags);
6562 : :
6563 : 0 : printk(")\n");
6564 : 0 : }
6565 : :
6566 : 0 : void dump_page_badflags(struct page *page, char *reason, unsigned long badflags)
6567 : : {
6568 : 0 : printk(KERN_ALERT
6569 : : "page:%p count:%d mapcount:%d mapping:%p index:%#lx\n",
6570 : : page, atomic_read(&page->_count), page_mapcount(page),
6571 : : page->mapping, page->index);
6572 : 0 : dump_page_flags(page->flags);
6573 [ # # ]: 0 : if (reason)
6574 : 0 : pr_alert("page dumped because: %s\n", reason);
6575 [ # # ]: 0 : if (page->flags & badflags) {
6576 : 0 : pr_alert("bad because of flags:\n");
6577 : 0 : dump_page_flags(page->flags & badflags);
6578 : : }
6579 : : mem_cgroup_print_bad_page(page);
6580 : 0 : }
6581 : :
6582 : 0 : void dump_page(struct page *page, char *reason)
6583 : : {
6584 : 0 : dump_page_badflags(page, reason, 0);
6585 : 0 : }
6586 : : EXPORT_SYMBOL_GPL(dump_page);
|