2 * linux/mm/page_alloc.c
4 * Manages the free list, the system allocates free pages here.
5 * Note that kmalloc() lives in slab.c
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)
17 #include <linux/stddef.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/kasan.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/memremap.h>
46 #include <linux/stop_machine.h>
47 #include <linux/sort.h>
48 #include <linux/pfn.h>
49 #include <linux/backing-dev.h>
50 #include <linux/fault-inject.h>
51 #include <linux/page-isolation.h>
52 #include <linux/page_ext.h>
53 #include <linux/debugobjects.h>
54 #include <linux/kmemleak.h>
55 #include <linux/compaction.h>
56 #include <trace/events/kmem.h>
57 #include <trace/events/oom.h>
58 #include <linux/prefetch.h>
59 #include <linux/mm_inline.h>
60 #include <linux/migrate.h>
61 #include <linux/hugetlb.h>
62 #include <linux/sched/rt.h>
63 #include <linux/sched/mm.h>
64 #include <linux/page_owner.h>
65 #include <linux/kthread.h>
66 #include <linux/memcontrol.h>
67 #include <linux/ftrace.h>
68 #include <linux/lockdep.h>
69 #include <linux/nmi.h>
71 #include <asm/sections.h>
72 #include <asm/tlbflush.h>
73 #include <asm/div64.h>
76 /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */
77 static DEFINE_MUTEX(pcp_batch_high_lock);
78 #define MIN_PERCPU_PAGELIST_FRACTION (8)
80 #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID
81 DEFINE_PER_CPU(int, numa_node);
82 EXPORT_PER_CPU_SYMBOL(numa_node);
85 DEFINE_STATIC_KEY_TRUE(vm_numa_stat_key);
87 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
89 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
90 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
91 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
92 * defined in <linux/topology.h>.
94 DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
95 EXPORT_PER_CPU_SYMBOL(_numa_mem_);
96 int _node_numa_mem_[MAX_NUMNODES];
99 /* work_structs for global per-cpu drains */
100 DEFINE_MUTEX(pcpu_drain_mutex);
101 DEFINE_PER_CPU(struct work_struct, pcpu_drain);
103 #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
104 volatile unsigned long latent_entropy __latent_entropy;
105 EXPORT_SYMBOL(latent_entropy);
109 * Array of node states.
111 nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
112 [N_POSSIBLE] = NODE_MASK_ALL,
113 [N_ONLINE] = { { [0] = 1UL } },
115 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
116 #ifdef CONFIG_HIGHMEM
117 [N_HIGH_MEMORY] = { { [0] = 1UL } },
119 [N_MEMORY] = { { [0] = 1UL } },
120 [N_CPU] = { { [0] = 1UL } },
123 EXPORT_SYMBOL(node_states);
125 /* Protect totalram_pages and zone->managed_pages */
126 static DEFINE_SPINLOCK(managed_page_count_lock);
128 unsigned long totalram_pages __read_mostly;
129 unsigned long totalreserve_pages __read_mostly;
130 unsigned long totalcma_pages __read_mostly;
132 int percpu_pagelist_fraction;
133 gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
136 * A cached value of the page's pageblock's migratetype, used when the page is
137 * put on a pcplist. Used to avoid the pageblock migratetype lookup when
138 * freeing from pcplists in most cases, at the cost of possibly becoming stale.
139 * Also the migratetype set in the page does not necessarily match the pcplist
140 * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
141 * other index - this ensures that it will be put on the correct CMA freelist.
143 static inline int get_pcppage_migratetype(struct page *page)
148 static inline void set_pcppage_migratetype(struct page *page, int migratetype)
150 page->index = migratetype;
153 #ifdef CONFIG_PM_SLEEP
155 * The following functions are used by the suspend/hibernate code to temporarily
156 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
157 * while devices are suspended. To avoid races with the suspend/hibernate code,
158 * they should always be called with pm_mutex held (gfp_allowed_mask also should
159 * only be modified with pm_mutex held, unless the suspend/hibernate code is
160 * guaranteed not to run in parallel with that modification).
163 static gfp_t saved_gfp_mask;
165 void pm_restore_gfp_mask(void)
167 WARN_ON(!mutex_is_locked(&pm_mutex));
168 if (saved_gfp_mask) {
169 gfp_allowed_mask = saved_gfp_mask;
174 void pm_restrict_gfp_mask(void)
176 WARN_ON(!mutex_is_locked(&pm_mutex));
177 WARN_ON(saved_gfp_mask);
178 saved_gfp_mask = gfp_allowed_mask;
179 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
182 bool pm_suspended_storage(void)
184 if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
188 #endif /* CONFIG_PM_SLEEP */
190 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
191 unsigned int pageblock_order __read_mostly;
194 static void __free_pages_ok(struct page *page, unsigned int order);
197 * results with 256, 32 in the lowmem_reserve sysctl:
198 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
199 * 1G machine -> (16M dma, 784M normal, 224M high)
200 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
201 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
202 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
204 * TBD: should special case ZONE_DMA32 machines here - in those we normally
205 * don't need any ZONE_NORMAL reservation
207 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
208 #ifdef CONFIG_ZONE_DMA
211 #ifdef CONFIG_ZONE_DMA32
214 #ifdef CONFIG_HIGHMEM
220 EXPORT_SYMBOL(totalram_pages);
222 static char * const zone_names[MAX_NR_ZONES] = {
223 #ifdef CONFIG_ZONE_DMA
226 #ifdef CONFIG_ZONE_DMA32
230 #ifdef CONFIG_HIGHMEM
234 #ifdef CONFIG_ZONE_DEVICE
239 char * const migratetype_names[MIGRATE_TYPES] = {
247 #ifdef CONFIG_MEMORY_ISOLATION
252 compound_page_dtor * const compound_page_dtors[] = {
255 #ifdef CONFIG_HUGETLB_PAGE
258 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
263 int min_free_kbytes = 1024;
264 int user_min_free_kbytes = -1;
265 int watermark_scale_factor = 10;
267 static unsigned long __meminitdata nr_kernel_pages;
268 static unsigned long __meminitdata nr_all_pages;
269 static unsigned long __meminitdata dma_reserve;
271 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
272 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
273 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
274 static unsigned long __initdata required_kernelcore;
275 static unsigned long __initdata required_movablecore;
276 static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
277 static bool mirrored_kernelcore;
279 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
281 EXPORT_SYMBOL(movable_zone);
282 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
285 int nr_node_ids __read_mostly = MAX_NUMNODES;
286 int nr_online_nodes __read_mostly = 1;
287 EXPORT_SYMBOL(nr_node_ids);
288 EXPORT_SYMBOL(nr_online_nodes);
291 int page_group_by_mobility_disabled __read_mostly;
293 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
294 static inline void reset_deferred_meminit(pg_data_t *pgdat)
296 unsigned long max_initialise;
297 unsigned long reserved_lowmem;
300 * Initialise at least 2G of a node but also take into account that
301 * two large system hashes that can take up 1GB for 0.25TB/node.
303 max_initialise = max(2UL << (30 - PAGE_SHIFT),
304 (pgdat->node_spanned_pages >> 8));
307 * Compensate the all the memblock reservations (e.g. crash kernel)
308 * from the initial estimation to make sure we will initialize enough
311 reserved_lowmem = memblock_reserved_memory_within(pgdat->node_start_pfn,
312 pgdat->node_start_pfn + max_initialise);
313 max_initialise += reserved_lowmem;
315 pgdat->static_init_size = min(max_initialise, pgdat->node_spanned_pages);
316 pgdat->first_deferred_pfn = ULONG_MAX;
319 /* Returns true if the struct page for the pfn is uninitialised */
320 static inline bool __meminit early_page_uninitialised(unsigned long pfn)
322 int nid = early_pfn_to_nid(pfn);
324 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
331 * Returns false when the remaining initialisation should be deferred until
332 * later in the boot cycle when it can be parallelised.
334 static inline bool update_defer_init(pg_data_t *pgdat,
335 unsigned long pfn, unsigned long zone_end,
336 unsigned long *nr_initialised)
338 /* Always populate low zones for address-contrained allocations */
339 if (zone_end < pgdat_end_pfn(pgdat))
342 if ((*nr_initialised > pgdat->static_init_size) &&
343 (pfn & (PAGES_PER_SECTION - 1)) == 0) {
344 pgdat->first_deferred_pfn = pfn;
351 static inline void reset_deferred_meminit(pg_data_t *pgdat)
355 static inline bool early_page_uninitialised(unsigned long pfn)
360 static inline bool update_defer_init(pg_data_t *pgdat,
361 unsigned long pfn, unsigned long zone_end,
362 unsigned long *nr_initialised)
368 /* Return a pointer to the bitmap storing bits affecting a block of pages */
369 static inline unsigned long *get_pageblock_bitmap(struct page *page,
372 #ifdef CONFIG_SPARSEMEM
373 return __pfn_to_section(pfn)->pageblock_flags;
375 return page_zone(page)->pageblock_flags;
376 #endif /* CONFIG_SPARSEMEM */
379 static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
381 #ifdef CONFIG_SPARSEMEM
382 pfn &= (PAGES_PER_SECTION-1);
383 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
385 pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
386 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
387 #endif /* CONFIG_SPARSEMEM */
391 * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
392 * @page: The page within the block of interest
393 * @pfn: The target page frame number
394 * @end_bitidx: The last bit of interest to retrieve
395 * @mask: mask of bits that the caller is interested in
397 * Return: pageblock_bits flags
399 static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page,
401 unsigned long end_bitidx,
404 unsigned long *bitmap;
405 unsigned long bitidx, word_bitidx;
408 bitmap = get_pageblock_bitmap(page, pfn);
409 bitidx = pfn_to_bitidx(page, pfn);
410 word_bitidx = bitidx / BITS_PER_LONG;
411 bitidx &= (BITS_PER_LONG-1);
413 word = bitmap[word_bitidx];
414 bitidx += end_bitidx;
415 return (word >> (BITS_PER_LONG - bitidx - 1)) & mask;
418 unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
419 unsigned long end_bitidx,
422 return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask);
425 static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
427 return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK);
431 * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
432 * @page: The page within the block of interest
433 * @flags: The flags to set
434 * @pfn: The target page frame number
435 * @end_bitidx: The last bit of interest
436 * @mask: mask of bits that the caller is interested in
438 void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
440 unsigned long end_bitidx,
443 unsigned long *bitmap;
444 unsigned long bitidx, word_bitidx;
445 unsigned long old_word, word;
447 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
449 bitmap = get_pageblock_bitmap(page, pfn);
450 bitidx = pfn_to_bitidx(page, pfn);
451 word_bitidx = bitidx / BITS_PER_LONG;
452 bitidx &= (BITS_PER_LONG-1);
454 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
456 bitidx += end_bitidx;
457 mask <<= (BITS_PER_LONG - bitidx - 1);
458 flags <<= (BITS_PER_LONG - bitidx - 1);
460 word = READ_ONCE(bitmap[word_bitidx]);
462 old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
463 if (word == old_word)
469 void set_pageblock_migratetype(struct page *page, int migratetype)
471 if (unlikely(page_group_by_mobility_disabled &&
472 migratetype < MIGRATE_PCPTYPES))
473 migratetype = MIGRATE_UNMOVABLE;
475 set_pageblock_flags_group(page, (unsigned long)migratetype,
476 PB_migrate, PB_migrate_end);
479 #ifdef CONFIG_DEBUG_VM
480 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
484 unsigned long pfn = page_to_pfn(page);
485 unsigned long sp, start_pfn;
488 seq = zone_span_seqbegin(zone);
489 start_pfn = zone->zone_start_pfn;
490 sp = zone->spanned_pages;
491 if (!zone_spans_pfn(zone, pfn))
493 } while (zone_span_seqretry(zone, seq));
496 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
497 pfn, zone_to_nid(zone), zone->name,
498 start_pfn, start_pfn + sp);
503 static int page_is_consistent(struct zone *zone, struct page *page)
505 if (!pfn_valid_within(page_to_pfn(page)))
507 if (zone != page_zone(page))
513 * Temporary debugging check for pages not lying within a given zone.
515 static int __maybe_unused bad_range(struct zone *zone, struct page *page)
517 if (page_outside_zone_boundaries(zone, page))
519 if (!page_is_consistent(zone, page))
525 static inline int __maybe_unused bad_range(struct zone *zone, struct page *page)
531 static void bad_page(struct page *page, const char *reason,
532 unsigned long bad_flags)
534 static unsigned long resume;
535 static unsigned long nr_shown;
536 static unsigned long nr_unshown;
539 * Allow a burst of 60 reports, then keep quiet for that minute;
540 * or allow a steady drip of one report per second.
542 if (nr_shown == 60) {
543 if (time_before(jiffies, resume)) {
549 "BUG: Bad page state: %lu messages suppressed\n",
556 resume = jiffies + 60 * HZ;
558 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
559 current->comm, page_to_pfn(page));
560 __dump_page(page, reason);
561 bad_flags &= page->flags;
563 pr_alert("bad because of flags: %#lx(%pGp)\n",
564 bad_flags, &bad_flags);
565 dump_page_owner(page);
570 /* Leave bad fields for debug, except PageBuddy could make trouble */
571 page_mapcount_reset(page); /* remove PageBuddy */
572 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
576 * Higher-order pages are called "compound pages". They are structured thusly:
578 * The first PAGE_SIZE page is called the "head page" and have PG_head set.
580 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
581 * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
583 * The first tail page's ->compound_dtor holds the offset in array of compound
584 * page destructors. See compound_page_dtors.
586 * The first tail page's ->compound_order holds the order of allocation.
587 * This usage means that zero-order pages may not be compound.
590 void free_compound_page(struct page *page)
592 __free_pages_ok(page, compound_order(page));
595 void prep_compound_page(struct page *page, unsigned int order)
598 int nr_pages = 1 << order;
600 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
601 set_compound_order(page, order);
603 for (i = 1; i < nr_pages; i++) {
604 struct page *p = page + i;
605 set_page_count(p, 0);
606 p->mapping = TAIL_MAPPING;
607 set_compound_head(p, page);
609 atomic_set(compound_mapcount_ptr(page), -1);
612 #ifdef CONFIG_DEBUG_PAGEALLOC
613 unsigned int _debug_guardpage_minorder;
614 bool _debug_pagealloc_enabled __read_mostly
615 = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
616 EXPORT_SYMBOL(_debug_pagealloc_enabled);
617 bool _debug_guardpage_enabled __read_mostly;
619 static int __init early_debug_pagealloc(char *buf)
623 return kstrtobool(buf, &_debug_pagealloc_enabled);
625 early_param("debug_pagealloc", early_debug_pagealloc);
627 static bool need_debug_guardpage(void)
629 /* If we don't use debug_pagealloc, we don't need guard page */
630 if (!debug_pagealloc_enabled())
633 if (!debug_guardpage_minorder())
639 static void init_debug_guardpage(void)
641 if (!debug_pagealloc_enabled())
644 if (!debug_guardpage_minorder())
647 _debug_guardpage_enabled = true;
650 struct page_ext_operations debug_guardpage_ops = {
651 .need = need_debug_guardpage,
652 .init = init_debug_guardpage,
655 static int __init debug_guardpage_minorder_setup(char *buf)
659 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
660 pr_err("Bad debug_guardpage_minorder value\n");
663 _debug_guardpage_minorder = res;
664 pr_info("Setting debug_guardpage_minorder to %lu\n", res);
667 early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
669 static inline bool set_page_guard(struct zone *zone, struct page *page,
670 unsigned int order, int migratetype)
672 struct page_ext *page_ext;
674 if (!debug_guardpage_enabled())
677 if (order >= debug_guardpage_minorder())
680 page_ext = lookup_page_ext(page);
681 if (unlikely(!page_ext))
684 __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
686 INIT_LIST_HEAD(&page->lru);
687 set_page_private(page, order);
688 /* Guard pages are not available for any usage */
689 __mod_zone_freepage_state(zone, -(1 << order), migratetype);
694 static inline void clear_page_guard(struct zone *zone, struct page *page,
695 unsigned int order, int migratetype)
697 struct page_ext *page_ext;
699 if (!debug_guardpage_enabled())
702 page_ext = lookup_page_ext(page);
703 if (unlikely(!page_ext))
706 __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
708 set_page_private(page, 0);
709 if (!is_migrate_isolate(migratetype))
710 __mod_zone_freepage_state(zone, (1 << order), migratetype);
713 struct page_ext_operations debug_guardpage_ops;
714 static inline bool set_page_guard(struct zone *zone, struct page *page,
715 unsigned int order, int migratetype) { return false; }
716 static inline void clear_page_guard(struct zone *zone, struct page *page,
717 unsigned int order, int migratetype) {}
720 static inline void set_page_order(struct page *page, unsigned int order)
722 set_page_private(page, order);
723 __SetPageBuddy(page);
726 static inline void rmv_page_order(struct page *page)
728 __ClearPageBuddy(page);
729 set_page_private(page, 0);
733 * This function checks whether a page is free && is the buddy
734 * we can do coalesce a page and its buddy if
735 * (a) the buddy is not in a hole (check before calling!) &&
736 * (b) the buddy is in the buddy system &&
737 * (c) a page and its buddy have the same order &&
738 * (d) a page and its buddy are in the same zone.
740 * For recording whether a page is in the buddy system, we set ->_mapcount
741 * PAGE_BUDDY_MAPCOUNT_VALUE.
742 * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is
743 * serialized by zone->lock.
745 * For recording page's order, we use page_private(page).
747 static inline int page_is_buddy(struct page *page, struct page *buddy,
750 if (page_is_guard(buddy) && page_order(buddy) == order) {
751 if (page_zone_id(page) != page_zone_id(buddy))
754 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
759 if (PageBuddy(buddy) && page_order(buddy) == order) {
761 * zone check is done late to avoid uselessly
762 * calculating zone/node ids for pages that could
765 if (page_zone_id(page) != page_zone_id(buddy))
768 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
776 * Freeing function for a buddy system allocator.
778 * The concept of a buddy system is to maintain direct-mapped table
779 * (containing bit values) for memory blocks of various "orders".
780 * The bottom level table contains the map for the smallest allocatable
781 * units of memory (here, pages), and each level above it describes
782 * pairs of units from the levels below, hence, "buddies".
783 * At a high level, all that happens here is marking the table entry
784 * at the bottom level available, and propagating the changes upward
785 * as necessary, plus some accounting needed to play nicely with other
786 * parts of the VM system.
787 * At each level, we keep a list of pages, which are heads of continuous
788 * free pages of length of (1 << order) and marked with _mapcount
789 * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
791 * So when we are allocating or freeing one, we can derive the state of the
792 * other. That is, if we allocate a small block, and both were
793 * free, the remainder of the region must be split into blocks.
794 * If a block is freed, and its buddy is also free, then this
795 * triggers coalescing into a block of larger size.
800 static inline void __free_one_page(struct page *page,
802 struct zone *zone, unsigned int order,
805 unsigned long combined_pfn;
806 unsigned long uninitialized_var(buddy_pfn);
808 unsigned int max_order;
810 max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
812 VM_BUG_ON(!zone_is_initialized(zone));
813 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
815 VM_BUG_ON(migratetype == -1);
816 if (likely(!is_migrate_isolate(migratetype)))
817 __mod_zone_freepage_state(zone, 1 << order, migratetype);
819 VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
820 VM_BUG_ON_PAGE(bad_range(zone, page), page);
823 while (order < max_order - 1) {
824 buddy_pfn = __find_buddy_pfn(pfn, order);
825 buddy = page + (buddy_pfn - pfn);
827 if (!pfn_valid_within(buddy_pfn))
829 if (!page_is_buddy(page, buddy, order))
832 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
833 * merge with it and move up one order.
835 if (page_is_guard(buddy)) {
836 clear_page_guard(zone, buddy, order, migratetype);
838 list_del(&buddy->lru);
839 zone->free_area[order].nr_free--;
840 rmv_page_order(buddy);
842 combined_pfn = buddy_pfn & pfn;
843 page = page + (combined_pfn - pfn);
847 if (max_order < MAX_ORDER) {
848 /* If we are here, it means order is >= pageblock_order.
849 * We want to prevent merge between freepages on isolate
850 * pageblock and normal pageblock. Without this, pageblock
851 * isolation could cause incorrect freepage or CMA accounting.
853 * We don't want to hit this code for the more frequent
856 if (unlikely(has_isolate_pageblock(zone))) {
859 buddy_pfn = __find_buddy_pfn(pfn, order);
860 buddy = page + (buddy_pfn - pfn);
861 buddy_mt = get_pageblock_migratetype(buddy);
863 if (migratetype != buddy_mt
864 && (is_migrate_isolate(migratetype) ||
865 is_migrate_isolate(buddy_mt)))
869 goto continue_merging;
873 set_page_order(page, order);
876 * If this is not the largest possible page, check if the buddy
877 * of the next-highest order is free. If it is, it's possible
878 * that pages are being freed that will coalesce soon. In case,
879 * that is happening, add the free page to the tail of the list
880 * so it's less likely to be used soon and more likely to be merged
881 * as a higher order page
883 if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) {
884 struct page *higher_page, *higher_buddy;
885 combined_pfn = buddy_pfn & pfn;
886 higher_page = page + (combined_pfn - pfn);
887 buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
888 higher_buddy = higher_page + (buddy_pfn - combined_pfn);
889 if (pfn_valid_within(buddy_pfn) &&
890 page_is_buddy(higher_page, higher_buddy, order + 1)) {
891 list_add_tail(&page->lru,
892 &zone->free_area[order].free_list[migratetype]);
897 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
899 zone->free_area[order].nr_free++;
903 * A bad page could be due to a number of fields. Instead of multiple branches,
904 * try and check multiple fields with one check. The caller must do a detailed
905 * check if necessary.
907 static inline bool page_expected_state(struct page *page,
908 unsigned long check_flags)
910 if (unlikely(atomic_read(&page->_mapcount) != -1))
913 if (unlikely((unsigned long)page->mapping |
914 page_ref_count(page) |
916 (unsigned long)page->mem_cgroup |
918 (page->flags & check_flags)))
924 static void free_pages_check_bad(struct page *page)
926 const char *bad_reason;
927 unsigned long bad_flags;
932 if (unlikely(atomic_read(&page->_mapcount) != -1))
933 bad_reason = "nonzero mapcount";
934 if (unlikely(page->mapping != NULL))
935 bad_reason = "non-NULL mapping";
936 if (unlikely(page_ref_count(page) != 0))
937 bad_reason = "nonzero _refcount";
938 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
939 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
940 bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
943 if (unlikely(page->mem_cgroup))
944 bad_reason = "page still charged to cgroup";
946 bad_page(page, bad_reason, bad_flags);
949 static inline int free_pages_check(struct page *page)
951 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
954 /* Something has gone sideways, find it */
955 free_pages_check_bad(page);
959 static int free_tail_pages_check(struct page *head_page, struct page *page)
964 * We rely page->lru.next never has bit 0 set, unless the page
965 * is PageTail(). Let's make sure that's true even for poisoned ->lru.
967 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
969 if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
973 switch (page - head_page) {
975 /* the first tail page: ->mapping is compound_mapcount() */
976 if (unlikely(compound_mapcount(page))) {
977 bad_page(page, "nonzero compound_mapcount", 0);
983 * the second tail page: ->mapping is
984 * page_deferred_list().next -- ignore value.
988 if (page->mapping != TAIL_MAPPING) {
989 bad_page(page, "corrupted mapping in tail page", 0);
994 if (unlikely(!PageTail(page))) {
995 bad_page(page, "PageTail not set", 0);
998 if (unlikely(compound_head(page) != head_page)) {
999 bad_page(page, "compound_head not consistent", 0);
1004 page->mapping = NULL;
1005 clear_compound_head(page);
1009 static __always_inline bool free_pages_prepare(struct page *page,
1010 unsigned int order, bool check_free)
1014 VM_BUG_ON_PAGE(PageTail(page), page);
1016 trace_mm_page_free(page, order);
1019 * Check tail pages before head page information is cleared to
1020 * avoid checking PageCompound for order-0 pages.
1022 if (unlikely(order)) {
1023 bool compound = PageCompound(page);
1026 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1029 ClearPageDoubleMap(page);
1030 for (i = 1; i < (1 << order); i++) {
1032 bad += free_tail_pages_check(page, page + i);
1033 if (unlikely(free_pages_check(page + i))) {
1037 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1040 if (PageMappingFlags(page))
1041 page->mapping = NULL;
1042 if (memcg_kmem_enabled() && PageKmemcg(page))
1043 memcg_kmem_uncharge(page, order);
1045 bad += free_pages_check(page);
1049 page_cpupid_reset_last(page);
1050 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1051 reset_page_owner(page, order);
1053 if (!PageHighMem(page)) {
1054 debug_check_no_locks_freed(page_address(page),
1055 PAGE_SIZE << order);
1056 debug_check_no_obj_freed(page_address(page),
1057 PAGE_SIZE << order);
1059 arch_free_page(page, order);
1060 kernel_poison_pages(page, 1 << order, 0);
1061 kernel_map_pages(page, 1 << order, 0);
1062 kasan_free_pages(page, order);
1067 #ifdef CONFIG_DEBUG_VM
1068 static inline bool free_pcp_prepare(struct page *page)
1070 return free_pages_prepare(page, 0, true);
1073 static inline bool bulkfree_pcp_prepare(struct page *page)
1078 static bool free_pcp_prepare(struct page *page)
1080 return free_pages_prepare(page, 0, false);
1083 static bool bulkfree_pcp_prepare(struct page *page)
1085 return free_pages_check(page);
1087 #endif /* CONFIG_DEBUG_VM */
1090 * Frees a number of pages from the PCP lists
1091 * Assumes all pages on list are in same zone, and of same order.
1092 * count is the number of pages to free.
1094 * If the zone was previously in an "all pages pinned" state then look to
1095 * see if this freeing clears that state.
1097 * And clear the zone's pages_scanned counter, to hold off the "all pages are
1098 * pinned" detection logic.
1100 static void free_pcppages_bulk(struct zone *zone, int count,
1101 struct per_cpu_pages *pcp)
1103 int migratetype = 0;
1105 bool isolated_pageblocks;
1107 spin_lock(&zone->lock);
1108 isolated_pageblocks = has_isolate_pageblock(zone);
1112 struct list_head *list;
1115 * Remove pages from lists in a round-robin fashion. A
1116 * batch_free count is maintained that is incremented when an
1117 * empty list is encountered. This is so more pages are freed
1118 * off fuller lists instead of spinning excessively around empty
1123 if (++migratetype == MIGRATE_PCPTYPES)
1125 list = &pcp->lists[migratetype];
1126 } while (list_empty(list));
1128 /* This is the only non-empty list. Free them all. */
1129 if (batch_free == MIGRATE_PCPTYPES)
1133 int mt; /* migratetype of the to-be-freed page */
1135 page = list_last_entry(list, struct page, lru);
1136 /* must delete as __free_one_page list manipulates */
1137 list_del(&page->lru);
1139 mt = get_pcppage_migratetype(page);
1140 /* MIGRATE_ISOLATE page should not go to pcplists */
1141 VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
1142 /* Pageblock could have been isolated meanwhile */
1143 if (unlikely(isolated_pageblocks))
1144 mt = get_pageblock_migratetype(page);
1146 if (bulkfree_pcp_prepare(page))
1149 __free_one_page(page, page_to_pfn(page), zone, 0, mt);
1150 trace_mm_page_pcpu_drain(page, 0, mt);
1151 } while (--count && --batch_free && !list_empty(list));
1153 spin_unlock(&zone->lock);
1156 static void free_one_page(struct zone *zone,
1157 struct page *page, unsigned long pfn,
1161 spin_lock(&zone->lock);
1162 if (unlikely(has_isolate_pageblock(zone) ||
1163 is_migrate_isolate(migratetype))) {
1164 migratetype = get_pfnblock_migratetype(page, pfn);
1166 __free_one_page(page, pfn, zone, order, migratetype);
1167 spin_unlock(&zone->lock);
1170 static void __meminit __init_single_page(struct page *page, unsigned long pfn,
1171 unsigned long zone, int nid)
1173 mm_zero_struct_page(page);
1174 set_page_links(page, zone, nid, pfn);
1175 init_page_count(page);
1176 page_mapcount_reset(page);
1177 page_cpupid_reset_last(page);
1179 INIT_LIST_HEAD(&page->lru);
1180 #ifdef WANT_PAGE_VIRTUAL
1181 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1182 if (!is_highmem_idx(zone))
1183 set_page_address(page, __va(pfn << PAGE_SHIFT));
1187 static void __meminit __init_single_pfn(unsigned long pfn, unsigned long zone,
1190 return __init_single_page(pfn_to_page(pfn), pfn, zone, nid);
1193 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1194 static void __meminit init_reserved_page(unsigned long pfn)
1199 if (!early_page_uninitialised(pfn))
1202 nid = early_pfn_to_nid(pfn);
1203 pgdat = NODE_DATA(nid);
1205 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1206 struct zone *zone = &pgdat->node_zones[zid];
1208 if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
1211 __init_single_pfn(pfn, zid, nid);
1214 static inline void init_reserved_page(unsigned long pfn)
1217 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1220 * Initialised pages do not have PageReserved set. This function is
1221 * called for each range allocated by the bootmem allocator and
1222 * marks the pages PageReserved. The remaining valid pages are later
1223 * sent to the buddy page allocator.
1225 void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
1227 unsigned long start_pfn = PFN_DOWN(start);
1228 unsigned long end_pfn = PFN_UP(end);
1230 for (; start_pfn < end_pfn; start_pfn++) {
1231 if (pfn_valid(start_pfn)) {
1232 struct page *page = pfn_to_page(start_pfn);
1234 init_reserved_page(start_pfn);
1236 /* Avoid false-positive PageTail() */
1237 INIT_LIST_HEAD(&page->lru);
1239 SetPageReserved(page);
1244 static void __free_pages_ok(struct page *page, unsigned int order)
1246 unsigned long flags;
1248 unsigned long pfn = page_to_pfn(page);
1250 if (!free_pages_prepare(page, order, true))
1253 migratetype = get_pfnblock_migratetype(page, pfn);
1254 local_irq_save(flags);
1255 __count_vm_events(PGFREE, 1 << order);
1256 free_one_page(page_zone(page), page, pfn, order, migratetype);
1257 local_irq_restore(flags);
1260 static void __init __free_pages_boot_core(struct page *page, unsigned int order)
1262 unsigned int nr_pages = 1 << order;
1263 struct page *p = page;
1267 for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
1269 __ClearPageReserved(p);
1270 set_page_count(p, 0);
1272 __ClearPageReserved(p);
1273 set_page_count(p, 0);
1275 page_zone(page)->managed_pages += nr_pages;
1276 set_page_refcounted(page);
1277 __free_pages(page, order);
1280 #if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \
1281 defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
1283 static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
1285 int __meminit early_pfn_to_nid(unsigned long pfn)
1287 static DEFINE_SPINLOCK(early_pfn_lock);
1290 spin_lock(&early_pfn_lock);
1291 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
1293 nid = first_online_node;
1294 spin_unlock(&early_pfn_lock);
1300 #ifdef CONFIG_NODES_SPAN_OTHER_NODES
1301 static inline bool __meminit __maybe_unused
1302 meminit_pfn_in_nid(unsigned long pfn, int node,
1303 struct mminit_pfnnid_cache *state)
1307 nid = __early_pfn_to_nid(pfn, state);
1308 if (nid >= 0 && nid != node)
1313 /* Only safe to use early in boot when initialisation is single-threaded */
1314 static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1316 return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache);
1321 static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1325 static inline bool __meminit __maybe_unused
1326 meminit_pfn_in_nid(unsigned long pfn, int node,
1327 struct mminit_pfnnid_cache *state)
1334 void __init __free_pages_bootmem(struct page *page, unsigned long pfn,
1337 if (early_page_uninitialised(pfn))
1339 return __free_pages_boot_core(page, order);
1343 * Check that the whole (or subset of) a pageblock given by the interval of
1344 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
1345 * with the migration of free compaction scanner. The scanners then need to
1346 * use only pfn_valid_within() check for arches that allow holes within
1349 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
1351 * It's possible on some configurations to have a setup like node0 node1 node0
1352 * i.e. it's possible that all pages within a zones range of pages do not
1353 * belong to a single zone. We assume that a border between node0 and node1
1354 * can occur within a single pageblock, but not a node0 node1 node0
1355 * interleaving within a single pageblock. It is therefore sufficient to check
1356 * the first and last page of a pageblock and avoid checking each individual
1357 * page in a pageblock.
1359 struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
1360 unsigned long end_pfn, struct zone *zone)
1362 struct page *start_page;
1363 struct page *end_page;
1365 /* end_pfn is one past the range we are checking */
1368 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1371 start_page = pfn_to_online_page(start_pfn);
1375 if (page_zone(start_page) != zone)
1378 end_page = pfn_to_page(end_pfn);
1380 /* This gives a shorter code than deriving page_zone(end_page) */
1381 if (page_zone_id(start_page) != page_zone_id(end_page))
1387 void set_zone_contiguous(struct zone *zone)
1389 unsigned long block_start_pfn = zone->zone_start_pfn;
1390 unsigned long block_end_pfn;
1392 block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
1393 for (; block_start_pfn < zone_end_pfn(zone);
1394 block_start_pfn = block_end_pfn,
1395 block_end_pfn += pageblock_nr_pages) {
1397 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
1399 if (!__pageblock_pfn_to_page(block_start_pfn,
1400 block_end_pfn, zone))
1404 /* We confirm that there is no hole */
1405 zone->contiguous = true;
1408 void clear_zone_contiguous(struct zone *zone)
1410 zone->contiguous = false;
1413 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1414 static void __init deferred_free_range(unsigned long pfn,
1415 unsigned long nr_pages)
1423 page = pfn_to_page(pfn);
1425 /* Free a large naturally-aligned chunk if possible */
1426 if (nr_pages == pageblock_nr_pages &&
1427 (pfn & (pageblock_nr_pages - 1)) == 0) {
1428 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
1429 __free_pages_boot_core(page, pageblock_order);
1433 for (i = 0; i < nr_pages; i++, page++, pfn++) {
1434 if ((pfn & (pageblock_nr_pages - 1)) == 0)
1435 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
1436 __free_pages_boot_core(page, 0);
1440 /* Completion tracking for deferred_init_memmap() threads */
1441 static atomic_t pgdat_init_n_undone __initdata;
1442 static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
1444 static inline void __init pgdat_init_report_one_done(void)
1446 if (atomic_dec_and_test(&pgdat_init_n_undone))
1447 complete(&pgdat_init_all_done_comp);
1451 * Helper for deferred_init_range, free the given range, reset the counters, and
1452 * return number of pages freed.
1454 static inline unsigned long __init __def_free(unsigned long *nr_free,
1455 unsigned long *free_base_pfn,
1458 unsigned long nr = *nr_free;
1460 deferred_free_range(*free_base_pfn, nr);
1468 static unsigned long __init deferred_init_range(int nid, int zid,
1469 unsigned long start_pfn,
1470 unsigned long end_pfn)
1472 struct mminit_pfnnid_cache nid_init_state = { };
1473 unsigned long nr_pgmask = pageblock_nr_pages - 1;
1474 unsigned long free_base_pfn = 0;
1475 unsigned long nr_pages = 0;
1476 unsigned long nr_free = 0;
1477 struct page *page = NULL;
1481 * First we check if pfn is valid on architectures where it is possible
1482 * to have holes within pageblock_nr_pages. On systems where it is not
1483 * possible, this function is optimized out.
1485 * Then, we check if a current large page is valid by only checking the
1486 * validity of the head pfn.
1488 * meminit_pfn_in_nid is checked on systems where pfns can interleave
1489 * within a node: a pfn is between start and end of a node, but does not
1490 * belong to this memory node.
1492 * Finally, we minimize pfn page lookups and scheduler checks by
1493 * performing it only once every pageblock_nr_pages.
1495 * We do it in two loops: first we initialize struct page, than free to
1496 * buddy allocator, becuse while we are freeing pages we can access
1497 * pages that are ahead (computing buddy page in __free_one_page()).
1499 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1500 if (!pfn_valid_within(pfn))
1502 if ((pfn & nr_pgmask) || pfn_valid(pfn)) {
1503 if (meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
1504 if (page && (pfn & nr_pgmask))
1507 page = pfn_to_page(pfn);
1508 __init_single_page(page, pfn, zid, nid);
1515 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1516 if (!pfn_valid_within(pfn)) {
1517 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1518 } else if (!(pfn & nr_pgmask) && !pfn_valid(pfn)) {
1519 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1520 } else if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
1521 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1522 } else if (page && (pfn & nr_pgmask)) {
1526 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1527 page = pfn_to_page(pfn);
1528 free_base_pfn = pfn;
1533 /* Free the last block of pages to allocator */
1534 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1539 /* Initialise remaining memory on a node */
1540 static int __init deferred_init_memmap(void *data)
1542 pg_data_t *pgdat = data;
1543 int nid = pgdat->node_id;
1544 unsigned long start = jiffies;
1545 unsigned long nr_pages = 0;
1546 unsigned long spfn, epfn;
1547 phys_addr_t spa, epa;
1550 unsigned long first_init_pfn = pgdat->first_deferred_pfn;
1551 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
1554 if (first_init_pfn == ULONG_MAX) {
1555 pgdat_init_report_one_done();
1559 /* Bind memory initialisation thread to a local node if possible */
1560 if (!cpumask_empty(cpumask))
1561 set_cpus_allowed_ptr(current, cpumask);
1563 /* Sanity check boundaries */
1564 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
1565 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
1566 pgdat->first_deferred_pfn = ULONG_MAX;
1568 /* Only the highest zone is deferred so find it */
1569 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1570 zone = pgdat->node_zones + zid;
1571 if (first_init_pfn < zone_end_pfn(zone))
1574 first_init_pfn = max(zone->zone_start_pfn, first_init_pfn);
1576 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1577 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1578 epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
1579 nr_pages += deferred_init_range(nid, zid, spfn, epfn);
1582 /* Sanity check that the next zone really is unpopulated */
1583 WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
1585 pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages,
1586 jiffies_to_msecs(jiffies - start));
1588 pgdat_init_report_one_done();
1591 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1593 void __init page_alloc_init_late(void)
1597 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1600 /* There will be num_node_state(N_MEMORY) threads */
1601 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
1602 for_each_node_state(nid, N_MEMORY) {
1603 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
1606 /* Block until all are initialised */
1607 wait_for_completion(&pgdat_init_all_done_comp);
1609 /* Reinit limits that are based on free pages after the kernel is up */
1610 files_maxfiles_init();
1612 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
1613 /* Discard memblock private memory */
1617 for_each_populated_zone(zone)
1618 set_zone_contiguous(zone);
1622 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
1623 void __init init_cma_reserved_pageblock(struct page *page)
1625 unsigned i = pageblock_nr_pages;
1626 struct page *p = page;
1629 __ClearPageReserved(p);
1630 set_page_count(p, 0);
1633 set_pageblock_migratetype(page, MIGRATE_CMA);
1635 if (pageblock_order >= MAX_ORDER) {
1636 i = pageblock_nr_pages;
1639 set_page_refcounted(p);
1640 __free_pages(p, MAX_ORDER - 1);
1641 p += MAX_ORDER_NR_PAGES;
1642 } while (i -= MAX_ORDER_NR_PAGES);
1644 set_page_refcounted(page);
1645 __free_pages(page, pageblock_order);
1648 adjust_managed_page_count(page, pageblock_nr_pages);
1653 * The order of subdivision here is critical for the IO subsystem.
1654 * Please do not alter this order without good reasons and regression
1655 * testing. Specifically, as large blocks of memory are subdivided,
1656 * the order in which smaller blocks are delivered depends on the order
1657 * they're subdivided in this function. This is the primary factor
1658 * influencing the order in which pages are delivered to the IO
1659 * subsystem according to empirical testing, and this is also justified
1660 * by considering the behavior of a buddy system containing a single
1661 * large block of memory acted on by a series of small allocations.
1662 * This behavior is a critical factor in sglist merging's success.
1666 static inline void expand(struct zone *zone, struct page *page,
1667 int low, int high, struct free_area *area,
1670 unsigned long size = 1 << high;
1672 while (high > low) {
1676 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
1679 * Mark as guard pages (or page), that will allow to
1680 * merge back to allocator when buddy will be freed.
1681 * Corresponding page table entries will not be touched,
1682 * pages will stay not present in virtual address space
1684 if (set_page_guard(zone, &page[size], high, migratetype))
1687 list_add(&page[size].lru, &area->free_list[migratetype]);
1689 set_page_order(&page[size], high);
1693 static void check_new_page_bad(struct page *page)
1695 const char *bad_reason = NULL;
1696 unsigned long bad_flags = 0;
1698 if (unlikely(atomic_read(&page->_mapcount) != -1))
1699 bad_reason = "nonzero mapcount";
1700 if (unlikely(page->mapping != NULL))
1701 bad_reason = "non-NULL mapping";
1702 if (unlikely(page_ref_count(page) != 0))
1703 bad_reason = "nonzero _count";
1704 if (unlikely(page->flags & __PG_HWPOISON)) {
1705 bad_reason = "HWPoisoned (hardware-corrupted)";
1706 bad_flags = __PG_HWPOISON;
1707 /* Don't complain about hwpoisoned pages */
1708 page_mapcount_reset(page); /* remove PageBuddy */
1711 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
1712 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
1713 bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
1716 if (unlikely(page->mem_cgroup))
1717 bad_reason = "page still charged to cgroup";
1719 bad_page(page, bad_reason, bad_flags);
1723 * This page is about to be returned from the page allocator
1725 static inline int check_new_page(struct page *page)
1727 if (likely(page_expected_state(page,
1728 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
1731 check_new_page_bad(page);
1735 static inline bool free_pages_prezeroed(void)
1737 return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
1738 page_poisoning_enabled();
1741 #ifdef CONFIG_DEBUG_VM
1742 static bool check_pcp_refill(struct page *page)
1747 static bool check_new_pcp(struct page *page)
1749 return check_new_page(page);
1752 static bool check_pcp_refill(struct page *page)
1754 return check_new_page(page);
1756 static bool check_new_pcp(struct page *page)
1760 #endif /* CONFIG_DEBUG_VM */
1762 static bool check_new_pages(struct page *page, unsigned int order)
1765 for (i = 0; i < (1 << order); i++) {
1766 struct page *p = page + i;
1768 if (unlikely(check_new_page(p)))
1775 inline void post_alloc_hook(struct page *page, unsigned int order,
1778 set_page_private(page, 0);
1779 set_page_refcounted(page);
1781 arch_alloc_page(page, order);
1782 kernel_map_pages(page, 1 << order, 1);
1783 kernel_poison_pages(page, 1 << order, 1);
1784 kasan_alloc_pages(page, order);
1785 set_page_owner(page, order, gfp_flags);
1788 static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
1789 unsigned int alloc_flags)
1793 post_alloc_hook(page, order, gfp_flags);
1795 if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO))
1796 for (i = 0; i < (1 << order); i++)
1797 clear_highpage(page + i);
1799 if (order && (gfp_flags & __GFP_COMP))
1800 prep_compound_page(page, order);
1803 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
1804 * allocate the page. The expectation is that the caller is taking
1805 * steps that will free more memory. The caller should avoid the page
1806 * being used for !PFMEMALLOC purposes.
1808 if (alloc_flags & ALLOC_NO_WATERMARKS)
1809 set_page_pfmemalloc(page);
1811 clear_page_pfmemalloc(page);
1815 * Go through the free lists for the given migratetype and remove
1816 * the smallest available page from the freelists
1818 static __always_inline
1819 struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
1822 unsigned int current_order;
1823 struct free_area *area;
1826 /* Find a page of the appropriate size in the preferred list */
1827 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
1828 area = &(zone->free_area[current_order]);
1829 page = list_first_entry_or_null(&area->free_list[migratetype],
1833 list_del(&page->lru);
1834 rmv_page_order(page);
1836 expand(zone, page, order, current_order, area, migratetype);
1837 set_pcppage_migratetype(page, migratetype);
1846 * This array describes the order lists are fallen back to when
1847 * the free lists for the desirable migrate type are depleted
1849 static int fallbacks[MIGRATE_TYPES][4] = {
1850 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1851 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1852 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
1854 [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
1856 #ifdef CONFIG_MEMORY_ISOLATION
1857 [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
1862 static __always_inline struct page *__rmqueue_cma_fallback(struct zone *zone,
1865 return __rmqueue_smallest(zone, order, MIGRATE_CMA);
1868 static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
1869 unsigned int order) { return NULL; }
1873 * Move the free pages in a range to the free lists of the requested type.
1874 * Note that start_page and end_pages are not aligned on a pageblock
1875 * boundary. If alignment is required, use move_freepages_block()
1877 static int move_freepages(struct zone *zone,
1878 struct page *start_page, struct page *end_page,
1879 int migratetype, int *num_movable)
1883 int pages_moved = 0;
1885 #ifndef CONFIG_HOLES_IN_ZONE
1887 * page_zone is not safe to call in this context when
1888 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
1889 * anyway as we check zone boundaries in move_freepages_block().
1890 * Remove at a later date when no bug reports exist related to
1891 * grouping pages by mobility
1893 VM_BUG_ON(page_zone(start_page) != page_zone(end_page));
1899 for (page = start_page; page <= end_page;) {
1900 if (!pfn_valid_within(page_to_pfn(page))) {
1905 /* Make sure we are not inadvertently changing nodes */
1906 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
1908 if (!PageBuddy(page)) {
1910 * We assume that pages that could be isolated for
1911 * migration are movable. But we don't actually try
1912 * isolating, as that would be expensive.
1915 (PageLRU(page) || __PageMovable(page)))
1922 order = page_order(page);
1923 list_move(&page->lru,
1924 &zone->free_area[order].free_list[migratetype]);
1926 pages_moved += 1 << order;
1932 int move_freepages_block(struct zone *zone, struct page *page,
1933 int migratetype, int *num_movable)
1935 unsigned long start_pfn, end_pfn;
1936 struct page *start_page, *end_page;
1938 start_pfn = page_to_pfn(page);
1939 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
1940 start_page = pfn_to_page(start_pfn);
1941 end_page = start_page + pageblock_nr_pages - 1;
1942 end_pfn = start_pfn + pageblock_nr_pages - 1;
1944 /* Do not cross zone boundaries */
1945 if (!zone_spans_pfn(zone, start_pfn))
1947 if (!zone_spans_pfn(zone, end_pfn))
1950 return move_freepages(zone, start_page, end_page, migratetype,
1954 static void change_pageblock_range(struct page *pageblock_page,
1955 int start_order, int migratetype)
1957 int nr_pageblocks = 1 << (start_order - pageblock_order);
1959 while (nr_pageblocks--) {
1960 set_pageblock_migratetype(pageblock_page, migratetype);
1961 pageblock_page += pageblock_nr_pages;
1966 * When we are falling back to another migratetype during allocation, try to
1967 * steal extra free pages from the same pageblocks to satisfy further
1968 * allocations, instead of polluting multiple pageblocks.
1970 * If we are stealing a relatively large buddy page, it is likely there will
1971 * be more free pages in the pageblock, so try to steal them all. For
1972 * reclaimable and unmovable allocations, we steal regardless of page size,
1973 * as fragmentation caused by those allocations polluting movable pageblocks
1974 * is worse than movable allocations stealing from unmovable and reclaimable
1977 static bool can_steal_fallback(unsigned int order, int start_mt)
1980 * Leaving this order check is intended, although there is
1981 * relaxed order check in next check. The reason is that
1982 * we can actually steal whole pageblock if this condition met,
1983 * but, below check doesn't guarantee it and that is just heuristic
1984 * so could be changed anytime.
1986 if (order >= pageblock_order)
1989 if (order >= pageblock_order / 2 ||
1990 start_mt == MIGRATE_RECLAIMABLE ||
1991 start_mt == MIGRATE_UNMOVABLE ||
1992 page_group_by_mobility_disabled)
1999 * This function implements actual steal behaviour. If order is large enough,
2000 * we can steal whole pageblock. If not, we first move freepages in this
2001 * pageblock to our migratetype and determine how many already-allocated pages
2002 * are there in the pageblock with a compatible migratetype. If at least half
2003 * of pages are free or compatible, we can change migratetype of the pageblock
2004 * itself, so pages freed in the future will be put on the correct free list.
2006 static void steal_suitable_fallback(struct zone *zone, struct page *page,
2007 int start_type, bool whole_block)
2009 unsigned int current_order = page_order(page);
2010 struct free_area *area;
2011 int free_pages, movable_pages, alike_pages;
2014 old_block_type = get_pageblock_migratetype(page);
2017 * This can happen due to races and we want to prevent broken
2018 * highatomic accounting.
2020 if (is_migrate_highatomic(old_block_type))
2023 /* Take ownership for orders >= pageblock_order */
2024 if (current_order >= pageblock_order) {
2025 change_pageblock_range(page, current_order, start_type);
2029 /* We are not allowed to try stealing from the whole block */
2033 free_pages = move_freepages_block(zone, page, start_type,
2036 * Determine how many pages are compatible with our allocation.
2037 * For movable allocation, it's the number of movable pages which
2038 * we just obtained. For other types it's a bit more tricky.
2040 if (start_type == MIGRATE_MOVABLE) {
2041 alike_pages = movable_pages;
2044 * If we are falling back a RECLAIMABLE or UNMOVABLE allocation
2045 * to MOVABLE pageblock, consider all non-movable pages as
2046 * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or
2047 * vice versa, be conservative since we can't distinguish the
2048 * exact migratetype of non-movable pages.
2050 if (old_block_type == MIGRATE_MOVABLE)
2051 alike_pages = pageblock_nr_pages
2052 - (free_pages + movable_pages);
2057 /* moving whole block can fail due to zone boundary conditions */
2062 * If a sufficient number of pages in the block are either free or of
2063 * comparable migratability as our allocation, claim the whole block.
2065 if (free_pages + alike_pages >= (1 << (pageblock_order-1)) ||
2066 page_group_by_mobility_disabled)
2067 set_pageblock_migratetype(page, start_type);
2072 area = &zone->free_area[current_order];
2073 list_move(&page->lru, &area->free_list[start_type]);
2077 * Check whether there is a suitable fallback freepage with requested order.
2078 * If only_stealable is true, this function returns fallback_mt only if
2079 * we can steal other freepages all together. This would help to reduce
2080 * fragmentation due to mixed migratetype pages in one pageblock.
2082 int find_suitable_fallback(struct free_area *area, unsigned int order,
2083 int migratetype, bool only_stealable, bool *can_steal)
2088 if (area->nr_free == 0)
2093 fallback_mt = fallbacks[migratetype][i];
2094 if (fallback_mt == MIGRATE_TYPES)
2097 if (list_empty(&area->free_list[fallback_mt]))
2100 if (can_steal_fallback(order, migratetype))
2103 if (!only_stealable)
2114 * Reserve a pageblock for exclusive use of high-order atomic allocations if
2115 * there are no empty page blocks that contain a page with a suitable order
2117 static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
2118 unsigned int alloc_order)
2121 unsigned long max_managed, flags;
2124 * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
2125 * Check is race-prone but harmless.
2127 max_managed = (zone->managed_pages / 100) + pageblock_nr_pages;
2128 if (zone->nr_reserved_highatomic >= max_managed)
2131 spin_lock_irqsave(&zone->lock, flags);
2133 /* Recheck the nr_reserved_highatomic limit under the lock */
2134 if (zone->nr_reserved_highatomic >= max_managed)
2138 mt = get_pageblock_migratetype(page);
2139 if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt)
2140 && !is_migrate_cma(mt)) {
2141 zone->nr_reserved_highatomic += pageblock_nr_pages;
2142 set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
2143 move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL);
2147 spin_unlock_irqrestore(&zone->lock, flags);
2151 * Used when an allocation is about to fail under memory pressure. This
2152 * potentially hurts the reliability of high-order allocations when under
2153 * intense memory pressure but failed atomic allocations should be easier
2154 * to recover from than an OOM.
2156 * If @force is true, try to unreserve a pageblock even though highatomic
2157 * pageblock is exhausted.
2159 static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
2162 struct zonelist *zonelist = ac->zonelist;
2163 unsigned long flags;
2170 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
2173 * Preserve at least one pageblock unless memory pressure
2176 if (!force && zone->nr_reserved_highatomic <=
2180 spin_lock_irqsave(&zone->lock, flags);
2181 for (order = 0; order < MAX_ORDER; order++) {
2182 struct free_area *area = &(zone->free_area[order]);
2184 page = list_first_entry_or_null(
2185 &area->free_list[MIGRATE_HIGHATOMIC],
2191 * In page freeing path, migratetype change is racy so
2192 * we can counter several free pages in a pageblock
2193 * in this loop althoug we changed the pageblock type
2194 * from highatomic to ac->migratetype. So we should
2195 * adjust the count once.
2197 if (is_migrate_highatomic_page(page)) {
2199 * It should never happen but changes to
2200 * locking could inadvertently allow a per-cpu
2201 * drain to add pages to MIGRATE_HIGHATOMIC
2202 * while unreserving so be safe and watch for
2205 zone->nr_reserved_highatomic -= min(
2207 zone->nr_reserved_highatomic);
2211 * Convert to ac->migratetype and avoid the normal
2212 * pageblock stealing heuristics. Minimally, the caller
2213 * is doing the work and needs the pages. More
2214 * importantly, if the block was always converted to
2215 * MIGRATE_UNMOVABLE or another type then the number
2216 * of pageblocks that cannot be completely freed
2219 set_pageblock_migratetype(page, ac->migratetype);
2220 ret = move_freepages_block(zone, page, ac->migratetype,
2223 spin_unlock_irqrestore(&zone->lock, flags);
2227 spin_unlock_irqrestore(&zone->lock, flags);
2234 * Try finding a free buddy page on the fallback list and put it on the free
2235 * list of requested migratetype, possibly along with other pages from the same
2236 * block, depending on fragmentation avoidance heuristics. Returns true if
2237 * fallback was found so that __rmqueue_smallest() can grab it.
2239 * The use of signed ints for order and current_order is a deliberate
2240 * deviation from the rest of this file, to make the for loop
2241 * condition simpler.
2243 static __always_inline bool
2244 __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
2246 struct free_area *area;
2253 * Find the largest available free page in the other list. This roughly
2254 * approximates finding the pageblock with the most free pages, which
2255 * would be too costly to do exactly.
2257 for (current_order = MAX_ORDER - 1; current_order >= order;
2259 area = &(zone->free_area[current_order]);
2260 fallback_mt = find_suitable_fallback(area, current_order,
2261 start_migratetype, false, &can_steal);
2262 if (fallback_mt == -1)
2266 * We cannot steal all free pages from the pageblock and the
2267 * requested migratetype is movable. In that case it's better to
2268 * steal and split the smallest available page instead of the
2269 * largest available page, because even if the next movable
2270 * allocation falls back into a different pageblock than this
2271 * one, it won't cause permanent fragmentation.
2273 if (!can_steal && start_migratetype == MIGRATE_MOVABLE
2274 && current_order > order)
2283 for (current_order = order; current_order < MAX_ORDER;
2285 area = &(zone->free_area[current_order]);
2286 fallback_mt = find_suitable_fallback(area, current_order,
2287 start_migratetype, false, &can_steal);
2288 if (fallback_mt != -1)
2293 * This should not happen - we already found a suitable fallback
2294 * when looking for the largest page.
2296 VM_BUG_ON(current_order == MAX_ORDER);
2299 page = list_first_entry(&area->free_list[fallback_mt],
2302 steal_suitable_fallback(zone, page, start_migratetype, can_steal);
2304 trace_mm_page_alloc_extfrag(page, order, current_order,
2305 start_migratetype, fallback_mt);
2312 * Do the hard work of removing an element from the buddy allocator.
2313 * Call me with the zone->lock already held.
2315 static __always_inline struct page *
2316 __rmqueue(struct zone *zone, unsigned int order, int migratetype)
2321 page = __rmqueue_smallest(zone, order, migratetype);
2322 if (unlikely(!page)) {
2323 if (migratetype == MIGRATE_MOVABLE)
2324 page = __rmqueue_cma_fallback(zone, order);
2326 if (!page && __rmqueue_fallback(zone, order, migratetype))
2330 trace_mm_page_alloc_zone_locked(page, order, migratetype);
2335 * Obtain a specified number of elements from the buddy allocator, all under
2336 * a single hold of the lock, for efficiency. Add them to the supplied list.
2337 * Returns the number of new pages which were placed at *list.
2339 static int rmqueue_bulk(struct zone *zone, unsigned int order,
2340 unsigned long count, struct list_head *list,
2345 spin_lock(&zone->lock);
2346 for (i = 0; i < count; ++i) {
2347 struct page *page = __rmqueue(zone, order, migratetype);
2348 if (unlikely(page == NULL))
2351 if (unlikely(check_pcp_refill(page)))
2355 * Split buddy pages returned by expand() are received here in
2356 * physical page order. The page is added to the tail of
2357 * caller's list. From the callers perspective, the linked list
2358 * is ordered by page number under some conditions. This is
2359 * useful for IO devices that can forward direction from the
2360 * head, thus also in the physical page order. This is useful
2361 * for IO devices that can merge IO requests if the physical
2362 * pages are ordered properly.
2364 list_add_tail(&page->lru, list);
2366 if (is_migrate_cma(get_pcppage_migratetype(page)))
2367 __mod_zone_page_state(zone, NR_FREE_CMA_PAGES,
2372 * i pages were removed from the buddy list even if some leak due
2373 * to check_pcp_refill failing so adjust NR_FREE_PAGES based
2374 * on i. Do not confuse with 'alloced' which is the number of
2375 * pages added to the pcp list.
2377 __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order));
2378 spin_unlock(&zone->lock);
2384 * Called from the vmstat counter updater to drain pagesets of this
2385 * currently executing processor on remote nodes after they have
2388 * Note that this function must be called with the thread pinned to
2389 * a single processor.
2391 void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp)
2393 unsigned long flags;
2394 int to_drain, batch;
2396 local_irq_save(flags);
2397 batch = READ_ONCE(pcp->batch);
2398 to_drain = min(pcp->count, batch);
2400 free_pcppages_bulk(zone, to_drain, pcp);
2401 pcp->count -= to_drain;
2403 local_irq_restore(flags);
2408 * Drain pcplists of the indicated processor and zone.
2410 * The processor must either be the current processor and the
2411 * thread pinned to the current processor or a processor that
2414 static void drain_pages_zone(unsigned int cpu, struct zone *zone)
2416 unsigned long flags;
2417 struct per_cpu_pageset *pset;
2418 struct per_cpu_pages *pcp;
2420 local_irq_save(flags);
2421 pset = per_cpu_ptr(zone->pageset, cpu);
2425 free_pcppages_bulk(zone, pcp->count, pcp);
2428 local_irq_restore(flags);
2432 * Drain pcplists of all zones on the indicated processor.
2434 * The processor must either be the current processor and the
2435 * thread pinned to the current processor or a processor that
2438 static void drain_pages(unsigned int cpu)
2442 for_each_populated_zone(zone) {
2443 drain_pages_zone(cpu, zone);
2448 * Spill all of this CPU's per-cpu pages back into the buddy allocator.
2450 * The CPU has to be pinned. When zone parameter is non-NULL, spill just
2451 * the single zone's pages.
2453 void drain_local_pages(struct zone *zone)
2455 int cpu = smp_processor_id();
2458 drain_pages_zone(cpu, zone);
2463 static void drain_local_pages_wq(struct work_struct *work)
2466 * drain_all_pages doesn't use proper cpu hotplug protection so
2467 * we can race with cpu offline when the WQ can move this from
2468 * a cpu pinned worker to an unbound one. We can operate on a different
2469 * cpu which is allright but we also have to make sure to not move to
2473 drain_local_pages(NULL);
2478 * Spill all the per-cpu pages from all CPUs back into the buddy allocator.
2480 * When zone parameter is non-NULL, spill just the single zone's pages.
2482 * Note that this can be extremely slow as the draining happens in a workqueue.
2484 void drain_all_pages(struct zone *zone)
2489 * Allocate in the BSS so we wont require allocation in
2490 * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y
2492 static cpumask_t cpus_with_pcps;
2495 * Make sure nobody triggers this path before mm_percpu_wq is fully
2498 if (WARN_ON_ONCE(!mm_percpu_wq))
2501 /* Workqueues cannot recurse */
2502 if (current->flags & PF_WQ_WORKER)
2506 * Do not drain if one is already in progress unless it's specific to
2507 * a zone. Such callers are primarily CMA and memory hotplug and need
2508 * the drain to be complete when the call returns.
2510 if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) {
2513 mutex_lock(&pcpu_drain_mutex);
2517 * We don't care about racing with CPU hotplug event
2518 * as offline notification will cause the notified
2519 * cpu to drain that CPU pcps and on_each_cpu_mask
2520 * disables preemption as part of its processing
2522 for_each_online_cpu(cpu) {
2523 struct per_cpu_pageset *pcp;
2525 bool has_pcps = false;
2528 pcp = per_cpu_ptr(zone->pageset, cpu);
2532 for_each_populated_zone(z) {
2533 pcp = per_cpu_ptr(z->pageset, cpu);
2534 if (pcp->pcp.count) {
2542 cpumask_set_cpu(cpu, &cpus_with_pcps);
2544 cpumask_clear_cpu(cpu, &cpus_with_pcps);
2547 for_each_cpu(cpu, &cpus_with_pcps) {
2548 struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu);
2549 INIT_WORK(work, drain_local_pages_wq);
2550 queue_work_on(cpu, mm_percpu_wq, work);
2552 for_each_cpu(cpu, &cpus_with_pcps)
2553 flush_work(per_cpu_ptr(&pcpu_drain, cpu));
2555 mutex_unlock(&pcpu_drain_mutex);
2558 #ifdef CONFIG_HIBERNATION
2561 * Touch the watchdog for every WD_PAGE_COUNT pages.
2563 #define WD_PAGE_COUNT (128*1024)
2565 void mark_free_pages(struct zone *zone)
2567 unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT;
2568 unsigned long flags;
2569 unsigned int order, t;
2572 if (zone_is_empty(zone))
2575 spin_lock_irqsave(&zone->lock, flags);
2577 max_zone_pfn = zone_end_pfn(zone);
2578 for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++)
2579 if (pfn_valid(pfn)) {
2580 page = pfn_to_page(pfn);
2582 if (!--page_count) {
2583 touch_nmi_watchdog();
2584 page_count = WD_PAGE_COUNT;
2587 if (page_zone(page) != zone)
2590 if (!swsusp_page_is_forbidden(page))
2591 swsusp_unset_page_free(page);
2594 for_each_migratetype_order(order, t) {
2595 list_for_each_entry(page,
2596 &zone->free_area[order].free_list[t], lru) {
2599 pfn = page_to_pfn(page);
2600 for (i = 0; i < (1UL << order); i++) {
2601 if (!--page_count) {
2602 touch_nmi_watchdog();
2603 page_count = WD_PAGE_COUNT;
2605 swsusp_set_page_free(pfn_to_page(pfn + i));
2609 spin_unlock_irqrestore(&zone->lock, flags);
2611 #endif /* CONFIG_PM */
2613 static bool free_unref_page_prepare(struct page *page, unsigned long pfn)
2617 if (!free_pcp_prepare(page))
2620 migratetype = get_pfnblock_migratetype(page, pfn);
2621 set_pcppage_migratetype(page, migratetype);
2625 static void free_unref_page_commit(struct page *page, unsigned long pfn)
2627 struct zone *zone = page_zone(page);
2628 struct per_cpu_pages *pcp;
2631 migratetype = get_pcppage_migratetype(page);
2632 __count_vm_event(PGFREE);
2635 * We only track unmovable, reclaimable and movable on pcp lists.
2636 * Free ISOLATE pages back to the allocator because they are being
2637 * offlined but treat HIGHATOMIC as movable pages so we can get those
2638 * areas back if necessary. Otherwise, we may have to free
2639 * excessively into the page allocator
2641 if (migratetype >= MIGRATE_PCPTYPES) {
2642 if (unlikely(is_migrate_isolate(migratetype))) {
2643 free_one_page(zone, page, pfn, 0, migratetype);
2646 migratetype = MIGRATE_MOVABLE;
2649 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2650 list_add(&page->lru, &pcp->lists[migratetype]);
2652 if (pcp->count >= pcp->high) {
2653 unsigned long batch = READ_ONCE(pcp->batch);
2654 free_pcppages_bulk(zone, batch, pcp);
2655 pcp->count -= batch;
2660 * Free a 0-order page
2662 void free_unref_page(struct page *page)
2664 unsigned long flags;
2665 unsigned long pfn = page_to_pfn(page);
2667 if (!free_unref_page_prepare(page, pfn))
2670 local_irq_save(flags);
2671 free_unref_page_commit(page, pfn);
2672 local_irq_restore(flags);
2676 * Free a list of 0-order pages
2678 void free_unref_page_list(struct list_head *list)
2680 struct page *page, *next;
2681 unsigned long flags, pfn;
2683 /* Prepare pages for freeing */
2684 list_for_each_entry_safe(page, next, list, lru) {
2685 pfn = page_to_pfn(page);
2686 if (!free_unref_page_prepare(page, pfn))
2687 list_del(&page->lru);
2688 set_page_private(page, pfn);
2691 local_irq_save(flags);
2692 list_for_each_entry_safe(page, next, list, lru) {
2693 unsigned long pfn = page_private(page);
2695 set_page_private(page, 0);
2696 trace_mm_page_free_batched(page);
2697 free_unref_page_commit(page, pfn);
2699 local_irq_restore(flags);
2703 * split_page takes a non-compound higher-order page, and splits it into
2704 * n (1<<order) sub-pages: page[0..n]
2705 * Each sub-page must be freed individually.
2707 * Note: this is probably too low level an operation for use in drivers.
2708 * Please consult with lkml before using this in your driver.
2710 void split_page(struct page *page, unsigned int order)
2714 VM_BUG_ON_PAGE(PageCompound(page), page);
2715 VM_BUG_ON_PAGE(!page_count(page), page);
2717 for (i = 1; i < (1 << order); i++)
2718 set_page_refcounted(page + i);
2719 split_page_owner(page, order);
2721 EXPORT_SYMBOL_GPL(split_page);
2723 int __isolate_free_page(struct page *page, unsigned int order)
2725 unsigned long watermark;
2729 BUG_ON(!PageBuddy(page));
2731 zone = page_zone(page);
2732 mt = get_pageblock_migratetype(page);
2734 if (!is_migrate_isolate(mt)) {
2736 * Obey watermarks as if the page was being allocated. We can
2737 * emulate a high-order watermark check with a raised order-0
2738 * watermark, because we already know our high-order page
2741 watermark = min_wmark_pages(zone) + (1UL << order);
2742 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
2745 __mod_zone_freepage_state(zone, -(1UL << order), mt);
2748 /* Remove page from free list */
2749 list_del(&page->lru);
2750 zone->free_area[order].nr_free--;
2751 rmv_page_order(page);
2754 * Set the pageblock if the isolated page is at least half of a
2757 if (order >= pageblock_order - 1) {
2758 struct page *endpage = page + (1 << order) - 1;
2759 for (; page < endpage; page += pageblock_nr_pages) {
2760 int mt = get_pageblock_migratetype(page);
2761 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt)
2762 && !is_migrate_highatomic(mt))
2763 set_pageblock_migratetype(page,
2769 return 1UL << order;
2773 * Update NUMA hit/miss statistics
2775 * Must be called with interrupts disabled.
2777 static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
2780 enum numa_stat_item local_stat = NUMA_LOCAL;
2782 /* skip numa counters update if numa stats is disabled */
2783 if (!static_branch_likely(&vm_numa_stat_key))
2786 if (z->node != numa_node_id())
2787 local_stat = NUMA_OTHER;
2789 if (z->node == preferred_zone->node)
2790 __inc_numa_state(z, NUMA_HIT);
2792 __inc_numa_state(z, NUMA_MISS);
2793 __inc_numa_state(preferred_zone, NUMA_FOREIGN);
2795 __inc_numa_state(z, local_stat);
2799 /* Remove page from the per-cpu list, caller must protect the list */
2800 static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype,
2801 struct per_cpu_pages *pcp,
2802 struct list_head *list)
2807 if (list_empty(list)) {
2808 pcp->count += rmqueue_bulk(zone, 0,
2811 if (unlikely(list_empty(list)))
2815 page = list_first_entry(list, struct page, lru);
2816 list_del(&page->lru);
2818 } while (check_new_pcp(page));
2823 /* Lock and remove page from the per-cpu list */
2824 static struct page *rmqueue_pcplist(struct zone *preferred_zone,
2825 struct zone *zone, unsigned int order,
2826 gfp_t gfp_flags, int migratetype)
2828 struct per_cpu_pages *pcp;
2829 struct list_head *list;
2831 unsigned long flags;
2833 local_irq_save(flags);
2834 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2835 list = &pcp->lists[migratetype];
2836 page = __rmqueue_pcplist(zone, migratetype, pcp, list);
2838 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
2839 zone_statistics(preferred_zone, zone);
2841 local_irq_restore(flags);
2846 * Allocate a page from the given zone. Use pcplists for order-0 allocations.
2849 struct page *rmqueue(struct zone *preferred_zone,
2850 struct zone *zone, unsigned int order,
2851 gfp_t gfp_flags, unsigned int alloc_flags,
2854 unsigned long flags;
2857 if (likely(order == 0)) {
2858 page = rmqueue_pcplist(preferred_zone, zone, order,
2859 gfp_flags, migratetype);
2864 * We most definitely don't want callers attempting to
2865 * allocate greater than order-1 page units with __GFP_NOFAIL.
2867 WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
2868 spin_lock_irqsave(&zone->lock, flags);
2872 if (alloc_flags & ALLOC_HARDER) {
2873 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
2875 trace_mm_page_alloc_zone_locked(page, order, migratetype);
2878 page = __rmqueue(zone, order, migratetype);
2879 } while (page && check_new_pages(page, order));
2880 spin_unlock(&zone->lock);
2883 __mod_zone_freepage_state(zone, -(1 << order),
2884 get_pcppage_migratetype(page));
2886 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
2887 zone_statistics(preferred_zone, zone);
2888 local_irq_restore(flags);
2891 VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
2895 local_irq_restore(flags);
2899 #ifdef CONFIG_FAIL_PAGE_ALLOC
2902 struct fault_attr attr;
2904 bool ignore_gfp_highmem;
2905 bool ignore_gfp_reclaim;
2907 } fail_page_alloc = {
2908 .attr = FAULT_ATTR_INITIALIZER,
2909 .ignore_gfp_reclaim = true,
2910 .ignore_gfp_highmem = true,
2914 static int __init setup_fail_page_alloc(char *str)
2916 return setup_fault_attr(&fail_page_alloc.attr, str);
2918 __setup("fail_page_alloc=", setup_fail_page_alloc);
2920 static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
2922 if (order < fail_page_alloc.min_order)
2924 if (gfp_mask & __GFP_NOFAIL)
2926 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
2928 if (fail_page_alloc.ignore_gfp_reclaim &&
2929 (gfp_mask & __GFP_DIRECT_RECLAIM))
2932 return should_fail(&fail_page_alloc.attr, 1 << order);
2935 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
2937 static int __init fail_page_alloc_debugfs(void)
2939 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
2942 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
2943 &fail_page_alloc.attr);
2945 return PTR_ERR(dir);
2947 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
2948 &fail_page_alloc.ignore_gfp_reclaim))
2950 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
2951 &fail_page_alloc.ignore_gfp_highmem))
2953 if (!debugfs_create_u32("min-order", mode, dir,
2954 &fail_page_alloc.min_order))
2959 debugfs_remove_recursive(dir);
2964 late_initcall(fail_page_alloc_debugfs);
2966 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
2968 #else /* CONFIG_FAIL_PAGE_ALLOC */
2970 static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
2975 #endif /* CONFIG_FAIL_PAGE_ALLOC */
2978 * Return true if free base pages are above 'mark'. For high-order checks it
2979 * will return true of the order-0 watermark is reached and there is at least
2980 * one free page of a suitable size. Checking now avoids taking the zone lock
2981 * to check in the allocation paths if no pages are free.
2983 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
2984 int classzone_idx, unsigned int alloc_flags,
2989 const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM));
2991 /* free_pages may go negative - that's OK */
2992 free_pages -= (1 << order) - 1;
2994 if (alloc_flags & ALLOC_HIGH)
2998 * If the caller does not have rights to ALLOC_HARDER then subtract
2999 * the high-atomic reserves. This will over-estimate the size of the
3000 * atomic reserve but it avoids a search.
3002 if (likely(!alloc_harder)) {
3003 free_pages -= z->nr_reserved_highatomic;
3006 * OOM victims can try even harder than normal ALLOC_HARDER
3007 * users on the grounds that it's definitely going to be in
3008 * the exit path shortly and free memory. Any allocation it
3009 * makes during the free path will be small and short-lived.
3011 if (alloc_flags & ALLOC_OOM)
3019 /* If allocation can't use CMA areas don't use free CMA pages */
3020 if (!(alloc_flags & ALLOC_CMA))
3021 free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
3025 * Check watermarks for an order-0 allocation request. If these
3026 * are not met, then a high-order request also cannot go ahead
3027 * even if a suitable page happened to be free.
3029 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
3032 /* If this is an order-0 request then the watermark is fine */
3036 /* For a high-order request, check at least one suitable page is free */
3037 for (o = order; o < MAX_ORDER; o++) {
3038 struct free_area *area = &z->free_area[o];
3044 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
3045 if (!list_empty(&area->free_list[mt]))
3050 if ((alloc_flags & ALLOC_CMA) &&
3051 !list_empty(&area->free_list[MIGRATE_CMA])) {
3056 !list_empty(&area->free_list[MIGRATE_HIGHATOMIC]))
3062 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
3063 int classzone_idx, unsigned int alloc_flags)
3065 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3066 zone_page_state(z, NR_FREE_PAGES));
3069 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
3070 unsigned long mark, int classzone_idx, unsigned int alloc_flags)
3072 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3076 /* If allocation can't use CMA areas don't use free CMA pages */
3077 if (!(alloc_flags & ALLOC_CMA))
3078 cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
3082 * Fast check for order-0 only. If this fails then the reserves
3083 * need to be calculated. There is a corner case where the check
3084 * passes but only the high-order atomic reserve are free. If
3085 * the caller is !atomic then it'll uselessly search the free
3086 * list. That corner case is then slower but it is harmless.
3088 if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
3091 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3095 bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
3096 unsigned long mark, int classzone_idx)
3098 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3100 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
3101 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
3103 return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
3108 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3110 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
3113 #else /* CONFIG_NUMA */
3114 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3118 #endif /* CONFIG_NUMA */
3121 * get_page_from_freelist goes through the zonelist trying to allocate
3124 static struct page *
3125 get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
3126 const struct alloc_context *ac)
3128 struct zoneref *z = ac->preferred_zoneref;
3130 struct pglist_data *last_pgdat_dirty_limit = NULL;
3133 * Scan zonelist, looking for a zone with enough free.
3134 * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
3136 for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3141 if (cpusets_enabled() &&
3142 (alloc_flags & ALLOC_CPUSET) &&
3143 !__cpuset_zone_allowed(zone, gfp_mask))
3146 * When allocating a page cache page for writing, we
3147 * want to get it from a node that is within its dirty
3148 * limit, such that no single node holds more than its
3149 * proportional share of globally allowed dirty pages.
3150 * The dirty limits take into account the node's
3151 * lowmem reserves and high watermark so that kswapd
3152 * should be able to balance it without having to
3153 * write pages from its LRU list.
3155 * XXX: For now, allow allocations to potentially
3156 * exceed the per-node dirty limit in the slowpath
3157 * (spread_dirty_pages unset) before going into reclaim,
3158 * which is important when on a NUMA setup the allowed
3159 * nodes are together not big enough to reach the
3160 * global limit. The proper fix for these situations
3161 * will require awareness of nodes in the
3162 * dirty-throttling and the flusher threads.
3164 if (ac->spread_dirty_pages) {
3165 if (last_pgdat_dirty_limit == zone->zone_pgdat)
3168 if (!node_dirty_ok(zone->zone_pgdat)) {
3169 last_pgdat_dirty_limit = zone->zone_pgdat;
3174 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
3175 if (!zone_watermark_fast(zone, order, mark,
3176 ac_classzone_idx(ac), alloc_flags)) {
3179 /* Checked here to keep the fast path fast */
3180 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
3181 if (alloc_flags & ALLOC_NO_WATERMARKS)
3184 if (node_reclaim_mode == 0 ||
3185 !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
3188 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
3190 case NODE_RECLAIM_NOSCAN:
3193 case NODE_RECLAIM_FULL:
3194 /* scanned but unreclaimable */
3197 /* did we reclaim enough */
3198 if (zone_watermark_ok(zone, order, mark,
3199 ac_classzone_idx(ac), alloc_flags))
3207 page = rmqueue(ac->preferred_zoneref->zone, zone, order,
3208 gfp_mask, alloc_flags, ac->migratetype);
3210 prep_new_page(page, order, gfp_mask, alloc_flags);
3213 * If this is a high-order atomic allocation then check
3214 * if the pageblock should be reserved for the future
3216 if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
3217 reserve_highatomic_pageblock(page, zone, order);
3227 * Large machines with many possible nodes should not always dump per-node
3228 * meminfo in irq context.
3230 static inline bool should_suppress_show_mem(void)
3235 ret = in_interrupt();
3240 static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)
3242 unsigned int filter = SHOW_MEM_FILTER_NODES;
3243 static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1);
3245 if (should_suppress_show_mem() || !__ratelimit(&show_mem_rs))
3249 * This documents exceptions given to allocations in certain
3250 * contexts that are allowed to allocate outside current's set
3253 if (!(gfp_mask & __GFP_NOMEMALLOC))
3254 if (tsk_is_oom_victim(current) ||
3255 (current->flags & (PF_MEMALLOC | PF_EXITING)))
3256 filter &= ~SHOW_MEM_FILTER_NODES;
3257 if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
3258 filter &= ~SHOW_MEM_FILTER_NODES;
3260 show_mem(filter, nodemask);
3263 void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)
3265 struct va_format vaf;
3267 static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL,
3268 DEFAULT_RATELIMIT_BURST);
3270 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
3273 pr_warn("%s: ", current->comm);
3275 va_start(args, fmt);
3278 pr_cont("%pV", &vaf);
3281 pr_cont(", mode:%#x(%pGg), nodemask=", gfp_mask, &gfp_mask);
3283 pr_cont("%*pbl\n", nodemask_pr_args(nodemask));
3285 pr_cont("(null)\n");
3287 cpuset_print_current_mems_allowed();
3290 warn_alloc_show_mem(gfp_mask, nodemask);
3293 static inline struct page *
3294 __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order,
3295 unsigned int alloc_flags,
3296 const struct alloc_context *ac)
3300 page = get_page_from_freelist(gfp_mask, order,
3301 alloc_flags|ALLOC_CPUSET, ac);
3303 * fallback to ignore cpuset restriction if our nodes
3307 page = get_page_from_freelist(gfp_mask, order,
3313 static inline struct page *
3314 __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
3315 const struct alloc_context *ac, unsigned long *did_some_progress)
3317 struct oom_control oc = {
3318 .zonelist = ac->zonelist,
3319 .nodemask = ac->nodemask,
3321 .gfp_mask = gfp_mask,
3326 *did_some_progress = 0;
3329 * Acquire the oom lock. If that fails, somebody else is
3330 * making progress for us.
3332 if (!mutex_trylock(&oom_lock)) {
3333 *did_some_progress = 1;
3334 schedule_timeout_uninterruptible(1);
3339 * Go through the zonelist yet one more time, keep very high watermark
3340 * here, this is only to catch a parallel oom killing, we must fail if
3341 * we're still under heavy pressure. But make sure that this reclaim
3342 * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY
3343 * allocation which will never fail due to oom_lock already held.
3345 page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) &
3346 ~__GFP_DIRECT_RECLAIM, order,
3347 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
3351 /* Coredumps can quickly deplete all memory reserves */
3352 if (current->flags & PF_DUMPCORE)
3354 /* The OOM killer will not help higher order allocs */
3355 if (order > PAGE_ALLOC_COSTLY_ORDER)
3358 * We have already exhausted all our reclaim opportunities without any
3359 * success so it is time to admit defeat. We will skip the OOM killer
3360 * because it is very likely that the caller has a more reasonable
3361 * fallback than shooting a random task.
3363 if (gfp_mask & __GFP_RETRY_MAYFAIL)
3365 /* The OOM killer does not needlessly kill tasks for lowmem */
3366 if (ac->high_zoneidx < ZONE_NORMAL)
3368 if (pm_suspended_storage())