alloc_exact_nid_api.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #include "alloc_exact_nid_api.h"
  3. #include "alloc_nid_api.h"
  4. #define FUNC_NAME "memblock_alloc_exact_nid_raw"
  5. /*
  6. * contains the fraction of MEM_SIZE contained in each node in basis point
  7. * units (one hundredth of 1% or 1/10000)
  8. */
  9. static const unsigned int node_fractions[] = {
  10. 2500, /* 1/4 */
  11. 625, /* 1/16 */
  12. 1250, /* 1/8 */
  13. 1250, /* 1/8 */
  14. 625, /* 1/16 */
  15. 625, /* 1/16 */
  16. 2500, /* 1/4 */
  17. 625, /* 1/16 */
  18. };
  19. /*
  20. * A test that tries to allocate a memory region in a specific NUMA node that
  21. * has enough memory to allocate a region of the requested size.
  22. * Expect to allocate an aligned region at the end of the requested node.
  23. */
  24. static int alloc_exact_nid_top_down_numa_simple_check(void)
  25. {
  26. int nid_req = 3;
  27. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  28. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  29. void *allocated_ptr = NULL;
  30. phys_addr_t size;
  31. phys_addr_t min_addr;
  32. phys_addr_t max_addr;
  33. PREFIX_PUSH();
  34. setup_numa_memblock(node_fractions);
  35. ASSERT_LE(SZ_4, req_node->size);
  36. size = req_node->size / SZ_4;
  37. min_addr = memblock_start_of_DRAM();
  38. max_addr = memblock_end_of_DRAM();
  39. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  40. min_addr, max_addr,
  41. nid_req);
  42. ASSERT_NE(allocated_ptr, NULL);
  43. ASSERT_MEM_NE(allocated_ptr, 0, size);
  44. ASSERT_EQ(new_rgn->size, size);
  45. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  46. ASSERT_LE(req_node->base, new_rgn->base);
  47. ASSERT_EQ(memblock.reserved.cnt, 1);
  48. ASSERT_EQ(memblock.reserved.total_size, size);
  49. test_pass_pop();
  50. return 0;
  51. }
  52. /*
  53. * A test that tries to allocate a memory region in a specific NUMA node that
  54. * is partially reserved but has enough memory for the allocated region:
  55. *
  56. * | +---------------------------------------+ |
  57. * | | requested | |
  58. * +-----------+---------------------------------------+----------+
  59. *
  60. * | +------------------+ +-----+ |
  61. * | | reserved | | new | |
  62. * +-----------+------------------+--------------+-----+----------+
  63. *
  64. * Expect to allocate an aligned region at the end of the requested node. The
  65. * region count and total size get updated.
  66. */
  67. static int alloc_exact_nid_top_down_numa_part_reserved_check(void)
  68. {
  69. int nid_req = 4;
  70. struct memblock_region *new_rgn = &memblock.reserved.regions[1];
  71. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  72. void *allocated_ptr = NULL;
  73. struct region r1;
  74. phys_addr_t size;
  75. phys_addr_t min_addr;
  76. phys_addr_t max_addr;
  77. PREFIX_PUSH();
  78. setup_numa_memblock(node_fractions);
  79. ASSERT_LE(SZ_8, req_node->size);
  80. r1.base = req_node->base;
  81. r1.size = req_node->size / SZ_2;
  82. size = r1.size / SZ_4;
  83. min_addr = memblock_start_of_DRAM();
  84. max_addr = memblock_end_of_DRAM();
  85. memblock_reserve(r1.base, r1.size);
  86. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  87. min_addr, max_addr,
  88. nid_req);
  89. ASSERT_NE(allocated_ptr, NULL);
  90. ASSERT_MEM_NE(allocated_ptr, 0, size);
  91. ASSERT_EQ(new_rgn->size, size);
  92. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  93. ASSERT_LE(req_node->base, new_rgn->base);
  94. ASSERT_EQ(memblock.reserved.cnt, 2);
  95. ASSERT_EQ(memblock.reserved.total_size, size + r1.size);
  96. test_pass_pop();
  97. return 0;
  98. }
  99. /*
  100. * A test that tries to allocate a memory region that spans over the min_addr
  101. * and max_addr range and overlaps with two different nodes, where the first
  102. * node is the requested node:
  103. *
  104. * min_addr
  105. * | max_addr
  106. * | |
  107. * v v
  108. * | +-----------------------+-----------+ |
  109. * | | requested | node3 | |
  110. * +-----------+-----------------------+-----------+--------------+
  111. * + +
  112. * | +-----------+ |
  113. * | | rgn | |
  114. * +-----------------------+-----------+--------------------------+
  115. *
  116. * Expect to drop the lower limit and allocate a memory region that ends at
  117. * the end of the requested node.
  118. */
  119. static int alloc_exact_nid_top_down_numa_split_range_low_check(void)
  120. {
  121. int nid_req = 2;
  122. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  123. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  124. void *allocated_ptr = NULL;
  125. phys_addr_t size = SZ_512;
  126. phys_addr_t min_addr;
  127. phys_addr_t max_addr;
  128. phys_addr_t req_node_end;
  129. PREFIX_PUSH();
  130. setup_numa_memblock(node_fractions);
  131. req_node_end = region_end(req_node);
  132. min_addr = req_node_end - SZ_256;
  133. max_addr = min_addr + size;
  134. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  135. min_addr, max_addr,
  136. nid_req);
  137. ASSERT_NE(allocated_ptr, NULL);
  138. ASSERT_MEM_NE(allocated_ptr, 0, size);
  139. ASSERT_EQ(new_rgn->size, size);
  140. ASSERT_EQ(new_rgn->base, req_node_end - size);
  141. ASSERT_LE(req_node->base, new_rgn->base);
  142. ASSERT_EQ(memblock.reserved.cnt, 1);
  143. ASSERT_EQ(memblock.reserved.total_size, size);
  144. test_pass_pop();
  145. return 0;
  146. }
  147. /*
  148. * A test that tries to allocate a memory region that spans over the min_addr
  149. * and max_addr range and overlaps with two different nodes, where the requested
  150. * node ends before min_addr:
  151. *
  152. * min_addr
  153. * | max_addr
  154. * | |
  155. * v v
  156. * | +---------------+ +-------------+---------+ |
  157. * | | requested | | node1 | node2 | |
  158. * +----+---------------+--------+-------------+---------+----------+
  159. * + +
  160. * | +---------+ |
  161. * | | rgn | |
  162. * +----------+---------+-------------------------------------------+
  163. *
  164. * Expect to drop the lower limit and allocate a memory region that ends at
  165. * the end of the requested node.
  166. */
  167. static int alloc_exact_nid_top_down_numa_no_overlap_split_check(void)
  168. {
  169. int nid_req = 2;
  170. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  171. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  172. struct memblock_region *node2 = &memblock.memory.regions[6];
  173. void *allocated_ptr = NULL;
  174. phys_addr_t size;
  175. phys_addr_t min_addr;
  176. phys_addr_t max_addr;
  177. PREFIX_PUSH();
  178. setup_numa_memblock(node_fractions);
  179. size = SZ_512;
  180. min_addr = node2->base - SZ_256;
  181. max_addr = min_addr + size;
  182. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  183. min_addr, max_addr,
  184. nid_req);
  185. ASSERT_NE(allocated_ptr, NULL);
  186. ASSERT_MEM_NE(allocated_ptr, 0, size);
  187. ASSERT_EQ(new_rgn->size, size);
  188. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  189. ASSERT_LE(req_node->base, new_rgn->base);
  190. ASSERT_EQ(memblock.reserved.cnt, 1);
  191. ASSERT_EQ(memblock.reserved.total_size, size);
  192. test_pass_pop();
  193. return 0;
  194. }
  195. /*
  196. * A test that tries to allocate memory within min_addr and max_add range when
  197. * the requested node and the range do not overlap, and requested node ends
  198. * before min_addr. The range overlaps with multiple nodes along node
  199. * boundaries:
  200. *
  201. * min_addr
  202. * | max_addr
  203. * | |
  204. * v v
  205. * |-----------+ +----------+----...----+----------+ |
  206. * | requested | | min node | ... | max node | |
  207. * +-----------+-----------+----------+----...----+----------+------+
  208. * + +
  209. * | +-----+ |
  210. * | | rgn | |
  211. * +-----+-----+----------------------------------------------------+
  212. *
  213. * Expect to drop the lower limit and allocate a memory region that ends at
  214. * the end of the requested node.
  215. */
  216. static int alloc_exact_nid_top_down_numa_no_overlap_low_check(void)
  217. {
  218. int nid_req = 0;
  219. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  220. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  221. struct memblock_region *min_node = &memblock.memory.regions[2];
  222. struct memblock_region *max_node = &memblock.memory.regions[5];
  223. void *allocated_ptr = NULL;
  224. phys_addr_t size = SZ_64;
  225. phys_addr_t max_addr;
  226. phys_addr_t min_addr;
  227. PREFIX_PUSH();
  228. setup_numa_memblock(node_fractions);
  229. min_addr = min_node->base;
  230. max_addr = region_end(max_node);
  231. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  232. min_addr, max_addr,
  233. nid_req);
  234. ASSERT_NE(allocated_ptr, NULL);
  235. ASSERT_MEM_NE(allocated_ptr, 0, size);
  236. ASSERT_EQ(new_rgn->size, size);
  237. ASSERT_EQ(new_rgn->base, region_end(req_node) - size);
  238. ASSERT_EQ(memblock.reserved.cnt, 1);
  239. ASSERT_EQ(memblock.reserved.total_size, size);
  240. test_pass_pop();
  241. return 0;
  242. }
  243. /*
  244. * A test that tries to allocate a memory region in a specific NUMA node that
  245. * has enough memory to allocate a region of the requested size.
  246. * Expect to allocate an aligned region at the beginning of the requested node.
  247. */
  248. static int alloc_exact_nid_bottom_up_numa_simple_check(void)
  249. {
  250. int nid_req = 3;
  251. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  252. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  253. void *allocated_ptr = NULL;
  254. phys_addr_t size;
  255. phys_addr_t min_addr;
  256. phys_addr_t max_addr;
  257. PREFIX_PUSH();
  258. setup_numa_memblock(node_fractions);
  259. ASSERT_LE(SZ_4, req_node->size);
  260. size = req_node->size / SZ_4;
  261. min_addr = memblock_start_of_DRAM();
  262. max_addr = memblock_end_of_DRAM();
  263. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  264. min_addr, max_addr,
  265. nid_req);
  266. ASSERT_NE(allocated_ptr, NULL);
  267. ASSERT_MEM_NE(allocated_ptr, 0, size);
  268. ASSERT_EQ(new_rgn->size, size);
  269. ASSERT_EQ(new_rgn->base, req_node->base);
  270. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  271. ASSERT_EQ(memblock.reserved.cnt, 1);
  272. ASSERT_EQ(memblock.reserved.total_size, size);
  273. test_pass_pop();
  274. return 0;
  275. }
  276. /*
  277. * A test that tries to allocate a memory region in a specific NUMA node that
  278. * is partially reserved but has enough memory for the allocated region:
  279. *
  280. * | +---------------------------------------+ |
  281. * | | requested | |
  282. * +-----------+---------------------------------------+---------+
  283. *
  284. * | +------------------+-----+ |
  285. * | | reserved | new | |
  286. * +-----------+------------------+-----+------------------------+
  287. *
  288. * Expect to allocate an aligned region in the requested node that merges with
  289. * the existing reserved region. The total size gets updated.
  290. */
  291. static int alloc_exact_nid_bottom_up_numa_part_reserved_check(void)
  292. {
  293. int nid_req = 4;
  294. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  295. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  296. void *allocated_ptr = NULL;
  297. struct region r1;
  298. phys_addr_t size;
  299. phys_addr_t min_addr;
  300. phys_addr_t max_addr;
  301. phys_addr_t total_size;
  302. PREFIX_PUSH();
  303. setup_numa_memblock(node_fractions);
  304. ASSERT_LE(SZ_8, req_node->size);
  305. r1.base = req_node->base;
  306. r1.size = req_node->size / SZ_2;
  307. size = r1.size / SZ_4;
  308. min_addr = memblock_start_of_DRAM();
  309. max_addr = memblock_end_of_DRAM();
  310. total_size = size + r1.size;
  311. memblock_reserve(r1.base, r1.size);
  312. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  313. min_addr, max_addr,
  314. nid_req);
  315. ASSERT_NE(allocated_ptr, NULL);
  316. ASSERT_MEM_NE(allocated_ptr, 0, size);
  317. ASSERT_EQ(new_rgn->size, total_size);
  318. ASSERT_EQ(new_rgn->base, req_node->base);
  319. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  320. ASSERT_EQ(memblock.reserved.cnt, 1);
  321. ASSERT_EQ(memblock.reserved.total_size, total_size);
  322. test_pass_pop();
  323. return 0;
  324. }
  325. /*
  326. * A test that tries to allocate a memory region that spans over the min_addr
  327. * and max_addr range and overlaps with two different nodes, where the first
  328. * node is the requested node:
  329. *
  330. * min_addr
  331. * | max_addr
  332. * | |
  333. * v v
  334. * | +-----------------------+-----------+ |
  335. * | | requested | node3 | |
  336. * +-----------+-----------------------+-----------+--------------+
  337. * + +
  338. * | +-----------+ |
  339. * | | rgn | |
  340. * +-----------+-----------+--------------------------------------+
  341. *
  342. * Expect to drop the lower limit and allocate a memory region at the beginning
  343. * of the requested node.
  344. */
  345. static int alloc_exact_nid_bottom_up_numa_split_range_low_check(void)
  346. {
  347. int nid_req = 2;
  348. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  349. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  350. void *allocated_ptr = NULL;
  351. phys_addr_t size = SZ_512;
  352. phys_addr_t min_addr;
  353. phys_addr_t max_addr;
  354. phys_addr_t req_node_end;
  355. PREFIX_PUSH();
  356. setup_numa_memblock(node_fractions);
  357. req_node_end = region_end(req_node);
  358. min_addr = req_node_end - SZ_256;
  359. max_addr = min_addr + size;
  360. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  361. min_addr, max_addr,
  362. nid_req);
  363. ASSERT_NE(allocated_ptr, NULL);
  364. ASSERT_MEM_NE(allocated_ptr, 0, size);
  365. ASSERT_EQ(new_rgn->size, size);
  366. ASSERT_EQ(new_rgn->base, req_node->base);
  367. ASSERT_LE(region_end(new_rgn), req_node_end);
  368. ASSERT_EQ(memblock.reserved.cnt, 1);
  369. ASSERT_EQ(memblock.reserved.total_size, size);
  370. test_pass_pop();
  371. return 0;
  372. }
  373. /*
  374. * A test that tries to allocate a memory region that spans over the min_addr
  375. * and max_addr range and overlaps with two different nodes, where the requested
  376. * node ends before min_addr:
  377. *
  378. * min_addr
  379. * | max_addr
  380. * | |
  381. * v v
  382. * | +---------------+ +-------------+---------+ |
  383. * | | requested | | node1 | node2 | |
  384. * +----+---------------+--------+-------------+---------+---------+
  385. * + +
  386. * | +---------+ |
  387. * | | rgn | |
  388. * +----+---------+------------------------------------------------+
  389. *
  390. * Expect to drop the lower limit and allocate a memory region that starts at
  391. * the beginning of the requested node.
  392. */
  393. static int alloc_exact_nid_bottom_up_numa_no_overlap_split_check(void)
  394. {
  395. int nid_req = 2;
  396. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  397. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  398. struct memblock_region *node2 = &memblock.memory.regions[6];
  399. void *allocated_ptr = NULL;
  400. phys_addr_t size;
  401. phys_addr_t min_addr;
  402. phys_addr_t max_addr;
  403. PREFIX_PUSH();
  404. setup_numa_memblock(node_fractions);
  405. size = SZ_512;
  406. min_addr = node2->base - SZ_256;
  407. max_addr = min_addr + size;
  408. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  409. min_addr, max_addr,
  410. nid_req);
  411. ASSERT_NE(allocated_ptr, NULL);
  412. ASSERT_MEM_NE(allocated_ptr, 0, size);
  413. ASSERT_EQ(new_rgn->size, size);
  414. ASSERT_EQ(new_rgn->base, req_node->base);
  415. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  416. ASSERT_EQ(memblock.reserved.cnt, 1);
  417. ASSERT_EQ(memblock.reserved.total_size, size);
  418. test_pass_pop();
  419. return 0;
  420. }
  421. /*
  422. * A test that tries to allocate memory within min_addr and max_add range when
  423. * the requested node and the range do not overlap, and requested node ends
  424. * before min_addr. The range overlaps with multiple nodes along node
  425. * boundaries:
  426. *
  427. * min_addr
  428. * | max_addr
  429. * | |
  430. * v v
  431. * |-----------+ +----------+----...----+----------+ |
  432. * | requested | | min node | ... | max node | |
  433. * +-----------+-----------+----------+----...----+----------+------+
  434. * + +
  435. * |-----+ |
  436. * | rgn | |
  437. * +-----+----------------------------------------------------------+
  438. *
  439. * Expect to drop the lower limit and allocate a memory region that starts at
  440. * the beginning of the requested node.
  441. */
  442. static int alloc_exact_nid_bottom_up_numa_no_overlap_low_check(void)
  443. {
  444. int nid_req = 0;
  445. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  446. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  447. struct memblock_region *min_node = &memblock.memory.regions[2];
  448. struct memblock_region *max_node = &memblock.memory.regions[5];
  449. void *allocated_ptr = NULL;
  450. phys_addr_t size = SZ_64;
  451. phys_addr_t max_addr;
  452. phys_addr_t min_addr;
  453. PREFIX_PUSH();
  454. setup_numa_memblock(node_fractions);
  455. min_addr = min_node->base;
  456. max_addr = region_end(max_node);
  457. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  458. min_addr, max_addr,
  459. nid_req);
  460. ASSERT_NE(allocated_ptr, NULL);
  461. ASSERT_MEM_NE(allocated_ptr, 0, size);
  462. ASSERT_EQ(new_rgn->size, size);
  463. ASSERT_EQ(new_rgn->base, req_node->base);
  464. ASSERT_LE(region_end(new_rgn), region_end(req_node));
  465. ASSERT_EQ(memblock.reserved.cnt, 1);
  466. ASSERT_EQ(memblock.reserved.total_size, size);
  467. test_pass_pop();
  468. return 0;
  469. }
  470. /*
  471. * A test that tries to allocate a memory region in a specific NUMA node that
  472. * does not have enough memory to allocate a region of the requested size:
  473. *
  474. * | +-----+ |
  475. * | | req | |
  476. * +---+-----+----------------------------+
  477. *
  478. * +---------+
  479. * | rgn |
  480. * +---------+
  481. *
  482. * Expect no allocation to happen.
  483. */
  484. static int alloc_exact_nid_numa_small_node_generic_check(void)
  485. {
  486. int nid_req = 1;
  487. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  488. void *allocated_ptr = NULL;
  489. phys_addr_t size;
  490. phys_addr_t min_addr;
  491. phys_addr_t max_addr;
  492. PREFIX_PUSH();
  493. setup_numa_memblock(node_fractions);
  494. size = SZ_2 * req_node->size;
  495. min_addr = memblock_start_of_DRAM();
  496. max_addr = memblock_end_of_DRAM();
  497. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  498. min_addr, max_addr,
  499. nid_req);
  500. ASSERT_EQ(allocated_ptr, NULL);
  501. test_pass_pop();
  502. return 0;
  503. }
  504. /*
  505. * A test that tries to allocate a memory region in a specific NUMA node that
  506. * is fully reserved:
  507. *
  508. * | +---------+ |
  509. * | |requested| |
  510. * +--------------+---------+-------------+
  511. *
  512. * | +---------+ |
  513. * | | reserved| |
  514. * +--------------+---------+-------------+
  515. *
  516. * Expect no allocation to happen.
  517. */
  518. static int alloc_exact_nid_numa_node_reserved_generic_check(void)
  519. {
  520. int nid_req = 2;
  521. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  522. void *allocated_ptr = NULL;
  523. phys_addr_t size;
  524. phys_addr_t min_addr;
  525. phys_addr_t max_addr;
  526. PREFIX_PUSH();
  527. setup_numa_memblock(node_fractions);
  528. size = req_node->size;
  529. min_addr = memblock_start_of_DRAM();
  530. max_addr = memblock_end_of_DRAM();
  531. memblock_reserve(req_node->base, req_node->size);
  532. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  533. min_addr, max_addr,
  534. nid_req);
  535. ASSERT_EQ(allocated_ptr, NULL);
  536. test_pass_pop();
  537. return 0;
  538. }
  539. /*
  540. * A test that tries to allocate a memory region in a specific NUMA node that
  541. * is partially reserved and does not have enough contiguous memory for the
  542. * allocated region:
  543. *
  544. * | +-----------------------+ |
  545. * | | requested | |
  546. * +-----------+-----------------------+----+
  547. *
  548. * | +----------+ |
  549. * | | reserved | |
  550. * +-----------------+----------+-----------+
  551. *
  552. * Expect no allocation to happen.
  553. */
  554. static int alloc_exact_nid_numa_part_reserved_fail_generic_check(void)
  555. {
  556. int nid_req = 4;
  557. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  558. void *allocated_ptr = NULL;
  559. struct region r1;
  560. phys_addr_t size;
  561. phys_addr_t min_addr;
  562. phys_addr_t max_addr;
  563. PREFIX_PUSH();
  564. setup_numa_memblock(node_fractions);
  565. ASSERT_LE(SZ_4, req_node->size);
  566. size = req_node->size / SZ_2;
  567. r1.base = req_node->base + (size / SZ_2);
  568. r1.size = size;
  569. min_addr = memblock_start_of_DRAM();
  570. max_addr = memblock_end_of_DRAM();
  571. memblock_reserve(r1.base, r1.size);
  572. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  573. min_addr, max_addr,
  574. nid_req);
  575. ASSERT_EQ(allocated_ptr, NULL);
  576. test_pass_pop();
  577. return 0;
  578. }
  579. /*
  580. * A test that tries to allocate a memory region that spans over the min_addr
  581. * and max_addr range and overlaps with two different nodes, where the second
  582. * node is the requested node:
  583. *
  584. * min_addr
  585. * | max_addr
  586. * | |
  587. * v v
  588. * | +--------------------------+---------+ |
  589. * | | first node |requested| |
  590. * +------+--------------------------+---------+----------------+
  591. *
  592. * Expect no allocation to happen.
  593. */
  594. static int alloc_exact_nid_numa_split_range_high_generic_check(void)
  595. {
  596. int nid_req = 3;
  597. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  598. void *allocated_ptr = NULL;
  599. phys_addr_t size = SZ_512;
  600. phys_addr_t min_addr;
  601. phys_addr_t max_addr;
  602. PREFIX_PUSH();
  603. setup_numa_memblock(node_fractions);
  604. min_addr = req_node->base - SZ_256;
  605. max_addr = min_addr + size;
  606. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  607. min_addr, max_addr,
  608. nid_req);
  609. ASSERT_EQ(allocated_ptr, NULL);
  610. test_pass_pop();
  611. return 0;
  612. }
  613. /*
  614. * A test that tries to allocate memory within min_addr and max_add range when
  615. * the requested node and the range do not overlap, and requested node starts
  616. * after max_addr. The range overlaps with multiple nodes along node
  617. * boundaries:
  618. *
  619. * min_addr
  620. * | max_addr
  621. * | |
  622. * v v
  623. * | +----------+----...----+----------+ +-----------+ |
  624. * | | min node | ... | max node | | requested | |
  625. * +-----+----------+----...----+----------+--------+-----------+---+
  626. *
  627. * Expect no allocation to happen.
  628. */
  629. static int alloc_exact_nid_numa_no_overlap_high_generic_check(void)
  630. {
  631. int nid_req = 7;
  632. struct memblock_region *min_node = &memblock.memory.regions[2];
  633. struct memblock_region *max_node = &memblock.memory.regions[5];
  634. void *allocated_ptr = NULL;
  635. phys_addr_t size = SZ_64;
  636. phys_addr_t max_addr;
  637. phys_addr_t min_addr;
  638. PREFIX_PUSH();
  639. setup_numa_memblock(node_fractions);
  640. min_addr = min_node->base;
  641. max_addr = region_end(max_node);
  642. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  643. min_addr, max_addr,
  644. nid_req);
  645. ASSERT_EQ(allocated_ptr, NULL);
  646. test_pass_pop();
  647. return 0;
  648. }
  649. /*
  650. * A test that tries to allocate a memory region in a specific NUMA node that
  651. * does not have enough memory to allocate a region of the requested size.
  652. * Additionally, none of the nodes have enough memory to allocate the region:
  653. *
  654. * +-----------------------------------+
  655. * | new |
  656. * +-----------------------------------+
  657. * |-------+-------+-------+-------+-------+-------+-------+-------|
  658. * | node0 | node1 | node2 | node3 | node4 | node5 | node6 | node7 |
  659. * +-------+-------+-------+-------+-------+-------+-------+-------+
  660. *
  661. * Expect no allocation to happen.
  662. */
  663. static int alloc_exact_nid_numa_large_region_generic_check(void)
  664. {
  665. int nid_req = 3;
  666. void *allocated_ptr = NULL;
  667. phys_addr_t size = MEM_SIZE / SZ_2;
  668. phys_addr_t min_addr;
  669. phys_addr_t max_addr;
  670. PREFIX_PUSH();
  671. setup_numa_memblock(node_fractions);
  672. min_addr = memblock_start_of_DRAM();
  673. max_addr = memblock_end_of_DRAM();
  674. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  675. min_addr, max_addr,
  676. nid_req);
  677. ASSERT_EQ(allocated_ptr, NULL);
  678. test_pass_pop();
  679. return 0;
  680. }
  681. /*
  682. * A test that tries to allocate memory within min_addr and max_addr range when
  683. * there are two reserved regions at the borders. The requested node starts at
  684. * min_addr and ends at max_addr and is the same size as the region to be
  685. * allocated:
  686. *
  687. * min_addr
  688. * | max_addr
  689. * | |
  690. * v v
  691. * | +-----------+-----------------------+-----------------------|
  692. * | | node5 | requested | node7 |
  693. * +------+-----------+-----------------------+-----------------------+
  694. * + +
  695. * | +----+-----------------------+----+ |
  696. * | | r2 | new | r1 | |
  697. * +-------------+----+-----------------------+----+------------------+
  698. *
  699. * Expect to merge all of the regions into one. The region counter and total
  700. * size fields get updated.
  701. */
  702. static int alloc_exact_nid_numa_reserved_full_merge_generic_check(void)
  703. {
  704. int nid_req = 6;
  705. int nid_next = nid_req + 1;
  706. struct memblock_region *new_rgn = &memblock.reserved.regions[0];
  707. struct memblock_region *req_node = &memblock.memory.regions[nid_req];
  708. struct memblock_region *next_node = &memblock.memory.regions[nid_next];
  709. void *allocated_ptr = NULL;
  710. struct region r1, r2;
  711. phys_addr_t size = req_node->size;
  712. phys_addr_t total_size;
  713. phys_addr_t max_addr;
  714. phys_addr_t min_addr;
  715. PREFIX_PUSH();
  716. setup_numa_memblock(node_fractions);
  717. r1.base = next_node->base;
  718. r1.size = SZ_128;
  719. r2.size = SZ_128;
  720. r2.base = r1.base - (size + r2.size);
  721. total_size = r1.size + r2.size + size;
  722. min_addr = r2.base + r2.size;
  723. max_addr = r1.base;
  724. memblock_reserve(r1.base, r1.size);
  725. memblock_reserve(r2.base, r2.size);
  726. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  727. min_addr, max_addr,
  728. nid_req);
  729. ASSERT_NE(allocated_ptr, NULL);
  730. ASSERT_MEM_NE(allocated_ptr, 0, size);
  731. ASSERT_EQ(new_rgn->size, total_size);
  732. ASSERT_EQ(new_rgn->base, r2.base);
  733. ASSERT_LE(new_rgn->base, req_node->base);
  734. ASSERT_LE(region_end(req_node), region_end(new_rgn));
  735. ASSERT_EQ(memblock.reserved.cnt, 1);
  736. ASSERT_EQ(memblock.reserved.total_size, total_size);
  737. test_pass_pop();
  738. return 0;
  739. }
  740. /*
  741. * A test that tries to allocate memory within min_addr and max_add range,
  742. * where the total range can fit the region, but it is split between two nodes
  743. * and everything else is reserved. Additionally, nid is set to NUMA_NO_NODE
  744. * instead of requesting a specific node:
  745. *
  746. * +-----------+
  747. * | new |
  748. * +-----------+
  749. * | +---------------------+-----------|
  750. * | | prev node | next node |
  751. * +------+---------------------+-----------+
  752. * + +
  753. * |----------------------+ +-----|
  754. * | r1 | | r2 |
  755. * +----------------------+-----------+-----+
  756. * ^ ^
  757. * | |
  758. * | max_addr
  759. * |
  760. * min_addr
  761. *
  762. * Expect no allocation to happen.
  763. */
  764. static int alloc_exact_nid_numa_split_all_reserved_generic_check(void)
  765. {
  766. void *allocated_ptr = NULL;
  767. struct memblock_region *next_node = &memblock.memory.regions[7];
  768. struct region r1, r2;
  769. phys_addr_t size = SZ_256;
  770. phys_addr_t max_addr;
  771. phys_addr_t min_addr;
  772. PREFIX_PUSH();
  773. setup_numa_memblock(node_fractions);
  774. r2.base = next_node->base + SZ_128;
  775. r2.size = memblock_end_of_DRAM() - r2.base;
  776. r1.size = MEM_SIZE - (r2.size + size);
  777. r1.base = memblock_start_of_DRAM();
  778. min_addr = r1.base + r1.size;
  779. max_addr = r2.base;
  780. memblock_reserve(r1.base, r1.size);
  781. memblock_reserve(r2.base, r2.size);
  782. allocated_ptr = memblock_alloc_exact_nid_raw(size, SMP_CACHE_BYTES,
  783. min_addr, max_addr,
  784. NUMA_NO_NODE);
  785. ASSERT_EQ(allocated_ptr, NULL);
  786. test_pass_pop();
  787. return 0;
  788. }
  789. /* Test case wrappers for NUMA tests */
  790. static int alloc_exact_nid_numa_simple_check(void)
  791. {
  792. test_print("\tRunning %s...\n", __func__);
  793. memblock_set_bottom_up(false);
  794. alloc_exact_nid_top_down_numa_simple_check();
  795. memblock_set_bottom_up(true);
  796. alloc_exact_nid_bottom_up_numa_simple_check();
  797. return 0;
  798. }
  799. static int alloc_exact_nid_numa_part_reserved_check(void)
  800. {
  801. test_print("\tRunning %s...\n", __func__);
  802. memblock_set_bottom_up(false);
  803. alloc_exact_nid_top_down_numa_part_reserved_check();
  804. memblock_set_bottom_up(true);
  805. alloc_exact_nid_bottom_up_numa_part_reserved_check();
  806. return 0;
  807. }
  808. static int alloc_exact_nid_numa_split_range_low_check(void)
  809. {
  810. test_print("\tRunning %s...\n", __func__);
  811. memblock_set_bottom_up(false);
  812. alloc_exact_nid_top_down_numa_split_range_low_check();
  813. memblock_set_bottom_up(true);
  814. alloc_exact_nid_bottom_up_numa_split_range_low_check();
  815. return 0;
  816. }
  817. static int alloc_exact_nid_numa_no_overlap_split_check(void)
  818. {
  819. test_print("\tRunning %s...\n", __func__);
  820. memblock_set_bottom_up(false);
  821. alloc_exact_nid_top_down_numa_no_overlap_split_check();
  822. memblock_set_bottom_up(true);
  823. alloc_exact_nid_bottom_up_numa_no_overlap_split_check();
  824. return 0;
  825. }
  826. static int alloc_exact_nid_numa_no_overlap_low_check(void)
  827. {
  828. test_print("\tRunning %s...\n", __func__);
  829. memblock_set_bottom_up(false);
  830. alloc_exact_nid_top_down_numa_no_overlap_low_check();
  831. memblock_set_bottom_up(true);
  832. alloc_exact_nid_bottom_up_numa_no_overlap_low_check();
  833. return 0;
  834. }
  835. static int alloc_exact_nid_numa_small_node_check(void)
  836. {
  837. test_print("\tRunning %s...\n", __func__);
  838. run_top_down(alloc_exact_nid_numa_small_node_generic_check);
  839. run_bottom_up(alloc_exact_nid_numa_small_node_generic_check);
  840. return 0;
  841. }
  842. static int alloc_exact_nid_numa_node_reserved_check(void)
  843. {
  844. test_print("\tRunning %s...\n", __func__);
  845. run_top_down(alloc_exact_nid_numa_node_reserved_generic_check);
  846. run_bottom_up(alloc_exact_nid_numa_node_reserved_generic_check);
  847. return 0;
  848. }
  849. static int alloc_exact_nid_numa_part_reserved_fail_check(void)
  850. {
  851. test_print("\tRunning %s...\n", __func__);
  852. run_top_down(alloc_exact_nid_numa_part_reserved_fail_generic_check);
  853. run_bottom_up(alloc_exact_nid_numa_part_reserved_fail_generic_check);
  854. return 0;
  855. }
  856. static int alloc_exact_nid_numa_split_range_high_check(void)
  857. {
  858. test_print("\tRunning %s...\n", __func__);
  859. run_top_down(alloc_exact_nid_numa_split_range_high_generic_check);
  860. run_bottom_up(alloc_exact_nid_numa_split_range_high_generic_check);
  861. return 0;
  862. }
  863. static int alloc_exact_nid_numa_no_overlap_high_check(void)
  864. {
  865. test_print("\tRunning %s...\n", __func__);
  866. run_top_down(alloc_exact_nid_numa_no_overlap_high_generic_check);
  867. run_bottom_up(alloc_exact_nid_numa_no_overlap_high_generic_check);
  868. return 0;
  869. }
  870. static int alloc_exact_nid_numa_large_region_check(void)
  871. {
  872. test_print("\tRunning %s...\n", __func__);
  873. run_top_down(alloc_exact_nid_numa_large_region_generic_check);
  874. run_bottom_up(alloc_exact_nid_numa_large_region_generic_check);
  875. return 0;
  876. }
  877. static int alloc_exact_nid_numa_reserved_full_merge_check(void)
  878. {
  879. test_print("\tRunning %s...\n", __func__);
  880. run_top_down(alloc_exact_nid_numa_reserved_full_merge_generic_check);
  881. run_bottom_up(alloc_exact_nid_numa_reserved_full_merge_generic_check);
  882. return 0;
  883. }
  884. static int alloc_exact_nid_numa_split_all_reserved_check(void)
  885. {
  886. test_print("\tRunning %s...\n", __func__);
  887. run_top_down(alloc_exact_nid_numa_split_all_reserved_generic_check);
  888. run_bottom_up(alloc_exact_nid_numa_split_all_reserved_generic_check);
  889. return 0;
  890. }
  891. int __memblock_alloc_exact_nid_numa_checks(void)
  892. {
  893. test_print("Running %s NUMA tests...\n", FUNC_NAME);
  894. alloc_exact_nid_numa_simple_check();
  895. alloc_exact_nid_numa_part_reserved_check();
  896. alloc_exact_nid_numa_split_range_low_check();
  897. alloc_exact_nid_numa_no_overlap_split_check();
  898. alloc_exact_nid_numa_no_overlap_low_check();
  899. alloc_exact_nid_numa_small_node_check();
  900. alloc_exact_nid_numa_node_reserved_check();
  901. alloc_exact_nid_numa_part_reserved_fail_check();
  902. alloc_exact_nid_numa_split_range_high_check();
  903. alloc_exact_nid_numa_no_overlap_high_check();
  904. alloc_exact_nid_numa_large_region_check();
  905. alloc_exact_nid_numa_reserved_full_merge_check();
  906. alloc_exact_nid_numa_split_all_reserved_check();
  907. return 0;
  908. }
  909. int memblock_alloc_exact_nid_checks(void)
  910. {
  911. prefix_reset();
  912. prefix_push(FUNC_NAME);
  913. reset_memblock_attributes();
  914. dummy_physical_memory_init();
  915. memblock_alloc_exact_nid_range_checks();
  916. memblock_alloc_exact_nid_numa_checks();
  917. dummy_physical_memory_cleanup();
  918. prefix_pop();
  919. return 0;
  920. }