1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
9 #include <rdma/uverbs_types.h>
10 #include <linux/uaccess.h>
11 #include <rdma/rdma_user_ioctl.h>
12 #include <rdma/ib_user_ioctl_verbs.h>
13 #include <rdma/ib_user_ioctl_cmds.h>
16 * =======================================
17 * Verbs action specifications
18 * =======================================
21 enum uverbs_attr_type {
23 UVERBS_ATTR_TYPE_PTR_IN,
24 UVERBS_ATTR_TYPE_PTR_OUT,
27 UVERBS_ATTR_TYPE_RAW_FD,
28 UVERBS_ATTR_TYPE_ENUM_IN,
29 UVERBS_ATTR_TYPE_IDRS_ARRAY,
32 enum uverbs_obj_access {
39 /* Specification of a single attribute inside the ioctl message */
41 struct uverbs_attr_spec {
45 * Support extending attributes by length. Allow the user to provide
46 * more bytes than ptr.len, but check that everything after is zero'd
51 * Valid only for PTR_IN. Allocate and copy the data inside
56 /* True if this is from UVERBS_ATTR_UHW */
61 /* Current known size to kernel */
63 /* User isn't allowed to provide something < min_len */
69 * higher bits mean the namespace and lower bits mean
70 * the type id within the namespace.
81 /* This weird split lets us remove some padding */
85 * The enum attribute can select one of the attributes
86 * contained in the ids array. Currently only PTR_IN
87 * attributes are supported in the ids array.
89 const struct uverbs_attr_spec *ids;
94 * higher bits mean the namespace and lower bits mean
95 * the type id within the namespace.
106 * Information about the API is loaded into a radix tree. For IOCTL we start
108 * object_id, attr_id, method_id
110 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
111 * on the current kernel support this is compressed into 16 bit key for the
112 * radix tree. Since this compression is entirely internal to the kernel the
113 * below limits can be revised if the kernel gains additional data.
115 * With 64 leafs per node this is a 3 level radix tree.
117 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
118 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
120 * This also encodes the tables for the write() and write() extended commands
122 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
123 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
124 * ie the WRITE path is treated as a special method type in the ioctl
127 enum uapi_radix_data {
128 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
130 UVERBS_API_ATTR_KEY_BITS = 6,
131 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
132 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
133 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
135 UVERBS_API_METHOD_KEY_BITS = 5,
136 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
137 UVERBS_API_METHOD_KEY_NUM_CORE = 22,
138 UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
139 UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
140 UVERBS_API_METHOD_KEY_NUM_DRIVER =
141 (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
142 UVERBS_API_METHOD_KEY_NUM_CORE,
143 UVERBS_API_METHOD_KEY_MASK = GENMASK(
144 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
145 UVERBS_API_METHOD_KEY_SHIFT),
147 UVERBS_API_OBJ_KEY_BITS = 5,
148 UVERBS_API_OBJ_KEY_SHIFT =
149 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
150 UVERBS_API_OBJ_KEY_NUM_CORE = 20,
151 UVERBS_API_OBJ_KEY_NUM_DRIVER =
152 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
153 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
155 /* This id guaranteed to not exist in the radix tree */
156 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
159 static inline __attribute_const__ u32 uapi_key_obj(u32 id)
161 if (id & UVERBS_API_NS_FLAG) {
162 id &= ~UVERBS_API_NS_FLAG;
163 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
164 return UVERBS_API_KEY_ERR;
165 id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
167 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
168 return UVERBS_API_KEY_ERR;
171 return id << UVERBS_API_OBJ_KEY_SHIFT;
174 static inline __attribute_const__ bool uapi_key_is_object(u32 key)
176 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
179 static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
181 if (id & UVERBS_API_NS_FLAG) {
182 id &= ~UVERBS_API_NS_FLAG;
183 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
184 return UVERBS_API_KEY_ERR;
185 id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
188 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
189 return UVERBS_API_KEY_ERR;
192 return id << UVERBS_API_METHOD_KEY_SHIFT;
195 static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
197 if (id >= UVERBS_API_WRITE_KEY_NUM)
198 return UVERBS_API_KEY_ERR;
199 return UVERBS_API_METHOD_IS_WRITE | id;
202 static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
204 if (id >= UVERBS_API_WRITE_KEY_NUM)
205 return UVERBS_API_KEY_ERR;
206 return UVERBS_API_METHOD_IS_WRITE_EX | id;
209 static inline __attribute_const__ u32
210 uapi_key_attr_to_ioctl_method(u32 attr_key)
213 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
216 static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
218 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
220 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
221 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
224 static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
226 return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
229 static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
231 return (key & UVERBS_API_METHOD_KEY_MASK) ==
232 UVERBS_API_METHOD_IS_WRITE_EX;
235 static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
237 /* 0 is the method slot itself */
238 return ioctl_method_key + 1;
241 static inline __attribute_const__ u32 uapi_key_attr(u32 id)
244 * The attr is designed to fit in the typical single radix tree node
245 * of 64 entries. Since allmost all methods have driver attributes we
246 * organize things so that the driver and core attributes interleave to
247 * reduce the length of the attributes array in typical cases.
249 if (id & UVERBS_API_NS_FLAG) {
250 id &= ~UVERBS_API_NS_FLAG;
252 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
253 return UVERBS_API_KEY_ERR;
256 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
257 return UVERBS_API_KEY_ERR;
264 /* Only true for ioctl methods */
265 static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
267 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
269 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
270 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
274 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
275 * basically it undoes the reservation of 0 in the ID numbering. attr_key
276 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
279 static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
284 static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
286 return attr_bkey + 1;
290 * =======================================
292 * =======================================
295 struct uverbs_attr_def {
297 struct uverbs_attr_spec attr;
300 struct uverbs_method_def {
302 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
305 const struct uverbs_attr_def * const (*attrs)[];
306 int (*handler)(struct uverbs_attr_bundle *attrs);
309 struct uverbs_object_def {
311 const struct uverbs_obj_type *type_attrs;
313 const struct uverbs_method_def * const (*methods)[];
316 enum uapi_definition_kind {
318 UAPI_DEF_OBJECT_START,
320 UAPI_DEF_CHAIN_OBJ_TREE,
322 UAPI_DEF_IS_SUPPORTED_FUNC,
323 UAPI_DEF_IS_SUPPORTED_DEV_FN,
326 enum uapi_definition_scope {
327 UAPI_SCOPE_OBJECT = 1,
328 UAPI_SCOPE_METHOD = 2,
331 struct uapi_definition {
349 bool (*func_is_supported)(struct ib_device *device);
350 int (*func_write)(struct uverbs_attr_bundle *attrs);
351 const struct uapi_definition *chain;
352 const struct uverbs_object_def *chain_obj_tree;
353 size_t needs_fn_offset;
357 /* Define things connected to object_id */
358 #define DECLARE_UVERBS_OBJECT(_object_id, ...) \
360 .kind = UAPI_DEF_OBJECT_START, \
361 .object_start = { .object_id = _object_id }, \
365 /* Use in a var_args of DECLARE_UVERBS_OBJECT */
366 #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
368 .kind = UAPI_DEF_WRITE, \
369 .scope = UAPI_SCOPE_OBJECT, \
370 .write = { .is_ex = 0, .command_num = _command_num }, \
371 .func_write = _func, \
376 /* Use in a var_args of DECLARE_UVERBS_OBJECT */
377 #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
379 .kind = UAPI_DEF_WRITE, \
380 .scope = UAPI_SCOPE_OBJECT, \
381 .write = { .is_ex = 1, .command_num = _command_num }, \
382 .func_write = _func, \
388 * Object is only supported if the function pointer named ibdev_fn in struct
389 * ib_device is not NULL.
391 #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
393 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
394 .scope = UAPI_SCOPE_OBJECT, \
396 offsetof(struct ib_device_ops, ibdev_fn) + \
397 BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
403 * Method is only supported if the function pointer named ibdev_fn in struct
404 * ib_device is not NULL.
406 #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
408 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
409 .scope = UAPI_SCOPE_METHOD, \
411 offsetof(struct ib_device_ops, ibdev_fn) + \
412 BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
417 /* Call a function to determine if the entire object is supported or not */
418 #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
420 .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
421 .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
424 /* Include another struct uapi_definition in this one */
425 #define UAPI_DEF_CHAIN(_def_var) \
427 .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
430 /* Temporary until the tree base description is replaced */
431 #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
433 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
434 .object_start = { .object_id = _object_enum }, \
435 .chain_obj_tree = _object_ptr, \
438 #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
439 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, \
440 PTR_IF(IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS), \
441 &UVERBS_OBJECT(_object_enum)), \
445 * =======================================
446 * Attribute Specifications
447 * =======================================
450 #define UVERBS_ATTR_SIZE(_min_len, _len) \
451 .u.ptr.min_len = _min_len, .u.ptr.len = _len
453 #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
456 * Specifies a uapi structure that cannot be extended. The user must always
457 * supply the whole structure and nothing more. The structure must be declared
458 * in a header under include/uapi/rdma.
460 #define UVERBS_ATTR_TYPE(_type) \
461 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
463 * Specifies a uapi structure where the user must provide at least up to
464 * member 'last'. Anything after last and up until the end of the structure
465 * can be non-zero, anything longer than the end of the structure must be
466 * zero. The structure must be declared in a header under include/uapi/rdma.
468 #define UVERBS_ATTR_STRUCT(_type, _last) \
469 .zero_trailing = 1, \
470 UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
472 * Specifies at least min_len bytes must be passed in, but the amount can be
473 * larger, up to the protocol maximum size. No check for zeroing is done.
475 #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
477 /* Must be used in the '...' of any UVERBS_ATTR */
478 #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
479 #define UA_MANDATORY .mandatory = 1
480 #define UA_OPTIONAL .mandatory = 0
483 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
484 * READ\WRITE accesses are supported.
486 #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
488 (&(const struct uverbs_attr_def){ \
490 BUILD_BUG_ON_ZERO((_min_len) == 0 || \
492 PAGE_SIZE / sizeof(void *) || \
493 (_min_len) > (_max_len) || \
494 (_access) == UVERBS_ACCESS_NEW || \
495 (_access) == UVERBS_ACCESS_DESTROY), \
496 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
497 .u2.objs_arr.obj_type = _idr_type, \
498 .u2.objs_arr.access = _access, \
499 .u2.objs_arr.min_len = _min_len, \
500 .u2.objs_arr.max_len = _max_len, \
504 * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
505 * the user must validate the type of the uobject instead.
507 #define UVERBS_IDR_ANY_OBJECT 0xFFFF
509 #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
510 (&(const struct uverbs_attr_def){ \
512 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
513 .u.obj.obj_type = _idr_type, \
514 .u.obj.access = _access, \
517 #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
518 (&(const struct uverbs_attr_def){ \
520 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
521 (_access) != UVERBS_ACCESS_READ), \
522 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
523 .u.obj.obj_type = _fd_type, \
524 .u.obj.access = _access, \
527 #define UVERBS_ATTR_RAW_FD(_attr_id, ...) \
528 (&(const struct uverbs_attr_def){ \
530 .attr = { .type = UVERBS_ATTR_TYPE_RAW_FD, __VA_ARGS__ } })
532 #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
533 (&(const struct uverbs_attr_def){ \
535 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
539 #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
540 (&(const struct uverbs_attr_def){ \
542 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
546 /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
547 #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
548 (&(const struct uverbs_attr_def){ \
550 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
551 .u2.enum_def.ids = _enum_arr, \
552 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
556 /* An input value that is a member in the enum _enum_type. */
557 #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
558 UVERBS_ATTR_PTR_IN( \
561 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
566 * An input value that is a bitwise combination of values of _enum_type.
567 * This permits the flag value to be passed as either a u32 or u64, it must
568 * be retrieved via uverbs_get_flag().
570 #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
571 UVERBS_ATTR_PTR_IN( \
573 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
574 !sizeof(_enum_type *)), \
579 * This spec is used in order to pass information to the hardware driver in a
580 * legacy way. Every verb that could get driver specific data should get this
583 #define UVERBS_ATTR_UHW() \
584 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
585 UVERBS_ATTR_MIN_SIZE(0), \
588 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
589 UVERBS_ATTR_MIN_SIZE(0), \
593 /* =================================================
594 * Parsing infrastructure
595 * =================================================
599 struct uverbs_ptr_attr {
601 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
613 struct uverbs_obj_attr {
614 struct ib_uobject *uobject;
615 const struct uverbs_api_attr *attr_elm;
618 struct uverbs_objs_arr_attr {
619 struct ib_uobject **uobjects;
625 struct uverbs_ptr_attr ptr_attr;
626 struct uverbs_obj_attr obj_attr;
627 struct uverbs_objs_arr_attr objs_arr_attr;
631 struct uverbs_attr_bundle {
632 struct ib_udata driver_udata;
633 struct ib_udata ucore;
634 struct ib_uverbs_file *ufile;
635 struct ib_ucontext *context;
636 struct ib_uobject *uobject;
637 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
638 struct uverbs_attr attrs[];
641 static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
644 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
645 attrs_bundle->attr_present);
649 * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
650 * ib_udata which is embedded in uverbs_attr_bundle.
652 * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
653 * in a NULL ucontext pointer, as a safety precaution. Callers should be using
654 * 'udata' to determine if the driver call is in user or kernel mode, not
658 static inline struct uverbs_attr_bundle *
659 rdma_udata_to_uverbs_attr_bundle(struct ib_udata *udata)
661 return container_of(udata, struct uverbs_attr_bundle, driver_udata);
664 #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
665 (udata ? container_of(rdma_udata_to_uverbs_attr_bundle(udata)->context, \
666 drv_dev_struct, member) : (drv_dev_struct *)NULL)
668 #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
670 static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
673 if (!uverbs_attr_is_valid(attrs_bundle, idx))
674 return ERR_PTR(-ENOENT);
676 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
679 static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
682 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
685 return PTR_ERR(attr);
687 return attr->ptr_attr.enum_id;
690 static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
693 const struct uverbs_attr *attr;
695 attr = uverbs_attr_get(attrs_bundle, idx);
697 return ERR_CAST(attr);
699 return attr->obj_attr.uobject->object;
702 static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
705 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
708 return ERR_CAST(attr);
710 return attr->obj_attr.uobject;
714 uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
716 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
719 return PTR_ERR(attr);
721 return attr->ptr_attr.len;
724 void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
728 * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
730 * @attrs: The attribute bundle
731 * @idx: The ID of the attribute
732 * @elem_size: The size of the element in the array
735 uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
738 int size = uverbs_attr_get_len(attrs, idx);
743 if (size % elem_size)
746 return size / elem_size;
750 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
751 * UVERBS_ATTR_TYPE_IDRS_ARRAY.
752 * @arr: Returned pointer to array of pointers for uobjects or NULL if
753 * the attribute isn't provided.
755 * Return: The array length or 0 if no attribute was provided.
757 static inline int uverbs_attr_get_uobjs_arr(
758 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
759 struct ib_uobject ***arr)
761 const struct uverbs_attr *attr =
762 uverbs_attr_get(attrs_bundle, attr_idx);
769 *arr = attr->objs_arr_attr.uobjects;
771 return attr->objs_arr_attr.len;
774 static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
776 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
779 static inline void *uverbs_attr_get_alloced_ptr(
780 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
782 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
787 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
791 static inline int _uverbs_copy_from(void *to,
792 const struct uverbs_attr_bundle *attrs_bundle,
796 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
799 return PTR_ERR(attr);
802 * Validation ensures attr->ptr_attr.len >= size. If the caller is
803 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
804 * uverbs_copy_from_or_zero.
806 if (unlikely(size < attr->ptr_attr.len))
809 if (uverbs_attr_ptr_is_inline(attr))
810 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
811 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
818 static inline int _uverbs_copy_from_or_zero(void *to,
819 const struct uverbs_attr_bundle *attrs_bundle,
823 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
827 return PTR_ERR(attr);
829 min_size = min_t(size_t, size, attr->ptr_attr.len);
831 if (uverbs_attr_ptr_is_inline(attr))
832 memcpy(to, &attr->ptr_attr.data, min_size);
833 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
838 memset(to + min_size, 0, size - min_size);
843 #define uverbs_copy_from(to, attrs_bundle, idx) \
844 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
846 #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
847 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
849 static inline struct ib_ucontext *
850 ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
852 return ib_uverbs_get_ucontext_file(attrs->ufile);
855 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
856 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
857 size_t idx, u64 allowed_bits);
858 int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
859 size_t idx, u64 allowed_bits);
860 int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
861 const void *from, size_t size);
862 __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
865 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
868 return _uverbs_alloc(bundle, size, GFP_KERNEL);
871 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
874 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
877 static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
878 size_t n, size_t size)
882 if (unlikely(check_mul_overflow(n, size, &bytes)))
883 return ERR_PTR(-EOVERFLOW);
884 return uverbs_zalloc(bundle, bytes);
887 int _uverbs_get_const_signed(s64 *to,
888 const struct uverbs_attr_bundle *attrs_bundle,
889 size_t idx, s64 lower_bound, u64 upper_bound,
891 int _uverbs_get_const_unsigned(u64 *to,
892 const struct uverbs_attr_bundle *attrs_bundle,
893 size_t idx, u64 upper_bound, u64 *def_val);
894 int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
895 size_t idx, const void *from, size_t size);
898 uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
899 size_t idx, u64 allowed_bits)
904 uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
905 size_t idx, u64 allowed_bits)
909 static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
910 size_t idx, const void *from, size_t size)
914 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
917 return ERR_PTR(-EINVAL);
919 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
922 return ERR_PTR(-EINVAL);
925 _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
926 size_t idx, s64 lower_bound, u64 upper_bound,
932 uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
933 size_t idx, const void *from, size_t size)
938 _uverbs_get_const_signed(s64 *to,
939 const struct uverbs_attr_bundle *attrs_bundle,
940 size_t idx, s64 lower_bound, u64 upper_bound,
946 _uverbs_get_const_unsigned(u64 *to,
947 const struct uverbs_attr_bundle *attrs_bundle,
948 size_t idx, u64 upper_bound, u64 *def_val)
954 #define uverbs_get_const_signed(_to, _attrs_bundle, _idx) \
958 _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
959 type_min(typeof(*(_to))), \
960 type_max(typeof(*(_to))), NULL); \
965 #define uverbs_get_const_unsigned(_to, _attrs_bundle, _idx) \
969 _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
970 type_max(typeof(*(_to))), NULL); \
975 #define uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, _default) \
978 s64 _def_val = _default; \
980 _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \
981 type_min(typeof(*(_to))), \
982 type_max(typeof(*(_to))), &_def_val); \
987 #define uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, _default) \
990 u64 _def_val = _default; \
992 _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \
993 type_max(typeof(*(_to))), &_def_val); \
998 #define uverbs_get_const(_to, _attrs_bundle, _idx) \
999 (is_signed_type(typeof(*(_to))) ? \
1000 uverbs_get_const_signed(_to, _attrs_bundle, _idx) : \
1001 uverbs_get_const_unsigned(_to, _attrs_bundle, _idx)) \
1003 #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
1004 (is_signed_type(typeof(*(_to))) ? \
1005 uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, \
1007 uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, \
1011 uverbs_get_raw_fd(int *to, const struct uverbs_attr_bundle *attrs_bundle,
1014 return uverbs_get_const_signed(to, attrs_bundle, idx);