]> git.baikalelectronics.ru Git - kernel.git/commitdiff
lib: add module support to string tests
authorGeert Uytterhoeven <geert@linux-m68k.org>
Fri, 17 Nov 2017 23:27:56 +0000 (15:27 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 18 Nov 2017 00:10:01 +0000 (16:10 -0800)
Extract the string test code into its own source file, to allow
compiling it either to a loadable module, or built into the kernel.

Fixes: 03270c13c5ffaa6a ("lib/string.c: add testcases for memset16/32/64")
Link: http://lkml.kernel.org/r/1505397744-3387-1-git-send-email-geert@linux-m68k.org
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Matthew Wilcox <mawilcox@microsoft.com>
Cc: Shuah Khan <shuah@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/Kconfig
lib/Makefile
lib/string.c
lib/test_string.c [new file with mode: 0644]

index a2b6745324ab358a5685ccb6ce690a4f64a8b809..368972f0db783dc5173e40ce0e337aac8d1a5077 100644 (file)
@@ -584,7 +584,7 @@ config PRIME_NUMBERS
        tristate
 
 config STRING_SELFTEST
-       bool "Test string functions"
+       tristate "Test string functions"
 
 endmenu
 
index 136a0b2545641dc0f6e4635fd7124a41081fa5a9..e3cb2a2413380aa7d70d65ca11689aea6ea261bf 100644 (file)
@@ -40,6 +40,7 @@ obj-y += bcd.o div64.o sort.o parser.o debug_locks.o random32.o \
         bsearch.o find_bit.o llist.o memweight.o kfifo.o \
         percpu-refcount.o percpu_ida.o rhashtable.o reciprocal_div.o \
         once.o refcount.o usercopy.o errseq.o
+obj-$(CONFIG_STRING_SELFTEST) += test_string.o
 obj-y += string_helpers.o
 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
 obj-y += hexdump.o
index 5e8d410a93df5eb569e15815782be2027ec69d6e..64a9e33f1daae4b2bb02ba7dbdc0bf431756935f 100644 (file)
@@ -1052,144 +1052,3 @@ void fortify_panic(const char *name)
        BUG();
 }
 EXPORT_SYMBOL(fortify_panic);
-
-#ifdef CONFIG_STRING_SELFTEST
-#include <linux/slab.h>
-#include <linux/module.h>
-
-static __init int memset16_selftest(void)
-{
-       unsigned i, j, k;
-       u16 v, *p;
-
-       p = kmalloc(256 * 2 * 2, GFP_KERNEL);
-       if (!p)
-               return -1;
-
-       for (i = 0; i < 256; i++) {
-               for (j = 0; j < 256; j++) {
-                       memset(p, 0xa1, 256 * 2 * sizeof(v));
-                       memset16(p + i, 0xb1b2, j);
-                       for (k = 0; k < 512; k++) {
-                               v = p[k];
-                               if (k < i) {
-                                       if (v != 0xa1a1)
-                                               goto fail;
-                               } else if (k < i + j) {
-                                       if (v != 0xb1b2)
-                                               goto fail;
-                               } else {
-                                       if (v != 0xa1a1)
-                                               goto fail;
-                               }
-                       }
-               }
-       }
-
-fail:
-       kfree(p);
-       if (i < 256)
-               return (i << 24) | (j << 16) | k;
-       return 0;
-}
-
-static __init int memset32_selftest(void)
-{
-       unsigned i, j, k;
-       u32 v, *p;
-
-       p = kmalloc(256 * 2 * 4, GFP_KERNEL);
-       if (!p)
-               return -1;
-
-       for (i = 0; i < 256; i++) {
-               for (j = 0; j < 256; j++) {
-                       memset(p, 0xa1, 256 * 2 * sizeof(v));
-                       memset32(p + i, 0xb1b2b3b4, j);
-                       for (k = 0; k < 512; k++) {
-                               v = p[k];
-                               if (k < i) {
-                                       if (v != 0xa1a1a1a1)
-                                               goto fail;
-                               } else if (k < i + j) {
-                                       if (v != 0xb1b2b3b4)
-                                               goto fail;
-                               } else {
-                                       if (v != 0xa1a1a1a1)
-                                               goto fail;
-                               }
-                       }
-               }
-       }
-
-fail:
-       kfree(p);
-       if (i < 256)
-               return (i << 24) | (j << 16) | k;
-       return 0;
-}
-
-static __init int memset64_selftest(void)
-{
-       unsigned i, j, k;
-       u64 v, *p;
-
-       p = kmalloc(256 * 2 * 8, GFP_KERNEL);
-       if (!p)
-               return -1;
-
-       for (i = 0; i < 256; i++) {
-               for (j = 0; j < 256; j++) {
-                       memset(p, 0xa1, 256 * 2 * sizeof(v));
-                       memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
-                       for (k = 0; k < 512; k++) {
-                               v = p[k];
-                               if (k < i) {
-                                       if (v != 0xa1a1a1a1a1a1a1a1ULL)
-                                               goto fail;
-                               } else if (k < i + j) {
-                                       if (v != 0xb1b2b3b4b5b6b7b8ULL)
-                                               goto fail;
-                               } else {
-                                       if (v != 0xa1a1a1a1a1a1a1a1ULL)
-                                               goto fail;
-                               }
-                       }
-               }
-       }
-
-fail:
-       kfree(p);
-       if (i < 256)
-               return (i << 24) | (j << 16) | k;
-       return 0;
-}
-
-static __init int string_selftest_init(void)
-{
-       int test, subtest;
-
-       test = 1;
-       subtest = memset16_selftest();
-       if (subtest)
-               goto fail;
-
-       test = 2;
-       subtest = memset32_selftest();
-       if (subtest)
-               goto fail;
-
-       test = 3;
-       subtest = memset64_selftest();
-       if (subtest)
-               goto fail;
-
-       pr_info("String selftests succeeded\n");
-       return 0;
-fail:
-       pr_crit("String selftest failure %d.%08x\n", test, subtest);
-       return 0;
-}
-
-module_init(string_selftest_init);
-#endif /* CONFIG_STRING_SELFTEST */
diff --git a/lib/test_string.c b/lib/test_string.c
new file mode 100644 (file)
index 0000000..0fcdb82
--- /dev/null
@@ -0,0 +1,141 @@
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+
+static __init int memset16_selftest(void)
+{
+       unsigned i, j, k;
+       u16 v, *p;
+
+       p = kmalloc(256 * 2 * 2, GFP_KERNEL);
+       if (!p)
+               return -1;
+
+       for (i = 0; i < 256; i++) {
+               for (j = 0; j < 256; j++) {
+                       memset(p, 0xa1, 256 * 2 * sizeof(v));
+                       memset16(p + i, 0xb1b2, j);
+                       for (k = 0; k < 512; k++) {
+                               v = p[k];
+                               if (k < i) {
+                                       if (v != 0xa1a1)
+                                               goto fail;
+                               } else if (k < i + j) {
+                                       if (v != 0xb1b2)
+                                               goto fail;
+                               } else {
+                                       if (v != 0xa1a1)
+                                               goto fail;
+                               }
+                       }
+               }
+       }
+
+fail:
+       kfree(p);
+       if (i < 256)
+               return (i << 24) | (j << 16) | k;
+       return 0;
+}
+
+static __init int memset32_selftest(void)
+{
+       unsigned i, j, k;
+       u32 v, *p;
+
+       p = kmalloc(256 * 2 * 4, GFP_KERNEL);
+       if (!p)
+               return -1;
+
+       for (i = 0; i < 256; i++) {
+               for (j = 0; j < 256; j++) {
+                       memset(p, 0xa1, 256 * 2 * sizeof(v));
+                       memset32(p + i, 0xb1b2b3b4, j);
+                       for (k = 0; k < 512; k++) {
+                               v = p[k];
+                               if (k < i) {
+                                       if (v != 0xa1a1a1a1)
+                                               goto fail;
+                               } else if (k < i + j) {
+                                       if (v != 0xb1b2b3b4)
+                                               goto fail;
+                               } else {
+                                       if (v != 0xa1a1a1a1)
+                                               goto fail;
+                               }
+                       }
+               }
+       }
+
+fail:
+       kfree(p);
+       if (i < 256)
+               return (i << 24) | (j << 16) | k;
+       return 0;
+}
+
+static __init int memset64_selftest(void)
+{
+       unsigned i, j, k;
+       u64 v, *p;
+
+       p = kmalloc(256 * 2 * 8, GFP_KERNEL);
+       if (!p)
+               return -1;
+
+       for (i = 0; i < 256; i++) {
+               for (j = 0; j < 256; j++) {
+                       memset(p, 0xa1, 256 * 2 * sizeof(v));
+                       memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
+                       for (k = 0; k < 512; k++) {
+                               v = p[k];
+                               if (k < i) {
+                                       if (v != 0xa1a1a1a1a1a1a1a1ULL)
+                                               goto fail;
+                               } else if (k < i + j) {
+                                       if (v != 0xb1b2b3b4b5b6b7b8ULL)
+                                               goto fail;
+                               } else {
+                                       if (v != 0xa1a1a1a1a1a1a1a1ULL)
+                                               goto fail;
+                               }
+                       }
+               }
+       }
+
+fail:
+       kfree(p);
+       if (i < 256)
+               return (i << 24) | (j << 16) | k;
+       return 0;
+}
+
+static __init int string_selftest_init(void)
+{
+       int test, subtest;
+
+       test = 1;
+       subtest = memset16_selftest();
+       if (subtest)
+               goto fail;
+
+       test = 2;
+       subtest = memset32_selftest();
+       if (subtest)
+               goto fail;
+
+       test = 3;
+       subtest = memset64_selftest();
+       if (subtest)
+               goto fail;
+
+       pr_info("String selftests succeeded\n");
+       return 0;
+fail:
+       pr_crit("String selftest failure %d.%08x\n", test, subtest);
+       return 0;
+}
+
+module_init(string_selftest_init);
+MODULE_LICENSE("GPL v2");