^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) static __init int memset16_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) unsigned i, j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) u16 v, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) p = kmalloc(256 * 2 * 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) memset(p, 0xa1, 256 * 2 * sizeof(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) memset16(p + i, 0xb1b2, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) for (k = 0; k < 512; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) v = p[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (k < i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (v != 0xa1a1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) } else if (k < i + j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (v != 0xb1b2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (v != 0xa1a1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (i < 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return (i << 24) | (j << 16) | k | 0x8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static __init int memset32_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned i, j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u32 v, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) p = kmalloc(256 * 2 * 4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) memset(p, 0xa1, 256 * 2 * sizeof(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memset32(p + i, 0xb1b2b3b4, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) for (k = 0; k < 512; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) v = p[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (k < i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (v != 0xa1a1a1a1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) } else if (k < i + j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (v != 0xb1b2b3b4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (v != 0xa1a1a1a1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (i < 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return (i << 24) | (j << 16) | k | 0x8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static __init int memset64_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) unsigned i, j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) u64 v, *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) p = kmalloc(256 * 2 * 8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) for (i = 0; i < 256; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (j = 0; j < 256; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) memset(p, 0xa1, 256 * 2 * sizeof(v));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) for (k = 0; k < 512; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) v = p[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (k < i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (v != 0xa1a1a1a1a1a1a1a1ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) } else if (k < i + j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (v != 0xb1b2b3b4b5b6b7b8ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (v != 0xa1a1a1a1a1a1a1a1ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (i < 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return (i << 24) | (j << 16) | k | 0x8000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) static __init int strchr_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) const char *test_string = "abcdefghijkl";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) const char *empty_string = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) char *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) for (i = 0; i < strlen(test_string) + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) result = strchr(test_string, test_string[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (result - test_string != i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return i + 'a';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) result = strchr(empty_string, '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (result != empty_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0x101;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) result = strchr(empty_string, 'a');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0x102;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) result = strchr(test_string, 'z');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0x103;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static __init int strnchr_selftest(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const char *test_string = "abcdefghijkl";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) const char *empty_string = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) char *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) for (i = 0; i < strlen(test_string) + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) for (j = 0; j < strlen(test_string) + 2; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) result = strnchr(test_string, j, test_string[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (j <= i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return ((i + 'a') << 8) | j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (result - test_string != i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return ((i + 'a') << 8) | j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) result = strnchr(empty_string, 0, '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0x10001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) result = strnchr(empty_string, 1, '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) if (result != empty_string)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0x10002;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) result = strnchr(empty_string, 1, 'a');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0x10003;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) result = strnchr(NULL, 0, '\0');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 0x10004;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static __init int string_selftest_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int test, subtest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) test = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) subtest = memset16_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) test = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) subtest = memset32_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) test = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) subtest = memset64_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) test = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) subtest = strchr_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) test = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) subtest = strnchr_selftest();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (subtest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pr_info("String selftests succeeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pr_crit("String selftest failure %d.%08x\n", test, subtest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) module_init(string_selftest_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) MODULE_LICENSE("GPL v2");