aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug12
-rw-r--r--lib/Makefile1
-rw-r--r--lib/div64.c2
-rw-r--r--lib/dma-debug.c8
-rw-r--r--lib/dynamic_debug.c8
-rw-r--r--lib/halfmd4.c3
-rw-r--r--lib/hexdump.c6
-rw-r--r--lib/idr.c4
-rw-r--r--lib/is_single_threaded.c5
-rw-r--r--lib/kasprintf.c16
-rw-r--r--lib/kobject.c30
-rw-r--r--lib/llist.c4
-rw-r--r--lib/percpu_ida.c2
-rw-r--r--lib/radix-tree.c10
-rw-r--r--lib/string.c2
-rw-r--r--lib/test-string_helpers.c36
-rw-r--r--lib/test_bpf.c30
-rw-r--r--lib/test_printf.c362
-rw-r--r--lib/vsprintf.c80
19 files changed, 546 insertions, 75 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 1d1521c26302..8c15b29d5adc 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -312,6 +312,15 @@ config DEBUG_SECTION_MISMATCH
312 - Enable verbose reporting from modpost in order to help resolve 312 - Enable verbose reporting from modpost in order to help resolve
313 the section mismatches that are reported. 313 the section mismatches that are reported.
314 314
315config SECTION_MISMATCH_WARN_ONLY
316 bool "Make section mismatch errors non-fatal"
317 default y
318 help
319 If you say N here, the build process will fail if there are any
320 section mismatch, instead of just throwing warnings.
321
322 If unsure, say Y.
323
315# 324#
316# Select this config option from the architecture Kconfig, if it 325# Select this config option from the architecture Kconfig, if it
317# is preferred to always offer frame pointers as a config 326# is preferred to always offer frame pointers as a config
@@ -1686,6 +1695,9 @@ config TEST_STRING_HELPERS
1686config TEST_KSTRTOX 1695config TEST_KSTRTOX
1687 tristate "Test kstrto*() family of functions at runtime" 1696 tristate "Test kstrto*() family of functions at runtime"
1688 1697
1698config TEST_PRINTF
1699 tristate "Test printf() family of functions at runtime"
1700
1689config TEST_RHASHTABLE 1701config TEST_RHASHTABLE
1690 tristate "Perform selftest on resizable hash table" 1702 tristate "Perform selftest on resizable hash table"
1691 default n 1703 default n
diff --git a/lib/Makefile b/lib/Makefile
index 8de3b012eac7..7f1de26613d2 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -42,6 +42,7 @@ obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
42obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o 42obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
43obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o 43obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
44obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o 44obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
45obj-$(CONFIG_TEST_PRINTF) += test_printf.o
45 46
46ifeq ($(CONFIG_DEBUG_KOBJECT),y) 47ifeq ($(CONFIG_DEBUG_KOBJECT),y)
47CFLAGS_kobject.o += -DDEBUG 48CFLAGS_kobject.o += -DDEBUG
diff --git a/lib/div64.c b/lib/div64.c
index 19ea7ed4b948..62a698a432bc 100644
--- a/lib/div64.c
+++ b/lib/div64.c
@@ -162,7 +162,7 @@ s64 div64_s64(s64 dividend, s64 divisor)
162{ 162{
163 s64 quot, t; 163 s64 quot, t;
164 164
165 quot = div64_u64(abs64(dividend), abs64(divisor)); 165 quot = div64_u64(abs(dividend), abs(divisor));
166 t = (dividend ^ divisor) >> 63; 166 t = (dividend ^ divisor) >> 63;
167 167
168 return (quot ^ t) - t; 168 return (quot ^ t) - t;
diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index fcb65d2a0b94..8855f019ebe8 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -1249,6 +1249,14 @@ static void check_sync(struct device *dev,
1249 dir2name[entry->direction], 1249 dir2name[entry->direction],
1250 dir2name[ref->direction]); 1250 dir2name[ref->direction]);
1251 1251
1252 if (ref->sg_call_ents && ref->type == dma_debug_sg &&
1253 ref->sg_call_ents != entry->sg_call_ents) {
1254 err_printk(ref->dev, entry, "DMA-API: device driver syncs "
1255 "DMA sg list with different entry count "
1256 "[map count=%d] [sync count=%d]\n",
1257 entry->sg_call_ents, ref->sg_call_ents);
1258 }
1259
1252out: 1260out:
1253 put_hash_bucket(bucket, &flags); 1261 put_hash_bucket(bucket, &flags);
1254} 1262}
diff --git a/lib/dynamic_debug.c b/lib/dynamic_debug.c
index e491e02eff54..e3952e9c8ec0 100644
--- a/lib/dynamic_debug.c
+++ b/lib/dynamic_debug.c
@@ -42,7 +42,7 @@ extern struct _ddebug __stop___verbose[];
42 42
43struct ddebug_table { 43struct ddebug_table {
44 struct list_head link; 44 struct list_head link;
45 char *mod_name; 45 const char *mod_name;
46 unsigned int num_ddebugs; 46 unsigned int num_ddebugs;
47 struct _ddebug *ddebugs; 47 struct _ddebug *ddebugs;
48}; 48};
@@ -841,12 +841,12 @@ int ddebug_add_module(struct _ddebug *tab, unsigned int n,
841 const char *name) 841 const char *name)
842{ 842{
843 struct ddebug_table *dt; 843 struct ddebug_table *dt;
844 char *new_name; 844 const char *new_name;
845 845
846 dt = kzalloc(sizeof(*dt), GFP_KERNEL); 846 dt = kzalloc(sizeof(*dt), GFP_KERNEL);
847 if (dt == NULL) 847 if (dt == NULL)
848 return -ENOMEM; 848 return -ENOMEM;
849 new_name = kstrdup(name, GFP_KERNEL); 849 new_name = kstrdup_const(name, GFP_KERNEL);
850 if (new_name == NULL) { 850 if (new_name == NULL) {
851 kfree(dt); 851 kfree(dt);
852 return -ENOMEM; 852 return -ENOMEM;
@@ -907,7 +907,7 @@ int ddebug_dyndbg_module_param_cb(char *param, char *val, const char *module)
907static void ddebug_table_free(struct ddebug_table *dt) 907static void ddebug_table_free(struct ddebug_table *dt)
908{ 908{
909 list_del_init(&dt->link); 909 list_del_init(&dt->link);
910 kfree(dt->mod_name); 910 kfree_const(dt->mod_name);
911 kfree(dt); 911 kfree(dt);
912} 912}
913 913
diff --git a/lib/halfmd4.c b/lib/halfmd4.c
index a8fe6274a13c..137e861d9690 100644
--- a/lib/halfmd4.c
+++ b/lib/halfmd4.c
@@ -1,6 +1,7 @@
1#include <linux/compiler.h> 1#include <linux/compiler.h>
2#include <linux/export.h> 2#include <linux/export.h>
3#include <linux/cryptohash.h> 3#include <linux/cryptohash.h>
4#include <linux/bitops.h>
4 5
5/* F, G and H are basic MD4 functions: selection, majority, parity */ 6/* F, G and H are basic MD4 functions: selection, majority, parity */
6#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) 7#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
@@ -14,7 +15,7 @@
14 * Rotation is separate from addition to prevent recomputation 15 * Rotation is separate from addition to prevent recomputation
15 */ 16 */
16#define ROUND(f, a, b, c, d, x, s) \ 17#define ROUND(f, a, b, c, d, x, s) \
17 (a += f(b, c, d) + x, a = (a << s) | (a >> (32 - s))) 18 (a += f(b, c, d) + x, a = rol32(a, s))
18#define K1 0 19#define K1 0
19#define K2 013240474631UL 20#define K2 013240474631UL
20#define K3 015666365641UL 21#define K3 015666365641UL
diff --git a/lib/hexdump.c b/lib/hexdump.c
index 8d74c20d8595..992457b1284c 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -169,11 +169,15 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
169 } 169 }
170 } else { 170 } else {
171 for (j = 0; j < len; j++) { 171 for (j = 0; j < len; j++) {
172 if (linebuflen < lx + 3) 172 if (linebuflen < lx + 2)
173 goto overflow2; 173 goto overflow2;
174 ch = ptr[j]; 174 ch = ptr[j];
175 linebuf[lx++] = hex_asc_hi(ch); 175 linebuf[lx++] = hex_asc_hi(ch);
176 if (linebuflen < lx + 2)
177 goto overflow2;
176 linebuf[lx++] = hex_asc_lo(ch); 178 linebuf[lx++] = hex_asc_lo(ch);
179 if (linebuflen < lx + 2)
180 goto overflow2;
177 linebuf[lx++] = ' '; 181 linebuf[lx++] = ' ';
178 } 182 }
179 if (j) 183 if (j)
diff --git a/lib/idr.c b/lib/idr.c
index 5335c43adf46..6098336df267 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -399,7 +399,7 @@ void idr_preload(gfp_t gfp_mask)
399 * allocation guarantee. Disallow usage from those contexts. 399 * allocation guarantee. Disallow usage from those contexts.
400 */ 400 */
401 WARN_ON_ONCE(in_interrupt()); 401 WARN_ON_ONCE(in_interrupt());
402 might_sleep_if(gfp_mask & __GFP_WAIT); 402 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
403 403
404 preempt_disable(); 404 preempt_disable();
405 405
@@ -453,7 +453,7 @@ int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
453 struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 453 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
454 int id; 454 int id;
455 455
456 might_sleep_if(gfp_mask & __GFP_WAIT); 456 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
457 457
458 /* sanity checks */ 458 /* sanity checks */
459 if (WARN_ON_ONCE(start < 0)) 459 if (WARN_ON_ONCE(start < 0))
diff --git a/lib/is_single_threaded.c b/lib/is_single_threaded.c
index bd2bea963364..391fd23976a2 100644
--- a/lib/is_single_threaded.c
+++ b/lib/is_single_threaded.c
@@ -36,8 +36,7 @@ bool current_is_single_threaded(void)
36 if (unlikely(p == task->group_leader)) 36 if (unlikely(p == task->group_leader))
37 continue; 37 continue;
38 38
39 t = p; 39 for_each_thread(p, t) {
40 do {
41 if (unlikely(t->mm == mm)) 40 if (unlikely(t->mm == mm))
42 goto found; 41 goto found;
43 if (likely(t->mm)) 42 if (likely(t->mm))
@@ -48,7 +47,7 @@ bool current_is_single_threaded(void)
48 * forked before exiting. 47 * forked before exiting.
49 */ 48 */
50 smp_rmb(); 49 smp_rmb();
51 } while_each_thread(p, t); 50 }
52 } 51 }
53 ret = true; 52 ret = true;
54found: 53found:
diff --git a/lib/kasprintf.c b/lib/kasprintf.c
index 32f12150fc4f..f194e6e593e1 100644
--- a/lib/kasprintf.c
+++ b/lib/kasprintf.c
@@ -31,6 +31,22 @@ char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
31} 31}
32EXPORT_SYMBOL(kvasprintf); 32EXPORT_SYMBOL(kvasprintf);
33 33
34/*
35 * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
36 * (or the sole vararg) points to rodata, we will then save a memory
37 * allocation and string copy. In any case, the return value should be
38 * freed using kfree_const().
39 */
40const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
41{
42 if (!strchr(fmt, '%'))
43 return kstrdup_const(fmt, gfp);
44 if (!strcmp(fmt, "%s"))
45 return kstrdup_const(va_arg(ap, const char*), gfp);
46 return kvasprintf(gfp, fmt, ap);
47}
48EXPORT_SYMBOL(kvasprintf_const);
49
34char *kasprintf(gfp_t gfp, const char *fmt, ...) 50char *kasprintf(gfp_t gfp, const char *fmt, ...)
35{ 51{
36 va_list ap; 52 va_list ap;
diff --git a/lib/kobject.c b/lib/kobject.c
index 055407746266..7cbccd2b4c72 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -257,18 +257,32 @@ static int kobject_add_internal(struct kobject *kobj)
257int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, 257int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
258 va_list vargs) 258 va_list vargs)
259{ 259{
260 char *s; 260 const char *s;
261 261
262 if (kobj->name && !fmt) 262 if (kobj->name && !fmt)
263 return 0; 263 return 0;
264 264
265 s = kvasprintf(GFP_KERNEL, fmt, vargs); 265 s = kvasprintf_const(GFP_KERNEL, fmt, vargs);
266 if (!s) 266 if (!s)
267 return -ENOMEM; 267 return -ENOMEM;
268 268
269 /* ewww... some of these buggers have '/' in the name ... */ 269 /*
270 strreplace(s, '/', '!'); 270 * ewww... some of these buggers have '/' in the name ... If
271 kfree(kobj->name); 271 * that's the case, we need to make sure we have an actual
272 * allocated copy to modify, since kvasprintf_const may have
273 * returned something from .rodata.
274 */
275 if (strchr(s, '/')) {
276 char *t;
277
278 t = kstrdup(s, GFP_KERNEL);
279 kfree_const(s);
280 if (!t)
281 return -ENOMEM;
282 strreplace(t, '/', '!');
283 s = t;
284 }
285 kfree_const(kobj->name);
272 kobj->name = s; 286 kobj->name = s;
273 287
274 return 0; 288 return 0;
@@ -466,7 +480,7 @@ int kobject_rename(struct kobject *kobj, const char *new_name)
466 envp[0] = devpath_string; 480 envp[0] = devpath_string;
467 envp[1] = NULL; 481 envp[1] = NULL;
468 482
469 name = dup_name = kstrdup(new_name, GFP_KERNEL); 483 name = dup_name = kstrdup_const(new_name, GFP_KERNEL);
470 if (!name) { 484 if (!name) {
471 error = -ENOMEM; 485 error = -ENOMEM;
472 goto out; 486 goto out;
@@ -486,7 +500,7 @@ int kobject_rename(struct kobject *kobj, const char *new_name)
486 kobject_uevent_env(kobj, KOBJ_MOVE, envp); 500 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
487 501
488out: 502out:
489 kfree(dup_name); 503 kfree_const(dup_name);
490 kfree(devpath_string); 504 kfree(devpath_string);
491 kfree(devpath); 505 kfree(devpath);
492 kobject_put(kobj); 506 kobject_put(kobj);
@@ -634,7 +648,7 @@ static void kobject_cleanup(struct kobject *kobj)
634 /* free name if we allocated it */ 648 /* free name if we allocated it */
635 if (name) { 649 if (name) {
636 pr_debug("kobject: '%s': free name\n", name); 650 pr_debug("kobject: '%s': free name\n", name);
637 kfree(name); 651 kfree_const(name);
638 } 652 }
639} 653}
640 654
diff --git a/lib/llist.c b/lib/llist.c
index 0b0e9779d675..ae5872b1df0c 100644
--- a/lib/llist.c
+++ b/lib/llist.c
@@ -66,12 +66,12 @@ struct llist_node *llist_del_first(struct llist_head *head)
66{ 66{
67 struct llist_node *entry, *old_entry, *next; 67 struct llist_node *entry, *old_entry, *next;
68 68
69 entry = head->first; 69 entry = smp_load_acquire(&head->first);
70 for (;;) { 70 for (;;) {
71 if (entry == NULL) 71 if (entry == NULL)
72 return NULL; 72 return NULL;
73 old_entry = entry; 73 old_entry = entry;
74 next = entry->next; 74 next = READ_ONCE(entry->next);
75 entry = cmpxchg(&head->first, old_entry, next); 75 entry = cmpxchg(&head->first, old_entry, next);
76 if (entry == old_entry) 76 if (entry == old_entry)
77 break; 77 break;
diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
index f75715131f20..6d40944960de 100644
--- a/lib/percpu_ida.c
+++ b/lib/percpu_ida.c
@@ -135,7 +135,7 @@ static inline unsigned alloc_local_tag(struct percpu_ida_cpu *tags)
135 * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, of course). 135 * TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, of course).
136 * 136 *
137 * @gfp indicates whether or not to wait until a free id is available (it's not 137 * @gfp indicates whether or not to wait until a free id is available (it's not
138 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep 138 * used for internal memory allocations); thus if passed __GFP_RECLAIM we may sleep
139 * however long it takes until another thread frees an id (same semantics as a 139 * however long it takes until another thread frees an id (same semantics as a
140 * mempool). 140 * mempool).
141 * 141 *
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index f9ebe1c82060..fcf5d98574ce 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -188,7 +188,7 @@ radix_tree_node_alloc(struct radix_tree_root *root)
188 * preloading in the interrupt anyway as all the allocations have to 188 * preloading in the interrupt anyway as all the allocations have to
189 * be atomic. So just do normal allocation when in interrupt. 189 * be atomic. So just do normal allocation when in interrupt.
190 */ 190 */
191 if (!(gfp_mask & __GFP_WAIT) && !in_interrupt()) { 191 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
192 struct radix_tree_preload *rtp; 192 struct radix_tree_preload *rtp;
193 193
194 /* 194 /*
@@ -249,7 +249,7 @@ radix_tree_node_free(struct radix_tree_node *node)
249 * with preemption not disabled. 249 * with preemption not disabled.
250 * 250 *
251 * To make use of this facility, the radix tree must be initialised without 251 * To make use of this facility, the radix tree must be initialised without
252 * __GFP_WAIT being passed to INIT_RADIX_TREE(). 252 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
253 */ 253 */
254static int __radix_tree_preload(gfp_t gfp_mask) 254static int __radix_tree_preload(gfp_t gfp_mask)
255{ 255{
@@ -286,12 +286,12 @@ out:
286 * with preemption not disabled. 286 * with preemption not disabled.
287 * 287 *
288 * To make use of this facility, the radix tree must be initialised without 288 * To make use of this facility, the radix tree must be initialised without
289 * __GFP_WAIT being passed to INIT_RADIX_TREE(). 289 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
290 */ 290 */
291int radix_tree_preload(gfp_t gfp_mask) 291int radix_tree_preload(gfp_t gfp_mask)
292{ 292{
293 /* Warn on non-sensical use... */ 293 /* Warn on non-sensical use... */
294 WARN_ON_ONCE(!(gfp_mask & __GFP_WAIT)); 294 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
295 return __radix_tree_preload(gfp_mask); 295 return __radix_tree_preload(gfp_mask);
296} 296}
297EXPORT_SYMBOL(radix_tree_preload); 297EXPORT_SYMBOL(radix_tree_preload);
@@ -303,7 +303,7 @@ EXPORT_SYMBOL(radix_tree_preload);
303 */ 303 */
304int radix_tree_maybe_preload(gfp_t gfp_mask) 304int radix_tree_maybe_preload(gfp_t gfp_mask)
305{ 305{
306 if (gfp_mask & __GFP_WAIT) 306 if (gfpflags_allow_blocking(gfp_mask))
307 return __radix_tree_preload(gfp_mask); 307 return __radix_tree_preload(gfp_mask);
308 /* Preloading doesn't help anything with this gfp mask, skip it */ 308 /* Preloading doesn't help anything with this gfp mask, skip it */
309 preempt_disable(); 309 preempt_disable();
diff --git a/lib/string.c b/lib/string.c
index 84775ba873b9..0323c0d5629a 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -904,7 +904,7 @@ void *memchr_inv(const void *start, int c, size_t bytes)
904 904
905 value64 = value; 905 value64 = value;
906#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 906#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
907 value64 *= 0x0101010101010101; 907 value64 *= 0x0101010101010101ULL;
908#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) 908#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
909 value64 *= 0x01010101; 909 value64 *= 0x01010101;
910 value64 |= value64 << 32; 910 value64 |= value64 << 32;
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c
index 8e376efd88a4..98866a770770 100644
--- a/lib/test-string_helpers.c
+++ b/lib/test-string_helpers.c
@@ -326,6 +326,39 @@ out:
326 kfree(out_test); 326 kfree(out_test);
327} 327}
328 328
329#define string_get_size_maxbuf 16
330#define test_string_get_size_one(size, blk_size, units, exp_result) \
331 do { \
332 BUILD_BUG_ON(sizeof(exp_result) >= string_get_size_maxbuf); \
333 __test_string_get_size((size), (blk_size), (units), \
334 (exp_result)); \
335 } while (0)
336
337
338static __init void __test_string_get_size(const u64 size, const u64 blk_size,
339 const enum string_size_units units,
340 const char *exp_result)
341{
342 char buf[string_get_size_maxbuf];
343
344 string_get_size(size, blk_size, units, buf, sizeof(buf));
345 if (!memcmp(buf, exp_result, strlen(exp_result) + 1))
346 return;
347
348 buf[sizeof(buf) - 1] = '\0';
349 pr_warn("Test 'test_string_get_size_one' failed!\n");
350 pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %d\n",
351 size, blk_size, units);
352 pr_warn("expected: '%s', got '%s'\n", exp_result, buf);
353}
354
355static __init void test_string_get_size(void)
356{
357 test_string_get_size_one(16384, 512, STRING_UNITS_2, "8.00 MiB");
358 test_string_get_size_one(8192, 4096, STRING_UNITS_10, "32.7 MB");
359 test_string_get_size_one(1, 512, STRING_UNITS_10, "512 B");
360}
361
329static int __init test_string_helpers_init(void) 362static int __init test_string_helpers_init(void)
330{ 363{
331 unsigned int i; 364 unsigned int i;
@@ -344,6 +377,9 @@ static int __init test_string_helpers_init(void)
344 for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++) 377 for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
345 test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1); 378 test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
346 379
380 /* Test string_get_size() */
381 test_string_get_size();
382
347 return -EINVAL; 383 return -EINVAL;
348} 384}
349module_init(test_string_helpers_init); 385module_init(test_string_helpers_init);
diff --git a/lib/test_bpf.c b/lib/test_bpf.c
index d1377390b3ad..10cd1860e5b0 100644
--- a/lib/test_bpf.c
+++ b/lib/test_bpf.c
@@ -5056,6 +5056,36 @@ static struct bpf_test tests[] = {
5056 { {0x1, 0x0 } }, 5056 { {0x1, 0x0 } },
5057 }, 5057 },
5058 { 5058 {
5059 "MOD default X",
5060 .u.insns = {
5061 /*
5062 * A = 0x42
5063 * A = A mod X ; this halt the filter execution if X is 0
5064 * ret 0x42
5065 */
5066 BPF_STMT(BPF_LD | BPF_IMM, 0x42),
5067 BPF_STMT(BPF_ALU | BPF_MOD | BPF_X, 0),
5068 BPF_STMT(BPF_RET | BPF_K, 0x42),
5069 },
5070 CLASSIC | FLAG_NO_DATA,
5071 {},
5072 { {0x1, 0x0 } },
5073 },
5074 {
5075 "MOD default A",
5076 .u.insns = {
5077 /*
5078 * A = A mod 1
5079 * ret A
5080 */
5081 BPF_STMT(BPF_ALU | BPF_MOD | BPF_K, 0x1),
5082 BPF_STMT(BPF_RET | BPF_A, 0x0),
5083 },
5084 CLASSIC | FLAG_NO_DATA,
5085 {},
5086 { {0x1, 0x0 } },
5087 },
5088 {
5059 "JMP EQ default A", 5089 "JMP EQ default A",
5060 .u.insns = { 5090 .u.insns = {
5061 /* 5091 /*
diff --git a/lib/test_printf.c b/lib/test_printf.c
new file mode 100644
index 000000000000..c5a666af9ba5
--- /dev/null
+++ b/lib/test_printf.c
@@ -0,0 +1,362 @@
1/*
2 * Test cases for printf facility.
3 */
4
5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6
7#include <linux/init.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/printk.h>
11#include <linux/random.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14
15#include <linux/socket.h>
16#include <linux/in.h>
17
18#define BUF_SIZE 256
19#define FILL_CHAR '$'
20
21#define PTR1 ((void*)0x01234567)
22#define PTR2 ((void*)(long)(int)0xfedcba98)
23
24#if BITS_PER_LONG == 64
25#define PTR1_ZEROES "000000000"
26#define PTR1_SPACES " "
27#define PTR1_STR "1234567"
28#define PTR2_STR "fffffffffedcba98"
29#define PTR_WIDTH 16
30#else
31#define PTR1_ZEROES "0"
32#define PTR1_SPACES " "
33#define PTR1_STR "1234567"
34#define PTR2_STR "fedcba98"
35#define PTR_WIDTH 8
36#endif
37#define PTR_WIDTH_STR stringify(PTR_WIDTH)
38
39static unsigned total_tests __initdata;
40static unsigned failed_tests __initdata;
41static char *test_buffer __initdata;
42
43static int __printf(4, 0) __init
44do_test(int bufsize, const char *expect, int elen,
45 const char *fmt, va_list ap)
46{
47 va_list aq;
48 int ret, written;
49
50 total_tests++;
51
52 memset(test_buffer, FILL_CHAR, BUF_SIZE);
53 va_copy(aq, ap);
54 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
55 va_end(aq);
56
57 if (ret != elen) {
58 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
59 bufsize, fmt, ret, elen);
60 return 1;
61 }
62
63 if (!bufsize) {
64 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE)) {
65 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
66 fmt);
67 return 1;
68 }
69 return 0;
70 }
71
72 written = min(bufsize-1, elen);
73 if (test_buffer[written]) {
74 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
75 bufsize, fmt);
76 return 1;
77 }
78
79 if (memcmp(test_buffer, expect, written)) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
81 bufsize, fmt, test_buffer, written, expect);
82 return 1;
83 }
84 return 0;
85}
86
87static void __printf(3, 4) __init
88__test(const char *expect, int elen, const char *fmt, ...)
89{
90 va_list ap;
91 int rand;
92 char *p;
93
94 BUG_ON(elen >= BUF_SIZE);
95
96 va_start(ap, fmt);
97
98 /*
99 * Every fmt+args is subjected to four tests: Three where we
100 * tell vsnprintf varying buffer sizes (plenty, not quite
101 * enough and 0), and then we also test that kvasprintf would
102 * be able to print it as expected.
103 */
104 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
105 rand = 1 + prandom_u32_max(elen+1);
106 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
107 failed_tests += do_test(rand, expect, elen, fmt, ap);
108 failed_tests += do_test(0, expect, elen, fmt, ap);
109
110 p = kvasprintf(GFP_KERNEL, fmt, ap);
111 if (p) {
112 if (memcmp(p, expect, elen+1)) {
113 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
114 fmt, p, expect);
115 failed_tests++;
116 }
117 kfree(p);
118 }
119 va_end(ap);
120}
121
122#define test(expect, fmt, ...) \
123 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
124
125static void __init
126test_basic(void)
127{
128 /* Work around annoying "warning: zero-length gnu_printf format string". */
129 char nul = '\0';
130
131 test("", &nul);
132 test("100%", "100%%");
133 test("xxx%yyy", "xxx%cyyy", '%');
134 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
135}
136
137static void __init
138test_number(void)
139{
140 test("0x1234abcd ", "%#-12x", 0x1234abcd);
141 test(" 0x1234abcd", "%#12x", 0x1234abcd);
142 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
143}
144
145static void __init
146test_string(void)
147{
148 test("", "%s%.0s", "", "123");
149 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
150 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
151 /*
152 * POSIX and C99 say that a missing precision should be
153 * treated as a precision of 0. However, the kernel's printf
154 * implementation treats this case as if the . wasn't
155 * present. Let's add a test case documenting the current
156 * behaviour; should anyone ever feel the need to follow the
157 * standards more closely, this can be revisited.
158 */
159 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
160 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
161}
162
163static void __init
164plain(void)
165{
166 test(PTR1_ZEROES PTR1_STR " " PTR2_STR, "%p %p", PTR1, PTR2);
167 /*
168 * The field width is overloaded for some %p extensions to
169 * pass another piece of information. For plain pointers, the
170 * behaviour is slightly odd: One cannot pass either the 0
171 * flag nor a precision to %p without gcc complaining, and if
172 * one explicitly gives a field width, the number is no longer
173 * zero-padded.
174 */
175 test("|" PTR1_STR PTR1_SPACES " | " PTR1_SPACES PTR1_STR "|",
176 "|%-*p|%*p|", PTR_WIDTH+2, PTR1, PTR_WIDTH+2, PTR1);
177 test("|" PTR2_STR " | " PTR2_STR "|",
178 "|%-*p|%*p|", PTR_WIDTH+2, PTR2, PTR_WIDTH+2, PTR2);
179
180 /*
181 * Unrecognized %p extensions are treated as plain %p, but the
182 * alphanumeric suffix is ignored (that is, does not occur in
183 * the output.)
184 */
185 test("|"PTR1_ZEROES PTR1_STR"|", "|%p0y|", PTR1);
186 test("|"PTR2_STR"|", "|%p0y|", PTR2);
187}
188
189static void __init
190symbol_ptr(void)
191{
192}
193
194static void __init
195kernel_ptr(void)
196{
197}
198
199static void __init
200struct_resource(void)
201{
202}
203
204static void __init
205addr(void)
206{
207}
208
209static void __init
210escaped_str(void)
211{
212}
213
214static void __init
215hex_string(void)
216{
217 const char buf[3] = {0xc0, 0xff, 0xee};
218
219 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
220 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
221 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
222 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
223}
224
225static void __init
226mac(void)
227{
228 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
229
230 test("2d:48:d6:fc:7a:05", "%pM", addr);
231 test("05:7a:fc:d6:48:2d", "%pMR", addr);
232 test("2d-48-d6-fc-7a-05", "%pMF", addr);
233 test("2d48d6fc7a05", "%pm", addr);
234 test("057afcd6482d", "%pmR", addr);
235}
236
237static void __init
238ip4(void)
239{
240 struct sockaddr_in sa;
241
242 sa.sin_family = AF_INET;
243 sa.sin_port = cpu_to_be16(12345);
244 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
245
246 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
247 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
248 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
249 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
250}
251
252static void __init
253ip6(void)
254{
255}
256
257static void __init
258ip(void)
259{
260 ip4();
261 ip6();
262}
263
264static void __init
265uuid(void)
266{
267 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
268 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
269
270 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
271 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
272 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
273 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
274}
275
276static void __init
277dentry(void)
278{
279}
280
281static void __init
282struct_va_format(void)
283{
284}
285
286static void __init
287struct_clk(void)
288{
289}
290
291static void __init
292bitmap(void)
293{
294 DECLARE_BITMAP(bits, 20);
295 const int primes[] = {2,3,5,7,11,13,17,19};
296 int i;
297
298 bitmap_zero(bits, 20);
299 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
300 test("|", "%20pbl|%*pbl", bits, 20, bits);
301
302 for (i = 0; i < ARRAY_SIZE(primes); ++i)
303 set_bit(primes[i], bits);
304 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
305 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
306
307 bitmap_fill(bits, 20);
308 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
309 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
310}
311
312static void __init
313netdev_features(void)
314{
315}
316
317static void __init
318test_pointer(void)
319{
320 plain();
321 symbol_ptr();
322 kernel_ptr();
323 struct_resource();
324 addr();
325 escaped_str();
326 hex_string();
327 mac();
328 ip();
329 uuid();
330 dentry();
331 struct_va_format();
332 struct_clk();
333 bitmap();
334 netdev_features();
335}
336
337static int __init
338test_printf_init(void)
339{
340 test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
341 if (!test_buffer)
342 return -ENOMEM;
343
344 test_basic();
345 test_number();
346 test_string();
347 test_pointer();
348
349 kfree(test_buffer);
350
351 if (failed_tests == 0)
352 pr_info("all %u tests passed\n", total_tests);
353 else
354 pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
355
356 return failed_tests ? -EINVAL : 0;
357}
358
359module_init(test_printf_init);
360
361MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
362MODULE_LICENSE("GPL");
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 95cd63b43b99..f9cee8e1233c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1449,6 +1449,8 @@ int kptr_restrict __read_mostly;
1449 * (legacy clock framework) of the clock 1449 * (legacy clock framework) of the clock
1450 * - 'Cr' For a clock, it prints the current rate of the clock 1450 * - 'Cr' For a clock, it prints the current rate of the clock
1451 * 1451 *
1452 * ** Please update also Documentation/printk-formats.txt when making changes **
1453 *
1452 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 1454 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1453 * function pointers are really function descriptors, which contain a 1455 * function pointers are really function descriptors, which contain a
1454 * pointer to the real address. 1456 * pointer to the real address.
@@ -1457,7 +1459,7 @@ static noinline_for_stack
1457char *pointer(const char *fmt, char *buf, char *end, void *ptr, 1459char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1458 struct printf_spec spec) 1460 struct printf_spec spec)
1459{ 1461{
1460 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 1462 const int default_width = 2 * sizeof(void *);
1461 1463
1462 if (!ptr && *fmt != 'K') { 1464 if (!ptr && *fmt != 'K') {
1463 /* 1465 /*
@@ -1769,14 +1771,14 @@ qualifier:
1769 1771
1770 case 'n': 1772 case 'n':
1771 /* 1773 /*
1772 * Since %n poses a greater security risk than utility, treat 1774 * Since %n poses a greater security risk than
1773 * it as an invalid format specifier. Warn about its use so 1775 * utility, treat it as any other invalid or
1774 * that new instances don't get added. 1776 * unsupported format specifier.
1775 */ 1777 */
1776 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt);
1777 /* Fall-through */ 1778 /* Fall-through */
1778 1779
1779 default: 1780 default:
1781 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
1780 spec->type = FORMAT_TYPE_INVALID; 1782 spec->type = FORMAT_TYPE_INVALID;
1781 return fmt - start; 1783 return fmt - start;
1782 } 1784 }
@@ -1811,41 +1813,16 @@ qualifier:
1811 * @fmt: The format string to use 1813 * @fmt: The format string to use
1812 * @args: Arguments for the format string 1814 * @args: Arguments for the format string
1813 * 1815 *
1814 * This function follows C99 vsnprintf, but has some extensions: 1816 * This function generally follows C99 vsnprintf, but has some
1815 * %pS output the name of a text symbol with offset 1817 * extensions and a few limitations:
1816 * %ps output the name of a text symbol without offset 1818 *
1817 * %pF output the name of a function pointer with its offset 1819 * %n is unsupported
1818 * %pf output the name of a function pointer without its offset 1820 * %p* is handled by pointer()
1819 * %pB output the name of a backtrace symbol with its offset
1820 * %pR output the address range in a struct resource with decoded flags
1821 * %pr output the address range in a struct resource with raw flags
1822 * %pb output the bitmap with field width as the number of bits
1823 * %pbl output the bitmap as range list with field width as the number of bits
1824 * %pM output a 6-byte MAC address with colons
1825 * %pMR output a 6-byte MAC address with colons in reversed order
1826 * %pMF output a 6-byte MAC address with dashes
1827 * %pm output a 6-byte MAC address without colons
1828 * %pmR output a 6-byte MAC address without colons in reversed order
1829 * %pI4 print an IPv4 address without leading zeros
1830 * %pi4 print an IPv4 address with leading zeros
1831 * %pI6 print an IPv6 address with colons
1832 * %pi6 print an IPv6 address without colons
1833 * %pI6c print an IPv6 address as specified by RFC 5952
1834 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1835 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1836 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1837 * case.
1838 * %*pE[achnops] print an escaped buffer
1839 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1840 * bytes of the input)
1841 * %pC output the name (Common Clock Framework) or address (legacy clock
1842 * framework) of a clock
1843 * %pCn output the name (Common Clock Framework) or address (legacy clock
1844 * framework) of a clock
1845 * %pCr output the current rate of a clock
1846 * %n is ignored
1847 * 1821 *
1848 * ** Please update Documentation/printk-formats.txt when making changes ** 1822 * See pointer() or Documentation/printk-formats.txt for more
1823 * extensive description.
1824 *
1825 * ** Please update the documentation in both places when making changes **
1849 * 1826 *
1850 * The return value is the number of characters which would 1827 * The return value is the number of characters which would
1851 * be generated for the given input, excluding the trailing 1828 * be generated for the given input, excluding the trailing
@@ -1944,10 +1921,15 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1944 break; 1921 break;
1945 1922
1946 case FORMAT_TYPE_INVALID: 1923 case FORMAT_TYPE_INVALID:
1947 if (str < end) 1924 /*
1948 *str = '%'; 1925 * Presumably the arguments passed gcc's type
1949 ++str; 1926 * checking, but there is no safe or sane way
1950 break; 1927 * for us to continue parsing the format and
1928 * fetching from the va_list; the remaining
1929 * specifiers and arguments would be out of
1930 * sync.
1931 */
1932 goto out;
1951 1933
1952 default: 1934 default:
1953 switch (spec.type) { 1935 switch (spec.type) {
@@ -1992,6 +1974,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1992 } 1974 }
1993 } 1975 }
1994 1976
1977out:
1995 if (size > 0) { 1978 if (size > 0) {
1996 if (str < end) 1979 if (str < end)
1997 *str = '\0'; 1980 *str = '\0';
@@ -2189,9 +2172,10 @@ do { \
2189 2172
2190 switch (spec.type) { 2173 switch (spec.type) {
2191 case FORMAT_TYPE_NONE: 2174 case FORMAT_TYPE_NONE:
2192 case FORMAT_TYPE_INVALID:
2193 case FORMAT_TYPE_PERCENT_CHAR: 2175 case FORMAT_TYPE_PERCENT_CHAR:
2194 break; 2176 break;
2177 case FORMAT_TYPE_INVALID:
2178 goto out;
2195 2179
2196 case FORMAT_TYPE_WIDTH: 2180 case FORMAT_TYPE_WIDTH:
2197 case FORMAT_TYPE_PRECISION: 2181 case FORMAT_TYPE_PRECISION:
@@ -2253,6 +2237,7 @@ do { \
2253 } 2237 }
2254 } 2238 }
2255 2239
2240out:
2256 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 2241 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
2257#undef save_arg 2242#undef save_arg
2258} 2243}
@@ -2286,7 +2271,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2286 char *str, *end; 2271 char *str, *end;
2287 const char *args = (const char *)bin_buf; 2272 const char *args = (const char *)bin_buf;
2288 2273
2289 if (WARN_ON_ONCE((int) size < 0)) 2274 if (WARN_ON_ONCE(size > INT_MAX))
2290 return 0; 2275 return 0;
2291 2276
2292 str = buf; 2277 str = buf;
@@ -2375,12 +2360,14 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2375 break; 2360 break;
2376 2361
2377 case FORMAT_TYPE_PERCENT_CHAR: 2362 case FORMAT_TYPE_PERCENT_CHAR:
2378 case FORMAT_TYPE_INVALID:
2379 if (str < end) 2363 if (str < end)
2380 *str = '%'; 2364 *str = '%';
2381 ++str; 2365 ++str;
2382 break; 2366 break;
2383 2367
2368 case FORMAT_TYPE_INVALID:
2369 goto out;
2370
2384 default: { 2371 default: {
2385 unsigned long long num; 2372 unsigned long long num;
2386 2373
@@ -2423,6 +2410,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2423 } /* switch(spec.type) */ 2410 } /* switch(spec.type) */
2424 } /* while(*fmt) */ 2411 } /* while(*fmt) */
2425 2412
2413out:
2426 if (size > 0) { 2414 if (size > 0) {
2427 if (str < end) 2415 if (str < end)
2428 *str = '\0'; 2416 *str = '\0';