diff options
author | Alexander Shishkin <alexander.shishkin@linux.intel.com> | 2018-10-05 08:43:05 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2018-10-11 06:12:55 -0400 |
commit | ce76d938dd98817f998c905e01fbb99b072c0bf6 (patch) | |
tree | 47f7757c447cf582d7523c7a5e1f467ef28f050c | |
parent | 6c7e4b6882ad080dad623ba2f4c1a4db578313cb (diff) |
lib: Add memcat_p(): paste 2 pointer arrays together
This adds a helper to paste 2 pointer arrays together, useful for merging
various types of attribute arrays. There are a few places in the kernel
tree where this is open coded, and I just added one more in the STM class.
The naming is inspired by memset_p() and memcat(), and partial credit for
it goes to Andy Shevchenko.
This patch adds the function wrapped in a type-enforcing macro and a test
module.
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | include/linux/string.h | 7 | ||||
-rw-r--r-- | lib/Kconfig.debug | 8 | ||||
-rw-r--r-- | lib/Makefile | 1 | ||||
-rw-r--r-- | lib/string.c | 31 | ||||
-rw-r--r-- | lib/test_memcat_p.c | 115 |
5 files changed, 162 insertions, 0 deletions
diff --git a/include/linux/string.h b/include/linux/string.h index 4a5a0eb7df51..27d0482e5e05 100644 --- a/include/linux/string.h +++ b/include/linux/string.h | |||
@@ -131,6 +131,13 @@ static inline void *memset_p(void **p, void *v, __kernel_size_t n) | |||
131 | return memset64((uint64_t *)p, (uintptr_t)v, n); | 131 | return memset64((uint64_t *)p, (uintptr_t)v, n); |
132 | } | 132 | } |
133 | 133 | ||
134 | extern void **__memcat_p(void **a, void **b); | ||
135 | #define memcat_p(a, b) ({ \ | ||
136 | BUILD_BUG_ON_MSG(!__same_type(*(a), *(b)), \ | ||
137 | "type mismatch in memcat_p()"); \ | ||
138 | (typeof(*a) *)__memcat_p((void **)(a), (void **)(b)); \ | ||
139 | }) | ||
140 | |||
134 | #ifndef __HAVE_ARCH_MEMCPY | 141 | #ifndef __HAVE_ARCH_MEMCPY |
135 | extern void * memcpy(void *,const void *,__kernel_size_t); | 142 | extern void * memcpy(void *,const void *,__kernel_size_t); |
136 | #endif | 143 | #endif |
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 4966c4fbe7f7..c0176510262e 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug | |||
@@ -1965,6 +1965,14 @@ config TEST_DEBUG_VIRTUAL | |||
1965 | 1965 | ||
1966 | If unsure, say N. | 1966 | If unsure, say N. |
1967 | 1967 | ||
1968 | config TEST_MEMCAT_P | ||
1969 | tristate "Test memcat_p() helper function" | ||
1970 | help | ||
1971 | Test the memcat_p() helper for correctly merging two | ||
1972 | pointer arrays together. | ||
1973 | |||
1974 | If unsure, say N. | ||
1975 | |||
1968 | endif # RUNTIME_TESTING_MENU | 1976 | endif # RUNTIME_TESTING_MENU |
1969 | 1977 | ||
1970 | config MEMTEST | 1978 | config MEMTEST |
diff --git a/lib/Makefile b/lib/Makefile index ca3f7ebb900d..c2588a2d7b1e 100644 --- a/lib/Makefile +++ b/lib/Makefile | |||
@@ -71,6 +71,7 @@ obj-$(CONFIG_TEST_UUID) += test_uuid.o | |||
71 | obj-$(CONFIG_TEST_PARMAN) += test_parman.o | 71 | obj-$(CONFIG_TEST_PARMAN) += test_parman.o |
72 | obj-$(CONFIG_TEST_KMOD) += test_kmod.o | 72 | obj-$(CONFIG_TEST_KMOD) += test_kmod.o |
73 | obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o | 73 | obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o |
74 | obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o | ||
74 | 75 | ||
75 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) | 76 | ifeq ($(CONFIG_DEBUG_KOBJECT),y) |
76 | CFLAGS_kobject.o += -DDEBUG | 77 | CFLAGS_kobject.o += -DDEBUG |
diff --git a/lib/string.c b/lib/string.c index 2c0900a5d51a..453f35994eb6 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <linux/export.h> | 27 | #include <linux/export.h> |
28 | #include <linux/bug.h> | 28 | #include <linux/bug.h> |
29 | #include <linux/errno.h> | 29 | #include <linux/errno.h> |
30 | #include <linux/slab.h> | ||
30 | 31 | ||
31 | #include <asm/byteorder.h> | 32 | #include <asm/byteorder.h> |
32 | #include <asm/word-at-a-time.h> | 33 | #include <asm/word-at-a-time.h> |
@@ -890,6 +891,36 @@ void *memscan(void *addr, int c, size_t size) | |||
890 | EXPORT_SYMBOL(memscan); | 891 | EXPORT_SYMBOL(memscan); |
891 | #endif | 892 | #endif |
892 | 893 | ||
894 | /* | ||
895 | * Merge two NULL-terminated pointer arrays into a newly allocated | ||
896 | * array, which is also NULL-terminated. Nomenclature is inspired by | ||
897 | * memset_p() and memcat() found elsewhere in the kernel source tree. | ||
898 | */ | ||
899 | void **__memcat_p(void **a, void **b) | ||
900 | { | ||
901 | void **p = a, **new; | ||
902 | int nr; | ||
903 | |||
904 | /* count the elements in both arrays */ | ||
905 | for (nr = 0, p = a; *p; nr++, p++) | ||
906 | ; | ||
907 | for (p = b; *p; nr++, p++) | ||
908 | ; | ||
909 | /* one for the NULL-terminator */ | ||
910 | nr++; | ||
911 | |||
912 | new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL); | ||
913 | if (!new) | ||
914 | return NULL; | ||
915 | |||
916 | /* nr -> last index; p points to NULL in b[] */ | ||
917 | for (nr--; nr >= 0; nr--, p = p == b ? &a[nr] : p - 1) | ||
918 | new[nr] = *p; | ||
919 | |||
920 | return new; | ||
921 | } | ||
922 | EXPORT_SYMBOL_GPL(__memcat_p); | ||
923 | |||
893 | #ifndef __HAVE_ARCH_STRSTR | 924 | #ifndef __HAVE_ARCH_STRSTR |
894 | /** | 925 | /** |
895 | * strstr - Find the first substring in a %NUL terminated string | 926 | * strstr - Find the first substring in a %NUL terminated string |
diff --git a/lib/test_memcat_p.c b/lib/test_memcat_p.c new file mode 100644 index 000000000000..2b163a749ecb --- /dev/null +++ b/lib/test_memcat_p.c | |||
@@ -0,0 +1,115 @@ | |||
1 | // SPDX-License-Identifier: GPL-2.0 | ||
2 | /* | ||
3 | * Test cases for memcat_p() in lib/string.c | ||
4 | */ | ||
5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
6 | |||
7 | #include <linux/string.h> | ||
8 | #include <linux/slab.h> | ||
9 | #include <linux/module.h> | ||
10 | |||
11 | struct test_struct { | ||
12 | int num; | ||
13 | unsigned int magic; | ||
14 | }; | ||
15 | |||
16 | #define MAGIC 0xf00ff00f | ||
17 | /* Size of each of the NULL-terminated input arrays */ | ||
18 | #define INPUT_MAX 128 | ||
19 | /* Expected number of non-NULL elements in the output array */ | ||
20 | #define EXPECT (INPUT_MAX * 2 - 2) | ||
21 | |||
22 | static int __init test_memcat_p_init(void) | ||
23 | { | ||
24 | struct test_struct **in0, **in1, **out, **p; | ||
25 | int err = -ENOMEM, i, r, total = 0; | ||
26 | |||
27 | in0 = kcalloc(INPUT_MAX, sizeof(*in0), GFP_KERNEL); | ||
28 | if (!in0) | ||
29 | return err; | ||
30 | |||
31 | in1 = kcalloc(INPUT_MAX, sizeof(*in1), GFP_KERNEL); | ||
32 | if (!in1) | ||
33 | goto err_free_in0; | ||
34 | |||
35 | for (i = 0, r = 1; i < INPUT_MAX - 1; i++) { | ||
36 | in0[i] = kmalloc(sizeof(**in0), GFP_KERNEL); | ||
37 | if (!in0[i]) | ||
38 | goto err_free_elements; | ||
39 | |||
40 | in1[i] = kmalloc(sizeof(**in1), GFP_KERNEL); | ||
41 | if (!in1[i]) { | ||
42 | kfree(in0[i]); | ||
43 | goto err_free_elements; | ||
44 | } | ||
45 | |||
46 | /* lifted from test_sort.c */ | ||
47 | r = (r * 725861) % 6599; | ||
48 | in0[i]->num = r; | ||
49 | in1[i]->num = -r; | ||
50 | in0[i]->magic = MAGIC; | ||
51 | in1[i]->magic = MAGIC; | ||
52 | } | ||
53 | |||
54 | in0[i] = in1[i] = NULL; | ||
55 | |||
56 | out = memcat_p(in0, in1); | ||
57 | if (!out) | ||
58 | goto err_free_all_elements; | ||
59 | |||
60 | err = -EINVAL; | ||
61 | for (i = 0, p = out; *p && (i < INPUT_MAX * 2 - 1); p++, i++) { | ||
62 | total += (*p)->num; | ||
63 | |||
64 | if ((*p)->magic != MAGIC) { | ||
65 | pr_err("test failed: wrong magic at %d: %u\n", i, | ||
66 | (*p)->magic); | ||
67 | goto err_free_out; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | if (total) { | ||
72 | pr_err("test failed: expected zero total, got %d\n", total); | ||
73 | goto err_free_out; | ||
74 | } | ||
75 | |||
76 | if (i != EXPECT) { | ||
77 | pr_err("test failed: expected output size %d, got %d\n", | ||
78 | EXPECT, i); | ||
79 | goto err_free_out; | ||
80 | } | ||
81 | |||
82 | for (i = 0; i < INPUT_MAX - 1; i++) | ||
83 | if (out[i] != in0[i] || out[i + INPUT_MAX - 1] != in1[i]) { | ||
84 | pr_err("test failed: wrong element order at %d\n", i); | ||
85 | goto err_free_out; | ||
86 | } | ||
87 | |||
88 | err = 0; | ||
89 | pr_info("test passed\n"); | ||
90 | |||
91 | err_free_out: | ||
92 | kfree(out); | ||
93 | err_free_all_elements: | ||
94 | i = INPUT_MAX; | ||
95 | err_free_elements: | ||
96 | for (i--; i >= 0; i--) { | ||
97 | kfree(in1[i]); | ||
98 | kfree(in0[i]); | ||
99 | } | ||
100 | |||
101 | kfree(in1); | ||
102 | err_free_in0: | ||
103 | kfree(in0); | ||
104 | |||
105 | return err; | ||
106 | } | ||
107 | |||
108 | static void __exit test_memcat_p_exit(void) | ||
109 | { | ||
110 | } | ||
111 | |||
112 | module_init(test_memcat_p_init); | ||
113 | module_exit(test_memcat_p_exit); | ||
114 | |||
115 | MODULE_LICENSE("GPL"); | ||