memblock.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Procedures for maintaining information about logical memory blocks.
  4. *
  5. * Peter Bergner, IBM Corp. June 2001.
  6. * Copyright (C) 2001 Peter Bergner.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/init.h>
  11. #include <linux/bitops.h>
  12. #include <linux/poison.h>
  13. #include <linux/pfn.h>
  14. #include <linux/debugfs.h>
  15. #include <linux/kmemleak.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/memblock.h>
  18. #include <asm/sections.h>
  19. #include <linux/io.h>
  20. #include "internal.h"
  21. #define INIT_MEMBLOCK_REGIONS 128
  22. #define INIT_PHYSMEM_REGIONS 4
  23. #ifndef INIT_MEMBLOCK_RESERVED_REGIONS
  24. # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS
  25. #endif
  26. #ifndef INIT_MEMBLOCK_MEMORY_REGIONS
  27. #define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS
  28. #endif
  29. /**
  30. * DOC: memblock overview
  31. *
  32. * Memblock is a method of managing memory regions during the early
  33. * boot period when the usual kernel memory allocators are not up and
  34. * running.
  35. *
  36. * Memblock views the system memory as collections of contiguous
  37. * regions. There are several types of these collections:
  38. *
  39. * * ``memory`` - describes the physical memory available to the
  40. * kernel; this may differ from the actual physical memory installed
  41. * in the system, for instance when the memory is restricted with
  42. * ``mem=`` command line parameter
  43. * * ``reserved`` - describes the regions that were allocated
  44. * * ``physmem`` - describes the actual physical memory available during
  45. * boot regardless of the possible restrictions and memory hot(un)plug;
  46. * the ``physmem`` type is only available on some architectures.
  47. *
  48. * Each region is represented by struct memblock_region that
  49. * defines the region extents, its attributes and NUMA node id on NUMA
  50. * systems. Every memory type is described by the struct memblock_type
  51. * which contains an array of memory regions along with
  52. * the allocator metadata. The "memory" and "reserved" types are nicely
  53. * wrapped with struct memblock. This structure is statically
  54. * initialized at build time. The region arrays are initially sized to
  55. * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and
  56. * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array
  57. * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS.
  58. * The memblock_allow_resize() enables automatic resizing of the region
  59. * arrays during addition of new regions. This feature should be used
  60. * with care so that memory allocated for the region array will not
  61. * overlap with areas that should be reserved, for example initrd.
  62. *
  63. * The early architecture setup should tell memblock what the physical
  64. * memory layout is by using memblock_add() or memblock_add_node()
  65. * functions. The first function does not assign the region to a NUMA
  66. * node and it is appropriate for UMA systems. Yet, it is possible to
  67. * use it on NUMA systems as well and assign the region to a NUMA node
  68. * later in the setup process using memblock_set_node(). The
  69. * memblock_add_node() performs such an assignment directly.
  70. *
  71. * Once memblock is setup the memory can be allocated using one of the
  72. * API variants:
  73. *
  74. * * memblock_phys_alloc*() - these functions return the **physical**
  75. * address of the allocated memory
  76. * * memblock_alloc*() - these functions return the **virtual** address
  77. * of the allocated memory.
  78. *
  79. * Note, that both API variants use implicit assumptions about allowed
  80. * memory ranges and the fallback methods. Consult the documentation
  81. * of memblock_alloc_internal() and memblock_alloc_range_nid()
  82. * functions for more elaborate description.
  83. *
  84. * As the system boot progresses, the architecture specific mem_init()
  85. * function frees all the memory to the buddy page allocator.
  86. *
  87. * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the
  88. * memblock data structures (except "physmem") will be discarded after the
  89. * system initialization completes.
  90. */
  91. #ifndef CONFIG_NUMA
  92. struct pglist_data __refdata contig_page_data;
  93. EXPORT_SYMBOL(contig_page_data);
  94. #endif
  95. unsigned long max_low_pfn;
  96. unsigned long min_low_pfn;
  97. unsigned long max_pfn;
  98. unsigned long long max_possible_pfn;
  99. static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock;
  100. static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock;
  101. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  102. static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS];
  103. #endif
  104. struct memblock memblock __initdata_memblock = {
  105. .memory.regions = memblock_memory_init_regions,
  106. .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS,
  107. .memory.name = "memory",
  108. .reserved.regions = memblock_reserved_init_regions,
  109. .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS,
  110. .reserved.name = "reserved",
  111. .bottom_up = false,
  112. .current_limit = MEMBLOCK_ALLOC_ANYWHERE,
  113. };
  114. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  115. struct memblock_type physmem = {
  116. .regions = memblock_physmem_init_regions,
  117. .max = INIT_PHYSMEM_REGIONS,
  118. .name = "physmem",
  119. };
  120. #endif
  121. /*
  122. * keep a pointer to &memblock.memory in the text section to use it in
  123. * __next_mem_range() and its helpers.
  124. * For architectures that do not keep memblock data after init, this
  125. * pointer will be reset to NULL at memblock_discard()
  126. */
  127. static __refdata struct memblock_type *memblock_memory = &memblock.memory;
  128. #define for_each_memblock_type(i, memblock_type, rgn) \
  129. for (i = 0, rgn = &memblock_type->regions[0]; \
  130. i < memblock_type->cnt; \
  131. i++, rgn = &memblock_type->regions[i])
  132. #define memblock_dbg(fmt, ...) \
  133. do { \
  134. if (memblock_debug) \
  135. pr_info(fmt, ##__VA_ARGS__); \
  136. } while (0)
  137. static int memblock_debug __initdata_memblock;
  138. static bool system_has_some_mirror __initdata_memblock;
  139. static int memblock_can_resize __initdata_memblock;
  140. static int memblock_memory_in_slab __initdata_memblock;
  141. static int memblock_reserved_in_slab __initdata_memblock;
  142. bool __init_memblock memblock_has_mirror(void)
  143. {
  144. return system_has_some_mirror;
  145. }
  146. static enum memblock_flags __init_memblock choose_memblock_flags(void)
  147. {
  148. return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE;
  149. }
  150. /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */
  151. static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size)
  152. {
  153. return *size = min(*size, PHYS_ADDR_MAX - base);
  154. }
  155. /*
  156. * Address comparison utilities
  157. */
  158. unsigned long __init_memblock
  159. memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2,
  160. phys_addr_t size2)
  161. {
  162. return ((base1 < (base2 + size2)) && (base2 < (base1 + size1)));
  163. }
  164. bool __init_memblock memblock_overlaps_region(struct memblock_type *type,
  165. phys_addr_t base, phys_addr_t size)
  166. {
  167. unsigned long i;
  168. memblock_cap_size(base, &size);
  169. for (i = 0; i < type->cnt; i++)
  170. if (memblock_addrs_overlap(base, size, type->regions[i].base,
  171. type->regions[i].size))
  172. return true;
  173. return false;
  174. }
  175. /**
  176. * __memblock_find_range_bottom_up - find free area utility in bottom-up
  177. * @start: start of candidate range
  178. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  179. * %MEMBLOCK_ALLOC_ACCESSIBLE
  180. * @size: size of free area to find
  181. * @align: alignment of free area to find
  182. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  183. * @flags: pick from blocks based on memory attributes
  184. *
  185. * Utility called from memblock_find_in_range_node(), find free area bottom-up.
  186. *
  187. * Return:
  188. * Found address on success, 0 on failure.
  189. */
  190. static phys_addr_t __init_memblock
  191. __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end,
  192. phys_addr_t size, phys_addr_t align, int nid,
  193. enum memblock_flags flags)
  194. {
  195. phys_addr_t this_start, this_end, cand;
  196. u64 i;
  197. for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) {
  198. this_start = clamp(this_start, start, end);
  199. this_end = clamp(this_end, start, end);
  200. cand = round_up(this_start, align);
  201. if (cand < this_end && this_end - cand >= size)
  202. return cand;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * __memblock_find_range_top_down - find free area utility, in top-down
  208. * @start: start of candidate range
  209. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  210. * %MEMBLOCK_ALLOC_ACCESSIBLE
  211. * @size: size of free area to find
  212. * @align: alignment of free area to find
  213. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  214. * @flags: pick from blocks based on memory attributes
  215. *
  216. * Utility called from memblock_find_in_range_node(), find free area top-down.
  217. *
  218. * Return:
  219. * Found address on success, 0 on failure.
  220. */
  221. static phys_addr_t __init_memblock
  222. __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end,
  223. phys_addr_t size, phys_addr_t align, int nid,
  224. enum memblock_flags flags)
  225. {
  226. phys_addr_t this_start, this_end, cand;
  227. u64 i;
  228. for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end,
  229. NULL) {
  230. this_start = clamp(this_start, start, end);
  231. this_end = clamp(this_end, start, end);
  232. if (this_end < size)
  233. continue;
  234. cand = round_down(this_end - size, align);
  235. if (cand >= this_start)
  236. return cand;
  237. }
  238. return 0;
  239. }
  240. /**
  241. * memblock_find_in_range_node - find free area in given range and node
  242. * @size: size of free area to find
  243. * @align: alignment of free area to find
  244. * @start: start of candidate range
  245. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  246. * %MEMBLOCK_ALLOC_ACCESSIBLE
  247. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  248. * @flags: pick from blocks based on memory attributes
  249. *
  250. * Find @size free area aligned to @align in the specified range and node.
  251. *
  252. * Return:
  253. * Found address on success, 0 on failure.
  254. */
  255. static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size,
  256. phys_addr_t align, phys_addr_t start,
  257. phys_addr_t end, int nid,
  258. enum memblock_flags flags)
  259. {
  260. /* pump up @end */
  261. if (end == MEMBLOCK_ALLOC_ACCESSIBLE ||
  262. end == MEMBLOCK_ALLOC_NOLEAKTRACE)
  263. end = memblock.current_limit;
  264. /* avoid allocating the first page */
  265. start = max_t(phys_addr_t, start, PAGE_SIZE);
  266. end = max(start, end);
  267. if (memblock_bottom_up())
  268. return __memblock_find_range_bottom_up(start, end, size, align,
  269. nid, flags);
  270. else
  271. return __memblock_find_range_top_down(start, end, size, align,
  272. nid, flags);
  273. }
  274. /**
  275. * memblock_find_in_range - find free area in given range
  276. * @start: start of candidate range
  277. * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or
  278. * %MEMBLOCK_ALLOC_ACCESSIBLE
  279. * @size: size of free area to find
  280. * @align: alignment of free area to find
  281. *
  282. * Find @size free area aligned to @align in the specified range.
  283. *
  284. * Return:
  285. * Found address on success, 0 on failure.
  286. */
  287. static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start,
  288. phys_addr_t end, phys_addr_t size,
  289. phys_addr_t align)
  290. {
  291. phys_addr_t ret;
  292. enum memblock_flags flags = choose_memblock_flags();
  293. again:
  294. ret = memblock_find_in_range_node(size, align, start, end,
  295. NUMA_NO_NODE, flags);
  296. if (!ret && (flags & MEMBLOCK_MIRROR)) {
  297. pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
  298. &size);
  299. flags &= ~MEMBLOCK_MIRROR;
  300. goto again;
  301. }
  302. return ret;
  303. }
  304. static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r)
  305. {
  306. type->total_size -= type->regions[r].size;
  307. memmove(&type->regions[r], &type->regions[r + 1],
  308. (type->cnt - (r + 1)) * sizeof(type->regions[r]));
  309. type->cnt--;
  310. /* Special case for empty arrays */
  311. if (type->cnt == 0) {
  312. WARN_ON(type->total_size != 0);
  313. type->regions[0].base = 0;
  314. type->regions[0].size = 0;
  315. type->regions[0].flags = 0;
  316. memblock_set_region_node(&type->regions[0], MAX_NUMNODES);
  317. }
  318. }
  319. #ifndef CONFIG_ARCH_KEEP_MEMBLOCK
  320. /**
  321. * memblock_discard - discard memory and reserved arrays if they were allocated
  322. */
  323. void __init memblock_discard(void)
  324. {
  325. phys_addr_t addr, size;
  326. if (memblock.reserved.regions != memblock_reserved_init_regions) {
  327. addr = __pa(memblock.reserved.regions);
  328. size = PAGE_ALIGN(sizeof(struct memblock_region) *
  329. memblock.reserved.max);
  330. if (memblock_reserved_in_slab)
  331. kfree(memblock.reserved.regions);
  332. else
  333. memblock_free_late(addr, size);
  334. }
  335. if (memblock.memory.regions != memblock_memory_init_regions) {
  336. addr = __pa(memblock.memory.regions);
  337. size = PAGE_ALIGN(sizeof(struct memblock_region) *
  338. memblock.memory.max);
  339. if (memblock_memory_in_slab)
  340. kfree(memblock.memory.regions);
  341. else
  342. memblock_free_late(addr, size);
  343. }
  344. memblock_memory = NULL;
  345. }
  346. #endif
  347. /**
  348. * memblock_double_array - double the size of the memblock regions array
  349. * @type: memblock type of the regions array being doubled
  350. * @new_area_start: starting address of memory range to avoid overlap with
  351. * @new_area_size: size of memory range to avoid overlap with
  352. *
  353. * Double the size of the @type regions array. If memblock is being used to
  354. * allocate memory for a new reserved regions array and there is a previously
  355. * allocated memory range [@new_area_start, @new_area_start + @new_area_size]
  356. * waiting to be reserved, ensure the memory used by the new array does
  357. * not overlap.
  358. *
  359. * Return:
  360. * 0 on success, -1 on failure.
  361. */
  362. static int __init_memblock memblock_double_array(struct memblock_type *type,
  363. phys_addr_t new_area_start,
  364. phys_addr_t new_area_size)
  365. {
  366. struct memblock_region *new_array, *old_array;
  367. phys_addr_t old_alloc_size, new_alloc_size;
  368. phys_addr_t old_size, new_size, addr, new_end;
  369. int use_slab = slab_is_available();
  370. int *in_slab;
  371. /* We don't allow resizing until we know about the reserved regions
  372. * of memory that aren't suitable for allocation
  373. */
  374. if (!memblock_can_resize)
  375. panic("memblock: cannot resize %s array\n", type->name);
  376. /* Calculate new doubled size */
  377. old_size = type->max * sizeof(struct memblock_region);
  378. new_size = old_size << 1;
  379. /*
  380. * We need to allocated new one align to PAGE_SIZE,
  381. * so we can free them completely later.
  382. */
  383. old_alloc_size = PAGE_ALIGN(old_size);
  384. new_alloc_size = PAGE_ALIGN(new_size);
  385. /* Retrieve the slab flag */
  386. if (type == &memblock.memory)
  387. in_slab = &memblock_memory_in_slab;
  388. else
  389. in_slab = &memblock_reserved_in_slab;
  390. /* Try to find some space for it */
  391. if (use_slab) {
  392. new_array = kmalloc(new_size, GFP_KERNEL);
  393. addr = new_array ? __pa(new_array) : 0;
  394. } else {
  395. /* only exclude range when trying to double reserved.regions */
  396. if (type != &memblock.reserved)
  397. new_area_start = new_area_size = 0;
  398. addr = memblock_find_in_range(new_area_start + new_area_size,
  399. memblock.current_limit,
  400. new_alloc_size, PAGE_SIZE);
  401. if (!addr && new_area_size)
  402. addr = memblock_find_in_range(0,
  403. min(new_area_start, memblock.current_limit),
  404. new_alloc_size, PAGE_SIZE);
  405. new_array = addr ? __va(addr) : NULL;
  406. }
  407. if (!addr) {
  408. pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n",
  409. type->name, type->max, type->max * 2);
  410. return -1;
  411. }
  412. new_end = addr + new_size - 1;
  413. memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]",
  414. type->name, type->max * 2, &addr, &new_end);
  415. /*
  416. * Found space, we now need to move the array over before we add the
  417. * reserved region since it may be our reserved array itself that is
  418. * full.
  419. */
  420. memcpy(new_array, type->regions, old_size);
  421. memset(new_array + type->max, 0, old_size);
  422. old_array = type->regions;
  423. type->regions = new_array;
  424. type->max <<= 1;
  425. /* Free old array. We needn't free it if the array is the static one */
  426. if (*in_slab)
  427. kfree(old_array);
  428. else if (old_array != memblock_memory_init_regions &&
  429. old_array != memblock_reserved_init_regions)
  430. memblock_free(old_array, old_alloc_size);
  431. /*
  432. * Reserve the new array if that comes from the memblock. Otherwise, we
  433. * needn't do it
  434. */
  435. if (!use_slab)
  436. BUG_ON(memblock_reserve(addr, new_alloc_size));
  437. /* Update slab flag */
  438. *in_slab = use_slab;
  439. return 0;
  440. }
  441. /**
  442. * memblock_merge_regions - merge neighboring compatible regions
  443. * @type: memblock type to scan
  444. * @start_rgn: start scanning from (@start_rgn - 1)
  445. * @end_rgn: end scanning at (@end_rgn - 1)
  446. * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn)
  447. */
  448. static void __init_memblock memblock_merge_regions(struct memblock_type *type,
  449. unsigned long start_rgn,
  450. unsigned long end_rgn)
  451. {
  452. int i = 0;
  453. if (start_rgn)
  454. i = start_rgn - 1;
  455. end_rgn = min(end_rgn, type->cnt - 1);
  456. while (i < end_rgn) {
  457. struct memblock_region *this = &type->regions[i];
  458. struct memblock_region *next = &type->regions[i + 1];
  459. if (this->base + this->size != next->base ||
  460. memblock_get_region_node(this) !=
  461. memblock_get_region_node(next) ||
  462. this->flags != next->flags) {
  463. BUG_ON(this->base + this->size > next->base);
  464. i++;
  465. continue;
  466. }
  467. this->size += next->size;
  468. /* move forward from next + 1, index of which is i + 2 */
  469. memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next));
  470. type->cnt--;
  471. end_rgn--;
  472. }
  473. }
  474. /**
  475. * memblock_insert_region - insert new memblock region
  476. * @type: memblock type to insert into
  477. * @idx: index for the insertion point
  478. * @base: base address of the new region
  479. * @size: size of the new region
  480. * @nid: node id of the new region
  481. * @flags: flags of the new region
  482. *
  483. * Insert new memblock region [@base, @base + @size) into @type at @idx.
  484. * @type must already have extra room to accommodate the new region.
  485. */
  486. static void __init_memblock memblock_insert_region(struct memblock_type *type,
  487. int idx, phys_addr_t base,
  488. phys_addr_t size,
  489. int nid,
  490. enum memblock_flags flags)
  491. {
  492. struct memblock_region *rgn = &type->regions[idx];
  493. BUG_ON(type->cnt >= type->max);
  494. memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn));
  495. rgn->base = base;
  496. rgn->size = size;
  497. rgn->flags = flags;
  498. memblock_set_region_node(rgn, nid);
  499. type->cnt++;
  500. type->total_size += size;
  501. }
  502. /**
  503. * memblock_add_range - add new memblock region
  504. * @type: memblock type to add new region into
  505. * @base: base address of the new region
  506. * @size: size of the new region
  507. * @nid: nid of the new region
  508. * @flags: flags of the new region
  509. *
  510. * Add new memblock region [@base, @base + @size) into @type. The new region
  511. * is allowed to overlap with existing ones - overlaps don't affect already
  512. * existing regions. @type is guaranteed to be minimal (all neighbouring
  513. * compatible regions are merged) after the addition.
  514. *
  515. * Return:
  516. * 0 on success, -errno on failure.
  517. */
  518. static int __init_memblock memblock_add_range(struct memblock_type *type,
  519. phys_addr_t base, phys_addr_t size,
  520. int nid, enum memblock_flags flags)
  521. {
  522. bool insert = false;
  523. phys_addr_t obase = base;
  524. phys_addr_t end = base + memblock_cap_size(base, &size);
  525. int idx, nr_new, start_rgn = -1, end_rgn;
  526. struct memblock_region *rgn;
  527. if (!size)
  528. return 0;
  529. /* special case for empty array */
  530. if (type->regions[0].size == 0) {
  531. WARN_ON(type->cnt != 0 || type->total_size);
  532. type->regions[0].base = base;
  533. type->regions[0].size = size;
  534. type->regions[0].flags = flags;
  535. memblock_set_region_node(&type->regions[0], nid);
  536. type->total_size = size;
  537. type->cnt = 1;
  538. return 0;
  539. }
  540. /*
  541. * The worst case is when new range overlaps all existing regions,
  542. * then we'll need type->cnt + 1 empty regions in @type. So if
  543. * type->cnt * 2 + 1 is less than or equal to type->max, we know
  544. * that there is enough empty regions in @type, and we can insert
  545. * regions directly.
  546. */
  547. if (type->cnt * 2 + 1 <= type->max)
  548. insert = true;
  549. repeat:
  550. /*
  551. * The following is executed twice. Once with %false @insert and
  552. * then with %true. The first counts the number of regions needed
  553. * to accommodate the new area. The second actually inserts them.
  554. */
  555. base = obase;
  556. nr_new = 0;
  557. for_each_memblock_type(idx, type, rgn) {
  558. phys_addr_t rbase = rgn->base;
  559. phys_addr_t rend = rbase + rgn->size;
  560. if (rbase >= end)
  561. break;
  562. if (rend <= base)
  563. continue;
  564. /*
  565. * @rgn overlaps. If it separates the lower part of new
  566. * area, insert that portion.
  567. */
  568. if (rbase > base) {
  569. #ifdef CONFIG_NUMA
  570. WARN_ON(nid != memblock_get_region_node(rgn));
  571. #endif
  572. WARN_ON(flags != rgn->flags);
  573. nr_new++;
  574. if (insert) {
  575. if (start_rgn == -1)
  576. start_rgn = idx;
  577. end_rgn = idx + 1;
  578. memblock_insert_region(type, idx++, base,
  579. rbase - base, nid,
  580. flags);
  581. }
  582. }
  583. /* area below @rend is dealt with, forget about it */
  584. base = min(rend, end);
  585. }
  586. /* insert the remaining portion */
  587. if (base < end) {
  588. nr_new++;
  589. if (insert) {
  590. if (start_rgn == -1)
  591. start_rgn = idx;
  592. end_rgn = idx + 1;
  593. memblock_insert_region(type, idx, base, end - base,
  594. nid, flags);
  595. }
  596. }
  597. if (!nr_new)
  598. return 0;
  599. /*
  600. * If this was the first round, resize array and repeat for actual
  601. * insertions; otherwise, merge and return.
  602. */
  603. if (!insert) {
  604. while (type->cnt + nr_new > type->max)
  605. if (memblock_double_array(type, obase, size) < 0)
  606. return -ENOMEM;
  607. insert = true;
  608. goto repeat;
  609. } else {
  610. memblock_merge_regions(type, start_rgn, end_rgn);
  611. return 0;
  612. }
  613. }
  614. /**
  615. * memblock_add_node - add new memblock region within a NUMA node
  616. * @base: base address of the new region
  617. * @size: size of the new region
  618. * @nid: nid of the new region
  619. * @flags: flags of the new region
  620. *
  621. * Add new memblock region [@base, @base + @size) to the "memory"
  622. * type. See memblock_add_range() description for mode details
  623. *
  624. * Return:
  625. * 0 on success, -errno on failure.
  626. */
  627. int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size,
  628. int nid, enum memblock_flags flags)
  629. {
  630. phys_addr_t end = base + size - 1;
  631. memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__,
  632. &base, &end, nid, flags, (void *)_RET_IP_);
  633. return memblock_add_range(&memblock.memory, base, size, nid, flags);
  634. }
  635. /**
  636. * memblock_add - add new memblock region
  637. * @base: base address of the new region
  638. * @size: size of the new region
  639. *
  640. * Add new memblock region [@base, @base + @size) to the "memory"
  641. * type. See memblock_add_range() description for mode details
  642. *
  643. * Return:
  644. * 0 on success, -errno on failure.
  645. */
  646. int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size)
  647. {
  648. phys_addr_t end = base + size - 1;
  649. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  650. &base, &end, (void *)_RET_IP_);
  651. return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0);
  652. }
  653. /**
  654. * memblock_validate_numa_coverage - check if amount of memory with
  655. * no node ID assigned is less than a threshold
  656. * @threshold_bytes: maximal memory size that can have unassigned node
  657. * ID (in bytes).
  658. *
  659. * A buggy firmware may report memory that does not belong to any node.
  660. * Check if amount of such memory is below @threshold_bytes.
  661. *
  662. * Return: true on success, false on failure.
  663. */
  664. bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes)
  665. {
  666. unsigned long nr_pages = 0;
  667. unsigned long start_pfn, end_pfn, mem_size_mb;
  668. int nid, i;
  669. /* calculate lose page */
  670. for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) {
  671. if (!numa_valid_node(nid))
  672. nr_pages += end_pfn - start_pfn;
  673. }
  674. if ((nr_pages << PAGE_SHIFT) > threshold_bytes) {
  675. mem_size_mb = memblock_phys_mem_size() >> 20;
  676. pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n",
  677. (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb);
  678. return false;
  679. }
  680. return true;
  681. }
  682. /**
  683. * memblock_isolate_range - isolate given range into disjoint memblocks
  684. * @type: memblock type to isolate range for
  685. * @base: base of range to isolate
  686. * @size: size of range to isolate
  687. * @start_rgn: out parameter for the start of isolated region
  688. * @end_rgn: out parameter for the end of isolated region
  689. *
  690. * Walk @type and ensure that regions don't cross the boundaries defined by
  691. * [@base, @base + @size). Crossing regions are split at the boundaries,
  692. * which may create at most two more regions. The index of the first
  693. * region inside the range is returned in *@start_rgn and the index of the
  694. * first region after the range is returned in *@end_rgn.
  695. *
  696. * Return:
  697. * 0 on success, -errno on failure.
  698. */
  699. static int __init_memblock memblock_isolate_range(struct memblock_type *type,
  700. phys_addr_t base, phys_addr_t size,
  701. int *start_rgn, int *end_rgn)
  702. {
  703. phys_addr_t end = base + memblock_cap_size(base, &size);
  704. int idx;
  705. struct memblock_region *rgn;
  706. *start_rgn = *end_rgn = 0;
  707. if (!size)
  708. return 0;
  709. /* we'll create at most two more regions */
  710. while (type->cnt + 2 > type->max)
  711. if (memblock_double_array(type, base, size) < 0)
  712. return -ENOMEM;
  713. for_each_memblock_type(idx, type, rgn) {
  714. phys_addr_t rbase = rgn->base;
  715. phys_addr_t rend = rbase + rgn->size;
  716. if (rbase >= end)
  717. break;
  718. if (rend <= base)
  719. continue;
  720. if (rbase < base) {
  721. /*
  722. * @rgn intersects from below. Split and continue
  723. * to process the next region - the new top half.
  724. */
  725. rgn->base = base;
  726. rgn->size -= base - rbase;
  727. type->total_size -= base - rbase;
  728. memblock_insert_region(type, idx, rbase, base - rbase,
  729. memblock_get_region_node(rgn),
  730. rgn->flags);
  731. } else if (rend > end) {
  732. /*
  733. * @rgn intersects from above. Split and redo the
  734. * current region - the new bottom half.
  735. */
  736. rgn->base = end;
  737. rgn->size -= end - rbase;
  738. type->total_size -= end - rbase;
  739. memblock_insert_region(type, idx--, rbase, end - rbase,
  740. memblock_get_region_node(rgn),
  741. rgn->flags);
  742. } else {
  743. /* @rgn is fully contained, record it */
  744. if (!*end_rgn)
  745. *start_rgn = idx;
  746. *end_rgn = idx + 1;
  747. }
  748. }
  749. return 0;
  750. }
  751. static int __init_memblock memblock_remove_range(struct memblock_type *type,
  752. phys_addr_t base, phys_addr_t size)
  753. {
  754. int start_rgn, end_rgn;
  755. int i, ret;
  756. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  757. if (ret)
  758. return ret;
  759. for (i = end_rgn - 1; i >= start_rgn; i--)
  760. memblock_remove_region(type, i);
  761. return 0;
  762. }
  763. int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size)
  764. {
  765. phys_addr_t end = base + size - 1;
  766. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  767. &base, &end, (void *)_RET_IP_);
  768. return memblock_remove_range(&memblock.memory, base, size);
  769. }
  770. /**
  771. * memblock_free - free boot memory allocation
  772. * @ptr: starting address of the boot memory allocation
  773. * @size: size of the boot memory block in bytes
  774. *
  775. * Free boot memory block previously allocated by memblock_alloc_xx() API.
  776. * The freeing memory will not be released to the buddy allocator.
  777. */
  778. void __init_memblock memblock_free(void *ptr, size_t size)
  779. {
  780. if (ptr)
  781. memblock_phys_free(__pa(ptr), size);
  782. }
  783. /**
  784. * memblock_phys_free - free boot memory block
  785. * @base: phys starting address of the boot memory block
  786. * @size: size of the boot memory block in bytes
  787. *
  788. * Free boot memory block previously allocated by memblock_phys_alloc_xx() API.
  789. * The freeing memory will not be released to the buddy allocator.
  790. */
  791. int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size)
  792. {
  793. phys_addr_t end = base + size - 1;
  794. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  795. &base, &end, (void *)_RET_IP_);
  796. kmemleak_free_part_phys(base, size);
  797. return memblock_remove_range(&memblock.reserved, base, size);
  798. }
  799. int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size)
  800. {
  801. phys_addr_t end = base + size - 1;
  802. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  803. &base, &end, (void *)_RET_IP_);
  804. return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0);
  805. }
  806. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  807. int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
  808. {
  809. phys_addr_t end = base + size - 1;
  810. memblock_dbg("%s: [%pa-%pa] %pS\n", __func__,
  811. &base, &end, (void *)_RET_IP_);
  812. return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0);
  813. }
  814. #endif
  815. /**
  816. * memblock_setclr_flag - set or clear flag for a memory region
  817. * @type: memblock type to set/clear flag for
  818. * @base: base address of the region
  819. * @size: size of the region
  820. * @set: set or clear the flag
  821. * @flag: the flag to update
  822. *
  823. * This function isolates region [@base, @base + @size), and sets/clears flag
  824. *
  825. * Return: 0 on success, -errno on failure.
  826. */
  827. static int __init_memblock memblock_setclr_flag(struct memblock_type *type,
  828. phys_addr_t base, phys_addr_t size, int set, int flag)
  829. {
  830. int i, ret, start_rgn, end_rgn;
  831. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  832. if (ret)
  833. return ret;
  834. for (i = start_rgn; i < end_rgn; i++) {
  835. struct memblock_region *r = &type->regions[i];
  836. if (set)
  837. r->flags |= flag;
  838. else
  839. r->flags &= ~flag;
  840. }
  841. memblock_merge_regions(type, start_rgn, end_rgn);
  842. return 0;
  843. }
  844. /**
  845. * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG.
  846. * @base: the base phys addr of the region
  847. * @size: the size of the region
  848. *
  849. * Return: 0 on success, -errno on failure.
  850. */
  851. int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size)
  852. {
  853. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG);
  854. }
  855. /**
  856. * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region.
  857. * @base: the base phys addr of the region
  858. * @size: the size of the region
  859. *
  860. * Return: 0 on success, -errno on failure.
  861. */
  862. int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size)
  863. {
  864. return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG);
  865. }
  866. /**
  867. * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR.
  868. * @base: the base phys addr of the region
  869. * @size: the size of the region
  870. *
  871. * Return: 0 on success, -errno on failure.
  872. */
  873. int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size)
  874. {
  875. if (!mirrored_kernelcore)
  876. return 0;
  877. system_has_some_mirror = true;
  878. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR);
  879. }
  880. /**
  881. * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP.
  882. * @base: the base phys addr of the region
  883. * @size: the size of the region
  884. *
  885. * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the
  886. * direct mapping of the physical memory. These regions will still be
  887. * covered by the memory map. The struct page representing NOMAP memory
  888. * frames in the memory map will be PageReserved()
  889. *
  890. * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from
  891. * memblock, the caller must inform kmemleak to ignore that memory
  892. *
  893. * Return: 0 on success, -errno on failure.
  894. */
  895. int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size)
  896. {
  897. return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP);
  898. }
  899. /**
  900. * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region.
  901. * @base: the base phys addr of the region
  902. * @size: the size of the region
  903. *
  904. * Return: 0 on success, -errno on failure.
  905. */
  906. int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size)
  907. {
  908. return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP);
  909. }
  910. /**
  911. * memblock_reserved_mark_noinit - Mark a reserved memory region with flag
  912. * MEMBLOCK_RSRV_NOINIT which results in the struct pages not being initialized
  913. * for this region.
  914. * @base: the base phys addr of the region
  915. * @size: the size of the region
  916. *
  917. * struct pages will not be initialized for reserved memory regions marked with
  918. * %MEMBLOCK_RSRV_NOINIT.
  919. *
  920. * Return: 0 on success, -errno on failure.
  921. */
  922. int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size)
  923. {
  924. return memblock_setclr_flag(&memblock.reserved, base, size, 1,
  925. MEMBLOCK_RSRV_NOINIT);
  926. }
  927. static bool should_skip_region(struct memblock_type *type,
  928. struct memblock_region *m,
  929. int nid, int flags)
  930. {
  931. int m_nid = memblock_get_region_node(m);
  932. /* we never skip regions when iterating memblock.reserved or physmem */
  933. if (type != memblock_memory)
  934. return false;
  935. /* only memory regions are associated with nodes, check it */
  936. if (numa_valid_node(nid) && nid != m_nid)
  937. return true;
  938. /* skip hotpluggable memory regions if needed */
  939. if (movable_node_is_enabled() && memblock_is_hotpluggable(m) &&
  940. !(flags & MEMBLOCK_HOTPLUG))
  941. return true;
  942. /* if we want mirror memory skip non-mirror memory regions */
  943. if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m))
  944. return true;
  945. /* skip nomap memory unless we were asked for it explicitly */
  946. if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m))
  947. return true;
  948. /* skip driver-managed memory unless we were asked for it explicitly */
  949. if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m))
  950. return true;
  951. return false;
  952. }
  953. /**
  954. * __next_mem_range - next function for for_each_free_mem_range() etc.
  955. * @idx: pointer to u64 loop variable
  956. * @nid: node selector, %NUMA_NO_NODE for all nodes
  957. * @flags: pick from blocks based on memory attributes
  958. * @type_a: pointer to memblock_type from where the range is taken
  959. * @type_b: pointer to memblock_type which excludes memory from being taken
  960. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  961. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  962. * @out_nid: ptr to int for nid of the range, can be %NULL
  963. *
  964. * Find the first area from *@idx which matches @nid, fill the out
  965. * parameters, and update *@idx for the next iteration. The lower 32bit of
  966. * *@idx contains index into type_a and the upper 32bit indexes the
  967. * areas before each region in type_b. For example, if type_b regions
  968. * look like the following,
  969. *
  970. * 0:[0-16), 1:[32-48), 2:[128-130)
  971. *
  972. * The upper 32bit indexes the following regions.
  973. *
  974. * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX)
  975. *
  976. * As both region arrays are sorted, the function advances the two indices
  977. * in lockstep and returns each intersection.
  978. */
  979. void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags,
  980. struct memblock_type *type_a,
  981. struct memblock_type *type_b, phys_addr_t *out_start,
  982. phys_addr_t *out_end, int *out_nid)
  983. {
  984. int idx_a = *idx & 0xffffffff;
  985. int idx_b = *idx >> 32;
  986. for (; idx_a < type_a->cnt; idx_a++) {
  987. struct memblock_region *m = &type_a->regions[idx_a];
  988. phys_addr_t m_start = m->base;
  989. phys_addr_t m_end = m->base + m->size;
  990. int m_nid = memblock_get_region_node(m);
  991. if (should_skip_region(type_a, m, nid, flags))
  992. continue;
  993. if (!type_b) {
  994. if (out_start)
  995. *out_start = m_start;
  996. if (out_end)
  997. *out_end = m_end;
  998. if (out_nid)
  999. *out_nid = m_nid;
  1000. idx_a++;
  1001. *idx = (u32)idx_a | (u64)idx_b << 32;
  1002. return;
  1003. }
  1004. /* scan areas before each reservation */
  1005. for (; idx_b < type_b->cnt + 1; idx_b++) {
  1006. struct memblock_region *r;
  1007. phys_addr_t r_start;
  1008. phys_addr_t r_end;
  1009. r = &type_b->regions[idx_b];
  1010. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  1011. r_end = idx_b < type_b->cnt ?
  1012. r->base : PHYS_ADDR_MAX;
  1013. /*
  1014. * if idx_b advanced past idx_a,
  1015. * break out to advance idx_a
  1016. */
  1017. if (r_start >= m_end)
  1018. break;
  1019. /* if the two regions intersect, we're done */
  1020. if (m_start < r_end) {
  1021. if (out_start)
  1022. *out_start =
  1023. max(m_start, r_start);
  1024. if (out_end)
  1025. *out_end = min(m_end, r_end);
  1026. if (out_nid)
  1027. *out_nid = m_nid;
  1028. /*
  1029. * The region which ends first is
  1030. * advanced for the next iteration.
  1031. */
  1032. if (m_end <= r_end)
  1033. idx_a++;
  1034. else
  1035. idx_b++;
  1036. *idx = (u32)idx_a | (u64)idx_b << 32;
  1037. return;
  1038. }
  1039. }
  1040. }
  1041. /* signal end of iteration */
  1042. *idx = ULLONG_MAX;
  1043. }
  1044. /**
  1045. * __next_mem_range_rev - generic next function for for_each_*_range_rev()
  1046. *
  1047. * @idx: pointer to u64 loop variable
  1048. * @nid: node selector, %NUMA_NO_NODE for all nodes
  1049. * @flags: pick from blocks based on memory attributes
  1050. * @type_a: pointer to memblock_type from where the range is taken
  1051. * @type_b: pointer to memblock_type which excludes memory from being taken
  1052. * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL
  1053. * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL
  1054. * @out_nid: ptr to int for nid of the range, can be %NULL
  1055. *
  1056. * Finds the next range from type_a which is not marked as unsuitable
  1057. * in type_b.
  1058. *
  1059. * Reverse of __next_mem_range().
  1060. */
  1061. void __init_memblock __next_mem_range_rev(u64 *idx, int nid,
  1062. enum memblock_flags flags,
  1063. struct memblock_type *type_a,
  1064. struct memblock_type *type_b,
  1065. phys_addr_t *out_start,
  1066. phys_addr_t *out_end, int *out_nid)
  1067. {
  1068. int idx_a = *idx & 0xffffffff;
  1069. int idx_b = *idx >> 32;
  1070. if (*idx == (u64)ULLONG_MAX) {
  1071. idx_a = type_a->cnt - 1;
  1072. if (type_b != NULL)
  1073. idx_b = type_b->cnt;
  1074. else
  1075. idx_b = 0;
  1076. }
  1077. for (; idx_a >= 0; idx_a--) {
  1078. struct memblock_region *m = &type_a->regions[idx_a];
  1079. phys_addr_t m_start = m->base;
  1080. phys_addr_t m_end = m->base + m->size;
  1081. int m_nid = memblock_get_region_node(m);
  1082. if (should_skip_region(type_a, m, nid, flags))
  1083. continue;
  1084. if (!type_b) {
  1085. if (out_start)
  1086. *out_start = m_start;
  1087. if (out_end)
  1088. *out_end = m_end;
  1089. if (out_nid)
  1090. *out_nid = m_nid;
  1091. idx_a--;
  1092. *idx = (u32)idx_a | (u64)idx_b << 32;
  1093. return;
  1094. }
  1095. /* scan areas before each reservation */
  1096. for (; idx_b >= 0; idx_b--) {
  1097. struct memblock_region *r;
  1098. phys_addr_t r_start;
  1099. phys_addr_t r_end;
  1100. r = &type_b->regions[idx_b];
  1101. r_start = idx_b ? r[-1].base + r[-1].size : 0;
  1102. r_end = idx_b < type_b->cnt ?
  1103. r->base : PHYS_ADDR_MAX;
  1104. /*
  1105. * if idx_b advanced past idx_a,
  1106. * break out to advance idx_a
  1107. */
  1108. if (r_end <= m_start)
  1109. break;
  1110. /* if the two regions intersect, we're done */
  1111. if (m_end > r_start) {
  1112. if (out_start)
  1113. *out_start = max(m_start, r_start);
  1114. if (out_end)
  1115. *out_end = min(m_end, r_end);
  1116. if (out_nid)
  1117. *out_nid = m_nid;
  1118. if (m_start >= r_start)
  1119. idx_a--;
  1120. else
  1121. idx_b--;
  1122. *idx = (u32)idx_a | (u64)idx_b << 32;
  1123. return;
  1124. }
  1125. }
  1126. }
  1127. /* signal end of iteration */
  1128. *idx = ULLONG_MAX;
  1129. }
  1130. /*
  1131. * Common iterator interface used to define for_each_mem_pfn_range().
  1132. */
  1133. void __init_memblock __next_mem_pfn_range(int *idx, int nid,
  1134. unsigned long *out_start_pfn,
  1135. unsigned long *out_end_pfn, int *out_nid)
  1136. {
  1137. struct memblock_type *type = &memblock.memory;
  1138. struct memblock_region *r;
  1139. int r_nid;
  1140. while (++*idx < type->cnt) {
  1141. r = &type->regions[*idx];
  1142. r_nid = memblock_get_region_node(r);
  1143. if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size))
  1144. continue;
  1145. if (!numa_valid_node(nid) || nid == r_nid)
  1146. break;
  1147. }
  1148. if (*idx >= type->cnt) {
  1149. *idx = -1;
  1150. return;
  1151. }
  1152. if (out_start_pfn)
  1153. *out_start_pfn = PFN_UP(r->base);
  1154. if (out_end_pfn)
  1155. *out_end_pfn = PFN_DOWN(r->base + r->size);
  1156. if (out_nid)
  1157. *out_nid = r_nid;
  1158. }
  1159. /**
  1160. * memblock_set_node - set node ID on memblock regions
  1161. * @base: base of area to set node ID for
  1162. * @size: size of area to set node ID for
  1163. * @type: memblock type to set node ID for
  1164. * @nid: node ID to set
  1165. *
  1166. * Set the nid of memblock @type regions in [@base, @base + @size) to @nid.
  1167. * Regions which cross the area boundaries are split as necessary.
  1168. *
  1169. * Return:
  1170. * 0 on success, -errno on failure.
  1171. */
  1172. int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size,
  1173. struct memblock_type *type, int nid)
  1174. {
  1175. #ifdef CONFIG_NUMA
  1176. int start_rgn, end_rgn;
  1177. int i, ret;
  1178. ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn);
  1179. if (ret)
  1180. return ret;
  1181. for (i = start_rgn; i < end_rgn; i++)
  1182. memblock_set_region_node(&type->regions[i], nid);
  1183. memblock_merge_regions(type, start_rgn, end_rgn);
  1184. #endif
  1185. return 0;
  1186. }
  1187. #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
  1188. /**
  1189. * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone()
  1190. *
  1191. * @idx: pointer to u64 loop variable
  1192. * @zone: zone in which all of the memory blocks reside
  1193. * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL
  1194. * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL
  1195. *
  1196. * This function is meant to be a zone/pfn specific wrapper for the
  1197. * for_each_mem_range type iterators. Specifically they are used in the
  1198. * deferred memory init routines and as such we were duplicating much of
  1199. * this logic throughout the code. So instead of having it in multiple
  1200. * locations it seemed like it would make more sense to centralize this to
  1201. * one new iterator that does everything they need.
  1202. */
  1203. void __init_memblock
  1204. __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone,
  1205. unsigned long *out_spfn, unsigned long *out_epfn)
  1206. {
  1207. int zone_nid = zone_to_nid(zone);
  1208. phys_addr_t spa, epa;
  1209. __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
  1210. &memblock.memory, &memblock.reserved,
  1211. &spa, &epa, NULL);
  1212. while (*idx != U64_MAX) {
  1213. unsigned long epfn = PFN_DOWN(epa);
  1214. unsigned long spfn = PFN_UP(spa);
  1215. /*
  1216. * Verify the end is at least past the start of the zone and
  1217. * that we have at least one PFN to initialize.
  1218. */
  1219. if (zone->zone_start_pfn < epfn && spfn < epfn) {
  1220. /* if we went too far just stop searching */
  1221. if (zone_end_pfn(zone) <= spfn) {
  1222. *idx = U64_MAX;
  1223. break;
  1224. }
  1225. if (out_spfn)
  1226. *out_spfn = max(zone->zone_start_pfn, spfn);
  1227. if (out_epfn)
  1228. *out_epfn = min(zone_end_pfn(zone), epfn);
  1229. return;
  1230. }
  1231. __next_mem_range(idx, zone_nid, MEMBLOCK_NONE,
  1232. &memblock.memory, &memblock.reserved,
  1233. &spa, &epa, NULL);
  1234. }
  1235. /* signal end of iteration */
  1236. if (out_spfn)
  1237. *out_spfn = ULONG_MAX;
  1238. if (out_epfn)
  1239. *out_epfn = 0;
  1240. }
  1241. #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */
  1242. /**
  1243. * memblock_alloc_range_nid - allocate boot memory block
  1244. * @size: size of memory block to be allocated in bytes
  1245. * @align: alignment of the region and block's size
  1246. * @start: the lower bound of the memory region to allocate (phys address)
  1247. * @end: the upper bound of the memory region to allocate (phys address)
  1248. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1249. * @exact_nid: control the allocation fall back to other nodes
  1250. *
  1251. * The allocation is performed from memory region limited by
  1252. * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE.
  1253. *
  1254. * If the specified node can not hold the requested memory and @exact_nid
  1255. * is false, the allocation falls back to any node in the system.
  1256. *
  1257. * For systems with memory mirroring, the allocation is attempted first
  1258. * from the regions with mirroring enabled and then retried from any
  1259. * memory region.
  1260. *
  1261. * In addition, function using kmemleak_alloc_phys for allocated boot
  1262. * memory block, it is never reported as leaks.
  1263. *
  1264. * Return:
  1265. * Physical address of allocated memory block on success, %0 on failure.
  1266. */
  1267. phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size,
  1268. phys_addr_t align, phys_addr_t start,
  1269. phys_addr_t end, int nid,
  1270. bool exact_nid)
  1271. {
  1272. enum memblock_flags flags = choose_memblock_flags();
  1273. phys_addr_t found;
  1274. /*
  1275. * Detect any accidental use of these APIs after slab is ready, as at
  1276. * this moment memblock may be deinitialized already and its
  1277. * internal data may be destroyed (after execution of memblock_free_all)
  1278. */
  1279. if (WARN_ON_ONCE(slab_is_available())) {
  1280. void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid);
  1281. return vaddr ? virt_to_phys(vaddr) : 0;
  1282. }
  1283. if (!align) {
  1284. /* Can't use WARNs this early in boot on powerpc */
  1285. dump_stack();
  1286. align = SMP_CACHE_BYTES;
  1287. }
  1288. again:
  1289. found = memblock_find_in_range_node(size, align, start, end, nid,
  1290. flags);
  1291. if (found && !memblock_reserve(found, size))
  1292. goto done;
  1293. if (numa_valid_node(nid) && !exact_nid) {
  1294. found = memblock_find_in_range_node(size, align, start,
  1295. end, NUMA_NO_NODE,
  1296. flags);
  1297. if (found && !memblock_reserve(found, size))
  1298. goto done;
  1299. }
  1300. if (flags & MEMBLOCK_MIRROR) {
  1301. flags &= ~MEMBLOCK_MIRROR;
  1302. pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n",
  1303. &size);
  1304. goto again;
  1305. }
  1306. return 0;
  1307. done:
  1308. /*
  1309. * Skip kmemleak for those places like kasan_init() and
  1310. * early_pgtable_alloc() due to high volume.
  1311. */
  1312. if (end != MEMBLOCK_ALLOC_NOLEAKTRACE)
  1313. /*
  1314. * Memblock allocated blocks are never reported as
  1315. * leaks. This is because many of these blocks are
  1316. * only referred via the physical address which is
  1317. * not looked up by kmemleak.
  1318. */
  1319. kmemleak_alloc_phys(found, size, 0);
  1320. /*
  1321. * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP,
  1322. * require memory to be accepted before it can be used by the
  1323. * guest.
  1324. *
  1325. * Accept the memory of the allocated buffer.
  1326. */
  1327. accept_memory(found, size);
  1328. return found;
  1329. }
  1330. /**
  1331. * memblock_phys_alloc_range - allocate a memory block inside specified range
  1332. * @size: size of memory block to be allocated in bytes
  1333. * @align: alignment of the region and block's size
  1334. * @start: the lower bound of the memory region to allocate (physical address)
  1335. * @end: the upper bound of the memory region to allocate (physical address)
  1336. *
  1337. * Allocate @size bytes in the between @start and @end.
  1338. *
  1339. * Return: physical address of the allocated memory block on success,
  1340. * %0 on failure.
  1341. */
  1342. phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size,
  1343. phys_addr_t align,
  1344. phys_addr_t start,
  1345. phys_addr_t end)
  1346. {
  1347. memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n",
  1348. __func__, (u64)size, (u64)align, &start, &end,
  1349. (void *)_RET_IP_);
  1350. return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE,
  1351. false);
  1352. }
  1353. /**
  1354. * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node
  1355. * @size: size of memory block to be allocated in bytes
  1356. * @align: alignment of the region and block's size
  1357. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1358. *
  1359. * Allocates memory block from the specified NUMA node. If the node
  1360. * has no available memory, attempts to allocated from any node in the
  1361. * system.
  1362. *
  1363. * Return: physical address of the allocated memory block on success,
  1364. * %0 on failure.
  1365. */
  1366. phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid)
  1367. {
  1368. return memblock_alloc_range_nid(size, align, 0,
  1369. MEMBLOCK_ALLOC_ACCESSIBLE, nid, false);
  1370. }
  1371. /**
  1372. * memblock_alloc_internal - allocate boot memory block
  1373. * @size: size of memory block to be allocated in bytes
  1374. * @align: alignment of the region and block's size
  1375. * @min_addr: the lower bound of the memory region to allocate (phys address)
  1376. * @max_addr: the upper bound of the memory region to allocate (phys address)
  1377. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1378. * @exact_nid: control the allocation fall back to other nodes
  1379. *
  1380. * Allocates memory block using memblock_alloc_range_nid() and
  1381. * converts the returned physical address to virtual.
  1382. *
  1383. * The @min_addr limit is dropped if it can not be satisfied and the allocation
  1384. * will fall back to memory below @min_addr. Other constraints, such
  1385. * as node and mirrored memory will be handled again in
  1386. * memblock_alloc_range_nid().
  1387. *
  1388. * Return:
  1389. * Virtual address of allocated memory block on success, NULL on failure.
  1390. */
  1391. static void * __init memblock_alloc_internal(
  1392. phys_addr_t size, phys_addr_t align,
  1393. phys_addr_t min_addr, phys_addr_t max_addr,
  1394. int nid, bool exact_nid)
  1395. {
  1396. phys_addr_t alloc;
  1397. if (max_addr > memblock.current_limit)
  1398. max_addr = memblock.current_limit;
  1399. alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid,
  1400. exact_nid);
  1401. /* retry allocation without lower limit */
  1402. if (!alloc && min_addr)
  1403. alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid,
  1404. exact_nid);
  1405. if (!alloc)
  1406. return NULL;
  1407. return phys_to_virt(alloc);
  1408. }
  1409. /**
  1410. * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node
  1411. * without zeroing memory
  1412. * @size: size of memory block to be allocated in bytes
  1413. * @align: alignment of the region and block's size
  1414. * @min_addr: the lower bound of the memory region from where the allocation
  1415. * is preferred (phys address)
  1416. * @max_addr: the upper bound of the memory region from where the allocation
  1417. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1418. * allocate only from memory limited by memblock.current_limit value
  1419. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1420. *
  1421. * Public function, provides additional debug information (including caller
  1422. * info), if enabled. Does not zero allocated memory.
  1423. *
  1424. * Return:
  1425. * Virtual address of allocated memory block on success, NULL on failure.
  1426. */
  1427. void * __init memblock_alloc_exact_nid_raw(
  1428. phys_addr_t size, phys_addr_t align,
  1429. phys_addr_t min_addr, phys_addr_t max_addr,
  1430. int nid)
  1431. {
  1432. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1433. __func__, (u64)size, (u64)align, nid, &min_addr,
  1434. &max_addr, (void *)_RET_IP_);
  1435. return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
  1436. true);
  1437. }
  1438. /**
  1439. * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing
  1440. * memory and without panicking
  1441. * @size: size of memory block to be allocated in bytes
  1442. * @align: alignment of the region and block's size
  1443. * @min_addr: the lower bound of the memory region from where the allocation
  1444. * is preferred (phys address)
  1445. * @max_addr: the upper bound of the memory region from where the allocation
  1446. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1447. * allocate only from memory limited by memblock.current_limit value
  1448. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1449. *
  1450. * Public function, provides additional debug information (including caller
  1451. * info), if enabled. Does not zero allocated memory, does not panic if request
  1452. * cannot be satisfied.
  1453. *
  1454. * Return:
  1455. * Virtual address of allocated memory block on success, NULL on failure.
  1456. */
  1457. void * __init memblock_alloc_try_nid_raw(
  1458. phys_addr_t size, phys_addr_t align,
  1459. phys_addr_t min_addr, phys_addr_t max_addr,
  1460. int nid)
  1461. {
  1462. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1463. __func__, (u64)size, (u64)align, nid, &min_addr,
  1464. &max_addr, (void *)_RET_IP_);
  1465. return memblock_alloc_internal(size, align, min_addr, max_addr, nid,
  1466. false);
  1467. }
  1468. /**
  1469. * memblock_alloc_try_nid - allocate boot memory block
  1470. * @size: size of memory block to be allocated in bytes
  1471. * @align: alignment of the region and block's size
  1472. * @min_addr: the lower bound of the memory region from where the allocation
  1473. * is preferred (phys address)
  1474. * @max_addr: the upper bound of the memory region from where the allocation
  1475. * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to
  1476. * allocate only from memory limited by memblock.current_limit value
  1477. * @nid: nid of the free area to find, %NUMA_NO_NODE for any node
  1478. *
  1479. * Public function, provides additional debug information (including caller
  1480. * info), if enabled. This function zeroes the allocated memory.
  1481. *
  1482. * Return:
  1483. * Virtual address of allocated memory block on success, NULL on failure.
  1484. */
  1485. void * __init memblock_alloc_try_nid(
  1486. phys_addr_t size, phys_addr_t align,
  1487. phys_addr_t min_addr, phys_addr_t max_addr,
  1488. int nid)
  1489. {
  1490. void *ptr;
  1491. memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n",
  1492. __func__, (u64)size, (u64)align, nid, &min_addr,
  1493. &max_addr, (void *)_RET_IP_);
  1494. ptr = memblock_alloc_internal(size, align,
  1495. min_addr, max_addr, nid, false);
  1496. if (ptr)
  1497. memset(ptr, 0, size);
  1498. return ptr;
  1499. }
  1500. /**
  1501. * memblock_free_late - free pages directly to buddy allocator
  1502. * @base: phys starting address of the boot memory block
  1503. * @size: size of the boot memory block in bytes
  1504. *
  1505. * This is only useful when the memblock allocator has already been torn
  1506. * down, but we are still initializing the system. Pages are released directly
  1507. * to the buddy allocator.
  1508. */
  1509. void __init memblock_free_late(phys_addr_t base, phys_addr_t size)
  1510. {
  1511. phys_addr_t cursor, end;
  1512. end = base + size - 1;
  1513. memblock_dbg("%s: [%pa-%pa] %pS\n",
  1514. __func__, &base, &end, (void *)_RET_IP_);
  1515. kmemleak_free_part_phys(base, size);
  1516. cursor = PFN_UP(base);
  1517. end = PFN_DOWN(base + size);
  1518. for (; cursor < end; cursor++) {
  1519. memblock_free_pages(pfn_to_page(cursor), cursor, 0);
  1520. totalram_pages_inc();
  1521. }
  1522. }
  1523. /*
  1524. * Remaining API functions
  1525. */
  1526. phys_addr_t __init_memblock memblock_phys_mem_size(void)
  1527. {
  1528. return memblock.memory.total_size;
  1529. }
  1530. phys_addr_t __init_memblock memblock_reserved_size(void)
  1531. {
  1532. return memblock.reserved.total_size;
  1533. }
  1534. /**
  1535. * memblock_estimated_nr_free_pages - return estimated number of free pages
  1536. * from memblock point of view
  1537. *
  1538. * During bootup, subsystems might need a rough estimate of the number of free
  1539. * pages in the whole system, before precise numbers are available from the
  1540. * buddy. Especially with CONFIG_DEFERRED_STRUCT_PAGE_INIT, the numbers
  1541. * obtained from the buddy might be very imprecise during bootup.
  1542. *
  1543. * Return:
  1544. * An estimated number of free pages from memblock point of view.
  1545. */
  1546. unsigned long __init memblock_estimated_nr_free_pages(void)
  1547. {
  1548. return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size());
  1549. }
  1550. /* lowest address */
  1551. phys_addr_t __init_memblock memblock_start_of_DRAM(void)
  1552. {
  1553. return memblock.memory.regions[0].base;
  1554. }
  1555. phys_addr_t __init_memblock memblock_end_of_DRAM(void)
  1556. {
  1557. int idx = memblock.memory.cnt - 1;
  1558. return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size);
  1559. }
  1560. static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit)
  1561. {
  1562. phys_addr_t max_addr = PHYS_ADDR_MAX;
  1563. struct memblock_region *r;
  1564. /*
  1565. * translate the memory @limit size into the max address within one of
  1566. * the memory memblock regions, if the @limit exceeds the total size
  1567. * of those regions, max_addr will keep original value PHYS_ADDR_MAX
  1568. */
  1569. for_each_mem_region(r) {
  1570. if (limit <= r->size) {
  1571. max_addr = r->base + limit;
  1572. break;
  1573. }
  1574. limit -= r->size;
  1575. }
  1576. return max_addr;
  1577. }
  1578. void __init memblock_enforce_memory_limit(phys_addr_t limit)
  1579. {
  1580. phys_addr_t max_addr;
  1581. if (!limit)
  1582. return;
  1583. max_addr = __find_max_addr(limit);
  1584. /* @limit exceeds the total size of the memory, do nothing */
  1585. if (max_addr == PHYS_ADDR_MAX)
  1586. return;
  1587. /* truncate both memory and reserved regions */
  1588. memblock_remove_range(&memblock.memory, max_addr,
  1589. PHYS_ADDR_MAX);
  1590. memblock_remove_range(&memblock.reserved, max_addr,
  1591. PHYS_ADDR_MAX);
  1592. }
  1593. void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size)
  1594. {
  1595. int start_rgn, end_rgn;
  1596. int i, ret;
  1597. if (!size)
  1598. return;
  1599. if (!memblock_memory->total_size) {
  1600. pr_warn("%s: No memory registered yet\n", __func__);
  1601. return;
  1602. }
  1603. ret = memblock_isolate_range(&memblock.memory, base, size,
  1604. &start_rgn, &end_rgn);
  1605. if (ret)
  1606. return;
  1607. /* remove all the MAP regions */
  1608. for (i = memblock.memory.cnt - 1; i >= end_rgn; i--)
  1609. if (!memblock_is_nomap(&memblock.memory.regions[i]))
  1610. memblock_remove_region(&memblock.memory, i);
  1611. for (i = start_rgn - 1; i >= 0; i--)
  1612. if (!memblock_is_nomap(&memblock.memory.regions[i]))
  1613. memblock_remove_region(&memblock.memory, i);
  1614. /* truncate the reserved regions */
  1615. memblock_remove_range(&memblock.reserved, 0, base);
  1616. memblock_remove_range(&memblock.reserved,
  1617. base + size, PHYS_ADDR_MAX);
  1618. }
  1619. void __init memblock_mem_limit_remove_map(phys_addr_t limit)
  1620. {
  1621. phys_addr_t max_addr;
  1622. if (!limit)
  1623. return;
  1624. max_addr = __find_max_addr(limit);
  1625. /* @limit exceeds the total size of the memory, do nothing */
  1626. if (max_addr == PHYS_ADDR_MAX)
  1627. return;
  1628. memblock_cap_memory_range(0, max_addr);
  1629. }
  1630. static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr)
  1631. {
  1632. unsigned int left = 0, right = type->cnt;
  1633. do {
  1634. unsigned int mid = (right + left) / 2;
  1635. if (addr < type->regions[mid].base)
  1636. right = mid;
  1637. else if (addr >= (type->regions[mid].base +
  1638. type->regions[mid].size))
  1639. left = mid + 1;
  1640. else
  1641. return mid;
  1642. } while (left < right);
  1643. return -1;
  1644. }
  1645. bool __init_memblock memblock_is_reserved(phys_addr_t addr)
  1646. {
  1647. return memblock_search(&memblock.reserved, addr) != -1;
  1648. }
  1649. bool __init_memblock memblock_is_memory(phys_addr_t addr)
  1650. {
  1651. return memblock_search(&memblock.memory, addr) != -1;
  1652. }
  1653. bool __init_memblock memblock_is_map_memory(phys_addr_t addr)
  1654. {
  1655. int i = memblock_search(&memblock.memory, addr);
  1656. if (i == -1)
  1657. return false;
  1658. return !memblock_is_nomap(&memblock.memory.regions[i]);
  1659. }
  1660. int __init_memblock memblock_search_pfn_nid(unsigned long pfn,
  1661. unsigned long *start_pfn, unsigned long *end_pfn)
  1662. {
  1663. struct memblock_type *type = &memblock.memory;
  1664. int mid = memblock_search(type, PFN_PHYS(pfn));
  1665. if (mid == -1)
  1666. return NUMA_NO_NODE;
  1667. *start_pfn = PFN_DOWN(type->regions[mid].base);
  1668. *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size);
  1669. return memblock_get_region_node(&type->regions[mid]);
  1670. }
  1671. /**
  1672. * memblock_is_region_memory - check if a region is a subset of memory
  1673. * @base: base of region to check
  1674. * @size: size of region to check
  1675. *
  1676. * Check if the region [@base, @base + @size) is a subset of a memory block.
  1677. *
  1678. * Return:
  1679. * 0 if false, non-zero if true
  1680. */
  1681. bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size)
  1682. {
  1683. int idx = memblock_search(&memblock.memory, base);
  1684. phys_addr_t end = base + memblock_cap_size(base, &size);
  1685. if (idx == -1)
  1686. return false;
  1687. return (memblock.memory.regions[idx].base +
  1688. memblock.memory.regions[idx].size) >= end;
  1689. }
  1690. /**
  1691. * memblock_is_region_reserved - check if a region intersects reserved memory
  1692. * @base: base of region to check
  1693. * @size: size of region to check
  1694. *
  1695. * Check if the region [@base, @base + @size) intersects a reserved
  1696. * memory block.
  1697. *
  1698. * Return:
  1699. * True if they intersect, false if not.
  1700. */
  1701. bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size)
  1702. {
  1703. return memblock_overlaps_region(&memblock.reserved, base, size);
  1704. }
  1705. void __init_memblock memblock_trim_memory(phys_addr_t align)
  1706. {
  1707. phys_addr_t start, end, orig_start, orig_end;
  1708. struct memblock_region *r;
  1709. for_each_mem_region(r) {
  1710. orig_start = r->base;
  1711. orig_end = r->base + r->size;
  1712. start = round_up(orig_start, align);
  1713. end = round_down(orig_end, align);
  1714. if (start == orig_start && end == orig_end)
  1715. continue;
  1716. if (start < end) {
  1717. r->base = start;
  1718. r->size = end - start;
  1719. } else {
  1720. memblock_remove_region(&memblock.memory,
  1721. r - memblock.memory.regions);
  1722. r--;
  1723. }
  1724. }
  1725. }
  1726. void __init_memblock memblock_set_current_limit(phys_addr_t limit)
  1727. {
  1728. memblock.current_limit = limit;
  1729. }
  1730. phys_addr_t __init_memblock memblock_get_current_limit(void)
  1731. {
  1732. return memblock.current_limit;
  1733. }
  1734. static void __init_memblock memblock_dump(struct memblock_type *type)
  1735. {
  1736. phys_addr_t base, end, size;
  1737. enum memblock_flags flags;
  1738. int idx;
  1739. struct memblock_region *rgn;
  1740. pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt);
  1741. for_each_memblock_type(idx, type, rgn) {
  1742. char nid_buf[32] = "";
  1743. base = rgn->base;
  1744. size = rgn->size;
  1745. end = base + size - 1;
  1746. flags = rgn->flags;
  1747. #ifdef CONFIG_NUMA
  1748. if (numa_valid_node(memblock_get_region_node(rgn)))
  1749. snprintf(nid_buf, sizeof(nid_buf), " on node %d",
  1750. memblock_get_region_node(rgn));
  1751. #endif
  1752. pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n",
  1753. type->name, idx, &base, &end, &size, nid_buf, flags);
  1754. }
  1755. }
  1756. static void __init_memblock __memblock_dump_all(void)
  1757. {
  1758. pr_info("MEMBLOCK configuration:\n");
  1759. pr_info(" memory size = %pa reserved size = %pa\n",
  1760. &memblock.memory.total_size,
  1761. &memblock.reserved.total_size);
  1762. memblock_dump(&memblock.memory);
  1763. memblock_dump(&memblock.reserved);
  1764. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  1765. memblock_dump(&physmem);
  1766. #endif
  1767. }
  1768. void __init_memblock memblock_dump_all(void)
  1769. {
  1770. if (memblock_debug)
  1771. __memblock_dump_all();
  1772. }
  1773. void __init memblock_allow_resize(void)
  1774. {
  1775. memblock_can_resize = 1;
  1776. }
  1777. static int __init early_memblock(char *p)
  1778. {
  1779. if (p && strstr(p, "debug"))
  1780. memblock_debug = 1;
  1781. return 0;
  1782. }
  1783. early_param("memblock", early_memblock);
  1784. static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  1785. {
  1786. struct page *start_pg, *end_pg;
  1787. phys_addr_t pg, pgend;
  1788. /*
  1789. * Convert start_pfn/end_pfn to a struct page pointer.
  1790. */
  1791. start_pg = pfn_to_page(start_pfn - 1) + 1;
  1792. end_pg = pfn_to_page(end_pfn - 1) + 1;
  1793. /*
  1794. * Convert to physical addresses, and round start upwards and end
  1795. * downwards.
  1796. */
  1797. pg = PAGE_ALIGN(__pa(start_pg));
  1798. pgend = PAGE_ALIGN_DOWN(__pa(end_pg));
  1799. /*
  1800. * If there are free pages between these, free the section of the
  1801. * memmap array.
  1802. */
  1803. if (pg < pgend)
  1804. memblock_phys_free(pg, pgend - pg);
  1805. }
  1806. /*
  1807. * The mem_map array can get very big. Free the unused area of the memory map.
  1808. */
  1809. static void __init free_unused_memmap(void)
  1810. {
  1811. unsigned long start, end, prev_end = 0;
  1812. int i;
  1813. if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) ||
  1814. IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP))
  1815. return;
  1816. /*
  1817. * This relies on each bank being in address order.
  1818. * The banks are sorted previously in bootmem_init().
  1819. */
  1820. for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
  1821. #ifdef CONFIG_SPARSEMEM
  1822. /*
  1823. * Take care not to free memmap entries that don't exist
  1824. * due to SPARSEMEM sections which aren't present.
  1825. */
  1826. start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
  1827. #endif
  1828. /*
  1829. * Align down here since many operations in VM subsystem
  1830. * presume that there are no holes in the memory map inside
  1831. * a pageblock
  1832. */
  1833. start = pageblock_start_pfn(start);
  1834. /*
  1835. * If we had a previous bank, and there is a space
  1836. * between the current bank and the previous, free it.
  1837. */
  1838. if (prev_end && prev_end < start)
  1839. free_memmap(prev_end, start);
  1840. /*
  1841. * Align up here since many operations in VM subsystem
  1842. * presume that there are no holes in the memory map inside
  1843. * a pageblock
  1844. */
  1845. prev_end = pageblock_align(end);
  1846. }
  1847. #ifdef CONFIG_SPARSEMEM
  1848. if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) {
  1849. prev_end = pageblock_align(end);
  1850. free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
  1851. }
  1852. #endif
  1853. }
  1854. static void __init __free_pages_memory(unsigned long start, unsigned long end)
  1855. {
  1856. int order;
  1857. while (start < end) {
  1858. /*
  1859. * Free the pages in the largest chunks alignment allows.
  1860. *
  1861. * __ffs() behaviour is undefined for 0. start == 0 is
  1862. * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for
  1863. * the case.
  1864. */
  1865. if (start)
  1866. order = min_t(int, MAX_PAGE_ORDER, __ffs(start));
  1867. else
  1868. order = MAX_PAGE_ORDER;
  1869. while (start + (1UL << order) > end)
  1870. order--;
  1871. memblock_free_pages(pfn_to_page(start), start, order);
  1872. start += (1UL << order);
  1873. }
  1874. }
  1875. static unsigned long __init __free_memory_core(phys_addr_t start,
  1876. phys_addr_t end)
  1877. {
  1878. unsigned long start_pfn = PFN_UP(start);
  1879. unsigned long end_pfn = min_t(unsigned long,
  1880. PFN_DOWN(end), max_low_pfn);
  1881. if (start_pfn >= end_pfn)
  1882. return 0;
  1883. __free_pages_memory(start_pfn, end_pfn);
  1884. return end_pfn - start_pfn;
  1885. }
  1886. static void __init memmap_init_reserved_pages(void)
  1887. {
  1888. struct memblock_region *region;
  1889. phys_addr_t start, end;
  1890. int nid;
  1891. /*
  1892. * set nid on all reserved pages and also treat struct
  1893. * pages for the NOMAP regions as PageReserved
  1894. */
  1895. for_each_mem_region(region) {
  1896. nid = memblock_get_region_node(region);
  1897. start = region->base;
  1898. end = start + region->size;
  1899. if (memblock_is_nomap(region))
  1900. reserve_bootmem_region(start, end, nid);
  1901. memblock_set_node(start, end, &memblock.reserved, nid);
  1902. }
  1903. /*
  1904. * initialize struct pages for reserved regions that don't have
  1905. * the MEMBLOCK_RSRV_NOINIT flag set
  1906. */
  1907. for_each_reserved_mem_region(region) {
  1908. if (!memblock_is_reserved_noinit(region)) {
  1909. nid = memblock_get_region_node(region);
  1910. start = region->base;
  1911. end = start + region->size;
  1912. if (!numa_valid_node(nid))
  1913. nid = early_pfn_to_nid(PFN_DOWN(start));
  1914. reserve_bootmem_region(start, end, nid);
  1915. }
  1916. }
  1917. }
  1918. static unsigned long __init free_low_memory_core_early(void)
  1919. {
  1920. unsigned long count = 0;
  1921. phys_addr_t start, end;
  1922. u64 i;
  1923. memblock_clear_hotplug(0, -1);
  1924. memmap_init_reserved_pages();
  1925. /*
  1926. * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
  1927. * because in some case like Node0 doesn't have RAM installed
  1928. * low ram will be on Node1
  1929. */
  1930. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
  1931. NULL)
  1932. count += __free_memory_core(start, end);
  1933. return count;
  1934. }
  1935. static int reset_managed_pages_done __initdata;
  1936. static void __init reset_node_managed_pages(pg_data_t *pgdat)
  1937. {
  1938. struct zone *z;
  1939. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  1940. atomic_long_set(&z->managed_pages, 0);
  1941. }
  1942. void __init reset_all_zones_managed_pages(void)
  1943. {
  1944. struct pglist_data *pgdat;
  1945. if (reset_managed_pages_done)
  1946. return;
  1947. for_each_online_pgdat(pgdat)
  1948. reset_node_managed_pages(pgdat);
  1949. reset_managed_pages_done = 1;
  1950. }
  1951. /**
  1952. * memblock_free_all - release free pages to the buddy allocator
  1953. */
  1954. void __init memblock_free_all(void)
  1955. {
  1956. unsigned long pages;
  1957. free_unused_memmap();
  1958. reset_all_zones_managed_pages();
  1959. pages = free_low_memory_core_early();
  1960. totalram_pages_add(pages);
  1961. }
  1962. /* Keep a table to reserve named memory */
  1963. #define RESERVE_MEM_MAX_ENTRIES 8
  1964. #define RESERVE_MEM_NAME_SIZE 16
  1965. struct reserve_mem_table {
  1966. char name[RESERVE_MEM_NAME_SIZE];
  1967. phys_addr_t start;
  1968. phys_addr_t size;
  1969. };
  1970. static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES];
  1971. static int reserved_mem_count;
  1972. /* Add wildcard region with a lookup name */
  1973. static void __init reserved_mem_add(phys_addr_t start, phys_addr_t size,
  1974. const char *name)
  1975. {
  1976. struct reserve_mem_table *map;
  1977. map = &reserved_mem_table[reserved_mem_count++];
  1978. map->start = start;
  1979. map->size = size;
  1980. strscpy(map->name, name);
  1981. }
  1982. /**
  1983. * reserve_mem_find_by_name - Find reserved memory region with a given name
  1984. * @name: The name that is attached to a reserved memory region
  1985. * @start: If found, holds the start address
  1986. * @size: If found, holds the size of the address.
  1987. *
  1988. * @start and @size are only updated if @name is found.
  1989. *
  1990. * Returns: 1 if found or 0 if not found.
  1991. */
  1992. int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size)
  1993. {
  1994. struct reserve_mem_table *map;
  1995. int i;
  1996. for (i = 0; i < reserved_mem_count; i++) {
  1997. map = &reserved_mem_table[i];
  1998. if (!map->size)
  1999. continue;
  2000. if (strcmp(name, map->name) == 0) {
  2001. *start = map->start;
  2002. *size = map->size;
  2003. return 1;
  2004. }
  2005. }
  2006. return 0;
  2007. }
  2008. EXPORT_SYMBOL_GPL(reserve_mem_find_by_name);
  2009. /*
  2010. * Parse reserve_mem=nn:align:name
  2011. */
  2012. static int __init reserve_mem(char *p)
  2013. {
  2014. phys_addr_t start, size, align, tmp;
  2015. char *name;
  2016. char *oldp;
  2017. int len;
  2018. if (!p)
  2019. return -EINVAL;
  2020. /* Check if there's room for more reserved memory */
  2021. if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES)
  2022. return -EBUSY;
  2023. oldp = p;
  2024. size = memparse(p, &p);
  2025. if (!size || p == oldp)
  2026. return -EINVAL;
  2027. if (*p != ':')
  2028. return -EINVAL;
  2029. align = memparse(p+1, &p);
  2030. if (*p != ':')
  2031. return -EINVAL;
  2032. /*
  2033. * memblock_phys_alloc() doesn't like a zero size align,
  2034. * but it is OK for this command to have it.
  2035. */
  2036. if (align < SMP_CACHE_BYTES)
  2037. align = SMP_CACHE_BYTES;
  2038. name = p + 1;
  2039. len = strlen(name);
  2040. /* name needs to have length but not too big */
  2041. if (!len || len >= RESERVE_MEM_NAME_SIZE)
  2042. return -EINVAL;
  2043. /* Make sure that name has text */
  2044. for (p = name; *p; p++) {
  2045. if (!isspace(*p))
  2046. break;
  2047. }
  2048. if (!*p)
  2049. return -EINVAL;
  2050. /* Make sure the name is not already used */
  2051. if (reserve_mem_find_by_name(name, &start, &tmp))
  2052. return -EBUSY;
  2053. start = memblock_phys_alloc(size, align);
  2054. if (!start)
  2055. return -ENOMEM;
  2056. reserved_mem_add(start, size, name);
  2057. return 1;
  2058. }
  2059. __setup("reserve_mem=", reserve_mem);
  2060. #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK)
  2061. static const char * const flagname[] = {
  2062. [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG",
  2063. [ilog2(MEMBLOCK_MIRROR)] = "MIRROR",
  2064. [ilog2(MEMBLOCK_NOMAP)] = "NOMAP",
  2065. [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG",
  2066. [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT",
  2067. };
  2068. static int memblock_debug_show(struct seq_file *m, void *private)
  2069. {
  2070. struct memblock_type *type = m->private;
  2071. struct memblock_region *reg;
  2072. int i, j, nid;
  2073. unsigned int count = ARRAY_SIZE(flagname);
  2074. phys_addr_t end;
  2075. for (i = 0; i < type->cnt; i++) {
  2076. reg = &type->regions[i];
  2077. end = reg->base + reg->size - 1;
  2078. nid = memblock_get_region_node(reg);
  2079. seq_printf(m, "%4d: ", i);
  2080. seq_printf(m, "%pa..%pa ", &reg->base, &end);
  2081. if (numa_valid_node(nid))
  2082. seq_printf(m, "%4d ", nid);
  2083. else
  2084. seq_printf(m, "%4c ", 'x');
  2085. if (reg->flags) {
  2086. for (j = 0; j < count; j++) {
  2087. if (reg->flags & (1U << j)) {
  2088. seq_printf(m, "%s\n", flagname[j]);
  2089. break;
  2090. }
  2091. }
  2092. if (j == count)
  2093. seq_printf(m, "%s\n", "UNKNOWN");
  2094. } else {
  2095. seq_printf(m, "%s\n", "NONE");
  2096. }
  2097. }
  2098. return 0;
  2099. }
  2100. DEFINE_SHOW_ATTRIBUTE(memblock_debug);
  2101. static int __init memblock_init_debugfs(void)
  2102. {
  2103. struct dentry *root = debugfs_create_dir("memblock", NULL);
  2104. debugfs_create_file("memory", 0444, root,
  2105. &memblock.memory, &memblock_debug_fops);
  2106. debugfs_create_file("reserved", 0444, root,
  2107. &memblock.reserved, &memblock_debug_fops);
  2108. #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP
  2109. debugfs_create_file("physmem", 0444, root, &physmem,
  2110. &memblock_debug_fops);
  2111. #endif
  2112. return 0;
  2113. }
  2114. __initcall(memblock_init_debugfs);
  2115. #endif /* CONFIG_DEBUG_FS */