atomic64_test.c 4.1 KB

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