Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Testsuite for atomic64_t functions |
| 3 | * |
| 4 | * Copyright © 2010 Luca Barbieri |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 11 | |
| 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 14 | #include <linux/init.h> |
Paul Gortmaker | 50af5ea | 2012-01-20 18:35:53 -0500 | [diff] [blame] | 15 | #include <linux/bug.h> |
Peter Huewe | 0dbdd1b | 2010-05-24 12:13:20 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 17 | #include <linux/atomic.h> |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 18 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 19 | #define TEST(bit, op, c_op, val) \ |
| 20 | do { \ |
| 21 | atomic##bit##_set(&v, v0); \ |
| 22 | r = v0; \ |
| 23 | atomic##bit##_##op(val, &v); \ |
| 24 | r c_op val; \ |
| 25 | WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \ |
| 26 | (unsigned long long)atomic##bit##_read(&v), \ |
| 27 | (unsigned long long)r); \ |
| 28 | } while (0) |
| 29 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame^] | 30 | /* |
| 31 | * Test for a atomic operation family, |
| 32 | * @test should be a macro accepting parameters (bit, op, ...) |
| 33 | */ |
| 34 | |
| 35 | #define FAMILY_TEST(test, bit, op, args...) \ |
| 36 | do { \ |
| 37 | test(bit, op, ##args); \ |
| 38 | test(bit, op##_acquire, ##args); \ |
| 39 | test(bit, op##_release, ##args); \ |
| 40 | test(bit, op##_relaxed, ##args); \ |
| 41 | } while (0) |
| 42 | |
| 43 | #define TEST_RETURN(bit, op, c_op, val) \ |
| 44 | do { \ |
| 45 | atomic##bit##_set(&v, v0); \ |
| 46 | r = v0; \ |
| 47 | r c_op val; \ |
| 48 | BUG_ON(atomic##bit##_##op(val, &v) != r); \ |
| 49 | BUG_ON(atomic##bit##_read(&v) != r); \ |
| 50 | } while (0) |
| 51 | |
| 52 | #define RETURN_FAMILY_TEST(bit, op, c_op, val) \ |
| 53 | do { \ |
| 54 | FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \ |
| 55 | } while (0) |
| 56 | |
| 57 | #define TEST_ARGS(bit, op, init, ret, expect, args...) \ |
| 58 | do { \ |
| 59 | atomic##bit##_set(&v, init); \ |
| 60 | BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \ |
| 61 | BUG_ON(atomic##bit##_read(&v) != expect); \ |
| 62 | } while (0) |
| 63 | |
| 64 | #define XCHG_FAMILY_TEST(bit, init, new) \ |
| 65 | do { \ |
| 66 | FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \ |
| 67 | } while (0) |
| 68 | |
| 69 | #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \ |
| 70 | do { \ |
| 71 | FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ |
| 72 | init, init, new, init, new); \ |
| 73 | FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \ |
| 74 | init, init, init, wrong, new); \ |
| 75 | } while (0) |
| 76 | |
| 77 | #define INC_RETURN_FAMILY_TEST(bit, i) \ |
| 78 | do { \ |
| 79 | FAMILY_TEST(TEST_ARGS, bit, inc_return, \ |
| 80 | i, (i) + one, (i) + one); \ |
| 81 | } while (0) |
| 82 | |
| 83 | #define DEC_RETURN_FAMILY_TEST(bit, i) \ |
| 84 | do { \ |
| 85 | FAMILY_TEST(TEST_ARGS, bit, dec_return, \ |
| 86 | i, (i) - one, (i) - one); \ |
| 87 | } while (0) |
| 88 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 89 | static __init void test_atomic(void) |
| 90 | { |
| 91 | int v0 = 0xaaa31337; |
| 92 | int v1 = 0xdeadbeef; |
| 93 | int onestwos = 0x11112222; |
| 94 | int one = 1; |
| 95 | |
| 96 | atomic_t v; |
| 97 | int r; |
| 98 | |
| 99 | TEST(, add, +=, onestwos); |
| 100 | TEST(, add, +=, -one); |
| 101 | TEST(, sub, -=, onestwos); |
| 102 | TEST(, sub, -=, -one); |
| 103 | TEST(, or, |=, v1); |
| 104 | TEST(, and, &=, v1); |
| 105 | TEST(, xor, ^=, v1); |
| 106 | TEST(, andnot, &= ~, v1); |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame^] | 107 | |
| 108 | RETURN_FAMILY_TEST(, add_return, +=, onestwos); |
| 109 | RETURN_FAMILY_TEST(, add_return, +=, -one); |
| 110 | RETURN_FAMILY_TEST(, sub_return, -=, onestwos); |
| 111 | RETURN_FAMILY_TEST(, sub_return, -=, -one); |
| 112 | |
| 113 | INC_RETURN_FAMILY_TEST(, v0); |
| 114 | DEC_RETURN_FAMILY_TEST(, v0); |
| 115 | |
| 116 | XCHG_FAMILY_TEST(, v0, v1); |
| 117 | CMPXCHG_FAMILY_TEST(, v0, v1, onestwos); |
| 118 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 121 | #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0) |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 122 | static __init void test_atomic64(void) |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 123 | { |
| 124 | long long v0 = 0xaaa31337c001d00dLL; |
| 125 | long long v1 = 0xdeadbeefdeafcafeLL; |
| 126 | long long v2 = 0xfaceabadf00df001LL; |
| 127 | long long onestwos = 0x1111111122222222LL; |
| 128 | long long one = 1LL; |
| 129 | |
| 130 | atomic64_t v = ATOMIC64_INIT(v0); |
| 131 | long long r = v0; |
| 132 | BUG_ON(v.counter != r); |
| 133 | |
| 134 | atomic64_set(&v, v1); |
| 135 | r = v1; |
| 136 | BUG_ON(v.counter != r); |
| 137 | BUG_ON(atomic64_read(&v) != r); |
| 138 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 139 | TEST(64, add, +=, onestwos); |
| 140 | TEST(64, add, +=, -one); |
| 141 | TEST(64, sub, -=, onestwos); |
| 142 | TEST(64, sub, -=, -one); |
| 143 | TEST(64, or, |=, v1); |
| 144 | TEST(64, and, &=, v1); |
| 145 | TEST(64, xor, ^=, v1); |
| 146 | TEST(64, andnot, &= ~, v1); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 147 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame^] | 148 | RETURN_FAMILY_TEST(64, add_return, +=, onestwos); |
| 149 | RETURN_FAMILY_TEST(64, add_return, +=, -one); |
| 150 | RETURN_FAMILY_TEST(64, sub_return, -=, onestwos); |
| 151 | RETURN_FAMILY_TEST(64, sub_return, -=, -one); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 152 | |
| 153 | INIT(v0); |
| 154 | atomic64_inc(&v); |
| 155 | r += one; |
| 156 | BUG_ON(v.counter != r); |
| 157 | |
| 158 | INIT(v0); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 159 | atomic64_dec(&v); |
| 160 | r -= one; |
| 161 | BUG_ON(v.counter != r); |
| 162 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame^] | 163 | INC_RETURN_FAMILY_TEST(64, v0); |
| 164 | DEC_RETURN_FAMILY_TEST(64, v0); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 165 | |
Boqun Feng | 978e5a3 | 2015-11-04 18:52:45 +0800 | [diff] [blame^] | 166 | XCHG_FAMILY_TEST(64, v0, v1); |
| 167 | CMPXCHG_FAMILY_TEST(64, v0, v1, v2); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 168 | |
| 169 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 170 | BUG_ON(atomic64_add_unless(&v, one, v0)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 171 | BUG_ON(v.counter != r); |
| 172 | |
| 173 | INIT(v0); |
Luca Barbieri | 9efbcd5 | 2010-03-01 19:55:45 +0100 | [diff] [blame] | 174 | BUG_ON(!atomic64_add_unless(&v, one, v1)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 175 | r += one; |
| 176 | BUG_ON(v.counter != r); |
| 177 | |
Catalin Marinas | 7463449 | 2012-07-30 14:41:09 -0700 | [diff] [blame] | 178 | #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 179 | INIT(onestwos); |
| 180 | BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1)); |
| 181 | r -= one; |
| 182 | BUG_ON(v.counter != r); |
| 183 | |
| 184 | INIT(0); |
| 185 | BUG_ON(atomic64_dec_if_positive(&v) != -one); |
| 186 | BUG_ON(v.counter != r); |
| 187 | |
| 188 | INIT(-one); |
| 189 | BUG_ON(atomic64_dec_if_positive(&v) != (-one - one)); |
| 190 | BUG_ON(v.counter != r); |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 191 | #else |
Catalin Marinas | 7463449 | 2012-07-30 14:41:09 -0700 | [diff] [blame] | 192 | #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol |
Luca Barbieri | 8f4f202 | 2010-02-26 12:22:40 +0100 | [diff] [blame] | 193 | #endif |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 194 | |
| 195 | INIT(onestwos); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 196 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 197 | r += one; |
| 198 | BUG_ON(v.counter != r); |
| 199 | |
| 200 | INIT(0); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 201 | BUG_ON(atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 202 | BUG_ON(v.counter != r); |
| 203 | |
| 204 | INIT(-one); |
Luca Barbieri | 25a304f | 2010-03-01 19:55:48 +0100 | [diff] [blame] | 205 | BUG_ON(!atomic64_inc_not_zero(&v)); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 206 | r += one; |
| 207 | BUG_ON(v.counter != r); |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static __init int test_atomics(void) |
| 211 | { |
| 212 | test_atomic(); |
| 213 | test_atomic64(); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 214 | |
| 215 | #ifdef CONFIG_X86 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 216 | 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] | 217 | #ifdef CONFIG_X86_64 |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 218 | "x86-64", |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 219 | #elif defined(CONFIG_X86_CMPXCHG64) |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 220 | "i586+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 221 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 222 | "i386+", |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 223 | #endif |
H. Peter Anvin | a5c9161 | 2010-03-01 11:49:23 -0800 | [diff] [blame] | 224 | boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without", |
| 225 | boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 226 | #else |
Fabian Frederick | b3b16d2 | 2014-06-04 16:12:00 -0700 | [diff] [blame] | 227 | pr_info("passed\n"); |
Luca Barbieri | 86a8938 | 2010-02-24 10:54:24 +0100 | [diff] [blame] | 228 | #endif |
| 229 | |
| 230 | return 0; |
| 231 | } |
| 232 | |
Peter Zijlstra | 41b9e9f | 2015-07-13 12:55:58 +0200 | [diff] [blame] | 233 | core_initcall(test_atomics); |