kernel-test.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2024 ARM Limited.
  4. */
  5. #define _GNU_SOURCE
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/socket.h>
  15. #include <linux/kernel.h>
  16. #include <linux/if_alg.h>
  17. #define DATA_SIZE (16 * 4096)
  18. static int base, sock;
  19. static int digest_len;
  20. static char *ref;
  21. static char *digest;
  22. static char *alg_name;
  23. static struct iovec data_iov;
  24. static int zerocopy[2];
  25. static int sigs;
  26. static int iter;
  27. static void handle_exit_signal(int sig, siginfo_t *info, void *context)
  28. {
  29. printf("Terminated by signal %d, iterations=%d, signals=%d\n",
  30. sig, iter, sigs);
  31. exit(0);
  32. }
  33. static void handle_kick_signal(int sig, siginfo_t *info, void *context)
  34. {
  35. sigs++;
  36. }
  37. static char *drivers[] = {
  38. "crct10dif-arm64-ce",
  39. /* "crct10dif-arm64-neon", - Same priority as generic */
  40. "sha1-ce",
  41. "sha224-arm64",
  42. "sha224-arm64-neon",
  43. "sha224-ce",
  44. "sha256-arm64",
  45. "sha256-arm64-neon",
  46. "sha256-ce",
  47. "sha384-ce",
  48. "sha512-ce",
  49. "sha3-224-ce",
  50. "sha3-256-ce",
  51. "sha3-384-ce",
  52. "sha3-512-ce",
  53. "sm3-ce",
  54. "sm3-neon",
  55. };
  56. static bool create_socket(void)
  57. {
  58. FILE *proc;
  59. struct sockaddr_alg addr;
  60. char buf[1024];
  61. char *c, *driver_name;
  62. bool is_shash, match;
  63. int ret, i;
  64. ret = socket(AF_ALG, SOCK_SEQPACKET, 0);
  65. if (ret < 0) {
  66. if (errno == EAFNOSUPPORT) {
  67. printf("AF_ALG not supported\n");
  68. return false;
  69. }
  70. printf("Failed to create AF_ALG socket: %s (%d)\n",
  71. strerror(errno), errno);
  72. return false;
  73. }
  74. base = ret;
  75. memset(&addr, 0, sizeof(addr));
  76. addr.salg_family = AF_ALG;
  77. strncpy((char *)addr.salg_type, "hash", sizeof(addr.salg_type));
  78. proc = fopen("/proc/crypto", "r");
  79. if (!proc) {
  80. printf("Unable to open /proc/crypto\n");
  81. return false;
  82. }
  83. driver_name = NULL;
  84. is_shash = false;
  85. match = false;
  86. /* Look through /proc/crypto for a driver with kernel mode FP usage */
  87. while (!match) {
  88. c = fgets(buf, sizeof(buf), proc);
  89. if (!c) {
  90. if (feof(proc)) {
  91. printf("Nothing found in /proc/crypto\n");
  92. return false;
  93. }
  94. continue;
  95. }
  96. /* Algorithm descriptions are separated by a blank line */
  97. if (*c == '\n') {
  98. if (is_shash && driver_name) {
  99. for (i = 0; i < ARRAY_SIZE(drivers); i++) {
  100. if (strcmp(drivers[i],
  101. driver_name) == 0) {
  102. match = true;
  103. }
  104. }
  105. }
  106. if (!match) {
  107. digest_len = 0;
  108. free(driver_name);
  109. driver_name = NULL;
  110. free(alg_name);
  111. alg_name = NULL;
  112. is_shash = false;
  113. }
  114. continue;
  115. }
  116. /* Remove trailing newline */
  117. c = strchr(buf, '\n');
  118. if (c)
  119. *c = '\0';
  120. /* Find the field/value separator and start of the value */
  121. c = strchr(buf, ':');
  122. if (!c)
  123. continue;
  124. c += 2;
  125. if (strncmp(buf, "digestsize", strlen("digestsize")) == 0)
  126. sscanf(c, "%d", &digest_len);
  127. if (strncmp(buf, "name", strlen("name")) == 0)
  128. alg_name = strdup(c);
  129. if (strncmp(buf, "driver", strlen("driver")) == 0)
  130. driver_name = strdup(c);
  131. if (strncmp(buf, "type", strlen("type")) == 0)
  132. if (strncmp(c, "shash", strlen("shash")) == 0)
  133. is_shash = true;
  134. }
  135. strncpy((char *)addr.salg_name, alg_name,
  136. sizeof(addr.salg_name) - 1);
  137. ret = bind(base, (struct sockaddr *)&addr, sizeof(addr));
  138. if (ret < 0) {
  139. printf("Failed to bind %s: %s (%d)\n",
  140. addr.salg_name, strerror(errno), errno);
  141. return false;
  142. }
  143. ret = accept(base, NULL, 0);
  144. if (ret < 0) {
  145. printf("Failed to accept %s: %s (%d)\n",
  146. addr.salg_name, strerror(errno), errno);
  147. return false;
  148. }
  149. sock = ret;
  150. ret = pipe(zerocopy);
  151. if (ret != 0) {
  152. printf("Failed to create zerocopy pipe: %s (%d)\n",
  153. strerror(errno), errno);
  154. return false;
  155. }
  156. ref = malloc(digest_len);
  157. if (!ref) {
  158. printf("Failed to allocated %d byte reference\n", digest_len);
  159. return false;
  160. }
  161. digest = malloc(digest_len);
  162. if (!digest) {
  163. printf("Failed to allocated %d byte digest\n", digest_len);
  164. return false;
  165. }
  166. return true;
  167. }
  168. static bool compute_digest(void *buf)
  169. {
  170. struct iovec iov;
  171. int ret, wrote;
  172. iov = data_iov;
  173. while (iov.iov_len) {
  174. ret = vmsplice(zerocopy[1], &iov, 1, SPLICE_F_GIFT);
  175. if (ret < 0) {
  176. printf("Failed to send buffer: %s (%d)\n",
  177. strerror(errno), errno);
  178. return false;
  179. }
  180. wrote = ret;
  181. ret = splice(zerocopy[0], NULL, sock, NULL, wrote, 0);
  182. if (ret < 0) {
  183. printf("Failed to splice buffer: %s (%d)\n",
  184. strerror(errno), errno);
  185. } else if (ret != wrote) {
  186. printf("Short splice: %d < %d\n", ret, wrote);
  187. }
  188. iov.iov_len -= wrote;
  189. iov.iov_base += wrote;
  190. }
  191. reread:
  192. ret = recv(sock, buf, digest_len, 0);
  193. if (ret == 0) {
  194. printf("No digest returned\n");
  195. return false;
  196. }
  197. if (ret != digest_len) {
  198. if (errno == -EAGAIN)
  199. goto reread;
  200. printf("Failed to get digest: %s (%d)\n",
  201. strerror(errno), errno);
  202. return false;
  203. }
  204. return true;
  205. }
  206. int main(void)
  207. {
  208. char *data;
  209. struct sigaction sa;
  210. int ret;
  211. /* Ensure we have unbuffered output */
  212. setvbuf(stdout, NULL, _IOLBF, 0);
  213. /* The parent will communicate with us via signals */
  214. memset(&sa, 0, sizeof(sa));
  215. sa.sa_sigaction = handle_exit_signal;
  216. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  217. sigemptyset(&sa.sa_mask);
  218. ret = sigaction(SIGTERM, &sa, NULL);
  219. if (ret < 0)
  220. printf("Failed to install SIGTERM handler: %s (%d)\n",
  221. strerror(errno), errno);
  222. sa.sa_sigaction = handle_kick_signal;
  223. ret = sigaction(SIGUSR2, &sa, NULL);
  224. if (ret < 0)
  225. printf("Failed to install SIGUSR2 handler: %s (%d)\n",
  226. strerror(errno), errno);
  227. data = malloc(DATA_SIZE);
  228. if (!data) {
  229. printf("Failed to allocate data buffer\n");
  230. return EXIT_FAILURE;
  231. }
  232. memset(data, 0, DATA_SIZE);
  233. data_iov.iov_base = data;
  234. data_iov.iov_len = DATA_SIZE;
  235. /*
  236. * If we can't create a socket assume it's a lack of system
  237. * support and fall back to a basic FPSIMD test for the
  238. * benefit of fp-stress.
  239. */
  240. if (!create_socket()) {
  241. execl("./fpsimd-test", "./fpsimd-test", NULL);
  242. printf("Failed to fall back to fspimd-test: %d (%s)\n",
  243. errno, strerror(errno));
  244. return EXIT_FAILURE;
  245. }
  246. /*
  247. * Compute a reference digest we hope is repeatable, we do
  248. * this at runtime partly to make it easier to play with
  249. * parameters.
  250. */
  251. if (!compute_digest(ref)) {
  252. printf("Failed to compute reference digest\n");
  253. return EXIT_FAILURE;
  254. }
  255. printf("AF_ALG using %s\n", alg_name);
  256. while (true) {
  257. if (!compute_digest(digest)) {
  258. printf("Failed to compute digest, iter=%d\n", iter);
  259. return EXIT_FAILURE;
  260. }
  261. if (memcmp(ref, digest, digest_len) != 0) {
  262. printf("Digest mismatch, iter=%d\n", iter);
  263. return EXIT_FAILURE;
  264. }
  265. iter++;
  266. }
  267. return EXIT_FAILURE;
  268. }