Blame view

mm/bootmem.c 17.8 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
57cfc29ef   Johannes Weiner   bootmem: clean up...
2
   *  bootmem - A boot-time physical memory allocator and configurator
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
   *
   *  Copyright (C) 1999 Ingo Molnar
57cfc29ef   Johannes Weiner   bootmem: clean up...
5
6
   *                1999 Kanoj Sarcar, SGI
   *                2008 Johannes Weiner
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
   *
57cfc29ef   Johannes Weiner   bootmem: clean up...
8
9
   * Access to this subsystem has to be serialized externally (which is true
   * for the boot process anyway).
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <linux/init.h>
bbc7b92e3   Franck Bui-Huu   [PATCH] bootmem: ...
12
  #include <linux/pfn.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
  #include <linux/bootmem.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
  #include <linux/module.h>
e786e86a5   Franck Bui-Huu   [PATCH] bootmem: ...
15
16
  
  #include <asm/bug.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <asm/io.h>
dfd54cbcc   Heiko Carstens   [PATCH] bootmem: ...
18
  #include <asm/processor.h>
e786e86a5   Franck Bui-Huu   [PATCH] bootmem: ...
19

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
  #include "internal.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
  unsigned long max_low_pfn;
  unsigned long min_low_pfn;
  unsigned long max_pfn;
92aa63a5a   Vivek Goyal   [PATCH] kdump: Re...
24
25
26
27
28
29
30
  #ifdef CONFIG_CRASH_DUMP
  /*
   * If we have booted due to a crash, max_pfn will be a very low value. We need
   * to know the amount of memory that the previous kernel used.
   */
  unsigned long saved_max_pfn;
  #endif
b61bfa3c4   Johannes Weiner   mm: move bootmem ...
31
  bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
636cc40cb   Johannes Weiner   bootmem: revisit ...
32
  static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
2e5237daf   Johannes Weiner   bootmem: add debu...
33
34
35
36
37
38
39
40
41
42
43
44
45
  static int bootmem_debug;
  
  static int __init bootmem_debug_setup(char *buf)
  {
  	bootmem_debug = 1;
  	return 0;
  }
  early_param("bootmem_debug", bootmem_debug_setup);
  
  #define bdebug(fmt, args...) ({				\
  	if (unlikely(bootmem_debug))			\
  		printk(KERN_INFO			\
  			"bootmem::%s " fmt,		\
80a914dc0   Harvey Harrison   misc: replace __F...
46
  			__func__, ## args);		\
2e5237daf   Johannes Weiner   bootmem: add debu...
47
  })
df049a5f4   Johannes Weiner   bootmem: revisit ...
48
  static unsigned long __init bootmap_bytes(unsigned long pages)
223e8dc92   Johannes Weiner   bootmem: reorder ...
49
  {
df049a5f4   Johannes Weiner   bootmem: revisit ...
50
  	unsigned long bytes = (pages + 7) / 8;
223e8dc92   Johannes Weiner   bootmem: reorder ...
51

df049a5f4   Johannes Weiner   bootmem: revisit ...
52
  	return ALIGN(bytes, sizeof(long));
223e8dc92   Johannes Weiner   bootmem: reorder ...
53
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
54
55
56
57
  /**
   * bootmem_bootmap_pages - calculate bitmap size in pages
   * @pages: number of pages the bitmap has to represent
   */
f71bf0cac   Franck Bui-Huu   [PATCH] bootmem: ...
58
  unsigned long __init bootmem_bootmap_pages(unsigned long pages)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  {
df049a5f4   Johannes Weiner   bootmem: revisit ...
60
  	unsigned long bytes = bootmap_bytes(pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61

df049a5f4   Johannes Weiner   bootmem: revisit ...
62
  	return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
  }
f71bf0cac   Franck Bui-Huu   [PATCH] bootmem: ...
64

679bc9fbb   KAMEZAWA Hiroyuki   [PATCH] for_each_...
65
66
67
  /*
   * link bdata in order
   */
69d49e681   Franck Bui-Huu   [PATCH] bootmem: ...
68
  static void __init link_bootmem(bootmem_data_t *bdata)
679bc9fbb   KAMEZAWA Hiroyuki   [PATCH] for_each_...
69
  {
636cc40cb   Johannes Weiner   bootmem: revisit ...
70
  	struct list_head *iter;
f71bf0cac   Franck Bui-Huu   [PATCH] bootmem: ...
71

636cc40cb   Johannes Weiner   bootmem: revisit ...
72
73
74
75
  	list_for_each(iter, &bdata_list) {
  		bootmem_data_t *ent;
  
  		ent = list_entry(iter, bootmem_data_t, list);
3560e249a   Johannes Weiner   bootmem: replace ...
76
  		if (bdata->node_min_pfn < ent->node_min_pfn)
636cc40cb   Johannes Weiner   bootmem: revisit ...
77
  			break;
679bc9fbb   KAMEZAWA Hiroyuki   [PATCH] for_each_...
78
  	}
636cc40cb   Johannes Weiner   bootmem: revisit ...
79
  	list_add_tail(&bdata->list, iter);
679bc9fbb   KAMEZAWA Hiroyuki   [PATCH] for_each_...
80
  }
bbc7b92e3   Franck Bui-Huu   [PATCH] bootmem: ...
81
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
   * Called once to set up the allocator itself.
   */
8ae044630   Johannes Weiner   mm: normalize int...
84
  static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
85
86
  	unsigned long mapstart, unsigned long start, unsigned long end)
  {
bbc7b92e3   Franck Bui-Huu   [PATCH] bootmem: ...
87
  	unsigned long mapsize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88

2dbb51c49   Mel Gorman   mm: make defensiv...
89
  	mminit_validate_memmodel_limits(&start, &end);
bbc7b92e3   Franck Bui-Huu   [PATCH] bootmem: ...
90
  	bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
3560e249a   Johannes Weiner   bootmem: replace ...
91
  	bdata->node_min_pfn = start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  	bdata->node_low_pfn = end;
679bc9fbb   KAMEZAWA Hiroyuki   [PATCH] for_each_...
93
  	link_bootmem(bdata);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
  
  	/*
  	 * Initially all pages are reserved - setup_arch() has to
  	 * register free RAM areas explicitly.
  	 */
df049a5f4   Johannes Weiner   bootmem: revisit ...
99
  	mapsize = bootmap_bytes(end - start);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  	memset(bdata->node_bootmem_map, 0xff, mapsize);
2e5237daf   Johannes Weiner   bootmem: add debu...
101
102
103
  	bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx
  ",
  		bdata - bootmem_node_data, start, mapstart, end, mapsize);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
  	return mapsize;
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
106
107
108
109
110
111
112
113
114
  /**
   * init_bootmem_node - register a node as boot memory
   * @pgdat: node to register
   * @freepfn: pfn where the bitmap for this node is to be placed
   * @startpfn: first pfn on the node
   * @endpfn: first pfn after the node
   *
   * Returns the number of bytes needed to hold the bitmap for this node.
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
115
116
117
118
119
  unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
  				unsigned long startpfn, unsigned long endpfn)
  {
  	return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
120
121
122
123
124
125
126
  /**
   * init_bootmem - register boot memory
   * @start: pfn where the bitmap is to be placed
   * @pages: number of available physical pages
   *
   * Returns the number of bytes needed to hold the bitmap.
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
127
128
129
130
131
132
133
134
135
  unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
  {
  	max_low_pfn = pages;
  	min_low_pfn = start;
  	return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
  }
  
  static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
  {
41546c174   Johannes Weiner   bootmem: clean up...
136
  	int aligned;
223e8dc92   Johannes Weiner   bootmem: reorder ...
137
  	struct page *page;
41546c174   Johannes Weiner   bootmem: clean up...
138
139
140
141
  	unsigned long start, end, pages, count = 0;
  
  	if (!bdata->node_bootmem_map)
  		return 0;
3560e249a   Johannes Weiner   bootmem: replace ...
142
  	start = bdata->node_min_pfn;
41546c174   Johannes Weiner   bootmem: clean up...
143
  	end = bdata->node_low_pfn;
223e8dc92   Johannes Weiner   bootmem: reorder ...
144
  	/*
41546c174   Johannes Weiner   bootmem: clean up...
145
146
  	 * If the start is aligned to the machines wordsize, we might
  	 * be able to free pages in bulks of that order.
223e8dc92   Johannes Weiner   bootmem: reorder ...
147
  	 */
41546c174   Johannes Weiner   bootmem: clean up...
148
  	aligned = !(start & (BITS_PER_LONG - 1));
223e8dc92   Johannes Weiner   bootmem: reorder ...
149

41546c174   Johannes Weiner   bootmem: clean up...
150
151
152
  	bdebug("nid=%td start=%lx end=%lx aligned=%d
  ",
  		bdata - bootmem_node_data, start, end, aligned);
223e8dc92   Johannes Weiner   bootmem: reorder ...
153

41546c174   Johannes Weiner   bootmem: clean up...
154
155
  	while (start < end) {
  		unsigned long *map, idx, vec;
223e8dc92   Johannes Weiner   bootmem: reorder ...
156

41546c174   Johannes Weiner   bootmem: clean up...
157
  		map = bdata->node_bootmem_map;
3560e249a   Johannes Weiner   bootmem: replace ...
158
  		idx = start - bdata->node_min_pfn;
41546c174   Johannes Weiner   bootmem: clean up...
159
160
161
162
163
164
  		vec = ~map[idx / BITS_PER_LONG];
  
  		if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
  			int order = ilog2(BITS_PER_LONG);
  
  			__free_pages_bootmem(pfn_to_page(start), order);
223e8dc92   Johannes Weiner   bootmem: reorder ...
165
  			count += BITS_PER_LONG;
41546c174   Johannes Weiner   bootmem: clean up...
166
167
168
169
170
171
  		} else {
  			unsigned long off = 0;
  
  			while (vec && off < BITS_PER_LONG) {
  				if (vec & 1) {
  					page = pfn_to_page(start + off);
223e8dc92   Johannes Weiner   bootmem: reorder ...
172
  					__free_pages_bootmem(page, 0);
41546c174   Johannes Weiner   bootmem: clean up...
173
  					count++;
223e8dc92   Johannes Weiner   bootmem: reorder ...
174
  				}
41546c174   Johannes Weiner   bootmem: clean up...
175
176
  				vec >>= 1;
  				off++;
223e8dc92   Johannes Weiner   bootmem: reorder ...
177
  			}
223e8dc92   Johannes Weiner   bootmem: reorder ...
178
  		}
41546c174   Johannes Weiner   bootmem: clean up...
179
  		start += BITS_PER_LONG;
223e8dc92   Johannes Weiner   bootmem: reorder ...
180
  	}
223e8dc92   Johannes Weiner   bootmem: reorder ...
181
  	page = virt_to_page(bdata->node_bootmem_map);
3560e249a   Johannes Weiner   bootmem: replace ...
182
  	pages = bdata->node_low_pfn - bdata->node_min_pfn;
41546c174   Johannes Weiner   bootmem: clean up...
183
184
185
186
  	pages = bootmem_bootmap_pages(pages);
  	count += pages;
  	while (pages--)
  		__free_pages_bootmem(page++, 0);
223e8dc92   Johannes Weiner   bootmem: reorder ...
187

2e5237daf   Johannes Weiner   bootmem: add debu...
188
189
  	bdebug("nid=%td released=%lx
  ", bdata - bootmem_node_data, count);
223e8dc92   Johannes Weiner   bootmem: reorder ...
190
191
  	return count;
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
192
193
194
195
196
197
  /**
   * free_all_bootmem_node - release a node's free pages to the buddy allocator
   * @pgdat: node to be released
   *
   * Returns the number of pages actually released.
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
198
199
200
201
202
  unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  {
  	register_page_bootmem_info_node(pgdat);
  	return free_all_bootmem_core(pgdat->bdata);
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
203
204
205
206
207
  /**
   * free_all_bootmem - release free pages to the buddy allocator
   *
   * Returns the number of pages actually released.
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
208
209
210
211
  unsigned long __init free_all_bootmem(void)
  {
  	return free_all_bootmem_core(NODE_DATA(0)->bdata);
  }
d747fa4bc   Johannes Weiner   bootmem: free/res...
212
213
214
215
216
217
218
  static void __init __free(bootmem_data_t *bdata,
  			unsigned long sidx, unsigned long eidx)
  {
  	unsigned long idx;
  
  	bdebug("nid=%td start=%lx end=%lx
  ", bdata - bootmem_node_data,
3560e249a   Johannes Weiner   bootmem: replace ...
219
220
  		sidx + bdata->node_min_pfn,
  		eidx + bdata->node_min_pfn);
d747fa4bc   Johannes Weiner   bootmem: free/res...
221

e2bf3cae5   Johannes Weiner   bootmem: factor o...
222
223
  	if (bdata->hint_idx > sidx)
  		bdata->hint_idx = sidx;
d747fa4bc   Johannes Weiner   bootmem: free/res...
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  	for (idx = sidx; idx < eidx; idx++)
  		if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  			BUG();
  }
  
  static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  			unsigned long eidx, int flags)
  {
  	unsigned long idx;
  	int exclusive = flags & BOOTMEM_EXCLUSIVE;
  
  	bdebug("nid=%td start=%lx end=%lx flags=%x
  ",
  		bdata - bootmem_node_data,
3560e249a   Johannes Weiner   bootmem: replace ...
238
239
  		sidx + bdata->node_min_pfn,
  		eidx + bdata->node_min_pfn,
d747fa4bc   Johannes Weiner   bootmem: free/res...
240
241
242
243
244
245
246
247
248
249
  		flags);
  
  	for (idx = sidx; idx < eidx; idx++)
  		if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  			if (exclusive) {
  				__free(bdata, sidx, idx);
  				return -EBUSY;
  			}
  			bdebug("silent double reserve of PFN %lx
  ",
3560e249a   Johannes Weiner   bootmem: replace ...
250
  				idx + bdata->node_min_pfn);
d747fa4bc   Johannes Weiner   bootmem: free/res...
251
252
253
  		}
  	return 0;
  }
e2bf3cae5   Johannes Weiner   bootmem: factor o...
254
255
256
  static int __init mark_bootmem_node(bootmem_data_t *bdata,
  				unsigned long start, unsigned long end,
  				int reserve, int flags)
223e8dc92   Johannes Weiner   bootmem: reorder ...
257
258
  {
  	unsigned long sidx, eidx;
223e8dc92   Johannes Weiner   bootmem: reorder ...
259

e2bf3cae5   Johannes Weiner   bootmem: factor o...
260
261
262
  	bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x
  ",
  		bdata - bootmem_node_data, start, end, reserve, flags);
223e8dc92   Johannes Weiner   bootmem: reorder ...
263

3560e249a   Johannes Weiner   bootmem: replace ...
264
  	BUG_ON(start < bdata->node_min_pfn);
e2bf3cae5   Johannes Weiner   bootmem: factor o...
265
  	BUG_ON(end > bdata->node_low_pfn);
223e8dc92   Johannes Weiner   bootmem: reorder ...
266

3560e249a   Johannes Weiner   bootmem: replace ...
267
268
  	sidx = start - bdata->node_min_pfn;
  	eidx = end - bdata->node_min_pfn;
223e8dc92   Johannes Weiner   bootmem: reorder ...
269

e2bf3cae5   Johannes Weiner   bootmem: factor o...
270
271
  	if (reserve)
  		return __reserve(bdata, sidx, eidx, flags);
223e8dc92   Johannes Weiner   bootmem: reorder ...
272
  	else
e2bf3cae5   Johannes Weiner   bootmem: factor o...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  		__free(bdata, sidx, eidx);
  	return 0;
  }
  
  static int __init mark_bootmem(unsigned long start, unsigned long end,
  				int reserve, int flags)
  {
  	unsigned long pos;
  	bootmem_data_t *bdata;
  
  	pos = start;
  	list_for_each_entry(bdata, &bdata_list, list) {
  		int err;
  		unsigned long max;
3560e249a   Johannes Weiner   bootmem: replace ...
287
288
  		if (pos < bdata->node_min_pfn ||
  		    pos >= bdata->node_low_pfn) {
e2bf3cae5   Johannes Weiner   bootmem: factor o...
289
290
291
292
293
  			BUG_ON(pos != start);
  			continue;
  		}
  
  		max = min(bdata->node_low_pfn, end);
223e8dc92   Johannes Weiner   bootmem: reorder ...
294

e2bf3cae5   Johannes Weiner   bootmem: factor o...
295
296
297
298
299
  		err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  		if (reserve && err) {
  			mark_bootmem(start, pos, 0, 0);
  			return err;
  		}
223e8dc92   Johannes Weiner   bootmem: reorder ...
300

e2bf3cae5   Johannes Weiner   bootmem: factor o...
301
302
303
304
305
  		if (max == end)
  			return 0;
  		pos = bdata->node_low_pfn;
  	}
  	BUG();
223e8dc92   Johannes Weiner   bootmem: reorder ...
306
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
307
308
309
310
311
312
313
314
  /**
   * free_bootmem_node - mark a page range as usable
   * @pgdat: node the range resides on
   * @physaddr: starting address of the range
   * @size: size of the range in bytes
   *
   * Partial pages will be considered reserved and left as they are.
   *
e2bf3cae5   Johannes Weiner   bootmem: factor o...
315
   * The range must reside completely on the specified node.
a66fd7dae   Johannes Weiner   bootmem: add docu...
316
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
317
318
319
  void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  			      unsigned long size)
  {
e2bf3cae5   Johannes Weiner   bootmem: factor o...
320
321
322
323
324
325
  	unsigned long start, end;
  
  	start = PFN_UP(physaddr);
  	end = PFN_DOWN(physaddr + size);
  
  	mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
223e8dc92   Johannes Weiner   bootmem: reorder ...
326
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
327
328
329
330
331
332
333
  /**
   * free_bootmem - mark a page range as usable
   * @addr: starting address of the range
   * @size: size of the range in bytes
   *
   * Partial pages will be considered reserved and left as they are.
   *
e2bf3cae5   Johannes Weiner   bootmem: factor o...
334
   * The range must be contiguous but may span node boundaries.
a66fd7dae   Johannes Weiner   bootmem: add docu...
335
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
336
337
  void __init free_bootmem(unsigned long addr, unsigned long size)
  {
e2bf3cae5   Johannes Weiner   bootmem: factor o...
338
  	unsigned long start, end;
a5645a61b   Yinghai Lu   mm: allow reserve...
339

e2bf3cae5   Johannes Weiner   bootmem: factor o...
340
341
  	start = PFN_UP(addr);
  	end = PFN_DOWN(addr + size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342

e2bf3cae5   Johannes Weiner   bootmem: factor o...
343
  	mark_bootmem(start, end, 0, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
345
346
347
348
349
350
351
352
353
  /**
   * reserve_bootmem_node - mark a page range as reserved
   * @pgdat: node the range resides on
   * @physaddr: starting address of the range
   * @size: size of the range in bytes
   * @flags: reservation flags (see linux/bootmem.h)
   *
   * Partial pages will be reserved.
   *
e2bf3cae5   Johannes Weiner   bootmem: factor o...
354
   * The range must reside completely on the specified node.
a66fd7dae   Johannes Weiner   bootmem: add docu...
355
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
356
357
  int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  				 unsigned long size, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
  {
e2bf3cae5   Johannes Weiner   bootmem: factor o...
359
  	unsigned long start, end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360

e2bf3cae5   Johannes Weiner   bootmem: factor o...
361
362
363
364
  	start = PFN_DOWN(physaddr);
  	end = PFN_UP(physaddr + size);
  
  	return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
223e8dc92   Johannes Weiner   bootmem: reorder ...
365
  }
5a982cbc7   Yinghai Lu   mm: fix boundary ...
366

223e8dc92   Johannes Weiner   bootmem: reorder ...
367
  #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
a66fd7dae   Johannes Weiner   bootmem: add docu...
368
369
370
371
372
373
374
375
  /**
   * reserve_bootmem - mark a page range as usable
   * @addr: starting address of the range
   * @size: size of the range in bytes
   * @flags: reservation flags (see linux/bootmem.h)
   *
   * Partial pages will be reserved.
   *
e2bf3cae5   Johannes Weiner   bootmem: factor o...
376
   * The range must be contiguous but may span node boundaries.
a66fd7dae   Johannes Weiner   bootmem: add docu...
377
   */
223e8dc92   Johannes Weiner   bootmem: reorder ...
378
379
380
  int __init reserve_bootmem(unsigned long addr, unsigned long size,
  			    int flags)
  {
e2bf3cae5   Johannes Weiner   bootmem: factor o...
381
  	unsigned long start, end;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382

e2bf3cae5   Johannes Weiner   bootmem: factor o...
383
384
  	start = PFN_DOWN(addr);
  	end = PFN_UP(addr + size);
223e8dc92   Johannes Weiner   bootmem: reorder ...
385

e2bf3cae5   Johannes Weiner   bootmem: factor o...
386
  	return mark_bootmem(start, end, 1, flags);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
  }
223e8dc92   Johannes Weiner   bootmem: reorder ...
388
  #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389

481ebd0d7   Johannes Weiner   bootmem: fix alig...
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx,
  			unsigned long step)
  {
  	unsigned long base = bdata->node_min_pfn;
  
  	/*
  	 * Align the index with respect to the node start so that the
  	 * combination of both satisfies the requested alignment.
  	 */
  
  	return ALIGN(base + idx, step) - base;
  }
  
  static unsigned long align_off(struct bootmem_data *bdata, unsigned long off,
  			unsigned long align)
  {
  	unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  
  	/* Same as align_idx for byte offsets */
  
  	return ALIGN(base + off, align) - base;
  }
5f2809e69   Johannes Weiner   bootmem: clean up...
412
413
414
  static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
  				unsigned long size, unsigned long align,
  				unsigned long goal, unsigned long limit)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
  {
0f3caba21   Johannes Weiner   bootmem: respect ...
416
  	unsigned long fallback = 0;
5f2809e69   Johannes Weiner   bootmem: clean up...
417
  	unsigned long min, max, start, sidx, midx, step;
594fe1a04   Johannes Weiner   bootmem: print re...
418
419
420
421
  	bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx
  ",
  		bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  		align, goal, limit);
5f2809e69   Johannes Weiner   bootmem: clean up...
422
423
424
  	BUG_ON(!size);
  	BUG_ON(align & (align - 1));
  	BUG_ON(limit && goal + size > limit);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425

7c309a64d   Christian Krafft   [PATCH] enable bo...
426
427
  	if (!bdata->node_bootmem_map)
  		return NULL;
3560e249a   Johannes Weiner   bootmem: replace ...
428
  	min = bdata->node_min_pfn;
5f2809e69   Johannes Weiner   bootmem: clean up...
429
  	max = bdata->node_low_pfn;
9a2dc04cf   Yinghai Lu   mm: offset align ...
430

5f2809e69   Johannes Weiner   bootmem: clean up...
431
432
433
434
435
436
  	goal >>= PAGE_SHIFT;
  	limit >>= PAGE_SHIFT;
  
  	if (limit && max > limit)
  		max = limit;
  	if (max <= min)
9a2dc04cf   Yinghai Lu   mm: offset align ...
437
  		return NULL;
5f2809e69   Johannes Weiner   bootmem: clean up...
438
  	step = max(align >> PAGE_SHIFT, 1UL);
281dd25cd   Yasunori Goto   [PATCH] swiotlb: ...
439

5f2809e69   Johannes Weiner   bootmem: clean up...
440
441
442
443
  	if (goal && min < goal && goal < max)
  		start = ALIGN(goal, step);
  	else
  		start = ALIGN(min, step);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
444

481ebd0d7   Johannes Weiner   bootmem: fix alig...
445
  	sidx = start - bdata->node_min_pfn;
3560e249a   Johannes Weiner   bootmem: replace ...
446
  	midx = max - bdata->node_min_pfn;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
447

5f2809e69   Johannes Weiner   bootmem: clean up...
448
  	if (bdata->hint_idx > sidx) {
0f3caba21   Johannes Weiner   bootmem: respect ...
449
450
451
452
453
  		/*
  		 * Handle the valid case of sidx being zero and still
  		 * catch the fallback below.
  		 */
  		fallback = sidx + 1;
481ebd0d7   Johannes Weiner   bootmem: fix alig...
454
  		sidx = align_idx(bdata, bdata->hint_idx, step);
5f2809e69   Johannes Weiner   bootmem: clean up...
455
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456

5f2809e69   Johannes Weiner   bootmem: clean up...
457
458
459
460
461
462
  	while (1) {
  		int merge;
  		void *region;
  		unsigned long eidx, i, start_off, end_off;
  find_block:
  		sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
481ebd0d7   Johannes Weiner   bootmem: fix alig...
463
  		sidx = align_idx(bdata, sidx, step);
5f2809e69   Johannes Weiner   bootmem: clean up...
464
  		eidx = sidx + PFN_UP(size);
ad09315ca   Yinghai Lu   mm: fix alloc_boo...
465

5f2809e69   Johannes Weiner   bootmem: clean up...
466
  		if (sidx >= midx || eidx > midx)
66d43e98e   Haren Myneni   [PATCH] fix in __...
467
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
468

5f2809e69   Johannes Weiner   bootmem: clean up...
469
470
  		for (i = sidx; i < eidx; i++)
  			if (test_bit(i, bdata->node_bootmem_map)) {
481ebd0d7   Johannes Weiner   bootmem: fix alig...
471
  				sidx = align_idx(bdata, i, step);
5f2809e69   Johannes Weiner   bootmem: clean up...
472
473
474
475
  				if (sidx == i)
  					sidx += step;
  				goto find_block;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476

627240aaa   Mikulas Patocka   bootmem allocator...
477
  		if (bdata->last_end_off & (PAGE_SIZE - 1) &&
5f2809e69   Johannes Weiner   bootmem: clean up...
478
  				PFN_DOWN(bdata->last_end_off) + 1 == sidx)
481ebd0d7   Johannes Weiner   bootmem: fix alig...
479
  			start_off = align_off(bdata, bdata->last_end_off, align);
5f2809e69   Johannes Weiner   bootmem: clean up...
480
481
482
483
484
485
486
487
488
489
490
491
  		else
  			start_off = PFN_PHYS(sidx);
  
  		merge = PFN_DOWN(start_off) < sidx;
  		end_off = start_off + size;
  
  		bdata->last_end_off = end_off;
  		bdata->hint_idx = PFN_UP(end_off);
  
  		/*
  		 * Reserve the area now:
  		 */
d747fa4bc   Johannes Weiner   bootmem: free/res...
492
493
494
  		if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  				PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  			BUG();
5f2809e69   Johannes Weiner   bootmem: clean up...
495

3560e249a   Johannes Weiner   bootmem: replace ...
496
497
  		region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  				start_off);
5f2809e69   Johannes Weiner   bootmem: clean up...
498
499
  		memset(region, 0, size);
  		return region;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
500
  	}
0f3caba21   Johannes Weiner   bootmem: respect ...
501
  	if (fallback) {
481ebd0d7   Johannes Weiner   bootmem: fix alig...
502
  		sidx = align_idx(bdata, fallback - 1, step);
0f3caba21   Johannes Weiner   bootmem: respect ...
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  		fallback = 0;
  		goto find_block;
  	}
  
  	return NULL;
  }
  
  static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  					unsigned long align,
  					unsigned long goal,
  					unsigned long limit)
  {
  	bootmem_data_t *bdata;
  
  restart:
  	list_for_each_entry(bdata, &bdata_list, list) {
  		void *region;
  
  		if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  			continue;
3560e249a   Johannes Weiner   bootmem: replace ...
523
  		if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
0f3caba21   Johannes Weiner   bootmem: respect ...
524
525
526
527
528
529
  			break;
  
  		region = alloc_bootmem_core(bdata, size, align, goal, limit);
  		if (region)
  			return region;
  	}
5f2809e69   Johannes Weiner   bootmem: clean up...
530
531
  	if (goal) {
  		goal = 0;
0f3caba21   Johannes Weiner   bootmem: respect ...
532
  		goto restart;
5f2809e69   Johannes Weiner   bootmem: clean up...
533
  	}
2e5237daf   Johannes Weiner   bootmem: add debu...
534

5f2809e69   Johannes Weiner   bootmem: clean up...
535
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
536
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
537
538
539
540
541
542
543
544
545
546
547
548
549
  /**
   * __alloc_bootmem_nopanic - allocate boot memory without panicking
   * @size: size of the request in bytes
   * @align: alignment of the region
   * @goal: preferred starting address of the region
   *
   * The goal is dropped if it can not be satisfied and the allocation will
   * fall back to memory below @goal.
   *
   * Allocation may happen on any node in the system.
   *
   * Returns NULL on failure.
   */
bb0923a66   Franck Bui-Huu   [PATCH] bootmem: ...
550
  void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
0f3caba21   Johannes Weiner   bootmem: respect ...
551
  					unsigned long goal)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
552
  {
0f3caba21   Johannes Weiner   bootmem: respect ...
553
554
  	return ___alloc_bootmem_nopanic(size, align, goal, 0);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555

0f3caba21   Johannes Weiner   bootmem: respect ...
556
557
558
559
560
561
562
563
564
565
566
567
568
  static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  					unsigned long goal, unsigned long limit)
  {
  	void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  
  	if (mem)
  		return mem;
  	/*
  	 * Whoops, we cannot satisfy the allocation request.
  	 */
  	printk(KERN_ALERT "bootmem alloc of %lu bytes failed!
  ", size);
  	panic("Out of memory");
a8062231d   Andi Kleen   [PATCH] x86_64: H...
569
570
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
571

a66fd7dae   Johannes Weiner   bootmem: add docu...
572
573
574
575
576
577
578
579
580
581
582
583
584
  /**
   * __alloc_bootmem - allocate boot memory
   * @size: size of the request in bytes
   * @align: alignment of the region
   * @goal: preferred starting address of the region
   *
   * The goal is dropped if it can not be satisfied and the allocation will
   * fall back to memory below @goal.
   *
   * Allocation may happen on any node in the system.
   *
   * The function panics if the request can not be satisfied.
   */
bb0923a66   Franck Bui-Huu   [PATCH] bootmem: ...
585
586
  void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  			      unsigned long goal)
a8062231d   Andi Kleen   [PATCH] x86_64: H...
587
  {
0f3caba21   Johannes Weiner   bootmem: respect ...
588
  	return ___alloc_bootmem(size, align, goal, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
589
  }
4cc278b72   Johannes Weiner   bootmem: Make __a...
590
591
592
593
594
595
596
597
598
599
600
601
  static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
  				unsigned long size, unsigned long align,
  				unsigned long goal, unsigned long limit)
  {
  	void *ptr;
  
  	ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
  	if (ptr)
  		return ptr;
  
  	return ___alloc_bootmem(size, align, goal, limit);
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
  /**
   * __alloc_bootmem_node - allocate boot memory from a specific node
   * @pgdat: node to allocate from
   * @size: size of the request in bytes
   * @align: alignment of the region
   * @goal: preferred starting address of the region
   *
   * The goal is dropped if it can not be satisfied and the allocation will
   * fall back to memory below @goal.
   *
   * Allocation may fall back to any node in the system if the specified node
   * can not hold the requested memory.
   *
   * The function panics if the request can not be satisfied.
   */
bb0923a66   Franck Bui-Huu   [PATCH] bootmem: ...
617
618
  void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  				   unsigned long align, unsigned long goal)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
  {
4cc278b72   Johannes Weiner   bootmem: Make __a...
620
  	return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
621
  }
e70260aab   Yasunori Goto   memory hotplug: m...
622
  #ifdef CONFIG_SPARSEMEM
a66fd7dae   Johannes Weiner   bootmem: add docu...
623
624
625
626
627
628
629
  /**
   * alloc_bootmem_section - allocate boot memory from a specific section
   * @size: size of the request in bytes
   * @section_nr: sparse map section to allocate from
   *
   * Return NULL on failure.
   */
e70260aab   Yasunori Goto   memory hotplug: m...
630
631
632
  void * __init alloc_bootmem_section(unsigned long size,
  				    unsigned long section_nr)
  {
75a56cfe9   Johannes Weiner   bootmem: revisit ...
633
634
  	bootmem_data_t *bdata;
  	unsigned long pfn, goal, limit;
e70260aab   Yasunori Goto   memory hotplug: m...
635
636
  
  	pfn = section_nr_to_pfn(section_nr);
75a56cfe9   Johannes Weiner   bootmem: revisit ...
637
638
639
  	goal = pfn << PAGE_SHIFT;
  	limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  	bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
e70260aab   Yasunori Goto   memory hotplug: m...
640

75a56cfe9   Johannes Weiner   bootmem: revisit ...
641
  	return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
e70260aab   Yasunori Goto   memory hotplug: m...
642
643
  }
  #endif
b54bbf7b8   Andi Kleen   mm: introduce non...
644
645
646
647
648
649
650
651
652
653
654
  void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  				   unsigned long align, unsigned long goal)
  {
  	void *ptr;
  
  	ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  	if (ptr)
  		return ptr;
  
  	return __alloc_bootmem_nopanic(size, align, goal);
  }
dfd54cbcc   Heiko Carstens   [PATCH] bootmem: ...
655
656
657
  #ifndef ARCH_LOW_ADDRESS_LIMIT
  #define ARCH_LOW_ADDRESS_LIMIT	0xffffffffUL
  #endif
008857c1a   Ravikiran G Thirumalai   [PATCH] Cleanup b...
658

a66fd7dae   Johannes Weiner   bootmem: add docu...
659
660
661
662
663
664
665
666
667
668
669
670
671
  /**
   * __alloc_bootmem_low - allocate low boot memory
   * @size: size of the request in bytes
   * @align: alignment of the region
   * @goal: preferred starting address of the region
   *
   * The goal is dropped if it can not be satisfied and the allocation will
   * fall back to memory below @goal.
   *
   * Allocation may happen on any node in the system.
   *
   * The function panics if the request can not be satisfied.
   */
bb0923a66   Franck Bui-Huu   [PATCH] bootmem: ...
672
673
  void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  				  unsigned long goal)
008857c1a   Ravikiran G Thirumalai   [PATCH] Cleanup b...
674
  {
0f3caba21   Johannes Weiner   bootmem: respect ...
675
  	return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
008857c1a   Ravikiran G Thirumalai   [PATCH] Cleanup b...
676
  }
a66fd7dae   Johannes Weiner   bootmem: add docu...
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
  /**
   * __alloc_bootmem_low_node - allocate low boot memory from a specific node
   * @pgdat: node to allocate from
   * @size: size of the request in bytes
   * @align: alignment of the region
   * @goal: preferred starting address of the region
   *
   * The goal is dropped if it can not be satisfied and the allocation will
   * fall back to memory below @goal.
   *
   * Allocation may fall back to any node in the system if the specified node
   * can not hold the requested memory.
   *
   * The function panics if the request can not be satisfied.
   */
008857c1a   Ravikiran G Thirumalai   [PATCH] Cleanup b...
692
693
694
  void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  				       unsigned long align, unsigned long goal)
  {
4cc278b72   Johannes Weiner   bootmem: Make __a...
695
696
  	return ___alloc_bootmem_node(pgdat->bdata, size, align,
  				goal, ARCH_LOW_ADDRESS_LIMIT);
008857c1a   Ravikiran G Thirumalai   [PATCH] Cleanup b...
697
  }