test_parman.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * lib/test_parman.c - Test module for parman
  3. * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2017 Jiri Pirko <jiri@mellanox.com>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the names of the copyright holders nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * Alternatively, this software may be distributed under the terms of the
  19. * GNU General Public License ("GPL") version 2 as published by the Free
  20. * Software Foundation.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  35. #include <linux/kernel.h>
  36. #include <linux/module.h>
  37. #include <linux/slab.h>
  38. #include <linux/bitops.h>
  39. #include <linux/err.h>
  40. #include <linux/random.h>
  41. #include <linux/parman.h>
  42. #define TEST_PARMAN_PRIO_SHIFT 7 /* defines number of prios for testing */
  43. #define TEST_PARMAN_PRIO_COUNT BIT(TEST_PARMAN_PRIO_SHIFT)
  44. #define TEST_PARMAN_PRIO_MASK (TEST_PARMAN_PRIO_COUNT - 1)
  45. #define TEST_PARMAN_ITEM_SHIFT 13 /* defines a total number
  46. * of items for testing
  47. */
  48. #define TEST_PARMAN_ITEM_COUNT BIT(TEST_PARMAN_ITEM_SHIFT)
  49. #define TEST_PARMAN_ITEM_MASK (TEST_PARMAN_ITEM_COUNT - 1)
  50. #define TEST_PARMAN_BASE_SHIFT 8
  51. #define TEST_PARMAN_BASE_COUNT BIT(TEST_PARMAN_BASE_SHIFT)
  52. #define TEST_PARMAN_RESIZE_STEP_SHIFT 7
  53. #define TEST_PARMAN_RESIZE_STEP_COUNT BIT(TEST_PARMAN_RESIZE_STEP_SHIFT)
  54. #define TEST_PARMAN_BULK_MAX_SHIFT (2 + TEST_PARMAN_RESIZE_STEP_SHIFT)
  55. #define TEST_PARMAN_BULK_MAX_COUNT BIT(TEST_PARMAN_BULK_MAX_SHIFT)
  56. #define TEST_PARMAN_BULK_MAX_MASK (TEST_PARMAN_BULK_MAX_COUNT - 1)
  57. #define TEST_PARMAN_RUN_BUDGET (TEST_PARMAN_ITEM_COUNT * 256)
  58. struct test_parman_prio {
  59. struct parman_prio parman_prio;
  60. unsigned long priority;
  61. };
  62. struct test_parman_item {
  63. struct parman_item parman_item;
  64. struct test_parman_prio *prio;
  65. bool used;
  66. };
  67. struct test_parman {
  68. struct parman *parman;
  69. struct test_parman_item **prio_array;
  70. unsigned long prio_array_limit;
  71. struct test_parman_prio prios[TEST_PARMAN_PRIO_COUNT];
  72. struct test_parman_item items[TEST_PARMAN_ITEM_COUNT];
  73. struct rnd_state rnd;
  74. unsigned long run_budget;
  75. unsigned long bulk_budget;
  76. bool bulk_noop;
  77. unsigned int used_items;
  78. };
  79. #define ITEM_PTRS_SIZE(count) (sizeof(struct test_parman_item *) * (count))
  80. static int test_parman_resize(void *priv, unsigned long new_count)
  81. {
  82. struct test_parman *test_parman = priv;
  83. struct test_parman_item **prio_array;
  84. unsigned long old_count;
  85. prio_array = krealloc(test_parman->prio_array,
  86. ITEM_PTRS_SIZE(new_count), GFP_KERNEL);
  87. if (new_count == 0)
  88. return 0;
  89. if (!prio_array)
  90. return -ENOMEM;
  91. old_count = test_parman->prio_array_limit;
  92. if (new_count > old_count)
  93. memset(&prio_array[old_count], 0,
  94. ITEM_PTRS_SIZE(new_count - old_count));
  95. test_parman->prio_array = prio_array;
  96. test_parman->prio_array_limit = new_count;
  97. return 0;
  98. }
  99. static void test_parman_move(void *priv, unsigned long from_index,
  100. unsigned long to_index, unsigned long count)
  101. {
  102. struct test_parman *test_parman = priv;
  103. struct test_parman_item **prio_array = test_parman->prio_array;
  104. memmove(&prio_array[to_index], &prio_array[from_index],
  105. ITEM_PTRS_SIZE(count));
  106. memset(&prio_array[from_index], 0, ITEM_PTRS_SIZE(count));
  107. }
  108. static const struct parman_ops test_parman_lsort_ops = {
  109. .base_count = TEST_PARMAN_BASE_COUNT,
  110. .resize_step = TEST_PARMAN_RESIZE_STEP_COUNT,
  111. .resize = test_parman_resize,
  112. .move = test_parman_move,
  113. .algo = PARMAN_ALGO_TYPE_LSORT,
  114. };
  115. static void test_parman_rnd_init(struct test_parman *test_parman)
  116. {
  117. prandom_seed_state(&test_parman->rnd, 3141592653589793238ULL);
  118. }
  119. static u32 test_parman_rnd_get(struct test_parman *test_parman)
  120. {
  121. return prandom_u32_state(&test_parman->rnd);
  122. }
  123. static unsigned long test_parman_priority_gen(struct test_parman *test_parman)
  124. {
  125. unsigned long priority;
  126. int i;
  127. again:
  128. priority = test_parman_rnd_get(test_parman);
  129. if (priority == 0)
  130. goto again;
  131. for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
  132. struct test_parman_prio *prio = &test_parman->prios[i];
  133. if (prio->priority == 0)
  134. break;
  135. if (prio->priority == priority)
  136. goto again;
  137. }
  138. return priority;
  139. }
  140. static void test_parman_prios_init(struct test_parman *test_parman)
  141. {
  142. int i;
  143. for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
  144. struct test_parman_prio *prio = &test_parman->prios[i];
  145. /* Assign random uniqueue priority to each prio structure */
  146. prio->priority = test_parman_priority_gen(test_parman);
  147. parman_prio_init(test_parman->parman, &prio->parman_prio,
  148. prio->priority);
  149. }
  150. }
  151. static void test_parman_prios_fini(struct test_parman *test_parman)
  152. {
  153. int i;
  154. for (i = 0; i < TEST_PARMAN_PRIO_COUNT; i++) {
  155. struct test_parman_prio *prio = &test_parman->prios[i];
  156. parman_prio_fini(&prio->parman_prio);
  157. }
  158. }
  159. static void test_parman_items_init(struct test_parman *test_parman)
  160. {
  161. int i;
  162. for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) {
  163. struct test_parman_item *item = &test_parman->items[i];
  164. unsigned int prio_index = test_parman_rnd_get(test_parman) &
  165. TEST_PARMAN_PRIO_MASK;
  166. /* Assign random prio to each item structure */
  167. item->prio = &test_parman->prios[prio_index];
  168. }
  169. }
  170. static void test_parman_items_fini(struct test_parman *test_parman)
  171. {
  172. int i;
  173. for (i = 0; i < TEST_PARMAN_ITEM_COUNT; i++) {
  174. struct test_parman_item *item = &test_parman->items[i];
  175. if (!item->used)
  176. continue;
  177. parman_item_remove(test_parman->parman,
  178. &item->prio->parman_prio,
  179. &item->parman_item);
  180. }
  181. }
  182. static struct test_parman *test_parman_create(const struct parman_ops *ops)
  183. {
  184. struct test_parman *test_parman;
  185. int err;
  186. test_parman = kzalloc(sizeof(*test_parman), GFP_KERNEL);
  187. if (!test_parman)
  188. return ERR_PTR(-ENOMEM);
  189. err = test_parman_resize(test_parman, TEST_PARMAN_BASE_COUNT);
  190. if (err)
  191. goto err_resize;
  192. test_parman->parman = parman_create(ops, test_parman);
  193. if (!test_parman->parman) {
  194. err = -ENOMEM;
  195. goto err_parman_create;
  196. }
  197. test_parman_rnd_init(test_parman);
  198. test_parman_prios_init(test_parman);
  199. test_parman_items_init(test_parman);
  200. test_parman->run_budget = TEST_PARMAN_RUN_BUDGET;
  201. return test_parman;
  202. err_parman_create:
  203. test_parman_resize(test_parman, 0);
  204. err_resize:
  205. kfree(test_parman);
  206. return ERR_PTR(err);
  207. }
  208. static void test_parman_destroy(struct test_parman *test_parman)
  209. {
  210. test_parman_items_fini(test_parman);
  211. test_parman_prios_fini(test_parman);
  212. parman_destroy(test_parman->parman);
  213. test_parman_resize(test_parman, 0);
  214. kfree(test_parman);
  215. }
  216. static bool test_parman_run_check_budgets(struct test_parman *test_parman)
  217. {
  218. if (test_parman->run_budget-- == 0)
  219. return false;
  220. if (test_parman->bulk_budget-- != 0)
  221. return true;
  222. test_parman->bulk_budget = test_parman_rnd_get(test_parman) &
  223. TEST_PARMAN_BULK_MAX_MASK;
  224. test_parman->bulk_noop = test_parman_rnd_get(test_parman) & 1;
  225. return true;
  226. }
  227. static int test_parman_run(struct test_parman *test_parman)
  228. {
  229. unsigned int i = test_parman_rnd_get(test_parman);
  230. int err;
  231. while (test_parman_run_check_budgets(test_parman)) {
  232. unsigned int item_index = i++ & TEST_PARMAN_ITEM_MASK;
  233. struct test_parman_item *item = &test_parman->items[item_index];
  234. if (test_parman->bulk_noop)
  235. continue;
  236. if (!item->used) {
  237. err = parman_item_add(test_parman->parman,
  238. &item->prio->parman_prio,
  239. &item->parman_item);
  240. if (err)
  241. return err;
  242. test_parman->prio_array[item->parman_item.index] = item;
  243. test_parman->used_items++;
  244. } else {
  245. test_parman->prio_array[item->parman_item.index] = NULL;
  246. parman_item_remove(test_parman->parman,
  247. &item->prio->parman_prio,
  248. &item->parman_item);
  249. test_parman->used_items--;
  250. }
  251. item->used = !item->used;
  252. }
  253. return 0;
  254. }
  255. static int test_parman_check_array(struct test_parman *test_parman,
  256. bool gaps_allowed)
  257. {
  258. unsigned int last_unused_items = 0;
  259. unsigned long last_priority = 0;
  260. unsigned int used_items = 0;
  261. int i;
  262. if (test_parman->prio_array_limit < TEST_PARMAN_BASE_COUNT) {
  263. pr_err("Array limit is lower than the base count (%lu < %lu)\n",
  264. test_parman->prio_array_limit, TEST_PARMAN_BASE_COUNT);
  265. return -EINVAL;
  266. }
  267. for (i = 0; i < test_parman->prio_array_limit; i++) {
  268. struct test_parman_item *item = test_parman->prio_array[i];
  269. if (!item) {
  270. last_unused_items++;
  271. continue;
  272. }
  273. if (last_unused_items && !gaps_allowed) {
  274. pr_err("Gap found in array even though they are forbidden\n");
  275. return -EINVAL;
  276. }
  277. last_unused_items = 0;
  278. used_items++;
  279. if (item->prio->priority < last_priority) {
  280. pr_err("Item belongs under higher priority then the last one (current: %lu, previous: %lu)\n",
  281. item->prio->priority, last_priority);
  282. return -EINVAL;
  283. }
  284. last_priority = item->prio->priority;
  285. if (item->parman_item.index != i) {
  286. pr_err("Item has different index in compare to where it actually is (%lu != %d)\n",
  287. item->parman_item.index, i);
  288. return -EINVAL;
  289. }
  290. }
  291. if (used_items != test_parman->used_items) {
  292. pr_err("Number of used items in array does not match (%u != %u)\n",
  293. used_items, test_parman->used_items);
  294. return -EINVAL;
  295. }
  296. if (last_unused_items >= TEST_PARMAN_RESIZE_STEP_COUNT) {
  297. pr_err("Number of unused item at the end of array is bigger than resize step (%u >= %lu)\n",
  298. last_unused_items, TEST_PARMAN_RESIZE_STEP_COUNT);
  299. return -EINVAL;
  300. }
  301. pr_info("Priority array check successful\n");
  302. return 0;
  303. }
  304. static int test_parman_lsort(void)
  305. {
  306. struct test_parman *test_parman;
  307. int err;
  308. test_parman = test_parman_create(&test_parman_lsort_ops);
  309. if (IS_ERR(test_parman))
  310. return PTR_ERR(test_parman);
  311. err = test_parman_run(test_parman);
  312. if (err)
  313. goto out;
  314. err = test_parman_check_array(test_parman, false);
  315. if (err)
  316. goto out;
  317. out:
  318. test_parman_destroy(test_parman);
  319. return err;
  320. }
  321. static int __init test_parman_init(void)
  322. {
  323. return test_parman_lsort();
  324. }
  325. static void __exit test_parman_exit(void)
  326. {
  327. }
  328. module_init(test_parman_init);
  329. module_exit(test_parman_exit);
  330. MODULE_LICENSE("Dual BSD/GPL");
  331. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  332. MODULE_DESCRIPTION("Test module for parman");