| /* |
| * linux/mm/page_alloc.c |
| * |
| * Manages the free list, the system allocates free pages here. |
| * Note that kmalloc() lives in slab.c |
| * |
| * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds |
| * Swap reorganised 29.12.95, Stephen Tweedie |
| * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999 |
| * Reshaped it to be a zoned allocator, Ingo Molnar, Red Hat, 1999 |
| * Discontiguous memory support, Kanoj Sarcar, SGI, Nov 1999 |
| * Zone balancing, Kanoj Sarcar, SGI, Jan 2000 |
| * Per cpu hot/cold page lists, bulk allocation, Martin J. Bligh, Sept 2002 |
| * (lots of bits borrowed from Ingo Molnar & Andrew Morton) |
| */ |
| |
| #include <linux/stddef.h> |
| #include <linux/mm.h> |
| #include <linux/swap.h> |
| #include <linux/interrupt.h> |
| #include <linux/pagemap.h> |
| #include <linux/jiffies.h> |
| #include <linux/bootmem.h> |
| #include <linux/memblock.h> |
| #include <linux/compiler.h> |
| #include <linux/kernel.h> |
| #include <linux/kmemcheck.h> |
| #include <linux/kasan.h> |
| #include <linux/module.h> |
| #include <linux/suspend.h> |
| #include <linux/pagevec.h> |
| #include <linux/blkdev.h> |
| #include <linux/slab.h> |
| #include <linux/ratelimit.h> |
| #include <linux/oom.h> |
| #include <linux/notifier.h> |
| #include <linux/topology.h> |
| #include <linux/sysctl.h> |
| #include <linux/cpu.h> |
| #include <linux/cpuset.h> |
| #include <linux/memory_hotplug.h> |
| #include <linux/nodemask.h> |
| #include <linux/vmalloc.h> |
| #include <linux/vmstat.h> |
| #include <linux/mempolicy.h> |
| #include <linux/memremap.h> |
| #include <linux/stop_machine.h> |
| #include <linux/sort.h> |
| #include <linux/pfn.h> |
| #include <linux/backing-dev.h> |
| #include <linux/fault-inject.h> |
| #include <linux/page-isolation.h> |
| #include <linux/page_ext.h> |
| #include <linux/debugobjects.h> |
| #include <linux/kmemleak.h> |
| #include <linux/compaction.h> |
| #include <trace/events/kmem.h> |
| #include <trace/events/oom.h> |
| #include <linux/prefetch.h> |
| #include <linux/mm_inline.h> |
| #include <linux/migrate.h> |
| #include <linux/hugetlb.h> |
| #include <linux/sched/rt.h> |
| #include <linux/sched/mm.h> |
| #include <linux/page_owner.h> |
| #include <linux/kthread.h> |
| #include <linux/memcontrol.h> |
| #include <linux/ftrace.h> |
| #include <linux/lockdep.h> |
| #include <linux/nmi.h> |
| |
| #include <asm/sections.h> |
| #include <asm/tlbflush.h> |
| #include <asm/div64.h> |
| #include "internal.h" |
| |
| /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ |
| static DEFINE_MUTEX(pcp_batch_high_lock); |
| #define MIN_PERCPU_PAGELIST_FRACTION (8) |
| |
| #ifdef CONFIG_USE_PERCPU_NUMA_NODE_ID |
| DEFINE_PER_CPU(int, numa_node); |
| EXPORT_PER_CPU_SYMBOL(numa_node); |
| #endif |
| |
| #ifdef CONFIG_HAVE_MEMORYLESS_NODES |
| /* |
| * N.B., Do NOT reference the '_numa_mem_' per cpu variable directly. |
| * It will not be defined when CONFIG_HAVE_MEMORYLESS_NODES is not defined. |
| * Use the accessor functions set_numa_mem(), numa_mem_id() and cpu_to_mem() |
| * defined in <linux/topology.h>. |
| */ |
| DEFINE_PER_CPU(int, _numa_mem_); /* Kernel "local memory" node */ |
| EXPORT_PER_CPU_SYMBOL(_numa_mem_); |
| int _node_numa_mem_[MAX_NUMNODES]; |
| #endif |
| |
| /* work_structs for global per-cpu drains */ |
| DEFINE_MUTEX(pcpu_drain_mutex); |
| DEFINE_PER_CPU(struct work_struct, pcpu_drain); |
| |
| #ifdef CONFIG_GCC_PLUGIN_LATENT_ENTROPY |
| volatile unsigned long latent_entropy __latent_entropy; |
| EXPORT_SYMBOL(latent_entropy); |
| #endif |
| |
| /* |
| * Array of node states. |
| */ |
| nodemask_t node_states[NR_NODE_STATES] __read_mostly = { |
| [N_POSSIBLE] = NODE_MASK_ALL, |
| [N_ONLINE] = { { [0] = 1UL } }, |
| #ifndef CONFIG_NUMA |
| [N_NORMAL_MEMORY] = { { [0] = 1UL } }, |
| #ifdef CONFIG_HIGHMEM |
| [N_HIGH_MEMORY] = { { [0] = 1UL } }, |
| #endif |
| [N_MEMORY] = { { [0] = 1UL } }, |
| [N_CPU] = { { [0] = 1UL } }, |
| #endif /* NUMA */ |
| }; |
| EXPORT_SYMBOL(node_states); |
| |
| /* Protect totalram_pages and zone->managed_pages */ |
| static DEFINE_SPINLOCK(managed_page_count_lock); |
| |
| unsigned long totalram_pages __read_mostly; |
| unsigned long totalreserve_pages __read_mostly; |
| unsigned long totalcma_pages __read_mostly; |
| |
| int percpu_pagelist_fraction; |
| gfp_t gfp_allowed_mask __read_mostly = GFP_BOOT_MASK; |
| |
| /* |
| * A cached value of the page's pageblock's migratetype, used when the page is |
| * put on a pcplist. Used to avoid the pageblock migratetype lookup when |
| * freeing from pcplists in most cases, at the cost of possibly becoming stale. |
| * Also the migratetype set in the page does not necessarily match the pcplist |
| * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any |
| * other index - this ensures that it will be put on the correct CMA freelist. |
| */ |
| static inline int get_pcppage_migratetype(struct page *page) |
| { |
| return page->index; |
| } |
| |
| static inline void set_pcppage_migratetype(struct page *page, int migratetype) |
| { |
| page->index = migratetype; |
| } |
| |
| #ifdef CONFIG_PM_SLEEP |
| /* |
| * The following functions are used by the suspend/hibernate code to temporarily |
| * change gfp_allowed_mask in order to avoid using I/O during memory allocations |
| * while devices are suspended. To avoid races with the suspend/hibernate code, |
| * they should always be called with pm_mutex held (gfp_allowed_mask also should |
| * only be modified with pm_mutex held, unless the suspend/hibernate code is |
| * guaranteed not to run in parallel with that modification). |
| */ |
| |
| static gfp_t saved_gfp_mask; |
| |
| void pm_restore_gfp_mask(void) |
| { |
| WARN_ON(!mutex_is_locked(&pm_mutex)); |
| if (saved_gfp_mask) { |
| gfp_allowed_mask = saved_gfp_mask; |
| saved_gfp_mask = 0; |
| } |
| } |
| |
| void pm_restrict_gfp_mask(void) |
| { |
| WARN_ON(!mutex_is_locked(&pm_mutex)); |
| WARN_ON(saved_gfp_mask); |
| saved_gfp_mask = gfp_allowed_mask; |
| gfp_allowed_mask &= ~(__GFP_IO | __GFP_FS); |
| } |
| |
| bool pm_suspended_storage(void) |
| { |
| if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) |
| return false; |
| return true; |
| } |
| #endif /* CONFIG_PM_SLEEP */ |
| |
| #ifdef CONFIG_HUGETLB_PAGE_SIZE_VARIABLE |
| unsigned int pageblock_order __read_mostly; |
| #endif |
| |
| static void __free_pages_ok(struct page *page, unsigned int order); |
| |
| /* |
| * results with 256, 32 in the lowmem_reserve sysctl: |
| * 1G machine -> (16M dma, 800M-16M normal, 1G-800M high) |
| * 1G machine -> (16M dma, 784M normal, 224M high) |
| * NORMAL allocation will leave 784M/256 of ram reserved in the ZONE_DMA |
| * HIGHMEM allocation will leave 224M/32 of ram reserved in ZONE_NORMAL |
| * HIGHMEM allocation will leave (224M+784M)/256 of ram reserved in ZONE_DMA |
| * |
| * TBD: should special case ZONE_DMA32 machines here - in those we normally |
| * don't need any ZONE_NORMAL reservation |
| */ |
| int sysctl_lowmem_reserve_ratio[MAX_NR_ZONES-1] = { |
| #ifdef CONFIG_ZONE_DMA |
| 256, |
| #endif |
| #ifdef CONFIG_ZONE_DMA32 |
| 256, |
| #endif |
| #ifdef CONFIG_HIGHMEM |
| 32, |
| #endif |
| 32, |
| }; |
| |
| EXPORT_SYMBOL(totalram_pages); |
| |
| static char * const zone_names[MAX_NR_ZONES] = { |
| #ifdef CONFIG_ZONE_DMA |
| "DMA", |
| #endif |
| #ifdef CONFIG_ZONE_DMA32 |
| "DMA32", |
| #endif |
| "Normal", |
| #ifdef CONFIG_HIGHMEM |
| "HighMem", |
| #endif |
| "Movable", |
| #ifdef CONFIG_ZONE_DEVICE |
| "Device", |
| #endif |
| }; |
| |
| char * const migratetype_names[MIGRATE_TYPES] = { |
| "Unmovable", |
| "Movable", |
| "Reclaimable", |
| "HighAtomic", |
| #ifdef CONFIG_CMA |
| "CMA", |
| #endif |
| #ifdef CONFIG_MEMORY_ISOLATION |
| "Isolate", |
| #endif |
| }; |
| |
| compound_page_dtor * const compound_page_dtors[] = { |
| NULL, |
| free_compound_page, |
| #ifdef CONFIG_HUGETLB_PAGE |
| free_huge_page, |
| #endif |
| #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
| free_transhuge_page, |
| #endif |
| }; |
| |
| int min_free_kbytes = 1024; |
| int user_min_free_kbytes = -1; |
| int watermark_scale_factor = 10; |
| |
| static unsigned long __meminitdata nr_kernel_pages; |
| static unsigned long __meminitdata nr_all_pages; |
| static unsigned long __meminitdata dma_reserve; |
| |
| #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
| static unsigned long __meminitdata arch_zone_lowest_possible_pfn[MAX_NR_ZONES]; |
| static unsigned long __meminitdata arch_zone_highest_possible_pfn[MAX_NR_ZONES]; |
| static unsigned long __initdata required_kernelcore; |
| static unsigned long __initdata required_movablecore; |
| static unsigned long __meminitdata zone_movable_pfn[MAX_NUMNODES]; |
| static bool mirrored_kernelcore; |
| |
| /* movable_zone is the "real" zone pages in ZONE_MOVABLE are taken from */ |
| int movable_zone; |
| EXPORT_SYMBOL(movable_zone); |
| #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
| |
| #if MAX_NUMNODES > 1 |
| int nr_node_ids __read_mostly = MAX_NUMNODES; |
| int nr_online_nodes __read_mostly = 1; |
| EXPORT_SYMBOL(nr_node_ids); |
| EXPORT_SYMBOL(nr_online_nodes); |
| #endif |
| |
| int page_group_by_mobility_disabled __read_mostly; |
| |
| #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| static inline void reset_deferred_meminit(pg_data_t *pgdat) |
| { |
| unsigned long max_initialise; |
| unsigned long reserved_lowmem; |
| |
| /* |
| * Initialise at least 2G of a node but also take into account that |
| * two large system hashes that can take up 1GB for 0.25TB/node. |
| */ |
| max_initialise = max(2UL << (30 - PAGE_SHIFT), |
| (pgdat->node_spanned_pages >> 8)); |
| |
| /* |
| * Compensate the all the memblock reservations (e.g. crash kernel) |
| * from the initial estimation to make sure we will initialize enough |
| * memory to boot. |
| */ |
| reserved_lowmem = memblock_reserved_memory_within(pgdat->node_start_pfn, |
| pgdat->node_start_pfn + max_initialise); |
| max_initialise += reserved_lowmem; |
| |
| pgdat->static_init_size = min(max_initialise, pgdat->node_spanned_pages); |
| pgdat->first_deferred_pfn = ULONG_MAX; |
| } |
| |
| /* Returns true if the struct page for the pfn is uninitialised */ |
| static inline bool __meminit early_page_uninitialised(unsigned long pfn) |
| { |
| int nid = early_pfn_to_nid(pfn); |
| |
| if (node_online(nid) && pfn >= NODE_DATA(nid)->first_deferred_pfn) |
| return true; |
| |
| return false; |
| } |
| |
| /* |
| * Returns false when the remaining initialisation should be deferred until |
| * later in the boot cycle when it can be parallelised. |
| */ |
| static inline bool update_defer_init(pg_data_t *pgdat, |
| unsigned long pfn, unsigned long zone_end, |
| unsigned long *nr_initialised) |
| { |
| /* Always populate low zones for address-contrained allocations */ |
| if (zone_end < pgdat_end_pfn(pgdat)) |
| return true; |
| (*nr_initialised)++; |
| if ((*nr_initialised > pgdat->static_init_size) && |
| (pfn & (PAGES_PER_SECTION - 1)) == 0) { |
| pgdat->first_deferred_pfn = pfn; |
| return false; |
| } |
| |
| return true; |
| } |
| #else |
| static inline void reset_deferred_meminit(pg_data_t *pgdat) |
| { |
| } |
| |
| static inline bool early_page_uninitialised(unsigned long pfn) |
| { |
| return false; |
| } |
| |
| static inline bool update_defer_init(pg_data_t *pgdat, |
| unsigned long pfn, unsigned long zone_end, |
| unsigned long *nr_initialised) |
| { |
| return true; |
| } |
| #endif |
| |
| /* Return a pointer to the bitmap storing bits affecting a block of pages */ |
| static inline unsigned long *get_pageblock_bitmap(struct page *page, |
| unsigned long pfn) |
| { |
| #ifdef CONFIG_SPARSEMEM |
| return __pfn_to_section(pfn)->pageblock_flags; |
| #else |
| return page_zone(page)->pageblock_flags; |
| #endif /* CONFIG_SPARSEMEM */ |
| } |
| |
| static inline int pfn_to_bitidx(struct page *page, unsigned long pfn) |
| { |
| #ifdef CONFIG_SPARSEMEM |
| pfn &= (PAGES_PER_SECTION-1); |
| return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
| #else |
| pfn = pfn - round_down(page_zone(page)->zone_start_pfn, pageblock_nr_pages); |
| return (pfn >> pageblock_order) * NR_PAGEBLOCK_BITS; |
| #endif /* CONFIG_SPARSEMEM */ |
| } |
| |
| /** |
| * get_pfnblock_flags_mask - Return the requested group of flags for the pageblock_nr_pages block of pages |
| * @page: The page within the block of interest |
| * @pfn: The target page frame number |
| * @end_bitidx: The last bit of interest to retrieve |
| * @mask: mask of bits that the caller is interested in |
| * |
| * Return: pageblock_bits flags |
| */ |
| static __always_inline unsigned long __get_pfnblock_flags_mask(struct page *page, |
| unsigned long pfn, |
| unsigned long end_bitidx, |
| unsigned long mask) |
| { |
| unsigned long *bitmap; |
| unsigned long bitidx, word_bitidx; |
| unsigned long word; |
| |
| bitmap = get_pageblock_bitmap(page, pfn); |
| bitidx = pfn_to_bitidx(page, pfn); |
| word_bitidx = bitidx / BITS_PER_LONG; |
| bitidx &= (BITS_PER_LONG-1); |
| |
| word = bitmap[word_bitidx]; |
| bitidx += end_bitidx; |
| return (word >> (BITS_PER_LONG - bitidx - 1)) & mask; |
| } |
| |
| unsigned long get_pfnblock_flags_mask(struct page *page, unsigned long pfn, |
| unsigned long end_bitidx, |
| unsigned long mask) |
| { |
| return __get_pfnblock_flags_mask(page, pfn, end_bitidx, mask); |
| } |
| |
| static __always_inline int get_pfnblock_migratetype(struct page *page, unsigned long pfn) |
| { |
| return __get_pfnblock_flags_mask(page, pfn, PB_migrate_end, MIGRATETYPE_MASK); |
| } |
| |
| /** |
| * set_pfnblock_flags_mask - Set the requested group of flags for a pageblock_nr_pages block of pages |
| * @page: The page within the block of interest |
| * @flags: The flags to set |
| * @pfn: The target page frame number |
| * @end_bitidx: The last bit of interest |
| * @mask: mask of bits that the caller is interested in |
| */ |
| void set_pfnblock_flags_mask(struct page *page, unsigned long flags, |
| unsigned long pfn, |
| unsigned long end_bitidx, |
| unsigned long mask) |
| { |
| unsigned long *bitmap; |
| unsigned long bitidx, word_bitidx; |
| unsigned long old_word, word; |
| |
| BUILD_BUG_ON(NR_PAGEBLOCK_BITS != 4); |
| |
| bitmap = get_pageblock_bitmap(page, pfn); |
| bitidx = pfn_to_bitidx(page, pfn); |
| word_bitidx = bitidx / BITS_PER_LONG; |
| bitidx &= (BITS_PER_LONG-1); |
| |
| VM_BUG_ON_PAGE(!zone_spans_pfn(page_zone(page), pfn), page); |
| |
| bitidx += end_bitidx; |
| mask <<= (BITS_PER_LONG - bitidx - 1); |
| flags <<= (BITS_PER_LONG - bitidx - 1); |
| |
| word = READ_ONCE(bitmap[word_bitidx]); |
| for (;;) { |
| old_word = cmpxchg(&bitmap[word_bitidx], word, (word & ~mask) | flags); |
| if (word == old_word) |
| break; |
| word = old_word; |
| } |
| } |
| |
| void set_pageblock_migratetype(struct page *page, int migratetype) |
| { |
| if (unlikely(page_group_by_mobility_disabled && |
| migratetype < MIGRATE_PCPTYPES)) |
| migratetype = MIGRATE_UNMOVABLE; |
| |
| set_pageblock_flags_group(page, (unsigned long)migratetype, |
| PB_migrate, PB_migrate_end); |
| } |
| |
| #ifdef CONFIG_DEBUG_VM |
| static int page_outside_zone_boundaries(struct zone *zone, struct page *page) |
| { |
| int ret = 0; |
| unsigned seq; |
| unsigned long pfn = page_to_pfn(page); |
| unsigned long sp, start_pfn; |
| |
| do { |
| seq = zone_span_seqbegin(zone); |
| start_pfn = zone->zone_start_pfn; |
| sp = zone->spanned_pages; |
| if (!zone_spans_pfn(zone, pfn)) |
| ret = 1; |
| } while (zone_span_seqretry(zone, seq)); |
| |
| if (ret) |
| pr_err("page 0x%lx outside node %d zone %s [ 0x%lx - 0x%lx ]\n", |
| pfn, zone_to_nid(zone), zone->name, |
| start_pfn, start_pfn + sp); |
| |
| return ret; |
| } |
| |
| static int page_is_consistent(struct zone *zone, struct page *page) |
| { |
| if (!pfn_valid_within(page_to_pfn(page))) |
| return 0; |
| if (zone != page_zone(page)) |
| return 0; |
| |
| return 1; |
| } |
| /* |
| * Temporary debugging check for pages not lying within a given zone. |
| */ |
| static int __maybe_unused bad_range(struct zone *zone, struct page *page) |
| { |
| if (page_outside_zone_boundaries(zone, page)) |
| return 1; |
| if (!page_is_consistent(zone, page)) |
| return 1; |
| |
| return 0; |
| } |
| #else |
| static inline int __maybe_unused bad_range(struct zone *zone, struct page *page) |
| { |
| return 0; |
| } |
| #endif |
| |
| static void bad_page(struct page *page, const char *reason, |
| unsigned long bad_flags) |
| { |
| static unsigned long resume; |
| static unsigned long nr_shown; |
| static unsigned long nr_unshown; |
| |
| /* |
| * Allow a burst of 60 reports, then keep quiet for that minute; |
| * or allow a steady drip of one report per second. |
| */ |
| if (nr_shown == 60) { |
| if (time_before(jiffies, resume)) { |
| nr_unshown++; |
| goto out; |
| } |
| if (nr_unshown) { |
| pr_alert( |
| "BUG: Bad page state: %lu messages suppressed\n", |
| nr_unshown); |
| nr_unshown = 0; |
| } |
| nr_shown = 0; |
| } |
| if (nr_shown++ == 0) |
| resume = jiffies + 60 * HZ; |
| |
| pr_alert("BUG: Bad page state in process %s pfn:%05lx\n", |
| current->comm, page_to_pfn(page)); |
| __dump_page(page, reason); |
| bad_flags &= page->flags; |
| if (bad_flags) |
| pr_alert("bad because of flags: %#lx(%pGp)\n", |
| bad_flags, &bad_flags); |
| dump_page_owner(page); |
| |
| print_modules(); |
| dump_stack(); |
| out: |
| /* Leave bad fields for debug, except PageBuddy could make trouble */ |
| page_mapcount_reset(page); /* remove PageBuddy */ |
| add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
| } |
| |
| /* |
| * Higher-order pages are called "compound pages". They are structured thusly: |
| * |
| * The first PAGE_SIZE page is called the "head page" and have PG_head set. |
| * |
| * The remaining PAGE_SIZE pages are called "tail pages". PageTail() is encoded |
| * in bit 0 of page->compound_head. The rest of bits is pointer to head page. |
| * |
| * The first tail page's ->compound_dtor holds the offset in array of compound |
| * page destructors. See compound_page_dtors. |
| * |
| * The first tail page's ->compound_order holds the order of allocation. |
| * This usage means that zero-order pages may not be compound. |
| */ |
| |
| void free_compound_page(struct page *page) |
| { |
| __free_pages_ok(page, compound_order(page)); |
| } |
| |
| void prep_compound_page(struct page *page, unsigned int order) |
| { |
| int i; |
| int nr_pages = 1 << order; |
| |
| set_compound_page_dtor(page, COMPOUND_PAGE_DTOR); |
| set_compound_order(page, order); |
| __SetPageHead(page); |
| for (i = 1; i < nr_pages; i++) { |
| struct page *p = page + i; |
| set_page_count(p, 0); |
| p->mapping = TAIL_MAPPING; |
| set_compound_head(p, page); |
| } |
| atomic_set(compound_mapcount_ptr(page), -1); |
| } |
| |
| #ifdef CONFIG_DEBUG_PAGEALLOC |
| unsigned int _debug_guardpage_minorder; |
| bool _debug_pagealloc_enabled __read_mostly |
| = IS_ENABLED(CONFIG_DEBUG_PAGEALLOC_ENABLE_DEFAULT); |
| EXPORT_SYMBOL(_debug_pagealloc_enabled); |
| bool _debug_guardpage_enabled __read_mostly; |
| |
| static int __init early_debug_pagealloc(char *buf) |
| { |
| if (!buf) |
| return -EINVAL; |
| return kstrtobool(buf, &_debug_pagealloc_enabled); |
| } |
| early_param("debug_pagealloc", early_debug_pagealloc); |
| |
| static bool need_debug_guardpage(void) |
| { |
| /* If we don't use debug_pagealloc, we don't need guard page */ |
| if (!debug_pagealloc_enabled()) |
| return false; |
| |
| if (!debug_guardpage_minorder()) |
| return false; |
| |
| return true; |
| } |
| |
| static void init_debug_guardpage(void) |
| { |
| if (!debug_pagealloc_enabled()) |
| return; |
| |
| if (!debug_guardpage_minorder()) |
| return; |
| |
| _debug_guardpage_enabled = true; |
| } |
| |
| struct page_ext_operations debug_guardpage_ops = { |
| .need = need_debug_guardpage, |
| .init = init_debug_guardpage, |
| }; |
| |
| static int __init debug_guardpage_minorder_setup(char *buf) |
| { |
| unsigned long res; |
| |
| if (kstrtoul(buf, 10, &res) < 0 || res > MAX_ORDER / 2) { |
| pr_err("Bad debug_guardpage_minorder value\n"); |
| return 0; |
| } |
| _debug_guardpage_minorder = res; |
| pr_info("Setting debug_guardpage_minorder to %lu\n", res); |
| return 0; |
| } |
| early_param("debug_guardpage_minorder", debug_guardpage_minorder_setup); |
| |
| static inline bool set_page_guard(struct zone *zone, struct page *page, |
| unsigned int order, int migratetype) |
| { |
| struct page_ext *page_ext; |
| |
| if (!debug_guardpage_enabled()) |
| return false; |
| |
| if (order >= debug_guardpage_minorder()) |
| return false; |
| |
| page_ext = lookup_page_ext(page); |
| if (unlikely(!page_ext)) |
| return false; |
| |
| __set_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags); |
| |
| INIT_LIST_HEAD(&page->lru); |
| set_page_private(page, order); |
| /* Guard pages are not available for any usage */ |
| __mod_zone_freepage_state(zone, -(1 << order), migratetype); |
| |
| return true; |
| } |
| |
| static inline void clear_page_guard(struct zone *zone, struct page *page, |
| unsigned int order, int migratetype) |
| { |
| struct page_ext *page_ext; |
| |
| if (!debug_guardpage_enabled()) |
| return; |
| |
| page_ext = lookup_page_ext(page); |
| if (unlikely(!page_ext)) |
| return; |
| |
| __clear_bit(PAGE_EXT_DEBUG_GUARD, &page_ext->flags); |
| |
| set_page_private(page, 0); |
| if (!is_migrate_isolate(migratetype)) |
| __mod_zone_freepage_state(zone, (1 << order), migratetype); |
| } |
| #else |
| struct page_ext_operations debug_guardpage_ops; |
| static inline bool set_page_guard(struct zone *zone, struct page *page, |
| unsigned int order, int migratetype) { return false; } |
| static inline void clear_page_guard(struct zone *zone, struct page *page, |
| unsigned int order, int migratetype) {} |
| #endif |
| |
| static inline void set_page_order(struct page *page, unsigned int order) |
| { |
| set_page_private(page, order); |
| __SetPageBuddy(page); |
| } |
| |
| static inline void rmv_page_order(struct page *page) |
| { |
| __ClearPageBuddy(page); |
| set_page_private(page, 0); |
| } |
| |
| /* |
| * This function checks whether a page is free && is the buddy |
| * we can do coalesce a page and its buddy if |
| * (a) the buddy is not in a hole (check before calling!) && |
| * (b) the buddy is in the buddy system && |
| * (c) a page and its buddy have the same order && |
| * (d) a page and its buddy are in the same zone. |
| * |
| * For recording whether a page is in the buddy system, we set ->_mapcount |
| * PAGE_BUDDY_MAPCOUNT_VALUE. |
| * Setting, clearing, and testing _mapcount PAGE_BUDDY_MAPCOUNT_VALUE is |
| * serialized by zone->lock. |
| * |
| * For recording page's order, we use page_private(page). |
| */ |
| static inline int page_is_buddy(struct page *page, struct page *buddy, |
| unsigned int order) |
| { |
| if (page_is_guard(buddy) && page_order(buddy) == order) { |
| if (page_zone_id(page) != page_zone_id(buddy)) |
| return 0; |
| |
| VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); |
| |
| return 1; |
| } |
| |
| if (PageBuddy(buddy) && page_order(buddy) == order) { |
| /* |
| * zone check is done late to avoid uselessly |
| * calculating zone/node ids for pages that could |
| * never merge. |
| */ |
| if (page_zone_id(page) != page_zone_id(buddy)) |
| return 0; |
| |
| VM_BUG_ON_PAGE(page_count(buddy) != 0, buddy); |
| |
| return 1; |
| } |
| return 0; |
| } |
| |
| /* |
| * Freeing function for a buddy system allocator. |
| * |
| * The concept of a buddy system is to maintain direct-mapped table |
| * (containing bit values) for memory blocks of various "orders". |
| * The bottom level table contains the map for the smallest allocatable |
| * units of memory (here, pages), and each level above it describes |
| * pairs of units from the levels below, hence, "buddies". |
| * At a high level, all that happens here is marking the table entry |
| * at the bottom level available, and propagating the changes upward |
| * as necessary, plus some accounting needed to play nicely with other |
| * parts of the VM system. |
| * At each level, we keep a list of pages, which are heads of continuous |
| * free pages of length of (1 << order) and marked with _mapcount |
| * PAGE_BUDDY_MAPCOUNT_VALUE. Page's order is recorded in page_private(page) |
| * field. |
| * So when we are allocating or freeing one, we can derive the state of the |
| * other. That is, if we allocate a small block, and both were |
| * free, the remainder of the region must be split into blocks. |
| * If a block is freed, and its buddy is also free, then this |
| * triggers coalescing into a block of larger size. |
| * |
| * -- nyc |
| */ |
| |
| static inline void __free_one_page(struct page *page, |
| unsigned long pfn, |
| struct zone *zone, unsigned int order, |
| int migratetype) |
| { |
| unsigned long combined_pfn; |
| unsigned long uninitialized_var(buddy_pfn); |
| struct page *buddy; |
| unsigned int max_order; |
| |
| max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1); |
| |
| VM_BUG_ON(!zone_is_initialized(zone)); |
| VM_BUG_ON_PAGE(page->flags & PAGE_FLAGS_CHECK_AT_PREP, page); |
| |
| VM_BUG_ON(migratetype == -1); |
| if (likely(!is_migrate_isolate(migratetype))) |
| __mod_zone_freepage_state(zone, 1 << order, migratetype); |
| |
| VM_BUG_ON_PAGE(pfn & ((1 << order) - 1), page); |
| VM_BUG_ON_PAGE(bad_range(zone, page), page); |
| |
| continue_merging: |
| while (order < max_order - 1) { |
| buddy_pfn = __find_buddy_pfn(pfn, order); |
| buddy = page + (buddy_pfn - pfn); |
| |
| if (!pfn_valid_within(buddy_pfn)) |
| goto done_merging; |
| if (!page_is_buddy(page, buddy, order)) |
| goto done_merging; |
| /* |
| * Our buddy is free or it is CONFIG_DEBUG_PAGEALLOC guard page, |
| * merge with it and move up one order. |
| */ |
| if (page_is_guard(buddy)) { |
| clear_page_guard(zone, buddy, order, migratetype); |
| } else { |
| list_del(&buddy->lru); |
| zone->free_area[order].nr_free--; |
| rmv_page_order(buddy); |
| } |
| combined_pfn = buddy_pfn & pfn; |
| page = page + (combined_pfn - pfn); |
| pfn = combined_pfn; |
| order++; |
| } |
| if (max_order < MAX_ORDER) { |
| /* If we are here, it means order is >= pageblock_order. |
| * We want to prevent merge between freepages on isolate |
| * pageblock and normal pageblock. Without this, pageblock |
| * isolation could cause incorrect freepage or CMA accounting. |
| * |
| * We don't want to hit this code for the more frequent |
| * low-order merging. |
| */ |
| if (unlikely(has_isolate_pageblock(zone))) { |
| int buddy_mt; |
| |
| buddy_pfn = __find_buddy_pfn(pfn, order); |
| buddy = page + (buddy_pfn - pfn); |
| buddy_mt = get_pageblock_migratetype(buddy); |
| |
| if (migratetype != buddy_mt |
| && (is_migrate_isolate(migratetype) || |
| is_migrate_isolate(buddy_mt))) |
| goto done_merging; |
| } |
| max_order++; |
| goto continue_merging; |
| } |
| |
| done_merging: |
| set_page_order(page, order); |
| |
| /* |
| * If this is not the largest possible page, check if the buddy |
| * of the next-highest order is free. If it is, it's possible |
| * that pages are being freed that will coalesce soon. In case, |
| * that is happening, add the free page to the tail of the list |
| * so it's less likely to be used soon and more likely to be merged |
| * as a higher order page |
| */ |
| if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn)) { |
| struct page *higher_page, *higher_buddy; |
| combined_pfn = buddy_pfn & pfn; |
| higher_page = page + (combined_pfn - pfn); |
| buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); |
| higher_buddy = higher_page + (buddy_pfn - combined_pfn); |
| if (pfn_valid_within(buddy_pfn) && |
| page_is_buddy(higher_page, higher_buddy, order + 1)) { |
| list_add_tail(&page->lru, |
| &zone->free_area[order].free_list[migratetype]); |
| goto out; |
| } |
| } |
| |
| list_add(&page->lru, &zone->free_area[order].free_list[migratetype]); |
| out: |
| zone->free_area[order].nr_free++; |
| } |
| |
| /* |
| * A bad page could be due to a number of fields. Instead of multiple branches, |
| * try and check multiple fields with one check. The caller must do a detailed |
| * check if necessary. |
| */ |
| static inline bool page_expected_state(struct page *page, |
| unsigned long check_flags) |
| { |
| if (unlikely(atomic_read(&page->_mapcount) != -1)) |
| return false; |
| |
| if (unlikely((unsigned long)page->mapping | |
| page_ref_count(page) | |
| #ifdef CONFIG_MEMCG |
| (unsigned long)page->mem_cgroup | |
| #endif |
| (page->flags & check_flags))) |
| return false; |
| |
| return true; |
| } |
| |
| static void free_pages_check_bad(struct page *page) |
| { |
| const char *bad_reason; |
| unsigned long bad_flags; |
| |
| bad_reason = NULL; |
| bad_flags = 0; |
| |
| if (unlikely(atomic_read(&page->_mapcount) != -1)) |
| bad_reason = "nonzero mapcount"; |
| if (unlikely(page->mapping != NULL)) |
| bad_reason = "non-NULL mapping"; |
| if (unlikely(page_ref_count(page) != 0)) |
| bad_reason = "nonzero _refcount"; |
| if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_FREE)) { |
| bad_reason = "PAGE_FLAGS_CHECK_AT_FREE flag(s) set"; |
| bad_flags = PAGE_FLAGS_CHECK_AT_FREE; |
| } |
| #ifdef CONFIG_MEMCG |
| if (unlikely(page->mem_cgroup)) |
| bad_reason = "page still charged to cgroup"; |
| #endif |
| bad_page(page, bad_reason, bad_flags); |
| } |
| |
| static inline int free_pages_check(struct page *page) |
| { |
| if (likely(page_expected_state(page, PAGE_FLAGS_CHECK_AT_FREE))) |
| return 0; |
| |
| /* Something has gone sideways, find it */ |
| free_pages_check_bad(page); |
| return 1; |
| } |
| |
| static int free_tail_pages_check(struct page *head_page, struct page *page) |
| { |
| int ret = 1; |
| |
| /* |
| * We rely page->lru.next never has bit 0 set, unless the page |
| * is PageTail(). Let's make sure that's true even for poisoned ->lru. |
| */ |
| BUILD_BUG_ON((unsigned long)LIST_POISON1 & 1); |
| |
| if (!IS_ENABLED(CONFIG_DEBUG_VM)) { |
| ret = 0; |
| goto out; |
| } |
| switch (page - head_page) { |
| case 1: |
| /* the first tail page: ->mapping is compound_mapcount() */ |
| if (unlikely(compound_mapcount(page))) { |
| bad_page(page, "nonzero compound_mapcount", 0); |
| goto out; |
| } |
| break; |
| case 2: |
| /* |
| * the second tail page: ->mapping is |
| * page_deferred_list().next -- ignore value. |
| */ |
| break; |
| default: |
| if (page->mapping != TAIL_MAPPING) { |
| bad_page(page, "corrupted mapping in tail page", 0); |
| goto out; |
| } |
| break; |
| } |
| if (unlikely(!PageTail(page))) { |
| bad_page(page, "PageTail not set", 0); |
| goto out; |
| } |
| if (unlikely(compound_head(page) != head_page)) { |
| bad_page(page, "compound_head not consistent", 0); |
| goto out; |
| } |
| ret = 0; |
| out: |
| page->mapping = NULL; |
| clear_compound_head(page); |
| return ret; |
| } |
| |
| static __always_inline bool free_pages_prepare(struct page *page, |
| unsigned int order, bool check_free) |
| { |
| int bad = 0; |
| |
| VM_BUG_ON_PAGE(PageTail(page), page); |
| |
| trace_mm_page_free(page, order); |
| kmemcheck_free_shadow(page, order); |
| |
| /* |
| * Check tail pages before head page information is cleared to |
| * avoid checking PageCompound for order-0 pages. |
| */ |
| if (unlikely(order)) { |
| bool compound = PageCompound(page); |
| int i; |
| |
| VM_BUG_ON_PAGE(compound && compound_order(page) != order, page); |
| |
| if (compound) |
| ClearPageDoubleMap(page); |
| for (i = 1; i < (1 << order); i++) { |
| if (compound) |
| bad += free_tail_pages_check(page, page + i); |
| if (unlikely(free_pages_check(page + i))) { |
| bad++; |
| continue; |
| } |
| (page + i)->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
| } |
| } |
| if (PageMappingFlags(page)) |
| page->mapping = NULL; |
| if (memcg_kmem_enabled() && PageKmemcg(page)) |
| memcg_kmem_uncharge(page, order); |
| if (check_free) |
| bad += free_pages_check(page); |
| if (bad) |
| return false; |
| |
| page_cpupid_reset_last(page); |
| page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; |
| reset_page_owner(page, order); |
| |
| if (!PageHighMem(page)) { |
| debug_check_no_locks_freed(page_address(page), |
| PAGE_SIZE << order); |
| debug_check_no_obj_freed(page_address(page), |
| PAGE_SIZE << order); |
| } |
| arch_free_page(page, order); |
| kernel_poison_pages(page, 1 << order, 0); |
| kernel_map_pages(page, 1 << order, 0); |
| kasan_free_pages(page, order); |
| |
| return true; |
| } |
| |
| #ifdef CONFIG_DEBUG_VM |
| static inline bool free_pcp_prepare(struct page *page) |
| { |
| return free_pages_prepare(page, 0, true); |
| } |
| |
| static inline bool bulkfree_pcp_prepare(struct page *page) |
| { |
| return false; |
| } |
| #else |
| static bool free_pcp_prepare(struct page *page) |
| { |
| return free_pages_prepare(page, 0, false); |
| } |
| |
| static bool bulkfree_pcp_prepare(struct page *page) |
| { |
| return free_pages_check(page); |
| } |
| #endif /* CONFIG_DEBUG_VM */ |
| |
| /* |
| * Frees a number of pages from the PCP lists |
| * Assumes all pages on list are in same zone, and of same order. |
| * count is the number of pages to free. |
| * |
| * If the zone was previously in an "all pages pinned" state then look to |
| * see if this freeing clears that state. |
| * |
| * And clear the zone's pages_scanned counter, to hold off the "all pages are |
| * pinned" detection logic. |
| */ |
| static void free_pcppages_bulk(struct zone *zone, int count, |
| struct per_cpu_pages *pcp) |
| { |
| int migratetype = 0; |
| int batch_free = 0; |
| bool isolated_pageblocks; |
| |
| spin_lock(&zone->lock); |
| isolated_pageblocks = has_isolate_pageblock(zone); |
| |
| while (count) { |
| struct page *page; |
| struct list_head *list; |
| |
| /* |
| * Remove pages from lists in a round-robin fashion. A |
| * batch_free count is maintained that is incremented when an |
| * empty list is encountered. This is so more pages are freed |
| * off fuller lists instead of spinning excessively around empty |
| * lists |
| */ |
| do { |
| batch_free++; |
| if (++migratetype == MIGRATE_PCPTYPES) |
| migratetype = 0; |
| list = &pcp->lists[migratetype]; |
| } while (list_empty(list)); |
| |
| /* This is the only non-empty list. Free them all. */ |
| if (batch_free == MIGRATE_PCPTYPES) |
| batch_free = count; |
| |
| do { |
| int mt; /* migratetype of the to-be-freed page */ |
| |
| page = list_last_entry(list, struct page, lru); |
| /* must delete as __free_one_page list manipulates */ |
| list_del(&page->lru); |
| |
| mt = get_pcppage_migratetype(page); |
| /* MIGRATE_ISOLATE page should not go to pcplists */ |
| VM_BUG_ON_PAGE(is_migrate_isolate(mt), page); |
| /* Pageblock could have been isolated meanwhile */ |
| if (unlikely(isolated_pageblocks)) |
| mt = get_pageblock_migratetype(page); |
| |
| if (bulkfree_pcp_prepare(page)) |
| continue; |
| |
| __free_one_page(page, page_to_pfn(page), zone, 0, mt); |
| trace_mm_page_pcpu_drain(page, 0, mt); |
| } while (--count && --batch_free && !list_empty(list)); |
| } |
| spin_unlock(&zone->lock); |
| } |
| |
| static void free_one_page(struct zone *zone, |
| struct page *page, unsigned long pfn, |
| unsigned int order, |
| int migratetype) |
| { |
| spin_lock(&zone->lock); |
| if (unlikely(has_isolate_pageblock(zone) || |
| is_migrate_isolate(migratetype))) { |
| migratetype = get_pfnblock_migratetype(page, pfn); |
| } |
| __free_one_page(page, pfn, zone, order, migratetype); |
| spin_unlock(&zone->lock); |
| } |
| |
| static void __meminit __init_single_page(struct page *page, unsigned long pfn, |
| unsigned long zone, int nid) |
| { |
| set_page_links(page, zone, nid, pfn); |
| init_page_count(page); |
| page_mapcount_reset(page); |
| page_cpupid_reset_last(page); |
| |
| INIT_LIST_HEAD(&page->lru); |
| #ifdef WANT_PAGE_VIRTUAL |
| /* The shift won't overflow because ZONE_NORMAL is below 4G. */ |
| if (!is_highmem_idx(zone)) |
| set_page_address(page, __va(pfn << PAGE_SHIFT)); |
| #endif |
| } |
| |
| static void __meminit __init_single_pfn(unsigned long pfn, unsigned long zone, |
| int nid) |
| { |
| return __init_single_page(pfn_to_page(pfn), pfn, zone, nid); |
| } |
| |
| #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| static void __meminit init_reserved_page(unsigned long pfn) |
| { |
| pg_data_t *pgdat; |
| int nid, zid; |
| |
| if (!early_page_uninitialised(pfn)) |
| return; |
| |
| nid = early_pfn_to_nid(pfn); |
| pgdat = NODE_DATA(nid); |
| |
| for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| struct zone *zone = &pgdat->node_zones[zid]; |
| |
| if (pfn >= zone->zone_start_pfn && pfn < zone_end_pfn(zone)) |
| break; |
| } |
| __init_single_pfn(pfn, zid, nid); |
| } |
| #else |
| static inline void init_reserved_page(unsigned long pfn) |
| { |
| } |
| #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
| |
| /* |
| * Initialised pages do not have PageReserved set. This function is |
| * called for each range allocated by the bootmem allocator and |
| * marks the pages PageReserved. The remaining valid pages are later |
| * sent to the buddy page allocator. |
| */ |
| void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end) |
| { |
| unsigned long start_pfn = PFN_DOWN(start); |
| unsigned long end_pfn = PFN_UP(end); |
| |
| for (; start_pfn < end_pfn; start_pfn++) { |
| if (pfn_valid(start_pfn)) { |
| struct page *page = pfn_to_page(start_pfn); |
| |
| init_reserved_page(start_pfn); |
| |
| /* Avoid false-positive PageTail() */ |
| INIT_LIST_HEAD(&page->lru); |
| |
| SetPageReserved(page); |
| } |
| } |
| } |
| |
| static void __free_pages_ok(struct page *page, unsigned int order) |
| { |
| unsigned long flags; |
| int migratetype; |
| unsigned long pfn = page_to_pfn(page); |
| |
| if (!free_pages_prepare(page, order, true)) |
| return; |
| |
| migratetype = get_pfnblock_migratetype(page, pfn); |
| local_irq_save(flags); |
| __count_vm_events(PGFREE, 1 << order); |
| free_one_page(page_zone(page), page, pfn, order, migratetype); |
| local_irq_restore(flags); |
| } |
| |
| static void __init __free_pages_boot_core(struct page *page, unsigned int order) |
| { |
| unsigned int nr_pages = 1 << order; |
| struct page *p = page; |
| unsigned int loop; |
| |
| prefetchw(p); |
| for (loop = 0; loop < (nr_pages - 1); loop++, p++) { |
| prefetchw(p + 1); |
| __ClearPageReserved(p); |
| set_page_count(p, 0); |
| } |
| __ClearPageReserved(p); |
| set_page_count(p, 0); |
| |
| page_zone(page)->managed_pages += nr_pages; |
| set_page_refcounted(page); |
| __free_pages(page, order); |
| } |
| |
| #if defined(CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID) || \ |
| defined(CONFIG_HAVE_MEMBLOCK_NODE_MAP) |
| |
| static struct mminit_pfnnid_cache early_pfnnid_cache __meminitdata; |
| |
| int __meminit early_pfn_to_nid(unsigned long pfn) |
| { |
| static DEFINE_SPINLOCK(early_pfn_lock); |
| int nid; |
| |
| spin_lock(&early_pfn_lock); |
| nid = __early_pfn_to_nid(pfn, &early_pfnnid_cache); |
| if (nid < 0) |
| nid = first_online_node; |
| spin_unlock(&early_pfn_lock); |
| |
| return nid; |
| } |
| #endif |
| |
| #ifdef CONFIG_NODES_SPAN_OTHER_NODES |
| static inline bool __meminit __maybe_unused |
| meminit_pfn_in_nid(unsigned long pfn, int node, |
| struct mminit_pfnnid_cache *state) |
| { |
| int nid; |
| |
| nid = __early_pfn_to_nid(pfn, state); |
| if (nid >= 0 && nid != node) |
| return false; |
| return true; |
| } |
| |
| /* Only safe to use early in boot when initialisation is single-threaded */ |
| static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node) |
| { |
| return meminit_pfn_in_nid(pfn, node, &early_pfnnid_cache); |
| } |
| |
| #else |
| |
| static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node) |
| { |
| return true; |
| } |
| static inline bool __meminit __maybe_unused |
| meminit_pfn_in_nid(unsigned long pfn, int node, |
| struct mminit_pfnnid_cache *state) |
| { |
| return true; |
| } |
| #endif |
| |
| |
| void __init __free_pages_bootmem(struct page *page, unsigned long pfn, |
| unsigned int order) |
| { |
| if (early_page_uninitialised(pfn)) |
| return; |
| return __free_pages_boot_core(page, order); |
| } |
| |
| /* |
| * Check that the whole (or subset of) a pageblock given by the interval of |
| * [start_pfn, end_pfn) is valid and within the same zone, before scanning it |
| * with the migration of free compaction scanner. The scanners then need to |
| * use only pfn_valid_within() check for arches that allow holes within |
| * pageblocks. |
| * |
| * Return struct page pointer of start_pfn, or NULL if checks were not passed. |
| * |
| * It's possible on some configurations to have a setup like node0 node1 node0 |
| * i.e. it's possible that all pages within a zones range of pages do not |
| * belong to a single zone. We assume that a border between node0 and node1 |
| * can occur within a single pageblock, but not a node0 node1 node0 |
| * interleaving within a single pageblock. It is therefore sufficient to check |
| * the first and last page of a pageblock and avoid checking each individual |
| * page in a pageblock. |
| */ |
| struct page *__pageblock_pfn_to_page(unsigned long start_pfn, |
| unsigned long end_pfn, struct zone *zone) |
| { |
| struct page *start_page; |
| struct page *end_page; |
| |
| /* end_pfn is one past the range we are checking */ |
| end_pfn--; |
| |
| if (!pfn_valid(start_pfn) || !pfn_valid(end_pfn)) |
| return NULL; |
| |
| start_page = pfn_to_online_page(start_pfn); |
| if (!start_page) |
| return NULL; |
| |
| if (page_zone(start_page) != zone) |
| return NULL; |
| |
| end_page = pfn_to_page(end_pfn); |
| |
| /* This gives a shorter code than deriving page_zone(end_page) */ |
| if (page_zone_id(start_page) != page_zone_id(end_page)) |
| return NULL; |
| |
| return start_page; |
| } |
| |
| void set_zone_contiguous(struct zone *zone) |
| { |
| unsigned long block_start_pfn = zone->zone_start_pfn; |
| unsigned long block_end_pfn; |
| |
| block_end_pfn = ALIGN(block_start_pfn + 1, pageblock_nr_pages); |
| for (; block_start_pfn < zone_end_pfn(zone); |
| block_start_pfn = block_end_pfn, |
| block_end_pfn += pageblock_nr_pages) { |
| |
| block_end_pfn = min(block_end_pfn, zone_end_pfn(zone)); |
| |
| if (!__pageblock_pfn_to_page(block_start_pfn, |
| block_end_pfn, zone)) |
| return; |
| } |
| |
| /* We confirm that there is no hole */ |
| zone->contiguous = true; |
| } |
| |
| void clear_zone_contiguous(struct zone *zone) |
| { |
| zone->contiguous = false; |
| } |
| |
| #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| static void __init deferred_free_range(struct page *page, |
| unsigned long pfn, int nr_pages) |
| { |
| int i; |
| |
| if (!page) |
| return; |
| |
| /* Free a large naturally-aligned chunk if possible */ |
| if (nr_pages == pageblock_nr_pages && |
| (pfn & (pageblock_nr_pages - 1)) == 0) { |
| set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
| __free_pages_boot_core(page, pageblock_order); |
| return; |
| } |
| |
| for (i = 0; i < nr_pages; i++, page++, pfn++) { |
| if ((pfn & (pageblock_nr_pages - 1)) == 0) |
| set_pageblock_migratetype(page, MIGRATE_MOVABLE); |
| __free_pages_boot_core(page, 0); |
| } |
| } |
| |
| /* Completion tracking for deferred_init_memmap() threads */ |
| static atomic_t pgdat_init_n_undone __initdata; |
| static __initdata DECLARE_COMPLETION(pgdat_init_all_done_comp); |
| |
| static inline void __init pgdat_init_report_one_done(void) |
| { |
| if (atomic_dec_and_test(&pgdat_init_n_undone)) |
| complete(&pgdat_init_all_done_comp); |
| } |
| |
| /* Initialise remaining memory on a node */ |
| static int __init deferred_init_memmap(void *data) |
| { |
| pg_data_t *pgdat = data; |
| int nid = pgdat->node_id; |
| struct mminit_pfnnid_cache nid_init_state = { }; |
| unsigned long start = jiffies; |
| unsigned long nr_pages = 0; |
| unsigned long walk_start, walk_end; |
| int i, zid; |
| struct zone *zone; |
| unsigned long first_init_pfn = pgdat->first_deferred_pfn; |
| const struct cpumask *cpumask = cpumask_of_node(pgdat->node_id); |
| |
| if (first_init_pfn == ULONG_MAX) { |
| pgdat_init_report_one_done(); |
| return 0; |
| } |
| |
| /* Bind memory initialisation thread to a local node if possible */ |
| if (!cpumask_empty(cpumask)) |
| set_cpus_allowed_ptr(current, cpumask); |
| |
| /* Sanity check boundaries */ |
| BUG_ON(pgdat->first_deferred_pfn < pgdat->node_start_pfn); |
| BUG_ON(pgdat->first_deferred_pfn > pgdat_end_pfn(pgdat)); |
| pgdat->first_deferred_pfn = ULONG_MAX; |
| |
| /* Only the highest zone is deferred so find it */ |
| for (zid = 0; zid < MAX_NR_ZONES; zid++) { |
| zone = pgdat->node_zones + zid; |
| if (first_init_pfn < zone_end_pfn(zone)) |
| break; |
| } |
| |
| for_each_mem_pfn_range(i, nid, &walk_start, &walk_end, NULL) { |
| unsigned long pfn, end_pfn; |
| struct page *page = NULL; |
| struct page *free_base_page = NULL; |
| unsigned long free_base_pfn = 0; |
| int nr_to_free = 0; |
| |
| end_pfn = min(walk_end, zone_end_pfn(zone)); |
| pfn = first_init_pfn; |
| if (pfn < walk_start) |
| pfn = walk_start; |
| if (pfn < zone->zone_start_pfn) |
| pfn = zone->zone_start_pfn; |
| |
| for (; pfn < end_pfn; pfn++) { |
| if (!pfn_valid_within(pfn)) |
| goto free_range; |
| |
| /* |
| * Ensure pfn_valid is checked every |
| * pageblock_nr_pages for memory holes |
| */ |
| if ((pfn & (pageblock_nr_pages - 1)) == 0) { |
| if (!pfn_valid(pfn)) { |
| page = NULL; |
| goto free_range; |
| } |
| } |
| |
| if (!meminit_pfn_in_nid(pfn, nid, &nid_init_state)) { |
| page = NULL; |
| goto free_range; |
| } |
| |
| /* Minimise pfn page lookups and scheduler checks */ |
| if (page && (pfn & (pageblock_nr_pages - 1)) != 0) { |
| page++; |
| } else { |
| nr_pages += nr_to_free; |
| deferred_free_range(free_base_page, |
| free_base_pfn, nr_to_free); |
| free_base_page = NULL; |
| free_base_pfn = nr_to_free = 0; |
| |
| page = pfn_to_page(pfn); |
| cond_resched(); |
| } |
| |
| if (page->flags) { |
| VM_BUG_ON(page_zone(page) != zone); |
| goto free_range; |
| } |
| |
| __init_single_page(page, pfn, zid, nid); |
| if (!free_base_page) { |
| free_base_page = page; |
| free_base_pfn = pfn; |
| nr_to_free = 0; |
| } |
| nr_to_free++; |
| |
| /* Where possible, batch up pages for a single free */ |
| continue; |
| free_range: |
| /* Free the current block of pages to allocator */ |
| nr_pages += nr_to_free; |
| deferred_free_range(free_base_page, free_base_pfn, |
| nr_to_free); |
| free_base_page = NULL; |
| free_base_pfn = nr_to_free = 0; |
| } |
| /* Free the last block of pages to allocator */ |
| nr_pages += nr_to_free; |
| deferred_free_range(free_base_page, free_base_pfn, nr_to_free); |
| |
| first_init_pfn = max(end_pfn, first_init_pfn); |
| } |
| |
| /* Sanity check that the next zone really is unpopulated */ |
| WARN_ON(++zid < MAX_NR_ZONES && populated_zone(++zone)); |
| |
| pr_info("node %d initialised, %lu pages in %ums\n", nid, nr_pages, |
| jiffies_to_msecs(jiffies - start)); |
| |
| pgdat_init_report_one_done(); |
| return 0; |
| } |
| #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
| |
| void __init page_alloc_init_late(void) |
| { |
| struct zone *zone; |
| |
| #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
| int nid; |
| |
| /* There will be num_node_state(N_MEMORY) threads */ |
| atomic_set(&pgdat_init_n_undone, num_node_state(N_MEMORY)); |
| for_each_node_state(nid, N_MEMORY) { |
| kthread_run(deferred_init_memmap, NODE_DATA(nid), "pgdatinit%d", nid); |
| } |
| |
| /* Block until all are initialised */ |
| wait_for_completion(&pgdat_init_all_done_comp); |
| |
| /* Reinit limits that are based on free pages after the kernel is up */ |
| files_maxfiles_init(); |
| #endif |
| #ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
| /* Discard memblock private memory */ |
| memblock_discard(); |
| #endif |
| |
| for_each_populated_zone(zone) |
| set_zone_contiguous(zone); |
| } |
| |
| #ifdef CONFIG_CMA |
| /* Free whole pageblock and set its migration type to MIGRATE_CMA. */ |
| void __init init_cma_reserved_pageblock(struct page *page) |
| { |
| unsigned i = pageblock_nr_pages; |
| struct page *p = page; |
| |
| do { |
| __ClearPageReserved(p); |
| set_page_count(p, 0); |
| } while (++p, --i); |
| |
| set_pageblock_migratetype(page, MIGRATE_CMA); |
| |
| if (pageblock_order >= MAX_ORDER) { |
| i = pageblock_nr_pages; |
| p = page; |
| do { |
| set_page_refcounted(p); |
| __free_pages(p, MAX_ORDER - 1); |
| p += MAX_ORDER_NR_PAGES; |
| } while (i -= MAX_ORDER_NR_PAGES); |
| } else { |
| set_page_refcounted(page); |
| __free_pages(page, pageblock_order); |
| } |
| |
| adjust_managed_page_count(page, pageblock_nr_pages); |
| } |
| #endif |
| |
| /* |
| * The order of subdivision here is critical for the IO subsystem. |
| * Please do not alter this order without good reasons and regression |
| * testing. Specifically, as large blocks of memory are subdivided, |
| * the order in which smaller blocks are delivered depends on the order |
| * they're subdivided in this function. This is the primary factor |
| * influencing the order in which pages are delivered to the IO |
| * subsystem according to empirical testing, and this is also justified |
| * by considering the behavior of a buddy system containing a single |
| * large block of memory acted on by a series of small allocations. |
| * This behavior is a critical factor in sglist merging's success. |
| * |
| * -- nyc |
| */ |
| static inline void expand(struct zone *zone, struct page *page, |
| int low, int high, struct free_area *area, |
| int migratetype) |
| { |
| unsigned long size = 1 << high; |
| |
| while (high > low) { |
| area--; |
| high--; |
| size >>= 1; |
| VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); |
| |
| /* |
| * Mark as guard pages (or page), that will allow to |
| * merge back to allocator when buddy will be freed. |
| * Corresponding page table entries will not be touched, |
| * pages will stay not present in virtual address space |
| */ |
| if (set_page_guard(zone, &page[size], high, migratetype)) |
| continue; |
| |
| list_add(&page[size].lru, &area->free_list[migratetype]); |
| area->nr_free++; |
| set_page_order(&page[size], high); |
| } |
| } |
| |
| static void check_new_page_bad(struct page *page) |
| { |
| const char *bad_reason = NULL; |
| unsigned long bad_flags = 0; |
| |
| if (unlikely(atomic_read(&page->_mapcount) != -1)) |
| bad_reason = "nonzero mapcount"; |
| if (unlikely(page->mapping != NULL)) |
| bad_reason = "non-NULL mapping"; |
| if (unlikely(page_ref_count(page) != 0)) |
| bad_reason = "nonzero _count"; |
| if (unlikely(page->flags & __PG_HWPOISON)) { |
| bad_reason = "HWPoisoned (hardware-corrupted)"; |
| bad_flags = __PG_HWPOISON; |
| /* Don't complain about hwpoisoned pages */ |
| page_mapcount_reset(page); /* remove PageBuddy */ |
| return; |
| } |
| if (unlikely(page->flags & PAGE_FLAGS_CHECK_AT_PREP)) { |
| bad_reason = "PAGE_FLAGS_CHECK_AT_PREP flag set"; |
| bad_flags = PAGE_FLAGS_CHECK_AT_PREP; |
| } |
| #ifdef CONFIG_MEMCG |
| if (unlikely(page->mem_cgroup)) |
| bad_reason = "page still charged to cgroup"; |
| #endif |
| bad_page(page, bad_reason, bad_flags); |
| } |
| |
| /* |
| * This page is about to be returned from the page allocator |
| */ |
| static inline int check_new_page(struct page *page) |
| { |
| if (likely(page_expected_state(page, |
| PAGE_FLAGS_CHECK_AT_PREP|__PG_HWPOISON))) |
| return 0; |
| |
| check_new_page_bad(page); |
| return 1; |
| } |
| |
| static inline bool free_pages_prezeroed(void) |
| { |
| return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) && |
| page_poisoning_enabled(); |
| } |
| |
| #ifdef CONFIG_DEBUG_VM |
| static bool check_pcp_refill(struct page *page) |
| { |
| return false; |
| } |
| |
| static bool check_new_pcp(struct page *page) |
| { |
| return check_new_page(page); |
| } |
| #else |
| static bool check_pcp_refill(struct page *page) |
| { |
| return check_new_page(page); |
| } |
| static bool check_new_pcp(struct page *page) |
| { |
| return false; |
| } |
| #endif /* CONFIG_DEBUG_VM */ |
| |
| static bool check_new_pages(struct page *page, unsigned int order) |
| { |
| int i; |
| for (i = 0; i < (1 << order); i++) { |
| struct page *p = page + i; |
| |
| if (unlikely(check_new_page(p))) |
| return true; |
| } |
| |
| return false; |
| } |
| |
| inline void post_alloc_hook(struct page *page, unsigned int order, |
| gfp_t gfp_flags) |
| { |
| set_page_private(page, 0); |
| set_page_refcounted(page); |
| |
| arch_alloc_page(page, order); |
| kernel_map_pages(page, 1 << order, 1); |
| kernel_poison_pages(page, 1 << order, 1); |
| kasan_alloc_pages(page, order); |
| set_page_owner(page, order, gfp_flags); |
| } |
| |
| static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, |
| unsigned int alloc_flags) |
| { |
| int i; |
| |
| post_alloc_hook(page, order, gfp_flags); |
| |
| if (!free_pages_prezeroed() && (gfp_flags & __GFP_ZERO)) |
| for (i = 0; i < (1 << order); i++) |
| clear_highpage(page + i); |
| |
| if (order && (gfp_flags & __GFP_COMP)) |
| prep_compound_page(page, order); |
| |
| /* |
| * page is set pfmemalloc when ALLOC_NO_WATERMARKS was necessary to |
| * allocate the page. The expectation is that the caller is taking |
| * steps that will free more memory. The caller should avoid the page |
| * being used for !PFMEMALLOC purposes. |
| */ |
| if (alloc_flags & ALLOC_NO_WATERMARKS) |
| set_page_pfmemalloc(page); |
| else |
| clear_page_pfmemalloc(page); |
| } |
| |
| /* |
| * Go through the free lists for the given migratetype and remove |
| * the smallest available page from the freelists |
| */ |
| static inline |
| struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, |
| int migratetype) |
| { |
| unsigned int current_order; |
| struct free_area *area; |
| struct page *page; |
| |
| /* Find a page of the appropriate size in the preferred list */ |
| for (current_order = order; current_order < MAX_ORDER; ++current_order) { |
| area = &(zone->free_area[current_order]); |
| page = list_first_entry_or_null(&area->free_list[migratetype], |
| struct page, lru); |
| if (!page) |
| continue; |
| list_del(&page->lru); |
| rmv_page_order(page); |
| area->nr_free--; |
| expand(zone, page, order, current_order, area, migratetype); |
| set_pcppage_migratetype(page, migratetype); |
| return page; |
| } |
| |
| return NULL; |
| } |
| |
| |
| /* |
| * This array describes the order lists are fallen back to when |
| * the free lists for the desirable migrate type are depleted |
| */ |
| static int fallbacks[MIGRATE_TYPES][4] = { |
| [MIGRATE_UNMOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
| [MIGRATE_RECLAIMABLE] = { MIGRATE_UNMOVABLE, MIGRATE_MOVABLE, MIGRATE_TYPES }, |
| [MIGRATE_MOVABLE] = { MIGRATE_RECLAIMABLE, MIGRATE_UNMOVABLE, MIGRATE_TYPES }, |
| #ifdef CONFIG_CMA |
| [MIGRATE_CMA] = { MIGRATE_TYPES }, /* Never used */ |
| #endif |
| #ifdef CONFIG_MEMORY_ISOLATION |
| [MIGRATE_ISOLATE] = { MIGRATE_TYPES }, /* Never used */ |
| #endif |
| }; |
| |
| #ifdef CONFIG_CMA |
| static struct page *__rmqueue_cma_fallback(struct zone *zone, |
| unsigned int order) |
| { |
| return __rmqueue_smallest(zone, order, MIGRATE_CMA); |
| } |
| #else |
| static inline struct page *__rmqueue_cma_fallback(struct zone *zone, |
| unsigned int order) { return NULL; } |
| #endif |
| |
| /* |
| * Move the free pages in a range to the free lists of the requested type. |
| * Note that start_page and end_pages are not aligned on a pageblock |
| * boundary. If alignment is required, use move_freepages_block() |
| */ |
| static int move_freepages(struct zone *zone, |
| struct page *start_page, struct page *end_page, |
| int migratetype, int *num_movable) |
| { |
| struct page *page; |
| unsigned int order; |
| int pages_moved = 0; |
| |
| #ifndef CONFIG_HOLES_IN_ZONE |
| /* |
| * page_zone is not safe to call in this context when |
| * CONFIG_HOLES_IN_ZONE is set. This bug check is probably redundant |
| * anyway as we check zone boundaries in move_freepages_block(). |
| * Remove at a later date when no bug reports exist related to |
| * grouping pages by mobility |
| */ |
| VM_BUG_ON(page_zone(start_page) != page_zone(end_page)); |
| #endif |
| |
| if (num_movable) |
| *num_movable = 0; |
| |
| for (page = start_page; page <= end_page;) { |
| if (!pfn_valid_within(page_to_pfn(page))) { |
| page++; |
| continue; |
| } |
| |
| /* Make sure we are not inadvertently changing nodes */ |
| VM_BUG_ON_PAGE(page_to_nid(page) != zone_to_nid(zone), page); |
| |
| if (!PageBuddy(page)) { |
| /* |
| * We assume that pages that could be isolated for |
| * migration are movable. But we don't actually try |
| * isolating, as that would be expensive. |
| */ |
| if (num_movable && |
| (PageLRU(page) || __PageMovable(page))) |
| (*num_movable)++; |
| |
| page++; |
| continue; |
| } |
| |
| order = page_order(page); |
| list_move(&page->lru, |
| &zone->free_area[order].free_list[migratetype]); |
| page += 1 << order; |
| pages_moved += 1 << order; |
| } |
| |
| return pages_moved; |
| } |
| |
| int move_freepages_block(struct zone *zone, struct page *page, |
| int migratetype, int *num_movable) |
| { |
| unsigned long start_pfn, end_pfn; |
| struct page *start_page, *end_page; |
| |
| start_pfn = page_to_pfn(page); |
| start_pfn = start_pfn & ~(pageblock_nr_pages-1); |
| start_page = pfn_to_page(start_pfn); |
| end_page = start_page + pageblock_nr_pages - 1; |
| end_pfn = start_pfn + pageblock_nr_pages - 1; |
| |
| /* Do not cross zone boundaries */ |
| if (!zone_spans_pfn(zone, start_pfn)) |
| start_page = page; |
| if (!zone_spans_pfn(zone, end_pfn)) |
| return 0; |
| |
| return move_freepages(zone, start_page, end_page, migratetype, |
| num_movable); |
| } |
| |
| static void change_pageblock_range(struct page *pageblock_page, |
| int start_order, int migratetype) |
| { |
| int nr_pageblocks = 1 << (start_order - pageblock_order); |
| |
| while (nr_pageblocks--) { |
| set_pageblock_migratetype(pageblock_page, migratetype); |
| pageblock_page += pageblock_nr_pages; |
| } |
| } |
| |
| /* |
| * When we are falling back to another migratetype during allocation, try to |
| * steal extra free pages from the same pageblocks to satisfy further |
| * allocations, instead of polluting multiple pageblocks. |
| * |
| * If we are stealing a relatively large buddy page, it is likely there will |
| * be more free pages in the pageblock, so try to steal them all. For |
| * reclaimable and unmovable allocations, we steal regardless of page size, |
| * as fragmentation caused by those allocations polluting movable pageblocks |
| * is worse than movable allocations stealing from unmovable and reclaimable |
| * pageblocks. |
| */ |
| static bool can_steal_fallback(unsigned int order, int start_mt) |
| { |
| /* |
| * Leaving this order check is intended, although there is |
| * relaxed order check in next check. The reason is that |
| * we can actually steal whole pageblock if this condition met, |
| * but, below check doesn't guarantee it and that is just heuristic |
| * so could be changed anytime. |
| */ |
| if (order >= pageblock_order) |
| return true; |
| |
| if (order >= pageblock_order / 2 || |
| start_mt == MIGRATE_RECLAIMABLE || |
| start_mt == MIGRATE_UNMOVABLE || |
| page_group_by_mobility_disabled) |
| return true; |
| |
| return false; |
| } |
| |
| /* |
| * This function implements actual steal behaviour. If order is large enough, |
| * we can steal whole pageblock. If not, we first move freepages in this |
| * pageblock to our migratetype and determine how many already-allocated pages |
| * are there in the pageblock with a compatible migratetype. If at least half |
| * of pages are free or compatible, we can change migratetype of the pageblock |
| * itself, so pages freed in the future will be put on the correct free list. |
| */ |
| static void steal_suitable_fallback(struct zone *zone, struct page *page, |
| int start_type, bool whole_block) |
| { |
| unsigned int current_order = page_order(page); |
| struct free_area *area; |
| int free_pages, movable_pages, alike_pages; |
| int old_block_type; |
| |
| old_block_type = get_pageblock_migratetype(page); |
| |
| /* |
| * This can happen due to races and we want to prevent broken |
| * highatomic accounting. |
| */ |
| if (is_migrate_highatomic(old_block_type)) |
| goto single_page; |
| |
| /* Take ownership for orders >= pageblock_order */ |
| if (current_order >= pageblock_order) { |
| change_pageblock_range(page, current_order, start_type); |
| goto single_page; |
| } |
| |
| /* We are not allowed to try stealing from the whole block */ |
| if (!whole_block) |
| goto single_page; |
| |
| free_pages = move_freepages_block(zone, page, start_type, |
| &movable_pages); |
| /* |
| * Determine how many pages are compatible with our allocation. |
| * For movable allocation, it's the number of movable pages which |
| * we just obtained. For other types it's a bit more tricky. |
| */ |
| if (start_type == MIGRATE_MOVABLE) { |
| alike_pages = movable_pages; |
| } else { |
| /* |
| * If we are falling back a RECLAIMABLE or UNMOVABLE allocation |
| * to MOVABLE pageblock, consider all non-movable pages as |
| * compatible. If it's UNMOVABLE falling back to RECLAIMABLE or |
| * vice versa, be conservative since we can't distinguish the |
| * exact migratetype of non-movable pages. |
| */ |
| if (old_block_type == MIGRATE_MOVABLE) |
| alike_pages = pageblock_nr_pages |
| - (free_pages + movable_pages); |
| else |
| alike_pages = 0; |
| } |
| |
| /* moving whole block can fail due to zone boundary conditions */ |
| if (!free_pages) |
| goto single_page; |
| |
| /* |
| * If a sufficient number of pages in the block are either free or of |
| * comparable migratability as our allocation, claim the whole block. |
| */ |
| if (free_pages + alike_pages >= (1 << (pageblock_order-1)) || |
| page_group_by_mobility_disabled) |
| set_pageblock_migratetype(page, start_type); |
| |
| return; |
| |
| single_page: |
| area = &zone->free_area[current_order]; |
| list_move(&page->lru, &area->free_list[start_type]); |
| } |
| |
| /* |
| * Check whether there is a suitable fallback freepage with requested order. |
| * If only_stealable is true, this function returns fallback_mt only if |
| * we can steal other freepages all together. This would help to reduce |
| * fragmentation due to mixed migratetype pages in one pageblock. |
| */ |
| int find_suitable_fallback(struct free_area *area, unsigned int order, |
| int migratetype, bool only_stealable, bool *can_steal) |
| { |
| int i; |
| int fallback_mt; |
| |
| if (area->nr_free == 0) |
| return -1; |
| |
| *can_steal = false; |
| for (i = 0;; i++) { |
| fallback_mt = fallbacks[migratetype][i]; |
| if (fallback_mt == MIGRATE_TYPES) |
| break; |
| |
| if (list_empty(&area->free_list[fallback_mt])) |
| continue; |
| |
| if (can_steal_fallback(order, migratetype)) |
| *can_steal = true; |
| |
| if (!only_stealable) |
| return fallback_mt; |
| |
| if (*can_steal) |
| return fallback_mt; |
| } |
| |
| return -1; |
| } |
| |
| /* |
| * Reserve a pageblock for exclusive use of high-order atomic allocations if |
| * there are no empty page blocks that contain a page with a suitable order |
| */ |
| static void reserve_highatomic_pageblock(struct page *page, struct zone *zone, |
| unsigned int alloc_order) |
| { |
| int mt; |
| unsigned long max_managed, flags; |
| |
| /* |
| * Limit the number reserved to 1 pageblock or roughly 1% of a zone. |
| * Check is race-prone but harmless. |
| */ |
| max_managed = (zone->managed_pages / 100) + pageblock_nr_pages; |
| if (zone->nr_reserved_highatomic >= max_managed) |
| return; |
| |
| spin_lock_irqsave(&zone->lock, flags); |
| |
| /* Recheck the nr_reserved_highatomic limit under the lock */ |
| if (zone->nr_reserved_highatomic >= max_managed) |
| goto out_unlock; |
| |
| /* Yoink! */ |
| mt = get_pageblock_migratetype(page); |
| if (!is_migrate_highatomic(mt) && !is_migrate_isolate(mt) |
| && !is_migrate_cma(mt)) { |
| zone->nr_reserved_highatomic += pageblock_nr_pages; |
| set_pageblock_migratetype(page, MIGRATE_HIGHATOMIC); |
| move_freepages_block(zone, page, MIGRATE_HIGHATOMIC, NULL); |
| } |
| |
| out_unlock: |
| spin_unlock_irqrestore(&zone->lock, flags); |
| } |
| |
| /* |
| * Used when an allocation is about to fail under memory pressure. This |
| * potentially hurts the reliability of high-order allocations when under |
| * intense memory pressure but failed atomic allocations should be easier |
| * to recover from than an OOM. |
| * |
| * If @force is true, try to unreserve a pageblock even though highatomic |
| * pageblock is exhausted. |
| */ |
| static bool unreserve_highatomic_pageblock(const struct alloc_context *ac, |
| bool force) |
| { |
| struct zonelist *zonelist = ac->zonelist; |
| unsigned long flags; |
| struct zoneref *z; |
| struct zone *zone; |
| struct page *page; |
| int order; |
| bool ret; |
| |
| for_each_zone_zonelist_nodemask(zone, z, zonelist, ac->high_zoneidx, |
| ac->nodemask) { |
| /* |
| * Preserve at least one pageblock unless memory pressure |
| * is really high. |
| */ |
| if (!force && zone->nr_reserved_highatomic <= |
| pageblock_nr_pages) |
| continue; |
| |
| spin_lock_irqsave(&zone->lock, flags); |
| for (order = 0; order < MAX_ORDER; order++) { |
| struct free_area *area = &(zone->free_area[order]); |
| |
| page = list_first_entry_or_null( |
| &area->free_list[MIGRATE_HIGHATOMIC], |
| struct page, lru); |
| if (!page) |
| continue; |
| |
| /* |
| * In page freeing path, migratetype change is racy so |
| * we can counter several free pages in a pageblock |
| * in this loop althoug we changed the pageblock type |
| * from highatomic to ac->migratetype. So we should |
| * adjust the count once. |
| */ |
| if (is_migrate_highatomic_page(page)) { |
| /* |
| * It should never happen but changes to |
| * locking could inadvertently allow a per-cpu |
| * drain to add pages to MIGRATE_HIGHATOMIC |
| * while unreserving so be safe and watch for |
| * underflows. |
| */ |
| zone->nr_reserved_highatomic -= min( |
| pageblock_nr_pages, |
| zone->nr_reserved_highatomic); |
| } |
| |
| /* |
| * Convert to ac->migratetype and avoid the normal |
| * pageblock stealing heuristics. Minimally, the caller |
| * is doing the work and needs the pages. More |
| * importantly, if the block was always converted to |
| * MIGRATE_UNMOVABLE or another type then the number |
| * of pageblocks that cannot be completely freed |
| * may increase. |
| */ |
| set_pageblock_migratetype(page, ac->migratetype); |
| ret = move_freepages_block(zone, page, ac->migratetype, |
| NULL); |
| if (ret) { |
| spin_unlock_irqrestore(&zone->lock, flags); |
| return ret; |
| } |
| } |
| spin_unlock_irqrestore(&zone->lock, flags); |
| } |
| |
| return false; |
| } |
| |
| /* |
| * Try finding a free buddy page on the fallback list and put it on the free |
| * list of requested migratetype, possibly along with other pages from the same |
| * block, depending on fragmentation avoidance heuristics. Returns true if |
| * fallback was found so that __rmqueue_smallest() can grab it. |
| * |
| * The use of signed ints for order and current_order is a deliberate |
| * deviation from the rest of this file, to make the for loop |
| * condition simpler. |
| */ |
| static inline bool |
| __rmqueue_fallback(struct zone *zone, int order, int start_migratetype) |
| { |
| struct free_area *area; |
| int current_order; |
| struct page *page; |
| int fallback_mt; |
| bool can_steal; |
| |
| /* |
| * Find the largest available free page in the other list. This roughly |
| * approximates finding the pageblock with the most free pages, which |
| * would be too costly to do exactly. |
| */ |
| for (current_order = MAX_ORDER - 1; current_order >= order; |
| --current_order) { |
| area = &(zone->free_area[current_order]); |
| fallback_mt = find_suitable_fallback(area, current_order, |
| start_migratetype, false, &can_steal); |
| if (fallback_mt == -1) |
| continue; |
| |
| /* |
| * We cannot steal all free pages from the pageblock and the |
| * requested migratetype is movable. In that case it's better to |
| * steal and split the smallest available page instead of the |
| * largest available page, because even if the next movable |
| * allocation falls back into a different pageblock than this |
| * one, it won't cause permanent fragmentation. |
| */ |
| if (!can_steal && start_migratetype == MIGRATE_MOVABLE |
| && current_order > order) |
| goto find_smallest; |
| |
| goto do_steal; |
| } |
| |
| return false; |
| |
| find_smallest: |
| for (current_order = order; current_order < MAX_ORDER; |
| current_order++) { |
| area = &(zone->free_area[current_order]); |
| fallback_mt = find_suitable_fallback(area, current_order, |
| start_migratetype, false, &can_steal); |
| if (fallback_mt != -1) |
| break; |
| } |
| |
| /* |
| * This should not happen - we already found a suitable fallback |
| * when looking for the largest page. |
| */ |
| VM_BUG_ON(current_order == MAX_ORDER); |
| |
| do_steal: |
| page = list_first_entry(&area->free_list[fallback_mt], |
| struct page, lru); |
| |
| steal_suitable_fallback(zone, page, start_migratetype, can_steal); |
| |
| trace_mm_page_alloc_extfrag(page, order, current_order, |
| start_migratetype, fallback_mt); |
| |
| return true; |
| |
| } |
| |
| /* |
| * Do the hard work of removing an element from the buddy allocator. |
| * Call me with the zone->lock already held. |
| */ |
| static struct page *__rmqueue(struct zone *zone, unsigned int order, |
| int migratetype) |
| { |
| struct page *page; |
| |
| retry: |
| page = __rmqueue_smallest(zone, order, migratetype); |
| if (unlikely(!page)) { |
| if (migratetype == MIGRATE_MOVABLE) |
| page = __rmqueue_cma_fallback(zone, order); |
| |
| if (!page && __rmqueue_fallback(zone, order, migratetype)) |
| goto retry; |
| } |
| |
| trace_mm_page_alloc_zone_locked(page, order, migratetype); |
| return page; |
| } |
| |
| /* |
| * Obtain a specified number of elements from the buddy allocator, all under |
| * a single hold of the lock, for efficiency. Add them to the supplied list. |
| * Returns the number of new pages which were placed at *list. |
| */ |
| static int rmqueue_bulk(struct zone *zone, unsigned int order, |
| unsigned long count, struct list_head *list, |
| int migratetype, bool cold) |
| { |
| int i, alloced = 0; |
| |
| spin_lock(&zone->lock); |
| for (i = 0; i < count; ++i) { |
| struct page *page = __rmqueue(zone, order, migratetype); |
| if (unlikely(page == NULL)) |
| break; |
| |
| if (unlikely(check_pcp_refill(page))) |
| continue; |
| |
| /* |
| * Split buddy pages returned by expand() are received here |
| * in physical page order. The page is added to the callers and |
| * list and the list head then moves forward. From the callers |
| * perspective, the linked list is ordered by page number in |
| * some conditions. This is useful for IO devices that can |
| * merge IO requests if the physical pages are ordered |
| * properly. |
| */ |
| if (likely(!cold)) |
| list_add(&page->lru, list); |
| else |
| list_add_tail(&page->lru, list); |
| list = &page->lru; |
| alloced++; |
| if (is_migrate_cma(get_pcppage_migratetype(page))) |
| __mod_zone_page_state(zone, NR_FREE_CMA_PAGES, |
| -(1 << order)); |
| } |
| |
| /* |
| * i pages were removed from the buddy list even if some leak due |
| * to check_pcp_refill failing so adjust NR_FREE_PAGES based |
| * on i. Do not confuse with 'alloced' which is the number of |
| * pages added to the pcp list. |
| */ |
| __mod_zone_page_state(zone, NR_FREE_PAGES, -(i << order)); |
| spin_unlock(&zone->lock); |
| return alloced; |
| } |
| |
| #ifdef CONFIG_NUMA |
| /* |
| * Called from the vmstat counter updater to drain pagesets of this |
| * currently executing processor on remote nodes after they have |
| * expired. |
| * |
| * Note that this function must be called with the thread pinned to |
| * a single processor. |
| */ |
| void drain_zone_pages(struct zone *zone, struct per_cpu_pages *pcp) |
| { |
| unsigned long flags; |
| int to_drain, batch; |
| |
| local_irq_save(flags); |
| batch = READ_ONCE(pcp->batch); |
| to_drain = min(pcp->count, batch); |
| if (to_drain > 0) { |
| free_pcppages_bulk(zone, to_drain, pcp); |
| pcp->count -= to_drain; |
| } |
| local_irq_restore(flags); |
| } |
| #endif |
| |
| /* |
| * Drain pcplists of the indicated processor and zone. |
| * |
| * The processor must either be the current processor and the |
| * thread pinned to the current processor or a processor that |
| * is not online. |
| */ |
| static void drain_pages_zone(unsigned int cpu, struct zone *zone) |
| { |
| unsigned long flags; |
| struct per_cpu_pageset *pset; |
| struct per_cpu_pages *pcp; |
| |
| local_irq_save(flags); |
| pset = per_cpu_ptr(zone->pageset, cpu); |
| |
| pcp = &pset->pcp; |
| if (pcp->count) { |
| free_pcppages_bulk(zone, pcp->count, pcp); |
| pcp->count = 0; |
| } |
| local_irq_restore(flags); |
| } |
| |
| /* |
| * Drain pcplists of all zones on the indicated processor. |
| * |
| * The processor must either be the current processor and the |
| * thread pinned to the current processor or a processor that |
| * is not online. |
| */ |
| static void drain_pages(unsigned int cpu) |
| { |
| struct zone *zone; |
| |
| for_each_populated_zone(zone) { |
| drain_pages_zone(cpu, zone); |
| } |
| } |
| |
| /* |
| * Spill all of this CPU's per-cpu pages back into the buddy allocator. |
| * |
| * The CPU has to be pinned. When zone parameter is non-NULL, spill just |
| * the single zone's pages. |
| */ |
| void drain_local_pages(struct zone *zone) |
| { |
| int cpu = smp_processor_id(); |
| |
| if (zone) |
| drain_pages_zone(cpu, zone); |
| else |
| drain_pages(cpu); |
| } |
| |
| static void drain_local_pages_wq(struct work_struct *work) |
| { |
| /* |
| * drain_all_pages doesn't use proper cpu hotplug protection so |
| * we can race with cpu offline when the WQ can move this from |
| * a cpu pinned worker to an unbound one. We can operate on a different |
| * cpu which is allright but we also have to make sure to not move to |
| * a different one. |
| */ |
| preempt_disable(); |
| drain_local_pages(NULL); |
| preempt_enable(); |
| } |
| |
| /* |
| * Spill all the per-cpu pages from all CPUs back into the buddy allocator. |
| * |
| * When zone parameter is non-NULL, spill just the single zone's pages. |
| * |
| * Note that this can be extremely slow as the draining happens in a workqueue. |
| */ |
| void drain_all_pages(struct zone *zone) |
| { |
| int cpu; |
| |
| /* |
| * Allocate in the BSS so we wont require allocation in |
| * direct reclaim path for CONFIG_CPUMASK_OFFSTACK=y |
| */ |
| static cpumask_t cpus_with_pcps; |
| |
| /* |
| * Make sure nobody triggers this path before mm_percpu_wq is fully |
| * initialized. |
| */ |
| if (WARN_ON_ONCE(!mm_percpu_wq)) |
| return; |
| |
| /* Workqueues cannot recurse */ |
| if (current->flags & PF_WQ_WORKER) |
| return; |
| |
| /* |
| * Do not drain if one is already in progress unless it's specific to |
| * a zone. Such callers are primarily CMA and memory hotplug and need |
| * the drain to be complete when the call returns. |
| */ |
| if (unlikely(!mutex_trylock(&pcpu_drain_mutex))) { |
| if (!zone) |
| return; |
| mutex_lock(&pcpu_drain_mutex); |
| } |
| |
| /* |
| * We don't care about racing with CPU hotplug event |
| * as offline notification will cause the notified |
| * cpu to drain that CPU pcps and on_each_cpu_mask |
| * disables preemption as part of its processing |
| */ |
| for_each_online_cpu(cpu) { |
| struct per_cpu_pageset *pcp; |
| struct zone *z; |
| bool has_pcps = false; |
| |
| if (zone) { |
| pcp = per_cpu_ptr(zone->pageset, cpu); |
| if (pcp->pcp.count) |
| has_pcps = true; |
| } else { |
| for_each_populated_zone(z) { |
| pcp = per_cpu_ptr(z->pageset, cpu); |
| if (pcp->pcp.count) { |
| has_pcps = true; |
| break; |
| } |
| } |
| } |
| |
| if (has_pcps) |
| cpumask_set_cpu(cpu, &cpus_with_pcps); |
| else |
| cpumask_clear_cpu(cpu, &cpus_with_pcps); |
| } |
| |
| for_each_cpu(cpu, &cpus_with_pcps) { |
| struct work_struct *work = per_cpu_ptr(&pcpu_drain, cpu); |
| INIT_WORK(work, drain_local_pages_wq); |
| queue_work_on(cpu, mm_percpu_wq, work); |
| } |
| for_each_cpu(cpu, &cpus_with_pcps) |
| flush_work(per_cpu_ptr(&pcpu_drain, cpu)); |
| |
| mutex_unlock(&pcpu_drain_mutex); |
| } |
| |
| #ifdef CONFIG_HIBERNATION |
| |
| /* |
| * Touch the watchdog for every WD_PAGE_COUNT pages. |
| */ |
| #define WD_PAGE_COUNT (128*1024) |
| |
| void mark_free_pages(struct zone *zone) |
| { |
| unsigned long pfn, max_zone_pfn, page_count = WD_PAGE_COUNT; |
| unsigned long flags; |
| unsigned int order, t; |
| struct page *page; |
| |
| if (zone_is_empty(zone)) |
| return; |
| |
| spin_lock_irqsave(&zone->lock, flags); |
| |
| max_zone_pfn = zone_end_pfn(zone); |
| for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) |
| if (pfn_valid(pfn)) { |
| page = pfn_to_page(pfn); |
| |
| if (!--page_count) { |
| touch_nmi_watchdog(); |
| page_count = WD_PAGE_COUNT; |
| } |
| |
| if (page_zone(page) != zone) |
| continue; |
| |
| if (!swsusp_page_is_forbidden(page)) |
| swsusp_unset_page_free(page); |
| } |
| |
| for_each_migratetype_order(order, t) { |
| list_for_each_entry(page, |
| &zone->free_area[order].free_list[t], lru) { |
| unsigned long i; |
| |
| pfn = page_to_pfn(page); |
| for (i = 0; i < (1UL << order); i++) { |
| if (!--page_count) { |
| touch_nmi_watchdog(); |
| page_count = WD_PAGE_COUNT; |
| } |
| swsusp_set_page_free(pfn_to_page(pfn + i)); |
| } |
| } |
| } |
| spin_unlock_irqrestore(&zone->lock, flags); |
| } |
| #endif /* CONFIG_PM */ |
| |
| /* |
| * Free a 0-order page |
| * cold == true ? free a cold page : free a hot page |
| */ |
| void free_hot_cold_page(struct page *page, bool cold) |
| { |
| struct zone *zone = page_zone(page); |
| struct per_cpu_pages *pcp; |
| unsigned long flags; |
| unsigned long pfn = page_to_pfn(page); |
| int migratetype; |
| |
| if (!free_pcp_prepare(page)) |
| return; |
| |
| migratetype = get_pfnblock_migratetype(page, pfn); |
| set_pcppage_migratetype(page, migratetype); |
| local_irq_save(flags); |
| __count_vm_event(PGFREE); |
| |
| /* |
| * We only track unmovable, reclaimable and movable on pcp lists. |
| * Free ISOLATE pages back to the allocator because they are being |
| * offlined but treat HIGHATOMIC as movable pages so we can get those |
| * areas back if necessary. Otherwise, we may have to free |
| * excessively into the page allocator |
| */ |
| if (migratetype >= MIGRATE_PCPTYPES) { |
| if (unlikely(is_migrate_isolate(migratetype))) { |
| free_one_page(zone, page, pfn, 0, migratetype); |
| goto out; |
| } |
| migratetype = MIGRATE_MOVABLE; |
| } |
| |
| pcp = &this_cpu_ptr(zone->pageset)->pcp; |
| if (!cold) |
| list_add(&page->lru, &pcp->lists[migratetype]); |
| else |
| list_add_tail(&page->lru, &pcp->lists[migratetype]); |
| pcp->count++; |
| if (pcp->count >= pcp->high) { |
| unsigned long batch = READ_ONCE(pcp->batch); |
| free_pcppages_bulk(zone, batch, pcp); |
| pcp->count -= batch; |
| } |
| |
| out: |
| local_irq_restore(flags); |
| } |
| |
| /* |
| * Free a list of 0-order pages |
| */ |
| void free_hot_cold_page_list(struct list_head *list, bool cold) |
| { |
| struct page *page, *next; |
| |
| list_for_each_entry_safe(page, next, list, lru) { |
| trace_mm_page_free_batched(page, cold); |
| free_hot_cold_page(page, cold); |
| } |
| } |
| |
| /* |
| * split_page takes a non-compound higher-order page, and splits it into |
| * n (1<<order) sub-pages: page[0..n] |
| * Each sub-page must be freed individually. |
| * |
| * Note: this is probably too low level an operation for use in drivers. |
| * Please consult with lkml before using this in your driver. |
| */ |
| void split_page(struct page *page, unsigned int order) |
| { |
| int i; |
| |
| VM_BUG_ON_PAGE(PageCompound(page), page); |
| VM_BUG_ON_PAGE(!page_count(page), page); |
| |
| #ifdef CONFIG_KMEMCHECK |
| /* |
| * Split shadow pages too, because free(page[0]) would |
| * otherwise free the whole shadow. |
| */ |
| if (kmemcheck_page_is_tracked(page)) |
| split_page(virt_to_page(page[0].shadow), order); |
| #endif |
| |
| for (i = 1; i < (1 << order); i++) |
| set_page_refcounted(page + i); |
| split_page_owner(page, order); |
| } |
| EXPORT_SYMBOL_GPL(split_page); |
| |
| int __isolate_free_page(struct page *page, unsigned int order) |
| { |
| unsigned long watermark; |
| struct zone *zone; |
| int mt; |
| |
| BUG_ON(!PageBuddy(page)); |
| |
| zone = page_zone(page); |
| mt = get_pageblock_migratetype(page); |
| |
| if (!is_migrate_isolate(mt)) { |
| /* |
| * Obey watermarks as if the page was being allocated. We can |
| * emulate a high-order watermark check with a raised order-0 |
| * watermark, because we already know our high-order page |
| * exists. |
| */ |
| watermark = min_wmark_pages(zone) + (1UL << order); |
| if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA)) |
| return 0; |
| |
| __mod_zone_freepage_state(zone, -(1UL << order), mt); |
| } |
| |
| /* Remove page from free list */ |
| list_del(&page->lru); |
| zone->free_area[order].nr_free--; |
| rmv_page_order(page); |
| |
| /* |
| * Set the pageblock if the isolated page is at least half of a |
| * pageblock |
| */ |
| if (order >= pageblock_order - 1) { |
| struct page *endpage = page + (1 << order) - 1; |
| for (; page < endpage; page += pageblock_nr_pages) { |
| int mt = get_pageblock_migratetype(page); |
| if (!is_migrate_isolate(mt) && !is_migrate_cma(mt) |
| && !is_migrate_highatomic(mt)) |
| set_pageblock_migratetype(page, |
| MIGRATE_MOVABLE); |
| } |
| } |
| |
| |
| return 1UL << order; |
| } |
| |
| /* |
| * Update NUMA hit/miss statistics |
| * |
| * Must be called with interrupts disabled. |
| */ |
| static inline void zone_statistics(struct zone *preferred_zone, struct zone *z) |
| { |
| #ifdef CONFIG_NUMA |
| enum numa_stat_item local_stat = NUMA_LOCAL; |
| |
| if (z->node != numa_node_id()) |
| local_stat = NUMA_OTHER; |
| |
| if (z->node == preferred_zone->node) |
| __inc_numa_state(z, NUMA_HIT); |
| else { |
| __inc_numa_state(z, NUMA_MISS); |
| __inc_numa_state(preferred_zone, NUMA_FOREIGN); |
| } |
| __inc_numa_state(z, local_stat); |
| #endif |
| } |
| |
| /* Remove page from the per-cpu list, caller must protect the list */ |
| static struct page *__rmqueue_pcplist(struct zone *zone, int migratetype, |
| bool cold, struct per_cpu_pages *pcp, |
| struct list_head *list) |
| { |
| struct page *page; |
| |
| do { |
| if (list_empty(list)) { |
| pcp->count += rmqueue_bulk(zone, 0, |
| pcp->batch, list, |
| migratetype, cold); |
| if (unlikely(list_empty(list))) |
| return NULL; |
| } |
| |
| if (cold) |
| page = list_last_entry(list, struct page, lru); |
| else |
| page = list_first_entry(list, struct page, lru); |
| |
| list_del(&page->lru); |
| pcp->count--; |
| } while (check_new_pcp(page)); |
| |
| return page; |
| } |
| |
| /* Lock and remove page from the per-cpu list */ |
| static struct page *rmqueue_pcplist(struct zone *preferred_zone, |
| struct zone *zone, unsigned int order, |
| gfp_t gfp_flags, int migratetype) |
| { |
| struct per_cpu_pages *pcp; |
| struct list_head *list; |
| bool cold = ((gfp_flags & __GFP_COLD) != 0); |
| struct page *page; |
| unsigned long flags; |
| |
| local_irq_save(flags); |
| pcp = &this_cpu_ptr(zone->pageset)->pcp; |
| list = &pcp->lists[migratetype]; |
| page = __rmqueue_pcplist(zone, migratetype, cold, pcp, list); |
| if (page) { |
| __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); |
| zone_statistics(preferred_zone, zone); |
| } |
| local_irq_restore(flags); |
| return page; |
| } |
| |
| /* |
| * Allocate a page from the given zone. Use pcplists for order-0 allocations. |
| */ |
| static inline |
| struct page *rmqueue(struct zone *preferred_zone, |
| struct zone *zone, unsigned int order, |
| gfp_t gfp_flags, unsigned int alloc_flags, |
| int migratetype) |
| { |
| unsigned long flags; |
| struct page *page; |
| |
| if (likely(order == 0)) { |
| page = rmqueue_pcplist(preferred_zone, zone, order, |
| gfp_flags, migratetype); |
| goto out; |
| } |
| |
| /* |
| * We most definitely don't want callers attempting to |
| * allocate greater than order-1 page units with __GFP_NOFAIL. |
| */ |
| WARN_ON_ONCE((gfp_flags & __GFP_NOFAIL) && (order > 1)); |
| spin_lock_irqsave(&zone->lock, flags); |
| |
| do { |
| page = NULL; |
| if (alloc_flags & ALLOC_HARDER) { |
| page = __rmqueue_smallest(zone, order, MIGRATE_HIGHATOMIC); |
| if (page) |
| trace_mm_page_alloc_zone_locked(page, order, migratetype); |
| } |
| if (!page) |
| page = __rmqueue(zone, order, migratetype); |
| } while (page && check_new_pages(page, order)); |
| spin_unlock(&zone->lock); |
| if (!page) |
| goto failed; |
| __mod_zone_freepage_state(zone, -(1 << order), |
| get_pcppage_migratetype(page)); |
| |
| __count_zid_vm_events(PGALLOC, page_zonenum(page), 1 << order); |
| zone_statistics(preferred_zone, zone); |
| local_irq_restore(flags); |
| |
| out: |
| VM_BUG_ON_PAGE(page && bad_range(zone, page), page); |
| return page; |
| |
| failed: |
| local_irq_restore(flags); |
| return NULL; |
| } |
| |
| #ifdef CONFIG_FAIL_PAGE_ALLOC |
| |
| static struct { |
| struct fault_attr attr; |
| |
| bool ignore_gfp_highmem; |
| bool ignore_gfp_reclaim; |
| u32 min_order; |
| } fail_page_alloc = { |
| .attr = FAULT_ATTR_INITIALIZER, |
| .ignore_gfp_reclaim = true, |
| .ignore_gfp_highmem = true, |
| .min_order = 1, |
| }; |
| |
| static int __init setup_fail_page_alloc(char *str) |
| { |
| return setup_fault_attr(&fail_page_alloc.attr, str); |
| } |
| __setup("fail_page_alloc=", setup_fail_page_alloc); |
| |
| static bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
| { |
| if (order < fail_page_alloc.min_order) |
| return false; |
| if (gfp_mask & __GFP_NOFAIL) |
| return false; |
| if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM)) |
| return false; |
| if (fail_page_alloc.ignore_gfp_reclaim && |
| (gfp_mask & __GFP_DIRECT_RECLAIM)) |
| return false; |
| |
| return should_fail(&fail_page_alloc.attr, 1 << order); |
| } |
| |
| #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| |
| static int __init fail_page_alloc_debugfs(void) |
| { |
| umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
| struct dentry *dir; |
| |
| dir = fault_create_debugfs_attr("fail_page_alloc", NULL, |
| &fail_page_alloc.attr); |
| if (IS_ERR(dir)) |
| return PTR_ERR(dir); |
| |
| if (!debugfs_create_bool("ignore-gfp-wait", mode, dir, |
| &fail_page_alloc.ignore_gfp_reclaim)) |
| goto fail; |
| if (!debugfs_create_bool("ignore-gfp-highmem", mode, dir, |
| &fail_page_alloc.ignore_gfp_highmem)) |
| goto fail; |
| if (!debugfs_create_u32("min-order", mode, dir, |
| &fail_page_alloc.min_order)) |
| goto fail; |
| |
| return 0; |
| fail: |
| debugfs_remove_recursive(dir); |
| |
| return -ENOMEM; |
| } |
| |
| late_initcall(fail_page_alloc_debugfs); |
| |
| #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |
| |
| #else /* CONFIG_FAIL_PAGE_ALLOC */ |
| |
| static inline bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order) |
| { |
| return false; |
| } |
| |
| #endif /* CONFIG_FAIL_PAGE_ALLOC */ |
| |
| /* |
| * Return true if free base pages are above 'mark'. For high-order checks it |
| * will return true of the order-0 watermark is reached and there is at least |
| * one free page of a suitable size. Checking now avoids taking the zone lock |
| * to check in the allocation paths if no pages are free. |
| */ |
| bool __zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
| int classzone_idx, unsigned int alloc_flags, |
| long free_pages) |
| { |
| long min = mark; |
| int o; |
| const bool alloc_harder = (alloc_flags & (ALLOC_HARDER|ALLOC_OOM)); |
| |
| /* free_pages may go negative - that's OK */ |
| free_pages -= (1 << order) - 1; |
| |
| if (alloc_flags & ALLOC_HIGH) |
| min -= min / 2; |
| |
| /* |
| * If the caller does not have rights to ALLOC_HARDER then subtract |
| * the high-atomic reserves. This will over-estimate the size of the |
| * atomic reserve but it avoids a search. |
| */ |
| if (likely(!alloc_harder)) { |
| free_pages -= z->nr_reserved_highatomic; |
| } else { |
| /* |
| * OOM victims can try even harder than normal ALLOC_HARDER |
| * users on the grounds that it's definitely going to be in |
| * the exit path shortly and free memory. Any allocation it |
| * makes during the free path will be small and short-lived. |
| */ |
| if (alloc_flags & ALLOC_OOM) |
| min -= min / 2; |
| else |
| min -= min / 4; |
| } |
| |
| |
| #ifdef CONFIG_CMA |
| /* If allocation can't use CMA areas don't use free CMA pages */ |
| if (!(alloc_flags & ALLOC_CMA)) |
| free_pages -= zone_page_state(z, NR_FREE_CMA_PAGES); |
| #endif |
| |
| /* |
| * Check watermarks for an order-0 allocation request. If these |
| * are not met, then a high-order request also cannot go ahead |
| * even if a suitable page happened to be free. |
| */ |
| if (free_pages <= min + z->lowmem_reserve[classzone_idx]) |
| return false; |
| |
| /* If this is an order-0 request then the watermark is fine */ |
| if (!order) |
| return true; |
| |
| /* For a high-order request, check at least one suitable page is free */ |
| for (o = order; o < MAX_ORDER; o++) { |
| struct free_area *area = &z->free_area[o]; |
| int mt; |
| |
| if (!area->nr_free) |
| continue; |
| |
| if (alloc_harder) |
| return true; |
| |
| for (mt = 0; mt < MIGRATE_PCPTYPES; mt++) { |
| if (!list_empty(&area->free_list[mt])) |
| return true; |
| } |
| |
| #ifdef CONFIG_CMA |
| if ((alloc_flags & ALLOC_CMA) && |
| !list_empty(&area->free_list[MIGRATE_CMA])) { |
| return true; |
| } |
| #endif |
| } |
| return false; |
| } |
| |
| bool zone_watermark_ok(struct zone *z, unsigned int order, unsigned long mark, |
| int classzone_idx, unsigned int alloc_flags) |
| { |
| return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags, |
| zone_page_state(z, NR_FREE_PAGES)); |
| } |
| |
| static inline bool zone_watermark_fast(struct zone *z, unsigned int order, |
| unsigned long mark, int classzone_idx, unsigned int alloc_flags) |
| { |
| long free_pages = zone_page_state(z, NR_FREE_PAGES); |
| long cma_pages = 0; |
| |
| #ifdef CONFIG_CMA |
| /* If allocation can't use CMA areas don't use free CMA pages */ |
| if (!(alloc_flags & ALLOC_CMA)) |
| cma_pages = zone_page_state(z, NR_FREE_CMA_PAGES); |
| #endif |
| |
| /* |
| * Fast check for order-0 only. If this fails then the reserves |
| * need to be calculated. There is a corner case where the check |
| * passes but only the high-order atomic reserve are free. If |
| * the caller is !atomic then it'll uselessly search the free |
| * list. That corner case is then slower but it is harmless. |
| */ |
| if (!order && (free_pages - cma_pages) > mark + z->lowmem_reserve[classzone_idx]) |
| return true; |
| |
| return __zone_watermark_ok(z, order, mark, classzone_idx, alloc_flags, |
| free_pages); |
| } |
| |
| bool zone_watermark_ok_safe(struct zone *z, unsigned int order, |
| unsigned long mark, int classzone_idx) |
| { |
| long free_pages = zone_page_state(z, NR_FREE_PAGES); |
| |
| if (z->percpu_drift_mark && free_pages < z->percpu_drift_mark) |
| free_pages = zone_page_state_snapshot(z, NR_FREE_PAGES); |
| |
| return __zone_watermark_ok(z, order, mark, classzone_idx, 0, |
| free_pages); |
| } |
| |
| #ifdef CONFIG_NUMA |
| static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
| { |
| return node_distance(zone_to_nid(local_zone), zone_to_nid(zone)) <= |
| RECLAIM_DISTANCE; |
| } |
| #else /* CONFIG_NUMA */ |
| static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) |
| { |
| return true; |
| } |
| #endif /* CONFIG_NUMA */ |
| |
| /* |
| * get_page_from_freelist goes through the zonelist trying to allocate |
| * a page. |
| */ |
| static struct page * |
| get_page_from_freelist(gfp_t gfp_mask, unsigned int order, int alloc_flags, |
| const struct alloc_context *ac) |
| { |
| struct zoneref *z = ac->preferred_zoneref; |
| struct zone *zone; |
| struct pglist_data *last_pgdat_dirty_limit = NULL; |
| |
| /* |
| * Scan zonelist, looking for a zone with enough free. |
| * See also __cpuset_node_allowed() comment in kernel/cpuset.c. |
| */ |
| for_next_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, |
| ac->nodemask) { |
| struct page *page; |
| unsigned long mark; |
| |
| if (cpusets_enabled() && |
| (alloc_flags & ALLOC_CPUSET) && |
| !__cpuset_zone_allowed(zone, gfp_mask)) |
| continue; |
| /* |
| * When allocating a page cache page for writing, we |
| * want to get it from a node that is within its dirty |
| * limit, such that no single node holds more than its |
| * proportional share of globally allowed dirty pages. |
| * The dirty limits take into account the node's |
| * lowmem reserves and high watermark so that kswapd |
| * should be able to balance it without having to |
| * write pages from its LRU list. |
| * |
| * XXX: For now, allow allocations to potentially |
| * exceed the per-node dirty limit in the slowpath |
| * (spread_dirty_pages unset) before going into reclaim, |
| * which is important when on a NUMA setup the allowed |
| * nodes are together not big enough to reach the |
| * global limit. The proper fix for these situations |
| * will require awareness of nodes in the |
| * dirty-throttling and the flusher threads. |
| */ |
| if (ac->spread_dirty_pages) { |
| if (last_pgdat_dirty_limit == zone->zone_pgdat) |
| continue; |
| |
| if (!node_dirty_ok(zone->zone_pgdat)) { |
| last_pgdat_dirty_limit = zone->zone_pgdat; |
| continue; |
| } |
| } |
| |
| mark = zone->watermark[alloc_flags & ALLOC_WMARK_MASK]; |
| if (!zone_watermark_fast(zone, order, mark, |
| ac_classzone_idx(ac), alloc_flags)) { |
| int ret; |
| |
| /* Checked here to keep the fast path fast */ |
| BUILD_BUG_ON(ALLOC_NO_WATERMARKS < NR_WMARK); |
| if (alloc_flags & ALLOC_NO_WATERMARKS) |
| goto try_this_zone; |
| |
| if (node_reclaim_mode == 0 || |
| !zone_allows_reclaim(ac->preferred_zoneref->zone, zone)) |
| continue; |
| |
| ret = node_reclaim(zone->zone_pgdat, gfp_mask, order); |
| switch (ret) { |
| case NODE_RECLAIM_NOSCAN: |
| /* did not scan */ |
| continue; |
| case NODE_RECLAIM_FULL: |
| /* scanned but unreclaimable */ |
| continue; |
| default: |
| /* did we reclaim enough */ |
| if (zone_watermark_ok(zone, order, mark, |
| ac_classzone_idx(ac), alloc_flags)) |
| goto try_this_zone; |
| |
| continue; |
| } |
| } |
| |
| try_this_zone: |
| page = rmqueue(ac->preferred_zoneref->zone, zone, order, |
| gfp_mask, alloc_flags, ac->migratetype); |
| if (page) { |
| prep_new_page(page, order, gfp_mask, alloc_flags); |
| |
| /* |
| * If this is a high-order atomic allocation then check |
| * if the pageblock should be reserved for the future |
| */ |
| if (unlikely(order && (alloc_flags & ALLOC_HARDER))) |
| reserve_highatomic_pageblock(page, zone, order); |
| |
| return page; |
| } |
| } |
| |
| return NULL; |
| } |
| |
| /* |
| * Large machines with many possible nodes should not always dump per-node |
| * meminfo in irq context. |
| */ |
| static inline bool should_suppress_show_mem(void) |
| { |
| bool ret = false; |
| |
| #if NODES_SHIFT > 8 |
| ret = in_interrupt(); |
| #endif |
| return ret; |
| } |
| |
| static void warn_alloc_show_mem(gfp_t gfp_mask, nodemask_t *nodemask) |
| { |
| unsigned int filter = SHOW_MEM_FILTER_NODES; |
| static DEFINE_RATELIMIT_STATE(show_mem_rs, HZ, 1); |
| |
| if (should_suppress_show_mem() || !__ratelimit(&show_mem_rs)) |
| return; |
| |
| /* |
| * This documents exceptions given to allocations in certain |
| * contexts that are allowed to allocate outside current's set |
| * of allowed nodes. |
| */ |
| if (!(gfp_mask & __GFP_NOMEMALLOC)) |
| if (tsk_is_oom_victim(current) || |
| (current->flags & (PF_MEMALLOC | PF_EXITING))) |
| filter &= ~SHOW_MEM_FILTER_NODES; |
| if (in_interrupt() || !(gfp_mask & __GFP_DIRECT_RECLAIM)) |
| filter &= ~SHOW_MEM_FILTER_NODES; |
| |
| show_mem(filter, nodemask); |
| } |
| |
| void warn_alloc(gfp_t gfp_mask, nodemask_t *nodemask, const char *fmt, ...) |
| { |
| struct va_format vaf; |
| va_list args; |
| static DEFINE_RATELIMIT_STATE(nopage_rs, DEFAULT_RATELIMIT_INTERVAL, |
| DEFAULT_RATELIMIT_BURST); |
| |
| if ((gfp_mask & __GFP_NOWARN) || !__ratelimit(&nopage_rs)) |
| return; |
| |
| pr_warn("%s: ", current->comm); |
| |
| va_start(args, fmt); |
| vaf.fmt = fmt; |
<