prime_numbers.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #define pr_fmt(fmt) "prime numbers: " fmt "\n"
  2. #include <linux/module.h>
  3. #include <linux/mutex.h>
  4. #include <linux/prime_numbers.h>
  5. #include <linux/slab.h>
  6. #define bitmap_size(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
  7. struct primes {
  8. struct rcu_head rcu;
  9. unsigned long last, sz;
  10. unsigned long primes[];
  11. };
  12. #if BITS_PER_LONG == 64
  13. static const struct primes small_primes = {
  14. .last = 61,
  15. .sz = 64,
  16. .primes = {
  17. BIT(2) |
  18. BIT(3) |
  19. BIT(5) |
  20. BIT(7) |
  21. BIT(11) |
  22. BIT(13) |
  23. BIT(17) |
  24. BIT(19) |
  25. BIT(23) |
  26. BIT(29) |
  27. BIT(31) |
  28. BIT(37) |
  29. BIT(41) |
  30. BIT(43) |
  31. BIT(47) |
  32. BIT(53) |
  33. BIT(59) |
  34. BIT(61)
  35. }
  36. };
  37. #elif BITS_PER_LONG == 32
  38. static const struct primes small_primes = {
  39. .last = 31,
  40. .sz = 32,
  41. .primes = {
  42. BIT(2) |
  43. BIT(3) |
  44. BIT(5) |
  45. BIT(7) |
  46. BIT(11) |
  47. BIT(13) |
  48. BIT(17) |
  49. BIT(19) |
  50. BIT(23) |
  51. BIT(29) |
  52. BIT(31)
  53. }
  54. };
  55. #else
  56. #error "unhandled BITS_PER_LONG"
  57. #endif
  58. static DEFINE_MUTEX(lock);
  59. static const struct primes __rcu *primes = RCU_INITIALIZER(&small_primes);
  60. static unsigned long selftest_max;
  61. static bool slow_is_prime_number(unsigned long x)
  62. {
  63. unsigned long y = int_sqrt(x);
  64. while (y > 1) {
  65. if ((x % y) == 0)
  66. break;
  67. y--;
  68. }
  69. return y == 1;
  70. }
  71. static unsigned long slow_next_prime_number(unsigned long x)
  72. {
  73. while (x < ULONG_MAX && !slow_is_prime_number(++x))
  74. ;
  75. return x;
  76. }
  77. static unsigned long clear_multiples(unsigned long x,
  78. unsigned long *p,
  79. unsigned long start,
  80. unsigned long end)
  81. {
  82. unsigned long m;
  83. m = 2 * x;
  84. if (m < start)
  85. m = roundup(start, x);
  86. while (m < end) {
  87. __clear_bit(m, p);
  88. m += x;
  89. }
  90. return x;
  91. }
  92. static bool expand_to_next_prime(unsigned long x)
  93. {
  94. const struct primes *p;
  95. struct primes *new;
  96. unsigned long sz, y;
  97. /* Betrand's Postulate (or Chebyshev's theorem) states that if n > 3,
  98. * there is always at least one prime p between n and 2n - 2.
  99. * Equivalently, if n > 1, then there is always at least one prime p
  100. * such that n < p < 2n.
  101. *
  102. * http://mathworld.wolfram.com/BertrandsPostulate.html
  103. * https://en.wikipedia.org/wiki/Bertrand's_postulate
  104. */
  105. sz = 2 * x;
  106. if (sz < x)
  107. return false;
  108. sz = round_up(sz, BITS_PER_LONG);
  109. new = kmalloc(sizeof(*new) + bitmap_size(sz),
  110. GFP_KERNEL | __GFP_NOWARN);
  111. if (!new)
  112. return false;
  113. mutex_lock(&lock);
  114. p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
  115. if (x < p->last) {
  116. kfree(new);
  117. goto unlock;
  118. }
  119. /* Where memory permits, track the primes using the
  120. * Sieve of Eratosthenes. The sieve is to remove all multiples of known
  121. * primes from the set, what remains in the set is therefore prime.
  122. */
  123. bitmap_fill(new->primes, sz);
  124. bitmap_copy(new->primes, p->primes, p->sz);
  125. for (y = 2UL; y < sz; y = find_next_bit(new->primes, sz, y + 1))
  126. new->last = clear_multiples(y, new->primes, p->sz, sz);
  127. new->sz = sz;
  128. BUG_ON(new->last <= x);
  129. rcu_assign_pointer(primes, new);
  130. if (p != &small_primes)
  131. kfree_rcu((struct primes *)p, rcu);
  132. unlock:
  133. mutex_unlock(&lock);
  134. return true;
  135. }
  136. static void free_primes(void)
  137. {
  138. const struct primes *p;
  139. mutex_lock(&lock);
  140. p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
  141. if (p != &small_primes) {
  142. rcu_assign_pointer(primes, &small_primes);
  143. kfree_rcu((struct primes *)p, rcu);
  144. }
  145. mutex_unlock(&lock);
  146. }
  147. /**
  148. * next_prime_number - return the next prime number
  149. * @x: the starting point for searching to test
  150. *
  151. * A prime number is an integer greater than 1 that is only divisible by
  152. * itself and 1. The set of prime numbers is computed using the Sieve of
  153. * Eratoshenes (on finding a prime, all multiples of that prime are removed
  154. * from the set) enabling a fast lookup of the next prime number larger than
  155. * @x. If the sieve fails (memory limitation), the search falls back to using
  156. * slow trial-divison, up to the value of ULONG_MAX (which is reported as the
  157. * final prime as a sentinel).
  158. *
  159. * Returns: the next prime number larger than @x
  160. */
  161. unsigned long next_prime_number(unsigned long x)
  162. {
  163. const struct primes *p;
  164. rcu_read_lock();
  165. p = rcu_dereference(primes);
  166. while (x >= p->last) {
  167. rcu_read_unlock();
  168. if (!expand_to_next_prime(x))
  169. return slow_next_prime_number(x);
  170. rcu_read_lock();
  171. p = rcu_dereference(primes);
  172. }
  173. x = find_next_bit(p->primes, p->last, x + 1);
  174. rcu_read_unlock();
  175. return x;
  176. }
  177. EXPORT_SYMBOL(next_prime_number);
  178. /**
  179. * is_prime_number - test whether the given number is prime
  180. * @x: the number to test
  181. *
  182. * A prime number is an integer greater than 1 that is only divisible by
  183. * itself and 1. Internally a cache of prime numbers is kept (to speed up
  184. * searching for sequential primes, see next_prime_number()), but if the number
  185. * falls outside of that cache, its primality is tested using trial-divison.
  186. *
  187. * Returns: true if @x is prime, false for composite numbers.
  188. */
  189. bool is_prime_number(unsigned long x)
  190. {
  191. const struct primes *p;
  192. bool result;
  193. rcu_read_lock();
  194. p = rcu_dereference(primes);
  195. while (x >= p->sz) {
  196. rcu_read_unlock();
  197. if (!expand_to_next_prime(x))
  198. return slow_is_prime_number(x);
  199. rcu_read_lock();
  200. p = rcu_dereference(primes);
  201. }
  202. result = test_bit(x, p->primes);
  203. rcu_read_unlock();
  204. return result;
  205. }
  206. EXPORT_SYMBOL(is_prime_number);
  207. static void dump_primes(void)
  208. {
  209. const struct primes *p;
  210. char *buf;
  211. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  212. rcu_read_lock();
  213. p = rcu_dereference(primes);
  214. if (buf)
  215. bitmap_print_to_pagebuf(true, buf, p->primes, p->sz);
  216. pr_info("primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s",
  217. p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], buf);
  218. rcu_read_unlock();
  219. kfree(buf);
  220. }
  221. static int selftest(unsigned long max)
  222. {
  223. unsigned long x, last;
  224. if (!max)
  225. return 0;
  226. for (last = 0, x = 2; x < max; x++) {
  227. bool slow = slow_is_prime_number(x);
  228. bool fast = is_prime_number(x);
  229. if (slow != fast) {
  230. pr_err("inconsistent result for is-prime(%lu): slow=%s, fast=%s!",
  231. x, slow ? "yes" : "no", fast ? "yes" : "no");
  232. goto err;
  233. }
  234. if (!slow)
  235. continue;
  236. if (next_prime_number(last) != x) {
  237. pr_err("incorrect result for next-prime(%lu): expected %lu, got %lu",
  238. last, x, next_prime_number(last));
  239. goto err;
  240. }
  241. last = x;
  242. }
  243. pr_info("selftest(%lu) passed, last prime was %lu", x, last);
  244. return 0;
  245. err:
  246. dump_primes();
  247. return -EINVAL;
  248. }
  249. static int __init primes_init(void)
  250. {
  251. return selftest(selftest_max);
  252. }
  253. static void __exit primes_exit(void)
  254. {
  255. free_primes();
  256. }
  257. module_init(primes_init);
  258. module_exit(primes_exit);
  259. module_param_named(selftest, selftest_max, ulong, 0400);
  260. MODULE_AUTHOR("Intel Corporation");
  261. MODULE_LICENSE("GPL");