memblock.c 48 KB

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