aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig13
-rw-r--r--lib/Kconfig.debug77
-rw-r--r--lib/Kconfig.ubsan31
-rw-r--r--lib/Makefile8
-rw-r--r--lib/atomic64_test.c2
-rw-r--r--lib/cpumask.c1
-rw-r--r--lib/debugobjects.c2
-rw-r--r--lib/devres.c2
-rw-r--r--lib/div64.c6
-rw-r--r--lib/dma-debug.c2
-rw-r--r--lib/dump_stack.c7
-rw-r--r--lib/genalloc.c93
-rw-r--r--lib/iomap_copy.c21
-rw-r--r--lib/irq_poll.c222
-rw-r--r--lib/kasprintf.c10
-rw-r--r--lib/klist.c6
-rw-r--r--lib/kobject.c1
-rw-r--r--lib/libcrc32c.c2
-rw-r--r--lib/lru_cache.c4
-rw-r--r--lib/mpi/longlong.h2
-rw-r--r--lib/mpi/mpi-inline.h2
-rw-r--r--lib/mpi/mpi-internal.h8
-rw-r--r--lib/mpi/mpicoder.c39
-rw-r--r--lib/radix-tree.c12
-rw-r--r--lib/raid6/altivec.uc1
-rw-r--r--lib/ratelimit.c2
-rw-r--r--lib/scatterlist.c6
-rw-r--r--lib/string_helpers.c63
-rw-r--r--lib/strncpy_from_user.c11
-rw-r--r--lib/strnlen_user.c18
-rw-r--r--lib/test-string_helpers.c67
-rw-r--r--lib/test_firmware.c79
-rw-r--r--lib/test_hexdump.c (renamed from lib/test-hexdump.c)146
-rw-r--r--lib/test_printf.c174
-rw-r--r--lib/test_static_keys.c62
-rw-r--r--lib/ubsan.c456
-rw-r--r--lib/ubsan.h84
-rw-r--r--lib/ucs2_string.c62
-rw-r--r--lib/vsprintf.c353
39 files changed, 1850 insertions, 307 deletions
diff --git a/lib/Kconfig b/lib/Kconfig
index f0df318104e7..133ebc0c1773 100644
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -210,9 +210,11 @@ config RANDOM32_SELFTEST
210# compression support is select'ed if needed 210# compression support is select'ed if needed
211# 211#
212config 842_COMPRESS 212config 842_COMPRESS
213 select CRC32
213 tristate 214 tristate
214 215
215config 842_DECOMPRESS 216config 842_DECOMPRESS
217 select CRC32
216 tristate 218 tristate
217 219
218config ZLIB_INFLATE 220config ZLIB_INFLATE
@@ -475,6 +477,11 @@ config DDR
475 information. This data is useful for drivers handling 477 information. This data is useful for drivers handling
476 DDR SDRAM controllers. 478 DDR SDRAM controllers.
477 479
480config IRQ_POLL
481 bool "IRQ polling library"
482 help
483 Helper library to poll interrupt mitigation using polling.
484
478config MPILIB 485config MPILIB
479 tristate 486 tristate
480 select CLZ_TAB 487 select CLZ_TAB
@@ -512,9 +519,9 @@ source "lib/fonts/Kconfig"
512config SG_SPLIT 519config SG_SPLIT
513 def_bool n 520 def_bool n
514 help 521 help
515 Provides a heler to split scatterlists into chunks, each chunk being a 522 Provides a helper to split scatterlists into chunks, each chunk being
516 scatterlist. This should be selected by a driver or an API which 523 a scatterlist. This should be selected by a driver or an API which
517 whishes to split a scatterlist amongst multiple DMA channel. 524 whishes to split a scatterlist amongst multiple DMA channels.
518 525
519# 526#
520# sg chaining option 527# sg chaining option
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index e2a236a7341f..f28f7fad452f 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -580,6 +580,14 @@ config DEBUG_VM_RB
580 580
581 If unsure, say N. 581 If unsure, say N.
582 582
583config DEBUG_VM_PGFLAGS
584 bool "Debug page-flags operations"
585 depends on DEBUG_VM
586 help
587 Enables extra validation on page flags operations.
588
589 If unsure, say N.
590
583config DEBUG_VIRTUAL 591config DEBUG_VIRTUAL
584 bool "Debug VM translations" 592 bool "Debug VM translations"
585 depends on DEBUG_KERNEL && X86 593 depends on DEBUG_KERNEL && X86
@@ -1392,6 +1400,21 @@ config RCU_EQS_DEBUG
1392 1400
1393endmenu # "RCU Debugging" 1401endmenu # "RCU Debugging"
1394 1402
1403config DEBUG_WQ_FORCE_RR_CPU
1404 bool "Force round-robin CPU selection for unbound work items"
1405 depends on DEBUG_KERNEL
1406 default n
1407 help
1408 Workqueue used to implicitly guarantee that work items queued
1409 without explicit CPU specified are put on the local CPU. This
1410 guarantee is no longer true and while local CPU is still
1411 preferred work items may be put on foreign CPUs. Kernel
1412 parameter "workqueue.debug_force_rr_cpu" is added to force
1413 round-robin CPU selection to flush out usages which depend on the
1414 now broken guarantee. This config option enables the debug
1415 feature by default. When enabled, memory and cache locality will
1416 be impacted.
1417
1395config DEBUG_BLOCK_EXT_DEVT 1418config DEBUG_BLOCK_EXT_DEVT
1396 bool "Force extended block device numbers and spread them" 1419 bool "Force extended block device numbers and spread them"
1397 depends on DEBUG_KERNEL 1420 depends on DEBUG_KERNEL
@@ -1419,6 +1442,19 @@ config DEBUG_BLOCK_EXT_DEVT
1419 1442
1420 Say N if you are unsure. 1443 Say N if you are unsure.
1421 1444
1445config CPU_HOTPLUG_STATE_CONTROL
1446 bool "Enable CPU hotplug state control"
1447 depends on DEBUG_KERNEL
1448 depends on HOTPLUG_CPU
1449 default n
1450 help
1451 Allows to write steps between "offline" and "online" to the CPUs
1452 sysfs target file so states can be stepped granular. This is a debug
1453 option for now as the hotplug machinery cannot be stopped and
1454 restarted at arbitrary points yet.
1455
1456 Say N if your are unsure.
1457
1422config NOTIFIER_ERROR_INJECTION 1458config NOTIFIER_ERROR_INJECTION
1423 tristate "Notifier error injection" 1459 tristate "Notifier error injection"
1424 depends on DEBUG_KERNEL 1460 depends on DEBUG_KERNEL
@@ -1589,7 +1625,6 @@ config FAULT_INJECTION_STACKTRACE_FILTER
1589 1625
1590config LATENCYTOP 1626config LATENCYTOP
1591 bool "Latency measuring infrastructure" 1627 bool "Latency measuring infrastructure"
1592 depends on HAVE_LATENCYTOP_SUPPORT
1593 depends on DEBUG_KERNEL 1628 depends on DEBUG_KERNEL
1594 depends on STACKTRACE_SUPPORT 1629 depends on STACKTRACE_SUPPORT
1595 depends on PROC_FS 1630 depends on PROC_FS
@@ -1886,3 +1921,43 @@ source "samples/Kconfig"
1886 1921
1887source "lib/Kconfig.kgdb" 1922source "lib/Kconfig.kgdb"
1888 1923
1924source "lib/Kconfig.ubsan"
1925
1926config ARCH_HAS_DEVMEM_IS_ALLOWED
1927 bool
1928
1929config STRICT_DEVMEM
1930 bool "Filter access to /dev/mem"
1931 depends on MMU
1932 depends on ARCH_HAS_DEVMEM_IS_ALLOWED
1933 default y if TILE || PPC
1934 ---help---
1935 If this option is disabled, you allow userspace (root) access to all
1936 of memory, including kernel and userspace memory. Accidental
1937 access to this is obviously disastrous, but specific access can
1938 be used by people debugging the kernel. Note that with PAT support
1939 enabled, even in this case there are restrictions on /dev/mem
1940 use due to the cache aliasing requirements.
1941
1942 If this option is switched on, and IO_STRICT_DEVMEM=n, the /dev/mem
1943 file only allows userspace access to PCI space and the BIOS code and
1944 data regions. This is sufficient for dosemu and X and all common
1945 users of /dev/mem.
1946
1947 If in doubt, say Y.
1948
1949config IO_STRICT_DEVMEM
1950 bool "Filter I/O access to /dev/mem"
1951 depends on STRICT_DEVMEM
1952 ---help---
1953 If this option is disabled, you allow userspace (root) access to all
1954 io-memory regardless of whether a driver is actively using that
1955 range. Accidental access to this is obviously disastrous, but
1956 specific access can be used by people debugging kernel drivers.
1957
1958 If this option is switched on, the /dev/mem file only allows
1959 userspace access to *idle* io-memory ranges (see /proc/iomem) This
1960 may break traditional users of /dev/mem (dosemu, legacy X, etc...)
1961 if the driver using a given range cannot be disabled.
1962
1963 If in doubt, say Y.
diff --git a/lib/Kconfig.ubsan b/lib/Kconfig.ubsan
new file mode 100644
index 000000000000..e07c1ba9ba13
--- /dev/null
+++ b/lib/Kconfig.ubsan
@@ -0,0 +1,31 @@
1config ARCH_HAS_UBSAN_SANITIZE_ALL
2 bool
3
4config UBSAN
5 bool "Undefined behaviour sanity checker"
6 help
7 This option enables undefined behaviour sanity checker
8 Compile-time instrumentation is used to detect various undefined
9 behaviours in runtime. Various types of checks may be enabled
10 via boot parameter ubsan_handle (see: Documentation/ubsan.txt).
11
12config UBSAN_SANITIZE_ALL
13 bool "Enable instrumentation for the entire kernel"
14 depends on UBSAN
15 depends on ARCH_HAS_UBSAN_SANITIZE_ALL
16 default y
17 help
18 This option activates instrumentation for the entire kernel.
19 If you don't enable this option, you have to explicitly specify
20 UBSAN_SANITIZE := y for the files/directories you want to check for UB.
21 Enabling this option will get kernel image size increased
22 significantly.
23
24config UBSAN_ALIGNMENT
25 bool "Enable checking of pointers alignment"
26 depends on UBSAN
27 default y if !HAVE_EFFICIENT_UNALIGNED_ACCESS
28 help
29 This option enables detection of unaligned memory accesses.
30 Enabling this option on architectures that support unaligned
31 accesses may produce a lot of false positives.
diff --git a/lib/Makefile b/lib/Makefile
index 180dd4d0dd41..a7c26a41a738 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -31,7 +31,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
31obj-y += string_helpers.o 31obj-y += string_helpers.o
32obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 32obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
33obj-y += hexdump.o 33obj-y += hexdump.o
34obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o 34obj-$(CONFIG_TEST_HEXDUMP) += test_hexdump.o
35obj-y += kstrtox.o 35obj-y += kstrtox.o
36obj-$(CONFIG_TEST_BPF) += test_bpf.o 36obj-$(CONFIG_TEST_BPF) += test_bpf.o
37obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o 37obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
@@ -154,7 +154,7 @@ obj-$(CONFIG_GLOB) += glob.o
154obj-$(CONFIG_MPILIB) += mpi/ 154obj-$(CONFIG_MPILIB) += mpi/
155obj-$(CONFIG_SIGNATURE) += digsig.o 155obj-$(CONFIG_SIGNATURE) += digsig.o
156 156
157obj-$(CONFIG_CLZ_TAB) += clz_tab.o 157lib-$(CONFIG_CLZ_TAB) += clz_tab.o
158 158
159obj-$(CONFIG_DDR) += jedec_ddr_data.o 159obj-$(CONFIG_DDR) += jedec_ddr_data.o
160 160
@@ -165,6 +165,7 @@ obj-$(CONFIG_GENERIC_NET_UTILS) += net_utils.o
165 165
166obj-$(CONFIG_SG_SPLIT) += sg_split.o 166obj-$(CONFIG_SG_SPLIT) += sg_split.o
167obj-$(CONFIG_STMP_DEVICE) += stmp_device.o 167obj-$(CONFIG_STMP_DEVICE) += stmp_device.o
168obj-$(CONFIG_IRQ_POLL) += irq_poll.o
168 169
169libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \ 170libfdt_files = fdt.o fdt_ro.o fdt_wip.o fdt_rw.o fdt_sw.o fdt_strerror.o \
170 fdt_empty_tree.o 171 fdt_empty_tree.o
@@ -209,3 +210,6 @@ quiet_cmd_build_OID_registry = GEN $@
209clean-files += oid_registry_data.c 210clean-files += oid_registry_data.c
210 211
211obj-$(CONFIG_UCS2_STRING) += ucs2_string.o 212obj-$(CONFIG_UCS2_STRING) += ucs2_string.o
213obj-$(CONFIG_UBSAN) += ubsan.o
214
215UBSAN_SANITIZE_ubsan.o := n
diff --git a/lib/atomic64_test.c b/lib/atomic64_test.c
index d62de8bf022d..123481814320 100644
--- a/lib/atomic64_test.c
+++ b/lib/atomic64_test.c
@@ -17,7 +17,7 @@
17#include <linux/atomic.h> 17#include <linux/atomic.h>
18 18
19#ifdef CONFIG_X86 19#ifdef CONFIG_X86
20#include <asm/processor.h> /* for boot_cpu_has below */ 20#include <asm/cpufeature.h> /* for boot_cpu_has below */
21#endif 21#endif
22 22
23#define TEST(bit, op, c_op, val) \ 23#define TEST(bit, op, c_op, val) \
diff --git a/lib/cpumask.c b/lib/cpumask.c
index 5a70f6196f57..81dedaab36cc 100644
--- a/lib/cpumask.c
+++ b/lib/cpumask.c
@@ -41,6 +41,7 @@ int cpumask_any_but(const struct cpumask *mask, unsigned int cpu)
41 break; 41 break;
42 return i; 42 return i;
43} 43}
44EXPORT_SYMBOL(cpumask_any_but);
44 45
45/* These are not inline because of header tangles. */ 46/* These are not inline because of header tangles. */
46#ifdef CONFIG_CPUMASK_OFFSTACK 47#ifdef CONFIG_CPUMASK_OFFSTACK
diff --git a/lib/debugobjects.c b/lib/debugobjects.c
index 547f7f923dbc..519b5a10fd70 100644
--- a/lib/debugobjects.c
+++ b/lib/debugobjects.c
@@ -21,7 +21,7 @@
21#define ODEBUG_HASH_BITS 14 21#define ODEBUG_HASH_BITS 14
22#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) 22#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
23 23
24#define ODEBUG_POOL_SIZE 512 24#define ODEBUG_POOL_SIZE 1024
25#define ODEBUG_POOL_MIN_LEVEL 256 25#define ODEBUG_POOL_MIN_LEVEL 256
26 26
27#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT 27#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
diff --git a/lib/devres.c b/lib/devres.c
index 8c85672639d3..cb1464c411a2 100644
--- a/lib/devres.c
+++ b/lib/devres.c
@@ -236,7 +236,7 @@ struct pcim_iomap_devres {
236 236
237static void pcim_iomap_release(struct device *gendev, void *res) 237static void pcim_iomap_release(struct device *gendev, void *res)
238{ 238{
239 struct pci_dev *dev = container_of(gendev, struct pci_dev, dev); 239 struct pci_dev *dev = to_pci_dev(gendev);
240 struct pcim_iomap_devres *this = res; 240 struct pcim_iomap_devres *this = res;
241 int i; 241 int i;
242 242
diff --git a/lib/div64.c b/lib/div64.c
index 62a698a432bc..7f345259c32f 100644
--- a/lib/div64.c
+++ b/lib/div64.c
@@ -13,7 +13,8 @@
13 * 13 *
14 * Code generated for this function might be very inefficient 14 * Code generated for this function might be very inefficient
15 * for some CPUs. __div64_32() can be overridden by linking arch-specific 15 * for some CPUs. __div64_32() can be overridden by linking arch-specific
16 * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S. 16 * assembly versions such as arch/ppc/lib/div64.S and arch/sh/lib/div64.S
17 * or by defining a preprocessor macro in arch/include/asm/div64.h.
17 */ 18 */
18 19
19#include <linux/export.h> 20#include <linux/export.h>
@@ -23,6 +24,7 @@
23/* Not needed on 64bit architectures */ 24/* Not needed on 64bit architectures */
24#if BITS_PER_LONG == 32 25#if BITS_PER_LONG == 32
25 26
27#ifndef __div64_32
26uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base) 28uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
27{ 29{
28 uint64_t rem = *n; 30 uint64_t rem = *n;
@@ -55,8 +57,8 @@ uint32_t __attribute__((weak)) __div64_32(uint64_t *n, uint32_t base)
55 *n = res; 57 *n = res;
56 return rem; 58 return rem;
57} 59}
58
59EXPORT_SYMBOL(__div64_32); 60EXPORT_SYMBOL(__div64_32);
61#endif
60 62
61#ifndef div_s64_rem 63#ifndef div_s64_rem
62s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder) 64s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
diff --git a/lib/dma-debug.c b/lib/dma-debug.c
index d34bd24c2c84..4a1515f4b452 100644
--- a/lib/dma-debug.c
+++ b/lib/dma-debug.c
@@ -1181,7 +1181,7 @@ static inline bool overlap(void *addr, unsigned long len, void *start, void *end
1181 1181
1182static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len) 1182static void check_for_illegal_area(struct device *dev, void *addr, unsigned long len)
1183{ 1183{
1184 if (overlap(addr, len, _text, _etext) || 1184 if (overlap(addr, len, _stext, _etext) ||
1185 overlap(addr, len, __start_rodata, __end_rodata)) 1185 overlap(addr, len, __start_rodata, __end_rodata))
1186 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len); 1186 err_printk(dev, NULL, "DMA-API: device driver maps memory from kernel text or rodata [addr=%p] [len=%lu]\n", addr, len);
1187} 1187}
diff --git a/lib/dump_stack.c b/lib/dump_stack.c
index 6745c6230db3..c30d07e99dba 100644
--- a/lib/dump_stack.c
+++ b/lib/dump_stack.c
@@ -25,6 +25,7 @@ static atomic_t dump_lock = ATOMIC_INIT(-1);
25 25
26asmlinkage __visible void dump_stack(void) 26asmlinkage __visible void dump_stack(void)
27{ 27{
28 unsigned long flags;
28 int was_locked; 29 int was_locked;
29 int old; 30 int old;
30 int cpu; 31 int cpu;
@@ -33,9 +34,8 @@ asmlinkage __visible void dump_stack(void)
33 * Permit this cpu to perform nested stack dumps while serialising 34 * Permit this cpu to perform nested stack dumps while serialising
34 * against other CPUs 35 * against other CPUs
35 */ 36 */
36 preempt_disable();
37
38retry: 37retry:
38 local_irq_save(flags);
39 cpu = smp_processor_id(); 39 cpu = smp_processor_id();
40 old = atomic_cmpxchg(&dump_lock, -1, cpu); 40 old = atomic_cmpxchg(&dump_lock, -1, cpu);
41 if (old == -1) { 41 if (old == -1) {
@@ -43,6 +43,7 @@ retry:
43 } else if (old == cpu) { 43 } else if (old == cpu) {
44 was_locked = 1; 44 was_locked = 1;
45 } else { 45 } else {
46 local_irq_restore(flags);
46 cpu_relax(); 47 cpu_relax();
47 goto retry; 48 goto retry;
48 } 49 }
@@ -52,7 +53,7 @@ retry:
52 if (!was_locked) 53 if (!was_locked)
53 atomic_set(&dump_lock, -1); 54 atomic_set(&dump_lock, -1);
54 55
55 preempt_enable(); 56 local_irq_restore(flags);
56} 57}
57#else 58#else
58asmlinkage __visible void dump_stack(void) 59asmlinkage __visible void dump_stack(void)
diff --git a/lib/genalloc.c b/lib/genalloc.c
index 116a166b096f..0a1139644d32 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -270,6 +270,25 @@ EXPORT_SYMBOL(gen_pool_destroy);
270 */ 270 */
271unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size) 271unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
272{ 272{
273 return gen_pool_alloc_algo(pool, size, pool->algo, pool->data);
274}
275EXPORT_SYMBOL(gen_pool_alloc);
276
277/**
278 * gen_pool_alloc_algo - allocate special memory from the pool
279 * @pool: pool to allocate from
280 * @size: number of bytes to allocate from the pool
281 * @algo: algorithm passed from caller
282 * @data: data passed to algorithm
283 *
284 * Allocate the requested number of bytes from the specified pool.
285 * Uses the pool allocation function (with first-fit algorithm by default).
286 * Can not be used in NMI handler on architectures without
287 * NMI-safe cmpxchg implementation.
288 */
289unsigned long gen_pool_alloc_algo(struct gen_pool *pool, size_t size,
290 genpool_algo_t algo, void *data)
291{
273 struct gen_pool_chunk *chunk; 292 struct gen_pool_chunk *chunk;
274 unsigned long addr = 0; 293 unsigned long addr = 0;
275 int order = pool->min_alloc_order; 294 int order = pool->min_alloc_order;
@@ -290,8 +309,8 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
290 309
291 end_bit = chunk_size(chunk) >> order; 310 end_bit = chunk_size(chunk) >> order;
292retry: 311retry:
293 start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits, 312 start_bit = algo(chunk->bits, end_bit, start_bit,
294 pool->data); 313 nbits, data, pool);
295 if (start_bit >= end_bit) 314 if (start_bit >= end_bit)
296 continue; 315 continue;
297 remain = bitmap_set_ll(chunk->bits, start_bit, nbits); 316 remain = bitmap_set_ll(chunk->bits, start_bit, nbits);
@@ -310,7 +329,7 @@ retry:
310 rcu_read_unlock(); 329 rcu_read_unlock();
311 return addr; 330 return addr;
312} 331}
313EXPORT_SYMBOL(gen_pool_alloc); 332EXPORT_SYMBOL(gen_pool_alloc_algo);
314 333
315/** 334/**
316 * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage 335 * gen_pool_dma_alloc - allocate special memory from the pool for DMA usage
@@ -501,15 +520,74 @@ EXPORT_SYMBOL(gen_pool_set_algo);
501 * @start: The bitnumber to start searching at 520 * @start: The bitnumber to start searching at
502 * @nr: The number of zeroed bits we're looking for 521 * @nr: The number of zeroed bits we're looking for
503 * @data: additional data - unused 522 * @data: additional data - unused
523 * @pool: pool to find the fit region memory from
504 */ 524 */
505unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, 525unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size,
506 unsigned long start, unsigned int nr, void *data) 526 unsigned long start, unsigned int nr, void *data,
527 struct gen_pool *pool)
507{ 528{
508 return bitmap_find_next_zero_area(map, size, start, nr, 0); 529 return bitmap_find_next_zero_area(map, size, start, nr, 0);
509} 530}
510EXPORT_SYMBOL(gen_pool_first_fit); 531EXPORT_SYMBOL(gen_pool_first_fit);
511 532
512/** 533/**
534 * gen_pool_first_fit_align - find the first available region
535 * of memory matching the size requirement (alignment constraint)
536 * @map: The address to base the search on
537 * @size: The bitmap size in bits
538 * @start: The bitnumber to start searching at
539 * @nr: The number of zeroed bits we're looking for
540 * @data: data for alignment
541 * @pool: pool to get order from
542 */
543unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size,
544 unsigned long start, unsigned int nr, void *data,
545 struct gen_pool *pool)
546{
547 struct genpool_data_align *alignment;
548 unsigned long align_mask;
549 int order;
550
551 alignment = data;
552 order = pool->min_alloc_order;
553 align_mask = ((alignment->align + (1UL << order) - 1) >> order) - 1;
554 return bitmap_find_next_zero_area(map, size, start, nr, align_mask);
555}
556EXPORT_SYMBOL(gen_pool_first_fit_align);
557
558/**
559 * gen_pool_fixed_alloc - reserve a specific region
560 * @map: The address to base the search on
561 * @size: The bitmap size in bits
562 * @start: The bitnumber to start searching at
563 * @nr: The number of zeroed bits we're looking for
564 * @data: data for alignment
565 * @pool: pool to get order from
566 */
567unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size,
568 unsigned long start, unsigned int nr, void *data,
569 struct gen_pool *pool)
570{
571 struct genpool_data_fixed *fixed_data;
572 int order;
573 unsigned long offset_bit;
574 unsigned long start_bit;
575
576 fixed_data = data;
577 order = pool->min_alloc_order;
578 offset_bit = fixed_data->offset >> order;
579 if (WARN_ON(fixed_data->offset & ((1UL << order) - 1)))
580 return size;
581
582 start_bit = bitmap_find_next_zero_area(map, size,
583 start + offset_bit, nr, 0);
584 if (start_bit != offset_bit)
585 start_bit = size;
586 return start_bit;
587}
588EXPORT_SYMBOL(gen_pool_fixed_alloc);
589
590/**
513 * gen_pool_first_fit_order_align - find the first available region 591 * gen_pool_first_fit_order_align - find the first available region
514 * of memory matching the size requirement. The region will be aligned 592 * of memory matching the size requirement. The region will be aligned
515 * to the order of the size specified. 593 * to the order of the size specified.
@@ -518,10 +596,11 @@ EXPORT_SYMBOL(gen_pool_first_fit);
518 * @start: The bitnumber to start searching at 596 * @start: The bitnumber to start searching at
519 * @nr: The number of zeroed bits we're looking for 597 * @nr: The number of zeroed bits we're looking for
520 * @data: additional data - unused 598 * @data: additional data - unused
599 * @pool: pool to find the fit region memory from
521 */ 600 */
522unsigned long gen_pool_first_fit_order_align(unsigned long *map, 601unsigned long gen_pool_first_fit_order_align(unsigned long *map,
523 unsigned long size, unsigned long start, 602 unsigned long size, unsigned long start,
524 unsigned int nr, void *data) 603 unsigned int nr, void *data, struct gen_pool *pool)
525{ 604{
526 unsigned long align_mask = roundup_pow_of_two(nr) - 1; 605 unsigned long align_mask = roundup_pow_of_two(nr) - 1;
527 606
@@ -537,12 +616,14 @@ EXPORT_SYMBOL(gen_pool_first_fit_order_align);
537 * @start: The bitnumber to start searching at 616 * @start: The bitnumber to start searching at
538 * @nr: The number of zeroed bits we're looking for 617 * @nr: The number of zeroed bits we're looking for
539 * @data: additional data - unused 618 * @data: additional data - unused
619 * @pool: pool to find the fit region memory from
540 * 620 *
541 * Iterate over the bitmap to find the smallest free region 621 * Iterate over the bitmap to find the smallest free region
542 * which we can allocate the memory. 622 * which we can allocate the memory.
543 */ 623 */
544unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size, 624unsigned long gen_pool_best_fit(unsigned long *map, unsigned long size,
545 unsigned long start, unsigned int nr, void *data) 625 unsigned long start, unsigned int nr, void *data,
626 struct gen_pool *pool)
546{ 627{
547 unsigned long start_bit = size; 628 unsigned long start_bit = size;
548 unsigned long len = size + 1; 629 unsigned long len = size + 1;
diff --git a/lib/iomap_copy.c b/lib/iomap_copy.c
index 4527e751b5e0..b8f1d6cbb200 100644
--- a/lib/iomap_copy.c
+++ b/lib/iomap_copy.c
@@ -42,6 +42,27 @@ void __attribute__((weak)) __iowrite32_copy(void __iomem *to,
42EXPORT_SYMBOL_GPL(__iowrite32_copy); 42EXPORT_SYMBOL_GPL(__iowrite32_copy);
43 43
44/** 44/**
45 * __ioread32_copy - copy data from MMIO space, in 32-bit units
46 * @to: destination (must be 32-bit aligned)
47 * @from: source, in MMIO space (must be 32-bit aligned)
48 * @count: number of 32-bit quantities to copy
49 *
50 * Copy data from MMIO space to kernel space, in units of 32 bits at a
51 * time. Order of access is not guaranteed, nor is a memory barrier
52 * performed afterwards.
53 */
54void __ioread32_copy(void *to, const void __iomem *from, size_t count)
55{
56 u32 *dst = to;
57 const u32 __iomem *src = from;
58 const u32 __iomem *end = src + count;
59
60 while (src < end)
61 *dst++ = __raw_readl(src++);
62}
63EXPORT_SYMBOL_GPL(__ioread32_copy);
64
65/**
45 * __iowrite64_copy - copy data to MMIO space, in 64-bit or 32-bit units 66 * __iowrite64_copy - copy data to MMIO space, in 64-bit or 32-bit units
46 * @to: destination, in MMIO space (must be 64-bit aligned) 67 * @to: destination, in MMIO space (must be 64-bit aligned)
47 * @from: source (must be 64-bit aligned) 68 * @from: source (must be 64-bit aligned)
diff --git a/lib/irq_poll.c b/lib/irq_poll.c
new file mode 100644
index 000000000000..836f7db4e548
--- /dev/null
+++ b/lib/irq_poll.c
@@ -0,0 +1,222 @@
1/*
2 * Functions related to interrupt-poll handling in the block layer. This
3 * is similar to NAPI for network devices.
4 */
5#include <linux/kernel.h>
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/bio.h>
9#include <linux/interrupt.h>
10#include <linux/cpu.h>
11#include <linux/irq_poll.h>
12#include <linux/delay.h>
13
14static unsigned int irq_poll_budget __read_mostly = 256;
15
16static DEFINE_PER_CPU(struct list_head, blk_cpu_iopoll);
17
18/**
19 * irq_poll_sched - Schedule a run of the iopoll handler
20 * @iop: The parent iopoll structure
21 *
22 * Description:
23 * Add this irq_poll structure to the pending poll list and trigger the
24 * raise of the blk iopoll softirq.
25 **/
26void irq_poll_sched(struct irq_poll *iop)
27{
28 unsigned long flags;
29
30 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
31 return;
32 if (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
33 return;
34
35 local_irq_save(flags);
36 list_add_tail(&iop->list, this_cpu_ptr(&blk_cpu_iopoll));
37 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
38 local_irq_restore(flags);
39}
40EXPORT_SYMBOL(irq_poll_sched);
41
42/**
43 * __irq_poll_complete - Mark this @iop as un-polled again
44 * @iop: The parent iopoll structure
45 *
46 * Description:
47 * See irq_poll_complete(). This function must be called with interrupts
48 * disabled.
49 **/
50static void __irq_poll_complete(struct irq_poll *iop)
51{
52 list_del(&iop->list);
53 smp_mb__before_atomic();
54 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
55}
56
57/**
58 * irq_poll_complete - Mark this @iop as un-polled again
59 * @iop: The parent iopoll structure
60 *
61 * Description:
62 * If a driver consumes less than the assigned budget in its run of the
63 * iopoll handler, it'll end the polled mode by calling this function. The
64 * iopoll handler will not be invoked again before irq_poll_sched()
65 * is called.
66 **/
67void irq_poll_complete(struct irq_poll *iop)
68{
69 unsigned long flags;
70
71 local_irq_save(flags);
72 __irq_poll_complete(iop);
73 local_irq_restore(flags);
74}
75EXPORT_SYMBOL(irq_poll_complete);
76
77static void irq_poll_softirq(struct softirq_action *h)
78{
79 struct list_head *list = this_cpu_ptr(&blk_cpu_iopoll);
80 int rearm = 0, budget = irq_poll_budget;
81 unsigned long start_time = jiffies;
82
83 local_irq_disable();
84
85 while (!list_empty(list)) {
86 struct irq_poll *iop;
87 int work, weight;
88
89 /*
90 * If softirq window is exhausted then punt.
91 */
92 if (budget <= 0 || time_after(jiffies, start_time)) {
93 rearm = 1;
94 break;
95 }
96
97 local_irq_enable();
98
99 /* Even though interrupts have been re-enabled, this
100 * access is safe because interrupts can only add new
101 * entries to the tail of this list, and only ->poll()
102 * calls can remove this head entry from the list.
103 */
104 iop = list_entry(list->next, struct irq_poll, list);
105
106 weight = iop->weight;
107 work = 0;
108 if (test_bit(IRQ_POLL_F_SCHED, &iop->state))
109 work = iop->poll(iop, weight);
110
111 budget -= work;
112
113 local_irq_disable();
114
115 /*
116 * Drivers must not modify the iopoll state, if they
117 * consume their assigned weight (or more, some drivers can't
118 * easily just stop processing, they have to complete an
119 * entire mask of commands).In such cases this code
120 * still "owns" the iopoll instance and therefore can
121 * move the instance around on the list at-will.
122 */
123 if (work >= weight) {
124 if (test_bit(IRQ_POLL_F_DISABLE, &iop->state))
125 __irq_poll_complete(iop);
126 else
127 list_move_tail(&iop->list, list);
128 }
129 }
130
131 if (rearm)
132 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
133
134 local_irq_enable();
135}
136
137/**
138 * irq_poll_disable - Disable iopoll on this @iop
139 * @iop: The parent iopoll structure
140 *
141 * Description:
142 * Disable io polling and wait for any pending callbacks to have completed.
143 **/
144void irq_poll_disable(struct irq_poll *iop)
145{
146 set_bit(IRQ_POLL_F_DISABLE, &iop->state);
147 while (test_and_set_bit(IRQ_POLL_F_SCHED, &iop->state))
148 msleep(1);
149 clear_bit(IRQ_POLL_F_DISABLE, &iop->state);
150}
151EXPORT_SYMBOL(irq_poll_disable);
152
153/**
154 * irq_poll_enable - Enable iopoll on this @iop
155 * @iop: The parent iopoll structure
156 *
157 * Description:
158 * Enable iopoll on this @iop. Note that the handler run will not be
159 * scheduled, it will only mark it as active.
160 **/
161void irq_poll_enable(struct irq_poll *iop)
162{
163 BUG_ON(!test_bit(IRQ_POLL_F_SCHED, &iop->state));
164 smp_mb__before_atomic();
165 clear_bit_unlock(IRQ_POLL_F_SCHED, &iop->state);
166}
167EXPORT_SYMBOL(irq_poll_enable);
168
169/**
170 * irq_poll_init - Initialize this @iop
171 * @iop: The parent iopoll structure
172 * @weight: The default weight (or command completion budget)
173 * @poll_fn: The handler to invoke
174 *
175 * Description:
176 * Initialize and enable this irq_poll structure.
177 **/
178void irq_poll_init(struct irq_poll *iop, int weight, irq_poll_fn *poll_fn)
179{
180 memset(iop, 0, sizeof(*iop));
181 INIT_LIST_HEAD(&iop->list);
182 iop->weight = weight;
183 iop->poll = poll_fn;
184}
185EXPORT_SYMBOL(irq_poll_init);
186
187static int irq_poll_cpu_notify(struct notifier_block *self,
188 unsigned long action, void *hcpu)
189{
190 /*
191 * If a CPU goes away, splice its entries to the current CPU
192 * and trigger a run of the softirq
193 */
194 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
195 int cpu = (unsigned long) hcpu;
196
197 local_irq_disable();
198 list_splice_init(&per_cpu(blk_cpu_iopoll, cpu),
199 this_cpu_ptr(&blk_cpu_iopoll));
200 __raise_softirq_irqoff(IRQ_POLL_SOFTIRQ);
201 local_irq_enable();
202 }
203
204 return NOTIFY_OK;
205}
206
207static struct notifier_block irq_poll_cpu_notifier = {
208 .notifier_call = irq_poll_cpu_notify,
209};
210
211static __init int irq_poll_setup(void)
212{
213 int i;
214
215 for_each_possible_cpu(i)
216 INIT_LIST_HEAD(&per_cpu(blk_cpu_iopoll, i));
217
218 open_softirq(IRQ_POLL_SOFTIRQ, irq_poll_softirq);
219 register_hotcpu_notifier(&irq_poll_cpu_notifier);
220 return 0;
221}
222subsys_initcall(irq_poll_setup);
diff --git a/lib/kasprintf.c b/lib/kasprintf.c
index f194e6e593e1..7f6c506a4942 100644
--- a/lib/kasprintf.c
+++ b/lib/kasprintf.c
@@ -13,19 +13,21 @@
13/* Simplified asprintf. */ 13/* Simplified asprintf. */
14char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap) 14char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
15{ 15{
16 unsigned int len; 16 unsigned int first, second;
17 char *p; 17 char *p;
18 va_list aq; 18 va_list aq;
19 19
20 va_copy(aq, ap); 20 va_copy(aq, ap);
21 len = vsnprintf(NULL, 0, fmt, aq); 21 first = vsnprintf(NULL, 0, fmt, aq);
22 va_end(aq); 22 va_end(aq);
23 23
24 p = kmalloc_track_caller(len+1, gfp); 24 p = kmalloc_track_caller(first+1, gfp);
25 if (!p) 25 if (!p)
26 return NULL; 26 return NULL;
27 27
28 vsnprintf(p, len+1, fmt, ap); 28 second = vsnprintf(p, first+1, fmt, ap);
29 WARN(first != second, "different return values (%u and %u) from vsnprintf(\"%s\", ...)",
30 first, second, fmt);
29 31
30 return p; 32 return p;
31} 33}
diff --git a/lib/klist.c b/lib/klist.c
index d74cf7a29afd..0507fa5d84c5 100644
--- a/lib/klist.c
+++ b/lib/klist.c
@@ -282,9 +282,9 @@ void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282 struct klist_node *n) 282 struct klist_node *n)
283{ 283{
284 i->i_klist = k; 284 i->i_klist = k;
285 i->i_cur = n; 285 i->i_cur = NULL;
286 if (n) 286 if (n && kref_get_unless_zero(&n->n_ref))
287 kref_get(&n->n_ref); 287 i->i_cur = n;
288} 288}
289EXPORT_SYMBOL_GPL(klist_iter_init_node); 289EXPORT_SYMBOL_GPL(klist_iter_init_node);
290 290
diff --git a/lib/kobject.c b/lib/kobject.c
index 7cbccd2b4c72..445dcaeb0f56 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -861,6 +861,7 @@ struct kobject *kset_find_obj(struct kset *kset, const char *name)
861 spin_unlock(&kset->list_lock); 861 spin_unlock(&kset->list_lock);
862 return ret; 862 return ret;
863} 863}
864EXPORT_SYMBOL_GPL(kset_find_obj);
864 865
865static void kset_release(struct kobject *kobj) 866static void kset_release(struct kobject *kobj)
866{ 867{
diff --git a/lib/libcrc32c.c b/lib/libcrc32c.c
index 6a08ce7d6adc..74a54b7f2562 100644
--- a/lib/libcrc32c.c
+++ b/lib/libcrc32c.c
@@ -36,6 +36,7 @@
36#include <linux/init.h> 36#include <linux/init.h>
37#include <linux/kernel.h> 37#include <linux/kernel.h>
38#include <linux/module.h> 38#include <linux/module.h>
39#include <linux/crc32c.h>
39 40
40static struct crypto_shash *tfm; 41static struct crypto_shash *tfm;
41 42
@@ -74,3 +75,4 @@ module_exit(libcrc32c_mod_fini);
74MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); 75MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
75MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations"); 76MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
76MODULE_LICENSE("GPL"); 77MODULE_LICENSE("GPL");
78MODULE_SOFTDEP("pre: crc32c");
diff --git a/lib/lru_cache.c b/lib/lru_cache.c
index 028f5d996eef..28ba40b99337 100644
--- a/lib/lru_cache.c
+++ b/lib/lru_cache.c
@@ -238,7 +238,7 @@ void lc_reset(struct lru_cache *lc)
238 * @seq: the seq_file to print into 238 * @seq: the seq_file to print into
239 * @lc: the lru cache to print statistics of 239 * @lc: the lru cache to print statistics of
240 */ 240 */
241size_t lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc) 241void lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
242{ 242{
243 /* NOTE: 243 /* NOTE:
244 * total calls to lc_get are 244 * total calls to lc_get are
@@ -250,8 +250,6 @@ size_t lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
250 seq_printf(seq, "\t%s: used:%u/%u hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n", 250 seq_printf(seq, "\t%s: used:%u/%u hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n",
251 lc->name, lc->used, lc->nr_elements, 251 lc->name, lc->used, lc->nr_elements,
252 lc->hits, lc->misses, lc->starving, lc->locked, lc->changed); 252 lc->hits, lc->misses, lc->starving, lc->locked, lc->changed);
253
254 return 0;
255} 253}
256 254
257static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr) 255static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
diff --git a/lib/mpi/longlong.h b/lib/mpi/longlong.h
index b90e255c2a68..93336502af08 100644
--- a/lib/mpi/longlong.h
+++ b/lib/mpi/longlong.h
@@ -216,7 +216,7 @@ extern UDItype __udiv_qrnnd(UDItype *, UDItype, UDItype, UDItype);
216 __asm__ ("%@ Inlined umul_ppmm\n" \ 216 __asm__ ("%@ Inlined umul_ppmm\n" \
217 "umull %r1, %r0, %r2, %r3" \ 217 "umull %r1, %r0, %r2, %r3" \
218 : "=&r" ((USItype)(xh)), \ 218 : "=&r" ((USItype)(xh)), \
219 "=r" ((USItype)(xl)) \ 219 "=&r" ((USItype)(xl)) \
220 : "r" ((USItype)(a)), \ 220 : "r" ((USItype)(a)), \
221 "r" ((USItype)(b)) \ 221 "r" ((USItype)(b)) \
222 : "r0", "r1") 222 : "r0", "r1")
diff --git a/lib/mpi/mpi-inline.h b/lib/mpi/mpi-inline.h
index e2b39852b30a..c245ea31f785 100644
--- a/lib/mpi/mpi-inline.h
+++ b/lib/mpi/mpi-inline.h
@@ -30,7 +30,7 @@
30#define G10_MPI_INLINE_H 30#define G10_MPI_INLINE_H
31 31
32#ifndef G10_MPI_INLINE_DECL 32#ifndef G10_MPI_INLINE_DECL
33#define G10_MPI_INLINE_DECL extern inline 33#define G10_MPI_INLINE_DECL static inline
34#endif 34#endif
35 35
36G10_MPI_INLINE_DECL mpi_limb_t 36G10_MPI_INLINE_DECL mpi_limb_t
diff --git a/lib/mpi/mpi-internal.h b/lib/mpi/mpi-internal.h
index c65dd1bff45a..7eceeddb3fb8 100644
--- a/lib/mpi/mpi-internal.h
+++ b/lib/mpi/mpi-internal.h
@@ -168,19 +168,19 @@ void mpi_rshift_limbs(MPI a, unsigned int count);
168int mpi_lshift_limbs(MPI a, unsigned int count); 168int mpi_lshift_limbs(MPI a, unsigned int count);
169 169
170/*-- mpihelp-add.c --*/ 170/*-- mpihelp-add.c --*/
171mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 171static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
172 mpi_size_t s1_size, mpi_limb_t s2_limb); 172 mpi_size_t s1_size, mpi_limb_t s2_limb);
173mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 173mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
174 mpi_ptr_t s2_ptr, mpi_size_t size); 174 mpi_ptr_t s2_ptr, mpi_size_t size);
175mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 175static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
176 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 176 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
177 177
178/*-- mpihelp-sub.c --*/ 178/*-- mpihelp-sub.c --*/
179mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 179static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
180 mpi_size_t s1_size, mpi_limb_t s2_limb); 180 mpi_size_t s1_size, mpi_limb_t s2_limb);
181mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 181mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
182 mpi_ptr_t s2_ptr, mpi_size_t size); 182 mpi_ptr_t s2_ptr, mpi_size_t size);
183mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 183static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
184 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 184 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
185 185
186/*-- mpihelp-cmp.c --*/ 186/*-- mpihelp-cmp.c --*/
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index ec533a6c77b5..eb15e7dc7b65 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -128,6 +128,23 @@ leave:
128} 128}
129EXPORT_SYMBOL_GPL(mpi_read_from_buffer); 129EXPORT_SYMBOL_GPL(mpi_read_from_buffer);
130 130
131static int count_lzeros(MPI a)
132{
133 mpi_limb_t alimb;
134 int i, lzeros = 0;
135
136 for (i = a->nlimbs - 1; i >= 0; i--) {
137 alimb = a->d[i];
138 if (alimb == 0) {
139 lzeros += sizeof(mpi_limb_t);
140 } else {
141 lzeros += count_leading_zeros(alimb) / 8;
142 break;
143 }
144 }
145 return lzeros;
146}
147
131/** 148/**
132 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first) 149 * mpi_read_buffer() - read MPI to a bufer provided by user (msb first)
133 * 150 *
@@ -148,7 +165,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
148 uint8_t *p; 165 uint8_t *p;
149 mpi_limb_t alimb; 166 mpi_limb_t alimb;
150 unsigned int n = mpi_get_size(a); 167 unsigned int n = mpi_get_size(a);
151 int i, lzeros = 0; 168 int i, lzeros;
152 169
153 if (!buf || !nbytes) 170 if (!buf || !nbytes)
154 return -EINVAL; 171 return -EINVAL;
@@ -156,14 +173,7 @@ int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
156 if (sign) 173 if (sign)
157 *sign = a->sign; 174 *sign = a->sign;
158 175
159 p = (void *)&a->d[a->nlimbs] - 1; 176 lzeros = count_lzeros(a);
160
161 for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
162 if (!*p)
163 lzeros++;
164 else
165 break;
166 }
167 177
168 if (buf_len < n - lzeros) { 178 if (buf_len < n - lzeros) {
169 *nbytes = n - lzeros; 179 *nbytes = n - lzeros;
@@ -351,7 +361,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
351 u8 *p, *p2; 361 u8 *p, *p2;
352 mpi_limb_t alimb, alimb2; 362 mpi_limb_t alimb, alimb2;
353 unsigned int n = mpi_get_size(a); 363 unsigned int n = mpi_get_size(a);
354 int i, x, y = 0, lzeros = 0, buf_len; 364 int i, x, y = 0, lzeros, buf_len;
355 365
356 if (!nbytes) 366 if (!nbytes)
357 return -EINVAL; 367 return -EINVAL;
@@ -359,14 +369,7 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
359 if (sign) 369 if (sign)
360 *sign = a->sign; 370 *sign = a->sign;
361 371
362 p = (void *)&a->d[a->nlimbs] - 1; 372 lzeros = count_lzeros(a);
363
364 for (i = a->nlimbs * sizeof(alimb) - 1; i >= 0; i--, p--) {
365 if (!*p)
366 lzeros++;
367 else
368 break;
369 }
370 373
371 if (*nbytes < n - lzeros) { 374 if (*nbytes < n - lzeros) {
372 *nbytes = n - lzeros; 375 *nbytes = n - lzeros;
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index fcf5d98574ce..6b79e9026e24 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -1019,9 +1019,13 @@ radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1019 return 0; 1019 return 0;
1020 1020
1021 radix_tree_for_each_slot(slot, root, &iter, first_index) { 1021 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1022 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot)); 1022 results[ret] = rcu_dereference_raw(*slot);
1023 if (!results[ret]) 1023 if (!results[ret])
1024 continue; 1024 continue;
1025 if (radix_tree_is_indirect_ptr(results[ret])) {
1026 slot = radix_tree_iter_retry(&iter);
1027 continue;
1028 }
1025 if (++ret == max_items) 1029 if (++ret == max_items)
1026 break; 1030 break;
1027 } 1031 }
@@ -1098,9 +1102,13 @@ radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
1098 return 0; 1102 return 0;
1099 1103
1100 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { 1104 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1101 results[ret] = indirect_to_ptr(rcu_dereference_raw(*slot)); 1105 results[ret] = rcu_dereference_raw(*slot);
1102 if (!results[ret]) 1106 if (!results[ret])
1103 continue; 1107 continue;
1108 if (radix_tree_is_indirect_ptr(results[ret])) {
1109 slot = radix_tree_iter_retry(&iter);
1110 continue;
1111 }
1104 if (++ret == max_items) 1112 if (++ret == max_items)
1105 break; 1113 break;
1106 } 1114 }
diff --git a/lib/raid6/altivec.uc b/lib/raid6/altivec.uc
index bec27fce7501..682aae8a1fef 100644
--- a/lib/raid6/altivec.uc
+++ b/lib/raid6/altivec.uc
@@ -101,6 +101,7 @@ static void raid6_altivec$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
101 101
102 raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs); 102 raid6_altivec$#_gen_syndrome_real(disks, bytes, ptrs);
103 103
104 disable_kernel_altivec();
104 preempt_enable(); 105 preempt_enable();
105} 106}
106 107
diff --git a/lib/ratelimit.c b/lib/ratelimit.c
index 40e03ea2a967..2c5de86460c5 100644
--- a/lib/ratelimit.c
+++ b/lib/ratelimit.c
@@ -49,7 +49,7 @@ int ___ratelimit(struct ratelimit_state *rs, const char *func)
49 if (rs->missed) 49 if (rs->missed)
50 printk(KERN_WARNING "%s: %d callbacks suppressed\n", 50 printk(KERN_WARNING "%s: %d callbacks suppressed\n",
51 func, rs->missed); 51 func, rs->missed);
52 rs->begin = 0; 52 rs->begin = jiffies;
53 rs->printed = 0; 53 rs->printed = 0;
54 rs->missed = 0; 54 rs->missed = 0;
55 } 55 }
diff --git a/lib/scatterlist.c b/lib/scatterlist.c
index bafa9933fa76..004fc70fc56a 100644
--- a/lib/scatterlist.c
+++ b/lib/scatterlist.c
@@ -598,9 +598,9 @@ EXPORT_SYMBOL(sg_miter_next);
598 * 598 *
599 * Description: 599 * Description:
600 * Stops mapping iterator @miter. @miter should have been started 600 * Stops mapping iterator @miter. @miter should have been started
601 * started using sg_miter_start(). A stopped iteration can be 601 * using sg_miter_start(). A stopped iteration can be resumed by
602 * resumed by calling sg_miter_next() on it. This is useful when 602 * calling sg_miter_next() on it. This is useful when resources (kmap)
603 * resources (kmap) need to be released during iteration. 603 * need to be released during iteration.
604 * 604 *
605 * Context: 605 * Context:
606 * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care 606 * Preemption disabled if the SG_MITER_ATOMIC is set. Don't care
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 5939f63d90cd..5c88204b6f1f 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -43,50 +43,73 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
43 [STRING_UNITS_10] = 1000, 43 [STRING_UNITS_10] = 1000,
44 [STRING_UNITS_2] = 1024, 44 [STRING_UNITS_2] = 1024,
45 }; 45 };
46 int i, j; 46 static const unsigned int rounding[] = { 500, 50, 5 };
47 u32 remainder = 0, sf_cap, exp; 47 int i = 0, j;
48 u32 remainder = 0, sf_cap;
48 char tmp[8]; 49 char tmp[8];
49 const char *unit; 50 const char *unit;
50 51
51 tmp[0] = '\0'; 52 tmp[0] = '\0';
52 i = 0; 53
53 if (!size) 54 if (blk_size == 0)
55 size = 0;
56 if (size == 0)
54 goto out; 57 goto out;
55 58
56 while (blk_size >= divisor[units]) { 59 /* This is Napier's algorithm. Reduce the original block size to
57 remainder = do_div(blk_size, divisor[units]); 60 *
61 * coefficient * divisor[units]^i
62 *
63 * we do the reduction so both coefficients are just under 32 bits so
64 * that multiplying them together won't overflow 64 bits and we keep
65 * as much precision as possible in the numbers.
66 *
67 * Note: it's safe to throw away the remainders here because all the
68 * precision is in the coefficients.
69 */
70 while (blk_size >> 32) {
71 do_div(blk_size, divisor[units]);
58 i++; 72 i++;
59 } 73 }
60 74
61 exp = divisor[units] / (u32)blk_size; 75 while (size >> 32) {
62 /* 76 do_div(size, divisor[units]);
63 * size must be strictly greater than exp here to ensure that remainder
64 * is greater than divisor[units] coming out of the if below.
65 */
66 if (size > exp) {
67 remainder = do_div(size, divisor[units]);
68 remainder *= blk_size;
69 i++; 77 i++;
70 } else {
71 remainder *= size;
72 } 78 }
73 79
80 /* now perform the actual multiplication keeping i as the sum of the
81 * two logarithms */
74 size *= blk_size; 82 size *= blk_size;
75 size += remainder / divisor[units];
76 remainder %= divisor[units];
77 83
84 /* and logarithmically reduce it until it's just under the divisor */
78 while (size >= divisor[units]) { 85 while (size >= divisor[units]) {
79 remainder = do_div(size, divisor[units]); 86 remainder = do_div(size, divisor[units]);
80 i++; 87 i++;
81 } 88 }
82 89
90 /* work out in j how many digits of precision we need from the
91 * remainder */
83 sf_cap = size; 92 sf_cap = size;
84 for (j = 0; sf_cap*10 < 1000; j++) 93 for (j = 0; sf_cap*10 < 1000; j++)
85 sf_cap *= 10; 94 sf_cap *= 10;
86 95
87 if (j) { 96 if (units == STRING_UNITS_2) {
97 /* express the remainder as a decimal. It's currently the
98 * numerator of a fraction whose denominator is
99 * divisor[units], which is 1 << 10 for STRING_UNITS_2 */
88 remainder *= 1000; 100 remainder *= 1000;
89 remainder /= divisor[units]; 101 remainder >>= 10;
102 }
103
104 /* add a 5 to the digit below what will be printed to ensure
105 * an arithmetical round up and carry it through to size */
106 remainder += rounding[j];
107 if (remainder >= 1000) {
108 remainder -= 1000;
109 size += 1;
110 }
111
112 if (j) {
90 snprintf(tmp, sizeof(tmp), ".%03u", remainder); 113 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
91 tmp[j+1] = '\0'; 114 tmp[j+1] = '\0';
92 } 115 }
diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
index e0af6ff73d14..33840324138c 100644
--- a/lib/strncpy_from_user.c
+++ b/lib/strncpy_from_user.c
@@ -39,7 +39,7 @@ static inline long do_strncpy_from_user(char *dst, const char __user *src, long
39 unsigned long c, data; 39 unsigned long c, data;
40 40
41 /* Fall back to byte-at-a-time if we get a page fault */ 41 /* Fall back to byte-at-a-time if we get a page fault */
42 if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) 42 if (unlikely(unsafe_get_user(c,(unsigned long __user *)(src+res))))
43 break; 43 break;
44 *(unsigned long *)(dst+res) = c; 44 *(unsigned long *)(dst+res) = c;
45 if (has_zero(c, &data, &constants)) { 45 if (has_zero(c, &data, &constants)) {
@@ -55,7 +55,7 @@ byte_at_a_time:
55 while (max) { 55 while (max) {
56 char c; 56 char c;
57 57
58 if (unlikely(__get_user(c,src+res))) 58 if (unlikely(unsafe_get_user(c,src+res)))
59 return -EFAULT; 59 return -EFAULT;
60 dst[res] = c; 60 dst[res] = c;
61 if (!c) 61 if (!c)
@@ -107,7 +107,12 @@ long strncpy_from_user(char *dst, const char __user *src, long count)
107 src_addr = (unsigned long)src; 107 src_addr = (unsigned long)src;
108 if (likely(src_addr < max_addr)) { 108 if (likely(src_addr < max_addr)) {
109 unsigned long max = max_addr - src_addr; 109 unsigned long max = max_addr - src_addr;
110 return do_strncpy_from_user(dst, src, count, max); 110 long retval;
111
112 user_access_begin();
113 retval = do_strncpy_from_user(dst, src, count, max);
114 user_access_end();
115 return retval;
111 } 116 }
112 return -EFAULT; 117 return -EFAULT;
113} 118}
diff --git a/lib/strnlen_user.c b/lib/strnlen_user.c
index 3a5f2b366d84..2625943625d7 100644
--- a/lib/strnlen_user.c
+++ b/lib/strnlen_user.c
@@ -45,7 +45,7 @@ static inline long do_strnlen_user(const char __user *src, unsigned long count,
45 src -= align; 45 src -= align;
46 max += align; 46 max += align;
47 47
48 if (unlikely(__get_user(c,(unsigned long __user *)src))) 48 if (unlikely(unsafe_get_user(c,(unsigned long __user *)src)))
49 return 0; 49 return 0;
50 c |= aligned_byte_mask(align); 50 c |= aligned_byte_mask(align);
51 51
@@ -61,7 +61,7 @@ static inline long do_strnlen_user(const char __user *src, unsigned long count,
61 if (unlikely(max <= sizeof(unsigned long))) 61 if (unlikely(max <= sizeof(unsigned long)))
62 break; 62 break;
63 max -= sizeof(unsigned long); 63 max -= sizeof(unsigned long);
64 if (unlikely(__get_user(c,(unsigned long __user *)(src+res)))) 64 if (unlikely(unsafe_get_user(c,(unsigned long __user *)(src+res))))
65 return 0; 65 return 0;
66 } 66 }
67 res -= align; 67 res -= align;
@@ -112,7 +112,12 @@ long strnlen_user(const char __user *str, long count)
112 src_addr = (unsigned long)str; 112 src_addr = (unsigned long)str;
113 if (likely(src_addr < max_addr)) { 113 if (likely(src_addr < max_addr)) {
114 unsigned long max = max_addr - src_addr; 114 unsigned long max = max_addr - src_addr;
115 return do_strnlen_user(str, count, max); 115 long retval;
116
117 user_access_begin();
118 retval = do_strnlen_user(str, count, max);
119 user_access_end();
120 return retval;
116 } 121 }
117 return 0; 122 return 0;
118} 123}
@@ -141,7 +146,12 @@ long strlen_user(const char __user *str)
141 src_addr = (unsigned long)str; 146 src_addr = (unsigned long)str;
142 if (likely(src_addr < max_addr)) { 147 if (likely(src_addr < max_addr)) {
143 unsigned long max = max_addr - src_addr; 148 unsigned long max = max_addr - src_addr;
144 return do_strnlen_user(str, ~0ul, max); 149 long retval;
150
151 user_access_begin();
152 retval = do_strnlen_user(str, ~0ul, max);
153 user_access_end();
154 return retval;
145 } 155 }
146 return 0; 156 return 0;
147} 157}
diff --git a/lib/test-string_helpers.c b/lib/test-string_helpers.c
index 98866a770770..25b5cbfb7615 100644
--- a/lib/test-string_helpers.c
+++ b/lib/test-string_helpers.c
@@ -327,36 +327,67 @@ out:
327} 327}
328 328
329#define string_get_size_maxbuf 16 329#define string_get_size_maxbuf 16
330#define test_string_get_size_one(size, blk_size, units, exp_result) \ 330#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2) \
331 do { \ 331 do { \
332 BUILD_BUG_ON(sizeof(exp_result) >= string_get_size_maxbuf); \ 332 BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf); \
333 __test_string_get_size((size), (blk_size), (units), \ 333 BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf); \
334 (exp_result)); \ 334 __test_string_get_size((size), (blk_size), (exp_result10), \
335 (exp_result2)); \
335 } while (0) 336 } while (0)
336 337
337 338
338static __init void __test_string_get_size(const u64 size, const u64 blk_size, 339static __init void test_string_get_size_check(const char *units,
339 const enum string_size_units units, 340 const char *exp,
340 const char *exp_result) 341 char *res,
342 const u64 size,
343 const u64 blk_size)
341{ 344{
342 char buf[string_get_size_maxbuf]; 345 if (!memcmp(res, exp, strlen(exp) + 1))
343
344 string_get_size(size, blk_size, units, buf, sizeof(buf));
345 if (!memcmp(buf, exp_result, strlen(exp_result) + 1))
346 return; 346 return;
347 347
348 buf[sizeof(buf) - 1] = '\0'; 348 res[string_get_size_maxbuf - 1] = '\0';
349 pr_warn("Test 'test_string_get_size_one' failed!\n"); 349
350 pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %d\n", 350 pr_warn("Test 'test_string_get_size' failed!\n");
351 pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
351 size, blk_size, units); 352 size, blk_size, units);
352 pr_warn("expected: '%s', got '%s'\n", exp_result, buf); 353 pr_warn("expected: '%s', got '%s'\n", exp, res);
354}
355
356static __init void __test_string_get_size(const u64 size, const u64 blk_size,
357 const char *exp_result10,
358 const char *exp_result2)
359{
360 char buf10[string_get_size_maxbuf];
361 char buf2[string_get_size_maxbuf];
362
363 string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
364 string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
365
366 test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
367 size, blk_size);
368
369 test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
370 size, blk_size);
353} 371}
354 372
355static __init void test_string_get_size(void) 373static __init void test_string_get_size(void)
356{ 374{
357 test_string_get_size_one(16384, 512, STRING_UNITS_2, "8.00 MiB"); 375 /* small values */
358 test_string_get_size_one(8192, 4096, STRING_UNITS_10, "32.7 MB"); 376 test_string_get_size_one(0, 512, "0 B", "0 B");
359 test_string_get_size_one(1, 512, STRING_UNITS_10, "512 B"); 377 test_string_get_size_one(1, 512, "512 B", "512 B");
378 test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
379
380 /* normal values */
381 test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
382 test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
383 test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
384
385 /* weird block sizes */
386 test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
387
388 /* huge values */
389 test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
390 test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
360} 391}
361 392
362static int __init test_string_helpers_init(void) 393static int __init test_string_helpers_init(void)
diff --git a/lib/test_firmware.c b/lib/test_firmware.c
index 86374c1c49a4..a3e8ec3fb1c5 100644
--- a/lib/test_firmware.c
+++ b/lib/test_firmware.c
@@ -12,6 +12,7 @@
12#include <linux/init.h> 12#include <linux/init.h>
13#include <linux/module.h> 13#include <linux/module.h>
14#include <linux/printk.h> 14#include <linux/printk.h>
15#include <linux/completion.h>
15#include <linux/firmware.h> 16#include <linux/firmware.h>
16#include <linux/device.h> 17#include <linux/device.h>
17#include <linux/fs.h> 18#include <linux/fs.h>
@@ -54,10 +55,9 @@ static ssize_t trigger_request_store(struct device *dev,
54 int rc; 55 int rc;
55 char *name; 56 char *name;
56 57
57 name = kzalloc(count + 1, GFP_KERNEL); 58 name = kstrndup(buf, count, GFP_KERNEL);
58 if (!name) 59 if (!name)
59 return -ENOSPC; 60 return -ENOSPC;
60 memcpy(name, buf, count);
61 61
62 pr_info("loading '%s'\n", name); 62 pr_info("loading '%s'\n", name);
63 63
@@ -65,17 +65,73 @@ static ssize_t trigger_request_store(struct device *dev,
65 release_firmware(test_firmware); 65 release_firmware(test_firmware);
66 test_firmware = NULL; 66 test_firmware = NULL;
67 rc = request_firmware(&test_firmware, name, dev); 67 rc = request_firmware(&test_firmware, name, dev);
68 if (rc) 68 if (rc) {
69 pr_info("load of '%s' failed: %d\n", name, rc); 69 pr_info("load of '%s' failed: %d\n", name, rc);
70 pr_info("loaded: %zu\n", test_firmware ? test_firmware->size : 0); 70 goto out;
71 }
72 pr_info("loaded: %zu\n", test_firmware->size);
73 rc = count;
74
75out:
71 mutex_unlock(&test_fw_mutex); 76 mutex_unlock(&test_fw_mutex);
72 77
73 kfree(name); 78 kfree(name);
74 79
75 return count; 80 return rc;
76} 81}
77static DEVICE_ATTR_WO(trigger_request); 82static DEVICE_ATTR_WO(trigger_request);
78 83
84static DECLARE_COMPLETION(async_fw_done);
85
86static void trigger_async_request_cb(const struct firmware *fw, void *context)
87{
88 test_firmware = fw;
89 complete(&async_fw_done);
90}
91
92static ssize_t trigger_async_request_store(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95{
96 int rc;
97 char *name;
98
99 name = kstrndup(buf, count, GFP_KERNEL);
100 if (!name)
101 return -ENOSPC;
102
103 pr_info("loading '%s'\n", name);
104
105 mutex_lock(&test_fw_mutex);
106 release_firmware(test_firmware);
107 test_firmware = NULL;
108 rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
109 NULL, trigger_async_request_cb);
110 if (rc) {
111 pr_info("async load of '%s' failed: %d\n", name, rc);
112 kfree(name);
113 goto out;
114 }
115 /* Free 'name' ASAP, to test for race conditions */
116 kfree(name);
117
118 wait_for_completion(&async_fw_done);
119
120 if (test_firmware) {
121 pr_info("loaded: %zu\n", test_firmware->size);
122 rc = count;
123 } else {
124 pr_err("failed to async load firmware\n");
125 rc = -ENODEV;
126 }
127
128out:
129 mutex_unlock(&test_fw_mutex);
130
131 return rc;
132}
133static DEVICE_ATTR_WO(trigger_async_request);
134
79static int __init test_firmware_init(void) 135static int __init test_firmware_init(void)
80{ 136{
81 int rc; 137 int rc;
@@ -92,9 +148,20 @@ static int __init test_firmware_init(void)
92 goto dereg; 148 goto dereg;
93 } 149 }
94 150
151 rc = device_create_file(test_fw_misc_device.this_device,
152 &dev_attr_trigger_async_request);
153 if (rc) {
154 pr_err("could not create async sysfs interface: %d\n", rc);
155 goto remove_file;
156 }
157
95 pr_warn("interface ready\n"); 158 pr_warn("interface ready\n");
96 159
97 return 0; 160 return 0;
161
162remove_file:
163 device_remove_file(test_fw_misc_device.this_device,
164 &dev_attr_trigger_async_request);
98dereg: 165dereg:
99 misc_deregister(&test_fw_misc_device); 166 misc_deregister(&test_fw_misc_device);
100 return rc; 167 return rc;
@@ -106,6 +173,8 @@ static void __exit test_firmware_exit(void)
106{ 173{
107 release_firmware(test_firmware); 174 release_firmware(test_firmware);
108 device_remove_file(test_fw_misc_device.this_device, 175 device_remove_file(test_fw_misc_device.this_device,
176 &dev_attr_trigger_async_request);
177 device_remove_file(test_fw_misc_device.this_device,
109 &dev_attr_trigger_request); 178 &dev_attr_trigger_request);
110 misc_deregister(&test_fw_misc_device); 179 misc_deregister(&test_fw_misc_device);
111 pr_warn("removed interface\n"); 180 pr_warn("removed interface\n");
diff --git a/lib/test-hexdump.c b/lib/test_hexdump.c
index 5241df36eedf..3f415d8101f3 100644
--- a/lib/test-hexdump.c
+++ b/lib/test_hexdump.c
@@ -42,19 +42,21 @@ static const char * const test_data_8_le[] __initconst = {
42 "e9ac0f9cad319ca6", "0cafb1439919d14c", 42 "e9ac0f9cad319ca6", "0cafb1439919d14c",
43}; 43};
44 44
45static void __init test_hexdump(size_t len, int rowsize, int groupsize, 45#define FILL_CHAR '#'
46 bool ascii) 46
47static unsigned total_tests __initdata;
48static unsigned failed_tests __initdata;
49
50static void __init test_hexdump_prepare_test(size_t len, int rowsize,
51 int groupsize, char *test,
52 size_t testlen, bool ascii)
47{ 53{
48 char test[32 * 3 + 2 + 32 + 1];
49 char real[32 * 3 + 2 + 32 + 1];
50 char *p; 54 char *p;
51 const char * const *result; 55 const char * const *result;
52 size_t l = len; 56 size_t l = len;
53 int gs = groupsize, rs = rowsize; 57 int gs = groupsize, rs = rowsize;
54 unsigned int i; 58 unsigned int i;
55 59
56 hex_dump_to_buffer(data_b, l, rs, gs, real, sizeof(real), ascii);
57
58 if (rs != 16 && rs != 32) 60 if (rs != 16 && rs != 32)
59 rs = 16; 61 rs = 16;
60 62
@@ -73,8 +75,6 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
73 else 75 else
74 result = test_data_1_le; 76 result = test_data_1_le;
75 77
76 memset(test, ' ', sizeof(test));
77
78 /* hex dump */ 78 /* hex dump */
79 p = test; 79 p = test;
80 for (i = 0; i < l / gs; i++) { 80 for (i = 0; i < l / gs; i++) {
@@ -82,24 +82,49 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
82 size_t amount = strlen(q); 82 size_t amount = strlen(q);
83 83
84 strncpy(p, q, amount); 84 strncpy(p, q, amount);
85 p += amount + 1; 85 p += amount;
86
87 *p++ = ' ';
86 } 88 }
87 if (i) 89 if (i)
88 p--; 90 p--;
89 91
90 /* ASCII part */ 92 /* ASCII part */
91 if (ascii) { 93 if (ascii) {
92 p = test + rs * 2 + rs / gs + 1; 94 do {
95 *p++ = ' ';
96 } while (p < test + rs * 2 + rs / gs + 1);
97
93 strncpy(p, data_a, l); 98 strncpy(p, data_a, l);
94 p += l; 99 p += l;
95 } 100 }
96 101
97 *p = '\0'; 102 *p = '\0';
103}
98 104
99 if (strcmp(test, real)) { 105#define TEST_HEXDUMP_BUF_SIZE (32 * 3 + 2 + 32 + 1)
106
107static void __init test_hexdump(size_t len, int rowsize, int groupsize,
108 bool ascii)
109{
110 char test[TEST_HEXDUMP_BUF_SIZE];
111 char real[TEST_HEXDUMP_BUF_SIZE];
112
113 total_tests++;
114
115 memset(real, FILL_CHAR, sizeof(real));
116 hex_dump_to_buffer(data_b, len, rowsize, groupsize, real, sizeof(real),
117 ascii);
118
119 memset(test, FILL_CHAR, sizeof(test));
120 test_hexdump_prepare_test(len, rowsize, groupsize, test, sizeof(test),
121 ascii);
122
123 if (memcmp(test, real, TEST_HEXDUMP_BUF_SIZE)) {
100 pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize); 124 pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
101 pr_err("Result: '%s'\n", real); 125 pr_err("Result: '%s'\n", real);
102 pr_err("Expect: '%s'\n", test); 126 pr_err("Expect: '%s'\n", test);
127 failed_tests++;
103 } 128 }
104} 129}
105 130
@@ -114,52 +139,72 @@ static void __init test_hexdump_set(int rowsize, bool ascii)
114 test_hexdump(len, rowsize, 1, ascii); 139 test_hexdump(len, rowsize, 1, ascii);
115} 140}
116 141
117static void __init test_hexdump_overflow(bool ascii) 142static void __init test_hexdump_overflow(size_t buflen, size_t len,
143 int rowsize, int groupsize,
144 bool ascii)
118{ 145{
119 char buf[56]; 146 char test[TEST_HEXDUMP_BUF_SIZE];
120 const char *t = test_data_1_le[0]; 147 char buf[TEST_HEXDUMP_BUF_SIZE];
121 size_t l = get_random_int() % sizeof(buf); 148 int rs = rowsize, gs = groupsize;
149 int ae, he, e, f, r;
122 bool a; 150 bool a;
123 int e, r;
124 151
125 memset(buf, ' ', sizeof(buf)); 152 total_tests++;
153
154 memset(buf, FILL_CHAR, sizeof(buf));
126 155
127 r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii); 156 r = hex_dump_to_buffer(data_b, len, rs, gs, buf, buflen, ascii);
157
158 /*
159 * Caller must provide the data length multiple of groupsize. The
160 * calculations below are made with that assumption in mind.
161 */
162 ae = rs * 2 /* hex */ + rs / gs /* spaces */ + 1 /* space */ + len /* ascii */;
163 he = (gs * 2 /* hex */ + 1 /* space */) * len / gs - 1 /* no trailing space */;
128 164
129 if (ascii) 165 if (ascii)
130 e = 50; 166 e = ae;
131 else 167 else
132 e = 2; 168 e = he;
133 buf[e + 2] = '\0'; 169
134 170 f = min_t(int, e + 1, buflen);
135 if (!l) { 171 if (buflen) {
136 a = r == e && buf[0] == ' '; 172 test_hexdump_prepare_test(len, rs, gs, test, sizeof(test), ascii);
137 } else if (l < 3) { 173 test[f - 1] = '\0';
138 a = r == e && buf[0] == '\0';
139 } else if (l < 4) {
140 a = r == e && !strcmp(buf, t);
141 } else if (ascii) {
142 if (l < 51)
143 a = r == e && buf[l - 1] == '\0' && buf[l - 2] == ' ';
144 else
145 a = r == e && buf[50] == '\0' && buf[49] == '.';
146 } else {
147 a = r == e && buf[e] == '\0';
148 } 174 }
175 memset(test + f, FILL_CHAR, sizeof(test) - f);
176
177 a = r == e && !memcmp(test, buf, TEST_HEXDUMP_BUF_SIZE);
178
179 buf[sizeof(buf) - 1] = '\0';
149 180
150 if (!a) { 181 if (!a) {
151 pr_err("Len: %zu rc: %u strlen: %zu\n", l, r, strlen(buf)); 182 pr_err("Len: %zu buflen: %zu strlen: %zu\n",
152 pr_err("Result: '%s'\n", buf); 183 len, buflen, strnlen(buf, sizeof(buf)));
184 pr_err("Result: %d '%s'\n", r, buf);
185 pr_err("Expect: %d '%s'\n", e, test);
186 failed_tests++;
153 } 187 }
154} 188}
155 189
190static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
191{
192 unsigned int i = 0;
193 int rs = (get_random_int() % 2 + 1) * 16;
194
195 do {
196 int gs = 1 << i;
197 size_t len = get_random_int() % rs + gs;
198
199 test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
200 } while (i++ < 3);
201}
202
156static int __init test_hexdump_init(void) 203static int __init test_hexdump_init(void)
157{ 204{
158 unsigned int i; 205 unsigned int i;
159 int rowsize; 206 int rowsize;
160 207
161 pr_info("Running tests...\n");
162
163 rowsize = (get_random_int() % 2 + 1) * 16; 208 rowsize = (get_random_int() % 2 + 1) * 16;
164 for (i = 0; i < 16; i++) 209 for (i = 0; i < 16; i++)
165 test_hexdump_set(rowsize, false); 210 test_hexdump_set(rowsize, false);
@@ -168,13 +213,26 @@ static int __init test_hexdump_init(void)
168 for (i = 0; i < 16; i++) 213 for (i = 0; i < 16; i++)
169 test_hexdump_set(rowsize, true); 214 test_hexdump_set(rowsize, true);
170 215
171 for (i = 0; i < 16; i++) 216 for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
172 test_hexdump_overflow(false); 217 test_hexdump_overflow_set(i, false);
173 218
174 for (i = 0; i < 16; i++) 219 for (i = 0; i <= TEST_HEXDUMP_BUF_SIZE; i++)
175 test_hexdump_overflow(true); 220 test_hexdump_overflow_set(i, true);
221
222 if (failed_tests == 0)
223 pr_info("all %u tests passed\n", total_tests);
224 else
225 pr_err("failed %u out of %u tests\n", failed_tests, total_tests);
176 226
177 return -EINVAL; 227 return failed_tests ? -EINVAL : 0;
178} 228}
179module_init(test_hexdump_init); 229module_init(test_hexdump_init);
230
231static void __exit test_hexdump_exit(void)
232{
233 /* do nothing */
234}
235module_exit(test_hexdump_exit);
236
237MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
180MODULE_LICENSE("Dual BSD/GPL"); 238MODULE_LICENSE("Dual BSD/GPL");
diff --git a/lib/test_printf.c b/lib/test_printf.c
index c5a666af9ba5..563f10e6876a 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -12,10 +12,16 @@
12#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/string.h> 13#include <linux/string.h>
14 14
15#include <linux/bitmap.h>
16#include <linux/dcache.h>
15#include <linux/socket.h> 17#include <linux/socket.h>
16#include <linux/in.h> 18#include <linux/in.h>
17 19
20#include <linux/gfp.h>
21#include <linux/mm.h>
22
18#define BUF_SIZE 256 23#define BUF_SIZE 256
24#define PAD_SIZE 16
19#define FILL_CHAR '$' 25#define FILL_CHAR '$'
20 26
21#define PTR1 ((void*)0x01234567) 27#define PTR1 ((void*)0x01234567)
@@ -39,6 +45,7 @@
39static unsigned total_tests __initdata; 45static unsigned total_tests __initdata;
40static unsigned failed_tests __initdata; 46static unsigned failed_tests __initdata;
41static char *test_buffer __initdata; 47static char *test_buffer __initdata;
48static char *alloced_buffer __initdata;
42 49
43static int __printf(4, 0) __init 50static int __printf(4, 0) __init
44do_test(int bufsize, const char *expect, int elen, 51do_test(int bufsize, const char *expect, int elen,
@@ -49,7 +56,7 @@ do_test(int bufsize, const char *expect, int elen,
49 56
50 total_tests++; 57 total_tests++;
51 58
52 memset(test_buffer, FILL_CHAR, BUF_SIZE); 59 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
53 va_copy(aq, ap); 60 va_copy(aq, ap);
54 ret = vsnprintf(test_buffer, bufsize, fmt, aq); 61 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
55 va_end(aq); 62 va_end(aq);
@@ -60,8 +67,13 @@ do_test(int bufsize, const char *expect, int elen,
60 return 1; 67 return 1;
61 } 68 }
62 69
70 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
71 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
72 return 1;
73 }
74
63 if (!bufsize) { 75 if (!bufsize) {
64 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE)) { 76 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
65 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", 77 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
66 fmt); 78 fmt);
67 return 1; 79 return 1;
@@ -76,6 +88,12 @@ do_test(int bufsize, const char *expect, int elen,
76 return 1; 88 return 1;
77 } 89 }
78 90
91 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
92 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
93 bufsize, fmt);
94 return 1;
95 }
96
79 if (memcmp(test_buffer, expect, written)) { 97 if (memcmp(test_buffer, expect, written)) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", 98 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
81 bufsize, fmt, test_buffer, written, expect); 99 bufsize, fmt, test_buffer, written, expect);
@@ -91,7 +109,12 @@ __test(const char *expect, int elen, const char *fmt, ...)
91 int rand; 109 int rand;
92 char *p; 110 char *p;
93 111
94 BUG_ON(elen >= BUF_SIZE); 112 if (elen >= BUF_SIZE) {
113 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
114 elen, fmt);
115 failed_tests++;
116 return;
117 }
95 118
96 va_start(ap, fmt); 119 va_start(ap, fmt);
97 120
@@ -109,6 +132,7 @@ __test(const char *expect, int elen, const char *fmt, ...)
109 132
110 p = kvasprintf(GFP_KERNEL, fmt, ap); 133 p = kvasprintf(GFP_KERNEL, fmt, ap);
111 if (p) { 134 if (p) {
135 total_tests++;
112 if (memcmp(p, expect, elen+1)) { 136 if (memcmp(p, expect, elen+1)) {
113 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", 137 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
114 fmt, p, expect); 138 fmt, p, expect);
@@ -140,6 +164,30 @@ test_number(void)
140 test("0x1234abcd ", "%#-12x", 0x1234abcd); 164 test("0x1234abcd ", "%#-12x", 0x1234abcd);
141 test(" 0x1234abcd", "%#12x", 0x1234abcd); 165 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); 166 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
167 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
168 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
169 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
170 /*
171 * POSIX/C99: »The result of converting zero with an explicit
172 * precision of zero shall be no characters.« Hence the output
173 * from the below test should really be "00|0||| ". However,
174 * the kernel's printf also produces a single 0 in that
175 * case. This test case simply documents the current
176 * behaviour.
177 */
178 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
179#ifndef __CHAR_UNSIGNED__
180 {
181 /*
182 * Passing a 'char' to a %02x specifier doesn't do
183 * what was presumably the intention when char is
184 * signed and the value is negative. One must either &
185 * with 0xff or cast to u8.
186 */
187 char val = -16;
188 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
189 }
190#endif
143} 191}
144 192
145static void __init 193static void __init
@@ -148,14 +196,23 @@ test_string(void)
148 test("", "%s%.0s", "", "123"); 196 test("", "%s%.0s", "", "123");
149 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); 197 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"); 198 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
199 test("1234 ", "%-10.4s", "123456");
200 test(" 1234", "%10.4s", "123456");
151 /* 201 /*
152 * POSIX and C99 say that a missing precision should be 202 * POSIX and C99 say that a negative precision (which is only
153 * treated as a precision of 0. However, the kernel's printf 203 * possible to pass via a * argument) should be treated as if
154 * implementation treats this case as if the . wasn't 204 * the precision wasn't present, and that if the precision is
155 * present. Let's add a test case documenting the current 205 * omitted (as in %.s), the precision should be taken to be
156 * behaviour; should anyone ever feel the need to follow the 206 * 0. However, the kernel's printf behave exactly opposite,
157 * standards more closely, this can be revisited. 207 * treating a negative precision as 0 and treating an omitted
208 * precision specifier as if no precision was given.
209 *
210 * These test cases document the current behaviour; should
211 * anyone ever feel the need to follow the standards more
212 * closely, this can be revisited.
158 */ 213 */
214 test(" ", "%4.*s", -5, "123456");
215 test("123456", "%.s", "123456");
159 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); 216 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
160 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); 217 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
161} 218}
@@ -273,9 +330,35 @@ uuid(void)
273 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); 330 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
274} 331}
275 332
333static struct dentry test_dentry[4] __initdata = {
334 { .d_parent = &test_dentry[0],
335 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
336 .d_iname = "foo" },
337 { .d_parent = &test_dentry[0],
338 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
339 .d_iname = "bravo" },
340 { .d_parent = &test_dentry[1],
341 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
342 .d_iname = "alfa" },
343 { .d_parent = &test_dentry[2],
344 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
345 .d_iname = "romeo" },
346};
347
276static void __init 348static void __init
277dentry(void) 349dentry(void)
278{ 350{
351 test("foo", "%pd", &test_dentry[0]);
352 test("foo", "%pd2", &test_dentry[0]);
353
354 test("romeo", "%pd", &test_dentry[3]);
355 test("alfa/romeo", "%pd2", &test_dentry[3]);
356 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
357 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
358 test("/bravo/alfa", "%pd4", &test_dentry[2]);
359
360 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
361 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
279} 362}
280 363
281static void __init 364static void __init
@@ -289,6 +372,20 @@ struct_clk(void)
289} 372}
290 373
291static void __init 374static void __init
375large_bitmap(void)
376{
377 const int nbits = 1 << 16;
378 unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
379 if (!bits)
380 return;
381
382 bitmap_set(bits, 1, 20);
383 bitmap_set(bits, 60000, 15);
384 test("1-20,60000-60014", "%*pbl", nbits, bits);
385 kfree(bits);
386}
387
388static void __init
292bitmap(void) 389bitmap(void)
293{ 390{
294 DECLARE_BITMAP(bits, 20); 391 DECLARE_BITMAP(bits, 20);
@@ -307,6 +404,8 @@ bitmap(void)
307 bitmap_fill(bits, 20); 404 bitmap_fill(bits, 20);
308 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); 405 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
309 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); 406 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
407
408 large_bitmap();
310} 409}
311 410
312static void __init 411static void __init
@@ -315,6 +414,55 @@ netdev_features(void)
315} 414}
316 415
317static void __init 416static void __init
417flags(void)
418{
419 unsigned long flags;
420 gfp_t gfp;
421 char *cmp_buffer;
422
423 flags = 0;
424 test("", "%pGp", &flags);
425
426 /* Page flags should filter the zone id */
427 flags = 1UL << NR_PAGEFLAGS;
428 test("", "%pGp", &flags);
429
430 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
431 | 1UL << PG_active | 1UL << PG_swapbacked;
432 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
433
434
435 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
436 | VM_DENYWRITE;
437 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
438
439 gfp = GFP_TRANSHUGE;
440 test("GFP_TRANSHUGE", "%pGg", &gfp);
441
442 gfp = GFP_ATOMIC|__GFP_DMA;
443 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
444
445 gfp = __GFP_ATOMIC;
446 test("__GFP_ATOMIC", "%pGg", &gfp);
447
448 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
449 if (!cmp_buffer)
450 return;
451
452 /* Any flags not translated by the table should remain numeric */
453 gfp = ~__GFP_BITS_MASK;
454 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
455 test(cmp_buffer, "%pGg", &gfp);
456
457 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
458 (unsigned long) gfp);
459 gfp |= __GFP_ATOMIC;
460 test(cmp_buffer, "%pGg", &gfp);
461
462 kfree(cmp_buffer);
463}
464
465static void __init
318test_pointer(void) 466test_pointer(void)
319{ 467{
320 plain(); 468 plain();
@@ -332,21 +480,23 @@ test_pointer(void)
332 struct_clk(); 480 struct_clk();
333 bitmap(); 481 bitmap();
334 netdev_features(); 482 netdev_features();
483 flags();
335} 484}
336 485
337static int __init 486static int __init
338test_printf_init(void) 487test_printf_init(void)
339{ 488{
340 test_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); 489 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
341 if (!test_buffer) 490 if (!alloced_buffer)
342 return -ENOMEM; 491 return -ENOMEM;
492 test_buffer = alloced_buffer + PAD_SIZE;
343 493
344 test_basic(); 494 test_basic();
345 test_number(); 495 test_number();
346 test_string(); 496 test_string();
347 test_pointer(); 497 test_pointer();
348 498
349 kfree(test_buffer); 499 kfree(alloced_buffer);
350 500
351 if (failed_tests == 0) 501 if (failed_tests == 0)
352 pr_info("all %u tests passed\n", total_tests); 502 pr_info("all %u tests passed\n", total_tests);
diff --git a/lib/test_static_keys.c b/lib/test_static_keys.c
index c61b299e367f..915d75df2086 100644
--- a/lib/test_static_keys.c
+++ b/lib/test_static_keys.c
@@ -46,8 +46,11 @@ struct test_key {
46 bool (*test_key)(void); 46 bool (*test_key)(void);
47}; 47};
48 48
49#define test_key_func(key, branch) \ 49#define test_key_func(key, branch) \
50 ({bool func(void) { return branch(key); } func; }) 50static bool key ## _ ## branch(void) \
51{ \
52 return branch(&key); \
53}
51 54
52static void invert_key(struct static_key *key) 55static void invert_key(struct static_key *key)
53{ 56{
@@ -92,6 +95,25 @@ static int verify_keys(struct test_key *keys, int size, bool invert)
92 return 0; 95 return 0;
93} 96}
94 97
98test_key_func(old_true_key, static_key_true)
99test_key_func(old_false_key, static_key_false)
100test_key_func(true_key, static_branch_likely)
101test_key_func(true_key, static_branch_unlikely)
102test_key_func(false_key, static_branch_likely)
103test_key_func(false_key, static_branch_unlikely)
104test_key_func(base_old_true_key, static_key_true)
105test_key_func(base_inv_old_true_key, static_key_true)
106test_key_func(base_old_false_key, static_key_false)
107test_key_func(base_inv_old_false_key, static_key_false)
108test_key_func(base_true_key, static_branch_likely)
109test_key_func(base_true_key, static_branch_unlikely)
110test_key_func(base_inv_true_key, static_branch_likely)
111test_key_func(base_inv_true_key, static_branch_unlikely)
112test_key_func(base_false_key, static_branch_likely)
113test_key_func(base_false_key, static_branch_unlikely)
114test_key_func(base_inv_false_key, static_branch_likely)
115test_key_func(base_inv_false_key, static_branch_unlikely)
116
95static int __init test_static_key_init(void) 117static int __init test_static_key_init(void)
96{ 118{
97 int ret; 119 int ret;
@@ -102,95 +124,95 @@ static int __init test_static_key_init(void)
102 { 124 {
103 .init_state = true, 125 .init_state = true,
104 .key = &old_true_key, 126 .key = &old_true_key,
105 .test_key = test_key_func(&old_true_key, static_key_true), 127 .test_key = &old_true_key_static_key_true,
106 }, 128 },
107 { 129 {
108 .init_state = false, 130 .init_state = false,
109 .key = &old_false_key, 131 .key = &old_false_key,
110 .test_key = test_key_func(&old_false_key, static_key_false), 132 .test_key = &old_false_key_static_key_false,
111 }, 133 },
112 /* internal keys - new keys */ 134 /* internal keys - new keys */
113 { 135 {
114 .init_state = true, 136 .init_state = true,
115 .key = &true_key.key, 137 .key = &true_key.key,
116 .test_key = test_key_func(&true_key, static_branch_likely), 138 .test_key = &true_key_static_branch_likely,
117 }, 139 },
118 { 140 {
119 .init_state = true, 141 .init_state = true,
120 .key = &true_key.key, 142 .key = &true_key.key,
121 .test_key = test_key_func(&true_key, static_branch_unlikely), 143 .test_key = &true_key_static_branch_unlikely,
122 }, 144 },
123 { 145 {
124 .init_state = false, 146 .init_state = false,
125 .key = &false_key.key, 147 .key = &false_key.key,
126 .test_key = test_key_func(&false_key, static_branch_likely), 148 .test_key = &false_key_static_branch_likely,
127 }, 149 },
128 { 150 {
129 .init_state = false, 151 .init_state = false,
130 .key = &false_key.key, 152 .key = &false_key.key,
131 .test_key = test_key_func(&false_key, static_branch_unlikely), 153 .test_key = &false_key_static_branch_unlikely,
132 }, 154 },
133 /* external keys - old keys */ 155 /* external keys - old keys */
134 { 156 {
135 .init_state = true, 157 .init_state = true,
136 .key = &base_old_true_key, 158 .key = &base_old_true_key,
137 .test_key = test_key_func(&base_old_true_key, static_key_true), 159 .test_key = &base_old_true_key_static_key_true,
138 }, 160 },
139 { 161 {
140 .init_state = false, 162 .init_state = false,
141 .key = &base_inv_old_true_key, 163 .key = &base_inv_old_true_key,
142 .test_key = test_key_func(&base_inv_old_true_key, static_key_true), 164 .test_key = &base_inv_old_true_key_static_key_true,
143 }, 165 },
144 { 166 {
145 .init_state = false, 167 .init_state = false,
146 .key = &base_old_false_key, 168 .key = &base_old_false_key,
147 .test_key = test_key_func(&base_old_false_key, static_key_false), 169 .test_key = &base_old_false_key_static_key_false,
148 }, 170 },
149 { 171 {
150 .init_state = true, 172 .init_state = true,
151 .key = &base_inv_old_false_key, 173 .key = &base_inv_old_false_key,
152 .test_key = test_key_func(&base_inv_old_false_key, static_key_false), 174 .test_key = &base_inv_old_false_key_static_key_false,
153 }, 175 },
154 /* external keys - new keys */ 176 /* external keys - new keys */
155 { 177 {
156 .init_state = true, 178 .init_state = true,
157 .key = &base_true_key.key, 179 .key = &base_true_key.key,
158 .test_key = test_key_func(&base_true_key, static_branch_likely), 180 .test_key = &base_true_key_static_branch_likely,
159 }, 181 },
160 { 182 {
161 .init_state = true, 183 .init_state = true,
162 .key = &base_true_key.key, 184 .key = &base_true_key.key,
163 .test_key = test_key_func(&base_true_key, static_branch_unlikely), 185 .test_key = &base_true_key_static_branch_unlikely,
164 }, 186 },
165 { 187 {
166 .init_state = false, 188 .init_state = false,
167 .key = &base_inv_true_key.key, 189 .key = &base_inv_true_key.key,
168 .test_key = test_key_func(&base_inv_true_key, static_branch_likely), 190 .test_key = &base_inv_true_key_static_branch_likely,
169 }, 191 },
170 { 192 {
171 .init_state = false, 193 .init_state = false,
172 .key = &base_inv_true_key.key, 194 .key = &base_inv_true_key.key,
173 .test_key = test_key_func(&base_inv_true_key, static_branch_unlikely), 195 .test_key = &base_inv_true_key_static_branch_unlikely,
174 }, 196 },
175 { 197 {
176 .init_state = false, 198 .init_state = false,
177 .key = &base_false_key.key, 199 .key = &base_false_key.key,
178 .test_key = test_key_func(&base_false_key, static_branch_likely), 200 .test_key = &base_false_key_static_branch_likely,
179 }, 201 },
180 { 202 {
181 .init_state = false, 203 .init_state = false,
182 .key = &base_false_key.key, 204 .key = &base_false_key.key,
183 .test_key = test_key_func(&base_false_key, static_branch_unlikely), 205 .test_key = &base_false_key_static_branch_unlikely,
184 }, 206 },
185 { 207 {
186 .init_state = true, 208 .init_state = true,
187 .key = &base_inv_false_key.key, 209 .key = &base_inv_false_key.key,
188 .test_key = test_key_func(&base_inv_false_key, static_branch_likely), 210 .test_key = &base_inv_false_key_static_branch_likely,
189 }, 211 },
190 { 212 {
191 .init_state = true, 213 .init_state = true,
192 .key = &base_inv_false_key.key, 214 .key = &base_inv_false_key.key,
193 .test_key = test_key_func(&base_inv_false_key, static_branch_unlikely), 215 .test_key = &base_inv_false_key_static_branch_unlikely,
194 }, 216 },
195 }; 217 };
196 218
diff --git a/lib/ubsan.c b/lib/ubsan.c
new file mode 100644
index 000000000000..8799ae5e2e42
--- /dev/null
+++ b/lib/ubsan.c
@@ -0,0 +1,456 @@
1/*
2 * UBSAN error reporting functions
3 *
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5 * Author: Andrey Ryabinin <ryabinin.a.a@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
12
13#include <linux/bitops.h>
14#include <linux/bug.h>
15#include <linux/ctype.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/types.h>
19#include <linux/sched.h>
20
21#include "ubsan.h"
22
23const char *type_check_kinds[] = {
24 "load of",
25 "store to",
26 "reference binding to",
27 "member access within",
28 "member call on",
29 "constructor call on",
30 "downcast of",
31 "downcast of"
32};
33
34#define REPORTED_BIT 31
35
36#if (BITS_PER_LONG == 64) && defined(__BIG_ENDIAN)
37#define COLUMN_MASK (~(1U << REPORTED_BIT))
38#define LINE_MASK (~0U)
39#else
40#define COLUMN_MASK (~0U)
41#define LINE_MASK (~(1U << REPORTED_BIT))
42#endif
43
44#define VALUE_LENGTH 40
45
46static bool was_reported(struct source_location *location)
47{
48 return test_and_set_bit(REPORTED_BIT, &location->reported);
49}
50
51static void print_source_location(const char *prefix,
52 struct source_location *loc)
53{
54 pr_err("%s %s:%d:%d\n", prefix, loc->file_name,
55 loc->line & LINE_MASK, loc->column & COLUMN_MASK);
56}
57
58static bool suppress_report(struct source_location *loc)
59{
60 return current->in_ubsan || was_reported(loc);
61}
62
63static bool type_is_int(struct type_descriptor *type)
64{
65 return type->type_kind == type_kind_int;
66}
67
68static bool type_is_signed(struct type_descriptor *type)
69{
70 WARN_ON(!type_is_int(type));
71 return type->type_info & 1;
72}
73
74static unsigned type_bit_width(struct type_descriptor *type)
75{
76 return 1 << (type->type_info >> 1);
77}
78
79static bool is_inline_int(struct type_descriptor *type)
80{
81 unsigned inline_bits = sizeof(unsigned long)*8;
82 unsigned bits = type_bit_width(type);
83
84 WARN_ON(!type_is_int(type));
85
86 return bits <= inline_bits;
87}
88
89static s_max get_signed_val(struct type_descriptor *type, unsigned long val)
90{
91 if (is_inline_int(type)) {
92 unsigned extra_bits = sizeof(s_max)*8 - type_bit_width(type);
93 return ((s_max)val) << extra_bits >> extra_bits;
94 }
95
96 if (type_bit_width(type) == 64)
97 return *(s64 *)val;
98
99 return *(s_max *)val;
100}
101
102static bool val_is_negative(struct type_descriptor *type, unsigned long val)
103{
104 return type_is_signed(type) && get_signed_val(type, val) < 0;
105}
106
107static u_max get_unsigned_val(struct type_descriptor *type, unsigned long val)
108{
109 if (is_inline_int(type))
110 return val;
111
112 if (type_bit_width(type) == 64)
113 return *(u64 *)val;
114
115 return *(u_max *)val;
116}
117
118static void val_to_string(char *str, size_t size, struct type_descriptor *type,
119 unsigned long value)
120{
121 if (type_is_int(type)) {
122 if (type_bit_width(type) == 128) {
123#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
124 u_max val = get_unsigned_val(type, value);
125
126 scnprintf(str, size, "0x%08x%08x%08x%08x",
127 (u32)(val >> 96),
128 (u32)(val >> 64),
129 (u32)(val >> 32),
130 (u32)(val));
131#else
132 WARN_ON(1);
133#endif
134 } else if (type_is_signed(type)) {
135 scnprintf(str, size, "%lld",
136 (s64)get_signed_val(type, value));
137 } else {
138 scnprintf(str, size, "%llu",
139 (u64)get_unsigned_val(type, value));
140 }
141 }
142}
143
144static bool location_is_valid(struct source_location *loc)
145{
146 return loc->file_name != NULL;
147}
148
149static DEFINE_SPINLOCK(report_lock);
150
151static void ubsan_prologue(struct source_location *location,
152 unsigned long *flags)
153{
154 current->in_ubsan++;
155 spin_lock_irqsave(&report_lock, *flags);
156
157 pr_err("========================================"
158 "========================================\n");
159 print_source_location("UBSAN: Undefined behaviour in", location);
160}
161
162static void ubsan_epilogue(unsigned long *flags)
163{
164 dump_stack();
165 pr_err("========================================"
166 "========================================\n");
167 spin_unlock_irqrestore(&report_lock, *flags);
168 current->in_ubsan--;
169}
170
171static void handle_overflow(struct overflow_data *data, unsigned long lhs,
172 unsigned long rhs, char op)
173{
174
175 struct type_descriptor *type = data->type;
176 unsigned long flags;
177 char lhs_val_str[VALUE_LENGTH];
178 char rhs_val_str[VALUE_LENGTH];
179
180 if (suppress_report(&data->location))
181 return;
182
183 ubsan_prologue(&data->location, &flags);
184
185 val_to_string(lhs_val_str, sizeof(lhs_val_str), type, lhs);
186 val_to_string(rhs_val_str, sizeof(rhs_val_str), type, rhs);
187 pr_err("%s integer overflow:\n",
188 type_is_signed(type) ? "signed" : "unsigned");
189 pr_err("%s %c %s cannot be represented in type %s\n",
190 lhs_val_str,
191 op,
192 rhs_val_str,
193 type->type_name);
194
195 ubsan_epilogue(&flags);
196}
197
198void __ubsan_handle_add_overflow(struct overflow_data *data,
199 unsigned long lhs,
200 unsigned long rhs)
201{
202
203 handle_overflow(data, lhs, rhs, '+');
204}
205EXPORT_SYMBOL(__ubsan_handle_add_overflow);
206
207void __ubsan_handle_sub_overflow(struct overflow_data *data,
208 unsigned long lhs,
209 unsigned long rhs)
210{
211 handle_overflow(data, lhs, rhs, '-');
212}
213EXPORT_SYMBOL(__ubsan_handle_sub_overflow);
214
215void __ubsan_handle_mul_overflow(struct overflow_data *data,
216 unsigned long lhs,
217 unsigned long rhs)
218{
219 handle_overflow(data, lhs, rhs, '*');
220}
221EXPORT_SYMBOL(__ubsan_handle_mul_overflow);
222
223void __ubsan_handle_negate_overflow(struct overflow_data *data,
224 unsigned long old_val)
225{
226 unsigned long flags;
227 char old_val_str[VALUE_LENGTH];
228
229 if (suppress_report(&data->location))
230 return;
231
232 ubsan_prologue(&data->location, &flags);
233
234 val_to_string(old_val_str, sizeof(old_val_str), data->type, old_val);
235
236 pr_err("negation of %s cannot be represented in type %s:\n",
237 old_val_str, data->type->type_name);
238
239 ubsan_epilogue(&flags);
240}
241EXPORT_SYMBOL(__ubsan_handle_negate_overflow);
242
243
244void __ubsan_handle_divrem_overflow(struct overflow_data *data,
245 unsigned long lhs,
246 unsigned long rhs)
247{
248 unsigned long flags;
249 char rhs_val_str[VALUE_LENGTH];
250
251 if (suppress_report(&data->location))
252 return;
253
254 ubsan_prologue(&data->location, &flags);
255
256 val_to_string(rhs_val_str, sizeof(rhs_val_str), data->type, rhs);
257
258 if (type_is_signed(data->type) && get_signed_val(data->type, rhs) == -1)
259 pr_err("division of %s by -1 cannot be represented in type %s\n",
260 rhs_val_str, data->type->type_name);
261 else
262 pr_err("division by zero\n");
263
264 ubsan_epilogue(&flags);
265}
266EXPORT_SYMBOL(__ubsan_handle_divrem_overflow);
267
268static void handle_null_ptr_deref(struct type_mismatch_data *data)
269{
270 unsigned long flags;
271
272 if (suppress_report(&data->location))
273 return;
274
275 ubsan_prologue(&data->location, &flags);
276
277 pr_err("%s null pointer of type %s\n",
278 type_check_kinds[data->type_check_kind],
279 data->type->type_name);
280
281 ubsan_epilogue(&flags);
282}
283
284static void handle_missaligned_access(struct type_mismatch_data *data,
285 unsigned long ptr)
286{
287 unsigned long flags;
288
289 if (suppress_report(&data->location))
290 return;
291
292 ubsan_prologue(&data->location, &flags);
293
294 pr_err("%s misaligned address %p for type %s\n",
295 type_check_kinds[data->type_check_kind],
296 (void *)ptr, data->type->type_name);
297 pr_err("which requires %ld byte alignment\n", data->alignment);
298
299 ubsan_epilogue(&flags);
300}
301
302static void handle_object_size_mismatch(struct type_mismatch_data *data,
303 unsigned long ptr)
304{
305 unsigned long flags;
306
307 if (suppress_report(&data->location))
308 return;
309
310 ubsan_prologue(&data->location, &flags);
311 pr_err("%s address %pk with insufficient space\n",
312 type_check_kinds[data->type_check_kind],
313 (void *) ptr);
314 pr_err("for an object of type %s\n", data->type->type_name);
315 ubsan_epilogue(&flags);
316}
317
318void __ubsan_handle_type_mismatch(struct type_mismatch_data *data,
319 unsigned long ptr)
320{
321
322 if (!ptr)
323 handle_null_ptr_deref(data);
324 else if (data->alignment && !IS_ALIGNED(ptr, data->alignment))
325 handle_missaligned_access(data, ptr);
326 else
327 handle_object_size_mismatch(data, ptr);
328}
329EXPORT_SYMBOL(__ubsan_handle_type_mismatch);
330
331void __ubsan_handle_nonnull_return(struct nonnull_return_data *data)
332{
333 unsigned long flags;
334
335 if (suppress_report(&data->location))
336 return;
337
338 ubsan_prologue(&data->location, &flags);
339
340 pr_err("null pointer returned from function declared to never return null\n");
341
342 if (location_is_valid(&data->attr_location))
343 print_source_location("returns_nonnull attribute specified in",
344 &data->attr_location);
345
346 ubsan_epilogue(&flags);
347}
348EXPORT_SYMBOL(__ubsan_handle_nonnull_return);
349
350void __ubsan_handle_vla_bound_not_positive(struct vla_bound_data *data,
351 unsigned long bound)
352{
353 unsigned long flags;
354 char bound_str[VALUE_LENGTH];
355
356 if (suppress_report(&data->location))
357 return;
358
359 ubsan_prologue(&data->location, &flags);
360
361 val_to_string(bound_str, sizeof(bound_str), data->type, bound);
362 pr_err("variable length array bound value %s <= 0\n", bound_str);
363
364 ubsan_epilogue(&flags);
365}
366EXPORT_SYMBOL(__ubsan_handle_vla_bound_not_positive);
367
368void __ubsan_handle_out_of_bounds(struct out_of_bounds_data *data,
369 unsigned long index)
370{
371 unsigned long flags;
372 char index_str[VALUE_LENGTH];
373
374 if (suppress_report(&data->location))
375 return;
376
377 ubsan_prologue(&data->location, &flags);
378
379 val_to_string(index_str, sizeof(index_str), data->index_type, index);
380 pr_err("index %s is out of range for type %s\n", index_str,
381 data->array_type->type_name);
382 ubsan_epilogue(&flags);
383}
384EXPORT_SYMBOL(__ubsan_handle_out_of_bounds);
385
386void __ubsan_handle_shift_out_of_bounds(struct shift_out_of_bounds_data *data,
387 unsigned long lhs, unsigned long rhs)
388{
389 unsigned long flags;
390 struct type_descriptor *rhs_type = data->rhs_type;
391 struct type_descriptor *lhs_type = data->lhs_type;
392 char rhs_str[VALUE_LENGTH];
393 char lhs_str[VALUE_LENGTH];
394
395 if (suppress_report(&data->location))
396 return;
397
398 ubsan_prologue(&data->location, &flags);
399
400 val_to_string(rhs_str, sizeof(rhs_str), rhs_type, rhs);
401 val_to_string(lhs_str, sizeof(lhs_str), lhs_type, lhs);
402
403 if (val_is_negative(rhs_type, rhs))
404 pr_err("shift exponent %s is negative\n", rhs_str);
405
406 else if (get_unsigned_val(rhs_type, rhs) >=
407 type_bit_width(lhs_type))
408 pr_err("shift exponent %s is too large for %u-bit type %s\n",
409 rhs_str,
410 type_bit_width(lhs_type),
411 lhs_type->type_name);
412 else if (val_is_negative(lhs_type, lhs))
413 pr_err("left shift of negative value %s\n",
414 lhs_str);
415 else
416 pr_err("left shift of %s by %s places cannot be"
417 " represented in type %s\n",
418 lhs_str, rhs_str,
419 lhs_type->type_name);
420
421 ubsan_epilogue(&flags);
422}
423EXPORT_SYMBOL(__ubsan_handle_shift_out_of_bounds);
424
425
426void __noreturn
427__ubsan_handle_builtin_unreachable(struct unreachable_data *data)
428{
429 unsigned long flags;
430
431 ubsan_prologue(&data->location, &flags);
432 pr_err("calling __builtin_unreachable()\n");
433 ubsan_epilogue(&flags);
434 panic("can't return from __builtin_unreachable()");
435}
436EXPORT_SYMBOL(__ubsan_handle_builtin_unreachable);
437
438void __ubsan_handle_load_invalid_value(struct invalid_value_data *data,
439 unsigned long val)
440{
441 unsigned long flags;
442 char val_str[VALUE_LENGTH];
443
444 if (suppress_report(&data->location))
445 return;
446
447 ubsan_prologue(&data->location, &flags);
448
449 val_to_string(val_str, sizeof(val_str), data->type, val);
450
451 pr_err("load of value %s is not a valid value for type %s\n",
452 val_str, data->type->type_name);
453
454 ubsan_epilogue(&flags);
455}
456EXPORT_SYMBOL(__ubsan_handle_load_invalid_value);
diff --git a/lib/ubsan.h b/lib/ubsan.h
new file mode 100644
index 000000000000..b2d18d4a53f5
--- /dev/null
+++ b/lib/ubsan.h
@@ -0,0 +1,84 @@
1#ifndef _LIB_UBSAN_H
2#define _LIB_UBSAN_H
3
4enum {
5 type_kind_int = 0,
6 type_kind_float = 1,
7 type_unknown = 0xffff
8};
9
10struct type_descriptor {
11 u16 type_kind;
12 u16 type_info;
13 char type_name[1];
14};
15
16struct source_location {
17 const char *file_name;
18 union {
19 unsigned long reported;
20 struct {
21 u32 line;
22 u32 column;
23 };
24 };
25};
26
27struct overflow_data {
28 struct source_location location;
29 struct type_descriptor *type;
30};
31
32struct type_mismatch_data {
33 struct source_location location;
34 struct type_descriptor *type;
35 unsigned long alignment;
36 unsigned char type_check_kind;
37};
38
39struct nonnull_arg_data {
40 struct source_location location;
41 struct source_location attr_location;
42 int arg_index;
43};
44
45struct nonnull_return_data {
46 struct source_location location;
47 struct source_location attr_location;
48};
49
50struct vla_bound_data {
51 struct source_location location;
52 struct type_descriptor *type;
53};
54
55struct out_of_bounds_data {
56 struct source_location location;
57 struct type_descriptor *array_type;
58 struct type_descriptor *index_type;
59};
60
61struct shift_out_of_bounds_data {
62 struct source_location location;
63 struct type_descriptor *lhs_type;
64 struct type_descriptor *rhs_type;
65};
66
67struct unreachable_data {
68 struct source_location location;
69};
70
71struct invalid_value_data {
72 struct source_location location;
73 struct type_descriptor *type;
74};
75
76#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
77typedef __int128 s_max;
78typedef unsigned __int128 u_max;
79#else
80typedef s64 s_max;
81typedef u64 u_max;
82#endif
83
84#endif
diff --git a/lib/ucs2_string.c b/lib/ucs2_string.c
index 6f500ef2301d..f0b323abb4c6 100644
--- a/lib/ucs2_string.c
+++ b/lib/ucs2_string.c
@@ -49,3 +49,65 @@ ucs2_strncmp(const ucs2_char_t *a, const ucs2_char_t *b, size_t len)
49 } 49 }
50} 50}
51EXPORT_SYMBOL(ucs2_strncmp); 51EXPORT_SYMBOL(ucs2_strncmp);
52
53unsigned long
54ucs2_utf8size(const ucs2_char_t *src)
55{
56 unsigned long i;
57 unsigned long j = 0;
58
59 for (i = 0; i < ucs2_strlen(src); i++) {
60 u16 c = src[i];
61
62 if (c >= 0x800)
63 j += 3;
64 else if (c >= 0x80)
65 j += 2;
66 else
67 j += 1;
68 }
69
70 return j;
71}
72EXPORT_SYMBOL(ucs2_utf8size);
73
74/*
75 * copy at most maxlength bytes of whole utf8 characters to dest from the
76 * ucs2 string src.
77 *
78 * The return value is the number of characters copied, not including the
79 * final NUL character.
80 */
81unsigned long
82ucs2_as_utf8(u8 *dest, const ucs2_char_t *src, unsigned long maxlength)
83{
84 unsigned int i;
85 unsigned long j = 0;
86 unsigned long limit = ucs2_strnlen(src, maxlength);
87
88 for (i = 0; maxlength && i < limit; i++) {
89 u16 c = src[i];
90
91 if (c >= 0x800) {
92 if (maxlength < 3)
93 break;
94 maxlength -= 3;
95 dest[j++] = 0xe0 | (c & 0xf000) >> 12;
96 dest[j++] = 0x80 | (c & 0x0fc0) >> 6;
97 dest[j++] = 0x80 | (c & 0x003f);
98 } else if (c >= 0x80) {
99 if (maxlength < 2)
100 break;
101 maxlength -= 2;
102 dest[j++] = 0xc0 | (c & 0x7c0) >> 6;
103 dest[j++] = 0x80 | (c & 0x03f);
104 } else {
105 maxlength -= 1;
106 dest[j++] = c & 0x7f;
107 }
108 }
109 if (maxlength)
110 dest[j] = '\0';
111 return j;
112}
113EXPORT_SYMBOL(ucs2_as_utf8);
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index ac3f9476b776..525c8e19bda2 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -35,6 +35,8 @@
35#include <linux/blkdev.h> 35#include <linux/blkdev.h>
36#endif 36#endif
37 37
38#include "../mm/internal.h" /* For the trace_print_flags arrays */
39
38#include <asm/page.h> /* for PAGE_SIZE */ 40#include <asm/page.h> /* for PAGE_SIZE */
39#include <asm/sections.h> /* for dereference_function_descriptor() */ 41#include <asm/sections.h> /* for dereference_function_descriptor() */
40#include <asm/byteorder.h> /* cpu_to_le16 */ 42#include <asm/byteorder.h> /* cpu_to_le16 */
@@ -383,13 +385,14 @@ enum format_type {
383}; 385};
384 386
385struct printf_spec { 387struct printf_spec {
386 u8 type; /* format_type enum */ 388 unsigned int type:8; /* format_type enum */
387 u8 flags; /* flags to number() */ 389 signed int field_width:24; /* width of output field */
388 u8 base; /* number base, 8, 10 or 16 only */ 390 unsigned int flags:8; /* flags to number() */
389 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 391 unsigned int base:8; /* number base, 8, 10 or 16 only */
390 s16 field_width; /* width of output field */ 392 signed int precision:16; /* # of digits/chars */
391 s16 precision; /* # of digits/chars */ 393} __packed;
392}; 394#define FIELD_WIDTH_MAX ((1 << 23) - 1)
395#define PRECISION_MAX ((1 << 15) - 1)
393 396
394static noinline_for_stack 397static noinline_for_stack
395char *number(char *buf, char *end, unsigned long long num, 398char *number(char *buf, char *end, unsigned long long num,
@@ -402,6 +405,10 @@ char *number(char *buf, char *end, unsigned long long num,
402 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 405 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
403 int i; 406 int i;
404 bool is_zero = num == 0LL; 407 bool is_zero = num == 0LL;
408 int field_width = spec.field_width;
409 int precision = spec.precision;
410
411 BUILD_BUG_ON(sizeof(struct printf_spec) != 8);
405 412
406 /* locase = 0 or 0x20. ORing digits or letters with 'locase' 413 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
407 * produces same digits or (maybe lowercased) letters */ 414 * produces same digits or (maybe lowercased) letters */
@@ -413,20 +420,20 @@ char *number(char *buf, char *end, unsigned long long num,
413 if ((signed long long)num < 0) { 420 if ((signed long long)num < 0) {
414 sign = '-'; 421 sign = '-';
415 num = -(signed long long)num; 422 num = -(signed long long)num;
416 spec.field_width--; 423 field_width--;
417 } else if (spec.flags & PLUS) { 424 } else if (spec.flags & PLUS) {
418 sign = '+'; 425 sign = '+';
419 spec.field_width--; 426 field_width--;
420 } else if (spec.flags & SPACE) { 427 } else if (spec.flags & SPACE) {
421 sign = ' '; 428 sign = ' ';
422 spec.field_width--; 429 field_width--;
423 } 430 }
424 } 431 }
425 if (need_pfx) { 432 if (need_pfx) {
426 if (spec.base == 16) 433 if (spec.base == 16)
427 spec.field_width -= 2; 434 field_width -= 2;
428 else if (!is_zero) 435 else if (!is_zero)
429 spec.field_width--; 436 field_width--;
430 } 437 }
431 438
432 /* generate full string in tmp[], in reverse order */ 439 /* generate full string in tmp[], in reverse order */
@@ -448,12 +455,12 @@ char *number(char *buf, char *end, unsigned long long num,
448 } 455 }
449 456
450 /* printing 100 using %2d gives "100", not "00" */ 457 /* printing 100 using %2d gives "100", not "00" */
451 if (i > spec.precision) 458 if (i > precision)
452 spec.precision = i; 459 precision = i;
453 /* leading space padding */ 460 /* leading space padding */
454 spec.field_width -= spec.precision; 461 field_width -= precision;
455 if (!(spec.flags & (ZEROPAD | LEFT))) { 462 if (!(spec.flags & (ZEROPAD | LEFT))) {
456 while (--spec.field_width >= 0) { 463 while (--field_width >= 0) {
457 if (buf < end) 464 if (buf < end)
458 *buf = ' '; 465 *buf = ' ';
459 ++buf; 466 ++buf;
@@ -482,14 +489,14 @@ char *number(char *buf, char *end, unsigned long long num,
482 if (!(spec.flags & LEFT)) { 489 if (!(spec.flags & LEFT)) {
483 char c = ' ' + (spec.flags & ZEROPAD); 490 char c = ' ' + (spec.flags & ZEROPAD);
484 BUILD_BUG_ON(' ' + ZEROPAD != '0'); 491 BUILD_BUG_ON(' ' + ZEROPAD != '0');
485 while (--spec.field_width >= 0) { 492 while (--field_width >= 0) {
486 if (buf < end) 493 if (buf < end)
487 *buf = c; 494 *buf = c;
488 ++buf; 495 ++buf;
489 } 496 }
490 } 497 }
491 /* hmm even more zero padding? */ 498 /* hmm even more zero padding? */
492 while (i <= --spec.precision) { 499 while (i <= --precision) {
493 if (buf < end) 500 if (buf < end)
494 *buf = '0'; 501 *buf = '0';
495 ++buf; 502 ++buf;
@@ -501,7 +508,7 @@ char *number(char *buf, char *end, unsigned long long num,
501 ++buf; 508 ++buf;
502 } 509 }
503 /* trailing space padding */ 510 /* trailing space padding */
504 while (--spec.field_width >= 0) { 511 while (--field_width >= 0) {
505 if (buf < end) 512 if (buf < end)
506 *buf = ' '; 513 *buf = ' ';
507 ++buf; 514 ++buf;
@@ -511,37 +518,20 @@ char *number(char *buf, char *end, unsigned long long num,
511} 518}
512 519
513static noinline_for_stack 520static noinline_for_stack
514char *string(char *buf, char *end, const char *s, struct printf_spec spec) 521char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
515{ 522{
516 int len, i; 523 struct printf_spec spec;
517 524
518 if ((unsigned long)s < PAGE_SIZE) 525 spec.type = FORMAT_TYPE_PTR;
519 s = "(null)"; 526 spec.field_width = 2 + 2 * size; /* 0x + hex */
520 527 spec.flags = SPECIAL | SMALL | ZEROPAD;
521 len = strnlen(s, spec.precision); 528 spec.base = 16;
522 529 spec.precision = -1;
523 if (!(spec.flags & LEFT)) {
524 while (len < spec.field_width--) {
525 if (buf < end)
526 *buf = ' ';
527 ++buf;
528 }
529 }
530 for (i = 0; i < len; ++i) {
531 if (buf < end)
532 *buf = *s;
533 ++buf; ++s;
534 }
535 while (len < spec.field_width--) {
536 if (buf < end)
537 *buf = ' ';
538 ++buf;
539 }
540 530
541 return buf; 531 return number(buf, end, num, spec);
542} 532}
543 533
544static void widen(char *buf, char *end, unsigned len, unsigned spaces) 534static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
545{ 535{
546 size_t size; 536 size_t size;
547 if (buf >= end) /* nowhere to put anything */ 537 if (buf >= end) /* nowhere to put anything */
@@ -559,6 +549,56 @@ static void widen(char *buf, char *end, unsigned len, unsigned spaces)
559 memset(buf, ' ', spaces); 549 memset(buf, ' ', spaces);
560} 550}
561 551
552/*
553 * Handle field width padding for a string.
554 * @buf: current buffer position
555 * @n: length of string
556 * @end: end of output buffer
557 * @spec: for field width and flags
558 * Returns: new buffer position after padding.
559 */
560static noinline_for_stack
561char *widen_string(char *buf, int n, char *end, struct printf_spec spec)
562{
563 unsigned spaces;
564
565 if (likely(n >= spec.field_width))
566 return buf;
567 /* we want to pad the sucker */
568 spaces = spec.field_width - n;
569 if (!(spec.flags & LEFT)) {
570 move_right(buf - n, end, n, spaces);
571 return buf + spaces;
572 }
573 while (spaces--) {
574 if (buf < end)
575 *buf = ' ';
576 ++buf;
577 }
578 return buf;
579}
580
581static noinline_for_stack
582char *string(char *buf, char *end, const char *s, struct printf_spec spec)
583{
584 int len = 0;
585 size_t lim = spec.precision;
586
587 if ((unsigned long)s < PAGE_SIZE)
588 s = "(null)";
589
590 while (lim--) {
591 char c = *s++;
592 if (!c)
593 break;
594 if (buf < end)
595 *buf = c;
596 ++buf;
597 ++len;
598 }
599 return widen_string(buf, len, end, spec);
600}
601
562static noinline_for_stack 602static noinline_for_stack
563char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec, 603char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
564 const char *fmt) 604 const char *fmt)
@@ -600,20 +640,7 @@ char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_sp
600 *buf = c; 640 *buf = c;
601 } 641 }
602 rcu_read_unlock(); 642 rcu_read_unlock();
603 if (n < spec.field_width) { 643 return widen_string(buf, n, end, spec);
604 /* we want to pad the sucker */
605 unsigned spaces = spec.field_width - n;
606 if (!(spec.flags & LEFT)) {
607 widen(buf - n, end, n, spaces);
608 return buf + spaces;
609 }
610 while (spaces--) {
611 if (buf < end)
612 *buf = ' ';
613 ++buf;
614 }
615 }
616 return buf;
617} 644}
618 645
619#ifdef CONFIG_BLOCK 646#ifdef CONFIG_BLOCK
@@ -659,11 +686,7 @@ char *symbol_string(char *buf, char *end, void *ptr,
659 686
660 return string(buf, end, sym, spec); 687 return string(buf, end, sym, spec);
661#else 688#else
662 spec.field_width = 2 * sizeof(void *); 689 return special_hex_number(buf, end, value, sizeof(void *));
663 spec.flags |= SPECIAL | SMALL | ZEROPAD;
664 spec.base = 16;
665
666 return number(buf, end, value, spec);
667#endif 690#endif
668} 691}
669 692
@@ -1324,40 +1347,45 @@ char *uuid_string(char *buf, char *end, const u8 *addr,
1324 return string(buf, end, uuid, spec); 1347 return string(buf, end, uuid, spec);
1325} 1348}
1326 1349
1327static 1350static noinline_for_stack
1328char *netdev_feature_string(char *buf, char *end, const u8 *addr, 1351char *netdev_bits(char *buf, char *end, const void *addr, const char *fmt)
1329 struct printf_spec spec)
1330{ 1352{
1331 spec.flags |= SPECIAL | SMALL | ZEROPAD; 1353 unsigned long long num;
1332 if (spec.field_width == -1) 1354 int size;
1333 spec.field_width = 2 + 2 * sizeof(netdev_features_t); 1355
1334 spec.base = 16; 1356 switch (fmt[1]) {
1357 case 'F':
1358 num = *(const netdev_features_t *)addr;
1359 size = sizeof(netdev_features_t);
1360 break;
1361 default:
1362 num = (unsigned long)addr;
1363 size = sizeof(unsigned long);
1364 break;
1365 }
1335 1366
1336 return number(buf, end, *(const netdev_features_t *)addr, spec); 1367 return special_hex_number(buf, end, num, size);
1337} 1368}
1338 1369
1339static noinline_for_stack 1370static noinline_for_stack
1340char *address_val(char *buf, char *end, const void *addr, 1371char *address_val(char *buf, char *end, const void *addr, const char *fmt)
1341 struct printf_spec spec, const char *fmt)
1342{ 1372{
1343 unsigned long long num; 1373 unsigned long long num;
1344 1374 int size;
1345 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1346 spec.base = 16;
1347 1375
1348 switch (fmt[1]) { 1376 switch (fmt[1]) {
1349 case 'd': 1377 case 'd':
1350 num = *(const dma_addr_t *)addr; 1378 num = *(const dma_addr_t *)addr;
1351 spec.field_width = sizeof(dma_addr_t) * 2 + 2; 1379 size = sizeof(dma_addr_t);
1352 break; 1380 break;
1353 case 'p': 1381 case 'p':
1354 default: 1382 default:
1355 num = *(const phys_addr_t *)addr; 1383 num = *(const phys_addr_t *)addr;
1356 spec.field_width = sizeof(phys_addr_t) * 2 + 2; 1384 size = sizeof(phys_addr_t);
1357 break; 1385 break;
1358 } 1386 }
1359 1387
1360 return number(buf, end, num, spec); 1388 return special_hex_number(buf, end, num, size);
1361} 1389}
1362 1390
1363static noinline_for_stack 1391static noinline_for_stack
@@ -1376,14 +1404,77 @@ char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
1376#ifdef CONFIG_COMMON_CLK 1404#ifdef CONFIG_COMMON_CLK
1377 return string(buf, end, __clk_get_name(clk), spec); 1405 return string(buf, end, __clk_get_name(clk), spec);
1378#else 1406#else
1379 spec.base = 16; 1407 return special_hex_number(buf, end, (unsigned long)clk, sizeof(unsigned long));
1380 spec.field_width = sizeof(unsigned long) * 2 + 2;
1381 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1382 return number(buf, end, (unsigned long)clk, spec);
1383#endif 1408#endif
1384 } 1409 }
1385} 1410}
1386 1411
1412static
1413char *format_flags(char *buf, char *end, unsigned long flags,
1414 const struct trace_print_flags *names)
1415{
1416 unsigned long mask;
1417 const struct printf_spec strspec = {
1418 .field_width = -1,
1419 .precision = -1,
1420 };
1421 const struct printf_spec numspec = {
1422 .flags = SPECIAL|SMALL,
1423 .field_width = -1,
1424 .precision = -1,
1425 .base = 16,
1426 };
1427
1428 for ( ; flags && names->name; names++) {
1429 mask = names->mask;
1430 if ((flags & mask) != mask)
1431 continue;
1432
1433 buf = string(buf, end, names->name, strspec);
1434
1435 flags &= ~mask;
1436 if (flags) {
1437 if (buf < end)
1438 *buf = '|';
1439 buf++;
1440 }
1441 }
1442
1443 if (flags)
1444 buf = number(buf, end, flags, numspec);
1445
1446 return buf;
1447}
1448
1449static noinline_for_stack
1450char *flags_string(char *buf, char *end, void *flags_ptr, const char *fmt)
1451{
1452 unsigned long flags;
1453 const struct trace_print_flags *names;
1454
1455 switch (fmt[1]) {
1456 case 'p':
1457 flags = *(unsigned long *)flags_ptr;
1458 /* Remove zone id */
1459 flags &= (1UL << NR_PAGEFLAGS) - 1;
1460 names = pageflag_names;
1461 break;
1462 case 'v':
1463 flags = *(unsigned long *)flags_ptr;
1464 names = vmaflag_names;
1465 break;
1466 case 'g':
1467 flags = *(gfp_t *)flags_ptr;
1468 names = gfpflag_names;
1469 break;
1470 default:
1471 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt[1]);
1472 return buf;
1473 }
1474
1475 return format_flags(buf, end, flags, names);
1476}
1477
1387int kptr_restrict __read_mostly; 1478int kptr_restrict __read_mostly;
1388 1479
1389/* 1480/*
@@ -1472,6 +1563,11 @@ int kptr_restrict __read_mostly;
1472 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address 1563 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1473 * (legacy clock framework) of the clock 1564 * (legacy clock framework) of the clock
1474 * - 'Cr' For a clock, it prints the current rate of the clock 1565 * - 'Cr' For a clock, it prints the current rate of the clock
1566 * - 'G' For flags to be printed as a collection of symbolic strings that would
1567 * construct the specific value. Supported flags given by option:
1568 * p page flags (see struct page) given as pointer to unsigned long
1569 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1570 * v vma flags (VM_*) given as pointer to unsigned long
1475 * 1571 *
1476 * ** Please update also Documentation/printk-formats.txt when making changes ** 1572 * ** Please update also Documentation/printk-formats.txt when making changes **
1477 * 1573 *
@@ -1567,22 +1663,23 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1567 return buf; 1663 return buf;
1568 } 1664 }
1569 case 'K': 1665 case 'K':
1570 /*
1571 * %pK cannot be used in IRQ context because its test
1572 * for CAP_SYSLOG would be meaningless.
1573 */
1574 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1575 in_nmi())) {
1576 if (spec.field_width == -1)
1577 spec.field_width = default_width;
1578 return string(buf, end, "pK-error", spec);
1579 }
1580
1581 switch (kptr_restrict) { 1666 switch (kptr_restrict) {
1582 case 0: 1667 case 0:
1583 /* Always print %pK values */ 1668 /* Always print %pK values */
1584 break; 1669 break;
1585 case 1: { 1670 case 1: {
1671 const struct cred *cred;
1672
1673 /*
1674 * kptr_restrict==1 cannot be used in IRQ context
1675 * because its test for CAP_SYSLOG would be meaningless.
1676 */
1677 if (in_irq() || in_serving_softirq() || in_nmi()) {
1678 if (spec.field_width == -1)
1679 spec.field_width = default_width;
1680 return string(buf, end, "pK-error", spec);
1681 }
1682
1586 /* 1683 /*
1587 * Only print the real pointer value if the current 1684 * Only print the real pointer value if the current
1588 * process has CAP_SYSLOG and is running with the 1685 * process has CAP_SYSLOG and is running with the
@@ -1592,8 +1689,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1592 * leak pointer values if a binary opens a file using 1689 * leak pointer values if a binary opens a file using
1593 * %pK and then elevates privileges before reading it. 1690 * %pK and then elevates privileges before reading it.
1594 */ 1691 */
1595 const struct cred *cred = current_cred(); 1692 cred = current_cred();
1596
1597 if (!has_capability_noaudit(current, CAP_SYSLOG) || 1693 if (!has_capability_noaudit(current, CAP_SYSLOG) ||
1598 !uid_eq(cred->euid, cred->uid) || 1694 !uid_eq(cred->euid, cred->uid) ||
1599 !gid_eq(cred->egid, cred->gid)) 1695 !gid_eq(cred->egid, cred->gid))
@@ -1609,13 +1705,9 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1609 break; 1705 break;
1610 1706
1611 case 'N': 1707 case 'N':
1612 switch (fmt[1]) { 1708 return netdev_bits(buf, end, ptr, fmt);
1613 case 'F':
1614 return netdev_feature_string(buf, end, ptr, spec);
1615 }
1616 break;
1617 case 'a': 1709 case 'a':
1618 return address_val(buf, end, ptr, spec, fmt); 1710 return address_val(buf, end, ptr, fmt);
1619 case 'd': 1711 case 'd':
1620 return dentry_name(buf, end, ptr, spec, fmt); 1712 return dentry_name(buf, end, ptr, spec, fmt);
1621 case 'C': 1713 case 'C':
@@ -1629,6 +1721,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1629 return bdev_name(buf, end, ptr, spec, fmt); 1721 return bdev_name(buf, end, ptr, spec, fmt);
1630#endif 1722#endif
1631 1723
1724 case 'G':
1725 return flags_string(buf, end, ptr, fmt);
1632 } 1726 }
1633 spec.flags |= SMALL; 1727 spec.flags |= SMALL;
1634 if (spec.field_width == -1) { 1728 if (spec.field_width == -1) {
@@ -1664,6 +1758,7 @@ static noinline_for_stack
1664int format_decode(const char *fmt, struct printf_spec *spec) 1758int format_decode(const char *fmt, struct printf_spec *spec)
1665{ 1759{
1666 const char *start = fmt; 1760 const char *start = fmt;
1761 char qualifier;
1667 1762
1668 /* we finished early by reading the field width */ 1763 /* we finished early by reading the field width */
1669 if (spec->type == FORMAT_TYPE_WIDTH) { 1764 if (spec->type == FORMAT_TYPE_WIDTH) {
@@ -1746,16 +1841,16 @@ precision:
1746 1841
1747qualifier: 1842qualifier:
1748 /* get the conversion qualifier */ 1843 /* get the conversion qualifier */
1749 spec->qualifier = -1; 1844 qualifier = 0;
1750 if (*fmt == 'h' || _tolower(*fmt) == 'l' || 1845 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1751 _tolower(*fmt) == 'z' || *fmt == 't') { 1846 _tolower(*fmt) == 'z' || *fmt == 't') {
1752 spec->qualifier = *fmt++; 1847 qualifier = *fmt++;
1753 if (unlikely(spec->qualifier == *fmt)) { 1848 if (unlikely(qualifier == *fmt)) {
1754 if (spec->qualifier == 'l') { 1849 if (qualifier == 'l') {
1755 spec->qualifier = 'L'; 1850 qualifier = 'L';
1756 ++fmt; 1851 ++fmt;
1757 } else if (spec->qualifier == 'h') { 1852 } else if (qualifier == 'h') {
1758 spec->qualifier = 'H'; 1853 qualifier = 'H';
1759 ++fmt; 1854 ++fmt;
1760 } 1855 }
1761 } 1856 }
@@ -1812,19 +1907,19 @@ qualifier:
1812 return fmt - start; 1907 return fmt - start;
1813 } 1908 }
1814 1909
1815 if (spec->qualifier == 'L') 1910 if (qualifier == 'L')
1816 spec->type = FORMAT_TYPE_LONG_LONG; 1911 spec->type = FORMAT_TYPE_LONG_LONG;
1817 else if (spec->qualifier == 'l') { 1912 else if (qualifier == 'l') {
1818 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG); 1913 BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
1819 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN); 1914 spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
1820 } else if (_tolower(spec->qualifier) == 'z') { 1915 } else if (_tolower(qualifier) == 'z') {
1821 spec->type = FORMAT_TYPE_SIZE_T; 1916 spec->type = FORMAT_TYPE_SIZE_T;
1822 } else if (spec->qualifier == 't') { 1917 } else if (qualifier == 't') {
1823 spec->type = FORMAT_TYPE_PTRDIFF; 1918 spec->type = FORMAT_TYPE_PTRDIFF;
1824 } else if (spec->qualifier == 'H') { 1919 } else if (qualifier == 'H') {
1825 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE); 1920 BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
1826 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN); 1921 spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
1827 } else if (spec->qualifier == 'h') { 1922 } else if (qualifier == 'h') {
1828 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT); 1923 BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
1829 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN); 1924 spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
1830 } else { 1925 } else {
@@ -1835,6 +1930,24 @@ qualifier:
1835 return ++fmt - start; 1930 return ++fmt - start;
1836} 1931}
1837 1932
1933static void
1934set_field_width(struct printf_spec *spec, int width)
1935{
1936 spec->field_width = width;
1937 if (WARN_ONCE(spec->field_width != width, "field width %d too large", width)) {
1938 spec->field_width = clamp(width, -FIELD_WIDTH_MAX, FIELD_WIDTH_MAX);
1939 }
1940}
1941
1942static void
1943set_precision(struct printf_spec *spec, int prec)
1944{
1945 spec->precision = prec;
1946 if (WARN_ONCE(spec->precision != prec, "precision %d too large", prec)) {
1947 spec->precision = clamp(prec, 0, PRECISION_MAX);
1948 }
1949}
1950
1838/** 1951/**
1839 * vsnprintf - Format a string and place it in a buffer 1952 * vsnprintf - Format a string and place it in a buffer
1840 * @buf: The buffer to place the result into 1953 * @buf: The buffer to place the result into
@@ -1902,11 +2015,11 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1902 } 2015 }
1903 2016
1904 case FORMAT_TYPE_WIDTH: 2017 case FORMAT_TYPE_WIDTH:
1905 spec.field_width = va_arg(args, int); 2018 set_field_width(&spec, va_arg(args, int));
1906 break; 2019 break;
1907 2020
1908 case FORMAT_TYPE_PRECISION: 2021 case FORMAT_TYPE_PRECISION:
1909 spec.precision = va_arg(args, int); 2022 set_precision(&spec, va_arg(args, int));
1910 break; 2023 break;
1911 2024
1912 case FORMAT_TYPE_CHAR: { 2025 case FORMAT_TYPE_CHAR: {
@@ -2346,11 +2459,11 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2346 } 2459 }
2347 2460
2348 case FORMAT_TYPE_WIDTH: 2461 case FORMAT_TYPE_WIDTH:
2349 spec.field_width = get_arg(int); 2462 set_field_width(&spec, get_arg(int));
2350 break; 2463 break;
2351 2464
2352 case FORMAT_TYPE_PRECISION: 2465 case FORMAT_TYPE_PRECISION:
2353 spec.precision = get_arg(int); 2466 set_precision(&spec, get_arg(int));
2354 break; 2467 break;
2355 2468
2356 case FORMAT_TYPE_CHAR: { 2469 case FORMAT_TYPE_CHAR: {