2 * Testsuite for atomic64_t functions
4 * Copyright © 2010 Luca Barbieri
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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/init.h>
15 #include <linux/bug.h>
16 #include <linux/kernel.h>
17 #include <linux/atomic.h>
18 #include <linux/module.h>
21 #include <asm/cpufeature.h> /* for boot_cpu_has below */
24 #define TEST(bit, op, c_op, val) \
26 atomic##bit##_set(&v, v0); \
28 atomic##bit##_##op(val, &v); \
30 WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \
31 (unsigned long long)atomic##bit##_read(&v), \
32 (unsigned long long)r); \
36 * Test for a atomic operation family,
37 * @test should be a macro accepting parameters (bit, op, ...)
40 #define FAMILY_TEST(test, bit, op, args...) \
42 test(bit, op, ##args); \
43 test(bit, op##_acquire, ##args); \
44 test(bit, op##_release, ##args); \
45 test(bit, op##_relaxed, ##args); \
48 #define TEST_RETURN(bit, op, c_op, val) \
50 atomic##bit##_set(&v, v0); \
53 BUG_ON(atomic##bit##_##op(val, &v) != r); \
54 BUG_ON(atomic##bit##_read(&v) != r); \
57 #define TEST_FETCH(bit, op, c_op, val) \
59 atomic##bit##_set(&v, v0); \
62 BUG_ON(atomic##bit##_##op(val, &v) != v0); \
63 BUG_ON(atomic##bit##_read(&v) != r); \
66 #define RETURN_FAMILY_TEST(bit, op, c_op, val) \
68 FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \
71 #define FETCH_FAMILY_TEST(bit, op, c_op, val) \
73 FAMILY_TEST(TEST_FETCH, bit, op, c_op, val); \
76 #define TEST_ARGS(bit, op, init, ret, expect, args...) \
78 atomic##bit##_set(&v, init); \
79 BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \
80 BUG_ON(atomic##bit##_read(&v) != expect); \
83 #define XCHG_FAMILY_TEST(bit, init, new) \
85 FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \
88 #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \
90 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
91 init, init, new, init, new); \
92 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
93 init, init, init, wrong, new); \
96 #define INC_RETURN_FAMILY_TEST(bit, i) \
98 FAMILY_TEST(TEST_ARGS, bit, inc_return, \
99 i, (i) + one, (i) + one); \
102 #define DEC_RETURN_FAMILY_TEST(bit, i) \
104 FAMILY_TEST(TEST_ARGS, bit, dec_return, \
105 i, (i) - one, (i) - one); \
108 static __init void test_atomic(void)
112 int onestwos = 0x11112222;
118 TEST(, add, +=, onestwos);
119 TEST(, add, +=, -one);
120 TEST(, sub, -=, onestwos);
121 TEST(, sub, -=, -one);
125 TEST(, andnot, &= ~, v1);
127 RETURN_FAMILY_TEST(, add_return, +=, onestwos);
128 RETURN_FAMILY_TEST(, add_return, +=, -one);
129 RETURN_FAMILY_TEST(, sub_return, -=, onestwos);
130 RETURN_FAMILY_TEST(, sub_return, -=, -one);
132 FETCH_FAMILY_TEST(, fetch_add, +=, onestwos);
133 FETCH_FAMILY_TEST(, fetch_add, +=, -one);
134 FETCH_FAMILY_TEST(, fetch_sub, -=, onestwos);
135 FETCH_FAMILY_TEST(, fetch_sub, -=, -one);
137 FETCH_FAMILY_TEST(, fetch_or, |=, v1);
138 FETCH_FAMILY_TEST(, fetch_and, &=, v1);
139 FETCH_FAMILY_TEST(, fetch_andnot, &= ~, v1);
140 FETCH_FAMILY_TEST(, fetch_xor, ^=, v1);
142 INC_RETURN_FAMILY_TEST(, v0);
143 DEC_RETURN_FAMILY_TEST(, v0);
145 XCHG_FAMILY_TEST(, v0, v1);
146 CMPXCHG_FAMILY_TEST(, v0, v1, onestwos);
150 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
151 static __init void test_atomic64(void)
153 long long v0 = 0xaaa31337c001d00dLL;
154 long long v1 = 0xdeadbeefdeafcafeLL;
155 long long v2 = 0xfaceabadf00df001LL;
156 long long v3 = 0x8000000000000000LL;
157 long long onestwos = 0x1111111122222222LL;
161 atomic64_t v = ATOMIC64_INIT(v0);
163 BUG_ON(v.counter != r);
165 atomic64_set(&v, v1);
167 BUG_ON(v.counter != r);
168 BUG_ON(atomic64_read(&v) != r);
170 TEST(64, add, +=, onestwos);
171 TEST(64, add, +=, -one);
172 TEST(64, sub, -=, onestwos);
173 TEST(64, sub, -=, -one);
174 TEST(64, or, |=, v1);
175 TEST(64, and, &=, v1);
176 TEST(64, xor, ^=, v1);
177 TEST(64, andnot, &= ~, v1);
179 RETURN_FAMILY_TEST(64, add_return, +=, onestwos);
180 RETURN_FAMILY_TEST(64, add_return, +=, -one);
181 RETURN_FAMILY_TEST(64, sub_return, -=, onestwos);
182 RETURN_FAMILY_TEST(64, sub_return, -=, -one);
184 FETCH_FAMILY_TEST(64, fetch_add, +=, onestwos);
185 FETCH_FAMILY_TEST(64, fetch_add, +=, -one);
186 FETCH_FAMILY_TEST(64, fetch_sub, -=, onestwos);
187 FETCH_FAMILY_TEST(64, fetch_sub, -=, -one);
189 FETCH_FAMILY_TEST(64, fetch_or, |=, v1);
190 FETCH_FAMILY_TEST(64, fetch_and, &=, v1);
191 FETCH_FAMILY_TEST(64, fetch_andnot, &= ~, v1);
192 FETCH_FAMILY_TEST(64, fetch_xor, ^=, v1);
197 BUG_ON(v.counter != r);
202 BUG_ON(v.counter != r);
204 INC_RETURN_FAMILY_TEST(64, v0);
205 DEC_RETURN_FAMILY_TEST(64, v0);
207 XCHG_FAMILY_TEST(64, v0, v1);
208 CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
211 BUG_ON(atomic64_add_unless(&v, one, v0));
212 BUG_ON(v.counter != r);
215 BUG_ON(!atomic64_add_unless(&v, one, v1));
217 BUG_ON(v.counter != r);
220 BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
222 BUG_ON(v.counter != r);
225 BUG_ON(atomic64_dec_if_positive(&v) != -one);
226 BUG_ON(v.counter != r);
229 BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
230 BUG_ON(v.counter != r);
233 BUG_ON(!atomic64_inc_not_zero(&v));
235 BUG_ON(v.counter != r);
238 BUG_ON(atomic64_inc_not_zero(&v));
239 BUG_ON(v.counter != r);
242 BUG_ON(!atomic64_inc_not_zero(&v));
244 BUG_ON(v.counter != r);
246 /* Confirm the return value fits in an int, even if the value doesn't */
248 r_int = atomic64_inc_not_zero(&v);
252 static __init int test_atomics_init(void)
258 pr_info("passed for %s platform %s CX8 and %s SSE\n",
261 #elif defined(CONFIG_X86_CMPXCHG64)
266 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
267 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
275 static __exit void test_atomics_exit(void) {}
277 module_init(test_atomics_init);
278 module_exit(test_atomics_exit);
280 MODULE_LICENSE("GPL");