query.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include "linux/io_uring/query.h"
  3. #include "query.h"
  4. #include "io_uring.h"
  5. #include "zcrx.h"
  6. union io_query_data {
  7. struct io_uring_query_opcode opcodes;
  8. struct io_uring_query_zcrx zcrx;
  9. struct io_uring_query_scq scq;
  10. };
  11. #define IO_MAX_QUERY_SIZE sizeof(union io_query_data)
  12. #define IO_MAX_QUERY_ENTRIES 1000
  13. static ssize_t io_query_ops(union io_query_data *data)
  14. {
  15. struct io_uring_query_opcode *e = &data->opcodes;
  16. e->nr_request_opcodes = IORING_OP_LAST;
  17. e->nr_register_opcodes = IORING_REGISTER_LAST;
  18. e->feature_flags = IORING_FEAT_FLAGS;
  19. e->ring_setup_flags = IORING_SETUP_FLAGS;
  20. e->enter_flags = IORING_ENTER_FLAGS;
  21. e->sqe_flags = SQE_VALID_FLAGS;
  22. e->nr_query_opcodes = __IO_URING_QUERY_MAX;
  23. e->__pad = 0;
  24. return sizeof(*e);
  25. }
  26. static ssize_t io_query_zcrx(union io_query_data *data)
  27. {
  28. struct io_uring_query_zcrx *e = &data->zcrx;
  29. e->register_flags = ZCRX_REG_IMPORT;
  30. e->area_flags = IORING_ZCRX_AREA_DMABUF;
  31. e->nr_ctrl_opcodes = __ZCRX_CTRL_LAST;
  32. e->rq_hdr_size = sizeof(struct io_uring);
  33. e->rq_hdr_alignment = L1_CACHE_BYTES;
  34. e->features = ZCRX_FEATURE_RX_PAGE_SIZE;
  35. e->__resv2 = 0;
  36. return sizeof(*e);
  37. }
  38. static ssize_t io_query_scq(union io_query_data *data)
  39. {
  40. struct io_uring_query_scq *e = &data->scq;
  41. e->hdr_size = sizeof(struct io_rings);
  42. e->hdr_alignment = SMP_CACHE_BYTES;
  43. return sizeof(*e);
  44. }
  45. static int io_handle_query_entry(union io_query_data *data, void __user *uhdr,
  46. u64 *next_entry)
  47. {
  48. struct io_uring_query_hdr hdr;
  49. size_t usize, res_size = 0;
  50. ssize_t ret = -EINVAL;
  51. void __user *udata;
  52. if (copy_from_user(&hdr, uhdr, sizeof(hdr)))
  53. return -EFAULT;
  54. usize = hdr.size;
  55. hdr.size = min(hdr.size, IO_MAX_QUERY_SIZE);
  56. udata = u64_to_user_ptr(hdr.query_data);
  57. if (hdr.query_op >= __IO_URING_QUERY_MAX) {
  58. ret = -EOPNOTSUPP;
  59. goto out;
  60. }
  61. if (!mem_is_zero(hdr.__resv, sizeof(hdr.__resv)) || hdr.result || !hdr.size)
  62. goto out;
  63. if (copy_from_user(data, udata, hdr.size))
  64. return -EFAULT;
  65. switch (hdr.query_op) {
  66. case IO_URING_QUERY_OPCODES:
  67. ret = io_query_ops(data);
  68. break;
  69. case IO_URING_QUERY_ZCRX:
  70. ret = io_query_zcrx(data);
  71. break;
  72. case IO_URING_QUERY_SCQ:
  73. ret = io_query_scq(data);
  74. break;
  75. }
  76. if (ret >= 0) {
  77. if (WARN_ON_ONCE(ret > IO_MAX_QUERY_SIZE))
  78. return -EFAULT;
  79. res_size = ret;
  80. ret = 0;
  81. }
  82. out:
  83. hdr.result = ret;
  84. hdr.size = min_t(size_t, usize, res_size);
  85. if (copy_struct_to_user(udata, usize, data, hdr.size, NULL))
  86. return -EFAULT;
  87. if (copy_to_user(uhdr, &hdr, sizeof(hdr)))
  88. return -EFAULT;
  89. *next_entry = hdr.next_entry;
  90. return 0;
  91. }
  92. int io_query(void __user *arg, unsigned nr_args)
  93. {
  94. union io_query_data entry_buffer;
  95. void __user *uhdr = arg;
  96. int ret, nr = 0;
  97. memset(&entry_buffer, 0, sizeof(entry_buffer));
  98. if (nr_args)
  99. return -EINVAL;
  100. while (uhdr) {
  101. u64 next_hdr;
  102. ret = io_handle_query_entry(&entry_buffer, uhdr, &next_hdr);
  103. if (ret)
  104. return ret;
  105. uhdr = u64_to_user_ptr(next_hdr);
  106. /* Have some limit to avoid a potential cycle */
  107. if (++nr >= IO_MAX_QUERY_ENTRIES)
  108. return -ERANGE;
  109. if (fatal_signal_pending(current))
  110. return -EINTR;
  111. cond_resched();
  112. }
  113. return 0;
  114. }