cpumask_kunit.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * KUnit tests for cpumask.
  4. *
  5. * Author: Sander Vanheule <sander@svanheule.net>
  6. */
  7. #include <kunit/test.h>
  8. #include <linux/cpu.h>
  9. #include <linux/cpumask.h>
  10. #define MASK_MSG(m) \
  11. "%s contains %sCPUs %*pbl", #m, (cpumask_weight(m) ? "" : "no "), \
  12. nr_cpumask_bits, cpumask_bits(m)
  13. #define EXPECT_FOR_EACH_CPU_EQ(test, mask) \
  14. do { \
  15. const cpumask_t *m = (mask); \
  16. int mask_weight = cpumask_weight(m); \
  17. int cpu, iter = 0; \
  18. for_each_cpu(cpu, m) \
  19. iter++; \
  20. KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(mask)); \
  21. } while (0)
  22. #define EXPECT_FOR_EACH_CPU_OP_EQ(test, op, mask1, mask2) \
  23. do { \
  24. const cpumask_t *m1 = (mask1); \
  25. const cpumask_t *m2 = (mask2); \
  26. int weight; \
  27. int cpu, iter = 0; \
  28. cpumask_##op(&mask_tmp, m1, m2); \
  29. weight = cpumask_weight(&mask_tmp); \
  30. for_each_cpu_##op(cpu, mask1, mask2) \
  31. iter++; \
  32. KUNIT_EXPECT_EQ((test), weight, iter); \
  33. } while (0)
  34. #define EXPECT_FOR_EACH_CPU_WRAP_EQ(test, mask) \
  35. do { \
  36. const cpumask_t *m = (mask); \
  37. int mask_weight = cpumask_weight(m); \
  38. int cpu, iter = 0; \
  39. for_each_cpu_wrap(cpu, m, nr_cpu_ids / 2) \
  40. iter++; \
  41. KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(mask)); \
  42. } while (0)
  43. #define EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, name) \
  44. do { \
  45. int mask_weight = num_##name##_cpus(); \
  46. int cpu, iter = 0; \
  47. for_each_##name##_cpu(cpu) \
  48. iter++; \
  49. KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(cpu_##name##_mask)); \
  50. } while (0)
  51. static cpumask_t mask_empty;
  52. static cpumask_t mask_all;
  53. static cpumask_t mask_tmp;
  54. static void test_cpumask_weight(struct kunit *test)
  55. {
  56. KUNIT_EXPECT_TRUE_MSG(test, cpumask_empty(&mask_empty), MASK_MSG(&mask_empty));
  57. KUNIT_EXPECT_TRUE_MSG(test, cpumask_full(&mask_all), MASK_MSG(&mask_all));
  58. KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_weight(&mask_empty), MASK_MSG(&mask_empty));
  59. KUNIT_EXPECT_EQ_MSG(test, nr_cpu_ids, cpumask_weight(cpu_possible_mask),
  60. MASK_MSG(cpu_possible_mask));
  61. KUNIT_EXPECT_EQ_MSG(test, nr_cpu_ids, cpumask_weight(&mask_all), MASK_MSG(&mask_all));
  62. }
  63. static void test_cpumask_first(struct kunit *test)
  64. {
  65. KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_first(&mask_empty), MASK_MSG(&mask_empty));
  66. KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_first(cpu_possible_mask), MASK_MSG(cpu_possible_mask));
  67. KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_first_zero(&mask_empty), MASK_MSG(&mask_empty));
  68. KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_first_zero(cpu_possible_mask),
  69. MASK_MSG(cpu_possible_mask));
  70. }
  71. static void test_cpumask_last(struct kunit *test)
  72. {
  73. KUNIT_EXPECT_LE_MSG(test, nr_cpumask_bits, cpumask_last(&mask_empty),
  74. MASK_MSG(&mask_empty));
  75. KUNIT_EXPECT_EQ_MSG(test, nr_cpu_ids - 1, cpumask_last(cpu_possible_mask),
  76. MASK_MSG(cpu_possible_mask));
  77. }
  78. static void test_cpumask_next(struct kunit *test)
  79. {
  80. KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_next_zero(-1, &mask_empty), MASK_MSG(&mask_empty));
  81. KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_next_zero(-1, cpu_possible_mask),
  82. MASK_MSG(cpu_possible_mask));
  83. KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_next(-1, &mask_empty),
  84. MASK_MSG(&mask_empty));
  85. KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_next(-1, cpu_possible_mask),
  86. MASK_MSG(cpu_possible_mask));
  87. }
  88. static void test_cpumask_iterators(struct kunit *test)
  89. {
  90. EXPECT_FOR_EACH_CPU_EQ(test, &mask_empty);
  91. EXPECT_FOR_EACH_CPU_WRAP_EQ(test, &mask_empty);
  92. EXPECT_FOR_EACH_CPU_OP_EQ(test, and, &mask_empty, &mask_empty);
  93. EXPECT_FOR_EACH_CPU_OP_EQ(test, and, cpu_possible_mask, &mask_empty);
  94. EXPECT_FOR_EACH_CPU_OP_EQ(test, andnot, &mask_empty, &mask_empty);
  95. EXPECT_FOR_EACH_CPU_EQ(test, cpu_possible_mask);
  96. EXPECT_FOR_EACH_CPU_WRAP_EQ(test, cpu_possible_mask);
  97. EXPECT_FOR_EACH_CPU_OP_EQ(test, and, cpu_possible_mask, cpu_possible_mask);
  98. EXPECT_FOR_EACH_CPU_OP_EQ(test, andnot, cpu_possible_mask, &mask_empty);
  99. }
  100. static void test_cpumask_iterators_builtin(struct kunit *test)
  101. {
  102. EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, possible);
  103. /* Ensure the dynamic masks are stable while running the tests */
  104. cpu_hotplug_disable();
  105. EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, online);
  106. EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, present);
  107. cpu_hotplug_enable();
  108. }
  109. static int test_cpumask_init(struct kunit *test)
  110. {
  111. cpumask_clear(&mask_empty);
  112. cpumask_setall(&mask_all);
  113. return 0;
  114. }
  115. static struct kunit_case test_cpumask_cases[] = {
  116. KUNIT_CASE(test_cpumask_weight),
  117. KUNIT_CASE(test_cpumask_first),
  118. KUNIT_CASE(test_cpumask_last),
  119. KUNIT_CASE(test_cpumask_next),
  120. KUNIT_CASE(test_cpumask_iterators),
  121. KUNIT_CASE(test_cpumask_iterators_builtin),
  122. {}
  123. };
  124. static struct kunit_suite test_cpumask_suite = {
  125. .name = "cpumask",
  126. .init = test_cpumask_init,
  127. .test_cases = test_cpumask_cases,
  128. };
  129. kunit_test_suite(test_cpumask_suite);
  130. MODULE_DESCRIPTION("KUnit tests for cpumask");
  131. MODULE_LICENSE("GPL");