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