aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig3
-rw-r--r--lib/Kconfig.debug10
-rw-r--r--lib/Makefile2
-rw-r--r--lib/dma-debug.c5
-rw-r--r--lib/nmi_backtrace.c2
-rw-r--r--lib/prime_numbers.c315
-rw-r--r--lib/rhashtable.c9
-rw-r--r--lib/show_mem.c4
-rw-r--r--lib/test_firmware.c92
-rw-r--r--lib/test_parman.c2
-rw-r--r--lib/test_user_copy.c3
11 files changed, 407 insertions, 40 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index f3552604e47a..44d1a1181fb5 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -551,6 +551,9 @@ config SBITMAP
551 bool 551 bool
552 552
553config PARMAN 553config PARMAN
554 tristate "parman" if COMPILE_TEST
555
556config PRIME_NUMBERS
554 tristate 557 tristate
555 558
556endmenu 559endmenu
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 7f44429ac820..66fb4389f05c 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -416,6 +416,16 @@ config MAGIC_SYSRQ_DEFAULT_ENABLE
416 This may be set to 1 or 0 to enable or disable them all, or 416 This may be set to 1 or 0 to enable or disable them all, or
417 to a bitmask as described in Documentation/sysrq.txt. 417 to a bitmask as described in Documentation/sysrq.txt.
418 418
419config MAGIC_SYSRQ_SERIAL
420 bool "Enable magic SysRq key over serial"
421 depends on MAGIC_SYSRQ
422 default y
423 help
424 Many embedded boards have a disconnected TTL level serial which can
425 generate some garbage that can lead to spurious false sysrq detects.
426 This option allows you to decide whether you want to enable the
427 magic SysRq key.
428
419config DEBUG_KERNEL 429config DEBUG_KERNEL
420 bool "Kernel debugging" 430 bool "Kernel debugging"
421 help 431 help
diff --git a/lib/Makefile b/lib/Makefile
index 6b768b58a38d..f1a0364af377 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -198,6 +198,8 @@ obj-$(CONFIG_ASN1) += asn1_decoder.o
198 198
199obj-$(CONFIG_FONT_SUPPORT) += fonts/ 199obj-$(CONFIG_FONT_SUPPORT) += fonts/
200 200
201obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o
202
201hostprogs-y := gen_crc32table 203hostprogs-y := gen_crc32table
202clean-files := crc32table.h 204clean-files := crc32table.h
203 205
diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index 8971370bfb16..60c57ec936db 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -1155,6 +1155,11 @@ static void check_unmap(struct dma_debug_entry *ref)
1155 dir2name[ref->direction]); 1155 dir2name[ref->direction]);
1156 } 1156 }
1157 1157
1158 /*
1159 * Drivers should use dma_mapping_error() to check the returned
1160 * addresses of dma_map_single() and dma_map_page().
1161 * If not, print this warning message. See Documentation/DMA-API.txt.
1162 */
1158 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) { 1163 if (entry->map_err_type == MAP_ERR_NOT_CHECKED) {
1159 err_printk(ref->dev, entry, 1164 err_printk(ref->dev, entry,
1160 "DMA-API: device driver failed to check map error" 1165 "DMA-API: device driver failed to check map error"
diff --git a/lib/nmi_backtrace.c b/lib/nmi_backtrace.c
index 75554754eadf..5f7999eacad5 100644
--- a/lib/nmi_backtrace.c
+++ b/lib/nmi_backtrace.c
@@ -77,7 +77,7 @@ void nmi_trigger_cpumask_backtrace(const cpumask_t *mask,
77 * Force flush any remote buffers that might be stuck in IRQ context 77 * Force flush any remote buffers that might be stuck in IRQ context
78 * and therefore could not run their irq_work. 78 * and therefore could not run their irq_work.
79 */ 79 */
80 printk_nmi_flush(); 80 printk_safe_flush();
81 81
82 clear_bit_unlock(0, &backtrace_flag); 82 clear_bit_unlock(0, &backtrace_flag);
83 put_cpu(); 83 put_cpu();
diff --git a/lib/prime_numbers.c b/lib/prime_numbers.c
new file mode 100644
index 000000000000..550eec457c2e
--- /dev/null
+++ b/lib/prime_numbers.c
@@ -0,0 +1,315 @@
1#define pr_fmt(fmt) "prime numbers: " fmt "\n"
2
3#include <linux/module.h>
4#include <linux/mutex.h>
5#include <linux/prime_numbers.h>
6#include <linux/slab.h>
7
8#define bitmap_size(nbits) (BITS_TO_LONGS(nbits) * sizeof(unsigned long))
9
10struct primes {
11 struct rcu_head rcu;
12 unsigned long last, sz;
13 unsigned long primes[];
14};
15
16#if BITS_PER_LONG == 64
17static const struct primes small_primes = {
18 .last = 61,
19 .sz = 64,
20 .primes = {
21 BIT(2) |
22 BIT(3) |
23 BIT(5) |
24 BIT(7) |
25 BIT(11) |
26 BIT(13) |
27 BIT(17) |
28 BIT(19) |
29 BIT(23) |
30 BIT(29) |
31 BIT(31) |
32 BIT(37) |
33 BIT(41) |
34 BIT(43) |
35 BIT(47) |
36 BIT(53) |
37 BIT(59) |
38 BIT(61)
39 }
40};
41#elif BITS_PER_LONG == 32
42static const struct primes small_primes = {
43 .last = 31,
44 .sz = 32,
45 .primes = {
46 BIT(2) |
47 BIT(3) |
48 BIT(5) |
49 BIT(7) |
50 BIT(11) |
51 BIT(13) |
52 BIT(17) |
53 BIT(19) |
54 BIT(23) |
55 BIT(29) |
56 BIT(31)
57 }
58};
59#else
60#error "unhandled BITS_PER_LONG"
61#endif
62
63static DEFINE_MUTEX(lock);
64static const struct primes __rcu *primes = RCU_INITIALIZER(&small_primes);
65
66static unsigned long selftest_max;
67
68static bool slow_is_prime_number(unsigned long x)
69{
70 unsigned long y = int_sqrt(x);
71
72 while (y > 1) {
73 if ((x % y) == 0)
74 break;
75 y--;
76 }
77
78 return y == 1;
79}
80
81static unsigned long slow_next_prime_number(unsigned long x)
82{
83 while (x < ULONG_MAX && !slow_is_prime_number(++x))
84 ;
85
86 return x;
87}
88
89static unsigned long clear_multiples(unsigned long x,
90 unsigned long *p,
91 unsigned long start,
92 unsigned long end)
93{
94 unsigned long m;
95
96 m = 2 * x;
97 if (m < start)
98 m = roundup(start, x);
99
100 while (m < end) {
101 __clear_bit(m, p);
102 m += x;
103 }
104
105 return x;
106}
107
108static bool expand_to_next_prime(unsigned long x)
109{
110 const struct primes *p;
111 struct primes *new;
112 unsigned long sz, y;
113
114 /* Betrand's Postulate (or Chebyshev's theorem) states that if n > 3,
115 * there is always at least one prime p between n and 2n - 2.
116 * Equivalently, if n > 1, then there is always at least one prime p
117 * such that n < p < 2n.
118 *
119 * http://mathworld.wolfram.com/BertrandsPostulate.html
120 * https://en.wikipedia.org/wiki/Bertrand's_postulate
121 */
122 sz = 2 * x;
123 if (sz < x)
124 return false;
125
126 sz = round_up(sz, BITS_PER_LONG);
127 new = kmalloc(sizeof(*new) + bitmap_size(sz),
128 GFP_KERNEL | __GFP_NOWARN);
129 if (!new)
130 return false;
131
132 mutex_lock(&lock);
133 p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
134 if (x < p->last) {
135 kfree(new);
136 goto unlock;
137 }
138
139 /* Where memory permits, track the primes using the
140 * Sieve of Eratosthenes. The sieve is to remove all multiples of known
141 * primes from the set, what remains in the set is therefore prime.
142 */
143 bitmap_fill(new->primes, sz);
144 bitmap_copy(new->primes, p->primes, p->sz);
145 for (y = 2UL; y < sz; y = find_next_bit(new->primes, sz, y + 1))
146 new->last = clear_multiples(y, new->primes, p->sz, sz);
147 new->sz = sz;
148
149 BUG_ON(new->last <= x);
150
151 rcu_assign_pointer(primes, new);
152 if (p != &small_primes)
153 kfree_rcu((struct primes *)p, rcu);
154
155unlock:
156 mutex_unlock(&lock);
157 return true;
158}
159
160static void free_primes(void)
161{
162 const struct primes *p;
163
164 mutex_lock(&lock);
165 p = rcu_dereference_protected(primes, lockdep_is_held(&lock));
166 if (p != &small_primes) {
167 rcu_assign_pointer(primes, &small_primes);
168 kfree_rcu((struct primes *)p, rcu);
169 }
170 mutex_unlock(&lock);
171}
172
173/**
174 * next_prime_number - return the next prime number
175 * @x: the starting point for searching to test
176 *
177 * A prime number is an integer greater than 1 that is only divisible by
178 * itself and 1. The set of prime numbers is computed using the Sieve of
179 * Eratoshenes (on finding a prime, all multiples of that prime are removed
180 * from the set) enabling a fast lookup of the next prime number larger than
181 * @x. If the sieve fails (memory limitation), the search falls back to using
182 * slow trial-divison, up to the value of ULONG_MAX (which is reported as the
183 * final prime as a sentinel).
184 *
185 * Returns: the next prime number larger than @x
186 */
187unsigned long next_prime_number(unsigned long x)
188{
189 const struct primes *p;
190
191 rcu_read_lock();
192 p = rcu_dereference(primes);
193 while (x >= p->last) {
194 rcu_read_unlock();
195
196 if (!expand_to_next_prime(x))
197 return slow_next_prime_number(x);
198
199 rcu_read_lock();
200 p = rcu_dereference(primes);
201 }
202 x = find_next_bit(p->primes, p->last, x + 1);
203 rcu_read_unlock();
204
205 return x;
206}
207EXPORT_SYMBOL(next_prime_number);
208
209/**
210 * is_prime_number - test whether the given number is prime
211 * @x: the number to test
212 *
213 * A prime number is an integer greater than 1 that is only divisible by
214 * itself and 1. Internally a cache of prime numbers is kept (to speed up
215 * searching for sequential primes, see next_prime_number()), but if the number
216 * falls outside of that cache, its primality is tested using trial-divison.
217 *
218 * Returns: true if @x is prime, false for composite numbers.
219 */
220bool is_prime_number(unsigned long x)
221{
222 const struct primes *p;
223 bool result;
224
225 rcu_read_lock();
226 p = rcu_dereference(primes);
227 while (x >= p->sz) {
228 rcu_read_unlock();
229
230 if (!expand_to_next_prime(x))
231 return slow_is_prime_number(x);
232
233 rcu_read_lock();
234 p = rcu_dereference(primes);
235 }
236 result = test_bit(x, p->primes);
237 rcu_read_unlock();
238
239 return result;
240}
241EXPORT_SYMBOL(is_prime_number);
242
243static void dump_primes(void)
244{
245 const struct primes *p;
246 char *buf;
247
248 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
249
250 rcu_read_lock();
251 p = rcu_dereference(primes);
252
253 if (buf)
254 bitmap_print_to_pagebuf(true, buf, p->primes, p->sz);
255 pr_info("primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s",
256 p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], buf);
257
258 rcu_read_unlock();
259
260 kfree(buf);
261}
262
263static int selftest(unsigned long max)
264{
265 unsigned long x, last;
266
267 if (!max)
268 return 0;
269
270 for (last = 0, x = 2; x < max; x++) {
271 bool slow = slow_is_prime_number(x);
272 bool fast = is_prime_number(x);
273
274 if (slow != fast) {
275 pr_err("inconsistent result for is-prime(%lu): slow=%s, fast=%s!",
276 x, slow ? "yes" : "no", fast ? "yes" : "no");
277 goto err;
278 }
279
280 if (!slow)
281 continue;
282
283 if (next_prime_number(last) != x) {
284 pr_err("incorrect result for next-prime(%lu): expected %lu, got %lu",
285 last, x, next_prime_number(last));
286 goto err;
287 }
288 last = x;
289 }
290
291 pr_info("selftest(%lu) passed, last prime was %lu", x, last);
292 return 0;
293
294err:
295 dump_primes();
296 return -EINVAL;
297}
298
299static int __init primes_init(void)
300{
301 return selftest(selftest_max);
302}
303
304static void __exit primes_exit(void)
305{
306 free_primes();
307}
308
309module_init(primes_init);
310module_exit(primes_exit);
311
312module_param_named(selftest, selftest_max, ulong, 0400);
313
314MODULE_AUTHOR("Intel Corporation");
315MODULE_LICENSE("GPL");
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index 172454e6b979..c5b9b9351cec 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -146,9 +146,7 @@ static void bucket_table_free(const struct bucket_table *tbl)
146 if (tbl->nest) 146 if (tbl->nest)
147 nested_bucket_table_free(tbl); 147 nested_bucket_table_free(tbl);
148 148
149 if (tbl) 149 kvfree(tbl->locks);
150 kvfree(tbl->locks);
151
152 kvfree(tbl); 150 kvfree(tbl);
153} 151}
154 152
@@ -1123,12 +1121,13 @@ struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
1123 union nested_table *ntbl; 1121 union nested_table *ntbl;
1124 1122
1125 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); 1123 ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]);
1126 ntbl = rht_dereference_bucket(ntbl[index].table, tbl, hash); 1124 ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
1127 subhash >>= tbl->nest; 1125 subhash >>= tbl->nest;
1128 1126
1129 while (ntbl && size > (1 << shift)) { 1127 while (ntbl && size > (1 << shift)) {
1130 index = subhash & ((1 << shift) - 1); 1128 index = subhash & ((1 << shift) - 1);
1131 ntbl = rht_dereference_bucket(ntbl[index].table, tbl, hash); 1129 ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
1130 tbl, hash);
1132 size >>= shift; 1131 size >>= shift;
1133 subhash >>= shift; 1132 subhash >>= shift;
1134 } 1133 }
diff --git a/lib/show_mem.c b/lib/show_mem.c
index 1feed6a2b12a..0beaa1d899aa 100644
--- a/lib/show_mem.c
+++ b/lib/show_mem.c
@@ -9,13 +9,13 @@
9#include <linux/quicklist.h> 9#include <linux/quicklist.h>
10#include <linux/cma.h> 10#include <linux/cma.h>
11 11
12void show_mem(unsigned int filter) 12void show_mem(unsigned int filter, nodemask_t *nodemask)
13{ 13{
14 pg_data_t *pgdat; 14 pg_data_t *pgdat;
15 unsigned long total = 0, reserved = 0, highmem = 0; 15 unsigned long total = 0, reserved = 0, highmem = 0;
16 16
17 printk("Mem-Info:\n"); 17 printk("Mem-Info:\n");
18 show_free_areas(filter); 18 show_free_areas(filter, nodemask);
19 19
20 for_each_online_pgdat(pgdat) { 20 for_each_online_pgdat(pgdat) {
21 unsigned long flags; 21 unsigned long flags;
diff --git a/lib/test_firmware.c b/lib/test_firmware.c
index a3e8ec3fb1c5..09371b0a9baf 100644
--- a/lib/test_firmware.c
+++ b/lib/test_firmware.c
@@ -42,12 +42,6 @@ static const struct file_operations test_fw_fops = {
42 .read = test_fw_misc_read, 42 .read = test_fw_misc_read,
43}; 43};
44 44
45static struct miscdevice test_fw_misc_device = {
46 .minor = MISC_DYNAMIC_MINOR,
47 .name = "test_firmware",
48 .fops = &test_fw_fops,
49};
50
51static ssize_t trigger_request_store(struct device *dev, 45static ssize_t trigger_request_store(struct device *dev,
52 struct device_attribute *attr, 46 struct device_attribute *attr,
53 const char *buf, size_t count) 47 const char *buf, size_t count)
@@ -132,39 +126,81 @@ out:
132} 126}
133static DEVICE_ATTR_WO(trigger_async_request); 127static DEVICE_ATTR_WO(trigger_async_request);
134 128
135static int __init test_firmware_init(void) 129static ssize_t trigger_custom_fallback_store(struct device *dev,
130 struct device_attribute *attr,
131 const char *buf, size_t count)
136{ 132{
137 int rc; 133 int rc;
134 char *name;
138 135
139 rc = misc_register(&test_fw_misc_device); 136 name = kstrndup(buf, count, GFP_KERNEL);
137 if (!name)
138 return -ENOSPC;
139
140 pr_info("loading '%s' using custom fallback mechanism\n", name);
141
142 mutex_lock(&test_fw_mutex);
143 release_firmware(test_firmware);
144 test_firmware = NULL;
145 rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
146 dev, GFP_KERNEL, NULL,
147 trigger_async_request_cb);
140 if (rc) { 148 if (rc) {
141 pr_err("could not register misc device: %d\n", rc); 149 pr_info("async load of '%s' failed: %d\n", name, rc);
142 return rc; 150 kfree(name);
151 goto out;
143 } 152 }
144 rc = device_create_file(test_fw_misc_device.this_device, 153 /* Free 'name' ASAP, to test for race conditions */
145 &dev_attr_trigger_request); 154 kfree(name);
146 if (rc) { 155
147 pr_err("could not create sysfs interface: %d\n", rc); 156 wait_for_completion(&async_fw_done);
148 goto dereg; 157
158 if (test_firmware) {
159 pr_info("loaded: %zu\n", test_firmware->size);
160 rc = count;
161 } else {
162 pr_err("failed to async load firmware\n");
163 rc = -ENODEV;
149 } 164 }
150 165
151 rc = device_create_file(test_fw_misc_device.this_device, 166out:
152 &dev_attr_trigger_async_request); 167 mutex_unlock(&test_fw_mutex);
168
169 return rc;
170}
171static DEVICE_ATTR_WO(trigger_custom_fallback);
172
173#define TEST_FW_DEV_ATTR(name) &dev_attr_##name.attr
174
175static struct attribute *test_dev_attrs[] = {
176 TEST_FW_DEV_ATTR(trigger_request),
177 TEST_FW_DEV_ATTR(trigger_async_request),
178 TEST_FW_DEV_ATTR(trigger_custom_fallback),
179 NULL,
180};
181
182ATTRIBUTE_GROUPS(test_dev);
183
184static struct miscdevice test_fw_misc_device = {
185 .minor = MISC_DYNAMIC_MINOR,
186 .name = "test_firmware",
187 .fops = &test_fw_fops,
188 .groups = test_dev_groups,
189};
190
191static int __init test_firmware_init(void)
192{
193 int rc;
194
195 rc = misc_register(&test_fw_misc_device);
153 if (rc) { 196 if (rc) {
154 pr_err("could not create async sysfs interface: %d\n", rc); 197 pr_err("could not register misc device: %d\n", rc);
155 goto remove_file; 198 return rc;
156 } 199 }
157 200
158 pr_warn("interface ready\n"); 201 pr_warn("interface ready\n");
159 202
160 return 0; 203 return 0;
161
162remove_file:
163 device_remove_file(test_fw_misc_device.this_device,
164 &dev_attr_trigger_async_request);
165dereg:
166 misc_deregister(&test_fw_misc_device);
167 return rc;
168} 204}
169 205
170module_init(test_firmware_init); 206module_init(test_firmware_init);
@@ -172,10 +208,6 @@ module_init(test_firmware_init);
172static void __exit test_firmware_exit(void) 208static void __exit test_firmware_exit(void)
173{ 209{
174 release_firmware(test_firmware); 210 release_firmware(test_firmware);
175 device_remove_file(test_fw_misc_device.this_device,
176 &dev_attr_trigger_async_request);
177 device_remove_file(test_fw_misc_device.this_device,
178 &dev_attr_trigger_request);
179 misc_deregister(&test_fw_misc_device); 211 misc_deregister(&test_fw_misc_device);
180 pr_warn("removed interface\n"); 212 pr_warn("removed interface\n");
181} 213}
diff --git a/lib/test_parman.c b/lib/test_parman.c
index fe9f3a785804..35e32243693c 100644
--- a/lib/test_parman.c
+++ b/lib/test_parman.c
@@ -334,7 +334,7 @@ static int test_parman_check_array(struct test_parman *test_parman,
334 last_priority = item->prio->priority; 334 last_priority = item->prio->priority;
335 335
336 if (item->parman_item.index != i) { 336 if (item->parman_item.index != i) {
337 pr_err("Item has different index in compare to where it actualy is (%lu != %d)\n", 337 pr_err("Item has different index in compare to where it actually is (%lu != %d)\n",
338 item->parman_item.index, i); 338 item->parman_item.index, i);
339 return -EINVAL; 339 return -EINVAL;
340 } 340 }
diff --git a/lib/test_user_copy.c b/lib/test_user_copy.c
index 6f335a3d4ae2..1a8d71a68531 100644
--- a/lib/test_user_copy.c
+++ b/lib/test_user_copy.c
@@ -30,7 +30,8 @@
30 * As there doesn't appear to be anything that can safely determine 30 * As there doesn't appear to be anything that can safely determine
31 * their capability at compile-time, we just have to opt-out certain archs. 31 * their capability at compile-time, we just have to opt-out certain archs.
32 */ 32 */
33#if BITS_PER_LONG == 64 || (!defined(CONFIG_AVR32) && \ 33#if BITS_PER_LONG == 64 || (!(defined(CONFIG_ARM) && !defined(MMU)) && \
34 !defined(CONFIG_AVR32) && \
34 !defined(CONFIG_BLACKFIN) && \ 35 !defined(CONFIG_BLACKFIN) && \
35 !defined(CONFIG_M32R) && \ 36 !defined(CONFIG_M32R) && \
36 !defined(CONFIG_M68K) && \ 37 !defined(CONFIG_M68K) && \