hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/include/rdma/uverbs_ioctl.h
....@@ -1,33 +1,6 @@
1
+/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
12 /*
23 * 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.
314 */
325
336 #ifndef _UVERBS_IOCTL_
....@@ -52,6 +25,7 @@
5225 UVERBS_ATTR_TYPE_IDR,
5326 UVERBS_ATTR_TYPE_FD,
5427 UVERBS_ATTR_TYPE_ENUM_IN,
28
+ UVERBS_ATTR_TYPE_IDRS_ARRAY,
5529 };
5630
5731 enum uverbs_obj_access {
....@@ -78,6 +52,8 @@
7852 */
7953 u8 alloc_and_copy:1;
8054 u8 mandatory:1;
55
+ /* True if this is from UVERBS_ATTR_UHW */
56
+ u8 is_udata:1;
8157
8258 union {
8359 struct {
....@@ -101,7 +77,7 @@
10177 } enum_def;
10278 } u;
10379
104
- /* This weird split of the enum lets us remove some padding */
80
+ /* This weird split lets us remove some padding */
10581 union {
10682 struct {
10783 /*
....@@ -111,6 +87,17 @@
11187 */
11288 const struct uverbs_attr_spec *ids;
11389 } enum_def;
90
+
91
+ struct {
92
+ /*
93
+ * higher bits mean the namespace and lower bits mean
94
+ * the type id within the namespace.
95
+ */
96
+ u16 obj_type;
97
+ u16 min_len;
98
+ u16 max_len;
99
+ u8 access;
100
+ } objs_arr;
114101 } u2;
115102 };
116103
....@@ -128,6 +115,13 @@
128115 *
129116 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns
130117 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot.
118
+ *
119
+ * This also encodes the tables for the write() and write() extended commands
120
+ * using the coding
121
+ * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command #
122
+ * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex #
123
+ * ie the WRITE path is treated as a special method type in the ioctl
124
+ * framework.
131125 */
132126 enum uapi_radix_data {
133127 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
....@@ -135,12 +129,16 @@
135129 UVERBS_API_ATTR_KEY_BITS = 6,
136130 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0),
137131 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1,
132
+ UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS,
138133
139134 UVERBS_API_METHOD_KEY_BITS = 5,
140135 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,
136
+ UVERBS_API_METHOD_KEY_NUM_CORE = 22,
137
+ UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT,
138
+ UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT,
139
+ UVERBS_API_METHOD_KEY_NUM_DRIVER =
140
+ (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) -
141
+ UVERBS_API_METHOD_KEY_NUM_CORE,
144142 UVERBS_API_METHOD_KEY_MASK = GENMASK(
145143 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1,
146144 UVERBS_API_METHOD_KEY_SHIFT),
....@@ -148,7 +146,7 @@
148146 UVERBS_API_OBJ_KEY_BITS = 5,
149147 UVERBS_API_OBJ_KEY_SHIFT =
150148 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT,
151
- UVERBS_API_OBJ_KEY_NUM_CORE = 24,
149
+ UVERBS_API_OBJ_KEY_NUM_CORE = 20,
152150 UVERBS_API_OBJ_KEY_NUM_DRIVER =
153151 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE,
154152 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT),
....@@ -193,7 +191,22 @@
193191 return id << UVERBS_API_METHOD_KEY_SHIFT;
194192 }
195193
196
-static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
194
+static inline __attribute_const__ u32 uapi_key_write_method(u32 id)
195
+{
196
+ if (id >= UVERBS_API_WRITE_KEY_NUM)
197
+ return UVERBS_API_KEY_ERR;
198
+ return UVERBS_API_METHOD_IS_WRITE | id;
199
+}
200
+
201
+static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id)
202
+{
203
+ if (id >= UVERBS_API_WRITE_KEY_NUM)
204
+ return UVERBS_API_KEY_ERR;
205
+ return UVERBS_API_METHOD_IS_WRITE_EX | id;
206
+}
207
+
208
+static inline __attribute_const__ u32
209
+uapi_key_attr_to_ioctl_method(u32 attr_key)
197210 {
198211 return attr_key &
199212 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
....@@ -201,8 +214,21 @@
201214
202215 static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
203216 {
204
- return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
217
+ unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
218
+
219
+ return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
205220 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
221
+}
222
+
223
+static inline __attribute_const__ bool uapi_key_is_write_method(u32 key)
224
+{
225
+ return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE;
226
+}
227
+
228
+static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key)
229
+{
230
+ return (key & UVERBS_API_METHOD_KEY_MASK) ==
231
+ UVERBS_API_METHOD_IS_WRITE_EX;
206232 }
207233
208234 static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
....@@ -234,9 +260,12 @@
234260 return id;
235261 }
236262
263
+/* Only true for ioctl methods */
237264 static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
238265 {
239
- return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
266
+ unsigned int method = key & UVERBS_API_METHOD_KEY_MASK;
267
+
268
+ return method != 0 && method < UVERBS_API_METHOD_IS_WRITE &&
240269 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
241270 }
242271
....@@ -249,6 +278,11 @@
249278 static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
250279 {
251280 return attr_key - 1;
281
+}
282
+
283
+static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey)
284
+{
285
+ return attr_bkey + 1;
252286 }
253287
254288 /*
....@@ -268,8 +302,7 @@
268302 u32 flags;
269303 size_t num_attrs;
270304 const struct uverbs_attr_def * const (*attrs)[];
271
- int (*handler)(struct ib_uverbs_file *ufile,
272
- struct uverbs_attr_bundle *ctx);
305
+ int (*handler)(struct uverbs_attr_bundle *attrs);
273306 };
274307
275308 struct uverbs_object_def {
....@@ -279,10 +312,131 @@
279312 const struct uverbs_method_def * const (*methods)[];
280313 };
281314
282
-struct uverbs_object_tree_def {
283
- size_t num_objects;
284
- const struct uverbs_object_def * const (*objects)[];
315
+enum uapi_definition_kind {
316
+ UAPI_DEF_END = 0,
317
+ UAPI_DEF_OBJECT_START,
318
+ UAPI_DEF_WRITE,
319
+ UAPI_DEF_CHAIN_OBJ_TREE,
320
+ UAPI_DEF_CHAIN,
321
+ UAPI_DEF_IS_SUPPORTED_FUNC,
322
+ UAPI_DEF_IS_SUPPORTED_DEV_FN,
285323 };
324
+
325
+enum uapi_definition_scope {
326
+ UAPI_SCOPE_OBJECT = 1,
327
+ UAPI_SCOPE_METHOD = 2,
328
+};
329
+
330
+struct uapi_definition {
331
+ u8 kind;
332
+ u8 scope;
333
+ union {
334
+ struct {
335
+ u16 object_id;
336
+ } object_start;
337
+ struct {
338
+ u16 command_num;
339
+ u8 is_ex:1;
340
+ u8 has_udata:1;
341
+ u8 has_resp:1;
342
+ u8 req_size;
343
+ u8 resp_size;
344
+ } write;
345
+ };
346
+
347
+ union {
348
+ bool (*func_is_supported)(struct ib_device *device);
349
+ int (*func_write)(struct uverbs_attr_bundle *attrs);
350
+ const struct uapi_definition *chain;
351
+ const struct uverbs_object_def *chain_obj_tree;
352
+ size_t needs_fn_offset;
353
+ };
354
+};
355
+
356
+/* Define things connected to object_id */
357
+#define DECLARE_UVERBS_OBJECT(_object_id, ...) \
358
+ { \
359
+ .kind = UAPI_DEF_OBJECT_START, \
360
+ .object_start = { .object_id = _object_id }, \
361
+ }, \
362
+ ##__VA_ARGS__
363
+
364
+/* Use in a var_args of DECLARE_UVERBS_OBJECT */
365
+#define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \
366
+ { \
367
+ .kind = UAPI_DEF_WRITE, \
368
+ .scope = UAPI_SCOPE_OBJECT, \
369
+ .write = { .is_ex = 0, .command_num = _command_num }, \
370
+ .func_write = _func, \
371
+ _cmd_desc, \
372
+ }, \
373
+ ##__VA_ARGS__
374
+
375
+/* Use in a var_args of DECLARE_UVERBS_OBJECT */
376
+#define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \
377
+ { \
378
+ .kind = UAPI_DEF_WRITE, \
379
+ .scope = UAPI_SCOPE_OBJECT, \
380
+ .write = { .is_ex = 1, .command_num = _command_num }, \
381
+ .func_write = _func, \
382
+ _cmd_desc, \
383
+ }, \
384
+ ##__VA_ARGS__
385
+
386
+/*
387
+ * Object is only supported if the function pointer named ibdev_fn in struct
388
+ * ib_device is not NULL.
389
+ */
390
+#define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \
391
+ { \
392
+ .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
393
+ .scope = UAPI_SCOPE_OBJECT, \
394
+ .needs_fn_offset = \
395
+ offsetof(struct ib_device_ops, ibdev_fn) + \
396
+ BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
397
+ ibdev_fn) != \
398
+ sizeof(void *)), \
399
+ }
400
+
401
+/*
402
+ * Method is only supported if the function pointer named ibdev_fn in struct
403
+ * ib_device is not NULL.
404
+ */
405
+#define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \
406
+ { \
407
+ .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \
408
+ .scope = UAPI_SCOPE_METHOD, \
409
+ .needs_fn_offset = \
410
+ offsetof(struct ib_device_ops, ibdev_fn) + \
411
+ BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \
412
+ ibdev_fn) != \
413
+ sizeof(void *)), \
414
+ }
415
+
416
+/* Call a function to determine if the entire object is supported or not */
417
+#define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \
418
+ { \
419
+ .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \
420
+ .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \
421
+ }
422
+
423
+/* Include another struct uapi_definition in this one */
424
+#define UAPI_DEF_CHAIN(_def_var) \
425
+ { \
426
+ .kind = UAPI_DEF_CHAIN, .chain = _def_var, \
427
+ }
428
+
429
+/* Temporary until the tree base description is replaced */
430
+#define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \
431
+ { \
432
+ .kind = UAPI_DEF_CHAIN_OBJ_TREE, \
433
+ .object_start = { .object_id = _object_enum }, \
434
+ .chain_obj_tree = _object_ptr, \
435
+ }, \
436
+ ##__VA_ARGS__
437
+#define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \
438
+ UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, &UVERBS_OBJECT(_object_enum), \
439
+ ##__VA_ARGS__)
286440
287441 /*
288442 * =======================================
....@@ -310,8 +464,7 @@
310464 */
311465 #define UVERBS_ATTR_STRUCT(_type, _last) \
312466 .zero_trailing = 1, \
313
- UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
314
- sizeof(_type))
467
+ UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type))
315468 /*
316469 * Specifies at least min_len bytes must be passed in, but the amount can be
317470 * larger, up to the protocol maximum size. No check for zeroing is done.
....@@ -322,6 +475,33 @@
322475 #define UA_ALLOC_AND_COPY .alloc_and_copy = 1
323476 #define UA_MANDATORY .mandatory = 1
324477 #define UA_OPTIONAL .mandatory = 0
478
+
479
+/*
480
+ * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only
481
+ * READ\WRITE accesses are supported.
482
+ */
483
+#define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \
484
+ ...) \
485
+ (&(const struct uverbs_attr_def){ \
486
+ .id = (_attr_id) + \
487
+ BUILD_BUG_ON_ZERO((_min_len) == 0 || \
488
+ (_max_len) > \
489
+ PAGE_SIZE / sizeof(void *) || \
490
+ (_min_len) > (_max_len) || \
491
+ (_access) == UVERBS_ACCESS_NEW || \
492
+ (_access) == UVERBS_ACCESS_DESTROY), \
493
+ .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \
494
+ .u2.objs_arr.obj_type = _idr_type, \
495
+ .u2.objs_arr.access = _access, \
496
+ .u2.objs_arr.min_len = _min_len, \
497
+ .u2.objs_arr.max_len = _max_len, \
498
+ __VA_ARGS__ } })
499
+
500
+/*
501
+ * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted,
502
+ * the user must validate the type of the uobject instead.
503
+ */
504
+#define UVERBS_IDR_ANY_OBJECT 0xFFFF
325505
326506 #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
327507 (&(const struct uverbs_attr_def){ \
....@@ -365,6 +545,15 @@
365545 __VA_ARGS__ }, \
366546 })
367547
548
+/* An input value that is a member in the enum _enum_type. */
549
+#define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \
550
+ UVERBS_ATTR_PTR_IN( \
551
+ _attr_id, \
552
+ UVERBS_ATTR_SIZE( \
553
+ sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \
554
+ sizeof(u64)), \
555
+ __VA_ARGS__)
556
+
368557 /*
369558 * An input value that is a bitwise combination of values of _enum_type.
370559 * This permits the flag value to be passed as either a u32 or u64, it must
....@@ -386,25 +575,12 @@
386575 #define UVERBS_ATTR_UHW() \
387576 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
388577 UVERBS_ATTR_MIN_SIZE(0), \
389
- UA_OPTIONAL), \
578
+ UA_OPTIONAL, \
579
+ .is_udata = 1), \
390580 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
391581 UVERBS_ATTR_MIN_SIZE(0), \
392
- UA_OPTIONAL)
393
-
394
-/*
395
- * =======================================
396
- * Declaration helpers
397
- * =======================================
398
- */
399
-
400
-#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
401
- static const struct uverbs_object_def *const _name##_ptr[] = { \
402
- __VA_ARGS__, \
403
- }; \
404
- static const struct uverbs_object_tree_def _name = { \
405
- .num_objects = ARRAY_SIZE(_name##_ptr), \
406
- .objects = &_name##_ptr, \
407
- }
582
+ UA_OPTIONAL, \
583
+ .is_udata = 1)
408584
409585 /* =================================================
410586 * Parsing infrastructure
....@@ -431,15 +607,25 @@
431607 const struct uverbs_api_attr *attr_elm;
432608 };
433609
610
+struct uverbs_objs_arr_attr {
611
+ struct ib_uobject **uobjects;
612
+ u16 len;
613
+};
614
+
434615 struct uverbs_attr {
435616 union {
436617 struct uverbs_ptr_attr ptr_attr;
437618 struct uverbs_obj_attr obj_attr;
619
+ struct uverbs_objs_arr_attr objs_arr_attr;
438620 };
439621 };
440622
441623 struct uverbs_attr_bundle {
624
+ struct ib_udata driver_udata;
625
+ struct ib_udata ucore;
442626 struct ib_uverbs_file *ufile;
627
+ struct ib_ucontext *context;
628
+ struct ib_uobject *uobject;
443629 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
444630 struct uverbs_attr attrs[];
445631 };
....@@ -450,6 +636,23 @@
450636 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
451637 attrs_bundle->attr_present);
452638 }
639
+
640
+/**
641
+ * rdma_udata_to_drv_context - Helper macro to get the driver's context out of
642
+ * ib_udata which is embedded in uverbs_attr_bundle.
643
+ *
644
+ * If udata is not NULL this cannot fail. Otherwise a NULL udata will result
645
+ * in a NULL ucontext pointer, as a safety precaution. Callers should be using
646
+ * 'udata' to determine if the driver call is in user or kernel mode, not
647
+ * 'ucontext'.
648
+ *
649
+ */
650
+#define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \
651
+ (udata ? container_of(container_of(udata, struct uverbs_attr_bundle, \
652
+ driver_udata) \
653
+ ->context, \
654
+ drv_dev_struct, member) : \
655
+ (drv_dev_struct *)NULL)
453656
454657 #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
455658
....@@ -505,6 +708,56 @@
505708 return PTR_ERR(attr);
506709
507710 return attr->ptr_attr.len;
711
+}
712
+
713
+void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle,
714
+ u16 idx);
715
+
716
+/*
717
+ * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr
718
+ * attribute.
719
+ * @attrs: The attribute bundle
720
+ * @idx: The ID of the attribute
721
+ * @elem_size: The size of the element in the array
722
+ */
723
+static inline int
724
+uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx,
725
+ size_t elem_size)
726
+{
727
+ int size = uverbs_attr_get_len(attrs, idx);
728
+
729
+ if (size < 0)
730
+ return size;
731
+
732
+ if (size % elem_size)
733
+ return -EINVAL;
734
+
735
+ return size / elem_size;
736
+}
737
+
738
+/**
739
+ * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for
740
+ * UVERBS_ATTR_TYPE_IDRS_ARRAY.
741
+ * @arr: Returned pointer to array of pointers for uobjects or NULL if
742
+ * the attribute isn't provided.
743
+ *
744
+ * Return: The array length or 0 if no attribute was provided.
745
+ */
746
+static inline int uverbs_attr_get_uobjs_arr(
747
+ const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx,
748
+ struct ib_uobject ***arr)
749
+{
750
+ const struct uverbs_attr *attr =
751
+ uverbs_attr_get(attrs_bundle, attr_idx);
752
+
753
+ if (IS_ERR(attr)) {
754
+ *arr = NULL;
755
+ return 0;
756
+ }
757
+
758
+ *arr = attr->objs_arr_attr.uobjects;
759
+
760
+ return attr->objs_arr_attr.len;
508761 }
509762
510763 static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
....@@ -582,6 +835,12 @@
582835 #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
583836 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
584837
838
+static inline struct ib_ucontext *
839
+ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs)
840
+{
841
+ return ib_uverbs_get_ucontext_file(attrs->ufile);
842
+}
843
+
585844 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
586845 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
587846 size_t idx, u64 allowed_bits);
....@@ -603,6 +862,21 @@
603862 {
604863 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
605864 }
865
+
866
+static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle,
867
+ size_t n, size_t size)
868
+{
869
+ size_t bytes;
870
+
871
+ if (unlikely(check_mul_overflow(n, size, &bytes)))
872
+ return ERR_PTR(-EOVERFLOW);
873
+ return uverbs_zalloc(bundle, bytes);
874
+}
875
+int _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
876
+ size_t idx, s64 lower_bound, u64 upper_bound,
877
+ s64 *def_val);
878
+int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
879
+ size_t idx, const void *from, size_t size);
606880 #else
607881 static inline int
608882 uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
....@@ -631,6 +905,40 @@
631905 {
632906 return ERR_PTR(-EINVAL);
633907 }
908
+static inline int
909
+_uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle,
910
+ size_t idx, s64 lower_bound, u64 upper_bound,
911
+ s64 *def_val)
912
+{
913
+ return -EINVAL;
914
+}
915
+static inline int
916
+uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle,
917
+ size_t idx, const void *from, size_t size)
918
+{
919
+ return -EINVAL;
920
+}
634921 #endif
635922
923
+#define uverbs_get_const(_to, _attrs_bundle, _idx) \
924
+ ({ \
925
+ s64 _val; \
926
+ int _ret = _uverbs_get_const(&_val, _attrs_bundle, _idx, \
927
+ type_min(typeof(*_to)), \
928
+ type_max(typeof(*_to)), NULL); \
929
+ (*_to) = _val; \
930
+ _ret; \
931
+ })
932
+
933
+#define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \
934
+ ({ \
935
+ s64 _val; \
936
+ s64 _def_val = _default; \
937
+ int _ret = \
938
+ _uverbs_get_const(&_val, _attrs_bundle, _idx, \
939
+ type_min(typeof(*_to)), \
940
+ type_max(typeof(*_to)), &_def_val); \
941
+ (*_to) = _val; \
942
+ _ret; \
943
+ })
636944 #endif