cregit-Linux how code gets into the kernel

Release 4.13 mm/page_alloc.c

Directory: mm
/*
 *  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/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; }

Contributors

PersonTokensPropCommitsCommitProp
Vlastimil Babka17100.00%1100.00%
Total17100.00%1100.00%


static inline void set_pcppage_migratetype(struct page *page, int migratetype) { page->index = migratetype; }

Contributors

PersonTokensPropCommitsCommitProp
Vlastimil Babka21100.00%1100.00%
Total21100.00%1100.00%

#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; } }

Contributors

PersonTokensPropCommitsCommitProp
Rafael J. Wysocki31100.00%2100.00%
Total31100.00%2100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Rafael J. Wysocki3085.71%266.67%
Mel Gorman514.29%133.33%
Total35100.00%3100.00%


bool pm_suspended_storage(void) { if ((gfp_allowed_mask & (__GFP_IO | __GFP_FS)) == (__GFP_IO | __GFP_FS)) return false; return true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman31100.00%2100.00%
Total31100.00%2100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Michal Hocko6378.75%150.00%
Mel Gorman1721.25%150.00%
Total80100.00%2100.00%

/* 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman42100.00%3100.00%
Total42100.00%3100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7695.00%133.33%
Michal Hocko33.75%133.33%
Li Zhang11.25%133.33%
Total80100.00%3100.00%

#else
static inline void reset_deferred_meminit(pg_data_t *pgdat) { }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10100.00%1100.00%
Total10100.00%1100.00%


static inline bool early_page_uninitialised(unsigned long pfn) { return false; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman14100.00%2100.00%
Total14100.00%2100.00%


static inline bool update_defer_init(pg_data_t *pgdat, unsigned long pfn, unsigned long zone_end, unsigned long *nr_initialised) { return true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman27100.00%1100.00%
Total27100.00%1100.00%

#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 */ }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman42100.00%3100.00%
Total42100.00%3100.00%


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 */ }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman4263.64%111.11%
Dave Hansen1218.18%333.33%
William Lee Irwin III57.58%111.11%
Cody P Schafer46.06%111.11%
Linus Torvalds (pre-git)11.52%111.11%
Andrew Morton11.52%111.11%
Rik Van Riel11.52%111.11%
Total66100.00%9100.00%

/** * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman8081.63%125.00%
Dave Hansen1212.24%125.00%
William Lee Irwin III33.06%125.00%
Kamezawa Hiroyuki33.06%125.00%
Total98100.00%4100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2262.86%133.33%
Dave Hansen1131.43%133.33%
William Lee Irwin III25.71%133.33%
Total35100.00%3100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1657.14%150.00%
Nicholas Piggin1242.86%150.00%
Total28100.00%2100.00%

/** * 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; } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman11768.02%17.14%
Hugh Dickins2514.53%214.29%
Andrew Morton126.98%214.29%
Dave Hansen52.91%17.14%
Vlastimil Babka42.33%214.29%
Fengguang Wu31.74%17.14%
Randy Dunlap21.16%17.14%
Dave Jones10.58%17.14%
Rusty Russell10.58%17.14%
William Lee Irwin III10.58%17.14%
Nicholas Piggin10.58%17.14%
Total172100.00%14100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2967.44%133.33%
Hugh Dickins1330.23%133.33%
Christoph Lameter12.33%133.33%
Total43100.00%3100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman8171.05%112.50%
Andrew Morton108.77%112.50%
Andy Whitcroft97.89%112.50%
Kirill A. Shutemov97.89%337.50%
Youquan Song32.63%112.50%
David Rientjes21.75%112.50%
Total114100.00%8100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2453.33%133.33%
JoonSoo Kim1635.56%133.33%
Christian Bornträger511.11%133.33%
Total45100.00%3100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman3168.89%125.00%
JoonSoo Kim1328.89%250.00%
Matthias Kaehlcke12.22%125.00%
Total45100.00%4100.00%

#else
static inline int __maybe_unused bad_range(struct zone *zone, struct page *page) { return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1571.43%133.33%
JoonSoo Kim523.81%133.33%
Matthias Kaehlcke14.76%133.33%
Total21100.00%3100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman12072.73%125.00%
Stanislaw Gruszka2313.94%125.00%
JoonSoo Kim2213.33%250.00%
Total165100.00%4100.00%

/* * 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)); }

Contributors

PersonTokensPropCommitsCommitProp
Stanislaw Gruszka1050.00%125.00%
JoonSoo Kim735.00%250.00%
Mel Gorman315.00%125.00%
Total20100.00%4100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6061.22%120.00%
Kamezawa Hiroyuki2020.41%120.00%
Stanislaw Gruszka77.14%120.00%
Hugh Dickins66.12%120.00%
JoonSoo Kim55.10%120.00%
Total98100.00%5100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1655.17%233.33%
Nicholas Piggin620.69%116.67%
Kamezawa Hiroyuki310.34%116.67%
Minfei Huang310.34%116.67%
Stanislaw Gruszka13.45%116.67%
Total29100.00%6100.00%

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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1860.00%133.33%
JoonSoo Kim930.00%133.33%
Stanislaw Gruszka310.00%133.33%
Total30100.00%3100.00%


static void init_debug_guardpage(void) { if (!debug_pagealloc_enabled()) return; if (!debug_guardpage_minorder()) return; _debug_guardpage_enabled = true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1661.54%125.00%
JoonSoo Kim726.92%125.00%
Stanislaw Gruszka27.69%125.00%
Nicholas Piggin13.85%125.00%
Total26100.00%4100.00%

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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5186.44%250.00%
Kamezawa Hiroyuki46.78%125.00%
Weijie Yang46.78%125.00%
Total59100.00%4100.00%

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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman4541.28%417.39%
JoonSoo Kim2522.94%313.04%
Andrew Morton98.26%417.39%
Yang Shi87.34%14.35%
Linus Torvalds (pre-git)65.50%417.39%
Vlastimil Babka43.67%14.35%
Kirill A. Shutemov32.75%14.35%
Cody P Schafer32.75%14.35%
Corrado Zoccolo21.83%14.35%
Kamezawa Hiroyuki21.83%14.35%
Hugh Dickins10.92%14.35%
Sasha Levin10.92%14.35%
Total109100.00%23100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5358.89%17.14%
Stanislaw Gruszka1213.33%17.14%
Yang Shi910.00%17.14%
Linus Torvalds (pre-git)44.44%321.43%
Kamezawa Hiroyuki22.22%17.14%
Dave Hansen22.22%17.14%
Andy Whitcroft22.22%17.14%
Bartlomiej Zolnierkiewicz22.22%17.14%
Sasha Levin11.11%17.14%
Martin J. Bligh11.11%17.14%
Andrew Morton11.11%17.14%
Cho KyongHo11.11%17.14%
Total90100.00%14100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1970.37%125.00%
JoonSoo Kim622.22%125.00%
Martin J. Bligh13.70%125.00%
Kamezawa Hiroyuki13.70%125.00%
Total27100.00%4100.00%


static inline void clear_page_guard(struct zone *zone, struct page *page, unsigned int order, int migratetype) {}

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman23100.00%1100.00%
Total23100.00%1100.00%

#endif
static inline void set_page_order(struct page *page, unsigned int order) { set_page_private(page, order); __SetPageBuddy(page); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2382.14%125.00%
Kamezawa Hiroyuki27.14%125.00%
Linus Torvalds (pre-git)27.14%125.00%
Stanislaw Gruszka13.57%125.00%
Total28100.00%4100.00%


static inline void rmv_page_order(struct page *page) { __ClearPageBuddy(page); set_page_private(page, 0); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1979.17%125.00%
Dave Hansen28.33%125.00%
Linus Torvalds (pre-git)28.33%125.00%
Kamezawa Hiroyuki14.17%125.00%
Total24100.00%4100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7967.52%114.29%
Vlastimil Babka1512.82%114.29%
Corrado Zoccolo1411.97%114.29%
Kamezawa Hiroyuki54.27%114.29%
Linus Torvalds (pre-git)32.56%228.57%
Andrew Morton10.85%114.29%
Total117100.00%7100.00%

/* * 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++; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman34269.51%1038.46%
Vlastimil Babka459.15%311.54%
Andrew Morton306.10%27.69%
Dave Hansen265.28%13.85%
JoonSoo Kim183.66%27.69%
Nicholas Piggin81.63%27.69%
Bartlomiej Zolnierkiewicz61.22%13.85%
Christoph Lameter51.02%13.85%
Tony Luck51.02%13.85%
Namhyung Kim30.61%13.85%
Kirill A. Shutemov30.61%13.85%
Daisuke Nishimura10.20%13.85%
Total492100.00%26100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5568.75%116.67%
Kirill A. Shutemov2531.25%583.33%
Total80100.00%6100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman8667.72%125.00%
Kirill A. Shutemov4132.28%375.00%
Total127100.00%4100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Robin Holt1952.78%150.00%
Mel Gorman1747.22%150.00%
Total36100.00%2100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman187100.00%1100.00%
Total187100.00%1100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman26188.47%233.33%
Vladimir Davydov175.76%116.67%
Kirill A. Shutemov93.05%116.67%
seokhoon.yoon72.37%116.67%
MinChan Kim10.34%116.67%
Total295100.00%6100.00%

#ifdef CONFIG_DEBUG_VM
static inline bool free_pcp_prepare(struct page *page) { return free_pages_prepare(page, 0, true); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman22100.00%2100.00%
Total22100.00%2100.00%


static inline bool bulkfree_pcp_prepare(struct page *page) { return false; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman15100.00%1100.00%
Total15100.00%1100.00%

#else
static bool free_pcp_prepare(struct page *page) { return free_pages_prepare(page, 0, false); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman21100.00%1100.00%
Total21100.00%1100.00%


static bool bulkfree_pcp_prepare(struct page *page) { return free_pages_check(page); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman17100.00%1100.00%
Total17100.00%1100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman20489.87%457.14%
Nathan Zimmer114.85%114.29%
Robin Holt83.52%114.29%
Kirill A. Shutemov41.76%114.29%
Total227100.00%7100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5769.51%333.33%
Andrew Morton1113.41%111.11%
Andrea Arcangeli44.88%111.11%
Kirill A. Shutemov44.88%111.11%
Yu Zhao33.66%111.11%
Andrey Ryabinin22.44%111.11%
Nicholas Piggin11.22%111.11%
Total82100.00%9100.00%


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 }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5667.47%327.27%
Motohiro Kosaki1315.66%19.09%
Nicholas Piggin910.84%327.27%
Linus Torvalds (pre-git)33.61%327.27%
Andrew Morton22.41%19.09%
Total83100.00%11100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2678.79%266.67%
David Howells721.21%133.33%
Total33100.00%3100.00%

#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
static void 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6972.63%116.67%
David Howells1818.95%116.67%
Yinghai Lu33.16%116.67%
Nicholas Piggin33.16%116.67%
Johannes Weiner11.05%116.67%
Jiang Liu11.05%116.67%
Total95100.00%6100.00%

#else
static inline void init_reserved_page(unsigned long pfn) { }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10100.00%2100.00%
Total10100.00%2100.00%

#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); } } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7897.50%375.00%
Stefan Bader22.50%125.00%
Total80100.00%4100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman88100.00%5100.00%
Total88100.00%5100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman114100.00%4100.00%
Total114100.00%4100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman54100.00%5100.00%
Total54100.00%5100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman4285.71%240.00%
Nicolai Stange48.16%120.00%
JoonSoo Kim24.08%120.00%
Matthias Kaehlcke12.04%120.00%
Total49100.00%5100.00%

/* 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman26100.00%2100.00%
Total26100.00%2100.00%

#else
static inline bool __meminit early_pfn_in_nid(unsigned long pfn, int node) { return true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman18100.00%1100.00%
Total18100.00%1100.00%


static inline bool __meminit __maybe_unused meminit_pfn_in_nid(unsigned long pfn, int node, struct mminit_pfnnid_cache *state) { return true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2395.83%150.00%
Matthias Kaehlcke14.17%150.00%
Total24100.00%2100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman3085.71%150.00%
JoonSoo Kim514.29%150.00%
Total35100.00%2100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6461.54%116.67%
Michal Nazarewicz2927.88%233.33%
Michal Hocko98.65%116.67%
Jiang Liu10.96%116.67%
Marek Szyprowski10.96%116.67%
Total104100.00%6100.00%


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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5564.71%19.09%
Linus Torvalds (pre-git)1315.29%327.27%
Andrew Morton78.24%218.18%
Stanislaw Gruszka67.06%19.09%
Nicholas Piggin22.35%218.18%
Bartlomiej Zolnierkiewicz11.18%19.09%
Sasha Levin11.18%19.09%
Total85100.00%11100.00%


void clear_zone_contiguous(struct zone *zone) { zone->contiguous = false; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1168.75%150.00%
Andrew Morton531.25%150.00%
Total16100.00%2100.00%

#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); } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5851.33%218.18%
Xishi Qiu2925.66%19.09%
Dave Hansen108.85%19.09%
Andrew Morton76.19%327.27%
Naoya Horiguchi43.54%19.09%
Kirill A. Shutemov32.65%19.09%
Fengguang Wu10.88%19.09%
Hugh Dickins10.88%19.09%
Total113100.00%11100.00%

/* 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1770.83%150.00%
Laura Abbott729.17%150.00%
Total24100.00%2100.00%

/* 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman45684.92%211.11%
Fengguang Wu193.54%15.56%
Xishi Qiu173.17%15.56%
Anisse Astier101.86%15.56%
Laura Abbott81.49%211.11%
Nicholas Piggin61.12%211.11%
Andrew Morton40.74%211.11%
Vlastimil Babka40.74%15.56%
Geliang Tang30.56%15.56%
JoonSoo Kim30.56%15.56%
Hugh Dickins30.56%15.56%
Linus Torvalds20.37%15.56%
Andrey Ryabinin10.19%15.56%
Michal Hocko10.19%15.56%
Total537100.00%18100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6375.00%240.00%
JoonSoo Kim1113.10%120.00%
Pavel Tatashin910.71%120.00%
Michal Nazarewicz11.19%120.00%
Total84100.00%5100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman11495.00%250.00%
Adam Litke54.17%125.00%
Sasha Levin10.83%125.00%
Total120100.00%4100.00%

#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); } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman12696.18%250.00%
Cody P Schafer32.29%125.00%
JoonSoo Kim21.53%125.00%
Total131100.00%4100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10367.76%233.33%
Srivatsa S. Bhat2013.16%116.67%
JoonSoo Kim159.87%116.67%
Naoya Horiguchi85.26%116.67%
Vlastimil Babka63.95%116.67%
Total152100.00%6100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Vlastimil Babka3389.19%133.33%
JoonSoo Kim38.11%133.33%
Mel Gorman12.70%133.33%
Total37100.00%3100.00%


static inline bool free_pages_prezeroed(void) { return IS_ENABLED(CONFIG_PAGE_POISONING_ZERO) && page_poisoning_enabled(); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1477.78%125.00%
JoonSoo Kim211.11%125.00%
Srivatsa S. Bhat15.56%125.00%
Vinayak Menon15.56%125.00%
Total18100.00%4100.00%

#ifdef CONFIG_DEBUG_VM
static bool check_pcp_refill(struct page *page) { return false; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman14100.00%1100.00%
Total14100.00%1100.00%


static bool check_new_pcp(struct page *page) { return check_new_page(page); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman17100.00%1100.00%
Total17100.00%1100.00%

#else
static bool check_pcp_refill(struct page *page) { return check_new_page(page); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman17100.00%1100.00%
Total17100.00%1100.00%


static bool check_new_pcp(struct page *page) { return false; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman14100.00%1100.00%
Total14100.00%1100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman62100.00%3100.00%
Total62100.00%3100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
JoonSoo Kim75100.00%1100.00%
Total75100.00%1100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10297.14%360.00%
JoonSoo Kim21.90%120.00%
Vinayak Menon10.95%120.00%
Total105100.00%5100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman12593.98%266.67%
Geliang Tang86.02%133.33%
Total133100.00%3100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman27100.00%2100.00%
Total27100.00%2100.00%

#else
static inline struct page *__rmqueue_cma_fallback(struct zone *zone, unsigned int order) { return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman21100.00%4100.00%
Total21100.00%4100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman13568.88%654.55%
Vlastimil Babka3919.90%218.18%
Ard Biesheuvel168.16%19.09%
JoonSoo Kim52.55%19.09%
Srivatsa S. Bhat10.51%19.09%
Total196100.00%11100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman9378.15%433.33%
JoonSoo Kim108.40%18.33%
Vlastimil Babka65.04%18.33%
Linus Torvalds (pre-git)54.20%325.00%
Andrew Morton43.36%216.67%
Nicholas Piggin10.84%18.33%
Total119100.00%12100.00%


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; } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman3576.09%466.67%
Andrew Morton919.57%116.67%
Nicholas Piggin24.35%116.67%
Total46100.00%6100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman4187.23%250.00%
Linus Torvalds (pre-git)36.38%125.00%
Bartlomiej Zolnierkiewicz36.38%125.00%
Total47100.00%4100.00%

/* * 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]); }

Contributors

PersonTokensPropCommitsCommitProp
Vlastimil Babka13061.90%218.18%
Mel Gorman4923.33%19.09%
Christoph Lameter2210.48%545.45%
Michal Nazarewicz31.43%19.09%
Motohiro Kosaki31.43%19.09%
Cody P Schafer31.43%19.09%
Total210100.00%11100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10080.65%114.29%
Rusty Russell1411.29%114.29%
David Rientjes43.23%114.29%
Nicholas Piggin21.61%114.29%
Vlastimil Babka21.61%114.29%
Christoph Lameter21.61%228.57%
Total124100.00%7100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10776.98%120.00%
Vlastimil Babka2215.83%240.00%
Christoph Lameter64.32%120.00%
Xishi Qiu42.88%120.00%
Total139100.00%5100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman12958.37%111.11%
MinChan Kim3415.38%333.33%
Gilad Ben-Yossef2812.67%111.11%
Vlastimil Babka2310.41%222.22%
Christoph Lameter62.71%111.11%
Xishi Qiu10.45%111.11%
Total221100.00%9100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman9341.89%18.33%
Vlastimil Babka9140.99%216.67%
Pavel Machek188.11%325.00%
Rafael J. Wysocki73.15%18.33%
Andrew Morton52.25%216.67%
Geliang Tang41.80%18.33%
JoonSoo Kim31.35%18.33%
Rusty Russell10.45%18.33%
Total222100.00%12100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6772.83%444.44%
Andrew Morton1213.04%111.11%
Vlastimil Babka77.61%111.11%
Hugh Dickins33.26%111.11%
Nicholas Piggin22.17%111.11%
Motohiro Kosaki11.09%111.11%
Total92100.00%9100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman14477.84%646.15%
Christoph Lameter2513.51%323.08%
Andrew Morton126.49%17.69%
Cody P Schafer21.08%17.69%
Nicholas Piggin10.54%17.69%
MinChan Kim10.54%17.69%
Total185100.00%13100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5672.73%150.00%
Konstantin Khlebnikov2127.27%150.00%
Total77100.00%2100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5364.63%116.67%
Nicholas Piggin1518.29%116.67%
JoonSoo Kim1012.20%233.33%
Vegard Nossum33.66%116.67%
Sasha Levin11.22%116.67%
Total82100.00%6100.00%

/* * 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); } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2796.43%375.00%
Michal Nazarewicz13.57%125.00%
Total28100.00%4100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman33100.00%2100.00%
Total33100.00%2100.00%


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(); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1669.57%150.00%
Michal Hocko730.43%150.00%
Total23100.00%2100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman21994.40%480.00%
Michal Hocko135.60%120.00%
Total232100.00%5100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman11453.77%631.58%
Andrew Morton3717.45%210.53%
Chen Yu3616.98%15.26%
Pavel Machek94.25%210.53%
Christoph Lameter83.77%315.79%
Nicholas Piggin52.36%315.79%
Michal Hocko20.94%15.26%
David Shaohua Li10.47%15.26%
Total212100.00%19100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman19488.18%753.85%
Johannes Weiner135.91%323.08%
Andrew Morton83.64%17.69%
Motohiro Kosaki41.82%17.69%
Xishi Qiu10.45%17.69%
Total220100.00%13100.00%

/* * 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); } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman3676.60%125.00%
Nicholas Piggin510.64%125.00%
Andrew Morton510.64%125.00%
Andi Kleen12.13%125.00%
Total47100.00%4100.00%

/* * 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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7678.35%112.50%
Nicholas Piggin66.19%112.50%
Akinobu Mita66.19%225.00%
Sasha Levin33.09%112.50%
Andrew Morton33.09%112.50%
JoonSoo Kim22.06%112.50%
Don Mullis11.03%112.50%
Total97100.00%8100.00%

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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman17081.34%112.50%
Akinobu Mita2813.40%225.00%
Vlastimil Babka41.91%225.00%
Xishi Qiu41.91%112.50%
MinChan Kim20.96%112.50%
zhong jiang10.48%112.50%
Total209100.00%8100.00%

/* * 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 zone_stat_item local_stat = NUMA_LOCAL; if (z->node != numa_node_id()) local_stat = NUMA_OTHER; if (z->node == preferred_zone->node) __inc_zone_state(z, NUMA_HIT); else { __inc_zone_state(z, NUMA_MISS); __inc_zone_state(preferred_zone, NUMA_FOREIGN); } __inc_zone_state(z, local_stat); #endif }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman5769.51%133.33%
Michal Hocko1619.51%133.33%
Akinobu Mita910.98%133.33%
Total82100.00%3100.00%

/* 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman11384.96%675.00%
Nicholas Piggin118.27%112.50%
Vlastimil Babka96.77%112.50%
Total133100.00%8100.00%

/* 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman137100.00%3100.00%
Total137100.00%3100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman25099.21%787.50%
Nicholas Piggin20.79%112.50%
Total252100.00%8100.00%

#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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1254.55%266.67%
David Rientjes1045.45%133.33%
Total22100.00%3100.00%

__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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6788.16%150.00%
Johannes Weiner911.84%150.00%
Total76100.00%2100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman12197.58%250.00%
Johannes Weiner21.61%125.00%
David Rientjes10.81%125.00%
Total124100.00%4100.00%

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; }

Contributors

PersonTokensPropCommitsCommitProp
Rohit Seth847.06%133.33%
Mel Gorman741.18%133.33%
Martin Hicks211.76%133.33%
Total17100.00%3100.00%

#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); /* 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 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman22388.14%743.75%
Johannes Weiner135.14%318.75%
Christoph Lameter62.37%212.50%
Nicholas Piggin41.58%16.25%
David Rientjes31.19%16.25%
Rohit Seth31.19%16.25%
Martin Hicks10.40%16.25%
Total253100.00%16100.00%


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)); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman4495.65%250.00%
Nicholas Piggin12.17%125.00%
Rohit Seth12.17%125.00%
Total46100.00%4100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10094.34%350.00%
Vlastimil Babka43.77%116.67%
Christoph Lameter10.94%116.67%
Rohit Seth10.94%116.67%
Total106100.00%6100.00%


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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman6494.12%350.00%
Martin Hicks34.41%233.33%
Vlastimil Babka11.47%116.67%
Total68100.00%6100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman2784.38%133.33%
Dave Hansen412.50%133.33%
Gavin Shan13.12%133.33%
Total32100.00%3100.00%

#else /* CONFIG_NUMA */
static bool zone_allows_reclaim(struct zone *local_zone, struct zone *zone) { return true; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman1894.74%150.00%
Dave Hansen15.26%150.00%
Total19100.00%2100.00%

#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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman24977.57%1238.71%
Vlastimil Babka226.85%619.35%
Michal Hocko175.30%39.68%
Linus Torvalds (pre-git)92.80%13.23%
Linus Torvalds61.87%26.45%
Christoph Lameter61.87%13.23%
David Rientjes51.56%13.23%
Martin Hicks20.62%13.23%
Johannes Weiner20.62%13.23%
Andrew Morton10.31%13.23%
Rik Van Riel10.31%13.23%
Al Viro10.31%13.23%
Total321100.00%31100.00%

/* * 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; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman29100.00%3100.00%
Total29100.00%3100.00%


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 (test_thread_flag(TIF_MEMDIE) || (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); }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7171.00%342.86%
Michal Hocko2727.00%228.57%
Marek Szyprowski11.00%114.29%
Andrew Morton11.00%114.29%
Total100100.00%7100.00%


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; vaf.va = &args; pr_cont("%pV", &vaf); va_end(args); pr_cont(", mode:%#x(%pGg), nodemask=", gfp_mask, &gfp_mask); if (nodemask) pr_cont("%*pbl\n", nodemask_pr_args(nodemask)); else pr_cont("(null)\n"); cpuset_print_current_mems_allowed(); dump_stack(); warn_alloc_show_mem(gfp_mask, nodemask); }

Contributors

PersonTokensPropCommitsCommitProp
Michal Hocko7252.55%444.44%
Mel Gorman4331.39%222.22%
David Rientjes1813.14%111.11%
Rohit Seth32.19%111.11%
Marek Szyprowski10.73%111.11%
Total137100.00%9100.00%


static inline struct page * __alloc_pages_cpuset_fallback(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags, const struct alloc_context *ac) { struct page *page; page = get_page_from_freelist(gfp_mask, order, alloc_flags|ALLOC_CPUSET, ac); /* * fallback to ignore cpuset restriction if our nodes * are depleted */ if (!page) page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); return page; }

Contributors

PersonTokensPropCommitsCommitProp
Michal Hocko6392.65%150.00%
Marek Szyprowski57.35%150.00%
Total68100.00%2100.00%


static inline struct page * __alloc_pages_may_oom(gfp_t gfp_mask, unsigned int order, const struct alloc_context *ac, unsigned long *did_some_progress) { struct oom_control oc = { .zonelist = ac->zonelist, .nodemask = ac->nodemask, .memcg = NULL, .gfp_mask = gfp_mask, .order = order, }; struct page *page; *did_some_progress = 0; /* * Acquire the oom lock. If that fails, somebody else is * making progress for us. */ if (!mutex_trylock(&oom_lock)) { *did_some_progress = 1; schedule_timeout_uninterruptible(1); return NULL; } /* * Go through the zonelist yet one more time, keep very high watermark * here, this is only to catch a parallel oom killing, we must fail if * we're still under heavy pressure. But make sure that this reclaim * attempt shall not depend on __GFP_DIRECT_RECLAIM && !__GFP_NORETRY * allocation which will never fail due to oom_lock already held. */ page = get_page_from_freelist((gfp_mask | __GFP_HARDWALL) & ~__GFP_DIRECT_RECLAIM, order, ALLOC_WMARK_HIGH|ALLOC_CPUSET, ac); if (page) goto out; /* Coredumps can quickly deplete all memory reserves */ if (current->flags & PF_DUMPCORE) goto out; /* The OOM killer will not help higher order allocs */ if (order > PAGE_ALLOC_COSTLY_ORDER) goto out; /* * We have already exhausted all our reclaim opportunities without any * success so it is time to admit defeat. We will skip the OOM killer * because it is very likely that the caller has a more reasonable * fallback than shooting a random task. */ if (gfp_mask & __GFP_RETRY_MAYFAIL) goto out; /* The OOM killer does not needlessly kill tasks for lowmem */ if (ac->high_zoneidx < ZONE_NORMAL) goto out; if (pm_suspended_storage()) goto out; /* * XXX: GFP_NOFS allocations should rather fail than rely on * other request to make a forward progress. * We are in an unfortunate situation where out_of_memory cannot * do much for this context but let's try it to at least get * access to memory reserved if the current task is killed (see * out_of_memory). Once filesystems are ready to handle allocation * failures more gracefully we should just bail out here. */ /* The OOM killer may not free memory on a specific node */ if (gfp_mask & __GFP_THISNODE) goto out; /* Exhausted what can be done so it's blamo time */ if (out_of_memory(&oc) || WARN_ON_ONCE(gfp_mask & __GFP_NOFAIL)) { *did_some_progress = 1; /* * Help non-failing allocations by giving them access to memory * reserves */ if (gfp_mask & __GFP_NOFAIL) page = __alloc_pages_cpuset_fallback(gfp_mask, order, ALLOC_NO_WATERMARKS, ac); } out: mutex_unlock(&oom_lock); return page; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman16165.98%527.78%
Marek Szyprowski2610.66%15.56%
Michal Hocko187.38%316.67%
Linus Torvalds (pre-git)83.28%211.11%
Johannes Weiner83.28%15.56%
Vlastimil Babka72.87%211.11%
Tetsuo Handa62.46%15.56%
Vladimir Davydov52.05%15.56%
Rohit Seth41.64%15.56%
Andrew Morton10.41%15.56%
Total244100.00%18100.00%

/* * Maximum number of compaction retries wit a progress before OOM * killer is consider as the only way to move forward. */ #define MAX_COMPACT_RETRIES 16 #ifdef CONFIG_COMPACTION /* Try memory compaction for high-order allocations before reclaim */
static struct page * __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags, const struct alloc_context *ac, enum compact_priority prio, enum compact_result *compact_result) { struct page *page; unsigned int noreclaim_flag; if (!order) return NULL; noreclaim_flag = memalloc_noreclaim_save(); *compact_result = try_to_compact_pages(gfp_mask, order, alloc_flags, ac, prio); memalloc_noreclaim_restore(noreclaim_flag); if (*compact_result <= COMPACT_INACTIVE) return NULL; /* * At least in one zone compaction wasn't deferred or skipped, so let's * count a compaction stall */ count_vm_event(COMPACTSTALL); page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); if (page) { struct zone *zone = page_zone(page); zone->compact_blockskip_flush = false; compaction_defer_reset(zone, order, true); count_vm_event(COMPACTSUCCESS); return page; } /* * It's bad if compaction run occurs and fails. The most likely reason * is that pages exist, but not enough to satisfy watermarks. */ count_vm_event(COMPACTFAIL); cond_resched(); return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman10868.79%642.86%
Peter Zijlstra1710.83%17.14%
Vlastimil Babka159.55%321.43%
Michal Hocko106.37%214.29%
Andrea Arcangeli42.55%17.14%
Bartlomiej Zolnierkiewicz31.91%17.14%
Total157100.00%14100.00%


static inline bool should_compact_retry(struct alloc_context *ac, int order, int alloc_flags, enum compact_result compact_result, enum compact_priority *compact_priority, int *compaction_retries) { int max_retries = MAX_COMPACT_RETRIES; int min_priority; bool ret = false; int retries = *compaction_retries; enum compact_priority priority = *compact_priority; if (!order) return false; if (compaction_made_progress(compact_result)) (*compaction_retries)++; /* * compaction considers all the zone as desperately out of memory * so it doesn't really make much sense to retry except when the * failure could be caused by insufficient priority */ if (compaction_failed(compact_result)) goto check_priority; /* * make sure the compaction wasn't deferred or didn't bail out early * due to locks contention before we declare that we should give up. * But do not retry if the given zonelist is not suitable for * compaction. */ if (compaction_withdrawn(compact_result)) { ret = compaction_zonelist_suitable(ac, order, alloc_flags); goto out; } /* * !costly requests are much more important than __GFP_RETRY_MAYFAIL * costly ones because they are de facto nofail and invoke OOM * killer to move on while costly can fail and users are ready * to cope with that. 1/4 retries is rather arbitrary but we * would need much more detailed feedback from compaction to * make a better decision. */ if (order > PAGE_ALLOC_COSTLY_ORDER) max_retries /= 4; if (*compaction_retries <= max_retries) { ret = true; goto out; } /* * Make sure there are attempts at the highest priority if we exhausted * all retries or failed at the lower priorities. */ check_priority: min_priority = (order > PAGE_ALLOC_COSTLY_ORDER) ? MIN_COMPACT_COSTLY_PRIORITY : MIN_COMPACT_PRIORITY; if (*compact_priority > min_priority) { (*compact_priority)--; *compaction_retries = 0; ret = true; } out: trace_compact_retry(order, priority, compact_result, retries, max_retries, ret); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Vlastimil Babka14371.86%360.00%
Michal Hocko5628.14%240.00%
Total199100.00%5100.00%

#else
static inline struct page * __alloc_pages_direct_compact(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags, const struct alloc_context *ac, enum compact_priority prio, enum compact_result *compact_result) { *compact_result = COMPACT_SKIPPED; return NULL; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman3376.74%240.00%
Michal Hocko818.60%240.00%
Vlastimil Babka24.65%120.00%
Total43100.00%5100.00%


static inline bool should_compact_retry(struct alloc_context *ac, unsigned int order, int alloc_flags, enum compact_result compact_result, enum compact_priority *compact_priority, int *compaction_retries) { struct zone *zone; struct zoneref *z; if (!order || order > PAGE_ALLOC_COSTLY_ORDER) return false; /* * There are setups with compaction disabled which would prefer to loop * inside the allocator rather than hit the oom killer prematurely. * Let's give them a good hope and keep retrying while the order-0 * watermarks are OK. */ for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, ac->nodemask) { if (zone_watermark_ok(zone, 0, min_wmark_pages(zone), ac_classzone_idx(ac), alloc_flags)) return true; } return false; }

Contributors

PersonTokensPropCommitsCommitProp
Michal Hocko9396.88%360.00%
Vlastimil Babka33.12%240.00%
Total96100.00%5100.00%

#endif /* CONFIG_COMPACTION */ /* Perform direct synchronous page reclaim */
static int __perform_reclaim(gfp_t gfp_mask, unsigned int order, const struct alloc_context *ac) { struct reclaim_state reclaim_state; int progress; unsigned int noreclaim_flag; cond_resched(); /* We now go into synchronous reclaim */ cpuset_memory_pressure_bump(); noreclaim_flag = memalloc_noreclaim_save(); lockdep_set_current_reclaim_state(gfp_mask); reclaim_state.reclaimed_slab = 0; current->reclaim_state = &reclaim_state; progress = try_to_free_pages(ac->zonelist, order, gfp_mask, ac->nodemask); current->reclaim_state = NULL; lockdep_clear_current_reclaim_state(); memalloc_noreclaim_restore(noreclaim_flag); cond_resched(); return progress; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7375.26%952.94%
Vlastimil Babka1616.49%317.65%
Rohit Seth22.06%15.88%
Andrew Morton22.06%15.88%
Johannes Weiner22.06%15.88%
Peter Zijlstra11.03%15.88%
Linus Torvalds (pre-git)11.03%15.88%
Total97100.00%17100.00%

/* The really slow allocator path where we enter direct reclaim */
static inline struct page * __alloc_pages_direct_reclaim(gfp_t gfp_mask, unsigned int order, unsigned int alloc_flags, const struct alloc_context *ac, unsigned long *did_some_progress) { struct page *page = NULL; bool drained = false; *did_some_progress = __perform_reclaim(gfp_mask, order, ac); if (unlikely(!(*did_some_progress))) return NULL; retry: page = get_page_from_freelist(gfp_mask, order, alloc_flags, ac); /* * If an allocation failed after direct reclaim, it could be because * pages are pinned on the per-cpu lists or in high alloc reserves. * Shrink them them and try again */ if (!page && !drained) { unreserve_highatomic_pageblock(ac, false); drain_all_pages(NULL); drained = true; goto retry; } return page; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman7463.25%215.38%
Linus Torvalds (pre-git)1512.82%215.38%
Michal Hocko108.55%215.38%
David Rientjes43.42%17.69%
Kirill Korotaev43.42%17.69%
Vlastimil Babka21.71%17.69%
MinChan Kim21.71%17.69%
Rohit Seth21.71%17.69%
Linus Torvalds21.71%17.69%
Andrew Morton21.71%17.69%
Total117100.00%13100.00%


static void wake_all_kswapds(unsigned int order, const struct alloc_context *ac) { struct zoneref *z; struct zone *zone; pg_data_t *last_pgdat = NULL; for_each_zone_zonelist_nodemask(zone, z, ac->zonelist, ac->high_zoneidx, ac->nodemask) { if (last_pgdat != zone->zone_pgdat) wakeup_kswapd(zone, order, ac->high_zoneidx); last_pgdat = zone->zone_pgdat; } }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman71100.00%6100.00%
Total71100.00%6100.00%


static inline unsigned int gfp_to_alloc_flags(gfp_t gfp_mask) { unsigned int alloc_flags = ALLOC_WMARK_MIN | ALLOC_CPUSET; /* __GFP_HIGH is assumed to be the same as ALLOC_HIGH to save a branch. */ BUILD_BUG_ON(__GFP_HIGH != (__force gfp_t) ALLOC_HIGH); /* * The caller may dip into page reserves a bit more if the caller * cannot run direct reclaim, or if the caller has realtime scheduling * policy or is asking for __GFP_HIGH memory. GFP_ATOMIC requests will * set both ALLOC_HARDER (__GFP_ATOMIC) and ALLOC_HIGH (__GFP_HIGH). */ alloc_flags |= (__force int) (gfp_mask & __GFP_HIGH); if (gfp_mask & __GFP_ATOMIC) { /* * Not worth trying to allocate harder for __GFP_NOMEMALLOC even * if it can't schedule. */ if (!(gfp_mask & __GFP_NOMEMALLOC)) alloc_flags |= ALLOC_HARDER; /* * Ignore cpuset mems for GFP_ATOMIC rather than fail, see the * comment for __cpuset_node_allowed(). */ alloc_flags &= ~ALLOC_CPUSET; } else if (unlikely(rt_task(current)) && !in_interrupt()) alloc_flags |= ALLOC_HARDER; #ifdef CONFIG_CMA if (gfpflags_to_migratetype(gfp_mask) == MIGRATE_MOVABLE) alloc_flags |= ALLOC_CMA; #endif return alloc_flags; }

Contributors

PersonTokensPropCommitsCommitProp
Mel Gorman8676.79%457.14%
Vlastimil Babka2421.43%228.57%
David Rientjes21.79%114.29%
Total112100.00%7100.00%


bool gfp_pfmemalloc_allowed(gfp_t gfp_mask) { if (unlikely(gfp_mask & __GFP_NOMEMALLOC