cpumask_kunit.c 5.3 KB

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