Skip to content

Commit 41b9e9f

Browse files
Peter ZijlstraKAGA-KOKO
authored andcommitted
atomic: Add simple atomic_t tests
Add a few atomic_t tests, gets some compile coverage for the new operations. Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]>
1 parent 805de8f commit 41b9e9f

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

lib/atomic64_test.c

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,39 @@
1616
#include <linux/kernel.h>
1717
#include <linux/atomic.h>
1818

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+
30+
static __init void test_atomic(void)
31+
{
32+
int v0 = 0xaaa31337;
33+
int v1 = 0xdeadbeef;
34+
int onestwos = 0x11112222;
35+
int one = 1;
36+
37+
atomic_t v;
38+
int r;
39+
40+
TEST(, add, +=, onestwos);
41+
TEST(, add, +=, -one);
42+
TEST(, sub, -=, onestwos);
43+
TEST(, sub, -=, -one);
44+
TEST(, or, |=, v1);
45+
TEST(, and, &=, v1);
46+
TEST(, xor, ^=, v1);
47+
TEST(, andnot, &= ~, v1);
48+
}
49+
1950
#define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
20-
static __init int test_atomic64(void)
51+
static __init void test_atomic64(void)
2152
{
2253
long long v0 = 0xaaa31337c001d00dLL;
2354
long long v1 = 0xdeadbeefdeafcafeLL;
@@ -34,15 +65,14 @@ static __init int test_atomic64(void)
3465
BUG_ON(v.counter != r);
3566
BUG_ON(atomic64_read(&v) != r);
3667

37-
INIT(v0);
38-
atomic64_add(onestwos, &v);
39-
r += onestwos;
40-
BUG_ON(v.counter != r);
41-
42-
INIT(v0);
43-
atomic64_add(-one, &v);
44-
r += -one;
45-
BUG_ON(v.counter != r);
68+
TEST(64, add, +=, onestwos);
69+
TEST(64, add, +=, -one);
70+
TEST(64, sub, -=, onestwos);
71+
TEST(64, sub, -=, -one);
72+
TEST(64, or, |=, v1);
73+
TEST(64, and, &=, v1);
74+
TEST(64, xor, ^=, v1);
75+
TEST(64, andnot, &= ~, v1);
4676

4777
INIT(v0);
4878
r += onestwos;
@@ -54,16 +84,6 @@ static __init int test_atomic64(void)
5484
BUG_ON(atomic64_add_return(-one, &v) != r);
5585
BUG_ON(v.counter != r);
5686

57-
INIT(v0);
58-
atomic64_sub(onestwos, &v);
59-
r -= onestwos;
60-
BUG_ON(v.counter != r);
61-
62-
INIT(v0);
63-
atomic64_sub(-one, &v);
64-
r -= -one;
65-
BUG_ON(v.counter != r);
66-
6787
INIT(v0);
6888
r -= onestwos;
6989
BUG_ON(atomic64_sub_return(onestwos, &v) != r);
@@ -147,6 +167,12 @@ static __init int test_atomic64(void)
147167
BUG_ON(!atomic64_inc_not_zero(&v));
148168
r += one;
149169
BUG_ON(v.counter != r);
170+
}
171+
172+
static __init int test_atomics(void)
173+
{
174+
test_atomic();
175+
test_atomic64();
150176

151177
#ifdef CONFIG_X86
152178
pr_info("passed for %s platform %s CX8 and %s SSE\n",
@@ -166,4 +192,4 @@ static __init int test_atomic64(void)
166192
return 0;
167193
}
168194

169-
core_initcall(test_atomic64);
195+
core_initcall(test_atomics);

0 commit comments

Comments
 (0)