blob: d9d170238165a99f89c679ae275561031e42f849 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Luca Barbieri86a89382010-02-24 10:54:24 +01002/*
3 * Testsuite for atomic64_t functions
4 *
5 * Copyright © 2010 Luca Barbieri
Luca Barbieri86a89382010-02-24 10:54:24 +01006 */
Fabian Frederickb3b16d22014-06-04 16:12:00 -07007
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
Luca Barbieri86a89382010-02-24 10:54:24 +010010#include <linux/init.h>
Paul Gortmaker50af5ea2012-01-20 18:35:53 -050011#include <linux/bug.h>
Peter Huewe0dbdd1b2010-05-24 12:13:20 -070012#include <linux/kernel.h>
Arun Sharma600634972011-07-26 16:09:06 -070013#include <linux/atomic.h>
Geert Uytterhoeven55ded952017-02-24 15:00:55 -080014#include <linux/module.h>
Luca Barbieri86a89382010-02-24 10:54:24 +010015
Andi Kleen153a4332015-12-01 17:00:57 -080016#ifdef CONFIG_X86
Borislav Petkovcd4d09e2016-01-26 22:12:04 +010017#include <asm/cpufeature.h> /* for boot_cpu_has below */
Andi Kleen153a4332015-12-01 17:00:57 -080018#endif
19
Peter Zijlstra41b9e9f2015-07-13 12:55:58 +020020#define TEST(bit, op, c_op, val) \
21do { \
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 Feng978e5a32015-11-04 18:52:45 +080031/*
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...) \
37do { \
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) \
45do { \
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 Zijlstra28aa2bd2016-04-18 00:54:38 +020053#define TEST_FETCH(bit, op, c_op, val) \
54do { \
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 Feng978e5a32015-11-04 18:52:45 +080062#define RETURN_FAMILY_TEST(bit, op, c_op, val) \
63do { \
64 FAMILY_TEST(TEST_RETURN, bit, op, c_op, val); \
65} while (0)
66
Peter Zijlstra28aa2bd2016-04-18 00:54:38 +020067#define FETCH_FAMILY_TEST(bit, op, c_op, val) \
68do { \
69 FAMILY_TEST(TEST_FETCH, bit, op, c_op, val); \
70} while (0)
71
Boqun Feng978e5a32015-11-04 18:52:45 +080072#define TEST_ARGS(bit, op, init, ret, expect, args...) \
73do { \
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) \
80do { \
81 FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new); \
82} while (0)
83
84#define CMPXCHG_FAMILY_TEST(bit, init, new, wrong) \
85do { \
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) \
93do { \
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) \
99do { \
100 FAMILY_TEST(TEST_ARGS, bit, dec_return, \
101 i, (i) - one, (i) - one); \
102} while (0)
103
Peter Zijlstra41b9e9f2015-07-13 12:55:58 +0200104static __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 Feng978e5a32015-11-04 18:52:45 +0800122
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 Zijlstra28aa2bd2016-04-18 00:54:38 +0200128 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 Feng978e5a32015-11-04 18:52:45 +0800138 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 Zijlstra41b9e9f2015-07-13 12:55:58 +0200144}
145
Luca Barbieri86a89382010-02-24 10:54:24 +0100146#define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
Peter Zijlstra41b9e9f2015-07-13 12:55:58 +0200147static __init void test_atomic64(void)
Luca Barbieri86a89382010-02-24 10:54:24 +0100148{
149 long long v0 = 0xaaa31337c001d00dLL;
150 long long v1 = 0xdeadbeefdeafcafeLL;
151 long long v2 = 0xfaceabadf00df001LL;
Michael Ellermanffba19c2017-07-14 14:49:41 -0700152 long long v3 = 0x8000000000000000LL;
Luca Barbieri86a89382010-02-24 10:54:24 +0100153 long long onestwos = 0x1111111122222222LL;
154 long long one = 1LL;
Michael Ellermanffba19c2017-07-14 14:49:41 -0700155 int r_int;
Luca Barbieri86a89382010-02-24 10:54:24 +0100156
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 Zijlstra41b9e9f2015-07-13 12:55:58 +0200166 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 Barbieri86a89382010-02-24 10:54:24 +0100174
Boqun Feng978e5a32015-11-04 18:52:45 +0800175 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 Barbieri86a89382010-02-24 10:54:24 +0100179
Peter Zijlstra28aa2bd2016-04-18 00:54:38 +0200180 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 Barbieri86a89382010-02-24 10:54:24 +0100190 INIT(v0);
191 atomic64_inc(&v);
192 r += one;
193 BUG_ON(v.counter != r);
194
195 INIT(v0);
Luca Barbieri86a89382010-02-24 10:54:24 +0100196 atomic64_dec(&v);
197 r -= one;
198 BUG_ON(v.counter != r);
199
Boqun Feng978e5a32015-11-04 18:52:45 +0800200 INC_RETURN_FAMILY_TEST(64, v0);
201 DEC_RETURN_FAMILY_TEST(64, v0);
Luca Barbieri86a89382010-02-24 10:54:24 +0100202
Boqun Feng978e5a32015-11-04 18:52:45 +0800203 XCHG_FAMILY_TEST(64, v0, v1);
204 CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
Luca Barbieri86a89382010-02-24 10:54:24 +0100205
206 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100207 BUG_ON(atomic64_add_unless(&v, one, v0));
Luca Barbieri86a89382010-02-24 10:54:24 +0100208 BUG_ON(v.counter != r);
209
210 INIT(v0);
Luca Barbieri9efbcd52010-03-01 19:55:45 +0100211 BUG_ON(!atomic64_add_unless(&v, one, v1));
Luca Barbieri86a89382010-02-24 10:54:24 +0100212 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 Barbieri86a89382010-02-24 10:54:24 +0100227
228 INIT(onestwos);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100229 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100230 r += one;
231 BUG_ON(v.counter != r);
232
233 INIT(0);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100234 BUG_ON(atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100235 BUG_ON(v.counter != r);
236
237 INIT(-one);
Luca Barbieri25a304f2010-03-01 19:55:48 +0100238 BUG_ON(!atomic64_inc_not_zero(&v));
Luca Barbieri86a89382010-02-24 10:54:24 +0100239 r += one;
240 BUG_ON(v.counter != r);
Michael Ellermanffba19c2017-07-14 14:49:41 -0700241
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 Zijlstra41b9e9f2015-07-13 12:55:58 +0200246}
247
Geert Uytterhoeven55ded952017-02-24 15:00:55 -0800248static __init int test_atomics_init(void)
Peter Zijlstra41b9e9f2015-07-13 12:55:58 +0200249{
250 test_atomic();
251 test_atomic64();
Luca Barbieri86a89382010-02-24 10:54:24 +0100252
253#ifdef CONFIG_X86
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700254 pr_info("passed for %s platform %s CX8 and %s SSE\n",
H. Peter Anvina5c91612010-03-01 11:49:23 -0800255#ifdef CONFIG_X86_64
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700256 "x86-64",
H. Peter Anvina5c91612010-03-01 11:49:23 -0800257#elif defined(CONFIG_X86_CMPXCHG64)
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700258 "i586+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100259#else
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700260 "i386+",
Luca Barbieri86a89382010-02-24 10:54:24 +0100261#endif
H. Peter Anvina5c91612010-03-01 11:49:23 -0800262 boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
263 boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
Luca Barbieri86a89382010-02-24 10:54:24 +0100264#else
Fabian Frederickb3b16d22014-06-04 16:12:00 -0700265 pr_info("passed\n");
Luca Barbieri86a89382010-02-24 10:54:24 +0100266#endif
267
268 return 0;
269}
270
Geert Uytterhoeven55ded952017-02-24 15:00:55 -0800271static __exit void test_atomics_exit(void) {}
272
273module_init(test_atomics_init);
274module_exit(test_atomics_exit);
275
276MODULE_LICENSE("GPL");