^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) /* validate @native and @pcp counter values match @expected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define CHECK(native, pcp, expected) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) WARN((native) != (expected), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) "raw %ld (0x%lx) != expected %lld (0x%llx)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) (native), (native), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) (long long)(expected), (long long)(expected)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) WARN(__this_cpu_read(pcp) != (expected), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) "pcp %ld (0x%lx) != expected %lld (0x%llx)", \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) __this_cpu_read(pcp), __this_cpu_read(pcp), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) (long long)(expected), (long long)(expected)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) static DEFINE_PER_CPU(long, long_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static DEFINE_PER_CPU(unsigned long, ulong_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int __init percpu_test_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * volatile prevents compiler from optimizing it uses, otherwise the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * +ul_one/-ul_one below would replace with inc/dec instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) volatile unsigned int ui_one = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) long l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) unsigned long ul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) pr_info("percpu test start\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) l += -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __this_cpu_add(long_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) CHECK(l, long_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) l += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) __this_cpu_add(long_counter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) CHECK(l, long_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __this_cpu_write(ulong_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ul += 1UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) __this_cpu_add(ulong_counter, 1UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) CHECK(ul, ulong_counter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ul += -1UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) __this_cpu_add(ulong_counter, -1UL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) CHECK(ul, ulong_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) ul += -(unsigned long)1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) __this_cpu_add(ulong_counter, -(unsigned long)1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) CHECK(ul, ulong_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) __this_cpu_write(ulong_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) ul -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) __this_cpu_dec(ulong_counter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) CHECK(ul, ulong_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) CHECK(ul, ulong_counter, ULONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) l += -ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) __this_cpu_add(long_counter, -ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) CHECK(l, long_counter, 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) l += ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) __this_cpu_add(long_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) CHECK(l, long_counter, (long)0x100000000LL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) __this_cpu_write(long_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) l -= ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) __this_cpu_sub(long_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) CHECK(l, long_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) __this_cpu_write(long_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) l += ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) __this_cpu_add(long_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) CHECK(l, long_counter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) l += -ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) __this_cpu_add(long_counter, -ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) CHECK(l, long_counter, (long)0x100000000LL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) l = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __this_cpu_write(long_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) l -= ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) this_cpu_sub(long_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) CHECK(l, long_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) CHECK(l, long_counter, ULONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) ul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) __this_cpu_write(ulong_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ul += ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) __this_cpu_add(ulong_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) CHECK(ul, ulong_counter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ul = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) __this_cpu_write(ulong_counter, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) ul -= ui_one;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __this_cpu_sub(ulong_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) CHECK(ul, ulong_counter, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) CHECK(ul, ulong_counter, ULONG_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ul = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) __this_cpu_write(ulong_counter, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ul = this_cpu_sub_return(ulong_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CHECK(ul, ulong_counter, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ul = __this_cpu_sub_return(ulong_counter, ui_one);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) CHECK(ul, ulong_counter, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pr_info("percpu test done\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return -EAGAIN; /* Fail will directly unload the module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void __exit percpu_test_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) module_init(percpu_test_init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) module_exit(percpu_test_exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MODULE_AUTHOR("Greg Thelen");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) MODULE_DESCRIPTION("percpu operations test");