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