2 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #ifndef _UVERBS_IOCTL_
34 #define _UVERBS_IOCTL_
36 #include <rdma/uverbs_types.h>
37 #include <linux/uaccess.h>
38 #include <rdma/rdma_user_ioctl.h>
39 #include <rdma/ib_user_ioctl_verbs.h>
40 #include <rdma/ib_user_ioctl_cmds.h>
43 * =======================================
44 * Verbs action specifications
45 * =======================================
48 enum uverbs_attr_type {
50 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
54 UVERBS_ATTR_TYPE_ENUM_IN,
57 enum uverbs_obj_access {
64 /* Specification of a single attribute inside the ioctl message */
66 struct uverbs_attr_spec {
70 * Support extending attributes by length. Allow the user to provide
71 * more bytes than ptr.len, but check that everything after is zero'd
76 * Valid only for PTR_IN. Allocate and copy the data inside
84 /* Current known size to kernel */
86 /* User isn't allowed to provide something < min_len */
92 * higher bits mean the namespace and lower bits mean
93 * the type id within the namespace.
104 /* This weird split of the enum lets us remove some padding */
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.
112 const struct uverbs_attr_spec *ids;
118 * Information about the API is loaded into a radix tree. For IOCTL we start
120 * object_id, attr_id, method_id
122 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
123 * on the current kernel support this is compressed into 16 bit key for the
124 * radix tree. Since this compression is entirely internal to the kernel the
125 * below limits can be revised if the kernel gains additional data.
127 * With 64 leafs per node this is a 3 level radix tree.
129 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
130 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
132 enum uapi_radix_data {
133 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
135 UVERBS_API_ATTR_KEY_BITS = 6,
136 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
137 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
139 UVERBS_API_METHOD_KEY_BITS = 5,
140 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
141 UVERBS_API_METHOD_KEY_NUM_CORE = 24,
142 UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) -
143 UVERBS_API_METHOD_KEY_NUM_CORE,
144 UVERBS_API_METHOD_KEY_MASK = GENMASK(
145 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
146 UVERBS_API_METHOD_KEY_SHIFT),
148 UVERBS_API_OBJ_KEY_BITS = 5,
149 UVERBS_API_OBJ_KEY_SHIFT =
150 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
151 UVERBS_API_OBJ_KEY_NUM_CORE = 24,
152 UVERBS_API_OBJ_KEY_NUM_DRIVER =
153 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
154 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
156 /* This id guaranteed to not exist in the radix tree */
157 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
160 static inline __attribute_const__ u32 uapi_key_obj(u32 id)
162 if (id & UVERBS_API_NS_FLAG) {
163 id &= ~UVERBS_API_NS_FLAG;
164 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
165 return UVERBS_API_KEY_ERR;
166 id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
168 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
169 return UVERBS_API_KEY_ERR;
172 return id << UVERBS_API_OBJ_KEY_SHIFT;
175 static inline __attribute_const__ bool uapi_key_is_object(u32 key)
177 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
180 static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
182 if (id & UVERBS_API_NS_FLAG) {
183 id &= ~UVERBS_API_NS_FLAG;
184 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
185 return UVERBS_API_KEY_ERR;
186 id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
189 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
190 return UVERBS_API_KEY_ERR;
193 return id << UVERBS_API_METHOD_KEY_SHIFT;
196 static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
199 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
202 static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
204 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
205 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
208 static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
210 /* 0 is the method slot itself */
211 return ioctl_method_key + 1;
214 static inline __attribute_const__ u32 uapi_key_attr(u32 id)
217 * The attr is designed to fit in the typical single radix tree node
218 * of 64 entries. Since allmost all methods have driver attributes we
219 * organize things so that the driver and core attributes interleave to
220 * reduce the length of the attributes array in typical cases.
222 if (id & UVERBS_API_NS_FLAG) {
223 id &= ~UVERBS_API_NS_FLAG;
225 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
226 return UVERBS_API_KEY_ERR;
229 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
230 return UVERBS_API_KEY_ERR;
237 static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
239 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
240 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
244 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
245 * basically it undoes the reservation of 0 in the ID numbering. attr_key
246 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
249 static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
255 * =======================================
257 * =======================================
260 struct uverbs_attr_def {
262 struct uverbs_attr_spec attr;
265 struct uverbs_method_def {
267 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
270 const struct uverbs_attr_def * const (*attrs)[];
271 int (*handler)(struct ib_uverbs_file *ufile,
272 struct uverbs_attr_bundle *ctx);
275 struct uverbs_object_def {
277 const struct uverbs_obj_type *type_attrs;
279 const struct uverbs_method_def * const (*methods)[];
282 struct uverbs_object_tree_def {
284 const struct uverbs_object_def * const (*objects)[];
288 * =======================================
289 * Attribute Specifications
290 * =======================================
293 #define UVERBS_ATTR_SIZE(_min_len, _len) \
294 .u.ptr.min_len = _min_len, .u.ptr.len = _len
296 #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
299 * Specifies a uapi structure that cannot be extended. The user must always
300 * supply the whole structure and nothing more. The structure must be declared
301 * in a header under include/uapi/rdma.
303 #define UVERBS_ATTR_TYPE(_type) \
304 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
306 * Specifies a uapi structure where the user must provide at least up to
307 * member 'last'. Anything after last and up until the end of the structure
308 * can be non-zero, anything longer than the end of the structure must be
309 * zero. The structure must be declared in a header under include/uapi/rdma.
311 #define UVERBS_ATTR_STRUCT(_type, _last) \
312 .zero_trailing = 1, \
313 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
316 * Specifies at least min_len bytes must be passed in, but the amount can be
317 * larger, up to the protocol maximum size. No check for zeroing is done.
319 #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
321 /* Must be used in the '...' of any UVERBS_ATTR */
322 #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
323 #define UA_MANDATORY .mandatory = 1
324 #define UA_OPTIONAL .mandatory = 0
326 #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
327 (&(const struct uverbs_attr_def){ \
329 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
330 .u.obj.obj_type = _idr_type, \
331 .u.obj.access = _access, \
334 #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
335 (&(const struct uverbs_attr_def){ \
337 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
338 (_access) != UVERBS_ACCESS_READ), \
339 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
340 .u.obj.obj_type = _fd_type, \
341 .u.obj.access = _access, \
344 #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
345 (&(const struct uverbs_attr_def){ \
347 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
351 #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
352 (&(const struct uverbs_attr_def){ \
354 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
358 /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
359 #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
360 (&(const struct uverbs_attr_def){ \
362 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
363 .u2.enum_def.ids = _enum_arr, \
364 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
369 * An input value that is a bitwise combination of values of _enum_type.
370 * This permits the flag value to be passed as either a u32 or u64, it must
371 * be retrieved via uverbs_get_flag().
373 #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
374 UVERBS_ATTR_PTR_IN( \
376 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
377 !sizeof(_enum_type *)), \
382 * This spec is used in order to pass information to the hardware driver in a
383 * legacy way. Every verb that could get driver specific data should get this
386 #define UVERBS_ATTR_UHW() \
387 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
388 UVERBS_ATTR_MIN_SIZE(0), \
390 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
391 UVERBS_ATTR_MIN_SIZE(0), \
395 * =======================================
396 * Declaration helpers
397 * =======================================
400 #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
401 static const struct uverbs_object_def *const _name##_ptr[] = { \
404 static const struct uverbs_object_tree_def _name = { \
405 .num_objects = ARRAY_SIZE(_name##_ptr), \
406 .objects = &_name##_ptr, \
409 /* =================================================
410 * Parsing infrastructure
411 * =================================================
415 struct uverbs_ptr_attr {
417 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
429 struct uverbs_obj_attr {
430 struct ib_uobject *uobject;
431 const struct uverbs_api_attr *attr_elm;
436 struct uverbs_ptr_attr ptr_attr;
437 struct uverbs_obj_attr obj_attr;
441 struct uverbs_attr_bundle {
442 struct ib_uverbs_file *ufile;
443 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
444 struct uverbs_attr attrs[];
447 static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
450 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
451 attrs_bundle->attr_present);
454 #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
456 static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
459 if (!uverbs_attr_is_valid(attrs_bundle, idx))
460 return ERR_PTR(-ENOENT);
462 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
465 static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
468 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
471 return PTR_ERR(attr);
473 return attr->ptr_attr.enum_id;
476 static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
479 const struct uverbs_attr *attr;
481 attr = uverbs_attr_get(attrs_bundle, idx);
483 return ERR_CAST(attr);
485 return attr->obj_attr.uobject->object;
488 static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
491 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
494 return ERR_CAST(attr);
496 return attr->obj_attr.uobject;
500 uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
502 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
505 return PTR_ERR(attr);
507 return attr->ptr_attr.len;
510 static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
512 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
515 static inline void *uverbs_attr_get_alloced_ptr(
516 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
518 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
523 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
527 static inline int _uverbs_copy_from(void *to,
528 const struct uverbs_attr_bundle *attrs_bundle,
532 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
535 return PTR_ERR(attr);
538 * Validation ensures attr->ptr_attr.len >= size. If the caller is
539 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
540 * uverbs_copy_from_or_zero.
542 if (unlikely(size < attr->ptr_attr.len))
545 if (uverbs_attr_ptr_is_inline(attr))
546 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
547 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
554 static inline int _uverbs_copy_from_or_zero(void *to,
555 const struct uverbs_attr_bundle *attrs_bundle,
559 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
563 return PTR_ERR(attr);
565 min_size = min_t(size_t, size, attr->ptr_attr.len);
567 if (uverbs_attr_ptr_is_inline(attr))
568 memcpy(to, &attr->ptr_attr.data, min_size);
569 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
574 memset(to + min_size, 0, size - min_size);
579 #define uverbs_copy_from(to, attrs_bundle, idx) \
580 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
582 #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
583 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
585 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
586 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
587 size_t idx, u64 allowed_bits);
588 int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
589 size_t idx, u64 allowed_bits);
590 int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
591 const void *from, size_t size);
592 __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
595 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
598 return _uverbs_alloc(bundle, size, GFP_KERNEL);
601 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
604 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
608 uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
609 size_t idx, u64 allowed_bits)
614 uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
615 size_t idx, u64 allowed_bits)
619 static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
620 size_t idx, const void *from, size_t size)
624 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
627 return ERR_PTR(-EINVAL);
629 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
632 return ERR_PTR(-EINVAL);