memblock.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. /*
  2. * Procedures for maintaining information about logical memory blocks.
  3. *
  4. * Peter Bergner, IBM Corp. June 2001.
  5. * Copyright (C) 2001 Peter Bergner.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/bitops.h>
  16. #include <linux/poison.h>
  17. #include <linux/pfn.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/memblock.h>
  21. #include <asm-generic/sections.h>
  22. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  23. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock;
  24. struct memblock memblock __initdata_memblock = {
  25. .memory.regions = memblock_memory_init_regions,
  26. .memory.cnt = 1, /* empty dummy entry */
  27. .memory.max = INIT_MEMBLOCK_REGIONS,
  28. .reserved.regions = memblock_reserved_init_regions,
  29. .reserved.cnt = 1, /* empty dummy entry */
  30. .reserved.max = INIT_MEMBLOCK_REGIONS,
  31. .bottom_up = false,
  32. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  33. };
  34. int memblock_debug __initdata_memblock;
  35. #ifdef CONFIG_MOVABLE_NODE
  36. bool movable_node_enabled __initdata_memblock = false;
  37. #endif
  38. static int memblock_can_resize __initdata_memblock;
  39. static int memblock_memory_in_slab __initdata_memblock = 0;
  40. static int memblock_reserved_in_slab __initdata_memblock = 0;
  41. /* inline so we don't get a warning when pr_debug is compiled out */
  42. static __init_memblock const char *
  43. memblock_type_name(struct memblock_type *type)
  44. {
  45. if (type == &memblock.memory)
  46. return "memory";
  47. else if (type == &memblock.reserved)
  48. return "reserved";
  49. else
  50. return "unknown";
  51. }
  52. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  53. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  54. {
  55. return *size = min(*size, (phys_addr_t)ULLONG_MAX - base);
  56. }
  57. /*
  58. * Address comparison utilities
  59. */
  60. static unsigned long __init_memblock memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1,
  61. phys_addr_t base2, phys_addr_t size2)
  62. {
  63. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  64. }
  65. static long __init_memblock memblock_overlaps_region(struct memblock_type *type,
  66. phys_addr_t base, phys_addr_t size)
  67. {
  68. unsigned long i;
  69. for (i = 0; i < type->cnt; i++) {
  70. phys_addr_t rgnbase = type->regions[i].base;
  71. phys_addr_t rgnsize = type->regions[i].size;
  72. if (memblock_addrs_overlap(base, size, rgnbase, rgnsize))
  73. break;
  74. }
  75. return (i < type->cnt) ? i : -1;
  76. }
  77. /*
  78. * __memblock_find_range_bottom_up - find free area utility in bottom-up
  79. * @start: start of candidate range
  80. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  81. * @size: size of free area to find
  82. * @align: alignment of free area to find
  83. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  84. *
  85. * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  86. *
  87. * RETURNS:
  88. * Found address on success, 0 on failure.
  89. */
  90. static phys_addr_t __init_memblock
  91. __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  92. phys_addr_t size, phys_addr_t align, int nid)
  93. {
  94. phys_addr_t this_start, this_end, cand;
  95. u64 i;
  96. for_each_free_mem_range(i, nid, &this_start, &this_end, NULL) {
  97. this_start = clamp(this_start, start, end);
  98. this_end = clamp(this_end, start, end);
  99. cand = round_up(this_start, align);
  100. if (cand < this_end && this_end - cand >= size)
  101. return cand;
  102. }
  103. return 0;
  104. }
  105. /**
  106. * __memblock_find_range_top_down - find free area utility, in top-down
  107. * @start: start of candidate range
  108. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  109. * @size: size of free area to find
  110. * @align: alignment of free area to find
  111. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  112. *
  113. * Utility called from memblock_find_in_range_node(), find free area top-down.
  114. *
  115. * RETURNS:
  116. * Found address on success, 0 on failure.
  117. */
  118. static phys_addr_t __init_memblock
  119. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  120. phys_addr_t size, phys_addr_t align, int nid)
  121. {
  122. phys_addr_t this_start, this_end, cand;
  123. u64 i;
  124. for_each_free_mem_range_reverse(i, nid, &this_start, &this_end, NULL) {
  125. this_start = clamp(this_start, start, end);
  126. this_end = clamp(this_end, start, end);
  127. if (this_end < size)
  128. continue;
  129. cand = round_down(this_end - size, align);
  130. if (cand >= this_start)
  131. return cand;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * memblock_find_in_range_node - find free area in given range and node
  137. * @start: start of candidate range
  138. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  139. * @size: size of free area to find
  140. * @align: alignment of free area to find
  141. * @nid: nid of the free area to find, %MAX_NUMNODES for any node
  142. *
  143. * Find @size free area aligned to @align in the specified range and node.
  144. *
  145. * When allocation direction is bottom-up, the @start should be greater
  146. * than the end of the kernel image. Otherwise, it will be trimmed. The
  147. * reason is that we want the bottom-up allocation just near the kernel
  148. * image so it is highly likely that the allocated memory and the kernel
  149. * will reside in the same node.
  150. *
  151. * If bottom-up allocation failed, will try to allocate memory top-down.
  152. *
  153. * RETURNS:
  154. * Found address on success, 0 on failure.
  155. */
  156. phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t start,
  157. phys_addr_t end, phys_addr_t size,
  158. phys_addr_t align, int nid)
  159. {
  160. int ret;
  161. phys_addr_t kernel_end;
  162. /* pump up @end */
  163. if (end == MEMBLOCK_ALLOC_ACCESSIBLE)
  164. end = memblock.current_limit;
  165. /* avoid allocating the first page */
  166. start = max_t(phys_addr_t, start, PAGE_SIZE);
  167. end = max(start, end);
  168. kernel_end = __pa_symbol(_end);
  169. /*
  170. * try bottom-up allocation only when bottom-up mode
  171. * is set and @end is above the kernel image.
  172. */
  173. if (memblock_bottom_up() && end > kernel_end) {
  174. phys_addr_t bottom_up_start;
  175. /* make sure we will allocate above the kernel */
  176. bottom_up_start = max(start, kernel_end);
  177. /* ok, try bottom-up allocation first */
  178. ret = __memblock_find_range_bottom_up(bottom_up_start, end,
  179. size, align, nid);
  180. if (ret)
  181. return ret;
  182. /*
  183. * we always limit bottom-up allocation above the kernel,
  184. * but top-down allocation doesn't have the limit, so
  185. * retrying top-down allocation may succeed when bottom-up
  186. * allocation failed.
  187. *
  188. * bottom-up allocation is expected to be fail very rarely,
  189. * so we use WARN_ONCE() here to see the stack trace if
  190. * fail happens.
  191. */
  192. WARN_ONCE(1, "memblock: bottom-up allocation failed, "
  193. "memory hotunplug may be affected\n");
  194. }
  195. return __memblock_find_range_top_down(start, end, size, align, nid);
  196. }
  197. /**
  198. * memblock_find_in_range - find free area in given range
  199. * @start: start of candidate range
  200. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_{ANYWHERE|ACCESSIBLE}
  201. * @size: size of free area to find
  202. * @align: alignment of free area to find
  203. *
  204. * Find @size free area aligned to @align in the specified range.
  205. *
  206. * RETURNS:
  207. * Found address on success, 0 on failure.
  208. */
  209. phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  210. phys_addr_t end, phys_addr_t size,
  211. phys_addr_t align)
  212. {
  213. return memblock_find_in_range_node(start, end, size, align,
  214. MAX_NUMNODES);
  215. }
  216. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  217. {
  218. type->total_size -= type->regions[r].size;
  219. memmove(&type->regions[r], &type->regions[r + 1],
  220. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  221. type->cnt--;
  222. /* Special case for empty arrays */
  223. if (type->cnt == 0) {
  224. WARN_ON(type->total_size != 0);
  225. type->cnt = 1;
  226. type->regions[0].base = 0;
  227. type->regions[0].size = 0;
  228. type->regions[0].flags = 0;
  229. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  230. }
  231. }
  232. phys_addr_t __init_memblock get_allocated_memblock_reserved_regions_info(
  233. phys_addr_t *addr)
  234. {
  235. if (memblock.reserved.regions == memblock_reserved_init_regions)
  236. return 0;
  237. /*
  238. * Don't allow nobootmem allocator to free reserved memory regions
  239. * array if
  240. * - CONFIG_DEBUG_FS is enabled;
  241. * - CONFIG_ARCH_DISCARD_MEMBLOCK is not enabled;
  242. * - reserved memory regions array have been resized during boot.
  243. * Otherwise debug_fs entry "sys/kernel/debug/memblock/reserved"
  244. * will show garbage instead of state of memory reservations.
  245. */
  246. if (IS_ENABLED(CONFIG_DEBUG_FS) &&
  247. !IS_ENABLED(CONFIG_ARCH_DISCARD_MEMBLOCK))
  248. return 0;
  249. *addr = __pa(memblock.reserved.regions);
  250. return PAGE_ALIGN(sizeof(struct memblock_region) *
  251. memblock.reserved.max);
  252. }
  253. /**
  254. * memblock_double_array - double the size of the memblock regions array
  255. * @type: memblock type of the regions array being doubled
  256. * @new_area_start: starting address of memory range to avoid overlap with
  257. * @new_area_size: size of memory range to avoid overlap with
  258. *
  259. * Double the size of the @type regions array. If memblock is being used to
  260. * allocate memory for a new reserved regions array and there is a previously
  261. * allocated memory range [@new_area_start,@new_area_start+@new_area_size]
  262. * waiting to be reserved, ensure the memory used by the new array does
  263. * not overlap.
  264. *
  265. * RETURNS:
  266. * 0 on success, -1 on failure.
  267. */
  268. static int __init_memblock memblock_double_array(struct memblock_type *type,
  269. phys_addr_t new_area_start,
  270. phys_addr_t new_area_size)
  271. {
  272. struct memblock_region *new_array, *old_array;
  273. phys_addr_t old_alloc_size, new_alloc_size;
  274. phys_addr_t old_size, new_size, addr;
  275. int use_slab = slab_is_available();
  276. int *in_slab;
  277. /* We don't allow resizing until we know about the reserved regions
  278. * of memory that aren't suitable for allocation
  279. */
  280. if (!memblock_can_resize)
  281. return -1;
  282. /* Calculate new doubled size */
  283. old_size = type->max * sizeof(struct memblock_region);
  284. new_size = old_size << 1;
  285. /*
  286. * We need to allocated new one align to PAGE_SIZE,
  287. * so we can free them completely later.
  288. */
  289. old_alloc_size = PAGE_ALIGN(old_size);
  290. new_alloc_size = PAGE_ALIGN(new_size);
  291. /* Retrieve the slab flag */
  292. if (type == &memblock.memory)
  293. in_slab = &memblock_memory_in_slab;
  294. else
  295. in_slab = &memblock_reserved_in_slab;
  296. /* Try to find some space for it.
  297. *
  298. * WARNING: We assume that either slab_is_available() and we use it or
  299. * we use MEMBLOCK for allocations. That means that this is unsafe to
  300. * use when bootmem is currently active (unless bootmem itself is
  301. * implemented on top of MEMBLOCK which isn't the case yet)
  302. *
  303. * This should however not be an issue for now, as we currently only
  304. * call into MEMBLOCK while it's still active, or much later when slab
  305. * is active for memory hotplug operations
  306. */
  307. if (use_slab) {
  308. new_array = kmalloc(new_size, GFP_KERNEL);
  309. addr = new_array ? __pa(new_array) : 0;
  310. } else {
  311. /* only exclude range when trying to double reserved.regions */
  312. if (type != &memblock.reserved)
  313. new_area_start = new_area_size = 0;
  314. addr = memblock_find_in_range(new_area_start + new_area_size,
  315. memblock.current_limit,
  316. new_alloc_size, PAGE_SIZE);
  317. if (!addr && new_area_size)
  318. addr = memblock_find_in_range(0,
  319. min(new_area_start, memblock.current_limit),
  320. new_alloc_size, PAGE_SIZE);
  321. new_array = addr ? __va(addr) : NULL;
  322. }
  323. if (!addr) {
  324. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  325. memblock_type_name(type), type->max, type->max * 2);
  326. return -1;
  327. }
  328. memblock_dbg("memblock: %s is doubled to %ld at [%#010llx-%#010llx]",
  329. memblock_type_name(type), type->max * 2, (u64)addr,
  330. (u64)addr + new_size - 1);
  331. /*
  332. * Found space, we now need to move the array over before we add the
  333. * reserved region since it may be our reserved array itself that is
  334. * full.
  335. */
  336. memcpy(new_array, type->regions, old_size);
  337. memset(new_array + type->max, 0, old_size);
  338. old_array = type->regions;
  339. type->regions = new_array;
  340. type->max <<= 1;
  341. /* Free old array. We needn't free it if the array is the static one */
  342. if (*in_slab)
  343. kfree(old_array);
  344. else if (old_array != memblock_memory_init_regions &&
  345. old_array != memblock_reserved_init_regions)
  346. memblock_free(__pa(old_array), old_alloc_size);
  347. /*
  348. * Reserve the new array if that comes from the memblock. Otherwise, we
  349. * needn't do it
  350. */
  351. if (!use_slab)
  352. BUG_ON(memblock_reserve(addr, new_alloc_size));
  353. /* Update slab flag */
  354. *in_slab = use_slab;
  355. return 0;
  356. }
  357. /**
  358. * memblock_merge_regions - merge neighboring compatible regions
  359. * @type: memblock type to scan
  360. *
  361. * Scan @type and merge neighboring compatible regions.
  362. */
  363. static void __init_memblock memblock_merge_regions(struct memblock_type *type)
  364. {
  365. int i = 0;
  366. /* cnt never goes below 1 */
  367. while (i < type->cnt - 1) {
  368. struct memblock_region *this = &type->regions[i];
  369. struct memblock_region *next = &type->regions[i + 1];
  370. if (this->base + this->size != next->base ||
  371. memblock_get_region_node(this) !=
  372. memblock_get_region_node(next) ||
  373. this->flags != next->flags) {
  374. BUG_ON(this->base + this->size > next->base);
  375. i++;
  376. continue;
  377. }
  378. this->size += next->size;
  379. /* move forward from next + 1, index of which is i + 2 */
  380. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  381. type->cnt--;
  382. }
  383. }
  384. /**
  385. * memblock_insert_region - insert new memblock region
  386. * @type: memblock type to insert into
  387. * @idx: index for the insertion point
  388. * @base: base address of the new region
  389. * @size: size of the new region
  390. * @nid: node id of the new region
  391. * @flags: flags of the new region
  392. *
  393. * Insert new memblock region [@base,@base+@size) into @type at @idx.
  394. * @type must already have extra room to accomodate the new region.
  395. */
  396. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  397. int idx, phys_addr_t base,
  398. phys_addr_t size,
  399. int nid, unsigned long flags)
  400. {
  401. struct memblock_region *rgn = &type->regions[idx];
  402. BUG_ON(type->cnt >= type->max);
  403. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  404. rgn->base = base;
  405. rgn->size = size;
  406. rgn->flags = flags;
  407. memblock_set_region_node(rgn, nid);
  408. type->cnt++;
  409. type->total_size += size;
  410. }
  411. /**
  412. * memblock_add_region - add new memblock region
  413. * @type: memblock type to add new region into
  414. * @base: base address of the new region
  415. * @size: size of the new region
  416. * @nid: nid of the new region
  417. * @flags: flags of the new region
  418. *
  419. * Add new memblock region [@base,@base+@size) into @type. The new region
  420. * is allowed to overlap with existing ones - overlaps don't affect already
  421. * existing regions. @type is guaranteed to be minimal (all neighbouring
  422. * compatible regions are merged) after the addition.
  423. *
  424. * RETURNS:
  425. * 0 on success, -errno on failure.
  426. */
  427. static int __init_memblock memblock_add_region(struct memblock_type *type,
  428. phys_addr_t base, phys_addr_t size,
  429. int nid, unsigned long flags)
  430. {
  431. bool insert = false;
  432. phys_addr_t obase = base;
  433. phys_addr_t end = base + memblock_cap_size(base, &size);
  434. int i, nr_new;
  435. if (!size)
  436. return 0;
  437. /* special case for empty array */
  438. if (type->regions[0].size == 0) {
  439. WARN_ON(type->cnt != 1 || type->total_size);
  440. type->regions[0].base = base;
  441. type->regions[0].size = size;
  442. type->regions[0].flags = flags;
  443. memblock_set_region_node(&type->regions[0], nid);
  444. type->total_size = size;
  445. return 0;
  446. }
  447. repeat:
  448. /*
  449. * The following is executed twice. Once with %false @insert and
  450. * then with %true. The first counts the number of regions needed
  451. * to accomodate the new area. The second actually inserts them.
  452. */
  453. base = obase;
  454. nr_new = 0;
  455. for (i = 0; i < type->cnt; i++) {
  456. struct memblock_region *rgn = &type->regions[i];
  457. phys_addr_t rbase = rgn->base;
  458. phys_addr_t rend = rbase + rgn->size;
  459. if (rbase >= end)
  460. break;
  461. if (rend <= base)
  462. continue;
  463. /*
  464. * @rgn overlaps. If it separates the lower part of new
  465. * area, insert that portion.
  466. */
  467. if (rbase > base) {
  468. nr_new++;
  469. if (insert)
  470. memblock_insert_region(type, i++, base,
  471. rbase - base, nid,
  472. flags);
  473. }
  474. /* area below @rend is dealt with, forget about it */
  475. base = min(rend, end);
  476. }
  477. /* insert the remaining portion */
  478. if (base < end) {
  479. nr_new++;
  480. if (insert)
  481. memblock_insert_region(type, i, base, end - base,
  482. nid, flags);
  483. }
  484. /*
  485. * If this was the first round, resize array and repeat for actual
  486. * insertions; otherwise, merge and return.
  487. */
  488. if (!insert) {
  489. while (type->cnt + nr_new > type->max)
  490. if (memblock_double_array(type, obase, size) < 0)
  491. return -ENOMEM;
  492. insert = true;
  493. goto repeat;
  494. } else {
  495. memblock_merge_regions(type);
  496. return 0;
  497. }
  498. }
  499. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  500. int nid)
  501. {
  502. return memblock_add_region(&memblock.memory, base, size, nid, 0);
  503. }
  504. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  505. {
  506. return memblock_add_region(&memblock.memory, base, size,
  507. MAX_NUMNODES, 0);
  508. }
  509. /**
  510. * memblock_isolate_range - isolate given range into disjoint memblocks
  511. * @type: memblock type to isolate range for
  512. * @base: base of range to isolate
  513. * @size: size of range to isolate
  514. * @start_rgn: out parameter for the start of isolated region
  515. * @end_rgn: out parameter for the end of isolated region
  516. *
  517. * Walk @type and ensure that regions don't cross the boundaries defined by
  518. * [@base,@base+@size). Crossing regions are split at the boundaries,
  519. * which may create at most two more regions. The index of the first
  520. * region inside the range is returned in *@start_rgn and end in *@end_rgn.
  521. *
  522. * RETURNS:
  523. * 0 on success, -errno on failure.
  524. */
  525. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  526. phys_addr_t base, phys_addr_t size,
  527. int *start_rgn, int *end_rgn)
  528. {
  529. phys_addr_t end = base + memblock_cap_size(base, &size);
  530. int i;
  531. *start_rgn = *end_rgn = 0;
  532. if (!size)
  533. return 0;
  534. /* we'll create at most two more regions */
  535. while (type->cnt + 2 > type->max)
  536. if (memblock_double_array(type, base, size) < 0)
  537. return -ENOMEM;
  538. for (i = 0; i < type->cnt; i++) {
  539. struct memblock_region *rgn = &type->regions[i];
  540. phys_addr_t rbase = rgn->base;
  541. phys_addr_t rend = rbase + rgn->size;
  542. if (rbase >= end)
  543. break;
  544. if (rend <= base)
  545. continue;
  546. if (rbase < base) {
  547. /*
  548. * @rgn intersects from below. Split and continue
  549. * to process the next region - the new top half.
  550. */
  551. rgn->base = base;
  552. rgn->size -= base - rbase;
  553. type->total_size -= base - rbase;
  554. memblock_insert_region(type, i, rbase, base - rbase,
  555. memblock_get_region_node(rgn),
  556. rgn->flags);
  557. } else if (rend > end) {
  558. /*
  559. * @rgn intersects from above. Split and redo the
  560. * current region - the new bottom half.
  561. */
  562. rgn->base = end;
  563. rgn->size -= end - rbase;
  564. type->total_size -= end - rbase;
  565. memblock_insert_region(type, i--, rbase, end - rbase,
  566. memblock_get_region_node(rgn),
  567. rgn->flags);
  568. } else {
  569. /* @rgn is fully contained, record it */
  570. if (!*end_rgn)
  571. *start_rgn = i;
  572. *end_rgn = i + 1;
  573. }
  574. }
  575. return 0;
  576. }
  577. static int __init_memblock __memblock_remove(struct memblock_type *type,
  578. phys_addr_t base, phys_addr_t size)
  579. {
  580. int start_rgn, end_rgn;
  581. int i, ret;
  582. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  583. if (ret)
  584. return ret;
  585. for (i = end_rgn - 1; i >= start_rgn; i--)
  586. memblock_remove_region(type, i);
  587. return 0;
  588. }
  589. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  590. {
  591. return __memblock_remove(&memblock.memory, base, size);
  592. }
  593. int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size)
  594. {
  595. memblock_dbg(" memblock_free: [%#016llx-%#016llx] %pF\n",
  596. (unsigned long long)base,
  597. (unsigned long long)base + size - 1,
  598. (void *)_RET_IP_);
  599. return __memblock_remove(&memblock.reserved, base, size);
  600. }
  601. static int __init_memblock memblock_reserve_region(phys_addr_t base,
  602. phys_addr_t size,
  603. int nid,
  604. unsigned long flags)
  605. {
  606. struct memblock_type *_rgn = &memblock.reserved;
  607. memblock_dbg("memblock_reserve: [%#016llx-%#016llx] flags %#02lx %pF\n",
  608. (unsigned long long)base,
  609. (unsigned long long)base + size - 1,
  610. flags, (void *)_RET_IP_);
  611. return memblock_add_region(_rgn, base, size, nid, flags);
  612. }
  613. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  614. {
  615. return memblock_reserve_region(base, size, MAX_NUMNODES, 0);
  616. }
  617. /**
  618. * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
  619. * @base: the base phys addr of the region
  620. * @size: the size of the region
  621. *
  622. * This function isolates region [@base, @base + @size), and mark it with flag
  623. * MEMBLOCK_HOTPLUG.
  624. *
  625. * Return 0 on succees, -errno on failure.
  626. */
  627. int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
  628. {
  629. struct memblock_type *type = &memblock.memory;
  630. int i, ret, start_rgn, end_rgn;
  631. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  632. if (ret)
  633. return ret;
  634. for (i = start_rgn; i < end_rgn; i++)
  635. memblock_set_region_flags(&type->regions[i], MEMBLOCK_HOTPLUG);
  636. memblock_merge_regions(type);
  637. return 0;
  638. }
  639. /**
  640. * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
  641. * @base: the base phys addr of the region
  642. * @size: the size of the region
  643. *
  644. * This function isolates region [@base, @base + @size), and clear flag
  645. * MEMBLOCK_HOTPLUG for the isolated regions.
  646. *
  647. * Return 0 on succees, -errno on failure.
  648. */
  649. int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
  650. {
  651. struct memblock_type *type = &memblock.memory;
  652. int i, ret, start_rgn, end_rgn;
  653. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  654. if (ret)
  655. return ret;
  656. for (i = start_rgn; i < end_rgn; i++)
  657. memblock_clear_region_flags(&type->regions[i],
  658. MEMBLOCK_HOTPLUG);
  659. memblock_merge_regions(type);
  660. return 0;
  661. }
  662. /**
  663. * __next_free_mem_range - next function for for_each_free_mem_range()
  664. * @idx: pointer to u64 loop variable
  665. * @nid: node selector, %MAX_NUMNODES for all nodes
  666. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  667. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  668. * @out_nid: ptr to int for nid of the range, can be %NULL
  669. *
  670. * Find the first free area from *@idx which matches @nid, fill the out
  671. * parameters, and update *@idx for the next iteration. The lower 32bit of
  672. * *@idx contains index into memory region and the upper 32bit indexes the
  673. * areas before each reserved region. For example, if reserved regions
  674. * look like the following,
  675. *
  676. * 0:[0-16), 1:[32-48), 2:[128-130)
  677. *
  678. * The upper 32bit indexes the following regions.
  679. *
  680. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  681. *
  682. * As both region arrays are sorted, the function advances the two indices
  683. * in lockstep and returns each intersection.
  684. */
  685. void __init_memblock __next_free_mem_range(u64 *idx, int nid,
  686. phys_addr_t *out_start,
  687. phys_addr_t *out_end, int *out_nid)
  688. {
  689. struct memblock_type *mem = &memblock.memory;
  690. struct memblock_type *rsv = &memblock.reserved;
  691. int mi = *idx & 0xffffffff;
  692. int ri = *idx >> 32;
  693. for ( ; mi < mem->cnt; mi++) {
  694. struct memblock_region *m = &mem->regions[mi];
  695. phys_addr_t m_start = m->base;
  696. phys_addr_t m_end = m->base + m->size;
  697. /* only memory regions are associated with nodes, check it */
  698. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  699. continue;
  700. /* scan areas before each reservation for intersection */
  701. for ( ; ri < rsv->cnt + 1; ri++) {
  702. struct memblock_region *r = &rsv->regions[ri];
  703. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  704. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  705. /* if ri advanced past mi, break out to advance mi */
  706. if (r_start >= m_end)
  707. break;
  708. /* if the two regions intersect, we're done */
  709. if (m_start < r_end) {
  710. if (out_start)
  711. *out_start = max(m_start, r_start);
  712. if (out_end)
  713. *out_end = min(m_end, r_end);
  714. if (out_nid)
  715. *out_nid = memblock_get_region_node(m);
  716. /*
  717. * The region which ends first is advanced
  718. * for the next iteration.
  719. */
  720. if (m_end <= r_end)
  721. mi++;
  722. else
  723. ri++;
  724. *idx = (u32)mi | (u64)ri << 32;
  725. return;
  726. }
  727. }
  728. }
  729. /* signal end of iteration */
  730. *idx = ULLONG_MAX;
  731. }
  732. /**
  733. * __next_free_mem_range_rev - next function for for_each_free_mem_range_reverse()
  734. * @idx: pointer to u64 loop variable
  735. * @nid: nid: node selector, %MAX_NUMNODES for all nodes
  736. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  737. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  738. * @out_nid: ptr to int for nid of the range, can be %NULL
  739. *
  740. * Reverse of __next_free_mem_range().
  741. *
  742. * Linux kernel cannot migrate pages used by itself. Memory hotplug users won't
  743. * be able to hot-remove hotpluggable memory used by the kernel. So this
  744. * function skip hotpluggable regions if needed when allocating memory for the
  745. * kernel.
  746. */
  747. void __init_memblock __next_free_mem_range_rev(u64 *idx, int nid,
  748. phys_addr_t *out_start,
  749. phys_addr_t *out_end, int *out_nid)
  750. {
  751. struct memblock_type *mem = &memblock.memory;
  752. struct memblock_type *rsv = &memblock.reserved;
  753. int mi = *idx & 0xffffffff;
  754. int ri = *idx >> 32;
  755. if (*idx == (u64)ULLONG_MAX) {
  756. mi = mem->cnt - 1;
  757. ri = rsv->cnt;
  758. }
  759. for ( ; mi >= 0; mi--) {
  760. struct memblock_region *m = &mem->regions[mi];
  761. phys_addr_t m_start = m->base;
  762. phys_addr_t m_end = m->base + m->size;
  763. /* only memory regions are associated with nodes, check it */
  764. if (nid != MAX_NUMNODES && nid != memblock_get_region_node(m))
  765. continue;
  766. /* skip hotpluggable memory regions if needed */
  767. if (movable_node_is_enabled() && memblock_is_hotpluggable(m))
  768. continue;
  769. /* scan areas before each reservation for intersection */
  770. for ( ; ri >= 0; ri--) {
  771. struct memblock_region *r = &rsv->regions[ri];
  772. phys_addr_t r_start = ri ? r[-1].base + r[-1].size : 0;
  773. phys_addr_t r_end = ri < rsv->cnt ? r->base : ULLONG_MAX;
  774. /* if ri advanced past mi, break out to advance mi */
  775. if (r_end <= m_start)
  776. break;
  777. /* if the two regions intersect, we're done */
  778. if (m_end > r_start) {
  779. if (out_start)
  780. *out_start = max(m_start, r_start);
  781. if (out_end)
  782. *out_end = min(m_end, r_end);
  783. if (out_nid)
  784. *out_nid = memblock_get_region_node(m);
  785. if (m_start >= r_start)
  786. mi--;
  787. else
  788. ri--;
  789. *idx = (u32)mi | (u64)ri << 32;
  790. return;
  791. }
  792. }
  793. }
  794. *idx = ULLONG_MAX;
  795. }
  796. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  797. /*
  798. * Common iterator interface used to define for_each_mem_range().
  799. */
  800. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  801. unsigned long *out_start_pfn,
  802. unsigned long *out_end_pfn, int *out_nid)
  803. {
  804. struct memblock_type *type = &memblock.memory;
  805. struct memblock_region *r;
  806. while (++*idx < type->cnt) {
  807. r = &type->regions[*idx];
  808. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  809. continue;
  810. if (nid == MAX_NUMNODES || nid == r->nid)
  811. break;
  812. }
  813. if (*idx >= type->cnt) {
  814. *idx = -1;
  815. return;
  816. }
  817. if (out_start_pfn)
  818. *out_start_pfn = PFN_UP(r->base);
  819. if (out_end_pfn)
  820. *out_end_pfn = PFN_DOWN(r->base + r->size);
  821. if (out_nid)
  822. *out_nid = r->nid;
  823. }
  824. /**
  825. * memblock_set_node - set node ID on memblock regions
  826. * @base: base of area to set node ID for
  827. * @size: size of area to set node ID for
  828. * @type: memblock type to set node ID for
  829. * @nid: node ID to set
  830. *
  831. * Set the nid of memblock @type regions in [@base,@base+@size) to @nid.
  832. * Regions which cross the area boundaries are split as necessary.
  833. *
  834. * RETURNS:
  835. * 0 on success, -errno on failure.
  836. */
  837. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  838. struct memblock_type *type, int nid)
  839. {
  840. int start_rgn, end_rgn;
  841. int i, ret;
  842. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  843. if (ret)
  844. return ret;
  845. for (i = start_rgn; i < end_rgn; i++)
  846. memblock_set_region_node(&type->regions[i], nid);
  847. memblock_merge_regions(type);
  848. return 0;
  849. }
  850. #endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */
  851. static phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size,
  852. phys_addr_t align, phys_addr_t max_addr,
  853. int nid)
  854. {
  855. phys_addr_t found;
  856. if (WARN_ON(!align))
  857. align = __alignof__(long long);
  858. /* align @size to avoid excessive fragmentation on reserved array */
  859. size = round_up(size, align);
  860. found = memblock_find_in_range_node(0, max_addr, size, align, nid);
  861. if (found && !memblock_reserve(found, size))
  862. return found;
  863. return 0;
  864. }
  865. phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid)
  866. {
  867. return memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, nid);
  868. }
  869. phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  870. {
  871. return memblock_alloc_base_nid(size, align, max_addr, MAX_NUMNODES);
  872. }
  873. phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr)
  874. {
  875. phys_addr_t alloc;
  876. alloc = __memblock_alloc_base(size, align, max_addr);
  877. if (alloc == 0)
  878. panic("ERROR: Failed to allocate 0x%llx bytes below 0x%llx.\n",
  879. (unsigned long long) size, (unsigned long long) max_addr);
  880. return alloc;
  881. }
  882. phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align)
  883. {
  884. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  885. }
  886. phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  887. {
  888. phys_addr_t res = memblock_alloc_nid(size, align, nid);
  889. if (res)
  890. return res;
  891. return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE);
  892. }
  893. /*
  894. * Remaining API functions
  895. */
  896. phys_addr_t __init memblock_phys_mem_size(void)
  897. {
  898. return memblock.memory.total_size;
  899. }
  900. phys_addr_t __init memblock_mem_size(unsigned long limit_pfn)
  901. {
  902. unsigned long pages = 0;
  903. struct memblock_region *r;
  904. unsigned long start_pfn, end_pfn;
  905. for_each_memblock(memory, r) {
  906. start_pfn = memblock_region_memory_base_pfn(r);
  907. end_pfn = memblock_region_memory_end_pfn(r);
  908. start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
  909. end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
  910. pages += end_pfn - start_pfn;
  911. }
  912. return (phys_addr_t)pages << PAGE_SHIFT;
  913. }
  914. /* lowest address */
  915. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  916. {
  917. return memblock.memory.regions[0].base;
  918. }
  919. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  920. {
  921. int idx = memblock.memory.cnt - 1;
  922. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  923. }
  924. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  925. {
  926. unsigned long i;
  927. phys_addr_t max_addr = (phys_addr_t)ULLONG_MAX;
  928. if (!limit)
  929. return;
  930. /* find out max address */
  931. for (i = 0; i < memblock.memory.cnt; i++) {
  932. struct memblock_region *r = &memblock.memory.regions[i];
  933. if (limit <= r->size) {
  934. max_addr = r->base + limit;
  935. break;
  936. }
  937. limit -= r->size;
  938. }
  939. /* truncate both memory and reserved regions */
  940. __memblock_remove(&memblock.memory, max_addr, (phys_addr_t)ULLONG_MAX);
  941. __memblock_remove(&memblock.reserved, max_addr, (phys_addr_t)ULLONG_MAX);
  942. }
  943. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  944. {
  945. unsigned int left = 0, right = type->cnt;
  946. do {
  947. unsigned int mid = (right + left) / 2;
  948. if (addr < type->regions[mid].base)
  949. right = mid;
  950. else if (addr >= (type->regions[mid].base +
  951. type->regions[mid].size))
  952. left = mid + 1;
  953. else
  954. return mid;
  955. } while (left < right);
  956. return -1;
  957. }
  958. int __init memblock_is_reserved(phys_addr_t addr)
  959. {
  960. return memblock_search(&memblock.reserved, addr) != -1;
  961. }
  962. int __init_memblock memblock_is_memory(phys_addr_t addr)
  963. {
  964. return memblock_search(&memblock.memory, addr) != -1;
  965. }
  966. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  967. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  968. unsigned long *start_pfn, unsigned long *end_pfn)
  969. {
  970. struct memblock_type *type = &memblock.memory;
  971. int mid = memblock_search(type, (phys_addr_t)pfn << PAGE_SHIFT);
  972. if (mid == -1)
  973. return -1;
  974. *start_pfn = type->regions[mid].base >> PAGE_SHIFT;
  975. *end_pfn = (type->regions[mid].base + type->regions[mid].size)
  976. >> PAGE_SHIFT;
  977. return type->regions[mid].nid;
  978. }
  979. #endif
  980. /**
  981. * memblock_is_region_memory - check if a region is a subset of memory
  982. * @base: base of region to check
  983. * @size: size of region to check
  984. *
  985. * Check if the region [@base, @base+@size) is a subset of a memory block.
  986. *
  987. * RETURNS:
  988. * 0 if false, non-zero if true
  989. */
  990. int __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  991. {
  992. int idx = memblock_search(&memblock.memory, base);
  993. phys_addr_t end = base + memblock_cap_size(base, &size);
  994. if (idx == -1)
  995. return 0;
  996. return memblock.memory.regions[idx].base <= base &&
  997. (memblock.memory.regions[idx].base +
  998. memblock.memory.regions[idx].size) >= end;
  999. }
  1000. /**
  1001. * memblock_is_region_reserved - check if a region intersects reserved memory
  1002. * @base: base of region to check
  1003. * @size: size of region to check
  1004. *
  1005. * Check if the region [@base, @base+@size) intersects a reserved memory block.
  1006. *
  1007. * RETURNS:
  1008. * 0 if false, non-zero if true
  1009. */
  1010. int __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  1011. {
  1012. memblock_cap_size(base, &size);
  1013. return memblock_overlaps_region(&memblock.reserved, base, size) >= 0;
  1014. }
  1015. void __init_memblock memblock_trim_memory(phys_addr_t align)
  1016. {
  1017. int i;
  1018. phys_addr_t start, end, orig_start, orig_end;
  1019. struct memblock_type *mem = &memblock.memory;
  1020. for (i = 0; i < mem->cnt; i++) {
  1021. orig_start = mem->regions[i].base;
  1022. orig_end = mem->regions[i].base + mem->regions[i].size;
  1023. start = round_up(orig_start, align);
  1024. end = round_down(orig_end, align);
  1025. if (start == orig_start && end == orig_end)
  1026. continue;
  1027. if (start < end) {
  1028. mem->regions[i].base = start;
  1029. mem->regions[i].size = end - start;
  1030. } else {
  1031. memblock_remove_region(mem, i);
  1032. i--;
  1033. }
  1034. }
  1035. }
  1036. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  1037. {
  1038. memblock.current_limit = limit;
  1039. }
  1040. static void __init_memblock memblock_dump(struct memblock_type *type, char *name)
  1041. {
  1042. unsigned long long base, size;
  1043. unsigned long flags;
  1044. int i;
  1045. pr_info(" %s.cnt = 0x%lx\n", name, type->cnt);
  1046. for (i = 0; i < type->cnt; i++) {
  1047. struct memblock_region *rgn = &type->regions[i];
  1048. char nid_buf[32] = "";
  1049. base = rgn->base;
  1050. size = rgn->size;
  1051. flags = rgn->flags;
  1052. #ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP
  1053. if (memblock_get_region_node(rgn) != MAX_NUMNODES)
  1054. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  1055. memblock_get_region_node(rgn));
  1056. #endif
  1057. pr_info(" %s[%#x]\t[%#016llx-%#016llx], %#llx bytes%s flags: %#lx\n",
  1058. name, i, base, base + size - 1, size, nid_buf, flags);
  1059. }
  1060. }
  1061. void __init_memblock __memblock_dump_all(void)
  1062. {
  1063. pr_info("MEMBLOCK configuration:\n");
  1064. pr_info(" memory size = %#llx reserved size = %#llx\n",
  1065. (unsigned long long)memblock.memory.total_size,
  1066. (unsigned long long)memblock.reserved.total_size);
  1067. memblock_dump(&memblock.memory, "memory");
  1068. memblock_dump(&memblock.reserved, "reserved");
  1069. }
  1070. void __init memblock_allow_resize(void)
  1071. {
  1072. memblock_can_resize = 1;
  1073. }
  1074. static int __init early_memblock(char *p)
  1075. {
  1076. if (p && strstr(p, "debug"))
  1077. memblock_debug = 1;
  1078. return 0;
  1079. }
  1080. early_param("memblock", early_memblock);
  1081. #if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK)
  1082. static int memblock_debug_show(struct seq_file *m, void *private)
  1083. {
  1084. struct memblock_type *type = m->private;
  1085. struct memblock_region *reg;
  1086. int i;
  1087. for (i = 0; i < type->cnt; i++) {
  1088. reg = &type->regions[i];
  1089. seq_printf(m, "%4d: ", i);
  1090. if (sizeof(phys_addr_t) == 4)
  1091. seq_printf(m, "0x%08lx..0x%08lx\n",
  1092. (unsigned long)reg->base,
  1093. (unsigned long)(reg->base + reg->size - 1));
  1094. else
  1095. seq_printf(m, "0x%016llx..0x%016llx\n",
  1096. (unsigned long long)reg->base,
  1097. (unsigned long long)(reg->base + reg->size - 1));
  1098. }
  1099. return 0;
  1100. }
  1101. static int memblock_debug_open(struct inode *inode, struct file *file)
  1102. {
  1103. return single_open(file, memblock_debug_show, inode->i_private);
  1104. }
  1105. static const struct file_operations memblock_debug_fops = {
  1106. .open = memblock_debug_open,
  1107. .read = seq_read,
  1108. .llseek = seq_lseek,
  1109. .release = single_release,
  1110. };
  1111. static int __init memblock_init_debugfs(void)
  1112. {
  1113. struct dentry *root = debugfs_create_dir("memblock", NULL);
  1114. if (!root)
  1115. return -ENXIO;
  1116. debugfs_create_file("memory", S_IRUGO, root, &memblock.memory, &memblock_debug_fops);
  1117. debugfs_create_file("reserved", S_IRUGO, root, &memblock.reserved, &memblock_debug_fops);
  1118. return 0;
  1119. }
  1120. __initcall(memblock_init_debugfs);
  1121. #endif /* CONFIG_DEBUG_FS */