blob: 1dbf663f7f436a18297daba9d6c822457ab9a78e [file] [log] [blame]
Matan Baraka0aa3092017-08-03 16:06:55 +03001/*
2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#ifndef _UVERBS_IOCTL_
34#define _UVERBS_IOCTL_
35
36#include <rdma/uverbs_types.h>
Matan Barak35410302017-08-03 16:07:01 +030037#include <linux/uaccess.h>
38#include <rdma/rdma_user_ioctl.h>
Matan Barakd70724f2017-08-03 16:07:04 +030039#include <rdma/ib_user_ioctl_verbs.h>
Matan Barak1f7ff9d2018-03-19 15:02:33 +020040#include <rdma/ib_user_ioctl_cmds.h>
Matan Baraka0aa3092017-08-03 16:06:55 +030041
42/*
43 * =======================================
44 * Verbs action specifications
45 * =======================================
46 */
47
Matan Barakf43dbeb2017-08-03 16:06:56 +030048enum uverbs_attr_type {
49 UVERBS_ATTR_TYPE_NA,
Matan Barakfac96582017-08-03 16:06:57 +030050 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
Matan Barakf43dbeb2017-08-03 16:06:56 +030052 UVERBS_ATTR_TYPE_IDR,
53 UVERBS_ATTR_TYPE_FD,
Matan Barak494c5582018-03-28 09:27:42 +030054 UVERBS_ATTR_TYPE_ENUM_IN,
Matan Barakf43dbeb2017-08-03 16:06:56 +030055};
56
Matan Baraka0aa3092017-08-03 16:06:55 +030057enum uverbs_obj_access {
58 UVERBS_ACCESS_READ,
59 UVERBS_ACCESS_WRITE,
60 UVERBS_ACCESS_NEW,
61 UVERBS_ACCESS_DESTROY
62};
63
Matan Barak1f07e08f2018-03-19 15:02:35 +020064/* Specification of a single attribute inside the ioctl message */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030065/* good size 16 */
Matan Barakf43dbeb2017-08-03 16:06:56 +030066struct uverbs_attr_spec {
Jason Gunthorped108dac2018-07-04 08:50:25 +030067 u8 type;
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030068
69 /*
Jason Gunthorpe422e3d32018-07-04 08:50:31 +030070 * Support extending attributes by length. Allow the user to provide
71 * more bytes than ptr.len, but check that everything after is zero'd
72 * by the user.
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030073 */
Jason Gunthorpe422e3d32018-07-04 08:50:31 +030074 u8 zero_trailing:1;
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030075 /*
76 * Valid only for PTR_IN. Allocate and copy the data inside
77 * the parser
78 */
79 u8 alloc_and_copy:1;
80 u8 mandatory:1;
Jason Gunthorped108dac2018-07-04 08:50:25 +030081
Matan Barakfac96582017-08-03 16:06:57 +030082 union {
Matan Barakfac96582017-08-03 16:06:57 +030083 struct {
Matan Barakc66db312018-03-19 15:02:36 +020084 /* Current known size to kernel */
Jason Gunthorped108dac2018-07-04 08:50:25 +030085 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020086 /* User isn't allowed to provide something < min_len */
Jason Gunthorped108dac2018-07-04 08:50:25 +030087 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020088 } ptr;
Jason Gunthorped108dac2018-07-04 08:50:25 +030089
Matan Barak1f07e08f2018-03-19 15:02:35 +020090 struct {
Matan Barakfac96582017-08-03 16:06:57 +030091 /*
92 * higher bits mean the namespace and lower bits mean
93 * the type id within the namespace.
94 */
Jason Gunthorped108dac2018-07-04 08:50:25 +030095 u16 obj_type;
96 u8 access;
Matan Barakfac96582017-08-03 16:06:57 +030097 } obj;
Jason Gunthorped108dac2018-07-04 08:50:25 +030098
Matan Barak494c5582018-03-28 09:27:42 +030099 struct {
Jason Gunthorped108dac2018-07-04 08:50:25 +0300100 u8 num_elems;
101 } enum_def;
102 } u;
103
104 /* This weird split of the enum lets us remove some padding */
105 union {
106 struct {
Matan Barak494c5582018-03-28 09:27:42 +0300107 /*
108 * The enum attribute can select one of the attributes
109 * contained in the ids array. Currently only PTR_IN
110 * attributes are supported in the ids array.
111 */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300112 const struct uverbs_attr_spec *ids;
Matan Barak494c5582018-03-28 09:27:42 +0300113 } enum_def;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300114 } u2;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300115};
116
117struct uverbs_attr_spec_hash {
118 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +0300119 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300120 struct uverbs_attr_spec attrs[0];
121};
122
Matan Barakfac96582017-08-03 16:06:57 +0300123struct uverbs_attr_bundle;
124struct ib_uverbs_file;
125
Matan Barakfac96582017-08-03 16:06:57 +0300126struct uverbs_method_spec {
127 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
128 u32 flags;
129 size_t num_buckets;
130 size_t num_child_attrs;
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600131 int (*handler)(struct ib_uverbs_file *ufile,
Matan Barakfac96582017-08-03 16:06:57 +0300132 struct uverbs_attr_bundle *ctx);
133 struct uverbs_attr_spec_hash *attr_buckets[0];
134};
135
136struct uverbs_method_spec_hash {
137 size_t num_methods;
138 struct uverbs_method_spec *methods[0];
139};
140
141struct uverbs_object_spec {
142 const struct uverbs_obj_type *type_attrs;
143 size_t num_buckets;
144 struct uverbs_method_spec_hash *method_buckets[0];
145};
146
147struct uverbs_object_spec_hash {
148 size_t num_objects;
149 struct uverbs_object_spec *objects[0];
150};
151
152struct uverbs_root_spec {
153 size_t num_buckets;
154 struct uverbs_object_spec_hash *object_buckets[0];
155};
156
Matan Barak50090102017-08-03 16:06:58 +0300157/*
Jason Gunthorpe9ed3e5f2018-08-09 20:14:36 -0600158 * Information about the API is loaded into a radix tree. For IOCTL we start
159 * with a tuple of:
160 * object_id, attr_id, method_id
161 *
162 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
163 * on the current kernel support this is compressed into 16 bit key for the
164 * radix tree. Since this compression is entirely internal to the kernel the
165 * below limits can be revised if the kernel gains additional data.
166 *
167 * With 64 leafs per node this is a 3 level radix tree.
168 *
169 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
170 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
171 */
172enum uapi_radix_data {
173 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
174
175 UVERBS_API_ATTR_KEY_BITS = 6,
176 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
177 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
178
179 UVERBS_API_METHOD_KEY_BITS = 5,
180 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
181 UVERBS_API_METHOD_KEY_NUM_CORE = 24,
182 UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
183 UVERBS_API_METHOD_KEY_NUM_CORE,
184 UVERBS_API_METHOD_KEY_MASK = GENMASK(
185 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
186 UVERBS_API_METHOD_KEY_SHIFT),
187
188 UVERBS_API_OBJ_KEY_BITS = 5,
189 UVERBS_API_OBJ_KEY_SHIFT =
190 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
191 UVERBS_API_OBJ_KEY_NUM_CORE = 24,
192 UVERBS_API_OBJ_KEY_NUM_DRIVER =
193 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
194 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
195
196 /* This id guaranteed to not exist in the radix tree */
197 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
198};
199
200static inline __attribute_const__ u32 uapi_key_obj(u32 id)
201{
202 if (id & UVERBS_API_NS_FLAG) {
203 id &= ~UVERBS_API_NS_FLAG;
204 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
205 return UVERBS_API_KEY_ERR;
206 id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
207 } else {
208 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
209 return UVERBS_API_KEY_ERR;
210 }
211
212 return id << UVERBS_API_OBJ_KEY_SHIFT;
213}
214
215static inline __attribute_const__ bool uapi_key_is_object(u32 key)
216{
217 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
218}
219
220static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
221{
222 if (id & UVERBS_API_NS_FLAG) {
223 id &= ~UVERBS_API_NS_FLAG;
224 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
225 return UVERBS_API_KEY_ERR;
226 id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
227 } else {
228 id++;
229 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
230 return UVERBS_API_KEY_ERR;
231 }
232
233 return id << UVERBS_API_METHOD_KEY_SHIFT;
234}
235
236static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
237{
238 return attr_key &
239 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
240}
241
242static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
243{
244 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
245 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
246}
247
248static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
249{
250 /* 0 is the method slot itself */
251 return ioctl_method_key + 1;
252}
253
254static inline __attribute_const__ u32 uapi_key_attr(u32 id)
255{
256 /*
257 * The attr is designed to fit in the typical single radix tree node
258 * of 64 entries. Since allmost all methods have driver attributes we
259 * organize things so that the driver and core attributes interleave to
260 * reduce the length of the attributes array in typical cases.
261 */
262 if (id & UVERBS_API_NS_FLAG) {
263 id &= ~UVERBS_API_NS_FLAG;
264 id++;
265 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
266 return UVERBS_API_KEY_ERR;
267 id = (id << 1) | 0;
268 } else {
269 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
270 return UVERBS_API_KEY_ERR;
271 id = (id << 1) | 1;
272 }
273
274 return id;
275}
276
277static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
278{
279 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
280 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
281}
282
283/*
284 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
285 * basically it undoes the reservation of 0 in the ID numbering. attr_key
286 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
287 * uapi_key_attr().
288 */
289static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
290{
291 return attr_key - 1;
292}
293
294/*
Matan Barak50090102017-08-03 16:06:58 +0300295 * =======================================
296 * Verbs definitions
297 * =======================================
298 */
299
Matan Barak09e3ebf2017-08-03 16:06:59 +0300300struct uverbs_attr_def {
301 u16 id;
302 struct uverbs_attr_spec attr;
303};
304
305struct uverbs_method_def {
306 u16 id;
307 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
308 u32 flags;
309 size_t num_attrs;
310 const struct uverbs_attr_def * const (*attrs)[];
Jason Gunthorpee83f0ec2018-07-25 21:40:18 -0600311 int (*handler)(struct ib_uverbs_file *ufile,
Matan Barak09e3ebf2017-08-03 16:06:59 +0300312 struct uverbs_attr_bundle *ctx);
313};
314
Matan Barak50090102017-08-03 16:06:58 +0300315struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300316 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300317 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300318 size_t num_methods;
319 const struct uverbs_method_def * const (*methods)[];
320};
321
322struct uverbs_object_tree_def {
323 size_t num_objects;
324 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300325};
326
Jason Gunthorped108dac2018-07-04 08:50:25 +0300327/*
328 * =======================================
329 * Attribute Specifications
330 * =======================================
331 */
Matan Barakc66db312018-03-19 15:02:36 +0200332
Matan Barakc66db312018-03-19 15:02:36 +0200333#define UVERBS_ATTR_SIZE(_min_len, _len) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300334 .u.ptr.min_len = _min_len, .u.ptr.len = _len
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300335
Yishai Hadasfd44e382018-07-23 15:25:07 +0300336#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
337
Jason Gunthorpe422e3d32018-07-04 08:50:31 +0300338/*
339 * Specifies a uapi structure that cannot be extended. The user must always
340 * supply the whole structure and nothing more. The structure must be declared
341 * in a header under include/uapi/rdma.
342 */
343#define UVERBS_ATTR_TYPE(_type) \
344 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
345/*
346 * Specifies a uapi structure where the user must provide at least up to
347 * member 'last'. Anything after last and up until the end of the structure
348 * can be non-zero, anything longer than the end of the structure must be
349 * zero. The structure must be declared in a header under include/uapi/rdma.
350 */
351#define UVERBS_ATTR_STRUCT(_type, _last) \
352 .zero_trailing = 1, \
353 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
354 sizeof(_type))
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300355/*
356 * Specifies at least min_len bytes must be passed in, but the amount can be
357 * larger, up to the protocol maximum size. No check for zeroing is done.
358 */
359#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
Matan Barakc66db312018-03-19 15:02:36 +0200360
Jason Gunthorped108dac2018-07-04 08:50:25 +0300361/* Must be used in the '...' of any UVERBS_ATTR */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300362#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
363#define UA_MANDATORY .mandatory = 1
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300364#define UA_OPTIONAL .mandatory = 0
Jason Gunthorped108dac2018-07-04 08:50:25 +0300365
366#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300367 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300368 .id = _attr_id, \
369 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
370 .u.obj.obj_type = _idr_type, \
371 .u.obj.access = _access, \
372 __VA_ARGS__ } })
373
374#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300375 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300376 .id = (_attr_id) + \
377 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
378 (_access) != UVERBS_ACCESS_READ), \
379 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
380 .u.obj.obj_type = _fd_type, \
381 .u.obj.access = _access, \
382 __VA_ARGS__ } })
383
384#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300385 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300386 .id = _attr_id, \
387 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
388 _type, \
389 __VA_ARGS__ } })
390
391#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300392 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300393 .id = _attr_id, \
394 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
395 _type, \
396 __VA_ARGS__ } })
397
398/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
399#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300400 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300401 .id = _attr_id, \
402 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
403 .u2.enum_def.ids = _enum_arr, \
404 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
405 __VA_ARGS__ }, \
406 })
Matan Barak35410302017-08-03 16:07:01 +0300407
408/*
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600409 * An input value that is a bitwise combination of values of _enum_type.
410 * This permits the flag value to be passed as either a u32 or u64, it must
411 * be retrieved via uverbs_get_flag().
412 */
413#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
414 UVERBS_ATTR_PTR_IN( \
415 _attr_id, \
416 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
417 !sizeof(_enum_type *)), \
418 sizeof(u64)), \
419 __VA_ARGS__)
420
421/*
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300422 * This spec is used in order to pass information to the hardware driver in a
423 * legacy way. Every verb that could get driver specific data should get this
424 * spec.
425 */
426#define UVERBS_ATTR_UHW() \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300427 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300428 UVERBS_ATTR_MIN_SIZE(0), \
429 UA_OPTIONAL), \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300430 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300431 UVERBS_ATTR_MIN_SIZE(0), \
432 UA_OPTIONAL)
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300433
434/*
Jason Gunthorped108dac2018-07-04 08:50:25 +0300435 * =======================================
436 * Declaration helpers
437 * =======================================
Matan Barak35410302017-08-03 16:07:01 +0300438 */
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300439
440#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
441 static const struct uverbs_object_def *const _name##_ptr[] = { \
442 __VA_ARGS__, \
443 }; \
444 static const struct uverbs_object_tree_def _name = { \
445 .num_objects = ARRAY_SIZE(_name##_ptr), \
446 .objects = &_name##_ptr, \
447 }
Matan Barak09e3ebf2017-08-03 16:06:59 +0300448
Matan Barakfac96582017-08-03 16:06:57 +0300449/* =================================================
450 * Parsing infrastructure
451 * =================================================
452 */
453
454struct uverbs_ptr_attr {
Matan Barak8762d142018-06-17 12:59:52 +0300455 /*
456 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
457 * used.
458 */
459 union {
460 void *ptr;
461 u64 data;
462 };
Matan Barakfac96582017-08-03 16:06:57 +0300463 u16 len;
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600464 u16 uattr_idx;
Matan Barak494c5582018-03-28 09:27:42 +0300465 u8 enum_id;
Matan Barakfac96582017-08-03 16:06:57 +0300466};
467
Matan Barakf43dbeb2017-08-03 16:06:56 +0300468struct uverbs_obj_attr {
469 struct ib_uobject *uobject;
470};
471
472struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300473 union {
474 struct uverbs_ptr_attr ptr_attr;
475 struct uverbs_obj_attr obj_attr;
476 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300477};
478
479struct uverbs_attr_bundle_hash {
480 /* if bit i is set, it means attrs[i] contains valid information */
481 unsigned long *valid_bitmap;
482 size_t num_attrs;
483 /*
484 * arrays of attributes, each element corresponds to the specification
485 * of the attribute in the same index.
486 */
487 struct uverbs_attr *attrs;
488};
489
490struct uverbs_attr_bundle {
Jason Gunthorpe4b3dd2b2018-08-09 20:14:38 -0600491 struct ib_uverbs_file *ufile;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300492 size_t num_buckets;
493 struct uverbs_attr_bundle_hash hash[];
494};
495
496static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
497 unsigned int idx)
498{
499 return test_bit(idx, attrs_hash->valid_bitmap);
500}
501
Matan Barak35410302017-08-03 16:07:01 +0300502static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
503 unsigned int idx)
504{
505 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
506
507 if (attrs_bundle->num_buckets <= idx_bucket)
508 return false;
509
510 return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
511 idx & ~UVERBS_ID_NS_MASK);
512}
513
Matan Barak41b2a712018-03-19 15:02:38 +0200514#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
515
Matan Barakd70724f2017-08-03 16:07:04 +0300516static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
517 u16 idx)
518{
519 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
520
521 if (!uverbs_attr_is_valid(attrs_bundle, idx))
522 return ERR_PTR(-ENOENT);
523
524 return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
525}
526
Matan Barak494c5582018-03-28 09:27:42 +0300527static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
528 u16 idx)
529{
530 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
531
532 if (IS_ERR(attr))
533 return PTR_ERR(attr);
534
535 return attr->ptr_attr.enum_id;
536}
537
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300538static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
539 u16 idx)
540{
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600541 const struct uverbs_attr *attr;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300542
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600543 attr = uverbs_attr_get(attrs_bundle, idx);
544 if (IS_ERR(attr))
545 return ERR_CAST(attr);
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300546
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600547 return attr->obj_attr.uobject->object;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300548}
549
Matan Barak3efa3812018-05-31 16:43:28 +0300550static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
551 u16 idx)
552{
553 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
554
555 if (IS_ERR(attr))
556 return ERR_CAST(attr);
557
558 return attr->obj_attr.uobject;
559}
560
Matan Barak8762d142018-06-17 12:59:52 +0300561static inline int
562uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
563{
564 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
565
566 if (IS_ERR(attr))
567 return PTR_ERR(attr);
568
569 return attr->ptr_attr.len;
570}
571
Matan Barak89d9e8d2018-02-13 12:18:29 +0200572static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
573{
574 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
575}
576
Matan Barak8762d142018-06-17 12:59:52 +0300577static inline void *uverbs_attr_get_alloced_ptr(
578 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
579{
580 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
581
582 if (IS_ERR(attr))
583 return (void *)attr;
584
585 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
586 attr->ptr_attr.ptr;
587}
588
Matan Barak89d9e8d2018-02-13 12:18:29 +0200589static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300590 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200591 size_t idx,
592 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300593{
594 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
595
596 if (IS_ERR(attr))
597 return PTR_ERR(attr);
598
Matan Barak89d9e8d2018-02-13 12:18:29 +0200599 /*
600 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200601 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
602 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200603 */
604 if (unlikely(size < attr->ptr_attr.len))
605 return -EINVAL;
606
607 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300608 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200609 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
610 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300611 return -EFAULT;
612
613 return 0;
614}
615
Matan Barakc66db312018-03-19 15:02:36 +0200616static inline int _uverbs_copy_from_or_zero(void *to,
617 const struct uverbs_attr_bundle *attrs_bundle,
618 size_t idx,
619 size_t size)
620{
621 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
622 size_t min_size;
623
624 if (IS_ERR(attr))
625 return PTR_ERR(attr);
626
627 min_size = min_t(size_t, size, attr->ptr_attr.len);
628
629 if (uverbs_attr_ptr_is_inline(attr))
630 memcpy(to, &attr->ptr_attr.data, min_size);
631 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
632 min_size))
633 return -EFAULT;
634
635 if (size > min_size)
636 memset(to + min_size, 0, size - min_size);
637
638 return 0;
639}
640
Matan Barakd70724f2017-08-03 16:07:04 +0300641#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200642 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300643
Matan Barakc66db312018-03-19 15:02:36 +0200644#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
645 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
646
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600647#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
648int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
649 size_t idx, u64 allowed_bits);
650int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
651 size_t idx, u64 allowed_bits);
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600652int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
653 const void *from, size_t size);
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600654__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
655 gfp_t flags);
656
657static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
658 size_t size)
659{
660 return _uverbs_alloc(bundle, size, GFP_KERNEL);
661}
662
663static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
664 size_t size)
665{
666 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
667}
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600668#else
669static inline int
670uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
671 size_t idx, u64 allowed_bits)
672{
673 return -EINVAL;
674}
675static inline int
676uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
677 size_t idx, u64 allowed_bits)
678{
679 return -EINVAL;
680}
Jason Gunthorpe6a1f4442018-08-09 20:14:39 -0600681static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
682 size_t idx, const void *from, size_t size)
683{
684 return -EINVAL;
685}
Jason Gunthorpe461bb2e2018-08-09 20:14:40 -0600686static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
687 size_t size)
688{
689 return ERR_PTR(-EINVAL);
690}
691static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
692 size_t size)
693{
694 return ERR_PTR(-EINVAL);
695}
Jason Gunthorpebccd0622018-07-26 16:37:14 -0600696#endif
697
Matan Barak118620d2017-08-03 16:07:00 +0300698/* =================================================
699 * Definitions -> Specs infrastructure
700 * =================================================
701 */
Matan Baraka0aa3092017-08-03 16:06:55 +0300702
Matan Barak118620d2017-08-03 16:07:00 +0300703/*
704 * uverbs_alloc_spec_tree - Merges different common and driver specific feature
705 * into one parsing tree that every uverbs command will be parsed upon.
706 *
707 * @num_trees: Number of trees in the array @trees.
708 * @trees: Array of pointers to tree root definitions to merge. Each such tree
709 * possibly contains objects, methods and attributes definitions.
710 *
711 * Returns:
712 * uverbs_root_spec *: The root of the merged parsing tree.
713 * On error, we return an error code. Error is checked via IS_ERR.
714 *
715 * The following merges could take place:
716 * a. Two trees representing the same method with different handler
717 * -> We take the handler of the tree that its handler != NULL
718 * and its index in the trees array is greater. The incentive for that
719 * is that developers are expected to first merge common trees and then
720 * merge trees that gives specialized the behaviour.
721 * b. Two trees representing the same object with different
722 * type_attrs (struct uverbs_obj_type):
723 * -> We take the type_attrs of the tree that its type_attr != NULL
724 * and its index in the trees array is greater. This could be used
725 * in order to override the free function, allocation size, etc.
726 * c. Two trees representing the same method attribute (same id but possibly
727 * different attributes):
728 * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
729 *
730 * An object without any methods is considered invalid and will abort the
731 * function with -ENOENT error.
732 */
Matan Barak52427112017-08-03 16:07:06 +0300733#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Matan Barak118620d2017-08-03 16:07:00 +0300734struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
735 const struct uverbs_object_tree_def **trees);
736void uverbs_free_spec_tree(struct uverbs_root_spec *root);
Matan Barak52427112017-08-03 16:07:06 +0300737#else
738static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
739 const struct uverbs_object_tree_def **trees)
740{
741 return NULL;
742}
743
744static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
745{
746}
747#endif
Matan Barak118620d2017-08-03 16:07:00 +0300748
749#endif