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,
55 UVERBS_ATTR_TYPE_IDRS_ARRAY,
58 enum uverbs_obj_access {
65 /* Specification of a single attribute inside the ioctl message */
67 struct uverbs_attr_spec {
71 * Support extending attributes by length. Allow the user to provide
72 * more bytes than ptr.len, but check that everything after is zero'd
77 * Valid only for PTR_IN. Allocate and copy the data inside
85 /* Current known size to kernel */
87 /* User isn't allowed to provide something < min_len */
93 * higher bits mean the namespace and lower bits mean
94 * the type id within the namespace.
105 /* This weird split lets us remove some padding */
109 * The enum attribute can select one of the attributes
110 * contained in the ids array. Currently only PTR_IN
111 * attributes are supported in the ids array.
113 const struct uverbs_attr_spec *ids;
118 * higher bits mean the namespace and lower bits mean
119 * the type id within the namespace.
130 * Information about the API is loaded into a radix tree. For IOCTL we start
132 * object_id, attr_id, method_id
134 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based
135 * on the current kernel support this is compressed into 16 bit key for the
136 * radix tree. Since this compression is entirely internal to the kernel the
137 * below limits can be revised if the kernel gains additional data.
139 * With 64 leafs per node this is a 3 level radix tree.
141 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
142 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
144 * This also encodes the tables for the write() and write() extended commands
146 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
147 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
148 * ie the WRITE path is treated as a special method type in the ioctl
151 enum uapi_radix_data {
152 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
154 UVERBS_API_ATTR_KEY_BITS = 6,
155 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
156 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
157 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
159 UVERBS_API_METHOD_KEY_BITS = 5,
160 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS,
161 UVERBS_API_METHOD_KEY_NUM_CORE = 22,
162 UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
163 UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
164 UVERBS_API_METHOD_KEY_NUM_DRIVER =
165 (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
166 UVERBS_API_METHOD_KEY_NUM_CORE,
167 UVERBS_API_METHOD_KEY_MASK = GENMASK(
168 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
169 UVERBS_API_METHOD_KEY_SHIFT),
171 UVERBS_API_OBJ_KEY_BITS = 5,
172 UVERBS_API_OBJ_KEY_SHIFT =
173 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
174 UVERBS_API_OBJ_KEY_NUM_CORE = 24,
175 UVERBS_API_OBJ_KEY_NUM_DRIVER =
176 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
177 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
179 /* This id guaranteed to not exist in the radix tree */
180 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
183 static inline __attribute_const__ u32 uapi_key_obj(u32 id)
185 if (id & UVERBS_API_NS_FLAG) {
186 id &= ~UVERBS_API_NS_FLAG;
187 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER)
188 return UVERBS_API_KEY_ERR;
189 id = id + UVERBS_API_OBJ_KEY_NUM_CORE;
191 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
192 return UVERBS_API_KEY_ERR;
195 return id << UVERBS_API_OBJ_KEY_SHIFT;
198 static inline __attribute_const__ bool uapi_key_is_object(u32 key)
200 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
203 static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
205 if (id & UVERBS_API_NS_FLAG) {
206 id &= ~UVERBS_API_NS_FLAG;
207 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER)
208 return UVERBS_API_KEY_ERR;
209 id = id + UVERBS_API_METHOD_KEY_NUM_CORE;
212 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
213 return UVERBS_API_KEY_ERR;
216 return id << UVERBS_API_METHOD_KEY_SHIFT;
219 static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
221 if (id >= UVERBS_API_WRITE_KEY_NUM)
222 return UVERBS_API_KEY_ERR;
223 return UVERBS_API_METHOD_IS_WRITE | id;
226 static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
228 if (id >= UVERBS_API_WRITE_KEY_NUM)
229 return UVERBS_API_KEY_ERR;
230 return UVERBS_API_METHOD_IS_WRITE_EX | id;
233 static inline __attribute_const__ u32
234 uapi_key_attr_to_ioctl_method(u32 attr_key)
237 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
240 static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
242 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
244 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
245 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
248 static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
250 return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
253 static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
255 return (key & UVERBS_API_METHOD_KEY_MASK) ==
256 UVERBS_API_METHOD_IS_WRITE_EX;
259 static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
261 /* 0 is the method slot itself */
262 return ioctl_method_key + 1;
265 static inline __attribute_const__ u32 uapi_key_attr(u32 id)
268 * The attr is designed to fit in the typical single radix tree node
269 * of 64 entries. Since allmost all methods have driver attributes we
270 * organize things so that the driver and core attributes interleave to
271 * reduce the length of the attributes array in typical cases.
273 if (id & UVERBS_API_NS_FLAG) {
274 id &= ~UVERBS_API_NS_FLAG;
276 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
277 return UVERBS_API_KEY_ERR;
280 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
281 return UVERBS_API_KEY_ERR;
288 /* Only true for ioctl methods */
289 static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
291 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
293 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
294 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
298 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN),
299 * basically it undoes the reservation of 0 in the ID numbering. attr_key
300 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of
303 static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
308 static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
310 return attr_bkey + 1;
314 * =======================================
316 * =======================================
319 struct uverbs_attr_def {
321 struct uverbs_attr_spec attr;
324 struct uverbs_method_def {
326 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
329 const struct uverbs_attr_def * const (*attrs)[];
330 int (*handler)(struct ib_uverbs_file *ufile,
331 struct uverbs_attr_bundle *ctx);
334 struct uverbs_object_def {
336 const struct uverbs_obj_type *type_attrs;
338 const struct uverbs_method_def * const (*methods)[];
341 enum uapi_definition_kind {
343 UAPI_DEF_OBJECT_START,
345 UAPI_DEF_CHAIN_OBJ_TREE,
347 UAPI_DEF_IS_SUPPORTED_FUNC,
348 UAPI_DEF_IS_SUPPORTED_DEV_FN,
351 enum uapi_definition_scope {
352 UAPI_SCOPE_OBJECT = 1,
353 UAPI_SCOPE_METHOD = 2,
356 struct uapi_definition {
370 bool (*func_is_supported)(struct ib_device *device);
371 ssize_t (*func_write)(struct uverbs_attr_bundle *attrs,
372 const char __user *buf, int in_len,
374 int (*func_write_ex)(struct uverbs_attr_bundle *attrs,
375 struct ib_udata *ucore,
376 struct ib_udata *uhw);
377 const struct uapi_definition *chain;
378 const struct uverbs_object_def *chain_obj_tree;
379 size_t needs_fn_offset;
383 /* Define things connected to object_id */
384 #define DECLARE_UVERBS_OBJECT(_object_id, ...) \
386 .kind = UAPI_DEF_OBJECT_START, \
387 .object_start = { .object_id = _object_id }, \
391 /* Use in a var_args of DECLARE_UVERBS_OBJECT */
392 #define DECLARE_UVERBS_WRITE(_command_num, _func, ...) \
394 .kind = UAPI_DEF_WRITE, \
395 .scope = UAPI_SCOPE_OBJECT, \
396 .write = { .is_ex = 0, .command_num = _command_num }, \
397 .func_write = _func, \
401 /* Use in a var_args of DECLARE_UVERBS_OBJECT */
402 #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, ...) \
404 .kind = UAPI_DEF_WRITE, \
405 .scope = UAPI_SCOPE_OBJECT, \
406 .write = { .is_ex = 1, .command_num = _command_num }, \
407 .func_write_ex = _func, \
412 * Object is only supported if the function pointer named ibdev_fn in struct
413 * ib_device is not NULL.
415 #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
417 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
418 .scope = UAPI_SCOPE_OBJECT, \
420 offsetof(struct ib_device, ibdev_fn) + \
422 sizeof(((struct ib_device *)0)->ibdev_fn) != \
427 * Method is only supported if the function pointer named ibdev_fn in struct
428 * ib_device is not NULL.
430 #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
432 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
433 .scope = UAPI_SCOPE_METHOD, \
435 offsetof(struct ib_device, ibdev_fn) + \
437 sizeof(((struct ib_device *)0)->ibdev_fn) != \
441 /* Call a function to determine if the entire object is supported or not */
442 #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
444 .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
445 .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
448 /* Include another struct uapi_definition in this one */
449 #define UAPI_DEF_CHAIN(_def_var) \
451 .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
454 /* Temporary until the tree base description is replaced */
455 #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr) \
457 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
458 .object_start = { .object_id = _object_enum }, \
459 .chain_obj_tree = _object_ptr, \
461 #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
462 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum)), \
466 * =======================================
467 * Attribute Specifications
468 * =======================================
471 #define UVERBS_ATTR_SIZE(_min_len, _len) \
472 .u.ptr.min_len = _min_len, .u.ptr.len = _len
474 #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
477 * Specifies a uapi structure that cannot be extended. The user must always
478 * supply the whole structure and nothing more. The structure must be declared
479 * in a header under include/uapi/rdma.
481 #define UVERBS_ATTR_TYPE(_type) \
482 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
484 * Specifies a uapi structure where the user must provide at least up to
485 * member 'last'. Anything after last and up until the end of the structure
486 * can be non-zero, anything longer than the end of the structure must be
487 * zero. The structure must be declared in a header under include/uapi/rdma.
489 #define UVERBS_ATTR_STRUCT(_type, _last) \
490 .zero_trailing = 1, \
491 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
494 * Specifies at least min_len bytes must be passed in, but the amount can be
495 * larger, up to the protocol maximum size. No check for zeroing is done.
497 #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
499 /* Must be used in the '...' of any UVERBS_ATTR */
500 #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
501 #define UA_MANDATORY .mandatory = 1
502 #define UA_OPTIONAL .mandatory = 0
505 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
506 * READ\WRITE accesses are supported.
508 #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
510 (&(const struct uverbs_attr_def){ \
512 BUILD_BUG_ON_ZERO((_min_len) == 0 || \
514 PAGE_SIZE / sizeof(void *) || \
515 (_min_len) > (_max_len) || \
516 (_access) == UVERBS_ACCESS_NEW || \
517 (_access) == UVERBS_ACCESS_DESTROY), \
518 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
519 .u2.objs_arr.obj_type = _idr_type, \
520 .u2.objs_arr.access = _access, \
521 .u2.objs_arr.min_len = _min_len, \
522 .u2.objs_arr.max_len = _max_len, \
525 #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
526 (&(const struct uverbs_attr_def){ \
528 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
529 .u.obj.obj_type = _idr_type, \
530 .u.obj.access = _access, \
533 #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
534 (&(const struct uverbs_attr_def){ \
536 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
537 (_access) != UVERBS_ACCESS_READ), \
538 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
539 .u.obj.obj_type = _fd_type, \
540 .u.obj.access = _access, \
543 #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
544 (&(const struct uverbs_attr_def){ \
546 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
550 #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
551 (&(const struct uverbs_attr_def){ \
553 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
557 /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
558 #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
559 (&(const struct uverbs_attr_def){ \
561 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
562 .u2.enum_def.ids = _enum_arr, \
563 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
567 /* An input value that is a member in the enum _enum_type. */
568 #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
569 UVERBS_ATTR_PTR_IN( \
572 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
577 * An input value that is a bitwise combination of values of _enum_type.
578 * This permits the flag value to be passed as either a u32 or u64, it must
579 * be retrieved via uverbs_get_flag().
581 #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
582 UVERBS_ATTR_PTR_IN( \
584 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
585 !sizeof(_enum_type *)), \
590 * This spec is used in order to pass information to the hardware driver in a
591 * legacy way. Every verb that could get driver specific data should get this
594 #define UVERBS_ATTR_UHW() \
595 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
596 UVERBS_ATTR_MIN_SIZE(0), \
598 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
599 UVERBS_ATTR_MIN_SIZE(0), \
602 /* =================================================
603 * Parsing infrastructure
604 * =================================================
608 struct uverbs_ptr_attr {
610 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
622 struct uverbs_obj_attr {
623 struct ib_uobject *uobject;
624 const struct uverbs_api_attr *attr_elm;
627 struct uverbs_objs_arr_attr {
628 struct ib_uobject **uobjects;
634 struct uverbs_ptr_attr ptr_attr;
635 struct uverbs_obj_attr obj_attr;
636 struct uverbs_objs_arr_attr objs_arr_attr;
640 struct uverbs_attr_bundle {
641 struct ib_uverbs_file *ufile;
642 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
643 struct uverbs_attr attrs[];
646 static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
649 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
650 attrs_bundle->attr_present);
653 #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
655 static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
658 if (!uverbs_attr_is_valid(attrs_bundle, idx))
659 return ERR_PTR(-ENOENT);
661 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
664 static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
667 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
670 return PTR_ERR(attr);
672 return attr->ptr_attr.enum_id;
675 static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
678 const struct uverbs_attr *attr;
680 attr = uverbs_attr_get(attrs_bundle, idx);
682 return ERR_CAST(attr);
684 return attr->obj_attr.uobject->object;
687 static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
690 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
693 return ERR_CAST(attr);
695 return attr->obj_attr.uobject;
699 uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
701 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
704 return PTR_ERR(attr);
706 return attr->ptr_attr.len;
710 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
711 * UVERBS_ATTR_TYPE_IDRS_ARRAY.
712 * @arr: Returned pointer to array of pointers for uobjects or NULL if
713 * the attribute isn't provided.
715 * Return: The array length or 0 if no attribute was provided.
717 static inline int uverbs_attr_get_uobjs_arr(
718 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
719 struct ib_uobject ***arr)
721 const struct uverbs_attr *attr =
722 uverbs_attr_get(attrs_bundle, attr_idx);
729 *arr = attr->objs_arr_attr.uobjects;
731 return attr->objs_arr_attr.len;
734 static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
736 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
739 static inline void *uverbs_attr_get_alloced_ptr(
740 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
742 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
747 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
751 static inline int _uverbs_copy_from(void *to,
752 const struct uverbs_attr_bundle *attrs_bundle,
756 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
759 return PTR_ERR(attr);
762 * Validation ensures attr->ptr_attr.len >= size. If the caller is
763 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
764 * uverbs_copy_from_or_zero.
766 if (unlikely(size < attr->ptr_attr.len))
769 if (uverbs_attr_ptr_is_inline(attr))
770 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
771 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
778 static inline int _uverbs_copy_from_or_zero(void *to,
779 const struct uverbs_attr_bundle *attrs_bundle,
783 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
787 return PTR_ERR(attr);
789 min_size = min_t(size_t, size, attr->ptr_attr.len);
791 if (uverbs_attr_ptr_is_inline(attr))
792 memcpy(to, &attr->ptr_attr.data, min_size);
793 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
798 memset(to + min_size, 0, size - min_size);
803 #define uverbs_copy_from(to, attrs_bundle, idx) \
804 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
806 #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
807 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
809 static inline struct ib_ucontext *
810 ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
812 return ib_uverbs_get_ucontext_file(attrs->ufile);
815 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
816 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
817 size_t idx, u64 allowed_bits);
818 int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
819 size_t idx, u64 allowed_bits);
820 int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
821 const void *from, size_t size);
822 __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
825 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
828 return _uverbs_alloc(bundle, size, GFP_KERNEL);
831 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
834 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
836 int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
837 size_t idx, s64 lower_bound, u64 upper_bound,
841 uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
842 size_t idx, u64 allowed_bits)
847 uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
848 size_t idx, u64 allowed_bits)
852 static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
853 size_t idx, const void *from, size_t size)
857 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
860 return ERR_PTR(-EINVAL);
862 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
865 return ERR_PTR(-EINVAL);
868 _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
869 size_t idx, s64 lower_bound, u64 upper_bound,
876 #define uverbs_get_const(_to, _attrs_bundle, _idx) \
879 int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
880 type_min(typeof(*_to)), \
881 type_max(typeof(*_to)), NULL); \
886 #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
889 s64 _def_val = _default; \
891 _uverbs_get_const(&_val, _attrs_bundle, _idx, \
892 type_min(typeof(*_to)), \
893 type_max(typeof(*_to)), &_def_val); \