memblock.c 51 KB

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