Back to home page

LXR

 
 

    


0001 /*
0002  * Testsuite for atomic64_t functions
0003  *
0004  * Copyright © 2010  Luca Barbieri
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  */
0011 
0012 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0013 
0014 #include <linux/init.h>
0015 #include <linux/bug.h>
0016 #include <linux/kernel.h>
0017 #include <linux/atomic.h>
0018 
0019 #ifdef CONFIG_X86
0020 #include <asm/cpufeature.h> /* for boot_cpu_has below */
0021 #endif
0022 
0023 #define TEST(bit, op, c_op, val)                \
0024 do {                                \
0025     atomic##bit##_set(&v, v0);              \
0026     r = v0;                         \
0027     atomic##bit##_##op(val, &v);                \
0028     r c_op val;                     \
0029     WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n",   \
0030         (unsigned long long)atomic##bit##_read(&v), \
0031         (unsigned long long)r);             \
0032 } while (0)
0033 
0034 /*
0035  * Test for a atomic operation family,
0036  * @test should be a macro accepting parameters (bit, op, ...)
0037  */
0038 
0039 #define FAMILY_TEST(test, bit, op, args...) \
0040 do {                        \
0041     test(bit, op, ##args);      \
0042     test(bit, op##_acquire, ##args);    \
0043     test(bit, op##_release, ##args);    \
0044     test(bit, op##_relaxed, ##args);    \
0045 } while (0)
0046 
0047 #define TEST_RETURN(bit, op, c_op, val)             \
0048 do {                                \
0049     atomic##bit##_set(&v, v0);              \
0050     r = v0;                         \
0051     r c_op val;                     \
0052     BUG_ON(atomic##bit##_##op(val, &v) != r);       \
0053     BUG_ON(atomic##bit##_read(&v) != r);            \
0054 } while (0)
0055 
0056 #define TEST_FETCH(bit, op, c_op, val)              \
0057 do {                                \
0058     atomic##bit##_set(&v, v0);              \
0059     r = v0;                         \
0060     r c_op val;                     \
0061     BUG_ON(atomic##bit##_##op(val, &v) != v0);      \
0062     BUG_ON(atomic##bit##_read(&v) != r);            \
0063 } while (0)
0064 
0065 #define RETURN_FAMILY_TEST(bit, op, c_op, val)          \
0066 do {                                \
0067     FAMILY_TEST(TEST_RETURN, bit, op, c_op, val);       \
0068 } while (0)
0069 
0070 #define FETCH_FAMILY_TEST(bit, op, c_op, val)           \
0071 do {                                \
0072     FAMILY_TEST(TEST_FETCH, bit, op, c_op, val);        \
0073 } while (0)
0074 
0075 #define TEST_ARGS(bit, op, init, ret, expect, args...)      \
0076 do {                                \
0077     atomic##bit##_set(&v, init);                \
0078     BUG_ON(atomic##bit##_##op(&v, ##args) != ret);      \
0079     BUG_ON(atomic##bit##_read(&v) != expect);       \
0080 } while (0)
0081 
0082 #define XCHG_FAMILY_TEST(bit, init, new)                \
0083 do {                                    \
0084     FAMILY_TEST(TEST_ARGS, bit, xchg, init, init, new, new);    \
0085 } while (0)
0086 
0087 #define CMPXCHG_FAMILY_TEST(bit, init, new, wrong)          \
0088 do {                                    \
0089     FAMILY_TEST(TEST_ARGS, bit, cmpxchg,                \
0090             init, init, new, init, new);            \
0091     FAMILY_TEST(TEST_ARGS, bit, cmpxchg,                \
0092             init, init, init, wrong, new);          \
0093 } while (0)
0094 
0095 #define INC_RETURN_FAMILY_TEST(bit, i)          \
0096 do {                            \
0097     FAMILY_TEST(TEST_ARGS, bit, inc_return,     \
0098             i, (i) + one, (i) + one);   \
0099 } while (0)
0100 
0101 #define DEC_RETURN_FAMILY_TEST(bit, i)          \
0102 do {                            \
0103     FAMILY_TEST(TEST_ARGS, bit, dec_return,     \
0104             i, (i) - one, (i) - one);   \
0105 } while (0)
0106 
0107 static __init void test_atomic(void)
0108 {
0109     int v0 = 0xaaa31337;
0110     int v1 = 0xdeadbeef;
0111     int onestwos = 0x11112222;
0112     int one = 1;
0113 
0114     atomic_t v;
0115     int r;
0116 
0117     TEST(, add, +=, onestwos);
0118     TEST(, add, +=, -one);
0119     TEST(, sub, -=, onestwos);
0120     TEST(, sub, -=, -one);
0121     TEST(, or, |=, v1);
0122     TEST(, and, &=, v1);
0123     TEST(, xor, ^=, v1);
0124     TEST(, andnot, &= ~, v1);
0125 
0126     RETURN_FAMILY_TEST(, add_return, +=, onestwos);
0127     RETURN_FAMILY_TEST(, add_return, +=, -one);
0128     RETURN_FAMILY_TEST(, sub_return, -=, onestwos);
0129     RETURN_FAMILY_TEST(, sub_return, -=, -one);
0130 
0131     FETCH_FAMILY_TEST(, fetch_add, +=, onestwos);
0132     FETCH_FAMILY_TEST(, fetch_add, +=, -one);
0133     FETCH_FAMILY_TEST(, fetch_sub, -=, onestwos);
0134     FETCH_FAMILY_TEST(, fetch_sub, -=, -one);
0135 
0136     FETCH_FAMILY_TEST(, fetch_or,  |=, v1);
0137     FETCH_FAMILY_TEST(, fetch_and, &=, v1);
0138     FETCH_FAMILY_TEST(, fetch_andnot, &= ~, v1);
0139     FETCH_FAMILY_TEST(, fetch_xor, ^=, v1);
0140 
0141     INC_RETURN_FAMILY_TEST(, v0);
0142     DEC_RETURN_FAMILY_TEST(, v0);
0143 
0144     XCHG_FAMILY_TEST(, v0, v1);
0145     CMPXCHG_FAMILY_TEST(, v0, v1, onestwos);
0146 
0147 }
0148 
0149 #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
0150 static __init void test_atomic64(void)
0151 {
0152     long long v0 = 0xaaa31337c001d00dLL;
0153     long long v1 = 0xdeadbeefdeafcafeLL;
0154     long long v2 = 0xfaceabadf00df001LL;
0155     long long onestwos = 0x1111111122222222LL;
0156     long long one = 1LL;
0157 
0158     atomic64_t v = ATOMIC64_INIT(v0);
0159     long long r = v0;
0160     BUG_ON(v.counter != r);
0161 
0162     atomic64_set(&v, v1);
0163     r = v1;
0164     BUG_ON(v.counter != r);
0165     BUG_ON(atomic64_read(&v) != r);
0166 
0167     TEST(64, add, +=, onestwos);
0168     TEST(64, add, +=, -one);
0169     TEST(64, sub, -=, onestwos);
0170     TEST(64, sub, -=, -one);
0171     TEST(64, or, |=, v1);
0172     TEST(64, and, &=, v1);
0173     TEST(64, xor, ^=, v1);
0174     TEST(64, andnot, &= ~, v1);
0175 
0176     RETURN_FAMILY_TEST(64, add_return, +=, onestwos);
0177     RETURN_FAMILY_TEST(64, add_return, +=, -one);
0178     RETURN_FAMILY_TEST(64, sub_return, -=, onestwos);
0179     RETURN_FAMILY_TEST(64, sub_return, -=, -one);
0180 
0181     FETCH_FAMILY_TEST(64, fetch_add, +=, onestwos);
0182     FETCH_FAMILY_TEST(64, fetch_add, +=, -one);
0183     FETCH_FAMILY_TEST(64, fetch_sub, -=, onestwos);
0184     FETCH_FAMILY_TEST(64, fetch_sub, -=, -one);
0185 
0186     FETCH_FAMILY_TEST(64, fetch_or,  |=, v1);
0187     FETCH_FAMILY_TEST(64, fetch_and, &=, v1);
0188     FETCH_FAMILY_TEST(64, fetch_andnot, &= ~, v1);
0189     FETCH_FAMILY_TEST(64, fetch_xor, ^=, v1);
0190 
0191     INIT(v0);
0192     atomic64_inc(&v);
0193     r += one;
0194     BUG_ON(v.counter != r);
0195 
0196     INIT(v0);
0197     atomic64_dec(&v);
0198     r -= one;
0199     BUG_ON(v.counter != r);
0200 
0201     INC_RETURN_FAMILY_TEST(64, v0);
0202     DEC_RETURN_FAMILY_TEST(64, v0);
0203 
0204     XCHG_FAMILY_TEST(64, v0, v1);
0205     CMPXCHG_FAMILY_TEST(64, v0, v1, v2);
0206 
0207     INIT(v0);
0208     BUG_ON(atomic64_add_unless(&v, one, v0));
0209     BUG_ON(v.counter != r);
0210 
0211     INIT(v0);
0212     BUG_ON(!atomic64_add_unless(&v, one, v1));
0213     r += one;
0214     BUG_ON(v.counter != r);
0215 
0216     INIT(onestwos);
0217     BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
0218     r -= one;
0219     BUG_ON(v.counter != r);
0220 
0221     INIT(0);
0222     BUG_ON(atomic64_dec_if_positive(&v) != -one);
0223     BUG_ON(v.counter != r);
0224 
0225     INIT(-one);
0226     BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
0227     BUG_ON(v.counter != r);
0228 
0229     INIT(onestwos);
0230     BUG_ON(!atomic64_inc_not_zero(&v));
0231     r += one;
0232     BUG_ON(v.counter != r);
0233 
0234     INIT(0);
0235     BUG_ON(atomic64_inc_not_zero(&v));
0236     BUG_ON(v.counter != r);
0237 
0238     INIT(-one);
0239     BUG_ON(!atomic64_inc_not_zero(&v));
0240     r += one;
0241     BUG_ON(v.counter != r);
0242 }
0243 
0244 static __init int test_atomics(void)
0245 {
0246     test_atomic();
0247     test_atomic64();
0248 
0249 #ifdef CONFIG_X86
0250     pr_info("passed for %s platform %s CX8 and %s SSE\n",
0251 #ifdef CONFIG_X86_64
0252         "x86-64",
0253 #elif defined(CONFIG_X86_CMPXCHG64)
0254         "i586+",
0255 #else
0256         "i386+",
0257 #endif
0258            boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
0259            boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
0260 #else
0261     pr_info("passed\n");
0262 #endif
0263 
0264     return 0;
0265 }
0266 
0267 core_initcall(test_atomics);