alloc_api.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "alloc_api.h"
  3. static int alloc_test_flags = TEST_F_NONE;
  4. static inline const char * const get_memblock_alloc_name(int flags)
  5. {
  6. if (flags & TEST_F_RAW)
  7. return "memblock_alloc_raw";
  8. return "memblock_alloc";
  9. }
  10. static inline void *run_memblock_alloc(phys_addr_t size, phys_addr_t align)
  11. {
  12. if (alloc_test_flags & TEST_F_RAW)
  13. return memblock_alloc_raw(size, align);
  14. return memblock_alloc(size, align);
  15. }
  16. /*
  17. * A simple test that tries to allocate a small memory region.
  18. * Expect to allocate an aligned region near the end of the available memory.
  19. */
  20. static int alloc_top_down_simple_check(void)
  21. {
  22. struct memblock_region *rgn = &memblock.reserved.regions[0];
  23. void *allocated_ptr = NULL;
  24. phys_addr_t size = SZ_2;
  25. phys_addr_t expected_start;
  26. PREFIX_PUSH();
  27. setup_memblock();
  28. expected_start = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
  29. allocated_ptr = run_memblock_alloc(size, SMP_CACHE_BYTES);
  30. ASSERT_NE(allocated_ptr, NULL);
  31. assert_mem_content(allocated_ptr, size, alloc_test_flags);
  32. ASSERT_EQ(rgn->size, size);
  33. ASSERT_EQ(rgn->base, expected_start);
  34. ASSERT_EQ(memblock.reserved.cnt, 1);
  35. ASSERT_EQ(memblock.reserved.total_size, size);
  36. test_pass_pop();
  37. return 0;
  38. }
  39. /*
  40. * A test that tries to allocate memory next to a reserved region that starts at
  41. * the misaligned address. Expect to create two separate entries, with the new
  42. * entry aligned to the provided alignment:
  43. *
  44. * +
  45. * | +--------+ +--------|
  46. * | | rgn2 | | rgn1 |
  47. * +------------+--------+---------+--------+
  48. * ^
  49. * |
  50. * Aligned address boundary
  51. *
  52. * The allocation direction is top-down and region arrays are sorted from lower
  53. * to higher addresses, so the new region will be the first entry in
  54. * memory.reserved array. The previously reserved region does not get modified.
  55. * Region counter and total size get updated.
  56. */
  57. static int alloc_top_down_disjoint_check(void)
  58. {
  59. /* After allocation, this will point to the "old" region */
  60. struct memblock_region *rgn1 = &memblock.reserved.regions[1];
  61. struct memblock_region *rgn2 = &memblock.reserved.regions[0];
  62. struct region r1;
  63. void *allocated_ptr = NULL;
  64. phys_addr_t r2_size = SZ_16;
  65. /* Use custom alignment */
  66. phys_addr_t alignment = SMP_CACHE_BYTES * 2;
  67. phys_addr_t total_size;
  68. phys_addr_t expected_start;
  69. PREFIX_PUSH();
  70. setup_memblock();
  71. r1.base = memblock_end_of_DRAM() - SZ_2;
  72. r1.size = SZ_2;
  73. total_size = r1.size + r2_size;
  74. expected_start = memblock_end_of_DRAM() - alignment;
  75. memblock_reserve(r1.base, r1.size);
  76. allocated_ptr = run_memblock_alloc(r2_size, alignment);
  77. ASSERT_NE(allocated_ptr, NULL);
  78. assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
  79. ASSERT_EQ(rgn1->size, r1.size);
  80. ASSERT_EQ(rgn1->base, r1.base);
  81. ASSERT_EQ(rgn2->size, r2_size);
  82. ASSERT_EQ(rgn2->base, expected_start);
  83. ASSERT_EQ(memblock.reserved.cnt, 2);
  84. ASSERT_EQ(memblock.reserved.total_size, total_size);
  85. test_pass_pop();
  86. return 0;
  87. }
  88. /*
  89. * A test that tries to allocate memory when there is enough space at the end
  90. * of the previously reserved block (i.e. first fit):
  91. *
  92. * | +--------+--------------|
  93. * | | r1 | r2 |
  94. * +--------------+--------+--------------+
  95. *
  96. * Expect a merge of both regions. Only the region size gets updated.
  97. */
  98. static int alloc_top_down_before_check(void)
  99. {
  100. struct memblock_region *rgn = &memblock.reserved.regions[0];
  101. void *allocated_ptr = NULL;
  102. /*
  103. * The first region ends at the aligned address to test region merging
  104. */
  105. phys_addr_t r1_size = SMP_CACHE_BYTES;
  106. phys_addr_t r2_size = SZ_512;
  107. phys_addr_t total_size = r1_size + r2_size;
  108. PREFIX_PUSH();
  109. setup_memblock();
  110. memblock_reserve_kern(memblock_end_of_DRAM() - total_size, r1_size);
  111. allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
  112. ASSERT_NE(allocated_ptr, NULL);
  113. assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
  114. ASSERT_EQ(rgn->size, total_size);
  115. ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - total_size);
  116. ASSERT_EQ(memblock.reserved.cnt, 1);
  117. ASSERT_EQ(memblock.reserved.total_size, total_size);
  118. test_pass_pop();
  119. return 0;
  120. }
  121. /*
  122. * A test that tries to allocate memory when there is not enough space at the
  123. * end of the previously reserved block (i.e. second fit):
  124. *
  125. * | +-----------+------+ |
  126. * | | r2 | r1 | |
  127. * +------------+-----------+------+-----+
  128. *
  129. * Expect a merge of both regions. Both the base address and size of the region
  130. * get updated.
  131. */
  132. static int alloc_top_down_after_check(void)
  133. {
  134. struct memblock_region *rgn = &memblock.reserved.regions[0];
  135. struct region r1;
  136. void *allocated_ptr = NULL;
  137. phys_addr_t r2_size = SZ_512;
  138. phys_addr_t total_size;
  139. PREFIX_PUSH();
  140. setup_memblock();
  141. /*
  142. * The first region starts at the aligned address to test region merging
  143. */
  144. r1.base = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
  145. r1.size = SZ_8;
  146. total_size = r1.size + r2_size;
  147. memblock_reserve_kern(r1.base, r1.size);
  148. allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
  149. ASSERT_NE(allocated_ptr, NULL);
  150. assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
  151. ASSERT_EQ(rgn->size, total_size);
  152. ASSERT_EQ(rgn->base, r1.base - r2_size);
  153. ASSERT_EQ(memblock.reserved.cnt, 1);
  154. ASSERT_EQ(memblock.reserved.total_size, total_size);
  155. test_pass_pop();
  156. return 0;
  157. }
  158. /*
  159. * A test that tries to allocate memory when there are two reserved regions with
  160. * a gap too small to fit the new region:
  161. *
  162. * | +--------+----------+ +------|
  163. * | | r3 | r2 | | r1 |
  164. * +-------+--------+----------+---+------+
  165. *
  166. * Expect to allocate a region before the one that starts at the lower address,
  167. * and merge them into one. The region counter and total size fields get
  168. * updated.
  169. */
  170. static int alloc_top_down_second_fit_check(void)
  171. {
  172. struct memblock_region *rgn = &memblock.reserved.regions[0];
  173. struct region r1, r2;
  174. void *allocated_ptr = NULL;
  175. phys_addr_t r3_size = SZ_1K;
  176. phys_addr_t total_size;
  177. PREFIX_PUSH();
  178. setup_memblock();
  179. r1.base = memblock_end_of_DRAM() - SZ_512;
  180. r1.size = SZ_512;
  181. r2.base = r1.base - SZ_512;
  182. r2.size = SZ_256;
  183. total_size = r1.size + r2.size + r3_size;
  184. memblock_reserve_kern(r1.base, r1.size);
  185. memblock_reserve_kern(r2.base, r2.size);
  186. allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
  187. ASSERT_NE(allocated_ptr, NULL);
  188. assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
  189. ASSERT_EQ(rgn->size, r2.size + r3_size);
  190. ASSERT_EQ(rgn->base, r2.base - r3_size);
  191. ASSERT_EQ(memblock.reserved.cnt, 2);
  192. ASSERT_EQ(memblock.reserved.total_size, total_size);
  193. test_pass_pop();
  194. return 0;
  195. }
  196. /*
  197. * A test that tries to allocate memory when there are two reserved regions with
  198. * a gap big enough to accommodate the new region:
  199. *
  200. * | +--------+--------+--------+ |
  201. * | | r2 | r3 | r1 | |
  202. * +-----+--------+--------+--------+-----+
  203. *
  204. * Expect to merge all of them, creating one big entry in memblock.reserved
  205. * array. The region counter and total size fields get updated.
  206. */
  207. static int alloc_in_between_generic_check(void)
  208. {
  209. struct memblock_region *rgn = &memblock.reserved.regions[0];
  210. struct region r1, r2;
  211. void *allocated_ptr = NULL;
  212. phys_addr_t gap_size = SMP_CACHE_BYTES;
  213. phys_addr_t r3_size = SZ_64;
  214. /*
  215. * Calculate regions size so there's just enough space for the new entry
  216. */
  217. phys_addr_t rgn_size = (MEM_SIZE - (2 * gap_size + r3_size)) / 2;
  218. phys_addr_t total_size;
  219. PREFIX_PUSH();
  220. setup_memblock();
  221. r1.size = rgn_size;
  222. r1.base = memblock_end_of_DRAM() - (gap_size + rgn_size);
  223. r2.size = rgn_size;
  224. r2.base = memblock_start_of_DRAM() + gap_size;
  225. total_size = r1.size + r2.size + r3_size;
  226. memblock_reserve_kern(r1.base, r1.size);
  227. memblock_reserve_kern(r2.base, r2.size);
  228. allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
  229. ASSERT_NE(allocated_ptr, NULL);
  230. assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
  231. ASSERT_EQ(rgn->size, total_size);
  232. ASSERT_EQ(rgn->base, r1.base - r2.size - r3_size);
  233. ASSERT_EQ(memblock.reserved.cnt, 1);
  234. ASSERT_EQ(memblock.reserved.total_size, total_size);
  235. test_pass_pop();
  236. return 0;
  237. }
  238. /*
  239. * A test that tries to allocate memory when the memory is filled with reserved
  240. * regions with memory gaps too small to fit the new region:
  241. *
  242. * +-------+
  243. * | new |
  244. * +--+----+
  245. * | +-----+ +-----+ +-----+ |
  246. * | | res | | res | | res | |
  247. * +----+-----+----+-----+----+-----+----+
  248. *
  249. * Expect no allocation to happen.
  250. */
  251. static int alloc_small_gaps_generic_check(void)
  252. {
  253. void *allocated_ptr = NULL;
  254. phys_addr_t region_size = SZ_1K;
  255. phys_addr_t gap_size = SZ_256;
  256. phys_addr_t region_end;
  257. PREFIX_PUSH();
  258. setup_memblock();
  259. region_end = memblock_start_of_DRAM();
  260. while (region_end < memblock_end_of_DRAM()) {
  261. memblock_reserve(region_end + gap_size, region_size);
  262. region_end += gap_size + region_size;
  263. }
  264. allocated_ptr = run_memblock_alloc(region_size, SMP_CACHE_BYTES);
  265. ASSERT_EQ(allocated_ptr, NULL);
  266. test_pass_pop();
  267. return 0;
  268. }
  269. /*
  270. * A test that tries to allocate memory when all memory is reserved.
  271. * Expect no allocation to happen.
  272. */
  273. static int alloc_all_reserved_generic_check(void)
  274. {
  275. void *allocated_ptr = NULL;
  276. PREFIX_PUSH();
  277. setup_memblock();
  278. /* Simulate full memory */
  279. memblock_reserve(memblock_start_of_DRAM(), MEM_SIZE);
  280. allocated_ptr = run_memblock_alloc(SZ_256, SMP_CACHE_BYTES);
  281. ASSERT_EQ(allocated_ptr, NULL);
  282. test_pass_pop();
  283. return 0;
  284. }
  285. /*
  286. * A test that tries to allocate memory when the memory is almost full,
  287. * with not enough space left for the new region:
  288. *
  289. * +-------+
  290. * | new |
  291. * +-------+
  292. * |-----------------------------+ |
  293. * | reserved | |
  294. * +-----------------------------+---+
  295. *
  296. * Expect no allocation to happen.
  297. */
  298. static int alloc_no_space_generic_check(void)
  299. {
  300. void *allocated_ptr = NULL;
  301. phys_addr_t available_size = SZ_256;
  302. phys_addr_t reserved_size = MEM_SIZE - available_size;
  303. PREFIX_PUSH();
  304. setup_memblock();
  305. /* Simulate almost-full memory */
  306. memblock_reserve(memblock_start_of_DRAM(), reserved_size);
  307. allocated_ptr = run_memblock_alloc(SZ_1K, SMP_CACHE_BYTES);
  308. ASSERT_EQ(allocated_ptr, NULL);
  309. test_pass_pop();
  310. return 0;
  311. }
  312. /*
  313. * A test that tries to allocate memory when the memory is almost full,
  314. * but there is just enough space left:
  315. *
  316. * |---------------------------+---------|
  317. * | reserved | new |
  318. * +---------------------------+---------+
  319. *
  320. * Expect to allocate memory and merge all the regions. The total size field
  321. * gets updated.
  322. */
  323. static int alloc_limited_space_generic_check(void)
  324. {
  325. struct memblock_region *rgn = &memblock.reserved.regions[0];
  326. void *allocated_ptr = NULL;
  327. phys_addr_t available_size = SZ_256;
  328. phys_addr_t reserved_size = MEM_SIZE - available_size;
  329. PREFIX_PUSH();
  330. setup_memblock();
  331. /* Simulate almost-full memory */
  332. memblock_reserve_kern(memblock_start_of_DRAM(), reserved_size);
  333. allocated_ptr = run_memblock_alloc(available_size, SMP_CACHE_BYTES);
  334. ASSERT_NE(allocated_ptr, NULL);
  335. assert_mem_content(allocated_ptr, available_size, alloc_test_flags);
  336. ASSERT_EQ(rgn->size, MEM_SIZE);
  337. ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
  338. ASSERT_EQ(memblock.reserved.cnt, 1);
  339. ASSERT_EQ(memblock.reserved.total_size, MEM_SIZE);
  340. test_pass_pop();
  341. return 0;
  342. }
  343. /*
  344. * A test that tries to allocate memory when there is no available memory
  345. * registered (i.e. memblock.memory has only a dummy entry).
  346. * Expect no allocation to happen.
  347. */
  348. static int alloc_no_memory_generic_check(void)
  349. {
  350. struct memblock_region *rgn = &memblock.reserved.regions[0];
  351. void *allocated_ptr = NULL;
  352. PREFIX_PUSH();
  353. reset_memblock_regions();
  354. allocated_ptr = run_memblock_alloc(SZ_1K, SMP_CACHE_BYTES);
  355. ASSERT_EQ(allocated_ptr, NULL);
  356. ASSERT_EQ(rgn->size, 0);
  357. ASSERT_EQ(rgn->base, 0);
  358. ASSERT_EQ(memblock.reserved.total_size, 0);
  359. test_pass_pop();
  360. return 0;
  361. }
  362. /*
  363. * A test that tries to allocate a region that is larger than the total size of
  364. * available memory (memblock.memory):
  365. *
  366. * +-----------------------------------+
  367. * | new |
  368. * +-----------------------------------+
  369. * | |
  370. * | |
  371. * +---------------------------------+
  372. *
  373. * Expect no allocation to happen.
  374. */
  375. static int alloc_too_large_generic_check(void)
  376. {
  377. struct memblock_region *rgn = &memblock.reserved.regions[0];
  378. void *allocated_ptr = NULL;
  379. PREFIX_PUSH();
  380. setup_memblock();
  381. allocated_ptr = run_memblock_alloc(MEM_SIZE + SZ_2, SMP_CACHE_BYTES);
  382. ASSERT_EQ(allocated_ptr, NULL);
  383. ASSERT_EQ(rgn->size, 0);
  384. ASSERT_EQ(rgn->base, 0);
  385. ASSERT_EQ(memblock.reserved.total_size, 0);
  386. test_pass_pop();
  387. return 0;
  388. }
  389. /*
  390. * A simple test that tries to allocate a small memory region.
  391. * Expect to allocate an aligned region at the beginning of the available
  392. * memory.
  393. */
  394. static int alloc_bottom_up_simple_check(void)
  395. {
  396. struct memblock_region *rgn = &memblock.reserved.regions[0];
  397. void *allocated_ptr = NULL;
  398. PREFIX_PUSH();
  399. setup_memblock();
  400. allocated_ptr = run_memblock_alloc(SZ_2, SMP_CACHE_BYTES);
  401. ASSERT_NE(allocated_ptr, NULL);
  402. assert_mem_content(allocated_ptr, SZ_2, alloc_test_flags);
  403. ASSERT_EQ(rgn->size, SZ_2);
  404. ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
  405. ASSERT_EQ(memblock.reserved.cnt, 1);
  406. ASSERT_EQ(memblock.reserved.total_size, SZ_2);
  407. test_pass_pop();
  408. return 0;
  409. }
  410. /*
  411. * A test that tries to allocate memory next to a reserved region that starts at
  412. * the misaligned address. Expect to create two separate entries, with the new
  413. * entry aligned to the provided alignment:
  414. *
  415. * +
  416. * | +----------+ +----------+ |
  417. * | | rgn1 | | rgn2 | |
  418. * +----+----------+---+----------+-----+
  419. * ^
  420. * |
  421. * Aligned address boundary
  422. *
  423. * The allocation direction is bottom-up, so the new region will be the second
  424. * entry in memory.reserved array. The previously reserved region does not get
  425. * modified. Region counter and total size get updated.
  426. */
  427. static int alloc_bottom_up_disjoint_check(void)
  428. {
  429. struct memblock_region *rgn1 = &memblock.reserved.regions[0];
  430. struct memblock_region *rgn2 = &memblock.reserved.regions[1];
  431. struct region r1;
  432. void *allocated_ptr = NULL;
  433. phys_addr_t r2_size = SZ_16;
  434. /* Use custom alignment */
  435. phys_addr_t alignment = SMP_CACHE_BYTES * 2;
  436. phys_addr_t total_size;
  437. phys_addr_t expected_start;
  438. PREFIX_PUSH();
  439. setup_memblock();
  440. r1.base = memblock_start_of_DRAM() + SZ_2;
  441. r1.size = SZ_2;
  442. total_size = r1.size + r2_size;
  443. expected_start = memblock_start_of_DRAM() + alignment;
  444. memblock_reserve(r1.base, r1.size);
  445. allocated_ptr = run_memblock_alloc(r2_size, alignment);
  446. ASSERT_NE(allocated_ptr, NULL);
  447. assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
  448. ASSERT_EQ(rgn1->size, r1.size);
  449. ASSERT_EQ(rgn1->base, r1.base);
  450. ASSERT_EQ(rgn2->size, r2_size);
  451. ASSERT_EQ(rgn2->base, expected_start);
  452. ASSERT_EQ(memblock.reserved.cnt, 2);
  453. ASSERT_EQ(memblock.reserved.total_size, total_size);
  454. test_pass_pop();
  455. return 0;
  456. }
  457. /*
  458. * A test that tries to allocate memory when there is enough space at
  459. * the beginning of the previously reserved block (i.e. first fit):
  460. *
  461. * |------------------+--------+ |
  462. * | r1 | r2 | |
  463. * +------------------+--------+---------+
  464. *
  465. * Expect a merge of both regions. Only the region size gets updated.
  466. */
  467. static int alloc_bottom_up_before_check(void)
  468. {
  469. struct memblock_region *rgn = &memblock.reserved.regions[0];
  470. void *allocated_ptr = NULL;
  471. phys_addr_t r1_size = SZ_512;
  472. phys_addr_t r2_size = SZ_128;
  473. phys_addr_t total_size = r1_size + r2_size;
  474. PREFIX_PUSH();
  475. setup_memblock();
  476. memblock_reserve_kern(memblock_start_of_DRAM() + r1_size, r2_size);
  477. allocated_ptr = run_memblock_alloc(r1_size, SMP_CACHE_BYTES);
  478. ASSERT_NE(allocated_ptr, NULL);
  479. assert_mem_content(allocated_ptr, r1_size, alloc_test_flags);
  480. ASSERT_EQ(rgn->size, total_size);
  481. ASSERT_EQ(rgn->base, memblock_start_of_DRAM());
  482. ASSERT_EQ(memblock.reserved.cnt, 1);
  483. ASSERT_EQ(memblock.reserved.total_size, total_size);
  484. test_pass_pop();
  485. return 0;
  486. }
  487. /*
  488. * A test that tries to allocate memory when there is not enough space at
  489. * the beginning of the previously reserved block (i.e. second fit):
  490. *
  491. * | +--------+--------------+ |
  492. * | | r1 | r2 | |
  493. * +----+--------+--------------+---------+
  494. *
  495. * Expect a merge of both regions. Only the region size gets updated.
  496. */
  497. static int alloc_bottom_up_after_check(void)
  498. {
  499. struct memblock_region *rgn = &memblock.reserved.regions[0];
  500. struct region r1;
  501. void *allocated_ptr = NULL;
  502. phys_addr_t r2_size = SZ_512;
  503. phys_addr_t total_size;
  504. PREFIX_PUSH();
  505. setup_memblock();
  506. /*
  507. * The first region starts at the aligned address to test region merging
  508. */
  509. r1.base = memblock_start_of_DRAM() + SMP_CACHE_BYTES;
  510. r1.size = SZ_64;
  511. total_size = r1.size + r2_size;
  512. memblock_reserve_kern(r1.base, r1.size);
  513. allocated_ptr = run_memblock_alloc(r2_size, SMP_CACHE_BYTES);
  514. ASSERT_NE(allocated_ptr, NULL);
  515. assert_mem_content(allocated_ptr, r2_size, alloc_test_flags);
  516. ASSERT_EQ(rgn->size, total_size);
  517. ASSERT_EQ(rgn->base, r1.base);
  518. ASSERT_EQ(memblock.reserved.cnt, 1);
  519. ASSERT_EQ(memblock.reserved.total_size, total_size);
  520. test_pass_pop();
  521. return 0;
  522. }
  523. /*
  524. * A test that tries to allocate memory when there are two reserved regions, the
  525. * first one starting at the beginning of the available memory, with a gap too
  526. * small to fit the new region:
  527. *
  528. * |------------+ +--------+--------+ |
  529. * | r1 | | r2 | r3 | |
  530. * +------------+-----+--------+--------+--+
  531. *
  532. * Expect to allocate after the second region, which starts at the higher
  533. * address, and merge them into one. The region counter and total size fields
  534. * get updated.
  535. */
  536. static int alloc_bottom_up_second_fit_check(void)
  537. {
  538. struct memblock_region *rgn = &memblock.reserved.regions[1];
  539. struct region r1, r2;
  540. void *allocated_ptr = NULL;
  541. phys_addr_t r3_size = SZ_1K;
  542. phys_addr_t total_size;
  543. PREFIX_PUSH();
  544. setup_memblock();
  545. r1.base = memblock_start_of_DRAM();
  546. r1.size = SZ_512;
  547. r2.base = r1.base + r1.size + SZ_512;
  548. r2.size = SZ_256;
  549. total_size = r1.size + r2.size + r3_size;
  550. memblock_reserve_kern(r1.base, r1.size);
  551. memblock_reserve_kern(r2.base, r2.size);
  552. allocated_ptr = run_memblock_alloc(r3_size, SMP_CACHE_BYTES);
  553. ASSERT_NE(allocated_ptr, NULL);
  554. assert_mem_content(allocated_ptr, r3_size, alloc_test_flags);
  555. ASSERT_EQ(rgn->size, r2.size + r3_size);
  556. ASSERT_EQ(rgn->base, r2.base);
  557. ASSERT_EQ(memblock.reserved.cnt, 2);
  558. ASSERT_EQ(memblock.reserved.total_size, total_size);
  559. test_pass_pop();
  560. return 0;
  561. }
  562. /* Test case wrappers */
  563. static int alloc_simple_check(void)
  564. {
  565. test_print("\tRunning %s...\n", __func__);
  566. memblock_set_bottom_up(false);
  567. alloc_top_down_simple_check();
  568. memblock_set_bottom_up(true);
  569. alloc_bottom_up_simple_check();
  570. return 0;
  571. }
  572. static int alloc_disjoint_check(void)
  573. {
  574. test_print("\tRunning %s...\n", __func__);
  575. memblock_set_bottom_up(false);
  576. alloc_top_down_disjoint_check();
  577. memblock_set_bottom_up(true);
  578. alloc_bottom_up_disjoint_check();
  579. return 0;
  580. }
  581. static int alloc_before_check(void)
  582. {
  583. test_print("\tRunning %s...\n", __func__);
  584. memblock_set_bottom_up(false);
  585. alloc_top_down_before_check();
  586. memblock_set_bottom_up(true);
  587. alloc_bottom_up_before_check();
  588. return 0;
  589. }
  590. static int alloc_after_check(void)
  591. {
  592. test_print("\tRunning %s...\n", __func__);
  593. memblock_set_bottom_up(false);
  594. alloc_top_down_after_check();
  595. memblock_set_bottom_up(true);
  596. alloc_bottom_up_after_check();
  597. return 0;
  598. }
  599. static int alloc_in_between_check(void)
  600. {
  601. test_print("\tRunning %s...\n", __func__);
  602. run_top_down(alloc_in_between_generic_check);
  603. run_bottom_up(alloc_in_between_generic_check);
  604. return 0;
  605. }
  606. static int alloc_second_fit_check(void)
  607. {
  608. test_print("\tRunning %s...\n", __func__);
  609. memblock_set_bottom_up(false);
  610. alloc_top_down_second_fit_check();
  611. memblock_set_bottom_up(true);
  612. alloc_bottom_up_second_fit_check();
  613. return 0;
  614. }
  615. static int alloc_small_gaps_check(void)
  616. {
  617. test_print("\tRunning %s...\n", __func__);
  618. run_top_down(alloc_small_gaps_generic_check);
  619. run_bottom_up(alloc_small_gaps_generic_check);
  620. return 0;
  621. }
  622. static int alloc_all_reserved_check(void)
  623. {
  624. test_print("\tRunning %s...\n", __func__);
  625. run_top_down(alloc_all_reserved_generic_check);
  626. run_bottom_up(alloc_all_reserved_generic_check);
  627. return 0;
  628. }
  629. static int alloc_no_space_check(void)
  630. {
  631. test_print("\tRunning %s...\n", __func__);
  632. run_top_down(alloc_no_space_generic_check);
  633. run_bottom_up(alloc_no_space_generic_check);
  634. return 0;
  635. }
  636. static int alloc_limited_space_check(void)
  637. {
  638. test_print("\tRunning %s...\n", __func__);
  639. run_top_down(alloc_limited_space_generic_check);
  640. run_bottom_up(alloc_limited_space_generic_check);
  641. return 0;
  642. }
  643. static int alloc_no_memory_check(void)
  644. {
  645. test_print("\tRunning %s...\n", __func__);
  646. run_top_down(alloc_no_memory_generic_check);
  647. run_bottom_up(alloc_no_memory_generic_check);
  648. return 0;
  649. }
  650. static int alloc_too_large_check(void)
  651. {
  652. test_print("\tRunning %s...\n", __func__);
  653. run_top_down(alloc_too_large_generic_check);
  654. run_bottom_up(alloc_too_large_generic_check);
  655. return 0;
  656. }
  657. static int memblock_alloc_checks_internal(int flags)
  658. {
  659. const char *func = get_memblock_alloc_name(flags);
  660. alloc_test_flags = flags;
  661. prefix_reset();
  662. prefix_push(func);
  663. test_print("Running %s tests...\n", func);
  664. reset_memblock_attributes();
  665. dummy_physical_memory_init();
  666. alloc_simple_check();
  667. alloc_disjoint_check();
  668. alloc_before_check();
  669. alloc_after_check();
  670. alloc_second_fit_check();
  671. alloc_small_gaps_check();
  672. alloc_in_between_check();
  673. alloc_all_reserved_check();
  674. alloc_no_space_check();
  675. alloc_limited_space_check();
  676. alloc_no_memory_check();
  677. alloc_too_large_check();
  678. dummy_physical_memory_cleanup();
  679. prefix_pop();
  680. return 0;
  681. }
  682. int memblock_alloc_checks(void)
  683. {
  684. memblock_alloc_checks_internal(TEST_F_NONE);
  685. memblock_alloc_checks_internal(TEST_F_RAW);
  686. return 0;
  687. }