memblock.c 48 KB

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