memblock.c 43 KB

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