lib/string.c: add testcases for memset16/32/64
authorMatthew Wilcox <mawilcox@microsoft.com>
Fri, 8 Sep 2017 23:13:52 +0000 (16:13 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 9 Sep 2017 01:26:48 +0000 (18:26 -0700)
[akpm@linux-foundation.org: minor tweaks]
Link: http://lkml.kernel.org/r/20170720184539.31609-3-willy@infradead.org
Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
Cc: "James E.J. Bottomley" <jejb@linux.vnet.ibm.com>
Cc: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: David Miller <davem@davemloft.net>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Ivan Kokshaysky <ink@jurassic.park.msu.ru>
Cc: Matt Turner <mattst88@gmail.com>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Ralf Baechle <ralf@linux-mips.org>
Cc: Richard Henderson <rth@twiddle.net>
Cc: Russell King <rmk+kernel@armlinux.org.uk>
Cc: Sam Ravnborg <sam@ravnborg.org>
Cc: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/Kconfig
lib/string.c

index 6762529..40b114a 100644 (file)
@@ -575,4 +575,7 @@ config PARMAN
 config PRIME_NUMBERS
        tristate
 
+config STRING_SELFTEST
+       bool "Test string functions"
+
 endmenu
index 198148b..abf6499 100644 (file)
@@ -1051,3 +1051,132 @@ 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 = kmalloc(256 * 2 * 2, GFP_KERNEL);
+
+       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 = kmalloc(256 * 2 * 4, GFP_KERNEL);
+
+       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 = kmalloc(256 * 2 * 8, GFP_KERNEL);
+
+       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 */