Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Testsuite for atomic64_t functions |
| 4 | * |
| 5 | * Copyright © 2010 Luca Barbieri |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 6 | */ |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 7 | |
| 8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 9 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 10 | #include <linux/init.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 11 | #include <linux/bug.h> |
Peter Huewe | 0dbdd1b | 2010-05-24 12:13:20 -0700 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 13 | #include <linux/atomic.h> |
Geert Uytterhoeven | 55ded95 | 2017-02-24 15:00:55 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 15 | |
Andi Kleen | 153a433 | 2015-12-01 17:00:57 -0800 | [diff] [blame] | 16 | #ifdef CONFIG_X86 |
Borislav Petkov | cd4d09e | 2016-01-26 22:12:04 +0100 | [diff] [blame] | 17 | #include <asm/cpufeature.h> /* for boot_cpu_has below */ |
Andi Kleen | 153a433 | 2015-12-01 17:00:57 -0800 | [diff] [blame] | 18 | #endif |
| 19 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 20 | #define TEST(bit, op, c_op, val) \ |
| 21 | do { \ |
| 22 | atomic##bit##_set(&v, v0); \ |
| 23 | r = v0; \ |
| 24 | atomic##bit##_##op(val, &v); \ |
| 25 | r c_op val; \ |
| 26 | WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \ |
| 27 | (unsigned long long)atomic##bit##_read(&v), \ |
| 28 | (unsigned long long)r); \ |
| 29 | } while (0) |
| 30 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 31 | /* |
| 32 | * Test for a atomic operation family, |
| 33 | * @test should be a macro accepting parameters (bit, op, ...) |
| 34 | */ |
| 35 | |
| 36 | #define FAMILY_TEST(test, bit, op, args...) \ |
| 37 | do { \ |
| 38 | test(bit, op, ##args); \ |
| 39 | test(bit, op##_acquire, ##args); \ |
| 40 | test(bit, op##_release, ##args); \ |
| 41 | test(bit, op##_relaxed, ##args); \ |
| 42 | } while (0) |
| 43 | |
| 44 | #define TEST_RETURN(bit, op, c_op, val) \ |
| 45 | do { \ |
| 46 | atomic##bit##_set(&v, v0); \ |
| 47 | r = v0; \ |
| 48 | r c_op val; \ |
| 49 | BUG_ON(atomic##bit##_##op(val, &v) != r); \ |
| 50 | BUG_ON(atomic##bit##_read(&v) != r); \ |
| 51 | } while (0) |
| 52 | |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 53 | #define TEST_FETCH(bit, op, c_op, val) \ |
| 54 | do { \ |
| 55 | atomic##bit##_set(&v, v0); \ |
| 56 | r = v0; \ |
| 57 | r c_op val; \ |
| 58 | BUG_ON(atomic##bit##_##op(val, &v) != v0); \ |
| 59 | BUG_ON(atomic##bit##_read(&v) != r); \ |
| 60 | } while (0) |
| 61 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 62 | #define RETURN_FAMILY_TEST(bit, op, c_op, val) \ |
| 63 | do { \ |
| 64 | FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \ |
| 65 | } while (0) |
| 66 | |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 67 | #define FETCH_FAMILY_TEST(bit, op, c_op, val) \ |
| 68 | do { \ |
| 69 | FAMILY_TEST(TEST_FETCH, bit, op, c_op, val); \ |
| 70 | } while (0) |
| 71 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 72 | #define TEST_ARGS(bit, op, init, ret, expect, args...) \ |
| 73 | do { \ |
| 74 | atomic##bit##_set(&v, init); \ |
| 75 | BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \ |
| 76 | BUG_ON(atomic##bit##_read(&v) != expect); \ |
| 77 | } while (0) |
| 78 | |
| 79 | #define XCHG_FAMILY_TEST(bit, init, new) \ |
| 80 | do { \ |
| 81 | FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \ |
| 82 | } while (0) |
| 83 | |
| 84 | #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \ |
| 85 | do { \ |
| 86 | FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ |
| 87 | init, init, new, init, new); \ |
| 88 | FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ |
| 89 | init, init, init, wrong, new); \ |
| 90 | } while (0) |
| 91 | |
| 92 | #define INC_RETURN_FAMILY_TEST(bit, i) \ |
| 93 | do { \ |
| 94 | FAMILY_TEST(TEST_ARGS, bit, inc_return, \ |
| 95 | i, (i) + one, (i) + one); \ |
| 96 | } while (0) |
| 97 | |
| 98 | #define DEC_RETURN_FAMILY_TEST(bit, i) \ |
| 99 | do { \ |
| 100 | FAMILY_TEST(TEST_ARGS, bit, dec_return, \ |
| 101 | i, (i) - one, (i) - one); \ |
| 102 | } while (0) |
| 103 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 104 | static __init void test_atomic(void) |
| 105 | { |
| 106 | int v0 = 0xaaa31337; |
| 107 | int v1 = 0xdeadbeef; |
| 108 | int onestwos = 0x11112222; |
| 109 | int one = 1; |
| 110 | |
| 111 | atomic_t v; |
| 112 | int r; |
| 113 | |
| 114 | TEST(, add, +=, onestwos); |
| 115 | TEST(, add, +=, -one); |
| 116 | TEST(, sub, -=, onestwos); |
| 117 | TEST(, sub, -=, -one); |
| 118 | TEST(, or, |=, v1); |
| 119 | TEST(, and, &=, v1); |
| 120 | TEST(, xor, ^=, v1); |
| 121 | TEST(, andnot, &= ~, v1); |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 122 | |
| 123 | RETURN_FAMILY_TEST(, add_return, +=, onestwos); |
| 124 | RETURN_FAMILY_TEST(, add_return, +=, -one); |
| 125 | RETURN_FAMILY_TEST(, sub_return, -=, onestwos); |
| 126 | RETURN_FAMILY_TEST(, sub_return, -=, -one); |
| 127 | |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 128 | FETCH_FAMILY_TEST(, fetch_add, +=, onestwos); |
| 129 | FETCH_FAMILY_TEST(, fetch_add, +=, -one); |
| 130 | FETCH_FAMILY_TEST(, fetch_sub, -=, onestwos); |
| 131 | FETCH_FAMILY_TEST(, fetch_sub, -=, -one); |
| 132 | |
| 133 | FETCH_FAMILY_TEST(, fetch_or, |=, v1); |
| 134 | FETCH_FAMILY_TEST(, fetch_and, &=, v1); |
| 135 | FETCH_FAMILY_TEST(, fetch_andnot, &= ~, v1); |
| 136 | FETCH_FAMILY_TEST(, fetch_xor, ^=, v1); |
| 137 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 138 | INC_RETURN_FAMILY_TEST(, v0); |
| 139 | DEC_RETURN_FAMILY_TEST(, v0); |
| 140 | |
| 141 | XCHG_FAMILY_TEST(, v0, v1); |
| 142 | CMPXCHG_FAMILY_TEST(, v0, v1, onestwos); |
| 143 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 146 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 147 | static __init void test_atomic64(void) |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 148 | { |
| 149 | long long v0 = 0xaaa31337c001d00dLL; |
| 150 | long long v1 = 0xdeadbeefdeafcafeLL; |
| 151 | long long v2 = 0xfaceabadf00df001LL; |
Michael Ellerman | ffba19c | 2017-07-14 14:49:41 -0700 | [diff] [blame] | 152 | long long v3 = 0x8000000000000000LL; |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 153 | long long onestwos = 0x1111111122222222LL; |
| 154 | long long one = 1LL; |
Michael Ellerman | ffba19c | 2017-07-14 14:49:41 -0700 | [diff] [blame] | 155 | int r_int; |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 156 | |
| 157 | atomic64_t v = ATOMIC64_INIT(v0); |
| 158 | long long r = v0; |
| 159 | BUG_ON(v.counter != r); |
| 160 | |
| 161 | atomic64_set(&v, v1); |
| 162 | r = v1; |
| 163 | BUG_ON(v.counter != r); |
| 164 | BUG_ON(atomic64_read(&v) != r); |
| 165 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 166 | TEST(64, add, +=, onestwos); |
| 167 | TEST(64, add, +=, -one); |
| 168 | TEST(64, sub, -=, onestwos); |
| 169 | TEST(64, sub, -=, -one); |
| 170 | TEST(64, or, |=, v1); |
| 171 | TEST(64, and, &=, v1); |
| 172 | TEST(64, xor, ^=, v1); |
| 173 | TEST(64, andnot, &= ~, v1); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 174 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 175 | RETURN_FAMILY_TEST(64, add_return, +=, onestwos); |
| 176 | RETURN_FAMILY_TEST(64, add_return, +=, -one); |
| 177 | RETURN_FAMILY_TEST(64, sub_return, -=, onestwos); |
| 178 | RETURN_FAMILY_TEST(64, sub_return, -=, -one); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 179 | |
Peter Zijlstra | 28aa2bd | 2016-04-18 00:54:38 +0200 | [diff] [blame] | 180 | FETCH_FAMILY_TEST(64, fetch_add, +=, onestwos); |
| 181 | FETCH_FAMILY_TEST(64, fetch_add, +=, -one); |
| 182 | FETCH_FAMILY_TEST(64, fetch_sub, -=, onestwos); |
| 183 | FETCH_FAMILY_TEST(64, fetch_sub, -=, -one); |
| 184 | |
| 185 | FETCH_FAMILY_TEST(64, fetch_or, |=, v1); |
| 186 | FETCH_FAMILY_TEST(64, fetch_and, &=, v1); |
| 187 | FETCH_FAMILY_TEST(64, fetch_andnot, &= ~, v1); |
| 188 | FETCH_FAMILY_TEST(64, fetch_xor, ^=, v1); |
| 189 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 190 | INIT(v0); |
| 191 | atomic64_inc(&v); |
| 192 | r += one; |
| 193 | BUG_ON(v.counter != r); |
| 194 | |
| 195 | INIT(v0); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 196 | atomic64_dec(&v); |
| 197 | r -= one; |
| 198 | BUG_ON(v.counter != r); |
| 199 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 200 | INC_RETURN_FAMILY_TEST(64, v0); |
| 201 | DEC_RETURN_FAMILY_TEST(64, v0); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 202 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame] | 203 | XCHG_FAMILY_TEST(64, v0, v1); |
| 204 | CMPXCHG_FAMILY_TEST(64, v0, v1, v2); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 205 | |
| 206 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 207 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 208 | BUG_ON(v.counter != r); |
| 209 | |
| 210 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 211 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 212 | r += one; |
| 213 | BUG_ON(v.counter != r); |
| 214 | |
| 215 | INIT(onestwos); |
| 216 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); |
| 217 | r -= one; |
| 218 | BUG_ON(v.counter != r); |
| 219 | |
| 220 | INIT(0); |
| 221 | BUG_ON(atomic64_dec_if_positive(&v) != -one); |
| 222 | BUG_ON(v.counter != r); |
| 223 | |
| 224 | INIT(-one); |
| 225 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); |
| 226 | BUG_ON(v.counter != r); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 227 | |
| 228 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 229 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 230 | r += one; |
| 231 | BUG_ON(v.counter != r); |
| 232 | |
| 233 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 234 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 235 | BUG_ON(v.counter != r); |
| 236 | |
| 237 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 238 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 239 | r += one; |
| 240 | BUG_ON(v.counter != r); |
Michael Ellerman | ffba19c | 2017-07-14 14:49:41 -0700 | [diff] [blame] | 241 | |
| 242 | /* Confirm the return value fits in an int, even if the value doesn't */ |
| 243 | INIT(v3); |
| 244 | r_int = atomic64_inc_not_zero(&v); |
| 245 | BUG_ON(!r_int); |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Geert Uytterhoeven | 55ded95 | 2017-02-24 15:00:55 -0800 | [diff] [blame] | 248 | static __init int test_atomics_init(void) |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 249 | { |
| 250 | test_atomic(); |
| 251 | test_atomic64(); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 252 | |
| 253 | #ifdef CONFIG_X86 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 254 | pr_info("passed for %s platform %s CX8 and %s SSE\n", |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 255 | #ifdef CONFIG_X86_64 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 256 | "x86-64", |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 257 | #elif defined(CONFIG_X86_CMPXCHG64) |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 258 | "i586+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 259 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 260 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 261 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 262 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 263 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 264 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 265 | pr_info("passed\n"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 266 | #endif |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Geert Uytterhoeven | 55ded95 | 2017-02-24 15:00:55 -0800 | [diff] [blame] | 271 | static __exit void test_atomics_exit(void) {} |
| 272 | |
| 273 | module_init(test_atomics_init); |
| 274 | module_exit(test_atomics_exit); |
| 275 | |
| 276 | MODULE_LICENSE("GPL"); |