atomic64_test.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Testsuite for atomic64_t functions
  3. *
  4. * Copyright © 2010 Luca Barbieri
  5. *
  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.
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/init.h>
  13. #include <linux/bug.h>
  14. #include <linux/kernel.h>
  15. #include <linux/atomic.h>
  16. #define TEST(bit, op, c_op, val) \
  17. do { \
  18. atomic##bit##_set(&v, v0); \
  19. r = v0; \
  20. atomic##bit##_##op(val, &v); \
  21. r c_op val; \
  22. WARN(atomic##bit##_read(&v) != r, "%Lx != %Lx\n", \
  23. (unsigned long long)atomic##bit##_read(&v), \
  24. (unsigned long long)r); \
  25. } while (0)
  26. static __init void test_atomic(void)
  27. {
  28. int v0 = 0xaaa31337;
  29. int v1 = 0xdeadbeef;
  30. int onestwos = 0x11112222;
  31. int one = 1;
  32. atomic_t v;
  33. int r;
  34. TEST(, add, +=, onestwos);
  35. TEST(, add, +=, -one);
  36. TEST(, sub, -=, onestwos);
  37. TEST(, sub, -=, -one);
  38. TEST(, or, |=, v1);
  39. TEST(, and, &=, v1);
  40. TEST(, xor, ^=, v1);
  41. TEST(, andnot, &= ~, v1);
  42. }
  43. #define INIT(c) do { atomic64_set(&v, c); r = c; } while (0)
  44. static __init void test_atomic64(void)
  45. {
  46. long long v0 = 0xaaa31337c001d00dLL;
  47. long long v1 = 0xdeadbeefdeafcafeLL;
  48. long long v2 = 0xfaceabadf00df001LL;
  49. long long onestwos = 0x1111111122222222LL;
  50. long long one = 1LL;
  51. atomic64_t v = ATOMIC64_INIT(v0);
  52. long long r = v0;
  53. BUG_ON(v.counter != r);
  54. atomic64_set(&v, v1);
  55. r = v1;
  56. BUG_ON(v.counter != r);
  57. BUG_ON(atomic64_read(&v) != r);
  58. TEST(64, add, +=, onestwos);
  59. TEST(64, add, +=, -one);
  60. TEST(64, sub, -=, onestwos);
  61. TEST(64, sub, -=, -one);
  62. TEST(64, or, |=, v1);
  63. TEST(64, and, &=, v1);
  64. TEST(64, xor, ^=, v1);
  65. TEST(64, andnot, &= ~, v1);
  66. INIT(v0);
  67. r += onestwos;
  68. BUG_ON(atomic64_add_return(onestwos, &v) != r);
  69. BUG_ON(v.counter != r);
  70. INIT(v0);
  71. r += -one;
  72. BUG_ON(atomic64_add_return(-one, &v) != r);
  73. BUG_ON(v.counter != r);
  74. INIT(v0);
  75. r -= onestwos;
  76. BUG_ON(atomic64_sub_return(onestwos, &v) != r);
  77. BUG_ON(v.counter != r);
  78. INIT(v0);
  79. r -= -one;
  80. BUG_ON(atomic64_sub_return(-one, &v) != r);
  81. BUG_ON(v.counter != r);
  82. INIT(v0);
  83. atomic64_inc(&v);
  84. r += one;
  85. BUG_ON(v.counter != r);
  86. INIT(v0);
  87. r += one;
  88. BUG_ON(atomic64_inc_return(&v) != r);
  89. BUG_ON(v.counter != r);
  90. INIT(v0);
  91. atomic64_dec(&v);
  92. r -= one;
  93. BUG_ON(v.counter != r);
  94. INIT(v0);
  95. r -= one;
  96. BUG_ON(atomic64_dec_return(&v) != r);
  97. BUG_ON(v.counter != r);
  98. INIT(v0);
  99. BUG_ON(atomic64_xchg(&v, v1) != v0);
  100. r = v1;
  101. BUG_ON(v.counter != r);
  102. INIT(v0);
  103. BUG_ON(atomic64_cmpxchg(&v, v0, v1) != v0);
  104. r = v1;
  105. BUG_ON(v.counter != r);
  106. INIT(v0);
  107. BUG_ON(atomic64_cmpxchg(&v, v2, v1) != v0);
  108. BUG_ON(v.counter != r);
  109. INIT(v0);
  110. BUG_ON(atomic64_add_unless(&v, one, v0));
  111. BUG_ON(v.counter != r);
  112. INIT(v0);
  113. BUG_ON(!atomic64_add_unless(&v, one, v1));
  114. r += one;
  115. BUG_ON(v.counter != r);
  116. #ifdef CONFIG_ARCH_HAS_ATOMIC64_DEC_IF_POSITIVE
  117. INIT(onestwos);
  118. BUG_ON(atomic64_dec_if_positive(&v) != (onestwos - 1));
  119. r -= one;
  120. BUG_ON(v.counter != r);
  121. INIT(0);
  122. BUG_ON(atomic64_dec_if_positive(&v) != -one);
  123. BUG_ON(v.counter != r);
  124. INIT(-one);
  125. BUG_ON(atomic64_dec_if_positive(&v) != (-one - one));
  126. BUG_ON(v.counter != r);
  127. #else
  128. #warning Please implement atomic64_dec_if_positive for your architecture and select the above Kconfig symbol
  129. #endif
  130. INIT(onestwos);
  131. BUG_ON(!atomic64_inc_not_zero(&v));
  132. r += one;
  133. BUG_ON(v.counter != r);
  134. INIT(0);
  135. BUG_ON(atomic64_inc_not_zero(&v));
  136. BUG_ON(v.counter != r);
  137. INIT(-one);
  138. BUG_ON(!atomic64_inc_not_zero(&v));
  139. r += one;
  140. BUG_ON(v.counter != r);
  141. }
  142. static __init int test_atomics(void)
  143. {
  144. test_atomic();
  145. test_atomic64();
  146. #ifdef CONFIG_X86
  147. pr_info("passed for %s platform %s CX8 and %s SSE\n",
  148. #ifdef CONFIG_X86_64
  149. "x86-64",
  150. #elif defined(CONFIG_X86_CMPXCHG64)
  151. "i586+",
  152. #else
  153. "i386+",
  154. #endif
  155. boot_cpu_has(X86_FEATURE_CX8) ? "with" : "without",
  156. boot_cpu_has(X86_FEATURE_XMM) ? "with" : "without");
  157. #else
  158. pr_info("passed\n");
  159. #endif
  160. return 0;
  161. }
  162. core_initcall(test_atomics);