aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:28 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2015-02-12 21:54:28 -0500
commit818099574b04c5301eacbbcd441022b353a65466 (patch)
tree77b3645b375105cb0389df2b4ea5ffa90329f7f8 /lib
parent802ea9d8645d33d24b7b4cd4537c14f3e698bde0 (diff)
parent6016daed58ee482a2f7684e93342e89139cf4419 (diff)
Merge branch 'akpm' (patches from Andrew)
Merge third set of updates from Andrew Morton: - the rest of MM [ This includes getting rid of the numa hinting bits, in favor of just generic protnone logic. Yay. - Linus ] - core kernel - procfs - some of lib/ (lots of lib/ material this time) * emailed patches from Andrew Morton <akpm@linux-foundation.org>: (104 commits) lib/lcm.c: replace include lib/percpu_ida.c: remove redundant includes lib/strncpy_from_user.c: replace module.h include lib/stmp_device.c: replace module.h include lib/sort.c: move include inside #if 0 lib/show_mem.c: remove redundant include lib/radix-tree.c: change to simpler include lib/plist.c: remove redundant include lib/nlattr.c: remove redundant include lib/kobject_uevent.c: remove redundant include lib/llist.c: remove redundant include lib/md5.c: simplify include lib/list_sort.c: rearrange includes lib/genalloc.c: remove redundant include lib/idr.c: remove redundant include lib/halfmd4.c: simplify includes lib/dynamic_queue_limits.c: simplify includes lib/sort.c: use simpler includes lib/interval_tree.c: simplify includes hexdump: make it return number of bytes placed in buffer ...
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug3
-rw-r--r--lib/Makefile4
-rw-r--r--lib/bitmap.c80
-rw-r--r--lib/dynamic_queue_limits.c4
-rw-r--r--lib/genalloc.c3
-rw-r--r--lib/halfmd4.c2
-rw-r--r--lib/hexdump.c105
-rw-r--r--lib/idr.c1
-rw-r--r--lib/interval_tree.c4
-rw-r--r--lib/kobject_uevent.c1
-rw-r--r--lib/lcm.c2
-rw-r--r--lib/list_sort.c7
-rw-r--r--lib/llist.c1
-rw-r--r--lib/md5.c2
-rw-r--r--lib/nlattr.c1
-rw-r--r--lib/percpu_ida.c3
-rw-r--r--lib/plist.c1
-rw-r--r--lib/radix-tree.c2
-rw-r--r--lib/show_mem.c1
-rw-r--r--lib/sort.c6
-rw-r--r--lib/stmp_device.c3
-rw-r--r--lib/string.c8
-rw-r--r--lib/string_helpers.c26
-rw-r--r--lib/strncpy_from_user.c3
-rw-r--r--lib/test-hexdump.c180
-rw-r--r--lib/vsprintf.c12
26 files changed, 320 insertions, 145 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index e5ea3ab856bf..79a9bb67aeaf 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -1580,6 +1580,9 @@ config ASYNC_RAID6_TEST
1580 1580
1581 If unsure, say N. 1581 If unsure, say N.
1582 1582
1583config TEST_HEXDUMP
1584 tristate "Test functions located in the hexdump module at runtime"
1585
1583config TEST_STRING_HELPERS 1586config TEST_STRING_HELPERS
1584 tristate "Test functions located in the string_helpers module at runtime" 1587 tristate "Test functions located in the string_helpers module at runtime"
1585 1588
diff --git a/lib/Makefile b/lib/Makefile
index 25c061f77df7..e456defd1021 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -23,12 +23,14 @@ lib-y += kobject.o klist.o
23obj-y += lockref.o 23obj-y += lockref.o
24 24
25obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ 25obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ 26 bust_spinlocks.o kasprintf.o bitmap.o scatterlist.o \
27 gcd.o lcm.o list_sort.o uuid.o flex_array.o clz_ctz.o \ 27 gcd.o lcm.o list_sort.o uuid.o flex_array.o clz_ctz.o \
28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \ 28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \
29 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o 29 percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o
30obj-y += string_helpers.o 30obj-y += string_helpers.o
31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
32obj-y += hexdump.o
33obj-$(CONFIG_TEST_HEXDUMP) += test-hexdump.o
32obj-y += kstrtox.o 34obj-y += kstrtox.o
33obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o 35obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
34obj-$(CONFIG_TEST_LKM) += test_module.o 36obj-$(CONFIG_TEST_LKM) += test_module.o
diff --git a/lib/bitmap.c b/lib/bitmap.c
index 324ea9eab8c1..ad161a6c82db 100644
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -744,10 +744,10 @@ EXPORT_SYMBOL(bitmap_parselist_user);
744/** 744/**
745 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap 745 * bitmap_pos_to_ord - find ordinal of set bit at given position in bitmap
746 * @buf: pointer to a bitmap 746 * @buf: pointer to a bitmap
747 * @pos: a bit position in @buf (0 <= @pos < @bits) 747 * @pos: a bit position in @buf (0 <= @pos < @nbits)
748 * @bits: number of valid bit positions in @buf 748 * @nbits: number of valid bit positions in @buf
749 * 749 *
750 * Map the bit at position @pos in @buf (of length @bits) to the 750 * Map the bit at position @pos in @buf (of length @nbits) to the
751 * ordinal of which set bit it is. If it is not set or if @pos 751 * ordinal of which set bit it is. If it is not set or if @pos
752 * is not a valid bit position, map to -1. 752 * is not a valid bit position, map to -1.
753 * 753 *
@@ -759,56 +759,40 @@ EXPORT_SYMBOL(bitmap_parselist_user);
759 * 759 *
760 * The bit positions 0 through @bits are valid positions in @buf. 760 * The bit positions 0 through @bits are valid positions in @buf.
761 */ 761 */
762static int bitmap_pos_to_ord(const unsigned long *buf, int pos, int bits) 762static int bitmap_pos_to_ord(const unsigned long *buf, unsigned int pos, unsigned int nbits)
763{ 763{
764 int i, ord; 764 if (pos >= nbits || !test_bit(pos, buf))
765
766 if (pos < 0 || pos >= bits || !test_bit(pos, buf))
767 return -1; 765 return -1;
768 766
769 i = find_first_bit(buf, bits); 767 return __bitmap_weight(buf, pos);
770 ord = 0;
771 while (i < pos) {
772 i = find_next_bit(buf, bits, i + 1);
773 ord++;
774 }
775 BUG_ON(i != pos);
776
777 return ord;
778} 768}
779 769
780/** 770/**
781 * bitmap_ord_to_pos - find position of n-th set bit in bitmap 771 * bitmap_ord_to_pos - find position of n-th set bit in bitmap
782 * @buf: pointer to bitmap 772 * @buf: pointer to bitmap
783 * @ord: ordinal bit position (n-th set bit, n >= 0) 773 * @ord: ordinal bit position (n-th set bit, n >= 0)
784 * @bits: number of valid bit positions in @buf 774 * @nbits: number of valid bit positions in @buf
785 * 775 *
786 * Map the ordinal offset of bit @ord in @buf to its position in @buf. 776 * Map the ordinal offset of bit @ord in @buf to its position in @buf.
787 * Value of @ord should be in range 0 <= @ord < weight(buf), else 777 * Value of @ord should be in range 0 <= @ord < weight(buf). If @ord
788 * results are undefined. 778 * >= weight(buf), returns @nbits.
789 * 779 *
790 * If for example, just bits 4 through 7 are set in @buf, then @ord 780 * If for example, just bits 4 through 7 are set in @buf, then @ord
791 * values 0 through 3 will get mapped to 4 through 7, respectively, 781 * values 0 through 3 will get mapped to 4 through 7, respectively,
792 * and all other @ord values return undefined values. When @ord value 3 782 * and all other @ord values returns @nbits. When @ord value 3
793 * gets mapped to (returns) @pos value 7 in this example, that means 783 * gets mapped to (returns) @pos value 7 in this example, that means
794 * that the 3rd set bit (starting with 0th) is at position 7 in @buf. 784 * that the 3rd set bit (starting with 0th) is at position 7 in @buf.
795 * 785 *
796 * The bit positions 0 through @bits are valid positions in @buf. 786 * The bit positions 0 through @nbits-1 are valid positions in @buf.
797 */ 787 */
798int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits) 788unsigned int bitmap_ord_to_pos(const unsigned long *buf, unsigned int ord, unsigned int nbits)
799{ 789{
800 int pos = 0; 790 unsigned int pos;
801
802 if (ord >= 0 && ord < bits) {
803 int i;
804 791
805 for (i = find_first_bit(buf, bits); 792 for (pos = find_first_bit(buf, nbits);
806 i < bits && ord > 0; 793 pos < nbits && ord;
807 i = find_next_bit(buf, bits, i + 1)) 794 pos = find_next_bit(buf, nbits, pos + 1))
808 ord--; 795 ord--;
809 if (i < bits && ord == 0)
810 pos = i;
811 }
812 796
813 return pos; 797 return pos;
814} 798}
@@ -819,7 +803,7 @@ int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
819 * @src: subset to be remapped 803 * @src: subset to be remapped
820 * @old: defines domain of map 804 * @old: defines domain of map
821 * @new: defines range of map 805 * @new: defines range of map
822 * @bits: number of bits in each of these bitmaps 806 * @nbits: number of bits in each of these bitmaps
823 * 807 *
824 * Let @old and @new define a mapping of bit positions, such that 808 * Let @old and @new define a mapping of bit positions, such that
825 * whatever position is held by the n-th set bit in @old is mapped 809 * whatever position is held by the n-th set bit in @old is mapped
@@ -847,22 +831,22 @@ int bitmap_ord_to_pos(const unsigned long *buf, int ord, int bits)
847 */ 831 */
848void bitmap_remap(unsigned long *dst, const unsigned long *src, 832void bitmap_remap(unsigned long *dst, const unsigned long *src,
849 const unsigned long *old, const unsigned long *new, 833 const unsigned long *old, const unsigned long *new,
850 int bits) 834 unsigned int nbits)
851{ 835{
852 int oldbit, w; 836 unsigned int oldbit, w;
853 837
854 if (dst == src) /* following doesn't handle inplace remaps */ 838 if (dst == src) /* following doesn't handle inplace remaps */
855 return; 839 return;
856 bitmap_zero(dst, bits); 840 bitmap_zero(dst, nbits);
857 841
858 w = bitmap_weight(new, bits); 842 w = bitmap_weight(new, nbits);
859 for_each_set_bit(oldbit, src, bits) { 843 for_each_set_bit(oldbit, src, nbits) {
860 int n = bitmap_pos_to_ord(old, oldbit, bits); 844 int n = bitmap_pos_to_ord(old, oldbit, nbits);
861 845
862 if (n < 0 || w == 0) 846 if (n < 0 || w == 0)
863 set_bit(oldbit, dst); /* identity map */ 847 set_bit(oldbit, dst); /* identity map */
864 else 848 else
865 set_bit(bitmap_ord_to_pos(new, n % w, bits), dst); 849 set_bit(bitmap_ord_to_pos(new, n % w, nbits), dst);
866 } 850 }
867} 851}
868EXPORT_SYMBOL(bitmap_remap); 852EXPORT_SYMBOL(bitmap_remap);
@@ -1006,9 +990,9 @@ EXPORT_SYMBOL(bitmap_bitremap);
1006 * All bits in @dst not set by the above rule are cleared. 990 * All bits in @dst not set by the above rule are cleared.
1007 */ 991 */
1008void bitmap_onto(unsigned long *dst, const unsigned long *orig, 992void bitmap_onto(unsigned long *dst, const unsigned long *orig,
1009 const unsigned long *relmap, int bits) 993 const unsigned long *relmap, unsigned int bits)
1010{ 994{
1011 int n, m; /* same meaning as in above comment */ 995 unsigned int n, m; /* same meaning as in above comment */
1012 996
1013 if (dst == orig) /* following doesn't handle inplace mappings */ 997 if (dst == orig) /* following doesn't handle inplace mappings */
1014 return; 998 return;
@@ -1039,22 +1023,22 @@ EXPORT_SYMBOL(bitmap_onto);
1039 * @dst: resulting smaller bitmap 1023 * @dst: resulting smaller bitmap
1040 * @orig: original larger bitmap 1024 * @orig: original larger bitmap
1041 * @sz: specified size 1025 * @sz: specified size
1042 * @bits: number of bits in each of these bitmaps 1026 * @nbits: number of bits in each of these bitmaps
1043 * 1027 *
1044 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst. 1028 * For each bit oldbit in @orig, set bit oldbit mod @sz in @dst.
1045 * Clear all other bits in @dst. See further the comment and 1029 * Clear all other bits in @dst. See further the comment and
1046 * Example [2] for bitmap_onto() for why and how to use this. 1030 * Example [2] for bitmap_onto() for why and how to use this.
1047 */ 1031 */
1048void bitmap_fold(unsigned long *dst, const unsigned long *orig, 1032void bitmap_fold(unsigned long *dst, const unsigned long *orig,
1049 int sz, int bits) 1033 unsigned int sz, unsigned int nbits)
1050{ 1034{
1051 int oldbit; 1035 unsigned int oldbit;
1052 1036
1053 if (dst == orig) /* following doesn't handle inplace mappings */ 1037 if (dst == orig) /* following doesn't handle inplace mappings */
1054 return; 1038 return;
1055 bitmap_zero(dst, bits); 1039 bitmap_zero(dst, nbits);
1056 1040
1057 for_each_set_bit(oldbit, orig, bits) 1041 for_each_set_bit(oldbit, orig, nbits)
1058 set_bit(oldbit % sz, dst); 1042 set_bit(oldbit % sz, dst);
1059} 1043}
1060EXPORT_SYMBOL(bitmap_fold); 1044EXPORT_SYMBOL(bitmap_fold);
diff --git a/lib/dynamic_queue_limits.c b/lib/dynamic_queue_limits.c
index 0777c5a45fa0..f346715e2255 100644
--- a/lib/dynamic_queue_limits.c
+++ b/lib/dynamic_queue_limits.c
@@ -3,12 +3,12 @@
3 * 3 *
4 * Copyright (c) 2011, Tom Herbert <therbert@google.com> 4 * Copyright (c) 2011, Tom Herbert <therbert@google.com>
5 */ 5 */
6#include <linux/module.h>
7#include <linux/types.h> 6#include <linux/types.h>
8#include <linux/ctype.h>
9#include <linux/kernel.h> 7#include <linux/kernel.h>
10#include <linux/jiffies.h> 8#include <linux/jiffies.h>
11#include <linux/dynamic_queue_limits.h> 9#include <linux/dynamic_queue_limits.h>
10#include <linux/compiler.h>
11#include <linux/export.h>
12 12
13#define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0) 13#define POSDIFF(A, B) ((int)((A) - (B)) > 0 ? (A) - (B) : 0)
14#define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0) 14#define AFTER_EQ(A, B) ((int)((A) - (B)) >= 0)
diff --git a/lib/genalloc.c b/lib/genalloc.c
index 2e65d206b01c..0fe1cbe87700 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -34,7 +34,6 @@
34#include <linux/rculist.h> 34#include <linux/rculist.h>
35#include <linux/interrupt.h> 35#include <linux/interrupt.h>
36#include <linux/genalloc.h> 36#include <linux/genalloc.h>
37#include <linux/of_address.h>
38#include <linux/of_device.h> 37#include <linux/of_device.h>
39 38
40static inline size_t chunk_size(const struct gen_pool_chunk *chunk) 39static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
@@ -415,7 +414,7 @@ bool addr_in_gen_pool(struct gen_pool *pool, unsigned long start,
415 size_t size) 414 size_t size)
416{ 415{
417 bool found = false; 416 bool found = false;
418 unsigned long end = start + size; 417 unsigned long end = start + size - 1;
419 struct gen_pool_chunk *chunk; 418 struct gen_pool_chunk *chunk;
420 419
421 rcu_read_lock(); 420 rcu_read_lock();
diff --git a/lib/halfmd4.c b/lib/halfmd4.c
index 66d0ee8b7776..a8fe6274a13c 100644
--- a/lib/halfmd4.c
+++ b/lib/halfmd4.c
@@ -1,4 +1,4 @@
1#include <linux/kernel.h> 1#include <linux/compiler.h>
2#include <linux/export.h> 2#include <linux/export.h>
3#include <linux/cryptohash.h> 3#include <linux/cryptohash.h>
4 4
diff --git a/lib/hexdump.c b/lib/hexdump.c
index 270773b91923..7ea09699855d 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -97,63 +97,79 @@ EXPORT_SYMBOL(bin2hex);
97 * 97 *
98 * example output buffer: 98 * example output buffer:
99 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO 99 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
100 *
101 * Return:
102 * The amount of bytes placed in the buffer without terminating NUL. If the
103 * output was truncated, then the return value is the number of bytes
104 * (excluding the terminating NUL) which would have been written to the final
105 * string if enough space had been available.
100 */ 106 */
101void hex_dump_to_buffer(const void *buf, size_t len, int rowsize, 107int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize,
102 int groupsize, char *linebuf, size_t linebuflen, 108 char *linebuf, size_t linebuflen, bool ascii)
103 bool ascii)
104{ 109{
105 const u8 *ptr = buf; 110 const u8 *ptr = buf;
111 int ngroups;
106 u8 ch; 112 u8 ch;
107 int j, lx = 0; 113 int j, lx = 0;
108 int ascii_column; 114 int ascii_column;
115 int ret;
109 116
110 if (rowsize != 16 && rowsize != 32) 117 if (rowsize != 16 && rowsize != 32)
111 rowsize = 16; 118 rowsize = 16;
112 119
113 if (!len)
114 goto nil;
115 if (len > rowsize) /* limit to one line at a time */ 120 if (len > rowsize) /* limit to one line at a time */
116 len = rowsize; 121 len = rowsize;
122 if (!is_power_of_2(groupsize) || groupsize > 8)
123 groupsize = 1;
117 if ((len % groupsize) != 0) /* no mixed size output */ 124 if ((len % groupsize) != 0) /* no mixed size output */
118 groupsize = 1; 125 groupsize = 1;
119 126
120 switch (groupsize) { 127 ngroups = len / groupsize;
121 case 8: { 128 ascii_column = rowsize * 2 + rowsize / groupsize + 1;
122 const u64 *ptr8 = buf;
123 int ngroups = len / groupsize;
124 129
125 for (j = 0; j < ngroups; j++) 130 if (!linebuflen)
126 lx += scnprintf(linebuf + lx, linebuflen - lx, 131 goto overflow1;
127 "%s%16.16llx", j ? " " : "",
128 (unsigned long long)*(ptr8 + j));
129 ascii_column = 17 * ngroups + 2;
130 break;
131 }
132 132
133 case 4: { 133 if (!len)
134 const u32 *ptr4 = buf; 134 goto nil;
135 int ngroups = len / groupsize;
136 135
137 for (j = 0; j < ngroups; j++) 136 if (groupsize == 8) {
138 lx += scnprintf(linebuf + lx, linebuflen - lx, 137 const u64 *ptr8 = buf;
139 "%s%8.8x", j ? " " : "", *(ptr4 + j));
140 ascii_column = 9 * ngroups + 2;
141 break;
142 }
143 138
144 case 2: { 139 for (j = 0; j < ngroups; j++) {
145 const u16 *ptr2 = buf; 140 ret = snprintf(linebuf + lx, linebuflen - lx,
146 int ngroups = len / groupsize; 141 "%s%16.16llx", j ? " " : "",
142 (unsigned long long)*(ptr8 + j));
143 if (ret >= linebuflen - lx)
144 goto overflow1;
145 lx += ret;
146 }
147 } else if (groupsize == 4) {
148 const u32 *ptr4 = buf;
147 149
148 for (j = 0; j < ngroups; j++) 150 for (j = 0; j < ngroups; j++) {
149 lx += scnprintf(linebuf + lx, linebuflen - lx, 151 ret = snprintf(linebuf + lx, linebuflen - lx,
150 "%s%4.4x", j ? " " : "", *(ptr2 + j)); 152 "%s%8.8x", j ? " " : "",
151 ascii_column = 5 * ngroups + 2; 153 *(ptr4 + j));
152 break; 154 if (ret >= linebuflen - lx)
153 } 155 goto overflow1;
156 lx += ret;
157 }
158 } else if (groupsize == 2) {
159 const u16 *ptr2 = buf;
154 160
155 default: 161 for (j = 0; j < ngroups; j++) {
156 for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) { 162 ret = snprintf(linebuf + lx, linebuflen - lx,
163 "%s%4.4x", j ? " " : "",
164 *(ptr2 + j));
165 if (ret >= linebuflen - lx)
166 goto overflow1;
167 lx += ret;
168 }
169 } else {
170 for (j = 0; j < len; j++) {
171 if (linebuflen < lx + 3)
172 goto overflow2;
157 ch = ptr[j]; 173 ch = ptr[j];
158 linebuf[lx++] = hex_asc_hi(ch); 174 linebuf[lx++] = hex_asc_hi(ch);
159 linebuf[lx++] = hex_asc_lo(ch); 175 linebuf[lx++] = hex_asc_lo(ch);
@@ -161,21 +177,28 @@ void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
161 } 177 }
162 if (j) 178 if (j)
163 lx--; 179 lx--;
164
165 ascii_column = 3 * rowsize + 2;
166 break;
167 } 180 }
168 if (!ascii) 181 if (!ascii)
169 goto nil; 182 goto nil;
170 183
171 while (lx < (linebuflen - 1) && lx < (ascii_column - 1)) 184 while (lx < ascii_column) {
185 if (linebuflen < lx + 2)
186 goto overflow2;
172 linebuf[lx++] = ' '; 187 linebuf[lx++] = ' ';
173 for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) { 188 }
189 for (j = 0; j < len; j++) {
190 if (linebuflen < lx + 2)
191 goto overflow2;
174 ch = ptr[j]; 192 ch = ptr[j];
175 linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.'; 193 linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
176 } 194 }
177nil: 195nil:
196 linebuf[lx] = '\0';
197 return lx;
198overflow2:
178 linebuf[lx++] = '\0'; 199 linebuf[lx++] = '\0';
200overflow1:
201 return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
179} 202}
180EXPORT_SYMBOL(hex_dump_to_buffer); 203EXPORT_SYMBOL(hex_dump_to_buffer);
181 204
diff --git a/lib/idr.c b/lib/idr.c
index e654aebd5f80..5335c43adf46 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -30,7 +30,6 @@
30#include <linux/idr.h> 30#include <linux/idr.h>
31#include <linux/spinlock.h> 31#include <linux/spinlock.h>
32#include <linux/percpu.h> 32#include <linux/percpu.h>
33#include <linux/hardirq.h>
34 33
35#define MAX_IDR_SHIFT (sizeof(int) * 8 - 1) 34#define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
36#define MAX_IDR_BIT (1U << MAX_IDR_SHIFT) 35#define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
diff --git a/lib/interval_tree.c b/lib/interval_tree.c
index f367f9ad544c..c85f6600a5f8 100644
--- a/lib/interval_tree.c
+++ b/lib/interval_tree.c
@@ -1,7 +1,7 @@
1#include <linux/init.h>
2#include <linux/interval_tree.h> 1#include <linux/interval_tree.h>
3#include <linux/interval_tree_generic.h> 2#include <linux/interval_tree_generic.h>
4#include <linux/module.h> 3#include <linux/compiler.h>
4#include <linux/export.h>
5 5
6#define START(node) ((node)->start) 6#define START(node) ((node)->start)
7#define LAST(node) ((node)->last) 7#define LAST(node) ((node)->last)
diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
index 9ebf9e20de53..f6c2c1e7779c 100644
--- a/lib/kobject_uevent.c
+++ b/lib/kobject_uevent.c
@@ -20,7 +20,6 @@
20#include <linux/export.h> 20#include <linux/export.h>
21#include <linux/kmod.h> 21#include <linux/kmod.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/user_namespace.h>
24#include <linux/socket.h> 23#include <linux/socket.h>
25#include <linux/skbuff.h> 24#include <linux/skbuff.h>
26#include <linux/netlink.h> 25#include <linux/netlink.h>
diff --git a/lib/lcm.c b/lib/lcm.c
index 51cc6b13cd52..e97dbd51e756 100644
--- a/lib/lcm.c
+++ b/lib/lcm.c
@@ -1,4 +1,4 @@
1#include <linux/kernel.h> 1#include <linux/compiler.h>
2#include <linux/gcd.h> 2#include <linux/gcd.h>
3#include <linux/export.h> 3#include <linux/export.h>
4#include <linux/lcm.h> 4#include <linux/lcm.h>
diff --git a/lib/list_sort.c b/lib/list_sort.c
index 12bcba1c8612..b29015102698 100644
--- a/lib/list_sort.c
+++ b/lib/list_sort.c
@@ -2,9 +2,11 @@
2#define pr_fmt(fmt) "list_sort_test: " fmt 2#define pr_fmt(fmt) "list_sort_test: " fmt
3 3
4#include <linux/kernel.h> 4#include <linux/kernel.h>
5#include <linux/module.h> 5#include <linux/bug.h>
6#include <linux/compiler.h>
7#include <linux/export.h>
8#include <linux/string.h>
6#include <linux/list_sort.h> 9#include <linux/list_sort.h>
7#include <linux/slab.h>
8#include <linux/list.h> 10#include <linux/list.h>
9 11
10#define MAX_LIST_LENGTH_BITS 20 12#define MAX_LIST_LENGTH_BITS 20
@@ -146,6 +148,7 @@ EXPORT_SYMBOL(list_sort);
146 148
147#ifdef CONFIG_TEST_LIST_SORT 149#ifdef CONFIG_TEST_LIST_SORT
148 150
151#include <linux/slab.h>
149#include <linux/random.h> 152#include <linux/random.h>
150 153
151/* 154/*
diff --git a/lib/llist.c b/lib/llist.c
index f76196d07409..0b0e9779d675 100644
--- a/lib/llist.c
+++ b/lib/llist.c
@@ -24,7 +24,6 @@
24 */ 24 */
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26#include <linux/export.h> 26#include <linux/export.h>
27#include <linux/interrupt.h>
28#include <linux/llist.h> 27#include <linux/llist.h>
29 28
30 29
diff --git a/lib/md5.c b/lib/md5.c
index 958a3c15923c..bb0cd01d356d 100644
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -1,4 +1,4 @@
1#include <linux/kernel.h> 1#include <linux/compiler.h>
2#include <linux/export.h> 2#include <linux/export.h>
3#include <linux/cryptohash.h> 3#include <linux/cryptohash.h>
4 4
diff --git a/lib/nlattr.c b/lib/nlattr.c
index 9c3e85ff0a6c..76a1b59523ab 100644
--- a/lib/nlattr.c
+++ b/lib/nlattr.c
@@ -9,7 +9,6 @@
9#include <linux/kernel.h> 9#include <linux/kernel.h>
10#include <linux/errno.h> 10#include <linux/errno.h>
11#include <linux/jiffies.h> 11#include <linux/jiffies.h>
12#include <linux/netdevice.h>
13#include <linux/skbuff.h> 12#include <linux/skbuff.h>
14#include <linux/string.h> 13#include <linux/string.h>
15#include <linux/types.h> 14#include <linux/types.h>
diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
index 93d145e5539c..f75715131f20 100644
--- a/lib/percpu_ida.c
+++ b/lib/percpu_ida.c
@@ -19,13 +19,10 @@
19#include <linux/bug.h> 19#include <linux/bug.h>
20#include <linux/err.h> 20#include <linux/err.h>
21#include <linux/export.h> 21#include <linux/export.h>
22#include <linux/hardirq.h>
23#include <linux/idr.h>
24#include <linux/init.h> 22#include <linux/init.h>
25#include <linux/kernel.h> 23#include <linux/kernel.h>
26#include <linux/percpu.h> 24#include <linux/percpu.h>
27#include <linux/sched.h> 25#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/string.h> 26#include <linux/string.h>
30#include <linux/spinlock.h> 27#include <linux/spinlock.h>
31#include <linux/percpu_ida.h> 28#include <linux/percpu_ida.h>
diff --git a/lib/plist.c b/lib/plist.c
index d408e774b746..3a30c53db061 100644
--- a/lib/plist.c
+++ b/lib/plist.c
@@ -25,7 +25,6 @@
25 25
26#include <linux/bug.h> 26#include <linux/bug.h>
27#include <linux/plist.h> 27#include <linux/plist.h>
28#include <linux/spinlock.h>
29 28
30#ifdef CONFIG_DEBUG_PI_LIST 29#ifdef CONFIG_DEBUG_PI_LIST
31 30
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index 3291a8e37490..3d2aa27b845b 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -33,7 +33,7 @@
33#include <linux/string.h> 33#include <linux/string.h>
34#include <linux/bitops.h> 34#include <linux/bitops.h>
35#include <linux/rcupdate.h> 35#include <linux/rcupdate.h>
36#include <linux/hardirq.h> /* in_interrupt() */ 36#include <linux/preempt_mask.h> /* in_interrupt() */
37 37
38 38
39/* 39/*
diff --git a/lib/show_mem.c b/lib/show_mem.c
index 7de89f4a36cf..adc98e1825ba 100644
--- a/lib/show_mem.c
+++ b/lib/show_mem.c
@@ -6,7 +6,6 @@
6 */ 6 */
7 7
8#include <linux/mm.h> 8#include <linux/mm.h>
9#include <linux/nmi.h>
10#include <linux/quicklist.h> 9#include <linux/quicklist.h>
11#include <linux/cma.h> 10#include <linux/cma.h>
12 11
diff --git a/lib/sort.c b/lib/sort.c
index 926d00429ed2..43c9fe73ae2e 100644
--- a/lib/sort.c
+++ b/lib/sort.c
@@ -4,10 +4,9 @@
4 * Jan 23 2005 Matt Mackall <mpm@selenic.com> 4 * Jan 23 2005 Matt Mackall <mpm@selenic.com>
5 */ 5 */
6 6
7#include <linux/kernel.h> 7#include <linux/types.h>
8#include <linux/module.h> 8#include <linux/export.h>
9#include <linux/sort.h> 9#include <linux/sort.h>
10#include <linux/slab.h>
11 10
12static void u32_swap(void *a, void *b, int size) 11static void u32_swap(void *a, void *b, int size)
13{ 12{
@@ -85,6 +84,7 @@ void sort(void *base, size_t num, size_t size,
85EXPORT_SYMBOL(sort); 84EXPORT_SYMBOL(sort);
86 85
87#if 0 86#if 0
87#include <linux/slab.h>
88/* a simple boot-time regression test */ 88/* a simple boot-time regression test */
89 89
90int cmpint(const void *a, const void *b) 90int cmpint(const void *a, const void *b)
diff --git a/lib/stmp_device.c b/lib/stmp_device.c
index 8ac9bcc4289a..a904656f4fd7 100644
--- a/lib/stmp_device.c
+++ b/lib/stmp_device.c
@@ -15,7 +15,8 @@
15#include <linux/io.h> 15#include <linux/io.h>
16#include <linux/errno.h> 16#include <linux/errno.h>
17#include <linux/delay.h> 17#include <linux/delay.h>
18#include <linux/module.h> 18#include <linux/compiler.h>
19#include <linux/export.h>
19#include <linux/stmp_device.h> 20#include <linux/stmp_device.h>
20 21
21#define STMP_MODULE_CLKGATE (1 << 30) 22#define STMP_MODULE_CLKGATE (1 << 30)
diff --git a/lib/string.c b/lib/string.c
index 10063300b830..3206d0178296 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -58,14 +58,6 @@ int strncasecmp(const char *s1, const char *s2, size_t len)
58} 58}
59EXPORT_SYMBOL(strncasecmp); 59EXPORT_SYMBOL(strncasecmp);
60#endif 60#endif
61#ifndef __HAVE_ARCH_STRNICMP
62#undef strnicmp
63int strnicmp(const char *s1, const char *s2, size_t len)
64{
65 return strncasecmp(s1, s2, len);
66}
67EXPORT_SYMBOL(strnicmp);
68#endif
69 61
70#ifndef __HAVE_ARCH_STRCASECMP 62#ifndef __HAVE_ARCH_STRCASECMP
71int strcasecmp(const char *s1, const char *s2) 63int strcasecmp(const char *s1, const char *s2)
diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index 58b78ba57439..8f8c4417f228 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -20,19 +20,18 @@
20 * @len: length of buffer 20 * @len: length of buffer
21 * 21 *
22 * This function returns a string formatted to 3 significant figures 22 * This function returns a string formatted to 3 significant figures
23 * giving the size in the required units. Returns 0 on success or 23 * giving the size in the required units. @buf should have room for
24 * error on failure. @buf is always zero terminated. 24 * at least 9 bytes and will always be zero terminated.
25 * 25 *
26 */ 26 */
27int string_get_size(u64 size, const enum string_size_units units, 27void string_get_size(u64 size, const enum string_size_units units,
28 char *buf, int len) 28 char *buf, int len)
29{ 29{
30 static const char *const units_10[] = { 30 static const char *const units_10[] = {
31 "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL 31 "B", "kB", "MB", "GB", "TB", "PB", "EB"
32 }; 32 };
33 static const char *const units_2[] = { 33 static const char *const units_2[] = {
34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", 34 "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
35 NULL
36 }; 35 };
37 static const char *const *const units_str[] = { 36 static const char *const *const units_str[] = {
38 [STRING_UNITS_10] = units_10, 37 [STRING_UNITS_10] = units_10,
@@ -43,13 +42,13 @@ int string_get_size(u64 size, const enum string_size_units units,
43 [STRING_UNITS_2] = 1024, 42 [STRING_UNITS_2] = 1024,
44 }; 43 };
45 int i, j; 44 int i, j;
46 u64 remainder = 0, sf_cap; 45 u32 remainder = 0, sf_cap;
47 char tmp[8]; 46 char tmp[8];
48 47
49 tmp[0] = '\0'; 48 tmp[0] = '\0';
50 i = 0; 49 i = 0;
51 if (size >= divisor[units]) { 50 if (size >= divisor[units]) {
52 while (size >= divisor[units] && units_str[units][i]) { 51 while (size >= divisor[units]) {
53 remainder = do_div(size, divisor[units]); 52 remainder = do_div(size, divisor[units]);
54 i++; 53 i++;
55 } 54 }
@@ -60,17 +59,14 @@ int string_get_size(u64 size, const enum string_size_units units,
60 59
61 if (j) { 60 if (j) {
62 remainder *= 1000; 61 remainder *= 1000;
63 do_div(remainder, divisor[units]); 62 remainder /= divisor[units];
64 snprintf(tmp, sizeof(tmp), ".%03lld", 63 snprintf(tmp, sizeof(tmp), ".%03u", remainder);
65 (unsigned long long)remainder);
66 tmp[j+1] = '\0'; 64 tmp[j+1] = '\0';
67 } 65 }
68 } 66 }
69 67
70 snprintf(buf, len, "%lld%s %s", (unsigned long long)size, 68 snprintf(buf, len, "%u%s %s", (u32)size,
71 tmp, units_str[units][i]); 69 tmp, units_str[units][i]);
72
73 return 0;
74} 70}
75EXPORT_SYMBOL(string_get_size); 71EXPORT_SYMBOL(string_get_size);
76 72
diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
index bb2b201d6ad0..e0af6ff73d14 100644
--- a/lib/strncpy_from_user.c
+++ b/lib/strncpy_from_user.c
@@ -1,4 +1,5 @@
1#include <linux/module.h> 1#include <linux/compiler.h>
2#include <linux/export.h>
2#include <linux/uaccess.h> 3#include <linux/uaccess.h>
3#include <linux/kernel.h> 4#include <linux/kernel.h>
4#include <linux/errno.h> 5#include <linux/errno.h>
diff --git a/lib/test-hexdump.c b/lib/test-hexdump.c
new file mode 100644
index 000000000000..daf29a390a89
--- /dev/null
+++ b/lib/test-hexdump.c
@@ -0,0 +1,180 @@
1/*
2 * Test cases for lib/hexdump.c module.
3 */
4#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/random.h>
10#include <linux/string.h>
11
12static const unsigned char data_b[] = {
13 '\xbe', '\x32', '\xdb', '\x7b', '\x0a', '\x18', '\x93', '\xb2', /* 00 - 07 */
14 '\x70', '\xba', '\xc4', '\x24', '\x7d', '\x83', '\x34', '\x9b', /* 08 - 0f */
15 '\xa6', '\x9c', '\x31', '\xad', '\x9c', '\x0f', '\xac', '\xe9', /* 10 - 17 */
16 '\x4c', '\xd1', '\x19', '\x99', '\x43', '\xb1', '\xaf', '\x0c', /* 18 - 1f */
17};
18
19static const unsigned char data_a[] = ".2.{....p..$}.4...1.....L...C...";
20
21static const char *test_data_1_le[] __initconst = {
22 "be", "32", "db", "7b", "0a", "18", "93", "b2",
23 "70", "ba", "c4", "24", "7d", "83", "34", "9b",
24 "a6", "9c", "31", "ad", "9c", "0f", "ac", "e9",
25 "4c", "d1", "19", "99", "43", "b1", "af", "0c",
26};
27
28static const char *test_data_2_le[] __initconst = {
29 "32be", "7bdb", "180a", "b293",
30 "ba70", "24c4", "837d", "9b34",
31 "9ca6", "ad31", "0f9c", "e9ac",
32 "d14c", "9919", "b143", "0caf",
33};
34
35static const char *test_data_4_le[] __initconst = {
36 "7bdb32be", "b293180a", "24c4ba70", "9b34837d",
37 "ad319ca6", "e9ac0f9c", "9919d14c", "0cafb143",
38};
39
40static const char *test_data_8_le[] __initconst = {
41 "b293180a7bdb32be", "9b34837d24c4ba70",
42 "e9ac0f9cad319ca6", "0cafb1439919d14c",
43};
44
45static void __init test_hexdump(size_t len, int rowsize, int groupsize,
46 bool ascii)
47{
48 char test[32 * 3 + 2 + 32 + 1];
49 char real[32 * 3 + 2 + 32 + 1];
50 char *p;
51 const char **result;
52 size_t l = len;
53 int gs = groupsize, rs = rowsize;
54 unsigned int i;
55
56 hex_dump_to_buffer(data_b, l, rs, gs, real, sizeof(real), ascii);
57
58 if (rs != 16 && rs != 32)
59 rs = 16;
60
61 if (l > rs)
62 l = rs;
63
64 if (!is_power_of_2(gs) || gs > 8 || (len % gs != 0))
65 gs = 1;
66
67 if (gs == 8)
68 result = test_data_8_le;
69 else if (gs == 4)
70 result = test_data_4_le;
71 else if (gs == 2)
72 result = test_data_2_le;
73 else
74 result = test_data_1_le;
75
76 memset(test, ' ', sizeof(test));
77
78 /* hex dump */
79 p = test;
80 for (i = 0; i < l / gs; i++) {
81 const char *q = *result++;
82 size_t amount = strlen(q);
83
84 strncpy(p, q, amount);
85 p += amount + 1;
86 }
87 if (i)
88 p--;
89
90 /* ASCII part */
91 if (ascii) {
92 p = test + rs * 2 + rs / gs + 1;
93 strncpy(p, data_a, l);
94 p += l;
95 }
96
97 *p = '\0';
98
99 if (strcmp(test, real)) {
100 pr_err("Len: %zu row: %d group: %d\n", len, rowsize, groupsize);
101 pr_err("Result: '%s'\n", real);
102 pr_err("Expect: '%s'\n", test);
103 }
104}
105
106static void __init test_hexdump_set(int rowsize, bool ascii)
107{
108 size_t d = min_t(size_t, sizeof(data_b), rowsize);
109 size_t len = get_random_int() % d + 1;
110
111 test_hexdump(len, rowsize, 4, ascii);
112 test_hexdump(len, rowsize, 2, ascii);
113 test_hexdump(len, rowsize, 8, ascii);
114 test_hexdump(len, rowsize, 1, ascii);
115}
116
117static void __init test_hexdump_overflow(bool ascii)
118{
119 char buf[56];
120 const char *t = test_data_1_le[0];
121 size_t l = get_random_int() % sizeof(buf);
122 bool a;
123 int e, r;
124
125 memset(buf, ' ', sizeof(buf));
126
127 r = hex_dump_to_buffer(data_b, 1, 16, 1, buf, l, ascii);
128
129 if (ascii)
130 e = 50;
131 else
132 e = 2;
133 buf[e + 2] = '\0';
134
135 if (!l) {
136 a = r == e && buf[0] == ' ';
137 } else if (l < 3) {
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 }
149
150 if (!a) {
151 pr_err("Len: %zu rc: %u strlen: %zu\n", l, r, strlen(buf));
152 pr_err("Result: '%s'\n", buf);
153 }
154}
155
156static int __init test_hexdump_init(void)
157{
158 unsigned int i;
159 int rowsize;
160
161 pr_info("Running tests...\n");
162
163 rowsize = (get_random_int() % 2 + 1) * 16;
164 for (i = 0; i < 16; i++)
165 test_hexdump_set(rowsize, false);
166
167 rowsize = (get_random_int() % 2 + 1) * 16;
168 for (i = 0; i < 16; i++)
169 test_hexdump_set(rowsize, true);
170
171 for (i = 0; i < 16; i++)
172 test_hexdump_overflow(false);
173
174 for (i = 0; i < 16; i++)
175 test_hexdump_overflow(true);
176
177 return -EINVAL;
178}
179module_init(test_hexdump_init);
180MODULE_LICENSE("Dual BSD/GPL");
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index ec337f64f52d..602d2081e713 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -114,8 +114,9 @@ int skip_atoi(const char **s)
114{ 114{
115 int i = 0; 115 int i = 0;
116 116
117 while (isdigit(**s)) 117 do {
118 i = i*10 + *((*s)++) - '0'; 118 i = i*10 + *((*s)++) - '0';
119 } while (isdigit(**s));
119 120
120 return i; 121 return i;
121} 122}
@@ -1604,8 +1605,7 @@ qualifier:
1604 1605
1605 case 'p': 1606 case 'p':
1606 spec->type = FORMAT_TYPE_PTR; 1607 spec->type = FORMAT_TYPE_PTR;
1607 return fmt - start; 1608 return ++fmt - start;
1608 /* skip alnum */
1609 1609
1610 case '%': 1610 case '%':
1611 spec->type = FORMAT_TYPE_PERCENT_CHAR; 1611 spec->type = FORMAT_TYPE_PERCENT_CHAR;
@@ -1728,7 +1728,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1728 1728
1729 /* Reject out-of-range values early. Large positive sizes are 1729 /* Reject out-of-range values early. Large positive sizes are
1730 used for unknown buffer sizes. */ 1730 used for unknown buffer sizes. */
1731 if (WARN_ON_ONCE((int) size < 0)) 1731 if (WARN_ON_ONCE(size > INT_MAX))
1732 return 0; 1732 return 0;
1733 1733
1734 str = buf; 1734 str = buf;
@@ -1794,7 +1794,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1794 break; 1794 break;
1795 1795
1796 case FORMAT_TYPE_PTR: 1796 case FORMAT_TYPE_PTR:
1797 str = pointer(fmt+1, str, end, va_arg(args, void *), 1797 str = pointer(fmt, str, end, va_arg(args, void *),
1798 spec); 1798 spec);
1799 while (isalnum(*fmt)) 1799 while (isalnum(*fmt))
1800 fmt++; 1800 fmt++;
@@ -2232,7 +2232,7 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
2232 } 2232 }
2233 2233
2234 case FORMAT_TYPE_PTR: 2234 case FORMAT_TYPE_PTR:
2235 str = pointer(fmt+1, str, end, get_arg(void *), spec); 2235 str = pointer(fmt, str, end, get_arg(void *), spec);
2236 while (isalnum(*fmt)) 2236 while (isalnum(*fmt))
2237 fmt++; 2237 fmt++;
2238 break; 2238 break;