Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 2 | * bootmem - A boot-time physical memory allocator and configurator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1999 Ingo Molnar |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 5 | * 1999 Kanoj Sarcar, SGI |
| 6 | * 2008 Johannes Weiner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 8 | * Access to this subsystem has to be serialized externally (which is true |
| 9 | * for the boot process anyway). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 12 | #include <linux/pfn.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 15 | #include <linux/export.h> |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 16 | #include <linux/kmemleak.h> |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 17 | #include <linux/range.h> |
Yinghai Lu | 72d7c3b | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 18 | #include <linux/memblock.h> |
Paul McQuade | d85fbee | 2014-10-09 15:29:05 -0700 | [diff] [blame] | 19 | #include <linux/bug.h> |
| 20 | #include <linux/io.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 21 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 22 | #include <asm/processor.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
Yinghai Lu | e782ab4 | 2011-02-24 14:43:06 +0100 | [diff] [blame] | 26 | #ifndef CONFIG_NEED_MULTIPLE_NODES |
| 27 | struct pglist_data __refdata contig_page_data = { |
| 28 | .bdata = &bootmem_node_data[0] |
| 29 | }; |
| 30 | EXPORT_SYMBOL(contig_page_data); |
| 31 | #endif |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | unsigned long max_low_pfn; |
| 34 | unsigned long min_low_pfn; |
| 35 | unsigned long max_pfn; |
Igor Mammedov | 8dd3303 | 2015-12-04 14:07:05 +0100 | [diff] [blame] | 36 | unsigned long long max_possible_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Johannes Weiner | b61bfa3 | 2008-07-23 21:26:55 -0700 | [diff] [blame] | 38 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
| 39 | |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 40 | static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list); |
| 41 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 42 | static int bootmem_debug; |
| 43 | |
| 44 | static int __init bootmem_debug_setup(char *buf) |
| 45 | { |
| 46 | bootmem_debug = 1; |
| 47 | return 0; |
| 48 | } |
| 49 | early_param("bootmem_debug", bootmem_debug_setup); |
| 50 | |
| 51 | #define bdebug(fmt, args...) ({ \ |
| 52 | if (unlikely(bootmem_debug)) \ |
| 53 | printk(KERN_INFO \ |
| 54 | "bootmem::%s " fmt, \ |
Harvey Harrison | 80a914d | 2008-10-15 22:01:25 -0700 | [diff] [blame] | 55 | __func__, ## args); \ |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 56 | }) |
| 57 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 58 | static unsigned long __init bootmap_bytes(unsigned long pages) |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 59 | { |
Uwe Kleine-König | 9571a98 | 2012-01-10 15:08:00 -0800 | [diff] [blame] | 60 | unsigned long bytes = DIV_ROUND_UP(pages, 8); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 61 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 62 | return ALIGN(bytes, sizeof(long)); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 65 | /** |
| 66 | * bootmem_bootmap_pages - calculate bitmap size in pages |
| 67 | * @pages: number of pages the bitmap has to represent |
| 68 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 69 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 71 | unsigned long bytes = bootmap_bytes(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 73 | return PAGE_ALIGN(bytes) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | } |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 75 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 76 | /* |
| 77 | * link bdata in order |
| 78 | */ |
Franck Bui-Huu | 69d49e6 | 2006-09-25 23:31:04 -0700 | [diff] [blame] | 79 | static void __init link_bootmem(bootmem_data_t *bdata) |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 80 | { |
Gavin Shan | 5c2b8a1 | 2012-05-29 15:06:46 -0700 | [diff] [blame] | 81 | bootmem_data_t *ent; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 82 | |
Gavin Shan | 5c2b8a1 | 2012-05-29 15:06:46 -0700 | [diff] [blame] | 83 | list_for_each_entry(ent, &bdata_list, list) { |
| 84 | if (bdata->node_min_pfn < ent->node_min_pfn) { |
| 85 | list_add_tail(&bdata->list, &ent->list); |
| 86 | return; |
| 87 | } |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 88 | } |
Gavin Shan | 5c2b8a1 | 2012-05-29 15:06:46 -0700 | [diff] [blame] | 89 | |
| 90 | list_add_tail(&bdata->list, &bdata_list); |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 93 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * Called once to set up the allocator itself. |
| 95 | */ |
Johannes Weiner | 8ae0446 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 96 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | unsigned long mapstart, unsigned long start, unsigned long end) |
| 98 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 99 | unsigned long mapsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 101 | mminit_validate_memmodel_limits(&start, &end); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 102 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 103 | bdata->node_min_pfn = start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | bdata->node_low_pfn = end; |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 105 | link_bootmem(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * Initially all pages are reserved - setup_arch() has to |
| 109 | * register free RAM areas explicitly. |
| 110 | */ |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 111 | mapsize = bootmap_bytes(end - start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
| 113 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 114 | bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", |
| 115 | bdata - bootmem_node_data, start, mapstart, end, mapsize); |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return mapsize; |
| 118 | } |
| 119 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 120 | /** |
| 121 | * init_bootmem_node - register a node as boot memory |
| 122 | * @pgdat: node to register |
| 123 | * @freepfn: pfn where the bitmap for this node is to be placed |
| 124 | * @startpfn: first pfn on the node |
| 125 | * @endpfn: first pfn after the node |
| 126 | * |
| 127 | * Returns the number of bytes needed to hold the bitmap for this node. |
| 128 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 129 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
| 130 | unsigned long startpfn, unsigned long endpfn) |
| 131 | { |
| 132 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); |
| 133 | } |
| 134 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 135 | /** |
| 136 | * init_bootmem - register boot memory |
| 137 | * @start: pfn where the bitmap is to be placed |
| 138 | * @pages: number of available physical pages |
| 139 | * |
| 140 | * Returns the number of bytes needed to hold the bitmap. |
| 141 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 142 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
| 143 | { |
| 144 | max_low_pfn = pages; |
| 145 | min_low_pfn = start; |
| 146 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); |
| 147 | } |
Yinghai Lu | 0932587 | 2011-02-24 14:43:05 +0100 | [diff] [blame] | 148 | |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 149 | /* |
| 150 | * free_bootmem_late - free bootmem pages directly to page allocator |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 151 | * @addr: starting physical address of the range |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 152 | * @size: size of the range in bytes |
| 153 | * |
| 154 | * This is only useful when the bootmem allocator has already been torn |
| 155 | * down, but we are still initializing the system. Pages are given directly |
| 156 | * to the page allocator, no bootmem metadata is updated because it is gone. |
| 157 | */ |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 158 | void __init free_bootmem_late(unsigned long physaddr, unsigned long size) |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 159 | { |
| 160 | unsigned long cursor, end; |
| 161 | |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 162 | kmemleak_free_part(__va(physaddr), size); |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 163 | |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 164 | cursor = PFN_UP(physaddr); |
| 165 | end = PFN_DOWN(physaddr + size); |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 166 | |
| 167 | for (; cursor < end; cursor++) { |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 168 | __free_pages_bootmem(pfn_to_page(cursor), cursor, 0); |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 169 | totalram_pages++; |
| 170 | } |
| 171 | } |
| 172 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 173 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) |
| 174 | { |
| 175 | struct page *page; |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 176 | unsigned long *map, start, end, pages, cur, count = 0; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 177 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 178 | if (!bdata->node_bootmem_map) |
| 179 | return 0; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 180 | |
Daeseok Youn | 4a099fb | 2013-11-12 15:08:14 -0800 | [diff] [blame] | 181 | map = bdata->node_bootmem_map; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 182 | start = bdata->node_min_pfn; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 183 | end = bdata->node_low_pfn; |
| 184 | |
Johannes Weiner | 799f933 | 2012-01-10 15:08:15 -0800 | [diff] [blame] | 185 | bdebug("nid=%td start=%lx end=%lx\n", |
| 186 | bdata - bootmem_node_data, start, end); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 187 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 188 | while (start < end) { |
Daeseok Youn | 4a099fb | 2013-11-12 15:08:14 -0800 | [diff] [blame] | 189 | unsigned long idx, vec; |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 190 | unsigned shift; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 191 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 192 | idx = start - bdata->node_min_pfn; |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 193 | shift = idx & (BITS_PER_LONG - 1); |
| 194 | /* |
| 195 | * vec holds at most BITS_PER_LONG map bits, |
| 196 | * bit 0 corresponds to start. |
| 197 | */ |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 198 | vec = ~map[idx / BITS_PER_LONG]; |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 199 | |
| 200 | if (shift) { |
| 201 | vec >>= shift; |
| 202 | if (end - start >= BITS_PER_LONG) |
| 203 | vec |= ~map[idx / BITS_PER_LONG + 1] << |
| 204 | (BITS_PER_LONG - shift); |
| 205 | } |
Johannes Weiner | 799f933 | 2012-01-10 15:08:15 -0800 | [diff] [blame] | 206 | /* |
| 207 | * If we have a properly aligned and fully unreserved |
| 208 | * BITS_PER_LONG block of pages in front of us, free |
| 209 | * it in one go. |
| 210 | */ |
| 211 | if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) { |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 212 | int order = ilog2(BITS_PER_LONG); |
| 213 | |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 214 | __free_pages_bootmem(pfn_to_page(start), start, order); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 215 | count += BITS_PER_LONG; |
Johannes Weiner | 799f933 | 2012-01-10 15:08:15 -0800 | [diff] [blame] | 216 | start += BITS_PER_LONG; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 217 | } else { |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 218 | cur = start; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 219 | |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 220 | start = ALIGN(start + 1, BITS_PER_LONG); |
| 221 | while (vec && cur != start) { |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 222 | if (vec & 1) { |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 223 | page = pfn_to_page(cur); |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 224 | __free_pages_bootmem(page, cur, 0); |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 225 | count++; |
| 226 | } |
| 227 | vec >>= 1; |
Max Filippov | 10d73e6 | 2013-01-11 14:31:52 -0800 | [diff] [blame] | 228 | ++cur; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 229 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 230 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 233 | cur = bdata->node_min_pfn; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 234 | page = virt_to_page(bdata->node_bootmem_map); |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 235 | pages = bdata->node_low_pfn - bdata->node_min_pfn; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 236 | pages = bootmem_bootmap_pages(pages); |
| 237 | count += pages; |
Andrew Morton | 5576646 | 2012-11-16 14:15:06 -0800 | [diff] [blame] | 238 | while (pages--) |
Mel Gorman | d70ddd7 | 2015-06-30 14:56:52 -0700 | [diff] [blame] | 239 | __free_pages_bootmem(page++, cur++, 0); |
Chris Metcalf | 1b4ace4 | 2015-09-08 15:02:12 -0700 | [diff] [blame] | 240 | bdata->node_bootmem_map = NULL; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 241 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 242 | bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count); |
| 243 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 244 | return count; |
| 245 | } |
| 246 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 247 | static int reset_managed_pages_done __initdata; |
| 248 | |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 249 | void reset_node_managed_pages(pg_data_t *pgdat) |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 250 | { |
| 251 | struct zone *z; |
| 252 | |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 253 | for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 254 | z->managed_pages = 0; |
| 255 | } |
| 256 | |
| 257 | void __init reset_all_zones_managed_pages(void) |
| 258 | { |
| 259 | struct pglist_data *pgdat; |
| 260 | |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 261 | if (reset_managed_pages_done) |
| 262 | return; |
| 263 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 264 | for_each_online_pgdat(pgdat) |
| 265 | reset_node_managed_pages(pgdat); |
Tang Chen | f784a3f | 2014-11-13 15:19:39 -0800 | [diff] [blame] | 266 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 267 | reset_managed_pages_done = 1; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 270 | /** |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 271 | * free_all_bootmem - release free pages to the buddy allocator |
| 272 | * |
| 273 | * Returns the number of pages actually released. |
| 274 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 275 | unsigned long __init free_all_bootmem(void) |
| 276 | { |
Yinghai Lu | aa235fc | 2010-03-31 20:45:27 -0700 | [diff] [blame] | 277 | unsigned long total_pages = 0; |
| 278 | bootmem_data_t *bdata; |
Jiang Liu | 9feedc9 | 2012-12-12 13:52:12 -0800 | [diff] [blame] | 279 | |
Jiang Liu | 7b4b2a0 | 2013-07-03 15:03:11 -0700 | [diff] [blame] | 280 | reset_all_zones_managed_pages(); |
Yinghai Lu | aa235fc | 2010-03-31 20:45:27 -0700 | [diff] [blame] | 281 | |
| 282 | list_for_each_entry(bdata, &bdata_list, list) |
| 283 | total_pages += free_all_bootmem_core(bdata); |
| 284 | |
Jiang Liu | 0c98853 | 2013-07-03 15:03:24 -0700 | [diff] [blame] | 285 | totalram_pages += total_pages; |
| 286 | |
Yinghai Lu | aa235fc | 2010-03-31 20:45:27 -0700 | [diff] [blame] | 287 | return total_pages; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 290 | static void __init __free(bootmem_data_t *bdata, |
| 291 | unsigned long sidx, unsigned long eidx) |
| 292 | { |
| 293 | unsigned long idx; |
| 294 | |
| 295 | bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data, |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 296 | sidx + bdata->node_min_pfn, |
| 297 | eidx + bdata->node_min_pfn); |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 298 | |
Chris Metcalf | 1b4ace4 | 2015-09-08 15:02:12 -0700 | [diff] [blame] | 299 | if (WARN_ON(bdata->node_bootmem_map == NULL)) |
| 300 | return; |
| 301 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 302 | if (bdata->hint_idx > sidx) |
| 303 | bdata->hint_idx = sidx; |
| 304 | |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 305 | for (idx = sidx; idx < eidx; idx++) |
| 306 | if (!test_and_clear_bit(idx, bdata->node_bootmem_map)) |
| 307 | BUG(); |
| 308 | } |
| 309 | |
| 310 | static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx, |
| 311 | unsigned long eidx, int flags) |
| 312 | { |
| 313 | unsigned long idx; |
| 314 | int exclusive = flags & BOOTMEM_EXCLUSIVE; |
| 315 | |
| 316 | bdebug("nid=%td start=%lx end=%lx flags=%x\n", |
| 317 | bdata - bootmem_node_data, |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 318 | sidx + bdata->node_min_pfn, |
| 319 | eidx + bdata->node_min_pfn, |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 320 | flags); |
| 321 | |
Chris Metcalf | 1b4ace4 | 2015-09-08 15:02:12 -0700 | [diff] [blame] | 322 | if (WARN_ON(bdata->node_bootmem_map == NULL)) |
| 323 | return 0; |
| 324 | |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 325 | for (idx = sidx; idx < eidx; idx++) |
| 326 | if (test_and_set_bit(idx, bdata->node_bootmem_map)) { |
| 327 | if (exclusive) { |
| 328 | __free(bdata, sidx, idx); |
| 329 | return -EBUSY; |
| 330 | } |
| 331 | bdebug("silent double reserve of PFN %lx\n", |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 332 | idx + bdata->node_min_pfn); |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 337 | static int __init mark_bootmem_node(bootmem_data_t *bdata, |
| 338 | unsigned long start, unsigned long end, |
| 339 | int reserve, int flags) |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 340 | { |
| 341 | unsigned long sidx, eidx; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 342 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 343 | bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n", |
| 344 | bdata - bootmem_node_data, start, end, reserve, flags); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 345 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 346 | BUG_ON(start < bdata->node_min_pfn); |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 347 | BUG_ON(end > bdata->node_low_pfn); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 348 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 349 | sidx = start - bdata->node_min_pfn; |
| 350 | eidx = end - bdata->node_min_pfn; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 351 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 352 | if (reserve) |
| 353 | return __reserve(bdata, sidx, eidx, flags); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 354 | else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 355 | __free(bdata, sidx, eidx); |
| 356 | return 0; |
| 357 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 358 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 359 | static int __init mark_bootmem(unsigned long start, unsigned long end, |
| 360 | int reserve, int flags) |
| 361 | { |
| 362 | unsigned long pos; |
| 363 | bootmem_data_t *bdata; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 364 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 365 | pos = start; |
| 366 | list_for_each_entry(bdata, &bdata_list, list) { |
| 367 | int err; |
| 368 | unsigned long max; |
| 369 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 370 | if (pos < bdata->node_min_pfn || |
| 371 | pos >= bdata->node_low_pfn) { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 372 | BUG_ON(pos != start); |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | max = min(bdata->node_low_pfn, end); |
| 377 | |
| 378 | err = mark_bootmem_node(bdata, pos, max, reserve, flags); |
| 379 | if (reserve && err) { |
| 380 | mark_bootmem(start, pos, 0, 0); |
| 381 | return err; |
| 382 | } |
| 383 | |
| 384 | if (max == end) |
| 385 | return 0; |
| 386 | pos = bdata->node_low_pfn; |
| 387 | } |
| 388 | BUG(); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 391 | /** |
| 392 | * free_bootmem_node - mark a page range as usable |
| 393 | * @pgdat: node the range resides on |
| 394 | * @physaddr: starting address of the range |
| 395 | * @size: size of the range in bytes |
| 396 | * |
| 397 | * Partial pages will be considered reserved and left as they are. |
| 398 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 399 | * The range must reside completely on the specified node. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 400 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 401 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 402 | unsigned long size) |
| 403 | { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 404 | unsigned long start, end; |
| 405 | |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 406 | kmemleak_free_part(__va(physaddr), size); |
| 407 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 408 | start = PFN_UP(physaddr); |
| 409 | end = PFN_DOWN(physaddr + size); |
| 410 | |
| 411 | mark_bootmem_node(pgdat->bdata, start, end, 0, 0); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 414 | /** |
| 415 | * free_bootmem - mark a page range as usable |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 416 | * @addr: starting physical address of the range |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 417 | * @size: size of the range in bytes |
| 418 | * |
| 419 | * Partial pages will be considered reserved and left as they are. |
| 420 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 421 | * The range must be contiguous but may span node boundaries. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 422 | */ |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 423 | void __init free_bootmem(unsigned long physaddr, unsigned long size) |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 424 | { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 425 | unsigned long start, end; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 426 | |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 427 | kmemleak_free_part(__va(physaddr), size); |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 428 | |
Joonsoo Kim | 81df9bf | 2012-12-11 16:03:10 -0800 | [diff] [blame] | 429 | start = PFN_UP(physaddr); |
| 430 | end = PFN_DOWN(physaddr + size); |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 431 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 432 | mark_bootmem(start, end, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 435 | /** |
| 436 | * reserve_bootmem_node - mark a page range as reserved |
| 437 | * @pgdat: node the range resides on |
| 438 | * @physaddr: starting address of the range |
| 439 | * @size: size of the range in bytes |
| 440 | * @flags: reservation flags (see linux/bootmem.h) |
| 441 | * |
| 442 | * Partial pages will be reserved. |
| 443 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 444 | * The range must reside completely on the specified node. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 445 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 446 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 447 | unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 449 | unsigned long start, end; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 450 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 451 | start = PFN_DOWN(physaddr); |
| 452 | end = PFN_UP(physaddr + size); |
| 453 | |
| 454 | return mark_bootmem_node(pgdat->bdata, start, end, 1, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 457 | /** |
Javi Merino | 0d4ba4d | 2012-08-24 18:09:31 +0100 | [diff] [blame] | 458 | * reserve_bootmem - mark a page range as reserved |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 459 | * @addr: starting address of the range |
| 460 | * @size: size of the range in bytes |
| 461 | * @flags: reservation flags (see linux/bootmem.h) |
| 462 | * |
| 463 | * Partial pages will be reserved. |
| 464 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 465 | * The range must be contiguous but may span node boundaries. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 466 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 467 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
| 468 | int flags) |
| 469 | { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 470 | unsigned long start, end; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 471 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 472 | start = PFN_DOWN(addr); |
| 473 | end = PFN_UP(addr + size); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 474 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 475 | return mark_bootmem(start, end, 1, flags); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 476 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 477 | |
Jan Beulich | 8aa043d | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 478 | static unsigned long __init align_idx(struct bootmem_data *bdata, |
| 479 | unsigned long idx, unsigned long step) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 480 | { |
| 481 | unsigned long base = bdata->node_min_pfn; |
| 482 | |
| 483 | /* |
| 484 | * Align the index with respect to the node start so that the |
| 485 | * combination of both satisfies the requested alignment. |
| 486 | */ |
| 487 | |
| 488 | return ALIGN(base + idx, step) - base; |
| 489 | } |
| 490 | |
Jan Beulich | 8aa043d | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 491 | static unsigned long __init align_off(struct bootmem_data *bdata, |
| 492 | unsigned long off, unsigned long align) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 493 | { |
| 494 | unsigned long base = PFN_PHYS(bdata->node_min_pfn); |
| 495 | |
| 496 | /* Same as align_idx for byte offsets */ |
| 497 | |
| 498 | return ALIGN(base + off, align) - base; |
| 499 | } |
| 500 | |
Johannes Weiner | c6785b6 | 2012-05-29 15:06:33 -0700 | [diff] [blame] | 501 | static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata, |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 502 | unsigned long size, unsigned long align, |
| 503 | unsigned long goal, unsigned long limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 505 | unsigned long fallback = 0; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 506 | unsigned long min, max, start, sidx, midx, step; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Johannes Weiner | 594fe1a | 2009-01-06 14:40:32 -0800 | [diff] [blame] | 508 | bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n", |
| 509 | bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT, |
| 510 | align, goal, limit); |
| 511 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 512 | BUG_ON(!size); |
| 513 | BUG_ON(align & (align - 1)); |
| 514 | BUG_ON(limit && goal + size > limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Christian Krafft | 7c309a6 | 2006-12-06 20:32:41 -0800 | [diff] [blame] | 516 | if (!bdata->node_bootmem_map) |
| 517 | return NULL; |
| 518 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 519 | min = bdata->node_min_pfn; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 520 | max = bdata->node_low_pfn; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 521 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 522 | goal >>= PAGE_SHIFT; |
| 523 | limit >>= PAGE_SHIFT; |
| 524 | |
| 525 | if (limit && max > limit) |
| 526 | max = limit; |
| 527 | if (max <= min) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 528 | return NULL; |
| 529 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 530 | step = max(align >> PAGE_SHIFT, 1UL); |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 531 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 532 | if (goal && min < goal && goal < max) |
| 533 | start = ALIGN(goal, step); |
| 534 | else |
| 535 | start = ALIGN(min, step); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 537 | sidx = start - bdata->node_min_pfn; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 538 | midx = max - bdata->node_min_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 540 | if (bdata->hint_idx > sidx) { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 541 | /* |
| 542 | * Handle the valid case of sidx being zero and still |
| 543 | * catch the fallback below. |
| 544 | */ |
| 545 | fallback = sidx + 1; |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 546 | sidx = align_idx(bdata, bdata->hint_idx, step); |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 549 | while (1) { |
| 550 | int merge; |
| 551 | void *region; |
| 552 | unsigned long eidx, i, start_off, end_off; |
| 553 | find_block: |
| 554 | sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx); |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 555 | sidx = align_idx(bdata, sidx, step); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 556 | eidx = sidx + PFN_UP(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 558 | if (sidx >= midx || eidx > midx) |
Haren Myneni | 66d43e9 | 2005-12-12 00:37:39 -0800 | [diff] [blame] | 559 | break; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 560 | |
| 561 | for (i = sidx; i < eidx; i++) |
| 562 | if (test_bit(i, bdata->node_bootmem_map)) { |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 563 | sidx = align_idx(bdata, i, step); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 564 | if (sidx == i) |
| 565 | sidx += step; |
| 566 | goto find_block; |
| 567 | } |
| 568 | |
Mikulas Patocka | 627240a | 2008-08-15 00:40:17 -0700 | [diff] [blame] | 569 | if (bdata->last_end_off & (PAGE_SIZE - 1) && |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 570 | PFN_DOWN(bdata->last_end_off) + 1 == sidx) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 571 | start_off = align_off(bdata, bdata->last_end_off, align); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 572 | else |
| 573 | start_off = PFN_PHYS(sidx); |
| 574 | |
| 575 | merge = PFN_DOWN(start_off) < sidx; |
| 576 | end_off = start_off + size; |
| 577 | |
| 578 | bdata->last_end_off = end_off; |
| 579 | bdata->hint_idx = PFN_UP(end_off); |
| 580 | |
| 581 | /* |
| 582 | * Reserve the area now: |
| 583 | */ |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 584 | if (__reserve(bdata, PFN_DOWN(start_off) + merge, |
| 585 | PFN_UP(end_off), BOOTMEM_EXCLUSIVE)) |
| 586 | BUG(); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 587 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 588 | region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) + |
| 589 | start_off); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 590 | memset(region, 0, size); |
Catalin Marinas | 008139d | 2009-08-27 14:29:17 +0100 | [diff] [blame] | 591 | /* |
| 592 | * The min_count is set to 0 so that bootmem allocated blocks |
| 593 | * are never reported as leaks. |
| 594 | */ |
| 595 | kmemleak_alloc(region, size, 0, 0); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 596 | return region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 599 | if (fallback) { |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 600 | sidx = align_idx(bdata, fallback - 1, step); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 601 | fallback = 0; |
| 602 | goto find_block; |
| 603 | } |
| 604 | |
| 605 | return NULL; |
| 606 | } |
| 607 | |
Johannes Weiner | c12ab50 | 2012-05-29 15:06:33 -0700 | [diff] [blame] | 608 | static void * __init alloc_bootmem_core(unsigned long size, |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 609 | unsigned long align, |
| 610 | unsigned long goal, |
| 611 | unsigned long limit) |
| 612 | { |
| 613 | bootmem_data_t *bdata; |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 614 | void *region; |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 615 | |
Joonsoo Kim | 3f7dfe2 | 2012-12-12 13:50:43 -0800 | [diff] [blame] | 616 | if (WARN_ON_ONCE(slab_is_available())) |
| 617 | return kzalloc(size, GFP_NOWAIT); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 618 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 619 | list_for_each_entry(bdata, &bdata_list, list) { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 620 | if (goal && bdata->node_low_pfn <= PFN_DOWN(goal)) |
| 621 | continue; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 622 | if (limit && bdata->node_min_pfn >= PFN_DOWN(limit)) |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 623 | break; |
| 624 | |
Johannes Weiner | c6785b6 | 2012-05-29 15:06:33 -0700 | [diff] [blame] | 625 | region = alloc_bootmem_bdata(bdata, size, align, goal, limit); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 626 | if (region) |
| 627 | return region; |
| 628 | } |
| 629 | |
Johannes Weiner | c12ab50 | 2012-05-29 15:06:33 -0700 | [diff] [blame] | 630 | return NULL; |
| 631 | } |
| 632 | |
| 633 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, |
| 634 | unsigned long align, |
| 635 | unsigned long goal, |
| 636 | unsigned long limit) |
| 637 | { |
| 638 | void *ptr; |
| 639 | |
| 640 | restart: |
| 641 | ptr = alloc_bootmem_core(size, align, goal, limit); |
| 642 | if (ptr) |
| 643 | return ptr; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 644 | if (goal) { |
| 645 | goal = 0; |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 646 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | } |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 652 | /** |
| 653 | * __alloc_bootmem_nopanic - allocate boot memory without panicking |
| 654 | * @size: size of the request in bytes |
| 655 | * @align: alignment of the region |
| 656 | * @goal: preferred starting address of the region |
| 657 | * |
| 658 | * The goal is dropped if it can not be satisfied and the allocation will |
| 659 | * fall back to memory below @goal. |
| 660 | * |
| 661 | * Allocation may happen on any node in the system. |
| 662 | * |
| 663 | * Returns NULL on failure. |
| 664 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 665 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 666 | unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 668 | unsigned long limit = 0; |
| 669 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 670 | return ___alloc_bootmem_nopanic(size, align, goal, limit); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 671 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 673 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, |
| 674 | unsigned long goal, unsigned long limit) |
| 675 | { |
| 676 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); |
| 677 | |
| 678 | if (mem) |
| 679 | return mem; |
| 680 | /* |
| 681 | * Whoops, we cannot satisfy the allocation request. |
| 682 | */ |
| 683 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 684 | panic("Out of memory"); |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 685 | return NULL; |
| 686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 688 | /** |
| 689 | * __alloc_bootmem - allocate boot memory |
| 690 | * @size: size of the request in bytes |
| 691 | * @align: alignment of the region |
| 692 | * @goal: preferred starting address of the region |
| 693 | * |
| 694 | * The goal is dropped if it can not be satisfied and the allocation will |
| 695 | * fall back to memory below @goal. |
| 696 | * |
| 697 | * Allocation may happen on any node in the system. |
| 698 | * |
| 699 | * The function panics if the request can not be satisfied. |
| 700 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 701 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 702 | unsigned long goal) |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 703 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 704 | unsigned long limit = 0; |
| 705 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 706 | return ___alloc_bootmem(size, align, goal, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Yinghai Lu | 99ab7b1 | 2012-07-11 14:02:53 -0700 | [diff] [blame] | 709 | void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat, |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 710 | unsigned long size, unsigned long align, |
| 711 | unsigned long goal, unsigned long limit) |
| 712 | { |
| 713 | void *ptr; |
| 714 | |
Joonsoo Kim | 3f7dfe2 | 2012-12-12 13:50:43 -0800 | [diff] [blame] | 715 | if (WARN_ON_ONCE(slab_is_available())) |
| 716 | return kzalloc(size, GFP_NOWAIT); |
Johannes Weiner | ab38184 | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 717 | again: |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 718 | |
Yinghai Lu | c8f4a2d | 2012-07-17 15:47:51 -0700 | [diff] [blame] | 719 | /* do not panic in alloc_bootmem_bdata() */ |
| 720 | if (limit && goal + size > limit) |
| 721 | limit = 0; |
| 722 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 723 | ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit); |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 724 | if (ptr) |
| 725 | return ptr; |
| 726 | |
Johannes Weiner | ab38184 | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 727 | ptr = alloc_bootmem_core(size, align, goal, limit); |
| 728 | if (ptr) |
| 729 | return ptr; |
| 730 | |
| 731 | if (goal) { |
| 732 | goal = 0; |
| 733 | goto again; |
| 734 | } |
| 735 | |
Johannes Weiner | 421456e | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
| 740 | unsigned long align, unsigned long goal) |
| 741 | { |
| 742 | if (WARN_ON_ONCE(slab_is_available())) |
| 743 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 744 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 745 | return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0); |
Johannes Weiner | 421456e | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 746 | } |
| 747 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 748 | void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
Johannes Weiner | 421456e | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 749 | unsigned long align, unsigned long goal, |
| 750 | unsigned long limit) |
| 751 | { |
| 752 | void *ptr; |
| 753 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 754 | ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0); |
Johannes Weiner | 421456e | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 755 | if (ptr) |
| 756 | return ptr; |
| 757 | |
Johannes Weiner | ab38184 | 2012-05-29 15:06:34 -0700 | [diff] [blame] | 758 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 759 | panic("Out of memory"); |
| 760 | return NULL; |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 763 | /** |
| 764 | * __alloc_bootmem_node - allocate boot memory from a specific node |
| 765 | * @pgdat: node to allocate from |
| 766 | * @size: size of the request in bytes |
| 767 | * @align: alignment of the region |
| 768 | * @goal: preferred starting address of the region |
| 769 | * |
| 770 | * The goal is dropped if it can not be satisfied and the allocation will |
| 771 | * fall back to memory below @goal. |
| 772 | * |
| 773 | * Allocation may fall back to any node in the system if the specified node |
| 774 | * can not hold the requested memory. |
| 775 | * |
| 776 | * The function panics if the request can not be satisfied. |
| 777 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 778 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 779 | unsigned long align, unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | { |
Pekka Enberg | c91c477 | 2009-06-11 08:10:28 +0300 | [diff] [blame] | 781 | if (WARN_ON_ONCE(slab_is_available())) |
| 782 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 783 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 784 | return ___alloc_bootmem_node(pgdat, size, align, goal, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size, |
| 788 | unsigned long align, unsigned long goal) |
| 789 | { |
| 790 | #ifdef MAX_DMA32_PFN |
| 791 | unsigned long end_pfn; |
| 792 | |
| 793 | if (WARN_ON_ONCE(slab_is_available())) |
| 794 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 795 | |
| 796 | /* update goal according ...MAX_DMA32_PFN */ |
Xishi Qiu | 83285c7 | 2013-11-12 15:07:19 -0800 | [diff] [blame] | 797 | end_pfn = pgdat_end_pfn(pgdat); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 798 | |
| 799 | if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) && |
| 800 | (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) { |
| 801 | void *ptr; |
| 802 | unsigned long new_goal; |
| 803 | |
| 804 | new_goal = MAX_DMA32_PFN << PAGE_SHIFT; |
Johannes Weiner | c6785b6 | 2012-05-29 15:06:33 -0700 | [diff] [blame] | 805 | ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 806 | new_goal, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 807 | if (ptr) |
| 808 | return ptr; |
| 809 | } |
| 810 | #endif |
| 811 | |
| 812 | return __alloc_bootmem_node(pgdat, size, align, goal); |
| 813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 816 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 817 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 818 | #endif |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 819 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 820 | /** |
| 821 | * __alloc_bootmem_low - allocate low boot memory |
| 822 | * @size: size of the request in bytes |
| 823 | * @align: alignment of the region |
| 824 | * @goal: preferred starting address of the region |
| 825 | * |
| 826 | * The goal is dropped if it can not be satisfied and the allocation will |
| 827 | * fall back to memory below @goal. |
| 828 | * |
| 829 | * Allocation may happen on any node in the system. |
| 830 | * |
| 831 | * The function panics if the request can not be satisfied. |
| 832 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 833 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 834 | unsigned long goal) |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 835 | { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 836 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 837 | } |
| 838 | |
Yinghai Lu | 38fa417 | 2013-01-24 12:20:15 -0800 | [diff] [blame] | 839 | void * __init __alloc_bootmem_low_nopanic(unsigned long size, |
| 840 | unsigned long align, |
| 841 | unsigned long goal) |
| 842 | { |
| 843 | return ___alloc_bootmem_nopanic(size, align, goal, |
| 844 | ARCH_LOW_ADDRESS_LIMIT); |
| 845 | } |
| 846 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 847 | /** |
| 848 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node |
| 849 | * @pgdat: node to allocate from |
| 850 | * @size: size of the request in bytes |
| 851 | * @align: alignment of the region |
| 852 | * @goal: preferred starting address of the region |
| 853 | * |
| 854 | * The goal is dropped if it can not be satisfied and the allocation will |
| 855 | * fall back to memory below @goal. |
| 856 | * |
| 857 | * Allocation may fall back to any node in the system if the specified node |
| 858 | * can not hold the requested memory. |
| 859 | * |
| 860 | * The function panics if the request can not be satisfied. |
| 861 | */ |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 862 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 863 | unsigned long align, unsigned long goal) |
| 864 | { |
Pekka Enberg | c91c477 | 2009-06-11 08:10:28 +0300 | [diff] [blame] | 865 | if (WARN_ON_ONCE(slab_is_available())) |
| 866 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 867 | |
Johannes Weiner | e907991 | 2012-05-29 15:06:36 -0700 | [diff] [blame] | 868 | return ___alloc_bootmem_node(pgdat, size, align, |
| 869 | goal, ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 870 | } |