io_uring_setup.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "../config-host.h"
  2. /* SPDX-License-Identifier: MIT */
  3. /*
  4. * io_uring_setup.c
  5. *
  6. * Description: Unit tests for the io_uring_setup system call.
  7. *
  8. * Copyright 2019, Red Hat, Inc.
  9. * Author: Jeff Moyer <jmoyer@redhat.com>
  10. */
  11. #include <stdio.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <sys/sysinfo.h>
  18. #include "liburing.h"
  19. #include "helpers.h"
  20. #include "../syscall.h"
  21. /* bogus: setup returns a valid fd on success... expect can't predict the
  22. fd we'll get, so this really only takes 1 parameter: error */
  23. static int try_io_uring_setup(unsigned entries, struct io_uring_params *p,
  24. int expect)
  25. {
  26. int ret;
  27. ret = io_uring_setup(entries, p);
  28. if (ret != expect) {
  29. fprintf(stderr, "expected %d, got %d\n", expect, ret);
  30. /* if we got a valid uring, close it */
  31. if (ret > 0)
  32. close(ret);
  33. return 1;
  34. }
  35. if (expect < 0 && expect != ret) {
  36. if (ret == -EPERM && geteuid() != 0) {
  37. printf("Needs root, not flagging as an error\n");
  38. return 0;
  39. }
  40. fprintf(stderr, "expected errno %d, got %d\n", expect, ret);
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. int main(int argc, char **argv)
  46. {
  47. int fd;
  48. unsigned int status = 0;
  49. struct io_uring_params p;
  50. if (argc > 1)
  51. return T_EXIT_SKIP;
  52. memset(&p, 0, sizeof(p));
  53. status |= try_io_uring_setup(0, &p, -EINVAL);
  54. status |= try_io_uring_setup(1, NULL, -EFAULT);
  55. /* resv array is non-zero */
  56. memset(&p, 0, sizeof(p));
  57. p.resv[0] = p.resv[1] = p.resv[2] = 1;
  58. status |= try_io_uring_setup(1, &p, -EINVAL);
  59. /* invalid flags */
  60. memset(&p, 0, sizeof(p));
  61. p.flags = ~0U;
  62. status |= try_io_uring_setup(1, &p, -EINVAL);
  63. /* IORING_SETUP_SQ_AFF set but not IORING_SETUP_SQPOLL */
  64. memset(&p, 0, sizeof(p));
  65. p.flags = IORING_SETUP_SQ_AFF;
  66. status |= try_io_uring_setup(1, &p, -EINVAL);
  67. /* attempt to bind to invalid cpu */
  68. memset(&p, 0, sizeof(p));
  69. p.flags = IORING_SETUP_SQPOLL | IORING_SETUP_SQ_AFF;
  70. p.sq_thread_cpu = get_nprocs_conf();
  71. status |= try_io_uring_setup(1, &p, -EINVAL);
  72. /* I think we can limit a process to a set of cpus. I assume
  73. * we shouldn't be able to setup a kernel thread outside of that.
  74. * try to do that. (task->cpus_allowed) */
  75. /* read/write on io_uring_fd */
  76. memset(&p, 0, sizeof(p));
  77. fd = io_uring_setup(1, &p);
  78. if (fd < 0) {
  79. fprintf(stderr, "io_uring_setup failed with %d, expected success\n",
  80. -fd);
  81. status = 1;
  82. } else {
  83. char buf[4096];
  84. int ret;
  85. ret = read(fd, buf, 4096);
  86. if (ret >= 0) {
  87. fprintf(stderr, "read from io_uring fd succeeded. expected fail\n");
  88. status = 1;
  89. }
  90. }
  91. if (!status)
  92. return T_EXIT_PASS;
  93. fprintf(stderr, "FAIL\n");
  94. return T_EXIT_FAIL;
  95. }