summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/842/842.h2
-rw-r--r--lib/842/842_compress.c13
-rw-r--r--lib/842/842_decompress.c17
-rw-r--r--lib/Kconfig1
-rw-r--r--lib/Kconfig.debug13
-rw-r--r--lib/Kconfig.kasan3
-rw-r--r--lib/Makefile4
-rw-r--r--lib/devres.c2
-rw-r--r--lib/digsig.c7
-rw-r--r--lib/div64.c2
-rw-r--r--lib/dma-debug.c10
-rw-r--r--lib/dynamic_debug.c8
-rw-r--r--lib/fault-inject.c2
-rw-r--r--lib/halfmd4.c3
-rw-r--r--lib/hexdump.c6
-rw-r--r--lib/idr.c4
-rw-r--r--lib/iommu-common.c16
-rw-r--r--lib/is_single_threaded.c5
-rw-r--r--lib/kasprintf.c16
-rw-r--r--lib/kobject.c42
-rw-r--r--lib/llist.c4
-rw-r--r--lib/mpi/longlong.h2
-rw-r--r--lib/mpi/mpicoder.c201
-rw-r--r--lib/nmi_backtrace.c11
-rw-r--r--lib/once.c62
-rw-r--r--lib/percpu_ida.c2
-rw-r--r--lib/radix-tree.c10
-rw-r--r--lib/random32.c37
-rw-r--r--lib/rhashtable.c5
-rw-r--r--lib/string.c91
-rw-r--r--lib/test-string_helpers.c36
-rw-r--r--lib/test_bpf.c30
-rw-r--r--lib/test_kasan.c69
-rw-r--r--lib/test_printf.c362
-rw-r--r--lib/vsprintf.c80
35 files changed, 1054 insertions, 124 deletions
diff --git a/lib/842/842.h b/lib/842/842.h
index 7c200030acf7..e0a122bc1cdb 100644
--- a/lib/842/842.h
+++ b/lib/842/842.h
@@ -76,6 +76,7 @@
76#include <linux/module.h> 76#include <linux/module.h>
77#include <linux/kernel.h> 77#include <linux/kernel.h>
78#include <linux/bitops.h> 78#include <linux/bitops.h>
79#include <linux/crc32.h>
79#include <asm/unaligned.h> 80#include <asm/unaligned.h>
80 81
81#include <linux/sw842.h> 82#include <linux/sw842.h>
@@ -98,6 +99,7 @@
98#define I2_BITS (8) 99#define I2_BITS (8)
99#define I4_BITS (9) 100#define I4_BITS (9)
100#define I8_BITS (8) 101#define I8_BITS (8)
102#define CRC_BITS (32)
101 103
102#define REPEAT_BITS_MAX (0x3f) 104#define REPEAT_BITS_MAX (0x3f)
103#define SHORT_DATA_BITS_MAX (0x7) 105#define SHORT_DATA_BITS_MAX (0x7)
diff --git a/lib/842/842_compress.c b/lib/842/842_compress.c
index 7ce68948e68c..4051339bdfbd 100644
--- a/lib/842/842_compress.c
+++ b/lib/842/842_compress.c
@@ -490,6 +490,7 @@ int sw842_compress(const u8 *in, unsigned int ilen,
490 int ret; 490 int ret;
491 u64 last, next, pad, total; 491 u64 last, next, pad, total;
492 u8 repeat_count = 0; 492 u8 repeat_count = 0;
493 u32 crc;
493 494
494 BUILD_BUG_ON(sizeof(*p) > SW842_MEM_COMPRESS); 495 BUILD_BUG_ON(sizeof(*p) > SW842_MEM_COMPRESS);
495 496
@@ -580,6 +581,18 @@ skip_comp:
580 if (ret) 581 if (ret)
581 return ret; 582 return ret;
582 583
584 /*
585 * crc(0:31) is appended to target data starting with the next
586 * bit after End of stream template.
587 * nx842 calculates CRC for data in big-endian format. So doing
588 * same here so that sw842 decompression can be used for both
589 * compressed data.
590 */
591 crc = crc32_be(0, in, ilen);
592 ret = add_bits(p, crc, CRC_BITS);
593 if (ret)
594 return ret;
595
583 if (p->bit) { 596 if (p->bit) {
584 p->out++; 597 p->out++;
585 p->olen--; 598 p->olen--;
diff --git a/lib/842/842_decompress.c b/lib/842/842_decompress.c
index 5446ff0c9ba0..8881dad2a6a0 100644
--- a/lib/842/842_decompress.c
+++ b/lib/842/842_decompress.c
@@ -285,6 +285,7 @@ int sw842_decompress(const u8 *in, unsigned int ilen,
285 struct sw842_param p; 285 struct sw842_param p;
286 int ret; 286 int ret;
287 u64 op, rep, tmp, bytes, total; 287 u64 op, rep, tmp, bytes, total;
288 u64 crc;
288 289
289 p.in = (u8 *)in; 290 p.in = (u8 *)in;
290 p.bit = 0; 291 p.bit = 0;
@@ -375,6 +376,22 @@ int sw842_decompress(const u8 *in, unsigned int ilen,
375 } 376 }
376 } while (op != OP_END); 377 } while (op != OP_END);
377 378
379 /*
380 * crc(0:31) is saved in compressed data starting with the
381 * next bit after End of stream template.
382 */
383 ret = next_bits(&p, &crc, CRC_BITS);
384 if (ret)
385 return ret;
386
387 /*
388 * Validate CRC saved in compressed data.
389 */
390 if (crc != (u64)crc32_be(0, out, total - p.olen)) {
391 pr_debug("CRC mismatch for decompression\n");
392 return -EINVAL;
393 }
394
378 if (unlikely((total - p.olen) > UINT_MAX)) 395 if (unlikely((total - p.olen) > UINT_MAX))
379 return -ENOSPC; 396 return -ENOSPC;
380 397
diff --git a/lib/Kconfig b/lib/Kconfig
index 2e491ac15622..f0df318104e7 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -220,6 +220,7 @@ config ZLIB_INFLATE
220 220
221config ZLIB_DEFLATE 221config ZLIB_DEFLATE
222 tristate 222 tristate
223 select BITREVERSE
223 224
224config LZO_COMPRESS 225config LZO_COMPRESS
225 tristate 226 tristate
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index ab76b99adc85..8c15b29d5adc 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -197,6 +197,7 @@ config ENABLE_MUST_CHECK
197config FRAME_WARN 197config FRAME_WARN
198 int "Warn for stack frames larger than (needs gcc 4.4)" 198 int "Warn for stack frames larger than (needs gcc 4.4)"
199 range 0 8192 199 range 0 8192
200 default 0 if KASAN
200 default 1024 if !64BIT 201 default 1024 if !64BIT
201 default 2048 if 64BIT 202 default 2048 if 64BIT
202 help 203 help
@@ -311,6 +312,15 @@ config DEBUG_SECTION_MISMATCH
311 - Enable verbose reporting from modpost in order to help resolve 312 - Enable verbose reporting from modpost in order to help resolve
312 the section mismatches that are reported. 313 the section mismatches that are reported.
313 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
314# 324#
315# Select this config option from the architecture Kconfig, if it 325# Select this config option from the architecture Kconfig, if it
316# is preferred to always offer frame pointers as a config 326# is preferred to always offer frame pointers as a config
@@ -1685,6 +1695,9 @@ config TEST_STRING_HELPERS
1685config TEST_KSTRTOX 1695config TEST_KSTRTOX
1686 tristate "Test kstrto*() family of functions at runtime" 1696 tristate "Test kstrto*() family of functions at runtime"
1687 1697
1698config TEST_PRINTF
1699 tristate "Test printf() family of functions at runtime"
1700
1688config TEST_RHASHTABLE 1701config TEST_RHASHTABLE
1689 tristate "Perform selftest on resizable hash table" 1702 tristate "Perform selftest on resizable hash table"
1690 default n 1703 default n
diff --git a/lib/Kconfig.kasan b/lib/Kconfig.kasan
index 39f24d6721e5..0fee5acd5aa0 100644
--- a/lib/Kconfig.kasan
+++ b/lib/Kconfig.kasan
@@ -15,8 +15,7 @@ config KASAN
15 global variables requires gcc 5.0 or later. 15 global variables requires gcc 5.0 or later.
16 This feature consumes about 1/8 of available memory and brings about 16 This feature consumes about 1/8 of available memory and brings about
17 ~x3 performance slowdown. 17 ~x3 performance slowdown.
18 For better error detection enable CONFIG_STACKTRACE, 18 For better error detection enable CONFIG_STACKTRACE.
19 and add slub_debug=U to boot cmdline.
20 19
21choice 20choice
22 prompt "Instrumentation type" 21 prompt "Instrumentation type"
diff --git a/lib/Makefile b/lib/Makefile
index 13a7c6ae3fec..7f1de26613d2 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -26,7 +26,8 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
26 bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \ 26 bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \ 27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iov_iter.o clz_ctz.o \
28 bsearch.o find_bit.o llist.o memweight.o kfifo.o \ 28 bsearch.o find_bit.o llist.o memweight.o kfifo.o \
29 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o 29 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o \
30 once.o
30obj-y += string_helpers.o 31obj-y += string_helpers.o
31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 32obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
32obj-y += hexdump.o 33obj-y += hexdump.o
@@ -41,6 +42,7 @@ obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
41obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o 42obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
42obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o 43obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
43obj-$(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
44 46
45ifeq ($(CONFIG_DEBUG_KOBJECT),y) 47ifeq ($(CONFIG_DEBUG_KOBJECT),y)
46CFLAGS_kobject.o += -DDEBUG 48CFLAGS_kobject.o += -DDEBUG
diff --git a/lib/devres.c b/lib/devres.c
index f13a2468ff39..8c85672639d3 100644
--- a/lib/devres.c
+++ b/lib/devres.c
@@ -418,7 +418,7 @@ void pcim_iounmap_regions(struct pci_dev *pdev, int mask)
418 if (!iomap) 418 if (!iomap)
419 return; 419 return;
420 420
421 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 421 for (i = 0; i < PCIM_IOMAP_MAX; i++) {
422 if (!(mask & (1 << i))) 422 if (!(mask & (1 << i)))
423 continue; 423 continue;
424 424
diff --git a/lib/digsig.c b/lib/digsig.c
index ae05ea393fc8..07be6c1ef4e2 100644
--- a/lib/digsig.c
+++ b/lib/digsig.c
@@ -79,12 +79,13 @@ static int digsig_verify_rsa(struct key *key,
79 unsigned char *out1 = NULL; 79 unsigned char *out1 = NULL;
80 const char *m; 80 const char *m;
81 MPI in = NULL, res = NULL, pkey[2]; 81 MPI in = NULL, res = NULL, pkey[2];
82 uint8_t *p, *datap, *endp; 82 uint8_t *p, *datap;
83 struct user_key_payload *ukp; 83 const uint8_t *endp;
84 const struct user_key_payload *ukp;
84 struct pubkey_hdr *pkh; 85 struct pubkey_hdr *pkh;
85 86
86 down_read(&key->sem); 87 down_read(&key->sem);
87 ukp = key->payload.data; 88 ukp = user_key_payload(key);
88 89
89 if (ukp->datalen < sizeof(*pkh)) 90 if (ukp->datalen < sizeof(*pkh))
90 goto err1; 91 goto err1;
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 dace71fe41f7..8855f019ebe8 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -100,7 +100,7 @@ static LIST_HEAD(free_entries);
100static DEFINE_SPINLOCK(free_entries_lock); 100static DEFINE_SPINLOCK(free_entries_lock);
101 101
102/* Global disable flag - will be set in case of an error */ 102/* Global disable flag - will be set in case of an error */
103static u32 global_disable __read_mostly; 103static bool global_disable __read_mostly;
104 104
105/* Early initialization disable flag, set at the end of dma_debug_init */ 105/* Early initialization disable flag, set at the end of dma_debug_init */
106static bool dma_debug_initialized __read_mostly; 106static bool dma_debug_initialized __read_mostly;
@@ -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/fault-inject.c b/lib/fault-inject.c
index f1cdeb024d17..6a823a53e357 100644
--- a/lib/fault-inject.c
+++ b/lib/fault-inject.c
@@ -44,7 +44,7 @@ static void fail_dump(struct fault_attr *attr)
44 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n" 44 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
45 "name %pd, interval %lu, probability %lu, " 45 "name %pd, interval %lu, probability %lu, "
46 "space %d, times %d\n", attr->dname, 46 "space %d, times %d\n", attr->dname,
47 attr->probability, attr->interval, 47 attr->interval, attr->probability,
48 atomic_read(&attr->space), 48 atomic_read(&attr->space),
49 atomic_read(&attr->times)); 49 atomic_read(&attr->times));
50 if (attr->verbose > 1) 50 if (attr->verbose > 1)
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/iommu-common.c b/lib/iommu-common.c
index ff19f66d3f7f..858dc1aae478 100644
--- a/lib/iommu-common.c
+++ b/lib/iommu-common.c
@@ -11,18 +11,13 @@
11#include <linux/dma-mapping.h> 11#include <linux/dma-mapping.h>
12#include <linux/hash.h> 12#include <linux/hash.h>
13 13
14#ifndef DMA_ERROR_CODE
15#define DMA_ERROR_CODE (~(dma_addr_t)0x0)
16#endif
17
18static unsigned long iommu_large_alloc = 15; 14static unsigned long iommu_large_alloc = 15;
19 15
20static DEFINE_PER_CPU(unsigned int, iommu_hash_common); 16static DEFINE_PER_CPU(unsigned int, iommu_hash_common);
21 17
22static inline bool need_flush(struct iommu_map_table *iommu) 18static inline bool need_flush(struct iommu_map_table *iommu)
23{ 19{
24 return (iommu->lazy_flush != NULL && 20 return ((iommu->flags & IOMMU_NEED_FLUSH) != 0);
25 (iommu->flags & IOMMU_NEED_FLUSH) != 0);
26} 21}
27 22
28static inline void set_flush(struct iommu_map_table *iommu) 23static inline void set_flush(struct iommu_map_table *iommu)
@@ -124,7 +119,7 @@ unsigned long iommu_tbl_range_alloc(struct device *dev,
124 /* Sanity check */ 119 /* Sanity check */
125 if (unlikely(npages == 0)) { 120 if (unlikely(npages == 0)) {
126 WARN_ON_ONCE(1); 121 WARN_ON_ONCE(1);
127 return DMA_ERROR_CODE; 122 return IOMMU_ERROR_CODE;
128 } 123 }
129 124
130 if (largealloc) { 125 if (largealloc) {
@@ -207,11 +202,12 @@ unsigned long iommu_tbl_range_alloc(struct device *dev,
207 goto again; 202 goto again;
208 } else { 203 } else {
209 /* give up */ 204 /* give up */
210 n = DMA_ERROR_CODE; 205 n = IOMMU_ERROR_CODE;
211 goto bail; 206 goto bail;
212 } 207 }
213 } 208 }
214 if (n < pool->hint || need_flush(iommu)) { 209 if (iommu->lazy_flush &&
210 (n < pool->hint || need_flush(iommu))) {
215 clear_flush(iommu); 211 clear_flush(iommu);
216 iommu->lazy_flush(iommu); 212 iommu->lazy_flush(iommu);
217 } 213 }
@@ -259,7 +255,7 @@ void iommu_tbl_range_free(struct iommu_map_table *iommu, u64 dma_addr,
259 unsigned long flags; 255 unsigned long flags;
260 unsigned long shift = iommu->table_shift; 256 unsigned long shift = iommu->table_shift;
261 257
262 if (entry == DMA_ERROR_CODE) /* use default addr->entry mapping */ 258 if (entry == IOMMU_ERROR_CODE) /* use default addr->entry mapping */
263 entry = (dma_addr - iommu->table_map_base) >> shift; 259 entry = (dma_addr - iommu->table_map_base) >> shift;
264 pool = get_pool(iommu, entry); 260 pool = get_pool(iommu, entry);
265 261
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 3e3a5c3cb330..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);
@@ -568,6 +582,7 @@ void kobject_del(struct kobject *kobj)
568 kobject_put(kobj->parent); 582 kobject_put(kobj->parent);
569 kobj->parent = NULL; 583 kobj->parent = NULL;
570} 584}
585EXPORT_SYMBOL(kobject_del);
571 586
572/** 587/**
573 * kobject_get - increment refcount for object. 588 * kobject_get - increment refcount for object.
@@ -584,6 +599,7 @@ struct kobject *kobject_get(struct kobject *kobj)
584 } 599 }
585 return kobj; 600 return kobj;
586} 601}
602EXPORT_SYMBOL(kobject_get);
587 603
588static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj) 604static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
589{ 605{
@@ -632,7 +648,7 @@ static void kobject_cleanup(struct kobject *kobj)
632 /* free name if we allocated it */ 648 /* free name if we allocated it */
633 if (name) { 649 if (name) {
634 pr_debug("kobject: '%s': free name\n", name); 650 pr_debug("kobject: '%s': free name\n", name);
635 kfree(name); 651 kfree_const(name);
636 } 652 }
637} 653}
638 654
@@ -675,6 +691,7 @@ void kobject_put(struct kobject *kobj)
675 kref_put(&kobj->kref, kobject_release); 691 kref_put(&kobj->kref, kobject_release);
676 } 692 }
677} 693}
694EXPORT_SYMBOL(kobject_put);
678 695
679static void dynamic_kobj_release(struct kobject *kobj) 696static void dynamic_kobj_release(struct kobject *kobj)
680{ 697{
@@ -803,6 +820,7 @@ int kset_register(struct kset *k)
803 kobject_uevent(&k->kobj, KOBJ_ADD); 820 kobject_uevent(&k->kobj, KOBJ_ADD);
804 return 0; 821 return 0;
805} 822}
823EXPORT_SYMBOL(kset_register);
806 824
807/** 825/**
808 * kset_unregister - remove a kset. 826 * kset_unregister - remove a kset.
@@ -815,6 +833,7 @@ void kset_unregister(struct kset *k)
815 kobject_del(&k->kobj); 833 kobject_del(&k->kobj);
816 kobject_put(&k->kobj); 834 kobject_put(&k->kobj);
817} 835}
836EXPORT_SYMBOL(kset_unregister);
818 837
819/** 838/**
820 * kset_find_obj - search for object in kset. 839 * kset_find_obj - search for object in kset.
@@ -1051,10 +1070,3 @@ void kobj_ns_drop(enum kobj_ns_type type, void *ns)
1051 kobj_ns_ops_tbl[type]->drop_ns(ns); 1070 kobj_ns_ops_tbl[type]->drop_ns(ns);
1052 spin_unlock(&kobj_ns_type_lock); 1071 spin_unlock(&kobj_ns_type_lock);
1053} 1072}
1054
1055EXPORT_SYMBOL(kobject_get);
1056EXPORT_SYMBOL(kobject_put);
1057EXPORT_SYMBOL(kobject_del);
1058
1059EXPORT_SYMBOL(kset_register);
1060EXPORT_SYMBOL(kset_unregister);
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/mpi/longlong.h b/lib/mpi/longlong.h
index a89d041592c8..b90e255c2a68 100644
--- a/lib/mpi/longlong.h
+++ b/lib/mpi/longlong.h
@@ -19,7 +19,7 @@
19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 19 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20 * MA 02111-1307, USA. */ 20 * MA 02111-1307, USA. */
21 21
22#include <asm-generic/bitops/count_zeros.h> 22#include <linux/count_zeros.h>
23 23
24/* You have to define the following before including this file: 24/* You have to define the following before including this file:
25 * 25 *
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index 95c52a95259e..3db76b8c1115 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -19,7 +19,7 @@
19 */ 19 */
20 20
21#include <linux/bitops.h> 21#include <linux/bitops.h>
22#include <asm-generic/bitops/count_zeros.h> 22#include <linux/count_zeros.h>
23#include "mpi-internal.h" 23#include "mpi-internal.h"
24 24
25#define MAX_EXTERN_MPI_BITS 16384 25#define MAX_EXTERN_MPI_BITS 16384
@@ -319,3 +319,202 @@ int mpi_set_buffer(MPI a, const void *xbuffer, unsigned nbytes, int sign)
319 return 0; 319 return 0;
320} 320}
321EXPORT_SYMBOL_GPL(mpi_set_buffer); 321EXPORT_SYMBOL_GPL(mpi_set_buffer);
322
323/**
324 * mpi_write_to_sgl() - Funnction exports MPI to an sgl (msb first)
325 *
326 * This function works in the same way as the mpi_read_buffer, but it
327 * takes an sgl instead of u8 * buf.
328 *
329 * @a: a multi precision integer
330 * @sgl: scatterlist to write to. Needs to be at least
331 * mpi_get_size(a) long.
332 * @nbytes: in/out param - it has the be set to the maximum number of
333 * bytes that can be written to sgl. This has to be at least
334 * the size of the integer a. On return it receives the actual
335 * length of the data written.
336 * @sign: if not NULL, it will be set to the sign of a.
337 *
338 * Return: 0 on success or error code in case of error
339 */
340int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
341 int *sign)
342{
343 u8 *p, *p2;
344 mpi_limb_t alimb, alimb2;
345 unsigned int n = mpi_get_size(a);
346 int i, x, y = 0, lzeros = 0, buf_len;
347
348 if (!nbytes || *nbytes < n)
349 return -EINVAL;
350
351 if (sign)
352 *sign = a->sign;
353
354 p = (void *)&a->d[a->nlimbs] - 1;
355
356 for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
357 if (!*p)
358 lzeros++;
359 else
360 break;
361 }
362
363 *nbytes = n - lzeros;
364 buf_len = sgl->length;
365 p2 = sg_virt(sgl);
366
367 for (i = a->nlimbs - 1; i >= 0; i--) {
368 alimb = a->d[i];
369 p = (u8 *)&alimb2;
370#if BYTES_PER_MPI_LIMB == 4
371 *p++ = alimb >> 24;
372 *p++ = alimb >> 16;
373 *p++ = alimb >> 8;
374 *p++ = alimb;
375#elif BYTES_PER_MPI_LIMB == 8
376 *p++ = alimb >> 56;
377 *p++ = alimb >> 48;
378 *p++ = alimb >> 40;
379 *p++ = alimb >> 32;
380 *p++ = alimb >> 24;
381 *p++ = alimb >> 16;
382 *p++ = alimb >> 8;
383 *p++ = alimb;
384#else
385#error please implement for this limb size.
386#endif
387 if (lzeros > 0) {
388 if (lzeros >= sizeof(alimb)) {
389 p -= sizeof(alimb);
390 continue;
391 } else {
392 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
393 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
394 + lzeros;
395 *limb1 = *limb2;
396 p -= lzeros;
397 y = lzeros;
398 }
399 lzeros -= sizeof(alimb);
400 }
401
402 p = p - (sizeof(alimb) - y);
403
404 for (x = 0; x < sizeof(alimb) - y; x++) {
405 if (!buf_len) {
406 sgl = sg_next(sgl);
407 if (!sgl)
408 return -EINVAL;
409 buf_len = sgl->length;
410 p2 = sg_virt(sgl);
411 }
412 *p2++ = *p++;
413 buf_len--;
414 }
415 y = 0;
416 }
417 return 0;
418}
419EXPORT_SYMBOL_GPL(mpi_write_to_sgl);
420
421/*
422 * mpi_read_raw_from_sgl() - Function allocates an MPI and populates it with
423 * data from the sgl
424 *
425 * This function works in the same way as the mpi_read_raw_data, but it
426 * takes an sgl instead of void * buffer. i.e. it allocates
427 * a new MPI and reads the content of the sgl to the MPI.
428 *
429 * @sgl: scatterlist to read from
430 * @len: number of bytes to read
431 *
432 * Return: Pointer to a new MPI or NULL on error
433 */
434MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len)
435{
436 struct scatterlist *sg;
437 int x, i, j, z, lzeros, ents;
438 unsigned int nbits, nlimbs, nbytes;
439 mpi_limb_t a;
440 MPI val = NULL;
441
442 lzeros = 0;
443 ents = sg_nents(sgl);
444
445 for_each_sg(sgl, sg, ents, i) {
446 const u8 *buff = sg_virt(sg);
447 int len = sg->length;
448
449 while (len && !*buff) {
450 lzeros++;
451 len--;
452 buff++;
453 }
454
455 if (len && *buff)
456 break;
457
458 ents--;
459 lzeros = 0;
460 }
461
462 sgl = sg;
463
464 if (!ents)
465 nbytes = 0;
466 else
467 nbytes = len - lzeros;
468
469 nbits = nbytes * 8;
470 if (nbits > MAX_EXTERN_MPI_BITS) {
471 pr_info("MPI: mpi too large (%u bits)\n", nbits);
472 return NULL;
473 }
474
475 if (nbytes > 0)
476 nbits -= count_leading_zeros(*(u8 *)(sg_virt(sgl) + lzeros));
477 else
478 nbits = 0;
479
480 nlimbs = DIV_ROUND_UP(nbytes, BYTES_PER_MPI_LIMB);
481 val = mpi_alloc(nlimbs);
482 if (!val)
483 return NULL;
484
485 val->nbits = nbits;
486 val->sign = 0;
487 val->nlimbs = nlimbs;
488
489 if (nbytes == 0)
490 return val;
491
492 j = nlimbs - 1;
493 a = 0;
494 z = 0;
495 x = BYTES_PER_MPI_LIMB - nbytes % BYTES_PER_MPI_LIMB;
496 x %= BYTES_PER_MPI_LIMB;
497
498 for_each_sg(sgl, sg, ents, i) {
499 const u8 *buffer = sg_virt(sg) + lzeros;
500 int len = sg->length - lzeros;
501 int buf_shift = x;
502
503 if (sg_is_last(sg) && (len % BYTES_PER_MPI_LIMB))
504 len += BYTES_PER_MPI_LIMB - (len % BYTES_PER_MPI_LIMB);
505
506 for (; x < len + buf_shift; x++) {
507 a <<= 8;
508 a |= *buffer++;
509 if (((z + x + 1) % BYTES_PER_MPI_LIMB) == 0) {
510 val->d[j--] = a;
511 a = 0;
512 }
513 }
514 z += x;
515 x = 0;
516 lzeros = 0;
517 }
518 return val;
519}
520EXPORT_SYMBOL_GPL(mpi_read_raw_from_sgl);
diff --git a/lib/nmi_backtrace.c b/lib/nmi_backtrace.c
index 88d3d32e5923..6019c53c669e 100644
--- a/lib/nmi_backtrace.c
+++ b/lib/nmi_backtrace.c
@@ -43,6 +43,12 @@ static void print_seq_line(struct nmi_seq_buf *s, int start, int end)
43 printk("%.*s", (end - start) + 1, buf); 43 printk("%.*s", (end - start) + 1, buf);
44} 44}
45 45
46/*
47 * When raise() is called it will be is passed a pointer to the
48 * backtrace_mask. Architectures that call nmi_cpu_backtrace()
49 * directly from their raise() functions may rely on the mask
50 * they are passed being updated as a side effect of this call.
51 */
46void nmi_trigger_all_cpu_backtrace(bool include_self, 52void nmi_trigger_all_cpu_backtrace(bool include_self,
47 void (*raise)(cpumask_t *mask)) 53 void (*raise)(cpumask_t *mask))
48{ 54{
@@ -149,7 +155,10 @@ bool nmi_cpu_backtrace(struct pt_regs *regs)
149 /* Replace printk to write into the NMI seq */ 155 /* Replace printk to write into the NMI seq */
150 this_cpu_write(printk_func, nmi_vprintk); 156 this_cpu_write(printk_func, nmi_vprintk);
151 pr_warn("NMI backtrace for cpu %d\n", cpu); 157 pr_warn("NMI backtrace for cpu %d\n", cpu);
152 show_regs(regs); 158 if (regs)
159 show_regs(regs);
160 else
161 dump_stack();
153 this_cpu_write(printk_func, printk_func_save); 162 this_cpu_write(printk_func, printk_func_save);
154 163
155 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); 164 cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask));
diff --git a/lib/once.c b/lib/once.c
new file mode 100644
index 000000000000..05c8604627eb
--- /dev/null
+++ b/lib/once.c
@@ -0,0 +1,62 @@
1#include <linux/slab.h>
2#include <linux/spinlock.h>
3#include <linux/once.h>
4#include <linux/random.h>
5
6struct once_work {
7 struct work_struct work;
8 struct static_key *key;
9};
10
11static void once_deferred(struct work_struct *w)
12{
13 struct once_work *work;
14
15 work = container_of(w, struct once_work, work);
16 BUG_ON(!static_key_enabled(work->key));
17 static_key_slow_dec(work->key);
18 kfree(work);
19}
20
21static void once_disable_jump(struct static_key *key)
22{
23 struct once_work *w;
24
25 w = kmalloc(sizeof(*w), GFP_ATOMIC);
26 if (!w)
27 return;
28
29 INIT_WORK(&w->work, once_deferred);
30 w->key = key;
31 schedule_work(&w->work);
32}
33
34static DEFINE_SPINLOCK(once_lock);
35
36bool __do_once_start(bool *done, unsigned long *flags)
37 __acquires(once_lock)
38{
39 spin_lock_irqsave(&once_lock, *flags);
40 if (*done) {
41 spin_unlock_irqrestore(&once_lock, *flags);
42 /* Keep sparse happy by restoring an even lock count on
43 * this lock. In case we return here, we don't call into
44 * __do_once_done but return early in the DO_ONCE() macro.
45 */
46 __acquire(once_lock);
47 return false;
48 }
49
50 return true;
51}
52EXPORT_SYMBOL(__do_once_start);
53
54void __do_once_done(bool *done, struct static_key *once_key,
55 unsigned long *flags)
56 __releases(once_lock)
57{
58 *done = true;
59 spin_unlock_irqrestore(&once_lock, *flags);
60 once_disable_jump(once_key);
61}
62EXPORT_SYMBOL(__do_once_done);
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/random32.c b/lib/random32.c
index 0bee183fa18f..12111910ccd0 100644
--- a/lib/random32.c
+++ b/lib/random32.c
@@ -181,7 +181,7 @@ void prandom_seed(u32 entropy)
181 * No locking on the CPUs, but then somewhat random results are, well, 181 * No locking on the CPUs, but then somewhat random results are, well,
182 * expected. 182 * expected.
183 */ 183 */
184 for_each_possible_cpu (i) { 184 for_each_possible_cpu(i) {
185 struct rnd_state *state = &per_cpu(net_rand_state, i); 185 struct rnd_state *state = &per_cpu(net_rand_state, i);
186 186
187 state->s1 = __seed(state->s1 ^ entropy, 2U); 187 state->s1 = __seed(state->s1 ^ entropy, 2U);
@@ -201,7 +201,7 @@ static int __init prandom_init(void)
201 prandom_state_selftest(); 201 prandom_state_selftest();
202 202
203 for_each_possible_cpu(i) { 203 for_each_possible_cpu(i) {
204 struct rnd_state *state = &per_cpu(net_rand_state,i); 204 struct rnd_state *state = &per_cpu(net_rand_state, i);
205 u32 weak_seed = (i + jiffies) ^ random_get_entropy(); 205 u32 weak_seed = (i + jiffies) ^ random_get_entropy();
206 206
207 prandom_seed_early(state, weak_seed, true); 207 prandom_seed_early(state, weak_seed, true);
@@ -238,13 +238,30 @@ static void __init __prandom_start_seed_timer(void)
238 add_timer(&seed_timer); 238 add_timer(&seed_timer);
239} 239}
240 240
241void prandom_seed_full_state(struct rnd_state __percpu *pcpu_state)
242{
243 int i;
244
245 for_each_possible_cpu(i) {
246 struct rnd_state *state = per_cpu_ptr(pcpu_state, i);
247 u32 seeds[4];
248
249 get_random_bytes(&seeds, sizeof(seeds));
250 state->s1 = __seed(seeds[0], 2U);
251 state->s2 = __seed(seeds[1], 8U);
252 state->s3 = __seed(seeds[2], 16U);
253 state->s4 = __seed(seeds[3], 128U);
254
255 prandom_warmup(state);
256 }
257}
258
241/* 259/*
242 * Generate better values after random number generator 260 * Generate better values after random number generator
243 * is fully initialized. 261 * is fully initialized.
244 */ 262 */
245static void __prandom_reseed(bool late) 263static void __prandom_reseed(bool late)
246{ 264{
247 int i;
248 unsigned long flags; 265 unsigned long flags;
249 static bool latch = false; 266 static bool latch = false;
250 static DEFINE_SPINLOCK(lock); 267 static DEFINE_SPINLOCK(lock);
@@ -266,19 +283,7 @@ static void __prandom_reseed(bool late)
266 goto out; 283 goto out;
267 284
268 latch = true; 285 latch = true;
269 286 prandom_seed_full_state(&net_rand_state);
270 for_each_possible_cpu(i) {
271 struct rnd_state *state = &per_cpu(net_rand_state,i);
272 u32 seeds[4];
273
274 get_random_bytes(&seeds, sizeof(seeds));
275 state->s1 = __seed(seeds[0], 2U);
276 state->s2 = __seed(seeds[1], 8U);
277 state->s3 = __seed(seeds[2], 16U);
278 state->s4 = __seed(seeds[3], 128U);
279
280 prandom_warmup(state);
281 }
282out: 287out:
283 spin_unlock_irqrestore(&lock, flags); 288 spin_unlock_irqrestore(&lock, flags);
284} 289}
diff --git a/lib/rhashtable.c b/lib/rhashtable.c
index cc0c69710dcf..a54ff8949f91 100644
--- a/lib/rhashtable.c
+++ b/lib/rhashtable.c
@@ -187,10 +187,7 @@ static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
187 head = rht_dereference_bucket(new_tbl->buckets[new_hash], 187 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
188 new_tbl, new_hash); 188 new_tbl, new_hash);
189 189
190 if (rht_is_a_nulls(head)) 190 RCU_INIT_POINTER(entry->next, head);
191 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
192 else
193 RCU_INIT_POINTER(entry->next, head);
194 191
195 rcu_assign_pointer(new_tbl->buckets[new_hash], entry); 192 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
196 spin_unlock(new_bucket_lock); 193 spin_unlock(new_bucket_lock);
diff --git a/lib/string.c b/lib/string.c
index 13d1e84ddb80..0323c0d5629a 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -27,6 +27,10 @@
27#include <linux/bug.h> 27#include <linux/bug.h>
28#include <linux/errno.h> 28#include <linux/errno.h>
29 29
30#include <asm/byteorder.h>
31#include <asm/word-at-a-time.h>
32#include <asm/page.h>
33
30#ifndef __HAVE_ARCH_STRNCASECMP 34#ifndef __HAVE_ARCH_STRNCASECMP
31/** 35/**
32 * strncasecmp - Case insensitive, length-limited string comparison 36 * strncasecmp - Case insensitive, length-limited string comparison
@@ -146,6 +150,91 @@ size_t strlcpy(char *dest, const char *src, size_t size)
146EXPORT_SYMBOL(strlcpy); 150EXPORT_SYMBOL(strlcpy);
147#endif 151#endif
148 152
153#ifndef __HAVE_ARCH_STRSCPY
154/**
155 * strscpy - Copy a C-string into a sized buffer
156 * @dest: Where to copy the string to
157 * @src: Where to copy the string from
158 * @count: Size of destination buffer
159 *
160 * Copy the string, or as much of it as fits, into the dest buffer.
161 * The routine returns the number of characters copied (not including
162 * the trailing NUL) or -E2BIG if the destination buffer wasn't big enough.
163 * The behavior is undefined if the string buffers overlap.
164 * The destination buffer is always NUL terminated, unless it's zero-sized.
165 *
166 * Preferred to strlcpy() since the API doesn't require reading memory
167 * from the src string beyond the specified "count" bytes, and since
168 * the return value is easier to error-check than strlcpy()'s.
169 * In addition, the implementation is robust to the string changing out
170 * from underneath it, unlike the current strlcpy() implementation.
171 *
172 * Preferred to strncpy() since it always returns a valid string, and
173 * doesn't unnecessarily force the tail of the destination buffer to be
174 * zeroed. If the zeroing is desired, it's likely cleaner to use strscpy()
175 * with an overflow test, then just memset() the tail of the dest buffer.
176 */
177ssize_t strscpy(char *dest, const char *src, size_t count)
178{
179 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
180 size_t max = count;
181 long res = 0;
182
183 if (count == 0)
184 return -E2BIG;
185
186#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
187 /*
188 * If src is unaligned, don't cross a page boundary,
189 * since we don't know if the next page is mapped.
190 */
191 if ((long)src & (sizeof(long) - 1)) {
192 size_t limit = PAGE_SIZE - ((long)src & (PAGE_SIZE - 1));
193 if (limit < max)
194 max = limit;
195 }
196#else
197 /* If src or dest is unaligned, don't do word-at-a-time. */
198 if (((long) dest | (long) src) & (sizeof(long) - 1))
199 max = 0;
200#endif
201
202 while (max >= sizeof(unsigned long)) {
203 unsigned long c, data;
204
205 c = *(unsigned long *)(src+res);
206 if (has_zero(c, &data, &constants)) {
207 data = prep_zero_mask(c, data, &constants);
208 data = create_zero_mask(data);
209 *(unsigned long *)(dest+res) = c & zero_bytemask(data);
210 return res + find_zero(data);
211 }
212 *(unsigned long *)(dest+res) = c;
213 res += sizeof(unsigned long);
214 count -= sizeof(unsigned long);
215 max -= sizeof(unsigned long);
216 }
217
218 while (count) {
219 char c;
220
221 c = src[res];
222 dest[res] = c;
223 if (!c)
224 return res;
225 res++;
226 count--;
227 }
228
229 /* Hit buffer length without finding a NUL; force NUL-termination. */
230 if (res)
231 dest[res-1] = '\0';
232
233 return -E2BIG;
234}
235EXPORT_SYMBOL(strscpy);
236#endif
237
149#ifndef __HAVE_ARCH_STRCAT 238#ifndef __HAVE_ARCH_STRCAT
150/** 239/**
151 * strcat - Append one %NUL-terminated string to another 240 * strcat - Append one %NUL-terminated string to another
@@ -815,7 +904,7 @@ void *memchr_inv(const void *start, int c, size_t bytes)
815 904
816 value64 = value; 905 value64 = value;
817#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64 906#if defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
818 value64 *= 0x0101010101010101; 907 value64 *= 0x0101010101010101ULL;
819#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER) 908#elif defined(CONFIG_ARCH_HAS_FAST_MULTIPLIER)
820 value64 *= 0x01010101; 909 value64 *= 0x01010101;
821 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_kasan.c b/lib/test_kasan.c
index c1efb1b61017..c32f3b0048dc 100644
--- a/lib/test_kasan.c
+++ b/lib/test_kasan.c
@@ -138,6 +138,71 @@ static noinline void __init kmalloc_oob_16(void)
138 kfree(ptr2); 138 kfree(ptr2);
139} 139}
140 140
141static noinline void __init kmalloc_oob_memset_2(void)
142{
143 char *ptr;
144 size_t size = 8;
145
146 pr_info("out-of-bounds in memset2\n");
147 ptr = kmalloc(size, GFP_KERNEL);
148 if (!ptr) {
149 pr_err("Allocation failed\n");
150 return;
151 }
152
153 memset(ptr+7, 0, 2);
154 kfree(ptr);
155}
156
157static noinline void __init kmalloc_oob_memset_4(void)
158{
159 char *ptr;
160 size_t size = 8;
161
162 pr_info("out-of-bounds in memset4\n");
163 ptr = kmalloc(size, GFP_KERNEL);
164 if (!ptr) {
165 pr_err("Allocation failed\n");
166 return;
167 }
168
169 memset(ptr+5, 0, 4);
170 kfree(ptr);
171}
172
173
174static noinline void __init kmalloc_oob_memset_8(void)
175{
176 char *ptr;
177 size_t size = 8;
178
179 pr_info("out-of-bounds in memset8\n");
180 ptr = kmalloc(size, GFP_KERNEL);
181 if (!ptr) {
182 pr_err("Allocation failed\n");
183 return;
184 }
185
186 memset(ptr+1, 0, 8);
187 kfree(ptr);
188}
189
190static noinline void __init kmalloc_oob_memset_16(void)
191{
192 char *ptr;
193 size_t size = 16;
194
195 pr_info("out-of-bounds in memset16\n");
196 ptr = kmalloc(size, GFP_KERNEL);
197 if (!ptr) {
198 pr_err("Allocation failed\n");
199 return;
200 }
201
202 memset(ptr+1, 0, 16);
203 kfree(ptr);
204}
205
141static noinline void __init kmalloc_oob_in_memset(void) 206static noinline void __init kmalloc_oob_in_memset(void)
142{ 207{
143 char *ptr; 208 char *ptr;
@@ -264,6 +329,10 @@ static int __init kmalloc_tests_init(void)
264 kmalloc_oob_krealloc_less(); 329 kmalloc_oob_krealloc_less();
265 kmalloc_oob_16(); 330 kmalloc_oob_16();
266 kmalloc_oob_in_memset(); 331 kmalloc_oob_in_memset();
332 kmalloc_oob_memset_2();
333 kmalloc_oob_memset_4();
334 kmalloc_oob_memset_8();
335 kmalloc_oob_memset_16();
267 kmalloc_uaf(); 336 kmalloc_uaf();
268 kmalloc_uaf_memset(); 337 kmalloc_uaf_memset();
269 kmalloc_uaf2(); 338 kmalloc_uaf2();
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';