1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) "prime numbers: " fmt
4 #include <linux/module.h>
5 #include <linux/mutex.h>
6 #include <linux/prime_numbers.h>
7 #include <linux/slab.h>
9 #define bitmap_size(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
13 unsigned long last
, sz
;
14 unsigned long primes
[];
17 #if BITS_PER_LONG == 64
18 static const struct primes small_primes
= {
42 #elif BITS_PER_LONG == 32
43 static const struct primes small_primes
= {
61 #error "unhandled BITS_PER_LONG"
64 static DEFINE_MUTEX(lock
);
65 static const struct primes __rcu
*primes
= RCU_INITIALIZER(&small_primes
);
67 static unsigned long selftest_max
;
69 static bool slow_is_prime_number(unsigned long x
)
71 unsigned long y
= int_sqrt(x
);
82 static unsigned long slow_next_prime_number(unsigned long x
)
84 while (x
< ULONG_MAX
&& !slow_is_prime_number(++x
))
90 static unsigned long clear_multiples(unsigned long x
,
99 m
= roundup(start
, x
);
109 static bool expand_to_next_prime(unsigned long x
)
111 const struct primes
*p
;
115 /* Betrand's Postulate (or Chebyshev's theorem) states that if n > 3,
116 * there is always at least one prime p between n and 2n - 2.
117 * Equivalently, if n > 1, then there is always at least one prime p
118 * such that n < p < 2n.
120 * http://mathworld.wolfram.com/BertrandsPostulate.html
121 * https://en.wikipedia.org/wiki/Bertrand's_postulate
127 sz
= round_up(sz
, BITS_PER_LONG
);
128 new = kmalloc(sizeof(*new) + bitmap_size(sz
),
129 GFP_KERNEL
| __GFP_NOWARN
);
134 p
= rcu_dereference_protected(primes
, lockdep_is_held(&lock
));
140 /* Where memory permits, track the primes using the
141 * Sieve of Eratosthenes. The sieve is to remove all multiples of known
142 * primes from the set, what remains in the set is therefore prime.
144 bitmap_fill(new->primes
, sz
);
145 bitmap_copy(new->primes
, p
->primes
, p
->sz
);
146 for (y
= 2UL; y
< sz
; y
= find_next_bit(new->primes
, sz
, y
+ 1))
147 new->last
= clear_multiples(y
, new->primes
, p
->sz
, sz
);
150 BUG_ON(new->last
<= x
);
152 rcu_assign_pointer(primes
, new);
153 if (p
!= &small_primes
)
154 kfree_rcu((struct primes
*)p
, rcu
);
161 static void free_primes(void)
163 const struct primes
*p
;
166 p
= rcu_dereference_protected(primes
, lockdep_is_held(&lock
));
167 if (p
!= &small_primes
) {
168 rcu_assign_pointer(primes
, &small_primes
);
169 kfree_rcu((struct primes
*)p
, rcu
);
175 * next_prime_number - return the next prime number
176 * @x: the starting point for searching to test
178 * A prime number is an integer greater than 1 that is only divisible by
179 * itself and 1. The set of prime numbers is computed using the Sieve of
180 * Eratoshenes (on finding a prime, all multiples of that prime are removed
181 * from the set) enabling a fast lookup of the next prime number larger than
182 * @x. If the sieve fails (memory limitation), the search falls back to using
183 * slow trial-divison, up to the value of ULONG_MAX (which is reported as the
184 * final prime as a sentinel).
186 * Returns: the next prime number larger than @x
188 unsigned long next_prime_number(unsigned long x
)
190 const struct primes
*p
;
193 p
= rcu_dereference(primes
);
194 while (x
>= p
->last
) {
197 if (!expand_to_next_prime(x
))
198 return slow_next_prime_number(x
);
201 p
= rcu_dereference(primes
);
203 x
= find_next_bit(p
->primes
, p
->last
, x
+ 1);
208 EXPORT_SYMBOL(next_prime_number
);
211 * is_prime_number - test whether the given number is prime
212 * @x: the number to test
214 * A prime number is an integer greater than 1 that is only divisible by
215 * itself and 1. Internally a cache of prime numbers is kept (to speed up
216 * searching for sequential primes, see next_prime_number()), but if the number
217 * falls outside of that cache, its primality is tested using trial-divison.
219 * Returns: true if @x is prime, false for composite numbers.
221 bool is_prime_number(unsigned long x
)
223 const struct primes
*p
;
227 p
= rcu_dereference(primes
);
231 if (!expand_to_next_prime(x
))
232 return slow_is_prime_number(x
);
235 p
= rcu_dereference(primes
);
237 result
= test_bit(x
, p
->primes
);
242 EXPORT_SYMBOL(is_prime_number
);
244 static void dump_primes(void)
246 const struct primes
*p
;
249 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
252 p
= rcu_dereference(primes
);
255 bitmap_print_to_pagebuf(true, buf
, p
->primes
, p
->sz
);
256 pr_info("primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s\n",
257 p
->last
, p
->sz
, p
->primes
[BITS_TO_LONGS(p
->sz
) - 1], buf
);
264 static int selftest(unsigned long max
)
266 unsigned long x
, last
;
271 for (last
= 0, x
= 2; x
< max
; x
++) {
272 bool slow
= slow_is_prime_number(x
);
273 bool fast
= is_prime_number(x
);
276 pr_err("inconsistent result for is-prime(%lu): slow=%s, fast=%s!\n",
277 x
, slow
? "yes" : "no", fast
? "yes" : "no");
284 if (next_prime_number(last
) != x
) {
285 pr_err("incorrect result for next-prime(%lu): expected %lu, got %lu\n",
286 last
, x
, next_prime_number(last
));
292 pr_info("%s(%lu) passed, last prime was %lu\n", __func__
, x
, last
);
300 static int __init
primes_init(void)
302 return selftest(selftest_max
);
305 static void __exit
primes_exit(void)
310 module_init(primes_init
);
311 module_exit(primes_exit
);
313 module_param_named(selftest
, selftest_max
, ulong
, 0400);
315 MODULE_AUTHOR("Intel Corporation");
316 MODULE_LICENSE("GPL");