drm_buddy_test.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. * Copyright © 2022 Maíra Canal <mairacanal@riseup.net>
  5. */
  6. #include <kunit/test.h>
  7. #include <linux/prime_numbers.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/sizes.h>
  10. #include <drm/drm_buddy.h>
  11. #include "../lib/drm_random.h"
  12. static unsigned int random_seed;
  13. static inline u64 get_size(int order, u64 chunk_size)
  14. {
  15. return (1 << order) * chunk_size;
  16. }
  17. static void drm_test_buddy_fragmentation_performance(struct kunit *test)
  18. {
  19. struct drm_buddy_block *block, *tmp;
  20. int num_blocks, i, ret, count = 0;
  21. LIST_HEAD(allocated_blocks);
  22. unsigned long elapsed_ms;
  23. LIST_HEAD(reverse_list);
  24. LIST_HEAD(test_blocks);
  25. LIST_HEAD(clear_list);
  26. LIST_HEAD(dirty_list);
  27. LIST_HEAD(free_list);
  28. struct drm_buddy mm;
  29. u64 mm_size = SZ_4G;
  30. ktime_t start, end;
  31. /*
  32. * Allocation under severe fragmentation
  33. *
  34. * Create severe fragmentation by allocating the entire 4 GiB address space
  35. * as tiny 8 KiB blocks but forcing a 64 KiB alignment. The resulting pattern
  36. * leaves many scattered holes. Split the allocations into two groups and
  37. * return them with different flags to block coalescing, then repeatedly
  38. * allocate and free 64 KiB blocks while timing the loop. This stresses how
  39. * quickly the allocator can satisfy larger, aligned requests from a pool of
  40. * highly fragmented space.
  41. */
  42. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
  43. "buddy_init failed\n");
  44. num_blocks = mm_size / SZ_64K;
  45. start = ktime_get();
  46. /* Allocate with maximum fragmentation - 8K blocks with 64K alignment */
  47. for (i = 0; i < num_blocks; i++)
  48. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, SZ_8K, SZ_64K,
  49. &allocated_blocks, 0),
  50. "buddy_alloc hit an error size=%u\n", SZ_8K);
  51. list_for_each_entry_safe(block, tmp, &allocated_blocks, link) {
  52. if (count % 4 == 0 || count % 4 == 3)
  53. list_move_tail(&block->link, &clear_list);
  54. else
  55. list_move_tail(&block->link, &dirty_list);
  56. count++;
  57. }
  58. /* Free with different flags to ensure no coalescing */
  59. drm_buddy_free_list(&mm, &clear_list, DRM_BUDDY_CLEARED);
  60. drm_buddy_free_list(&mm, &dirty_list, 0);
  61. for (i = 0; i < num_blocks; i++)
  62. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, SZ_64K, SZ_64K,
  63. &test_blocks, 0),
  64. "buddy_alloc hit an error size=%u\n", SZ_64K);
  65. drm_buddy_free_list(&mm, &test_blocks, 0);
  66. end = ktime_get();
  67. elapsed_ms = ktime_to_ms(ktime_sub(end, start));
  68. kunit_info(test, "Fragmented allocation took %lu ms\n", elapsed_ms);
  69. drm_buddy_fini(&mm);
  70. /*
  71. * Reverse free order under fragmentation
  72. *
  73. * Construct a fragmented 4 GiB space by allocating every 8 KiB block with
  74. * 64 KiB alignment, creating a dense scatter of small regions. Half of the
  75. * blocks are selectively freed to form sparse gaps, while the remaining
  76. * allocations are preserved, reordered in reverse, and released back with
  77. * the cleared flag. This models a pathological reverse-ordered free pattern
  78. * and measures how quickly the allocator can merge and reclaim space when
  79. * deallocation occurs in the opposite order of allocation, exposing the
  80. * cost difference between a linear freelist scan and an ordered tree lookup.
  81. */
  82. ret = drm_buddy_init(&mm, mm_size, SZ_4K);
  83. KUNIT_ASSERT_EQ(test, ret, 0);
  84. start = ktime_get();
  85. /* Allocate maximum fragmentation */
  86. for (i = 0; i < num_blocks; i++)
  87. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, SZ_8K, SZ_64K,
  88. &allocated_blocks, 0),
  89. "buddy_alloc hit an error size=%u\n", SZ_8K);
  90. list_for_each_entry_safe(block, tmp, &allocated_blocks, link) {
  91. if (count % 2 == 0)
  92. list_move_tail(&block->link, &free_list);
  93. count++;
  94. }
  95. drm_buddy_free_list(&mm, &free_list, DRM_BUDDY_CLEARED);
  96. list_for_each_entry_safe_reverse(block, tmp, &allocated_blocks, link)
  97. list_move(&block->link, &reverse_list);
  98. drm_buddy_free_list(&mm, &reverse_list, DRM_BUDDY_CLEARED);
  99. end = ktime_get();
  100. elapsed_ms = ktime_to_ms(ktime_sub(end, start));
  101. kunit_info(test, "Reverse-ordered free took %lu ms\n", elapsed_ms);
  102. drm_buddy_fini(&mm);
  103. }
  104. static void drm_test_buddy_alloc_range_bias(struct kunit *test)
  105. {
  106. u32 mm_size, size, ps, bias_size, bias_start, bias_end, bias_rem;
  107. DRM_RND_STATE(prng, random_seed);
  108. unsigned int i, count, *order;
  109. struct drm_buddy_block *block;
  110. unsigned long flags;
  111. struct drm_buddy mm;
  112. LIST_HEAD(allocated);
  113. bias_size = SZ_1M;
  114. ps = roundup_pow_of_two(prandom_u32_state(&prng) % bias_size);
  115. ps = max(SZ_4K, ps);
  116. mm_size = (SZ_8M-1) & ~(ps-1); /* Multiple roots */
  117. kunit_info(test, "mm_size=%u, ps=%u\n", mm_size, ps);
  118. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
  119. "buddy_init failed\n");
  120. count = mm_size / bias_size;
  121. order = drm_random_order(count, &prng);
  122. KUNIT_EXPECT_TRUE(test, order);
  123. /*
  124. * Idea is to split the address space into uniform bias ranges, and then
  125. * in some random order allocate within each bias, using various
  126. * patterns within. This should detect if allocations leak out from a
  127. * given bias, for example.
  128. */
  129. for (i = 0; i < count; i++) {
  130. LIST_HEAD(tmp);
  131. u32 size;
  132. bias_start = order[i] * bias_size;
  133. bias_end = bias_start + bias_size;
  134. bias_rem = bias_size;
  135. /* internal round_up too big */
  136. KUNIT_ASSERT_TRUE_MSG(test,
  137. drm_buddy_alloc_blocks(&mm, bias_start,
  138. bias_end, bias_size + ps, bias_size,
  139. &allocated,
  140. DRM_BUDDY_RANGE_ALLOCATION),
  141. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  142. bias_start, bias_end, bias_size, bias_size);
  143. /* size too big */
  144. KUNIT_ASSERT_TRUE_MSG(test,
  145. drm_buddy_alloc_blocks(&mm, bias_start,
  146. bias_end, bias_size + ps, ps,
  147. &allocated,
  148. DRM_BUDDY_RANGE_ALLOCATION),
  149. "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
  150. bias_start, bias_end, bias_size + ps, ps);
  151. /* bias range too small for size */
  152. KUNIT_ASSERT_TRUE_MSG(test,
  153. drm_buddy_alloc_blocks(&mm, bias_start + ps,
  154. bias_end, bias_size, ps,
  155. &allocated,
  156. DRM_BUDDY_RANGE_ALLOCATION),
  157. "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
  158. bias_start + ps, bias_end, bias_size, ps);
  159. /* bias misaligned */
  160. KUNIT_ASSERT_TRUE_MSG(test,
  161. drm_buddy_alloc_blocks(&mm, bias_start + ps,
  162. bias_end - ps,
  163. bias_size >> 1, bias_size >> 1,
  164. &allocated,
  165. DRM_BUDDY_RANGE_ALLOCATION),
  166. "buddy_alloc h didn't fail with bias(%x-%x), size=%u, ps=%u\n",
  167. bias_start + ps, bias_end - ps, bias_size >> 1, bias_size >> 1);
  168. /* single big page */
  169. KUNIT_ASSERT_FALSE_MSG(test,
  170. drm_buddy_alloc_blocks(&mm, bias_start,
  171. bias_end, bias_size, bias_size,
  172. &tmp,
  173. DRM_BUDDY_RANGE_ALLOCATION),
  174. "buddy_alloc i failed with bias(%x-%x), size=%u, ps=%u\n",
  175. bias_start, bias_end, bias_size, bias_size);
  176. drm_buddy_free_list(&mm, &tmp, 0);
  177. /* single page with internal round_up */
  178. KUNIT_ASSERT_FALSE_MSG(test,
  179. drm_buddy_alloc_blocks(&mm, bias_start,
  180. bias_end, ps, bias_size,
  181. &tmp,
  182. DRM_BUDDY_RANGE_ALLOCATION),
  183. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  184. bias_start, bias_end, ps, bias_size);
  185. drm_buddy_free_list(&mm, &tmp, 0);
  186. /* random size within */
  187. size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
  188. if (size)
  189. KUNIT_ASSERT_FALSE_MSG(test,
  190. drm_buddy_alloc_blocks(&mm, bias_start,
  191. bias_end, size, ps,
  192. &tmp,
  193. DRM_BUDDY_RANGE_ALLOCATION),
  194. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  195. bias_start, bias_end, size, ps);
  196. bias_rem -= size;
  197. /* too big for current avail */
  198. KUNIT_ASSERT_TRUE_MSG(test,
  199. drm_buddy_alloc_blocks(&mm, bias_start,
  200. bias_end, bias_rem + ps, ps,
  201. &allocated,
  202. DRM_BUDDY_RANGE_ALLOCATION),
  203. "buddy_alloc didn't fail with bias(%x-%x), size=%u, ps=%u\n",
  204. bias_start, bias_end, bias_rem + ps, ps);
  205. if (bias_rem) {
  206. /* random fill of the remainder */
  207. size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
  208. size = max(size, ps);
  209. KUNIT_ASSERT_FALSE_MSG(test,
  210. drm_buddy_alloc_blocks(&mm, bias_start,
  211. bias_end, size, ps,
  212. &allocated,
  213. DRM_BUDDY_RANGE_ALLOCATION),
  214. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  215. bias_start, bias_end, size, ps);
  216. /*
  217. * Intentionally allow some space to be left
  218. * unallocated, and ideally not always on the bias
  219. * boundaries.
  220. */
  221. drm_buddy_free_list(&mm, &tmp, 0);
  222. } else {
  223. list_splice_tail(&tmp, &allocated);
  224. }
  225. }
  226. kfree(order);
  227. drm_buddy_free_list(&mm, &allocated, 0);
  228. drm_buddy_fini(&mm);
  229. /*
  230. * Something more free-form. Idea is to pick a random starting bias
  231. * range within the address space and then start filling it up. Also
  232. * randomly grow the bias range in both directions as we go along. This
  233. * should give us bias start/end which is not always uniform like above,
  234. * and in some cases will require the allocator to jump over already
  235. * allocated nodes in the middle of the address space.
  236. */
  237. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
  238. "buddy_init failed\n");
  239. bias_start = round_up(prandom_u32_state(&prng) % (mm_size - ps), ps);
  240. bias_end = round_up(bias_start + prandom_u32_state(&prng) % (mm_size - bias_start), ps);
  241. bias_end = max(bias_end, bias_start + ps);
  242. bias_rem = bias_end - bias_start;
  243. do {
  244. u32 size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
  245. KUNIT_ASSERT_FALSE_MSG(test,
  246. drm_buddy_alloc_blocks(&mm, bias_start,
  247. bias_end, size, ps,
  248. &allocated,
  249. DRM_BUDDY_RANGE_ALLOCATION),
  250. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  251. bias_start, bias_end, size, ps);
  252. bias_rem -= size;
  253. /*
  254. * Try to randomly grow the bias range in both directions, or
  255. * only one, or perhaps don't grow at all.
  256. */
  257. do {
  258. u32 old_bias_start = bias_start;
  259. u32 old_bias_end = bias_end;
  260. if (bias_start)
  261. bias_start -= round_up(prandom_u32_state(&prng) % bias_start, ps);
  262. if (bias_end != mm_size)
  263. bias_end += round_up(prandom_u32_state(&prng) % (mm_size - bias_end), ps);
  264. bias_rem += old_bias_start - bias_start;
  265. bias_rem += bias_end - old_bias_end;
  266. } while (!bias_rem && (bias_start || bias_end != mm_size));
  267. } while (bias_rem);
  268. KUNIT_ASSERT_EQ(test, bias_start, 0);
  269. KUNIT_ASSERT_EQ(test, bias_end, mm_size);
  270. KUNIT_ASSERT_TRUE_MSG(test,
  271. drm_buddy_alloc_blocks(&mm, bias_start, bias_end,
  272. ps, ps,
  273. &allocated,
  274. DRM_BUDDY_RANGE_ALLOCATION),
  275. "buddy_alloc passed with bias(%x-%x), size=%u\n",
  276. bias_start, bias_end, ps);
  277. drm_buddy_free_list(&mm, &allocated, 0);
  278. drm_buddy_fini(&mm);
  279. /*
  280. * Allocate cleared blocks in the bias range when the DRM buddy's clear avail is
  281. * zero. This will validate the bias range allocation in scenarios like system boot
  282. * when no cleared blocks are available and exercise the fallback path too. The resulting
  283. * blocks should always be dirty.
  284. */
  285. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, ps),
  286. "buddy_init failed\n");
  287. bias_start = round_up(prandom_u32_state(&prng) % (mm_size - ps), ps);
  288. bias_end = round_up(bias_start + prandom_u32_state(&prng) % (mm_size - bias_start), ps);
  289. bias_end = max(bias_end, bias_start + ps);
  290. bias_rem = bias_end - bias_start;
  291. flags = DRM_BUDDY_CLEAR_ALLOCATION | DRM_BUDDY_RANGE_ALLOCATION;
  292. size = max(round_up(prandom_u32_state(&prng) % bias_rem, ps), ps);
  293. KUNIT_ASSERT_FALSE_MSG(test,
  294. drm_buddy_alloc_blocks(&mm, bias_start,
  295. bias_end, size, ps,
  296. &allocated,
  297. flags),
  298. "buddy_alloc failed with bias(%x-%x), size=%u, ps=%u\n",
  299. bias_start, bias_end, size, ps);
  300. list_for_each_entry(block, &allocated, link)
  301. KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), false);
  302. drm_buddy_free_list(&mm, &allocated, 0);
  303. drm_buddy_fini(&mm);
  304. }
  305. static void drm_test_buddy_alloc_clear(struct kunit *test)
  306. {
  307. unsigned long n_pages, total, i = 0;
  308. const unsigned long ps = SZ_4K;
  309. struct drm_buddy_block *block;
  310. const int max_order = 12;
  311. LIST_HEAD(allocated);
  312. struct drm_buddy mm;
  313. unsigned int order;
  314. u32 mm_size, size;
  315. LIST_HEAD(dirty);
  316. LIST_HEAD(clean);
  317. mm_size = SZ_4K << max_order;
  318. KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
  319. KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
  320. /*
  321. * Idea is to allocate and free some random portion of the address space,
  322. * returning those pages as non-dirty and randomly alternate between
  323. * requesting dirty and non-dirty pages (not going over the limit
  324. * we freed as non-dirty), putting that into two separate lists.
  325. * Loop over both lists at the end checking that the dirty list
  326. * is indeed all dirty pages and vice versa. Free it all again,
  327. * keeping the dirty/clear status.
  328. */
  329. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  330. 5 * ps, ps, &allocated,
  331. DRM_BUDDY_TOPDOWN_ALLOCATION),
  332. "buddy_alloc hit an error size=%lu\n", 5 * ps);
  333. drm_buddy_free_list(&mm, &allocated, DRM_BUDDY_CLEARED);
  334. n_pages = 10;
  335. do {
  336. unsigned long flags;
  337. struct list_head *list;
  338. int slot = i % 2;
  339. if (slot == 0) {
  340. list = &dirty;
  341. flags = 0;
  342. } else {
  343. list = &clean;
  344. flags = DRM_BUDDY_CLEAR_ALLOCATION;
  345. }
  346. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  347. ps, ps, list,
  348. flags),
  349. "buddy_alloc hit an error size=%lu\n", ps);
  350. } while (++i < n_pages);
  351. list_for_each_entry(block, &clean, link)
  352. KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), true);
  353. list_for_each_entry(block, &dirty, link)
  354. KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), false);
  355. drm_buddy_free_list(&mm, &clean, DRM_BUDDY_CLEARED);
  356. /*
  357. * Trying to go over the clear limit for some allocation.
  358. * The allocation should never fail with reasonable page-size.
  359. */
  360. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  361. 10 * ps, ps, &clean,
  362. DRM_BUDDY_CLEAR_ALLOCATION),
  363. "buddy_alloc hit an error size=%lu\n", 10 * ps);
  364. drm_buddy_free_list(&mm, &clean, DRM_BUDDY_CLEARED);
  365. drm_buddy_free_list(&mm, &dirty, 0);
  366. drm_buddy_fini(&mm);
  367. KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
  368. /*
  369. * Create a new mm. Intentionally fragment the address space by creating
  370. * two alternating lists. Free both lists, one as dirty the other as clean.
  371. * Try to allocate double the previous size with matching min_page_size. The
  372. * allocation should never fail as it calls the force_merge. Also check that
  373. * the page is always dirty after force_merge. Free the page as dirty, then
  374. * repeat the whole thing, increment the order until we hit the max_order.
  375. */
  376. i = 0;
  377. n_pages = mm_size / ps;
  378. do {
  379. struct list_head *list;
  380. int slot = i % 2;
  381. if (slot == 0)
  382. list = &dirty;
  383. else
  384. list = &clean;
  385. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  386. ps, ps, list, 0),
  387. "buddy_alloc hit an error size=%lu\n", ps);
  388. } while (++i < n_pages);
  389. drm_buddy_free_list(&mm, &clean, DRM_BUDDY_CLEARED);
  390. drm_buddy_free_list(&mm, &dirty, 0);
  391. order = 1;
  392. do {
  393. size = SZ_4K << order;
  394. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  395. size, size, &allocated,
  396. DRM_BUDDY_CLEAR_ALLOCATION),
  397. "buddy_alloc hit an error size=%u\n", size);
  398. total = 0;
  399. list_for_each_entry(block, &allocated, link) {
  400. if (size != mm_size)
  401. KUNIT_EXPECT_EQ(test, drm_buddy_block_is_clear(block), false);
  402. total += drm_buddy_block_size(&mm, block);
  403. }
  404. KUNIT_EXPECT_EQ(test, total, size);
  405. drm_buddy_free_list(&mm, &allocated, 0);
  406. } while (++order <= max_order);
  407. drm_buddy_fini(&mm);
  408. /*
  409. * Create a new mm with a non power-of-two size. Allocate a random size from each
  410. * root, free as cleared and then call fini. This will ensure the multi-root
  411. * force merge during fini.
  412. */
  413. mm_size = (SZ_4K << max_order) + (SZ_4K << (max_order - 2));
  414. KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
  415. KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
  416. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, SZ_4K << max_order,
  417. 4 * ps, ps, &allocated,
  418. DRM_BUDDY_RANGE_ALLOCATION),
  419. "buddy_alloc hit an error size=%lu\n", 4 * ps);
  420. drm_buddy_free_list(&mm, &allocated, DRM_BUDDY_CLEARED);
  421. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, SZ_4K << max_order,
  422. 2 * ps, ps, &allocated,
  423. DRM_BUDDY_CLEAR_ALLOCATION),
  424. "buddy_alloc hit an error size=%lu\n", 2 * ps);
  425. drm_buddy_free_list(&mm, &allocated, DRM_BUDDY_CLEARED);
  426. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, SZ_4K << max_order, mm_size,
  427. ps, ps, &allocated,
  428. DRM_BUDDY_RANGE_ALLOCATION),
  429. "buddy_alloc hit an error size=%lu\n", ps);
  430. drm_buddy_free_list(&mm, &allocated, DRM_BUDDY_CLEARED);
  431. drm_buddy_fini(&mm);
  432. }
  433. static void drm_test_buddy_alloc_contiguous(struct kunit *test)
  434. {
  435. const unsigned long ps = SZ_4K, mm_size = 16 * 3 * SZ_4K;
  436. unsigned long i, n_pages, total;
  437. struct drm_buddy_block *block;
  438. struct drm_buddy mm;
  439. LIST_HEAD(left);
  440. LIST_HEAD(middle);
  441. LIST_HEAD(right);
  442. LIST_HEAD(allocated);
  443. KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, mm_size, ps));
  444. /*
  445. * Idea is to fragment the address space by alternating block
  446. * allocations between three different lists; one for left, middle and
  447. * right. We can then free a list to simulate fragmentation. In
  448. * particular we want to exercise the DRM_BUDDY_CONTIGUOUS_ALLOCATION,
  449. * including the try_harder path.
  450. */
  451. i = 0;
  452. n_pages = mm_size / ps;
  453. do {
  454. struct list_head *list;
  455. int slot = i % 3;
  456. if (slot == 0)
  457. list = &left;
  458. else if (slot == 1)
  459. list = &middle;
  460. else
  461. list = &right;
  462. KUNIT_ASSERT_FALSE_MSG(test,
  463. drm_buddy_alloc_blocks(&mm, 0, mm_size,
  464. ps, ps, list, 0),
  465. "buddy_alloc hit an error size=%lu\n",
  466. ps);
  467. } while (++i < n_pages);
  468. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  469. 3 * ps, ps, &allocated,
  470. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  471. "buddy_alloc didn't error size=%lu\n", 3 * ps);
  472. drm_buddy_free_list(&mm, &middle, 0);
  473. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  474. 3 * ps, ps, &allocated,
  475. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  476. "buddy_alloc didn't error size=%lu\n", 3 * ps);
  477. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  478. 2 * ps, ps, &allocated,
  479. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  480. "buddy_alloc didn't error size=%lu\n", 2 * ps);
  481. drm_buddy_free_list(&mm, &right, 0);
  482. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  483. 3 * ps, ps, &allocated,
  484. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  485. "buddy_alloc didn't error size=%lu\n", 3 * ps);
  486. /*
  487. * At this point we should have enough contiguous space for 2 blocks,
  488. * however they are never buddies (since we freed middle and right) so
  489. * will require the try_harder logic to find them.
  490. */
  491. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  492. 2 * ps, ps, &allocated,
  493. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  494. "buddy_alloc hit an error size=%lu\n", 2 * ps);
  495. drm_buddy_free_list(&mm, &left, 0);
  496. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size,
  497. 3 * ps, ps, &allocated,
  498. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  499. "buddy_alloc hit an error size=%lu\n", 3 * ps);
  500. total = 0;
  501. list_for_each_entry(block, &allocated, link)
  502. total += drm_buddy_block_size(&mm, block);
  503. KUNIT_ASSERT_EQ(test, total, ps * 2 + ps * 3);
  504. drm_buddy_free_list(&mm, &allocated, 0);
  505. drm_buddy_fini(&mm);
  506. }
  507. static void drm_test_buddy_alloc_pathological(struct kunit *test)
  508. {
  509. u64 mm_size, size, start = 0;
  510. struct drm_buddy_block *block;
  511. const int max_order = 3;
  512. unsigned long flags = 0;
  513. int order, top;
  514. struct drm_buddy mm;
  515. LIST_HEAD(blocks);
  516. LIST_HEAD(holes);
  517. LIST_HEAD(tmp);
  518. /*
  519. * Create a pot-sized mm, then allocate one of each possible
  520. * order within. This should leave the mm with exactly one
  521. * page left. Free the largest block, then whittle down again.
  522. * Eventually we will have a fully 50% fragmented mm.
  523. */
  524. mm_size = SZ_4K << max_order;
  525. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
  526. "buddy_init failed\n");
  527. KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
  528. for (top = max_order; top; top--) {
  529. /* Make room by freeing the largest allocated block */
  530. block = list_first_entry_or_null(&blocks, typeof(*block), link);
  531. if (block) {
  532. list_del(&block->link);
  533. drm_buddy_free_block(&mm, block);
  534. }
  535. for (order = top; order--;) {
  536. size = get_size(order, mm.chunk_size);
  537. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start,
  538. mm_size, size, size,
  539. &tmp, flags),
  540. "buddy_alloc hit -ENOMEM with order=%d, top=%d\n",
  541. order, top);
  542. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  543. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  544. list_move_tail(&block->link, &blocks);
  545. }
  546. /* There should be one final page for this sub-allocation */
  547. size = get_size(0, mm.chunk_size);
  548. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  549. size, size, &tmp, flags),
  550. "buddy_alloc hit -ENOMEM for hole\n");
  551. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  552. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  553. list_move_tail(&block->link, &holes);
  554. size = get_size(top, mm.chunk_size);
  555. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  556. size, size, &tmp, flags),
  557. "buddy_alloc unexpectedly succeeded at top-order %d/%d, it should be full!",
  558. top, max_order);
  559. }
  560. drm_buddy_free_list(&mm, &holes, 0);
  561. /* Nothing larger than blocks of chunk_size now available */
  562. for (order = 1; order <= max_order; order++) {
  563. size = get_size(order, mm.chunk_size);
  564. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  565. size, size, &tmp, flags),
  566. "buddy_alloc unexpectedly succeeded at order %d, it should be full!",
  567. order);
  568. }
  569. list_splice_tail(&holes, &blocks);
  570. drm_buddy_free_list(&mm, &blocks, 0);
  571. drm_buddy_fini(&mm);
  572. }
  573. static void drm_test_buddy_alloc_pessimistic(struct kunit *test)
  574. {
  575. u64 mm_size, size, start = 0;
  576. struct drm_buddy_block *block, *bn;
  577. const unsigned int max_order = 16;
  578. unsigned long flags = 0;
  579. struct drm_buddy mm;
  580. unsigned int order;
  581. LIST_HEAD(blocks);
  582. LIST_HEAD(tmp);
  583. /*
  584. * Create a pot-sized mm, then allocate one of each possible
  585. * order within. This should leave the mm with exactly one
  586. * page left.
  587. */
  588. mm_size = SZ_4K << max_order;
  589. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
  590. "buddy_init failed\n");
  591. KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
  592. for (order = 0; order < max_order; order++) {
  593. size = get_size(order, mm.chunk_size);
  594. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  595. size, size, &tmp, flags),
  596. "buddy_alloc hit -ENOMEM with order=%d\n",
  597. order);
  598. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  599. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  600. list_move_tail(&block->link, &blocks);
  601. }
  602. /* And now the last remaining block available */
  603. size = get_size(0, mm.chunk_size);
  604. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  605. size, size, &tmp, flags),
  606. "buddy_alloc hit -ENOMEM on final alloc\n");
  607. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  608. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  609. list_move_tail(&block->link, &blocks);
  610. /* Should be completely full! */
  611. for (order = max_order; order--;) {
  612. size = get_size(order, mm.chunk_size);
  613. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  614. size, size, &tmp, flags),
  615. "buddy_alloc unexpectedly succeeded, it should be full!");
  616. }
  617. block = list_last_entry(&blocks, typeof(*block), link);
  618. list_del(&block->link);
  619. drm_buddy_free_block(&mm, block);
  620. /* As we free in increasing size, we make available larger blocks */
  621. order = 1;
  622. list_for_each_entry_safe(block, bn, &blocks, link) {
  623. list_del(&block->link);
  624. drm_buddy_free_block(&mm, block);
  625. size = get_size(order, mm.chunk_size);
  626. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  627. size, size, &tmp, flags),
  628. "buddy_alloc hit -ENOMEM with order=%d\n",
  629. order);
  630. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  631. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  632. list_del(&block->link);
  633. drm_buddy_free_block(&mm, block);
  634. order++;
  635. }
  636. /* To confirm, now the whole mm should be available */
  637. size = get_size(max_order, mm.chunk_size);
  638. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  639. size, size, &tmp, flags),
  640. "buddy_alloc (realloc) hit -ENOMEM with order=%d\n",
  641. max_order);
  642. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  643. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  644. list_del(&block->link);
  645. drm_buddy_free_block(&mm, block);
  646. drm_buddy_free_list(&mm, &blocks, 0);
  647. drm_buddy_fini(&mm);
  648. }
  649. static void drm_test_buddy_alloc_optimistic(struct kunit *test)
  650. {
  651. u64 mm_size, size, start = 0;
  652. struct drm_buddy_block *block;
  653. unsigned long flags = 0;
  654. const int max_order = 16;
  655. struct drm_buddy mm;
  656. LIST_HEAD(blocks);
  657. LIST_HEAD(tmp);
  658. int order;
  659. /*
  660. * Create a mm with one block of each order available, and
  661. * try to allocate them all.
  662. */
  663. mm_size = SZ_4K * ((1 << (max_order + 1)) - 1);
  664. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
  665. "buddy_init failed\n");
  666. KUNIT_EXPECT_EQ(test, mm.max_order, max_order);
  667. for (order = 0; order <= max_order; order++) {
  668. size = get_size(order, mm.chunk_size);
  669. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  670. size, size, &tmp, flags),
  671. "buddy_alloc hit -ENOMEM with order=%d\n",
  672. order);
  673. block = list_first_entry_or_null(&tmp, struct drm_buddy_block, link);
  674. KUNIT_ASSERT_TRUE_MSG(test, block, "alloc_blocks has no blocks\n");
  675. list_move_tail(&block->link, &blocks);
  676. }
  677. /* Should be completely full! */
  678. size = get_size(0, mm.chunk_size);
  679. KUNIT_ASSERT_TRUE_MSG(test, drm_buddy_alloc_blocks(&mm, start, mm_size,
  680. size, size, &tmp, flags),
  681. "buddy_alloc unexpectedly succeeded, it should be full!");
  682. drm_buddy_free_list(&mm, &blocks, 0);
  683. drm_buddy_fini(&mm);
  684. }
  685. static void drm_test_buddy_alloc_limit(struct kunit *test)
  686. {
  687. u64 size = U64_MAX, start = 0;
  688. struct drm_buddy_block *block;
  689. unsigned long flags = 0;
  690. LIST_HEAD(allocated);
  691. struct drm_buddy mm;
  692. KUNIT_EXPECT_FALSE(test, drm_buddy_init(&mm, size, SZ_4K));
  693. KUNIT_EXPECT_EQ_MSG(test, mm.max_order, DRM_BUDDY_MAX_ORDER,
  694. "mm.max_order(%d) != %d\n", mm.max_order,
  695. DRM_BUDDY_MAX_ORDER);
  696. size = mm.chunk_size << mm.max_order;
  697. KUNIT_EXPECT_FALSE(test, drm_buddy_alloc_blocks(&mm, start, size, size,
  698. mm.chunk_size, &allocated, flags));
  699. block = list_first_entry_or_null(&allocated, struct drm_buddy_block, link);
  700. KUNIT_EXPECT_TRUE(test, block);
  701. KUNIT_EXPECT_EQ_MSG(test, drm_buddy_block_order(block), mm.max_order,
  702. "block order(%d) != %d\n",
  703. drm_buddy_block_order(block), mm.max_order);
  704. KUNIT_EXPECT_EQ_MSG(test, drm_buddy_block_size(&mm, block),
  705. BIT_ULL(mm.max_order) * mm.chunk_size,
  706. "block size(%llu) != %llu\n",
  707. drm_buddy_block_size(&mm, block),
  708. BIT_ULL(mm.max_order) * mm.chunk_size);
  709. drm_buddy_free_list(&mm, &allocated, 0);
  710. drm_buddy_fini(&mm);
  711. }
  712. static void drm_test_buddy_alloc_exceeds_max_order(struct kunit *test)
  713. {
  714. u64 mm_size = SZ_8G + SZ_2G, size = SZ_8G + SZ_1G, min_block_size = SZ_8G;
  715. struct drm_buddy mm;
  716. LIST_HEAD(blocks);
  717. int err;
  718. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_init(&mm, mm_size, SZ_4K),
  719. "buddy_init failed\n");
  720. /* CONTIGUOUS allocation should succeed via try_harder fallback */
  721. KUNIT_ASSERT_FALSE_MSG(test, drm_buddy_alloc_blocks(&mm, 0, mm_size, size,
  722. SZ_4K, &blocks,
  723. DRM_BUDDY_CONTIGUOUS_ALLOCATION),
  724. "buddy_alloc hit an error size=%llu\n", size);
  725. drm_buddy_free_list(&mm, &blocks, 0);
  726. /* Non-CONTIGUOUS with large min_block_size should return -EINVAL */
  727. err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, &blocks, 0);
  728. KUNIT_EXPECT_EQ(test, err, -EINVAL);
  729. /* Non-CONTIGUOUS + RANGE with large min_block_size should return -EINVAL */
  730. err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, min_block_size, &blocks,
  731. DRM_BUDDY_RANGE_ALLOCATION);
  732. KUNIT_EXPECT_EQ(test, err, -EINVAL);
  733. /* CONTIGUOUS + RANGE should return -EINVAL (no try_harder for RANGE) */
  734. err = drm_buddy_alloc_blocks(&mm, 0, mm_size, size, SZ_4K, &blocks,
  735. DRM_BUDDY_CONTIGUOUS_ALLOCATION | DRM_BUDDY_RANGE_ALLOCATION);
  736. KUNIT_EXPECT_EQ(test, err, -EINVAL);
  737. drm_buddy_fini(&mm);
  738. }
  739. static int drm_buddy_suite_init(struct kunit_suite *suite)
  740. {
  741. while (!random_seed)
  742. random_seed = get_random_u32();
  743. kunit_info(suite, "Testing DRM buddy manager, with random_seed=0x%x\n",
  744. random_seed);
  745. return 0;
  746. }
  747. static struct kunit_case drm_buddy_tests[] = {
  748. KUNIT_CASE(drm_test_buddy_alloc_limit),
  749. KUNIT_CASE(drm_test_buddy_alloc_optimistic),
  750. KUNIT_CASE(drm_test_buddy_alloc_pessimistic),
  751. KUNIT_CASE(drm_test_buddy_alloc_pathological),
  752. KUNIT_CASE(drm_test_buddy_alloc_contiguous),
  753. KUNIT_CASE(drm_test_buddy_alloc_clear),
  754. KUNIT_CASE(drm_test_buddy_alloc_range_bias),
  755. KUNIT_CASE(drm_test_buddy_fragmentation_performance),
  756. KUNIT_CASE(drm_test_buddy_alloc_exceeds_max_order),
  757. {}
  758. };
  759. static struct kunit_suite drm_buddy_test_suite = {
  760. .name = "drm_buddy",
  761. .suite_init = drm_buddy_suite_init,
  762. .test_cases = drm_buddy_tests,
  763. };
  764. kunit_test_suite(drm_buddy_test_suite);
  765. MODULE_AUTHOR("Intel Corporation");
  766. MODULE_DESCRIPTION("Kunit test for drm_buddy functions");
  767. MODULE_LICENSE("GPL");