Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * KUnit tests for cpumask.
0004  *
0005  * Author: Sander Vanheule <sander@svanheule.net>
0006  */
0007 
0008 #include <kunit/test.h>
0009 #include <linux/cpu.h>
0010 #include <linux/cpumask.h>
0011 
0012 #define MASK_MSG(m) \
0013     "%s contains %sCPUs %*pbl", #m, (cpumask_weight(m) ? "" : "no "), \
0014     nr_cpumask_bits, cpumask_bits(m)
0015 
0016 #define EXPECT_FOR_EACH_CPU_EQ(test, mask)          \
0017     do {                            \
0018         const cpumask_t *m = (mask);            \
0019         int mask_weight = cpumask_weight(m);        \
0020         int cpu, iter = 0;              \
0021         for_each_cpu(cpu, m)                \
0022             iter++;                 \
0023         KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(mask)); \
0024     } while (0)
0025 
0026 #define EXPECT_FOR_EACH_CPU_NOT_EQ(test, mask)                  \
0027     do {                                    \
0028         const cpumask_t *m = (mask);                    \
0029         int mask_weight = cpumask_weight(m);                \
0030         int cpu, iter = 0;                      \
0031         for_each_cpu_not(cpu, m)                    \
0032             iter++;                         \
0033         KUNIT_EXPECT_EQ_MSG((test), nr_cpu_ids - mask_weight, iter, MASK_MSG(mask));    \
0034     } while (0)
0035 
0036 #define EXPECT_FOR_EACH_CPU_WRAP_EQ(test, mask)         \
0037     do {                            \
0038         const cpumask_t *m = (mask);            \
0039         int mask_weight = cpumask_weight(m);        \
0040         int cpu, iter = 0;              \
0041         for_each_cpu_wrap(cpu, m, nr_cpu_ids / 2)   \
0042             iter++;                 \
0043         KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(mask)); \
0044     } while (0)
0045 
0046 #define EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, name)      \
0047     do {                            \
0048         int mask_weight = num_##name##_cpus();      \
0049         int cpu, iter = 0;              \
0050         for_each_##name##_cpu(cpu)          \
0051             iter++;                 \
0052         KUNIT_EXPECT_EQ_MSG((test), mask_weight, iter, MASK_MSG(cpu_##name##_mask));    \
0053     } while (0)
0054 
0055 static cpumask_t mask_empty;
0056 static cpumask_t mask_all;
0057 
0058 static void test_cpumask_weight(struct kunit *test)
0059 {
0060     KUNIT_EXPECT_TRUE_MSG(test, cpumask_empty(&mask_empty), MASK_MSG(&mask_empty));
0061     KUNIT_EXPECT_TRUE_MSG(test, cpumask_full(&mask_all), MASK_MSG(&mask_all));
0062 
0063     KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_weight(&mask_empty), MASK_MSG(&mask_empty));
0064     KUNIT_EXPECT_EQ_MSG(test, nr_cpu_ids, cpumask_weight(cpu_possible_mask),
0065                 MASK_MSG(cpu_possible_mask));
0066     KUNIT_EXPECT_EQ_MSG(test, nr_cpumask_bits, cpumask_weight(&mask_all), MASK_MSG(&mask_all));
0067 }
0068 
0069 static void test_cpumask_first(struct kunit *test)
0070 {
0071     KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_first(&mask_empty), MASK_MSG(&mask_empty));
0072     KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_first(cpu_possible_mask), MASK_MSG(cpu_possible_mask));
0073 
0074     KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_first_zero(&mask_empty), MASK_MSG(&mask_empty));
0075     KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_first_zero(cpu_possible_mask),
0076                 MASK_MSG(cpu_possible_mask));
0077 }
0078 
0079 static void test_cpumask_last(struct kunit *test)
0080 {
0081     KUNIT_EXPECT_LE_MSG(test, nr_cpumask_bits, cpumask_last(&mask_empty),
0082                 MASK_MSG(&mask_empty));
0083     KUNIT_EXPECT_EQ_MSG(test, nr_cpu_ids - 1, cpumask_last(cpu_possible_mask),
0084                 MASK_MSG(cpu_possible_mask));
0085 }
0086 
0087 static void test_cpumask_next(struct kunit *test)
0088 {
0089     KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_next_zero(-1, &mask_empty), MASK_MSG(&mask_empty));
0090     KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_next_zero(-1, cpu_possible_mask),
0091                 MASK_MSG(cpu_possible_mask));
0092 
0093     KUNIT_EXPECT_LE_MSG(test, nr_cpu_ids, cpumask_next(-1, &mask_empty),
0094                 MASK_MSG(&mask_empty));
0095     KUNIT_EXPECT_EQ_MSG(test, 0, cpumask_next(-1, cpu_possible_mask),
0096                 MASK_MSG(cpu_possible_mask));
0097 }
0098 
0099 static void test_cpumask_iterators(struct kunit *test)
0100 {
0101     EXPECT_FOR_EACH_CPU_EQ(test, &mask_empty);
0102     EXPECT_FOR_EACH_CPU_NOT_EQ(test, &mask_empty);
0103     EXPECT_FOR_EACH_CPU_WRAP_EQ(test, &mask_empty);
0104 
0105     EXPECT_FOR_EACH_CPU_EQ(test, cpu_possible_mask);
0106     EXPECT_FOR_EACH_CPU_NOT_EQ(test, cpu_possible_mask);
0107     EXPECT_FOR_EACH_CPU_WRAP_EQ(test, cpu_possible_mask);
0108 }
0109 
0110 static void test_cpumask_iterators_builtin(struct kunit *test)
0111 {
0112     EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, possible);
0113 
0114     /* Ensure the dynamic masks are stable while running the tests */
0115     cpu_hotplug_disable();
0116 
0117     EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, online);
0118     EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, present);
0119 
0120     cpu_hotplug_enable();
0121 }
0122 
0123 static int test_cpumask_init(struct kunit *test)
0124 {
0125     cpumask_clear(&mask_empty);
0126     cpumask_setall(&mask_all);
0127 
0128     return 0;
0129 }
0130 
0131 static struct kunit_case test_cpumask_cases[] = {
0132     KUNIT_CASE(test_cpumask_weight),
0133     KUNIT_CASE(test_cpumask_first),
0134     KUNIT_CASE(test_cpumask_last),
0135     KUNIT_CASE(test_cpumask_next),
0136     KUNIT_CASE(test_cpumask_iterators),
0137     KUNIT_CASE(test_cpumask_iterators_builtin),
0138     {}
0139 };
0140 
0141 static struct kunit_suite test_cpumask_suite = {
0142     .name = "cpumask",
0143     .init = test_cpumask_init,
0144     .test_cases = test_cpumask_cases,
0145 };
0146 kunit_test_suite(test_cpumask_suite);
0147 
0148 MODULE_LICENSE("GPL");