memblock.c 51 KB

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