xref: /linux/io_uring/query.c (revision cf72cbb39da84b6f02f90c07f33b102fc10b16f0)
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_event 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 zcrx_rq_hdr);
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_event *e = &data->zcrx_notif;
51 
52 	e->event_flags = ZCRX_EVENT_TYPE_MASK;
53 	e->stats_size = sizeof(struct zcrx_stats);
54 	e->stats_off_alignment = __alignof__(struct zcrx_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 	/* copy_struct_to_user() zeros up to usize bytes */
80 	if (hdr.size > PAGE_SIZE)
81 		return -E2BIG;
82 	usize = hdr.size;
83 	hdr.size = min(hdr.size, IO_MAX_QUERY_SIZE);
84 	udata = u64_to_user_ptr(hdr.query_data);
85 
86 	if (hdr.query_op >= __IO_URING_QUERY_MAX) {
87 		ret = -EOPNOTSUPP;
88 		goto out;
89 	}
90 	if (!mem_is_zero(hdr.__resv, sizeof(hdr.__resv)) || hdr.result || !hdr.size)
91 		goto out;
92 	if (copy_from_user(data, udata, hdr.size))
93 		return -EFAULT;
94 
95 	switch (hdr.query_op) {
96 	case IO_URING_QUERY_OPCODES:
97 		ret = io_query_ops(data);
98 		break;
99 	case IO_URING_QUERY_ZCRX:
100 		ret = io_query_zcrx(data);
101 		break;
102 	case IO_URING_QUERY_ZCRX_EVENT:
103 		ret = io_query_zcrx_notif(data);
104 		break;
105 	case IO_URING_QUERY_SCQ:
106 		ret = io_query_scq(data);
107 		break;
108 	}
109 
110 	if (ret >= 0) {
111 		if (WARN_ON_ONCE(ret > IO_MAX_QUERY_SIZE))
112 			return -EFAULT;
113 		res_size = ret;
114 		ret = 0;
115 	}
116 out:
117 	hdr.result = ret;
118 	hdr.size = min_t(size_t, usize, res_size);
119 
120 	if (copy_struct_to_user(udata, usize, data, hdr.size, NULL))
121 		return -EFAULT;
122 	if (copy_to_user(uhdr, &hdr, sizeof(hdr)))
123 		return -EFAULT;
124 	*next_entry = hdr.next_entry;
125 	return 0;
126 }
127 
128 int io_query(void __user *arg, unsigned nr_args)
129 {
130 	union io_query_data entry_buffer;
131 	void __user *uhdr = arg;
132 	int ret, nr = 0;
133 
134 	memset(&entry_buffer, 0, sizeof(entry_buffer));
135 
136 	if (nr_args)
137 		return -EINVAL;
138 
139 	while (uhdr) {
140 		u64 next_hdr;
141 
142 		ret = io_handle_query_entry(&entry_buffer, uhdr, &next_hdr);
143 		if (ret)
144 			return ret;
145 		uhdr = u64_to_user_ptr(next_hdr);
146 
147 		/* Have some limit to avoid a potential cycle */
148 		if (++nr >= IO_MAX_QUERY_ENTRIES)
149 			return -ERANGE;
150 		if (fatal_signal_pending(current))
151 			return -EINTR;
152 		cond_resched();
153 	}
154 	return 0;
155 }
156