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 #ifdef CONFIG_HAVE_MEMORYLESS_NODES
87 * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly.
88 * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined.
89 * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem()
90 * defined in <linux/topology.h>.
92 DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */
93 EXPORT_PER_CPU_SYMBOL(_numa_mem_);
94 int _node_numa_mem_[MAX_NUMNODES];
97 /* work_structs for global per-cpu drains */
98 DEFINE_MUTEX(pcpu_drain_mutex);
99 DEFINE_PER_CPU(struct work_struct, pcpu_drain);
101 #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY
102 volatile unsigned long latent_entropy __latent_entropy;
103 EXPORT_SYMBOL(latent_entropy);
107 * Array of node states.
109 nodemask_t node_states[NR_NODE_STATES] __read_mostly = {
110 [N_POSSIBLE] = NODE_MASK_ALL,
111 [N_ONLINE] = { { [0] = 1UL } },
113 [N_NORMAL_MEMORY] = { { [0] = 1UL } },
114 #ifdef CONFIG_HIGHMEM
115 [N_HIGH_MEMORY] = { { [0] = 1UL } },
117 [N_MEMORY] = { { [0] = 1UL } },
118 [N_CPU] = { { [0] = 1UL } },
121 EXPORT_SYMBOL(node_states);
123 /* Protect totalram_pages and zone->managed_pages */
124 static DEFINE_SPINLOCK(managed_page_count_lock);
126 unsigned long totalram_pages __read_mostly;
127 unsigned long totalreserve_pages __read_mostly;
128 unsigned long totalcma_pages __read_mostly;
130 int percpu_pagelist_fraction;
131 gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK;
134 * A cached value of the page's pageblock's migratetype, used when the page is
135 * put on a pcplist. Used to avoid the pageblock migratetype lookup when
136 * freeing from pcplists in most cases, at the cost of possibly becoming stale.
137 * Also the migratetype set in the page does not necessarily match the pcplist
138 * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any
139 * other index - this ensures that it will be put on the correct CMA freelist.
141 static inline int get_pcppage_migratetype(struct page *page)
146 static inline void set_pcppage_migratetype(struct page *page, int migratetype)
148 page->index = migratetype;
151 #ifdef CONFIG_PM_SLEEP
153 * The following functions are used by the suspend/hibernate code to temporarily
154 * change gfp_allowed_mask in order to avoid using I/O during memory allocations
155 * while devices are suspended. To avoid races with the suspend/hibernate code,
156 * they should always be called with pm_mutex held (gfp_allowed_mask also should
157 * only be modified with pm_mutex held, unless the suspend/hibernate code is
158 * guaranteed not to run in parallel with that modification).
161 static gfp_t saved_gfp_mask;
163 void pm_restore_gfp_mask(void)
165 WARN_ON(!mutex_is_locked(&pm_mutex));
166 if (saved_gfp_mask) {
167 gfp_allowed_mask = saved_gfp_mask;
172 void pm_restrict_gfp_mask(void)
174 WARN_ON(!mutex_is_locked(&pm_mutex));
175 WARN_ON(saved_gfp_mask);
176 saved_gfp_mask = gfp_allowed_mask;
177 gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS);
180 bool pm_suspended_storage(void)
182 if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS))
186 #endif /* CONFIG_PM_SLEEP */
188 #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE
189 unsigned int pageblock_order __read_mostly;
192 static void __free_pages_ok(struct page *page, unsigned int order);
195 * results with 256, 32 in the lowmem_reserve sysctl:
196 * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high)
197 * 1G machine -> (16M dma, 784M normal, 224M high)
198 * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA
199 * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL
200 * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA
202 * TBD: should special case ZONE_DMA32 machines here - in those we normally
203 * don't need any ZONE_NORMAL reservation
205 int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = {
206 #ifdef CONFIG_ZONE_DMA
209 #ifdef CONFIG_ZONE_DMA32
212 #ifdef CONFIG_HIGHMEM
218 EXPORT_SYMBOL(totalram_pages);
220 static char * const zone_names[MAX_NR_ZONES] = {
221 #ifdef CONFIG_ZONE_DMA
224 #ifdef CONFIG_ZONE_DMA32
228 #ifdef CONFIG_HIGHMEM
232 #ifdef CONFIG_ZONE_DEVICE
237 char * const migratetype_names[MIGRATE_TYPES] = {
245 #ifdef CONFIG_MEMORY_ISOLATION
250 compound_page_dtor * const compound_page_dtors[] = {
253 #ifdef CONFIG_HUGETLB_PAGE
256 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
261 int min_free_kbytes = 1024;
262 int user_min_free_kbytes = -1;
263 int watermark_scale_factor = 10;
265 static unsigned long __meminitdata nr_kernel_pages;
266 static unsigned long __meminitdata nr_all_pages;
267 static unsigned long __meminitdata dma_reserve;
269 #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
270 static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES];
271 static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES];
272 static unsigned long __initdata required_kernelcore;
273 static unsigned long __initdata required_movablecore;
274 static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES];
275 static bool mirrored_kernelcore;
277 /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */
279 EXPORT_SYMBOL(movable_zone);
280 #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
283 int nr_node_ids __read_mostly = MAX_NUMNODES;
284 int nr_online_nodes __read_mostly = 1;
285 EXPORT_SYMBOL(nr_node_ids);
286 EXPORT_SYMBOL(nr_online_nodes);
289 int page_group_by_mobility_disabled __read_mostly;
291 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
292 static inline void reset_deferred_meminit(pg_data_t *pgdat)
294 unsigned long max_initialise;
295 unsigned long reserved_lowmem;
298 * Initialise at least 2G of a node but also take into account that
299 * two large system hashes that can take up 1GB for 0.25TB/node.
301 max_initialise = max(2UL << (30 - PAGE_SHIFT),
302 (pgdat->node_spanned_pages >> 8));
305 * Compensate the all the memblock reservations (e.g. crash kernel)
306 * from the initial estimation to make sure we will initialize enough
309 reserved_lowmem = memblock_reserved_memory_within(pgdat->node_start_pfn,
310 pgdat->node_start_pfn + max_initialise);
311 max_initialise += reserved_lowmem;
313 pgdat->static_init_size = min(max_initialise, pgdat->node_spanned_pages);
314 pgdat->first_deferred_pfn = ULONG_MAX;
317 /* Returns true if the struct page for the pfn is uninitialised */
318 static inline bool __meminit early_page_uninitialised(unsigned long pfn)
320 int nid = early_pfn_to_nid(pfn);
322 if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn)
329 * Returns false when the remaining initialisation should be deferred until
330 * later in the boot cycle when it can be parallelised.
332 static inline bool update_defer_init(pg_data_t *pgdat,
333 unsigned long pfn, unsigned long zone_end,
334 unsigned long *nr_initialised)
336 /* Always populate low zones for address-contrained allocations */
337 if (zone_end < pgdat_end_pfn(pgdat))
340 if ((*nr_initialised > pgdat->static_init_size) &&
341 (pfn & (PAGES_PER_SECTION - 1)) == 0) {
342 pgdat->first_deferred_pfn = pfn;
349 static inline void reset_deferred_meminit(pg_data_t *pgdat)
353 static inline bool early_page_uninitialised(unsigned long pfn)
358 static inline bool update_defer_init(pg_data_t *pgdat,
359 unsigned long pfn, unsigned long zone_end,
360 unsigned long *nr_initialised)
366 /* Return a pointer to the bitmap storing bits affecting a block of pages */
367 static inline unsigned long *get_pageblock_bitmap(struct page *page,
370 #ifdef CONFIG_SPARSEMEM
371 return __pfn_to_section(pfn)->pageblock_flags;
373 return page_zone(page)->pageblock_flags;
374 #endif /* CONFIG_SPARSEMEM */
377 static inline int pfn_to_bitidx(struct page *page, unsigned long pfn)
379 #ifdef CONFIG_SPARSEMEM
380 pfn &= (PAGES_PER_SECTION-1);
381 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
383 pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages);
384 return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS;
385 #endif /* CONFIG_SPARSEMEM */
389 * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages
390 * @page: The page within the block of interest
391 * @pfn: The target page frame number
392 * @end_bitidx: The last bit of interest to retrieve
393 * @mask: mask of bits that the caller is interested in
395 * Return: pageblock_bits flags
397 static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page,
399 unsigned long end_bitidx,
402 unsigned long *bitmap;
403 unsigned long bitidx, word_bitidx;
406 bitmap = get_pageblock_bitmap(page, pfn);
407 bitidx = pfn_to_bitidx(page, pfn);
408 word_bitidx = bitidx / BITS_PER_LONG;
409 bitidx &= (BITS_PER_LONG-1);
411 word = bitmap[word_bitidx];
412 bitidx += end_bitidx;
413 return (word >> (BITS_PER_LONG - bitidx - 1)) & mask;
416 unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn,
417 unsigned long end_bitidx,
420 return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask);
423 static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn)
425 return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK);
429 * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages
430 * @page: The page within the block of interest
431 * @flags: The flags to set
432 * @pfn: The target page frame number
433 * @end_bitidx: The last bit of interest
434 * @mask: mask of bits that the caller is interested in
436 void set_pfnblock_flags_mask(struct page *page, unsigned long flags,
438 unsigned long end_bitidx,
441 unsigned long *bitmap;
442 unsigned long bitidx, word_bitidx;
443 unsigned long old_word, word;
445 BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4);
447 bitmap = get_pageblock_bitmap(page, pfn);
448 bitidx = pfn_to_bitidx(page, pfn);
449 word_bitidx = bitidx / BITS_PER_LONG;
450 bitidx &= (BITS_PER_LONG-1);
452 VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page);
454 bitidx += end_bitidx;
455 mask <<= (BITS_PER_LONG - bitidx - 1);
456 flags <<= (BITS_PER_LONG - bitidx - 1);
458 word = READ_ONCE(bitmap[word_bitidx]);
460 old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags);
461 if (word == old_word)
467 void set_pageblock_migratetype(struct page *page, int migratetype)
469 if (unlikely(page_group_by_mobility_disabled &&
470 migratetype < MIGRATE_PCPTYPES))
471 migratetype = MIGRATE_UNMOVABLE;
473 set_pageblock_flags_group(page, (unsigned long)migratetype,
474 PB_migrate, PB_migrate_end);
477 #ifdef CONFIG_DEBUG_VM
478 static int page_outside_zone_boundaries(struct zone *zone, struct page *page)
482 unsigned long pfn = page_to_pfn(page);
483 unsigned long sp, start_pfn;
486 seq = zone_span_seqbegin(zone);
487 start_pfn = zone->zone_start_pfn;
488 sp = zone->spanned_pages;
489 if (!zone_spans_pfn(zone, pfn))
491 } while (zone_span_seqretry(zone, seq));
494 pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n",
495 pfn, zone_to_nid(zone), zone->name,
496 start_pfn, start_pfn + sp);
501 static int page_is_consistent(struct zone *zone, struct page *page)
503 if (!pfn_valid_within(page_to_pfn(page)))
505 if (zone != page_zone(page))
511 * Temporary debugging check for pages not lying within a given zone.
513 static int __maybe_unused bad_range(struct zone *zone, struct page *page)
515 if (page_outside_zone_boundaries(zone, page))
517 if (!page_is_consistent(zone, page))
523 static inline int __maybe_unused bad_range(struct zone *zone, struct page *page)
529 static void bad_page(struct page *page, const char *reason,
530 unsigned long bad_flags)
532 static unsigned long resume;
533 static unsigned long nr_shown;
534 static unsigned long nr_unshown;
537 * Allow a burst of 60 reports, then keep quiet for that minute;
538 * or allow a steady drip of one report per second.
540 if (nr_shown == 60) {
541 if (time_before(jiffies, resume)) {
547 "BUG: Bad page state: %lu messages suppressed\n",
554 resume = jiffies + 60 * HZ;
556 pr_alert("BUG: Bad page state in process %s pfn:%05lx\n",
557 current->comm, page_to_pfn(page));
558 __dump_page(page, reason);
559 bad_flags &= page->flags;
561 pr_alert("bad because of flags: %#lx(%pGp)\n",
562 bad_flags, &bad_flags);
563 dump_page_owner(page);
568 /* Leave bad fields for debug, except PageBuddy could make trouble */
569 page_mapcount_reset(page); /* remove PageBuddy */
570 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
574 * Higher-order pages are called "compound pages". They are structured thusly:
576 * The first PAGE_SIZE page is called the "head page" and have PG_head set.
578 * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded
579 * in bit 0 of page->compound_head. The rest of bits is pointer to head page.
581 * The first tail page's ->compound_dtor holds the offset in array of compound
582 * page destructors. See compound_page_dtors.
584 * The first tail page's ->compound_order holds the order of allocation.
585 * This usage means that zero-order pages may not be compound.
588 void free_compound_page(struct page *page)
590 __free_pages_ok(page, compound_order(page));
593 void prep_compound_page(struct page *page, unsigned int order)
596 int nr_pages = 1 << order;
598 set_compound_page_dtor(page, COMPOUND_PAGE_DTOR);
599 set_compound_order(page, order);
601 for (i = 1; i < nr_pages; i++) {
602 struct page *p = page + i;
603 set_page_count(p, 0);
604 p->mapping = TAIL_MAPPING;
605 set_compound_head(p, page);
607 atomic_set(compound_mapcount_ptr(page), -1);
610 #ifdef CONFIG_DEBUG_PAGEALLOC
611 unsigned int _debug_guardpage_minorder;
612 bool _debug_pagealloc_enabled __read_mostly
613 = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT);
614 EXPORT_SYMBOL(_debug_pagealloc_enabled);
615 bool _debug_guardpage_enabled __read_mostly;
617 static int __init early_debug_pagealloc(char *buf)
621 return kstrtobool(buf, &_debug_pagealloc_enabled);
623 early_param("debug_pagealloc", early_debug_pagealloc);
625 static bool need_debug_guardpage(void)
627 /* If we don't use debug_pagealloc, we don't need guard page */
628 if (!debug_pagealloc_enabled())
631 if (!debug_guardpage_minorder())
637 static void init_debug_guardpage(void)
639 if (!debug_pagealloc_enabled())
642 if (!debug_guardpage_minorder())
645 _debug_guardpage_enabled = true;
648 struct page_ext_operations debug_guardpage_ops = {
649 .need = need_debug_guardpage,
650 .init = init_debug_guardpage,
653 static int __init debug_guardpage_minorder_setup(char *buf)
657 if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) {
658 pr_err("Bad debug_guardpage_minorder value\n");
661 _debug_guardpage_minorder = res;
662 pr_info("Setting debug_guardpage_minorder to %lu\n", res);
665 early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup);
667 static inline bool set_page_guard(struct zone *zone, struct page *page,
668 unsigned int order, int migratetype)
670 struct page_ext *page_ext;
672 if (!debug_guardpage_enabled())
675 if (order >= debug_guardpage_minorder())
678 page_ext = lookup_page_ext(page);
679 if (unlikely(!page_ext))
682 __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
684 INIT_LIST_HEAD(&page->lru);
685 set_page_private(page, order);
686 /* Guard pages are not available for any usage */
687 __mod_zone_freepage_state(zone, -(1 << order), migratetype);
692 static inline void clear_page_guard(struct zone *zone, struct page *page,
693 unsigned int order, int migratetype)
695 struct page_ext *page_ext;
697 if (!debug_guardpage_enabled())
700 page_ext = lookup_page_ext(page);
701 if (unlikely(!page_ext))
704 __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags);
706 set_page_private(page, 0);
707 if (!is_migrate_isolate(migratetype))
708 __mod_zone_freepage_state(zone, (1 << order), migratetype);
711 struct page_ext_operations debug_guardpage_ops;
712 static inline bool set_page_guard(struct zone *zone, struct page *page,
713 unsigned int order, int migratetype) { return false; }
714 static inline void clear_page_guard(struct zone *zone, struct page *page,
715 unsigned int order, int migratetype) {}
718 static inline void set_page_order(struct page *page, unsigned int order)
720 set_page_private(page, order);
721 __SetPageBuddy(page);
724 static inline void rmv_page_order(struct page *page)
726 __ClearPageBuddy(page);
727 set_page_private(page, 0);
731 * This function checks whether a page is free && is the buddy
732 * we can do coalesce a page and its buddy if
733 * (a) the buddy is not in a hole (check before calling!) &&
734 * (b) the buddy is in the buddy system &&
735 * (c) a page and its buddy have the same order &&
736 * (d) a page and its buddy are in the same zone.
738 * For recording whether a page is in the buddy system, we set ->_mapcount
739 * PAGE_BUDDY_MAPCOUNT_VALUE.
740 * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is
741 * serialized by zone->lock.
743 * For recording page's order, we use page_private(page).
745 static inline int page_is_buddy(struct page *page, struct page *buddy,
748 if (page_is_guard(buddy) && page_order(buddy) == order) {
749 if (page_zone_id(page) != page_zone_id(buddy))
752 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
757 if (PageBuddy(buddy) && page_order(buddy) == order) {
759 * zone check is done late to avoid uselessly
760 * calculating zone/node ids for pages that could
763 if (page_zone_id(page) != page_zone_id(buddy))
766 VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy);
774 * Freeing function for a buddy system allocator.
776 * The concept of a buddy system is to maintain direct-mapped table
777 * (containing bit values) for memory blocks of various "orders".
778 * The bottom level table contains the map for the smallest allocatable
779 * units of memory (here, pages), and each level above it describes
780 * pairs of units from the levels below, hence, "buddies".
781 * At a high level, all that happens here is marking the table entry
782 * at the bottom level available, and propagating the changes upward
783 * as necessary, plus some accounting needed to play nicely with other
784 * parts of the VM system.
785 * At each level, we keep a list of pages, which are heads of continuous
786 * free pages of length of (1 << order) and marked with _mapcount
787 * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page)
789 * So when we are allocating or freeing one, we can derive the state of the
790 * other. That is, if we allocate a small block, and both were
791 * free, the remainder of the region must be split into blocks.
792 * If a block is freed, and its buddy is also free, then this
793 * triggers coalescing into a block of larger size.
798 static inline void __free_one_page(struct page *page,
800 struct zone *zone, unsigned int order,
803 unsigned long combined_pfn;
804 unsigned long uninitialized_var(buddy_pfn);
806 unsigned int max_order;
808 max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1);
810 VM_BUG_ON(!zone_is_initialized(zone));
811 VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page);
813 VM_BUG_ON(migratetype == -1);
814 if (likely(!is_migrate_isolate(migratetype)))
815 __mod_zone_freepage_state(zone, 1 << order, migratetype);
817 VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page);
818 VM_BUG_ON_PAGE(bad_range(zone, page), page);
821 while (order < max_order - 1) {
822 buddy_pfn = __find_buddy_pfn(pfn, order);
823 buddy = page + (buddy_pfn - pfn);
825 if (!pfn_valid_within(buddy_pfn))
827 if (!page_is_buddy(page, buddy, order))
830 * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page,
831 * merge with it and move up one order.
833 if (page_is_guard(buddy)) {
834 clear_page_guard(zone, buddy, order, migratetype);
836 list_del(&buddy->lru);
837 zone->free_area[order].nr_free--;
838 rmv_page_order(buddy);
840 combined_pfn = buddy_pfn & pfn;
841 page = page + (combined_pfn - pfn);
845 if (max_order < MAX_ORDER) {
846 /* If we are here, it means order is >= pageblock_order.
847 * We want to prevent merge between freepages on isolate
848 * pageblock and normal pageblock. Without this, pageblock
849 * isolation could cause incorrect freepage or CMA accounting.
851 * We don't want to hit this code for the more frequent
854 if (unlikely(has_isolate_pageblock(zone))) {
857 buddy_pfn = __find_buddy_pfn(pfn, order);
858 buddy = page + (buddy_pfn - pfn);
859 buddy_mt = get_pageblock_migratetype(buddy);
861 if (migratetype != buddy_mt
862 && (is_migrate_isolate(migratetype) ||
863 is_migrate_isolate(buddy_mt)))
867 goto continue_merging;
871 set_page_order(page, order);
874 * If this is not the largest possible page, check if the buddy
875 * of the next-highest order is free. If it is, it's possible
876 * that pages are being freed that will coalesce soon. In case,
877 * that is happening, add the free page to the tail of the list
878 * so it's less likely to be used soon and more likely to be merged
879 * as a higher order page
881 if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) {
882 struct page *higher_page, *higher_buddy;
883 combined_pfn = buddy_pfn & pfn;
884 higher_page = page + (combined_pfn - pfn);
885 buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1);
886 higher_buddy = higher_page + (buddy_pfn - combined_pfn);
887 if (pfn_valid_within(buddy_pfn) &&
888 page_is_buddy(higher_page, higher_buddy, order + 1)) {
889 list_add_tail(&page->lru,
890 &zone->free_area[order].free_list[migratetype]);
895 list_add(&page->lru, &zone->free_area[order].free_list[migratetype]);
897 zone->free_area[order].nr_free++;
901 * A bad page could be due to a number of fields. Instead of multiple branches,
902 * try and check multiple fields with one check. The caller must do a detailed
903 * check if necessary.
905 static inline bool page_expected_state(struct page *page,
906 unsigned long check_flags)
908 if (unlikely(atomic_read(&page->_mapcount) != -1))
911 if (unlikely((unsigned long)page->mapping |
912 page_ref_count(page) |
914 (unsigned long)page->mem_cgroup |
916 (page->flags & check_flags)))
922 static void free_pages_check_bad(struct page *page)
924 const char *bad_reason;
925 unsigned long bad_flags;
930 if (unlikely(atomic_read(&page->_mapcount) != -1))
931 bad_reason = "nonzero mapcount";
932 if (unlikely(page->mapping != NULL))
933 bad_reason = "non-NULL mapping";
934 if (unlikely(page_ref_count(page) != 0))
935 bad_reason = "nonzero _refcount";
936 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) {
937 bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set";
938 bad_flags = PAGE_FLAGS_CHECK_AT_FREE;
941 if (unlikely(page->mem_cgroup))
942 bad_reason = "page still charged to cgroup";
944 bad_page(page, bad_reason, bad_flags);
947 static inline int free_pages_check(struct page *page)
949 if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE)))
952 /* Something has gone sideways, find it */
953 free_pages_check_bad(page);
957 static int free_tail_pages_check(struct page *head_page, struct page *page)
962 * We rely page->lru.next never has bit 0 set, unless the page
963 * is PageTail(). Let's make sure that's true even for poisoned ->lru.
965 BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1);
967 if (!IS_ENABLED(CONFIG_DEBUG_VM)) {
971 switch (page - head_page) {
973 /* the first tail page: ->mapping is compound_mapcount() */
974 if (unlikely(compound_mapcount(page))) {
975 bad_page(page, "nonzero compound_mapcount", 0);
981 * the second tail page: ->mapping is
982 * page_deferred_list().next -- ignore value.
986 if (page->mapping != TAIL_MAPPING) {
987 bad_page(page, "corrupted mapping in tail page", 0);
992 if (unlikely(!PageTail(page))) {
993 bad_page(page, "PageTail not set", 0);
996 if (unlikely(compound_head(page) != head_page)) {
997 bad_page(page, "compound_head not consistent", 0);
1002 page->mapping = NULL;
1003 clear_compound_head(page);
1007 static __always_inline bool free_pages_prepare(struct page *page,
1008 unsigned int order, bool check_free)
1012 VM_BUG_ON_PAGE(PageTail(page), page);
1014 trace_mm_page_free(page, order);
1017 * Check tail pages before head page information is cleared to
1018 * avoid checking PageCompound for order-0 pages.
1020 if (unlikely(order)) {
1021 bool compound = PageCompound(page);
1024 VM_BUG_ON_PAGE(compound && compound_order(page) != order, page);
1027 ClearPageDoubleMap(page);
1028 for (i = 1; i < (1 << order); i++) {
1030 bad += free_tail_pages_check(page, page + i);
1031 if (unlikely(free_pages_check(page + i))) {
1035 (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1038 if (PageMappingFlags(page))
1039 page->mapping = NULL;
1040 if (memcg_kmem_enabled() && PageKmemcg(page))
1041 memcg_kmem_uncharge(page, order);
1043 bad += free_pages_check(page);
1047 page_cpupid_reset_last(page);
1048 page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP;
1049 reset_page_owner(page, order);
1051 if (!PageHighMem(page)) {
1052 debug_check_no_locks_freed(page_address(page),
1053 PAGE_SIZE << order);
1054 debug_check_no_obj_freed(page_address(page),
1055 PAGE_SIZE << order);
1057 arch_free_page(page, order);
1058 kernel_poison_pages(page, 1 << order, 0);
1059 kernel_map_pages(page, 1 << order, 0);
1060 kasan_free_pages(page, order);
1065 #ifdef CONFIG_DEBUG_VM
1066 static inline bool free_pcp_prepare(struct page *page)
1068 return free_pages_prepare(page, 0, true);
1071 static inline bool bulkfree_pcp_prepare(struct page *page)
1076 static bool free_pcp_prepare(struct page *page)
1078 return free_pages_prepare(page, 0, false);
1081 static bool bulkfree_pcp_prepare(struct page *page)
1083 return free_pages_check(page);
1085 #endif /* CONFIG_DEBUG_VM */
1088 * Frees a number of pages from the PCP lists
1089 * Assumes all pages on list are in same zone, and of same order.
1090 * count is the number of pages to free.
1092 * If the zone was previously in an "all pages pinned" state then look to
1093 * see if this freeing clears that state.
1095 * And clear the zone's pages_scanned counter, to hold off the "all pages are
1096 * pinned" detection logic.
1098 static void free_pcppages_bulk(struct zone *zone, int count,
1099 struct per_cpu_pages *pcp)
1101 int migratetype = 0;
1103 bool isolated_pageblocks;
1105 spin_lock(&zone->lock);
1106 isolated_pageblocks = has_isolate_pageblock(zone);
1110 struct list_head *list;
1113 * Remove pages from lists in a round-robin fashion. A
1114 * batch_free count is maintained that is incremented when an
1115 * empty list is encountered. This is so more pages are freed
1116 * off fuller lists instead of spinning excessively around empty
1121 if (++migratetype == MIGRATE_PCPTYPES)
1123 list = &pcp->lists[migratetype];
1124 } while (list_empty(list));
1126 /* This is the only non-empty list. Free them all. */
1127 if (batch_free == MIGRATE_PCPTYPES)
1131 int mt; /* migratetype of the to-be-freed page */
1133 page = list_last_entry(list, struct page, lru);
1134 /* must delete as __free_one_page list manipulates */
1135 list_del(&page->lru);
1137 mt = get_pcppage_migratetype(page);
1138 /* MIGRATE_ISOLATE page should not go to pcplists */
1139 VM_BUG_ON_PAGE(is_migrate_isolate(mt), page);
1140 /* Pageblock could have been isolated meanwhile */
1141 if (unlikely(isolated_pageblocks))
1142 mt = get_pageblock_migratetype(page);
1144 if (bulkfree_pcp_prepare(page))
1147 __free_one_page(page, page_to_pfn(page), zone, 0, mt);
1148 trace_mm_page_pcpu_drain(page, 0, mt);
1149 } while (--count && --batch_free && !list_empty(list));
1151 spin_unlock(&zone->lock);
1154 static void free_one_page(struct zone *zone,
1155 struct page *page, unsigned long pfn,
1159 spin_lock(&zone->lock);
1160 if (unlikely(has_isolate_pageblock(zone) ||
1161 is_migrate_isolate(migratetype))) {
1162 migratetype = get_pfnblock_migratetype(page, pfn);
1164 __free_one_page(page, pfn, zone, order, migratetype);
1165 spin_unlock(&zone->lock);
1168 static void __meminit __init_single_page(struct page *page, unsigned long pfn,
1169 unsigned long zone, int nid)
1171 set_page_links(page, zone, nid, pfn);
1172 init_page_count(page);
1173 page_mapcount_reset(page);
1174 page_cpupid_reset_last(page);
1176 INIT_LIST_HEAD(&page->lru);
1177 #ifdef WANT_PAGE_VIRTUAL
1178 /* The shift won't overflow because ZONE_NORMAL is below 4G. */
1179 if (!is_highmem_idx(zone))
1180 set_page_address(page, __va(pfn << PAGE_SHIFT));
1184 static void __meminit __init_single_pfn(unsigned long pfn, unsigned long zone,
1187 return __init_single_page(pfn_to_page(pfn), pfn, zone, nid);
1190 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1191 static void __meminit init_reserved_page(unsigned long pfn)
1196 if (!early_page_uninitialised(pfn))
1199 nid = early_pfn_to_nid(pfn);
1200 pgdat = NODE_DATA(nid);
1202 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1203 struct zone *zone = &pgdat->node_zones[zid];
1205 if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone))
1208 __init_single_pfn(pfn, zid, nid);
1211 static inline void init_reserved_page(unsigned long pfn)
1214 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1217 * Initialised pages do not have PageReserved set. This function is
1218 * called for each range allocated by the bootmem allocator and
1219 * marks the pages PageReserved. The remaining valid pages are later
1220 * sent to the buddy page allocator.
1222 void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end)
1224 unsigned long start_pfn = PFN_DOWN(start);
1225 unsigned long end_pfn = PFN_UP(end);
1227 for (; start_pfn < end_pfn; start_pfn++) {
1228 if (pfn_valid(start_pfn)) {
1229 struct page *page = pfn_to_page(start_pfn);
1231 init_reserved_page(start_pfn);
1233 /* Avoid false-positive PageTail() */
1234 INIT_LIST_HEAD(&page->lru);
1236 SetPageReserved(page);
1241 static void __free_pages_ok(struct page *page, unsigned int order)
1243 unsigned long flags;
1245 unsigned long pfn = page_to_pfn(page);
1247 if (!free_pages_prepare(page, order, true))
1250 migratetype = get_pfnblock_migratetype(page, pfn);
1251 local_irq_save(flags);
1252 __count_vm_events(PGFREE, 1 << order);
1253 free_one_page(page_zone(page), page, pfn, order, migratetype);
1254 local_irq_restore(flags);
1257 static void __init __free_pages_boot_core(struct page *page, unsigned int order)
1259 unsigned int nr_pages = 1 << order;
1260 struct page *p = page;
1264 for (loop = 0; loop < (nr_pages - 1); loop++, p++) {
1266 __ClearPageReserved(p);
1267 set_page_count(p, 0);
1269 __ClearPageReserved(p);
1270 set_page_count(p, 0);
1272 page_zone(page)->managed_pages += nr_pages;
1273 set_page_refcounted(page);
1274 __free_pages(page, order);
1277 #if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \
1278 defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP)
1280 static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata;
1282 int __meminit early_pfn_to_nid(unsigned long pfn)
1284 static DEFINE_SPINLOCK(early_pfn_lock);
1287 spin_lock(&early_pfn_lock);
1288 nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache);
1290 nid = first_online_node;
1291 spin_unlock(&early_pfn_lock);
1297 #ifdef CONFIG_NODES_SPAN_OTHER_NODES
1298 static inline bool __meminit __maybe_unused
1299 meminit_pfn_in_nid(unsigned long pfn, int node,
1300 struct mminit_pfnnid_cache *state)
1304 nid = __early_pfn_to_nid(pfn, state);
1305 if (nid >= 0 && nid != node)
1310 /* Only safe to use early in boot when initialisation is single-threaded */
1311 static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1313 return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache);
1318 static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
1322 static inline bool __meminit __maybe_unused
1323 meminit_pfn_in_nid(unsigned long pfn, int node,
1324 struct mminit_pfnnid_cache *state)
1331 void __init __free_pages_bootmem(struct page *page, unsigned long pfn,
1334 if (early_page_uninitialised(pfn))
1336 return __free_pages_boot_core(page, order);
1340 * Check that the whole (or subset of) a pageblock given by the interval of
1341 * [start_pfn, end_pfn) is valid and within the same zone, before scanning it
1342 * with the migration of free compaction scanner. The scanners then need to
1343 * use only pfn_valid_within() check for arches that allow holes within
1346 * Return struct page pointer of start_pfn, or NULL if checks were not passed.
1348 * It's possible on some configurations to have a setup like node0 node1 node0
1349 * i.e. it's possible that all pages within a zones range of pages do not
1350 * belong to a single zone. We assume that a border between node0 and node1
1351 * can occur within a single pageblock, but not a node0 node1 node0
1352 * interleaving within a single pageblock. It is therefore sufficient to check
1353 * the first and last page of a pageblock and avoid checking each individual
1354 * page in a pageblock.
1356 struct page *__pageblock_pfn_to_page(unsigned long start_pfn,
1357 unsigned long end_pfn, struct zone *zone)
1359 struct page *start_page;
1360 struct page *end_page;
1362 /* end_pfn is one past the range we are checking */
1365 if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn))
1368 start_page = pfn_to_online_page(start_pfn);
1372 if (page_zone(start_page) != zone)
1375 end_page = pfn_to_page(end_pfn);
1377 /* This gives a shorter code than deriving page_zone(end_page) */
1378 if (page_zone_id(start_page) != page_zone_id(end_page))
1384 void set_zone_contiguous(struct zone *zone)
1386 unsigned long block_start_pfn = zone->zone_start_pfn;
1387 unsigned long block_end_pfn;
1389 block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages);
1390 for (; block_start_pfn < zone_end_pfn(zone);
1391 block_start_pfn = block_end_pfn,
1392 block_end_pfn += pageblock_nr_pages) {
1394 block_end_pfn = min(block_end_pfn, zone_end_pfn(zone));
1396 if (!__pageblock_pfn_to_page(block_start_pfn,
1397 block_end_pfn, zone))
1401 /* We confirm that there is no hole */
1402 zone->contiguous = true;
1405 void clear_zone_contiguous(struct zone *zone)
1407 zone->contiguous = false;
1410 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1411 static void __init deferred_free_range(unsigned long pfn,
1412 unsigned long nr_pages)
1420 page = pfn_to_page(pfn);
1422 /* Free a large naturally-aligned chunk if possible */
1423 if (nr_pages == pageblock_nr_pages &&
1424 (pfn & (pageblock_nr_pages - 1)) == 0) {
1425 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
1426 __free_pages_boot_core(page, pageblock_order);
1430 for (i = 0; i < nr_pages; i++, page++, pfn++) {
1431 if ((pfn & (pageblock_nr_pages - 1)) == 0)
1432 set_pageblock_migratetype(page, MIGRATE_MOVABLE);
1433 __free_pages_boot_core(page, 0);
1437 /* Completion tracking for deferred_init_memmap() threads */
1438 static atomic_t pgdat_init_n_undone __initdata;
1439 static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp);
1441 static inline void __init pgdat_init_report_one_done(void)
1443 if (atomic_dec_and_test(&pgdat_init_n_undone))
1444 complete(&pgdat_init_all_done_comp);
1448 * Helper for deferred_init_range, free the given range, reset the counters, and
1449 * return number of pages freed.
1451 static inline unsigned long __init __def_free(unsigned long *nr_free,
1452 unsigned long *free_base_pfn,
1455 unsigned long nr = *nr_free;
1457 deferred_free_range(*free_base_pfn, nr);
1465 static unsigned long __init deferred_init_range(int nid, int zid,
1466 unsigned long start_pfn,
1467 unsigned long end_pfn)
1469 struct mminit_pfnnid_cache nid_init_state = { };
1470 unsigned long nr_pgmask = pageblock_nr_pages - 1;
1471 unsigned long free_base_pfn = 0;
1472 unsigned long nr_pages = 0;
1473 unsigned long nr_free = 0;
1474 struct page *page = NULL;
1478 * First we check if pfn is valid on architectures where it is possible
1479 * to have holes within pageblock_nr_pages. On systems where it is not
1480 * possible, this function is optimized out.
1482 * Then, we check if a current large page is valid by only checking the
1483 * validity of the head pfn.
1485 * meminit_pfn_in_nid is checked on systems where pfns can interleave
1486 * within a node: a pfn is between start and end of a node, but does not
1487 * belong to this memory node.
1489 * Finally, we minimize pfn page lookups and scheduler checks by
1490 * performing it only once every pageblock_nr_pages.
1492 * We do it in two loops: first we initialize struct page, than free to
1493 * buddy allocator, becuse while we are freeing pages we can access
1494 * pages that are ahead (computing buddy page in __free_one_page()).
1496 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1497 if (!pfn_valid_within(pfn))
1499 if ((pfn & nr_pgmask) || pfn_valid(pfn)) {
1500 if (meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
1501 if (page && (pfn & nr_pgmask))
1504 page = pfn_to_page(pfn);
1505 __init_single_page(page, pfn, zid, nid);
1512 for (pfn = start_pfn; pfn < end_pfn; pfn++) {
1513 if (!pfn_valid_within(pfn)) {
1514 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1515 } else if (!(pfn & nr_pgmask) && !pfn_valid(pfn)) {
1516 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1517 } else if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) {
1518 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1519 } else if (page && (pfn & nr_pgmask)) {
1523 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1524 page = pfn_to_page(pfn);
1525 free_base_pfn = pfn;
1530 /* Free the last block of pages to allocator */
1531 nr_pages += __def_free(&nr_free, &free_base_pfn, &page);
1536 /* Initialise remaining memory on a node */
1537 static int __init deferred_init_memmap(void *data)
1539 pg_data_t *pgdat = data;
1540 int nid = pgdat->node_id;
1541 unsigned long start = jiffies;
1542 unsigned long nr_pages = 0;
1543 unsigned long spfn, epfn;
1544 phys_addr_t spa, epa;
1547 unsigned long first_init_pfn = pgdat->first_deferred_pfn;
1548 const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id);
1551 if (first_init_pfn == ULONG_MAX) {
1552 pgdat_init_report_one_done();
1556 /* Bind memory initialisation thread to a local node if possible */
1557 if (!cpumask_empty(cpumask))
1558 set_cpus_allowed_ptr(current, cpumask);
1560 /* Sanity check boundaries */
1561 BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn);
1562 BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat));
1563 pgdat->first_deferred_pfn = ULONG_MAX;
1565 /* Only the highest zone is deferred so find it */
1566 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
1567 zone = pgdat->node_zones + zid;
1568 if (first_init_pfn < zone_end_pfn(zone))
1571 first_init_pfn = max(zone->zone_start_pfn, first_init_pfn);
1573 for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &spa, &epa, NULL) {
1574 spfn = max_t(unsigned long, first_init_pfn, PFN_UP(spa));
1575 epfn = min_t(unsigned long, zone_end_pfn(zone), PFN_DOWN(epa));
1576 nr_pages += deferred_init_range(nid, zid, spfn, epfn);
1579 /* Sanity check that the next zone really is unpopulated */
1580 WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone));
1582 pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages,
1583 jiffies_to_msecs(jiffies - start));
1585 pgdat_init_report_one_done();
1588 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
1590 void __init page_alloc_init_late(void)
1594 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
1597 /* There will be num_node_state(N_MEMORY) threads */
1598 atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY));
1599 for_each_node_state(nid, N_MEMORY) {
1600 kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid);
1603 /* Block until all are initialised */
1604 wait_for_completion(&pgdat_init_all_done_comp);
1606 /* Reinit limits that are based on free pages after the kernel is up */
1607 files_maxfiles_init();
1609 #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK
1610 /* Discard memblock private memory */
1614 for_each_populated_zone(zone)
1615 set_zone_contiguous(zone);
1619 /* Free whole pageblock and set its migration type to MIGRATE_CMA. */
1620 void __init init_cma_reserved_pageblock(struct page *page)
1622 unsigned i = pageblock_nr_pages;
1623 struct page *p = page;
1626 __ClearPageReserved(p);
1627 set_page_count(p, 0);
1630 set_pageblock_migratetype(page, MIGRATE_CMA);
1632 if (pageblock_order >= MAX_ORDER) {
1633 i = pageblock_nr_pages;
1636 set_page_refcounted(p);
1637 __free_pages(p, MAX_ORDER - 1);
1638 p += MAX_ORDER_NR_PAGES;
1639 } while (i -= MAX_ORDER_NR_PAGES);
1641 set_page_refcounted(page);
1642 __free_pages(page, pageblock_order);
1645 adjust_managed_page_count(page, pageblock_nr_pages);
1650 * The order of subdivision here is critical for the IO subsystem.
1651 * Please do not alter this order without good reasons and regression
1652 * testing. Specifically, as large blocks of memory are subdivided,
1653 * the order in which smaller blocks are delivered depends on the order
1654 * they're subdivided in this function. This is the primary factor
1655 * influencing the order in which pages are delivered to the IO
1656 * subsystem according to empirical testing, and this is also justified
1657 * by considering the behavior of a buddy system containing a single
1658 * large block of memory acted on by a series of small allocations.
1659 * This behavior is a critical factor in sglist merging's success.
1663 static inline void expand(struct zone *zone, struct page *page,
1664 int low, int high, struct free_area *area,
1667 unsigned long size = 1 << high;
1669 while (high > low) {
1673 VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]);
1676 * Mark as guard pages (or page), that will allow to
1677 * merge back to allocator when buddy will be freed.
1678 * Corresponding page table entries will not be touched,
1679 * pages will stay not present in virtual address space
1681 if (set_page_guard(zone, &page[size], high, migratetype))
1684 list_add(&page[size].lru, &area->free_list[migratetype]);
1686 set_page_order(&page[size], high);
1690 static void check_new_page_bad(struct page *page)
1692 const char *bad_reason = NULL;
1693 unsigned long bad_flags = 0;
1695 if (unlikely(atomic_read(&page->_mapcount) != -1))
1696 bad_reason = "nonzero mapcount";
1697 if (unlikely(page->mapping != NULL))
1698 bad_reason = "non-NULL mapping";
1699 if (unlikely(page_ref_count(page) != 0))
1700 bad_reason = "nonzero _count";
1701 if (unlikely(page->flags & __PG_HWPOISON)) {
1702 bad_reason = "HWPoisoned (hardware-corrupted)";
1703 bad_flags = __PG_HWPOISON;
1704 /* Don't complain about hwpoisoned pages */
1705 page_mapcount_reset(page); /* remove PageBuddy */
1708 if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) {
1709 bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set";
1710 bad_flags = PAGE_FLAGS_CHECK_AT_PREP;
1713 if (unlikely(page->mem_cgroup))
1714 bad_reason = "page still charged to cgroup";
1716 bad_page(page, bad_reason, bad_flags);
1720 * This page is about to be returned from the page allocator
1722 static inline int check_new_page(struct page *page)
1724 if (likely(page_expected_state(page,
1725 PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON)))
1728 check_new_page_bad(page);
1732 static inline bool free_pages_prezeroed(void)
1734 return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) &&
1735 page_poisoning_enabled();
1738 #ifdef CONFIG_DEBUG_VM
1739 static bool check_pcp_refill(struct page *page)
1744 static bool check_new_pcp(struct page *page)
1746 return check_new_page(page);
1749 static bool check_pcp_refill(struct page *page)
1751 return check_new_page(page);
1753 static bool check_new_pcp(struct page *page)
1757 #endif /* CONFIG_DEBUG_VM */
1759 static bool check_new_pages(struct page *page, unsigned int order)
1762 for (i = 0; i < (1 << order); i++) {
1763 struct page *p = page + i;
1765 if (unlikely(check_new_page(p)))
1772 inline void post_alloc_hook(struct page *page, unsigned int order,
1775 set_page_private(page, 0);
1776 set_page_refcounted(page);
1778 arch_alloc_page(page, order);
1779 kernel_map_pages(page, 1 << order, 1);
1780 kernel_poison_pages(page, 1 << order, 1);
1781 kasan_alloc_pages(page, order);
1782 set_page_owner(page, order, gfp_flags);
1785 static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
1786 unsigned int alloc_flags)
1790 post_alloc_hook(page, order, gfp_flags);
1792 if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO))
1793 for (i = 0; i < (1 << order); i++)
1794 clear_highpage(page + i);
1796 if (order && (gfp_flags & __GFP_COMP))
1797 prep_compound_page(page, order);
1800 * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to
1801 * allocate the page. The expectation is that the caller is taking
1802 * steps that will free more memory. The caller should avoid the page
1803 * being used for !PFMEMALLOC purposes.
1805 if (alloc_flags & ALLOC_NO_WATERMARKS)
1806 set_page_pfmemalloc(page);
1808 clear_page_pfmemalloc(page);
1812 * Go through the free lists for the given migratetype and remove
1813 * the smallest available page from the freelists
1816 struct page *__rmqueue_smallest(struct zone *zone, unsigned int order,
1819 unsigned int current_order;
1820 struct free_area *area;
1823 /* Find a page of the appropriate size in the preferred list */
1824 for (current_order = order; current_order < MAX_ORDER; ++current_order) {
1825 area = &(zone->free_area[current_order]);
1826 page = list_first_entry_or_null(&area->free_list[migratetype],
1830 list_del(&page->lru);
1831 rmv_page_order(page);
1833 expand(zone, page, order, current_order, area, migratetype);
1834 set_pcppage_migratetype(page, migratetype);
1843 * This array describes the order lists are fallen back to when
1844 * the free lists for the desirable migrate type are depleted
1846 static int fallbacks[MIGRATE_TYPES][4] = {
1847 [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1848 [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES },
1849 [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES },
1851 [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */
1853 #ifdef CONFIG_MEMORY_ISOLATION
1854 [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */
1859 static struct page *__rmqueue_cma_fallback(struct zone *zone,
1862 return __rmqueue_smallest(zone, order, MIGRATE_CMA);
1865 static inline struct page *__rmqueue_cma_fallback(struct zone *zone,
1866 unsigned int order) { return NULL; }
1870 * Move the free pages in a range to the free lists of the requested type.
1871 * Note that start_page and end_pages are not aligned on a pageblock
1872 * boundary. If alignment is required, use move_freepages_block()
1874 static int move_freepages(struct zone *zone,
1875 struct page *start_page, struct page *end_page,
1876 int migratetype, int *num_movable)
1880 int pages_moved = 0;
1882 #ifndef CONFIG_HOLES_IN_ZONE
1884 * page_zone is not safe to call in this context when
1885 * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant
1886 * anyway as we check zone boundaries in move_freepages_block().
1887 * Remove at a later date when no bug reports exist related to
1888 * grouping pages by mobility
1890 VM_BUG_ON(page_zone(start_page) != page_zone(end_page));
1896 for (page = start_page; page <= end_page;) {
1897 if (!pfn_valid_within(page_to_pfn(page))) {
1902 /* Make sure we are not inadvertently changing nodes */
1903 VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page);
1905 if (!PageBuddy(page)) {
1907 * We assume that pages that could be isolated for
1908 * migration are movable. But we don't actually try
1909 * isolating, as that would be expensive.
1912 (PageLRU(page) || __PageMovable(page)))
1919 order = page_order(page);
1920 list_move(&page->lru,
1921 &zone->free_area[order].free_list[migratetype]);
1923 pages_moved += 1 << order;
1929 int move_freepages_block(struct zone *zone, struct page *page,
1930 int migratetype, int *num_movable)
1932 unsigned long start_pfn, end_pfn;
1933 struct page *start_page, *end_page;
1935 start_pfn = page_to_pfn(page);
1936 start_pfn = start_pfn & ~(pageblock_nr_pages-1);
1937 start_page = pfn_to_page(start_pfn);
1938 end_page = start_page + pageblock_nr_pages - 1;
1939 end_pfn = start_pfn + pageblock_nr_pages - 1;
1941 /* Do not cross zone boundaries */
1942 if (!zone_spans_pfn(zone, start_pfn))
1944 if (!zone_spans_pfn(zone, end_pfn))
1947 return move_freepages(zone, start_page, end_page, migratetype,
1951 static void change_pageblock_range(struct page *pageblock_page,
1952 int start_order, int migratetype)
1954 int nr_pageblocks = 1 << (start_order - pageblock_order);
1956 while (nr_pageblocks--) {
1957 set_pageblock_migratetype(pageblock_page, migratetype);
1958 pageblock_page += pageblock_nr_pages;
1963 * When we are falling back to another migratetype during allocation, try to
1964 * steal extra free pages from the same pageblocks to satisfy further
1965 * allocations, instead of polluting multiple pageblocks.
1967 * If we are stealing a relatively large buddy page, it is likely there will
1968 * be more free pages in the pageblock, so try to steal them all. For
1969 * reclaimable and unmovable allocations, we steal regardless of page size,
1970 * as fragmentation caused by those allocations polluting movable pageblocks
1971 * is worse than movable allocations stealing from unmovable and reclaimable
1974 static bool can_steal_fallback(unsigned int order, int start_mt)
1977 * Leaving this order check is intended, although there is
1978 * relaxed order check in next check. The reason is that
1979 * we can actually steal whole pageblock if this condition met,
1980 * but, below check doesn't guarantee it and that is just heuristic
1981 * so could be changed anytime.
1983 if (order >= pageblock_order)
1986 if (order >= pageblock_order / 2 ||
1987 start_mt == MIGRATE_RECLAIMABLE ||
1988 start_mt == MIGRATE_UNMOVABLE ||
1989 page_group_by_mobility_disabled)
1996 * This function implements actual steal behaviour. If order is large enough,
1997 * we can steal whole pageblock. If not, we first move freepages in this
1998 * pageblock to our migratetype and determine how many already-allocated pages
1999 * are there in the pageblock with a compatible migratetype. If at least half
2000 * of pages are free or compatible, we can change migratetype of the pageblock
2001 * itself, so pages freed in the future will be put on the correct free list.
2003 static void steal_suitable_fallback(struct zone *zone, struct page *page,
2004 int start_type, bool whole_block)
2006 unsigned int current_order = page_order(page);
2007 struct free_area *area;
2008 int free_pages, movable_pages, alike_pages;
2011 old_block_type = get_pageblock_migratetype(page);
2014 * This can happen due to races and we want to prevent broken
2015 * highatomic accounting.
2017 if (is_migrate_highatomic(old_block_type))
2020 /* Take ownership for orders >= pageblock_order */
2021 if (current_order >= pageblock_order) {
2022 change_pageblock_range(page, current_order, start_type);
2026 /* We are not allowed to try stealing from the whole block */
2030 free_pages = move_freepages_block(zone, page, start_type,
2033 * Determine how many pages are compatible with our allocation.
2034 * For movable allocation, it's the number of movable pages which
2035 * we just obtained. For other types it's a bit more tricky.
2037 if (start_type == MIGRATE_MOVABLE) {
2038 alike_pages = movable_pages;
2041 * If we are falling back a RECLAIMABLE or UNMOVABLE allocation
2042 * to MOVABLE pageblock, consider all non-movable pages as
2043 * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or
2044 * vice versa, be conservative since we can't distinguish the
2045 * exact migratetype of non-movable pages.
2047 if (old_block_type == MIGRATE_MOVABLE)
2048 alike_pages = pageblock_nr_pages
2049 - (free_pages + movable_pages);
2054 /* moving whole block can fail due to zone boundary conditions */
2059 * If a sufficient number of pages in the block are either free or of
2060 * comparable migratability as our allocation, claim the whole block.
2062 if (free_pages + alike_pages >= (1 << (pageblock_order-1)) ||
2063 page_group_by_mobility_disabled)
2064 set_pageblock_migratetype(page, start_type);
2069 area = &zone->free_area[current_order];
2070 list_move(&page->lru, &area->free_list[start_type]);
2074 * Check whether there is a suitable fallback freepage with requested order.
2075 * If only_stealable is true, this function returns fallback_mt only if
2076 * we can steal other freepages all together. This would help to reduce
2077 * fragmentation due to mixed migratetype pages in one pageblock.
2079 int find_suitable_fallback(struct free_area *area, unsigned int order,
2080 int migratetype, bool only_stealable, bool *can_steal)
2085 if (area->nr_free == 0)
2090 fallback_mt = fallbacks[migratetype][i];
2091 if (fallback_mt == MIGRATE_TYPES)
2094 if (list_empty(&area->free_list[fallback_mt]))
2097 if (can_steal_fallback(order, migratetype))
2100 if (!only_stealable)
2111 * Reserve a pageblock for exclusive use of high-order atomic allocations if
2112 * there are no empty page blocks that contain a page with a suitable order
2114 static void reserve_highatomic_pageblock(struct page *page, struct zone *zone,
2115 unsigned int alloc_order)
2118 unsigned long max_managed, flags;
2121 * Limit the number reserved to 1 pageblock or roughly 1% of a zone.
2122 * Check is race-prone but harmless.
2124 max_managed = (zone->managed_pages / 100) + pageblock_nr_pages;
2125 if (zone->nr_reserved_highatomic >= max_managed)
2128 spin_lock_irqsave(&zone->lock, flags);
2130 /* Recheck the nr_reserved_highatomic limit under the lock */
2131 if (zone->nr_reserved_highatomic >= max_managed)
2135 mt = get_pageblock_migratetype(page);
2136 if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt)
2137 && !is_migrate_cma(mt)) {
2138 zone->nr_reserved_highatomic += pageblock_nr_pages;
2139 set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC);
2140 move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL);
2144 spin_unlock_irqrestore(&zone->lock, flags);
2148 * Used when an allocation is about to fail under memory pressure. This
2149 * potentially hurts the reliability of high-order allocations when under
2150 * intense memory pressure but failed atomic allocations should be easier
2151 * to recover from than an OOM.
2153 * If @force is true, try to unreserve a pageblock even though highatomic
2154 * pageblock is exhausted.
2156 static bool unreserve_highatomic_pageblock(const struct alloc_context *ac,
2159 struct zonelist *zonelist = ac->zonelist;
2160 unsigned long flags;
2167 for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx,
2170 * Preserve at least one pageblock unless memory pressure
2173 if (!force && zone->nr_reserved_highatomic <=
2177 spin_lock_irqsave(&zone->lock, flags);
2178 for (order = 0; order < MAX_ORDER; order++) {
2179 struct free_area *area = &(zone->free_area[order]);
2181 page = list_first_entry_or_null(
2182 &area->free_list[MIGRATE_HIGHATOMIC],
2188 * In page freeing path, migratetype change is racy so
2189 * we can counter several free pages in a pageblock
2190 * in this loop althoug we changed the pageblock type
2191 * from highatomic to ac->migratetype. So we should
2192 * adjust the count once.
2194 if (is_migrate_highatomic_page(page)) {
2196 * It should never happen but changes to
2197 * locking could inadvertently allow a per-cpu
2198 * drain to add pages to MIGRATE_HIGHATOMIC
2199 * while unreserving so be safe and watch for
2202 zone->nr_reserved_highatomic -= min(
2204 zone->nr_reserved_highatomic);
2208 * Convert to ac->migratetype and avoid the normal
2209 * pageblock stealing heuristics. Minimally, the caller
2210 * is doing the work and needs the pages. More
2211 * importantly, if the block was always converted to
2212 * MIGRATE_UNMOVABLE or another type then the number
2213 * of pageblocks that cannot be completely freed
2216 set_pageblock_migratetype(page, ac->migratetype);
2217 ret = move_freepages_block(zone, page, ac->migratetype,
2220 spin_unlock_irqrestore(&zone->lock, flags);
2224 spin_unlock_irqrestore(&zone->lock, flags);
2231 * Try finding a free buddy page on the fallback list and put it on the free
2232 * list of requested migratetype, possibly along with other pages from the same
2233 * block, depending on fragmentation avoidance heuristics. Returns true if
2234 * fallback was found so that __rmqueue_smallest() can grab it.
2236 * The use of signed ints for order and current_order is a deliberate
2237 * deviation from the rest of this file, to make the for loop
2238 * condition simpler.
2241 __rmqueue_fallback(struct zone *zone, int order, int start_migratetype)
2243 struct free_area *area;
2250 * Find the largest available free page in the other list. This roughly
2251 * approximates finding the pageblock with the most free pages, which
2252 * would be too costly to do exactly.
2254 for (current_order = MAX_ORDER - 1; current_order >= order;
2256 area = &(zone->free_area[current_order]);
2257 fallback_mt = find_suitable_fallback(area, current_order,
2258 start_migratetype, false, &can_steal);
2259 if (fallback_mt == -1)
2263 * We cannot steal all free pages from the pageblock and the
2264 * requested migratetype is movable. In that case it's better to
2265 * steal and split the smallest available page instead of the
2266 * largest available page, because even if the next movable
2267 * allocation falls back into a different pageblock than this
2268 * one, it won't cause permanent fragmentation.
2270 if (!can_steal && start_migratetype == MIGRATE_MOVABLE
2271 && current_order > order)
2280 for (current_order = order; current_order < MAX_ORDER;
2282 area = &(zone->free_area[current_order]);
2283 fallback_mt = find_suitable_fallback(area, current_order,
2284 start_migratetype, false, &can_steal);
2285 if (fallback_mt != -1)
2290 * This should not happen - we already found a suitable fallback
2291 * when looking for the largest page.
2293 VM_BUG_ON(current_order == MAX_ORDER);
2296 page = list_first_entry(&area->free_list[fallback_mt],
2299 steal_suitable_fallback(zone, page, start_migratetype, can_steal);
2301 trace_mm_page_alloc_extfrag(page, order, current_order,
2302 start_migratetype, fallback_mt);
2309 * Do the hard work of removing an element from the buddy allocator.
2310 * Call me with the zone->lock already held.
2312 static struct page *__rmqueue(struct zone *zone, unsigned int order,
2318 page = __rmqueue_smallest(zone, order, migratetype);
2319 if (unlikely(!page)) {
2320 if (migratetype == MIGRATE_MOVABLE)
2321 page = __rmqueue_cma_fallback(zone, order);
2323 if (!page && __rmqueue_fallback(zone, order, migratetype))
2327 trace_mm_page_alloc_zone_locked(page, order, migratetype);
2332 * Obtain a specified number of elements from the buddy allocator, all under
2333 * a single hold of the lock, for efficiency. Add them to the supplied list.
2334 * Returns the number of new pages which were placed at *list.
2336 static int rmqueue_bulk(struct zone *zone, unsigned int order,
2337 unsigned long count, struct list_head *list,
2338 int migratetype, bool cold)
2342 spin_lock(&zone->lock);
2343 for (i = 0; i < count; ++i) {
2344 struct page *page = __rmqueue(zone, order, migratetype);
2345 if (unlikely(page == NULL))
2348 if (unlikely(check_pcp_refill(page)))
2352 * Split buddy pages returned by expand() are received here
2353 * in physical page order. The page is added to the callers and
2354 * list and the list head then moves forward. From the callers
2355 * perspective, the linked list is ordered by page number in
2356 * some conditions. This is useful for IO devices that can
2357 * merge IO requests if the physical pages are ordered
2361 list_add(&page->lru, list);
2363 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 */
2614 * Free a 0-order page
2615 * cold == true ? free a cold page : free a hot page
2617 void free_hot_cold_page(struct page *page, bool cold)
2619 struct zone *zone = page_zone(page);
2620 struct per_cpu_pages *pcp;
2621 unsigned long flags;
2622 unsigned long pfn = page_to_pfn(page);
2625 if (!free_pcp_prepare(page))
2628 migratetype = get_pfnblock_migratetype(page, pfn);
2629 set_pcppage_migratetype(page, migratetype);
2630 local_irq_save(flags);
2631 __count_vm_event(PGFREE);
2634 * We only track unmovable, reclaimable and movable on pcp lists.
2635 * Free ISOLATE pages back to the allocator because they are being
2636 * offlined but treat HIGHATOMIC as movable pages so we can get those
2637 * areas back if necessary. Otherwise, we may have to free
2638 * excessively into the page allocator
2640 if (migratetype >= MIGRATE_PCPTYPES) {
2641 if (unlikely(is_migrate_isolate(migratetype))) {
2642 free_one_page(zone, page, pfn, 0, migratetype);
2645 migratetype = MIGRATE_MOVABLE;
2648 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2650 list_add(&page->lru, &pcp->lists[migratetype]);
2652 list_add_tail(&page->lru, &pcp->lists[migratetype]);
2654 if (pcp->count >= pcp->high) {
2655 unsigned long batch = READ_ONCE(pcp->batch);
2656 free_pcppages_bulk(zone, batch, pcp);
2657 pcp->count -= batch;
2661 local_irq_restore(flags);
2665 * Free a list of 0-order pages
2667 void free_hot_cold_page_list(struct list_head *list, bool cold)
2669 struct page *page, *next;
2671 list_for_each_entry_safe(page, next, list, lru) {
2672 trace_mm_page_free_batched(page, cold);
2673 free_hot_cold_page(page, cold);
2678 * split_page takes a non-compound higher-order page, and splits it into
2679 * n (1<<order) sub-pages: page[0..n]
2680 * Each sub-page must be freed individually.
2682 * Note: this is probably too low level an operation for use in drivers.
2683 * Please consult with lkml before using this in your driver.
2685 void split_page(struct page *page, unsigned int order)
2689 VM_BUG_ON_PAGE(PageCompound(page), page);
2690 VM_BUG_ON_PAGE(!page_count(page), page);
2692 for (i = 1; i < (1 << order); i++)
2693 set_page_refcounted(page + i);
2694 split_page_owner(page, order);
2696 EXPORT_SYMBOL_GPL(split_page);
2698 int __isolate_free_page(struct page *page, unsigned int order)
2700 unsigned long watermark;
2704 BUG_ON(!PageBuddy(page));
2706 zone = page_zone(page);
2707 mt = get_pageblock_migratetype(page);
2709 if (!is_migrate_isolate(mt)) {
2711 * Obey watermarks as if the page was being allocated. We can
2712 * emulate a high-order watermark check with a raised order-0
2713 * watermark, because we already know our high-order page
2716 watermark = min_wmark_pages(zone) + (1UL << order);
2717 if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA))
2720 __mod_zone_freepage_state(zone, -(1UL << order), mt);
2723 /* Remove page from free list */
2724 list_del(&page->lru);
2725 zone->free_area[order].nr_free--;
2726 rmv_page_order(page);
2729 * Set the pageblock if the isolated page is at least half of a
2732 if (order >= pageblock_order - 1) {
2733 struct page *endpage = page + (1 << order) - 1;
2734 for (; page < endpage; page += pageblock_nr_pages) {
2735 int mt = get_pageblock_migratetype(page);
2736 if (!is_migrate_isolate(mt) && !is_migrate_cma(mt)
2737 && !is_migrate_highatomic(mt))
2738 set_pageblock_migratetype(page,
2744 return 1UL << order;
2748 * Update NUMA hit/miss statistics
2750 * Must be called with interrupts disabled.
2752 static inline void zone_statistics(struct zone *preferred_zone, struct zone *z)
2755 enum numa_stat_item local_stat = NUMA_LOCAL;
2757 if (z->node != numa_node_id())
2758 local_stat = NUMA_OTHER;
2760 if (z->node == preferred_zone->node)
2761 __inc_numa_state(z, NUMA_HIT);
2763 __inc_numa_state(z, NUMA_MISS);
2764 __inc_numa_state(preferred_zone, NUMA_FOREIGN);
2766 __inc_numa_state(z, local_stat);
2770 /* Remove page from the per-cpu list, caller must protect the list */
2771 static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype,
2772 bool cold, struct per_cpu_pages *pcp,
2773 struct list_head *list)
2778 if (list_empty(list)) {
2779 pcp->count += rmqueue_bulk(zone, 0,
2782 if (unlikely(list_empty(list)))
2787 page = list_last_entry(list, struct page, lru);
2789 page = list_first_entry(list, struct page, lru);
2791 list_del(&page->lru);
2793 } while (check_new_pcp(page));
2798 /* Lock and remove page from the per-cpu list */
2799 static struct page *rmqueue_pcplist(struct zone *preferred_zone,
2800 struct zone *zone, unsigned int order,
2801 gfp_t gfp_flags, int migratetype)
2803 struct per_cpu_pages *pcp;
2804 struct list_head *list;
2805 bool cold = ((gfp_flags & __GFP_COLD) != 0);
2807 unsigned long flags;
2809 local_irq_save(flags);
2810 pcp = &this_cpu_ptr(zone->pageset)->pcp;
2811 list = &pcp->lists[migratetype];
2812 page = __rmqueue_pcplist(zone, migratetype, cold, pcp, list);
2814 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
2815 zone_statistics(preferred_zone, zone);
2817 local_irq_restore(flags);
2822 * Allocate a page from the given zone. Use pcplists for order-0 allocations.
2825 struct page *rmqueue(struct zone *preferred_zone,
2826 struct zone *zone, unsigned int order,
2827 gfp_t gfp_flags, unsigned int alloc_flags,
2830 unsigned long flags;
2833 if (likely(order == 0)) {
2834 page = rmqueue_pcplist(preferred_zone, zone, order,
2835 gfp_flags, migratetype);
2840 * We most definitely don't want callers attempting to
2841 * allocate greater than order-1 page units with __GFP_NOFAIL.
2843 WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1));
2844 spin_lock_irqsave(&zone->lock, flags);
2848 if (alloc_flags & ALLOC_HARDER) {
2849 page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC);
2851 trace_mm_page_alloc_zone_locked(page, order, migratetype);
2854 page = __rmqueue(zone, order, migratetype);
2855 } while (page && check_new_pages(page, order));
2856 spin_unlock(&zone->lock);
2859 __mod_zone_freepage_state(zone, -(1 << order),
2860 get_pcppage_migratetype(page));
2862 __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order);
2863 zone_statistics(preferred_zone, zone);
2864 local_irq_restore(flags);
2867 VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
2871 local_irq_restore(flags);
2875 #ifdef CONFIG_FAIL_PAGE_ALLOC
2878 struct fault_attr attr;
2880 bool ignore_gfp_highmem;
2881 bool ignore_gfp_reclaim;
2883 } fail_page_alloc = {
2884 .attr = FAULT_ATTR_INITIALIZER,
2885 .ignore_gfp_reclaim = true,
2886 .ignore_gfp_highmem = true,
2890 static int __init setup_fail_page_alloc(char *str)
2892 return setup_fault_attr(&fail_page_alloc.attr, str);
2894 __setup("fail_page_alloc=", setup_fail_page_alloc);
2896 static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
2898 if (order < fail_page_alloc.min_order)
2900 if (gfp_mask & __GFP_NOFAIL)
2902 if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
2904 if (fail_page_alloc.ignore_gfp_reclaim &&
2905 (gfp_mask & __GFP_DIRECT_RECLAIM))
2908 return should_fail(&fail_page_alloc.attr, 1 << order);
2911 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
2913 static int __init fail_page_alloc_debugfs(void)
2915 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
2918 dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
2919 &fail_page_alloc.attr);
2921 return PTR_ERR(dir);
2923 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
2924 &fail_page_alloc.ignore_gfp_reclaim))
2926 if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir,
2927 &fail_page_alloc.ignore_gfp_highmem))
2929 if (!debugfs_create_u32("min-order", mode, dir,
2930 &fail_page_alloc.min_order))
2935 debugfs_remove_recursive(dir);
2940 late_initcall(fail_page_alloc_debugfs);
2942 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
2944 #else /* CONFIG_FAIL_PAGE_ALLOC */
2946 static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
2951 #endif /* CONFIG_FAIL_PAGE_ALLOC */
2954 * Return true if free base pages are above 'mark'. For high-order checks it
2955 * will return true of the order-0 watermark is reached and there is at least
2956 * one free page of a suitable size. Checking now avoids taking the zone lock
2957 * to check in the allocation paths if no pages are free.
2959 bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
2960 int classzone_idx, unsigned int alloc_flags,
2965 const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM));
2967 /* free_pages may go negative - that's OK */
2968 free_pages -= (1 << order) - 1;
2970 if (alloc_flags & ALLOC_HIGH)
2974 * If the caller does not have rights to ALLOC_HARDER then subtract
2975 * the high-atomic reserves. This will over-estimate the size of the
2976 * atomic reserve but it avoids a search.
2978 if (likely(!alloc_harder)) {
2979 free_pages -= z->nr_reserved_highatomic;
2982 * OOM victims can try even harder than normal ALLOC_HARDER
2983 * users on the grounds that it's definitely going to be in
2984 * the exit path shortly and free memory. Any allocation it
2985 * makes during the free path will be small and short-lived.
2987 if (alloc_flags & ALLOC_OOM)
2995 /* If allocation can't use CMA areas don't use free CMA pages */
2996 if (!(alloc_flags & ALLOC_CMA))
2997 free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES);
3001 * Check watermarks for an order-0 allocation request. If these
3002 * are not met, then a high-order request also cannot go ahead
3003 * even if a suitable page happened to be free.
3005 if (free_pages <= min + z->lowmem_reserve[classzone_idx])
3008 /* If this is an order-0 request then the watermark is fine */
3012 /* For a high-order request, check at least one suitable page is free */
3013 for (o = order; o < MAX_ORDER; o++) {
3014 struct free_area *area = &z->free_area[o];
3023 for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) {
3024 if (!list_empty(&area->free_list[mt]))
3029 if ((alloc_flags & ALLOC_CMA) &&
3030 !list_empty(&area->free_list[MIGRATE_CMA])) {
3038 bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark,
3039 int classzone_idx, unsigned int alloc_flags)
3041 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3042 zone_page_state(z, NR_FREE_PAGES));
3045 static inline bool zone_watermark_fast(struct zone *z, unsigned int order,
3046 unsigned long mark, int classzone_idx, unsigned int alloc_flags)
3048 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3052 /* If allocation can't use CMA areas don't use free CMA pages */
3053 if (!(alloc_flags & ALLOC_CMA))
3054 cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES);
3058 * Fast check for order-0 only. If this fails then the reserves
3059 * need to be calculated. There is a corner case where the check
3060 * passes but only the high-order atomic reserve are free. If
3061 * the caller is !atomic then it'll uselessly search the free
3062 * list. That corner case is then slower but it is harmless.
3064 if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx])
3067 return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags,
3071 bool zone_watermark_ok_safe(struct zone *z, unsigned int order,
3072 unsigned long mark, int classzone_idx)
3074 long free_pages = zone_page_state(z, NR_FREE_PAGES);
3076 if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark)
3077 free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES);
3079 return __zone_watermark_ok(z, order, mark, classzone_idx, 0,
3084 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3086 return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <=
3089 #else /* CONFIG_NUMA */
3090 static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone)
3094 #endif /* CONFIG_NUMA */
3097 * get_page_from_freelist goes through the zonelist trying to allocate
3100 static struct page *
3101 get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags,
3102 const struct alloc_context *ac)
3104 struct zoneref *z = ac->preferred_zoneref;
3106 struct pglist_data *last_pgdat_dirty_limit = NULL;
3109 * Scan zonelist, looking for a zone with enough free.
3110 * See also __cpuset_node_allowed() comment in kernel/cpuset.c.
3112 for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx,
3117 if (cpusets_enabled() &&
3118 (alloc_flags & ALLOC_CPUSET) &&
3119 !__cpuset_zone_allowed(zone, gfp_mask))
3122 * When allocating a page cache page for writing, we
3123 * want to get it from a node that is within its dirty
3124 * limit, such that no single node holds more than its
3125 * proportional share of globally allowed dirty pages.
3126 * The dirty limits take into account the node's
3127 * lowmem reserves and high watermark so that kswapd
3128 * should be able to balance it without having to
3129 * write pages from its LRU list.
3131 * XXX: For now, allow allocations to potentially
3132 * exceed the per-node dirty limit in the slowpath
3133 * (spread_dirty_pages unset) before going into reclaim,
3134 * which is important when on a NUMA setup the allowed
3135 * nodes are together not big enough to reach the
3136 * global limit. The proper fix for these situations
3137 * will require awareness of nodes in the
3138 * dirty-throttling and the flusher threads.
3140 if (ac->spread_dirty_pages) {
3141 if (last_pgdat_dirty_limit == zone->zone_pgdat)
3144 if (!node_dirty_ok(zone->zone_pgdat)) {
3145 last_pgdat_dirty_limit = zone->zone_pgdat;
3150 mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK];
3151 if (!zone_watermark_fast(zone, order, mark,
3152 ac_classzone_idx(ac), alloc_flags)) {
3155 /* Checked here to keep the fast path fast */
3156 BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK);
3157 if (alloc_flags & ALLOC_NO_WATERMARKS)
3160 if (node_reclaim_mode == 0 ||
3161 !zone_allows_reclaim(ac->preferred_zoneref->zone, zone))
3164 ret = node_reclaim(zone->zone_pgdat, gfp_mask, order);
3166 case NODE_RECLAIM_NOSCAN:
3169 case NODE_RECLAIM_FULL:
3170 /* scanned but unreclaimable */
3173 /* did we reclaim enough */
3174 if (zone_watermark_ok(zone, order, mark,
3175 ac_classzone_idx(ac), alloc_flags))
3183 page = rmqueue(ac->preferred_zoneref->zone, zone, order,
3184 gfp_mask, alloc_flags, ac->migratetype);
3186 prep_new_page(page, order, gfp_mask, alloc_flags);
3189 * If this is a high-order atomic allocation then check
3190 * if the pageblock should be reserved for the future
3192 if (unlikely(order && (alloc_flags & ALLOC_HARDER)))
3193 reserve_highatomic_pageblock(page, zone, order);
3203 * Large machines with many possible nodes should not always dump per-node
3204 * meminfo in irq context.
3206 static inline bool should_suppress_show_mem(void)
3211 ret = in_interrupt();
3216 static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask)
3218 unsigned int filter = SHOW_MEM_FILTER_NODES;
3219 static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1);
3221 if (should_suppress_show_mem() || !__ratelimit(&show_mem_rs))
3225 * This documents exceptions given to allocations in certain
3226 * contexts that are allowed to allocate outside current's set
3229 if (!(gfp_mask & __GFP_NOMEMALLOC))
3230 if (tsk_is_oom_victim(current) ||
3231 (current->flags & (PF_MEMALLOC | PF_EXITING)))
3232 filter &= ~SHOW_MEM_FILTER_NODES;
3233 if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM))
3234 filter &= ~SHOW_MEM_FILTER_NODES;
3236 show_mem(filter, nodemask);
3239 void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...)
3241 struct va_format vaf;
3243 static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL,
3244 DEFAULT_RATELIMIT_BURST);
3246 if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs))
3249 pr_warn("%s: ", current->comm);
3251 va_start(args, fmt);
3254 pr_cont("%pV", &vaf);
3257 pr_cont(", mode:%#x(%pGg), nodemask=", gfp_mask, &gfp_mask);
3259 pr_cont("%*pbl\n", nodemask_pr_args(nodemask));
3261 pr_cont("(null)\n");
3263 cpuset_print_current_mems_allowed();
3266 warn_alloc_show_mem(gfp_mask, nodemask);
3269 static inline struct page *
3270 __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order,
3271 unsigned int alloc_flags,
3272 const struct alloc_context *ac)
3276 page = get_page_from_freelist(gfp_mask, order,
3277 alloc_flags|ALLOC_CPUSET, ac);
3279 * fallback to ignore cpuset restriction if our nodes
3283 page = get_page_from_freelist(gfp_mask, order,
3289 static inline struct page *
3290 __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order,
3291 const struct alloc_context *ac, unsigned long *did_some_progress)
3293 struct oom_control oc = {
3294 .zonelist = ac->zonelist,
3295 .nodemask = ac->nodemask,
3297 .gfp_mask = gfp_mask,
3302 *did_some_progress = 0;
3305 * Acquire the oom lock. If that fails, somebody else is
3306 * making progress for us.
3308 if (!mutex_trylock(&oom_lock)) {
3309 *did_some_progress = 1;
3310 schedule_timeout_uninterruptible(1);
3315 * Go through the zonelist yet one more time, keep very high watermark
3316 * here, this is only to catch a parallel oom killing, we must fail if
3317 * we're still under heavy pressure. But make sure that this reclaim
3318 * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY
3319 * allocation which will never fail due to oom_lock already held.
3321 page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) &
3322 ~__GFP_DIRECT_RECLAIM, order,
3323 ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac);
3327 /* Coredumps can quickly deplete all memory reserves */
3328 if (current->flags & PF_DUMPCORE)
3330 /* The OOM killer will not help higher order allocs */
3331 if (order > PAGE_ALLOC_COSTLY_ORDER)
3334 * We have already exhausted all our reclaim opportunities without any
3335 * success so it is time to admit defeat. We will skip the OOM killer
3336 * because it is very likely that the caller has a more reasonable
3337 * fallback than shooting a random task.
3339 if (gfp_mask & __GFP_RETRY_MAYFAIL)
3341 /* The OOM killer does not needlessly kill tasks for lowmem */
3342 if (ac->high_zoneidx < ZONE_NORMAL)
3344 if (pm_suspended_storage())
3347 * XXX: GFP_NOFS allocations should rather fail than rely on
3348 * other request to make a forward progress.
3349 * We are in an unfortunate situation where out_of_memory cannot
3350 * do much for this context but let's try it to at least get
3351 * access to memory reserved if the current task is killed (see
3352 * out_of_memory). Once filesystems are ready to handle allocation
3353 * failures more gracefully we should just bail out here.
3356 /* The OOM killer may not free memory on a specific node */
3357 if (gfp_mask & __GFP_THISNODE)
3360 /* Exhausted what can be done so it's blamo time */
3361 if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) {