1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Testsuite for atomic64_t functions
5 * Copyright © 2010 Luca Barbieri
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/init.h>
11 #include <linux/bug.h>
12 #include <linux/kernel.h>
13 #include <linux/atomic.h>
14 #include <linux/module.h>
17 #include <asm/cpufeature.h> /* for boot_cpu_has below */
20 #define TEST(bit, op, c_op, val) \
22 atomic##bit##_set(&v, v0); \
24 atomic##bit##_##op(val, &v); \
26 WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \
27 (unsigned long long)atomic##bit##_read(&v), \
28 (unsigned long long)r); \
32 * Test for a atomic operation family,
33 * @test should be a macro accepting parameters (bit, op, ...)
36 #define FAMILY_TEST(test, bit, op, args...) \
38 test(bit, op, ##args); \
39 test(bit, op##_acquire, ##args); \
40 test(bit, op##_release, ##args); \
41 test(bit, op##_relaxed, ##args); \
44 #define TEST_RETURN(bit, op, c_op, val) \
46 atomic##bit##_set(&v, v0); \
49 BUG_ON(atomic##bit##_##op(val, &v) != r); \
50 BUG_ON(atomic##bit##_read(&v) != r); \
53 #define TEST_FETCH(bit, op, c_op, val) \
55 atomic##bit##_set(&v, v0); \
58 BUG_ON(atomic##bit##_##op(val, &v) != v0); \
59 BUG_ON(atomic##bit##_read(&v) != r); \
62 #define RETURN_FAMILY_TEST(bit, op, c_op, val) \
64 FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \
67 #define FETCH_FAMILY_TEST(bit, op, c_op, val) \
69 FAMILY_TEST(TEST_FETCH, bit, op, c_op, val); \
72 #define TEST_ARGS(bit, op, init, ret, expect, args...) \
74 atomic##bit##_set(&v, init); \
75 BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \
76 BUG_ON(atomic##bit##_read(&v) != expect); \
79 #define XCHG_FAMILY_TEST(bit, init, new) \
81 FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \
84 #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \
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); \
92 #define INC_RETURN_FAMILY_TEST(bit, i) \
94 FAMILY_TEST(TEST_ARGS, bit, inc_return, \
95 i, (i) + one, (i) + one); \
98 #define DEC_RETURN_FAMILY_TEST(bit, i) \
100 FAMILY_TEST(TEST_ARGS, bit, dec_return, \
101 i, (i) - one, (i) - one); \
104 static __init void test_atomic(void)
108 int onestwos = 0x11112222;
114 TEST(, add, +=, onestwos);
115 TEST(, add, +=, -one);
116 TEST(, sub, -=, onestwos);
117 TEST(, sub, -=, -one);
121 TEST(, andnot, &= ~, v1);
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);
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);
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);
138 INC_RETURN_FAMILY_TEST(, v0);
139 DEC_RETURN_FAMILY_TEST(, v0);
141 XCHG_FAMILY_TEST(, v0, v1);
142 CMPXCHG_FAMILY_TEST(, v0, v1, onestwos);
146 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
147 static __init void test_atomic64(void)
149 long long v0 = 0xaaa31337c001d00dLL;
150 long long v1 = 0xdeadbeefdeafcafeLL;
151 long long v2 = 0xfaceabadf00df001LL;
152 long long v3 = 0x8000000000000000LL;
153 long long onestwos = 0x1111111122222222LL;
157 atomic64_t v = ATOMIC64_INIT(v0);
159 BUG_ON(v.counter != r);
161 atomic64_set(&v, v1);
163 BUG_ON(v.counter != r);
164 BUG_ON(atomic64_read(&v) != r);
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);
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);
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);
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);
193 BUG_ON(v.counter != r);
198 BUG_ON(v.counter != r);
200 INC_RETURN_FAMILY_TEST(64, v0);
201 DEC_RETURN_FAMILY_TEST(64, v0);
203 XCHG_FAMILY_TEST(64, v0, v1);
204 CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
207 BUG_ON(atomic64_add_unless(&v, one, v0));
208 BUG_ON(v.counter != r);
211 BUG_ON(!atomic64_add_unless(&v, one, v1));
213 BUG_ON(v.counter != r);
216 BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
218 BUG_ON(v.counter != r);
221 BUG_ON(atomic64_dec_if_positive(&v) != -one);
222 BUG_ON(v.counter != r);
225 BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
226 BUG_ON(v.counter != r);
229 BUG_ON(!atomic64_inc_not_zero(&v));
231 BUG_ON(v.counter != r);
234 BUG_ON(atomic64_inc_not_zero(&v));
235 BUG_ON(v.counter != r);
238 BUG_ON(!atomic64_inc_not_zero(&v));
240 BUG_ON(v.counter != r);
242 /* Confirm the return value fits in an int, even if the value doesn't */
244 r_int = atomic64_inc_not_zero(&v);
248 static __init int test_atomics_init(void)
254 pr_info("passed for %s platform %s CX8 and %s SSE\n",
257 #elif defined(CONFIG_X86_CMPXCHG64)
262 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
263 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
271 static __exit void test_atomics_exit(void) {}
273 module_init(test_atomics_init);
274 module_exit(test_atomics_exit);
276 MODULE_LICENSE("GPL");