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>
20 #include <asm/cpufeature.h> /* for boot_cpu_has below */
23 #define TEST(bit, op, c_op, val) \
25 atomic##bit##_set(&v, v0); \
27 atomic##bit##_##op(val, &v); \
29 WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \
30 (unsigned long long)atomic##bit##_read(&v), \
31 (unsigned long long)r); \
35 * Test for a atomic operation family,
36 * @test should be a macro accepting parameters (bit, op, ...)
39 #define FAMILY_TEST(test, bit, op, args...) \
41 test(bit, op, ##args); \
42 test(bit, op##_acquire, ##args); \
43 test(bit, op##_release, ##args); \
44 test(bit, op##_relaxed, ##args); \
47 #define TEST_RETURN(bit, op, c_op, val) \
49 atomic##bit##_set(&v, v0); \
52 BUG_ON(atomic##bit##_##op(val, &v) != r); \
53 BUG_ON(atomic##bit##_read(&v) != r); \
56 #define TEST_FETCH(bit, op, c_op, val) \
58 atomic##bit##_set(&v, v0); \
61 BUG_ON(atomic##bit##_##op(val, &v) != v0); \
62 BUG_ON(atomic##bit##_read(&v) != r); \
65 #define RETURN_FAMILY_TEST(bit, op, c_op, val) \
67 FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \
70 #define FETCH_FAMILY_TEST(bit, op, c_op, val) \
72 FAMILY_TEST(TEST_FETCH, bit, op, c_op, val); \
75 #define TEST_ARGS(bit, op, init, ret, expect, args...) \
77 atomic##bit##_set(&v, init); \
78 BUG_ON(atomic##bit##_##op(&v, ##args) != ret); \
79 BUG_ON(atomic##bit##_read(&v) != expect); \
82 #define XCHG_FAMILY_TEST(bit, init, new) \
84 FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \
87 #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \
89 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
90 init, init, new, init, new); \
91 FAMILY_TEST(TEST_ARGS, bit, cmpxchg, \
92 init, init, init, wrong, new); \
95 #define INC_RETURN_FAMILY_TEST(bit, i) \
97 FAMILY_TEST(TEST_ARGS, bit, inc_return, \
98 i, (i) + one, (i) + one); \
101 #define DEC_RETURN_FAMILY_TEST(bit, i) \
103 FAMILY_TEST(TEST_ARGS, bit, dec_return, \
104 i, (i) - one, (i) - one); \
107 static __init void test_atomic(void)
111 int onestwos = 0x11112222;
117 TEST(, add, +=, onestwos);
118 TEST(, add, +=, -one);
119 TEST(, sub, -=, onestwos);
120 TEST(, sub, -=, -one);
124 TEST(, andnot, &= ~, v1);
126 RETURN_FAMILY_TEST(, add_return, +=, onestwos);
127 RETURN_FAMILY_TEST(, add_return, +=, -one);
128 RETURN_FAMILY_TEST(, sub_return, -=, onestwos);
129 RETURN_FAMILY_TEST(, sub_return, -=, -one);
131 FETCH_FAMILY_TEST(, fetch_add, +=, onestwos);
132 FETCH_FAMILY_TEST(, fetch_add, +=, -one);
133 FETCH_FAMILY_TEST(, fetch_sub, -=, onestwos);
134 FETCH_FAMILY_TEST(, fetch_sub, -=, -one);
136 FETCH_FAMILY_TEST(, fetch_or, |=, v1);
137 FETCH_FAMILY_TEST(, fetch_and, &=, v1);
138 FETCH_FAMILY_TEST(, fetch_andnot, &= ~, v1);
139 FETCH_FAMILY_TEST(, fetch_xor, ^=, v1);
141 INC_RETURN_FAMILY_TEST(, v0);
142 DEC_RETURN_FAMILY_TEST(, v0);
144 XCHG_FAMILY_TEST(, v0, v1);
145 CMPXCHG_FAMILY_TEST(, v0, v1, onestwos);
149 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
150 static __init void test_atomic64(void)
152 long long v0 = 0xaaa31337c001d00dLL;
153 long long v1 = 0xdeadbeefdeafcafeLL;
154 long long v2 = 0xfaceabadf00df001LL;
155 long long onestwos = 0x1111111122222222LL;
158 atomic64_t v = ATOMIC64_INIT(v0);
160 BUG_ON(v.counter != r);
162 atomic64_set(&v, v1);
164 BUG_ON(v.counter != r);
165 BUG_ON(atomic64_read(&v) != r);
167 TEST(64, add, +=, onestwos);
168 TEST(64, add, +=, -one);
169 TEST(64, sub, -=, onestwos);
170 TEST(64, sub, -=, -one);
171 TEST(64, or, |=, v1);
172 TEST(64, and, &=, v1);
173 TEST(64, xor, ^=, v1);
174 TEST(64, andnot, &= ~, v1);
176 RETURN_FAMILY_TEST(64, add_return, +=, onestwos);
177 RETURN_FAMILY_TEST(64, add_return, +=, -one);
178 RETURN_FAMILY_TEST(64, sub_return, -=, onestwos);
179 RETURN_FAMILY_TEST(64, sub_return, -=, -one);
181 FETCH_FAMILY_TEST(64, fetch_add, +=, onestwos);
182 FETCH_FAMILY_TEST(64, fetch_add, +=, -one);
183 FETCH_FAMILY_TEST(64, fetch_sub, -=, onestwos);
184 FETCH_FAMILY_TEST(64, fetch_sub, -=, -one);
186 FETCH_FAMILY_TEST(64, fetch_or, |=, v1);
187 FETCH_FAMILY_TEST(64, fetch_and, &=, v1);
188 FETCH_FAMILY_TEST(64, fetch_andnot, &= ~, v1);
189 FETCH_FAMILY_TEST(64, fetch_xor, ^=, v1);
194 BUG_ON(v.counter != r);
199 BUG_ON(v.counter != r);
201 INC_RETURN_FAMILY_TEST(64, v0);
202 DEC_RETURN_FAMILY_TEST(64, v0);
204 XCHG_FAMILY_TEST(64, v0, v1);
205 CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
208 BUG_ON(atomic64_add_unless(&v, one, v0));
209 BUG_ON(v.counter != r);
212 BUG_ON(!atomic64_add_unless(&v, one, v1));
214 BUG_ON(v.counter != r);
216 #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
218 BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
220 BUG_ON(v.counter != r);
223 BUG_ON(atomic64_dec_if_positive(&v) != -one);
224 BUG_ON(v.counter != r);
227 BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
228 BUG_ON(v.counter != r);
230 #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol
234 BUG_ON(!atomic64_inc_not_zero(&v));
236 BUG_ON(v.counter != r);
239 BUG_ON(atomic64_inc_not_zero(&v));
240 BUG_ON(v.counter != r);
243 BUG_ON(!atomic64_inc_not_zero(&v));
245 BUG_ON(v.counter != r);
248 static __init int test_atomics(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 core_initcall(test_atomics);