Geert Uytterhoeven | d6b28e0 | 2017-11-17 15:27:56 -0800 | [diff] [blame^] | 1 | #include <linux/module.h> |
| 2 | #include <linux/printk.h> |
| 3 | #include <linux/slab.h> |
| 4 | #include <linux/string.h> |
| 5 | |
| 6 | static __init int memset16_selftest(void) |
| 7 | { |
| 8 | unsigned i, j, k; |
| 9 | u16 v, *p; |
| 10 | |
| 11 | p = kmalloc(256 * 2 * 2, GFP_KERNEL); |
| 12 | if (!p) |
| 13 | return -1; |
| 14 | |
| 15 | for (i = 0; i < 256; i++) { |
| 16 | for (j = 0; j < 256; j++) { |
| 17 | memset(p, 0xa1, 256 * 2 * sizeof(v)); |
| 18 | memset16(p + i, 0xb1b2, j); |
| 19 | for (k = 0; k < 512; k++) { |
| 20 | v = p[k]; |
| 21 | if (k < i) { |
| 22 | if (v != 0xa1a1) |
| 23 | goto fail; |
| 24 | } else if (k < i + j) { |
| 25 | if (v != 0xb1b2) |
| 26 | goto fail; |
| 27 | } else { |
| 28 | if (v != 0xa1a1) |
| 29 | goto fail; |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fail: |
| 36 | kfree(p); |
| 37 | if (i < 256) |
| 38 | return (i << 24) | (j << 16) | k; |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | static __init int memset32_selftest(void) |
| 43 | { |
| 44 | unsigned i, j, k; |
| 45 | u32 v, *p; |
| 46 | |
| 47 | p = kmalloc(256 * 2 * 4, GFP_KERNEL); |
| 48 | if (!p) |
| 49 | return -1; |
| 50 | |
| 51 | for (i = 0; i < 256; i++) { |
| 52 | for (j = 0; j < 256; j++) { |
| 53 | memset(p, 0xa1, 256 * 2 * sizeof(v)); |
| 54 | memset32(p + i, 0xb1b2b3b4, j); |
| 55 | for (k = 0; k < 512; k++) { |
| 56 | v = p[k]; |
| 57 | if (k < i) { |
| 58 | if (v != 0xa1a1a1a1) |
| 59 | goto fail; |
| 60 | } else if (k < i + j) { |
| 61 | if (v != 0xb1b2b3b4) |
| 62 | goto fail; |
| 63 | } else { |
| 64 | if (v != 0xa1a1a1a1) |
| 65 | goto fail; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | fail: |
| 72 | kfree(p); |
| 73 | if (i < 256) |
| 74 | return (i << 24) | (j << 16) | k; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static __init int memset64_selftest(void) |
| 79 | { |
| 80 | unsigned i, j, k; |
| 81 | u64 v, *p; |
| 82 | |
| 83 | p = kmalloc(256 * 2 * 8, GFP_KERNEL); |
| 84 | if (!p) |
| 85 | return -1; |
| 86 | |
| 87 | for (i = 0; i < 256; i++) { |
| 88 | for (j = 0; j < 256; j++) { |
| 89 | memset(p, 0xa1, 256 * 2 * sizeof(v)); |
| 90 | memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j); |
| 91 | for (k = 0; k < 512; k++) { |
| 92 | v = p[k]; |
| 93 | if (k < i) { |
| 94 | if (v != 0xa1a1a1a1a1a1a1a1ULL) |
| 95 | goto fail; |
| 96 | } else if (k < i + j) { |
| 97 | if (v != 0xb1b2b3b4b5b6b7b8ULL) |
| 98 | goto fail; |
| 99 | } else { |
| 100 | if (v != 0xa1a1a1a1a1a1a1a1ULL) |
| 101 | goto fail; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | fail: |
| 108 | kfree(p); |
| 109 | if (i < 256) |
| 110 | return (i << 24) | (j << 16) | k; |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static __init int string_selftest_init(void) |
| 115 | { |
| 116 | int test, subtest; |
| 117 | |
| 118 | test = 1; |
| 119 | subtest = memset16_selftest(); |
| 120 | if (subtest) |
| 121 | goto fail; |
| 122 | |
| 123 | test = 2; |
| 124 | subtest = memset32_selftest(); |
| 125 | if (subtest) |
| 126 | goto fail; |
| 127 | |
| 128 | test = 3; |
| 129 | subtest = memset64_selftest(); |
| 130 | if (subtest) |
| 131 | goto fail; |
| 132 | |
| 133 | pr_info("String selftests succeeded\n"); |
| 134 | return 0; |
| 135 | fail: |
| 136 | pr_crit("String selftest failure %d.%08x\n", test, subtest); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | module_init(string_selftest_init); |
| 141 | MODULE_LICENSE("GPL v2"); |