memblock.c 45 KB

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