Merge tag 'arm64-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64...
[linux-2.6-block.git] / include / rdma / uverbs_ioctl.h
CommitLineData
a0aa309c
MB
1/*
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>
35410306
MB
37#include <linux/uaccess.h>
38#include <rdma/rdma_user_ioctl.h>
d70724f1 39#include <rdma/ib_user_ioctl_verbs.h>
1f7ff9d5 40#include <rdma/ib_user_ioctl_cmds.h>
a0aa309c
MB
41
42/*
43 * =======================================
44 * Verbs action specifications
45 * =======================================
46 */
47
f43dbebf
MB
48enum uverbs_attr_type {
49 UVERBS_ATTR_TYPE_NA,
fac9658c
MB
50 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
f43dbebf
MB
52 UVERBS_ATTR_TYPE_IDR,
53 UVERBS_ATTR_TYPE_FD,
494c5580 54 UVERBS_ATTR_TYPE_ENUM_IN,
f43dbebf
MB
55};
56
a0aa309c
MB
57enum uverbs_obj_access {
58 UVERBS_ACCESS_READ,
59 UVERBS_ACCESS_WRITE,
60 UVERBS_ACCESS_NEW,
61 UVERBS_ACCESS_DESTROY
62};
63
1f07e08f 64/* Specification of a single attribute inside the ioctl message */
83bb4442 65/* good size 16 */
f43dbebf 66struct uverbs_attr_spec {
d108dac0 67 u8 type;
83bb4442
JG
68
69 /*
422e3d37
JG
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
72 * by the user.
83bb4442 73 */
422e3d37 74 u8 zero_trailing:1;
83bb4442
JG
75 /*
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;
d108dac0 81
fac9658c 82 union {
1f07e08f 83 struct {
c66db311 84 /* Current known size to kernel */
d108dac0 85 u16 len;
c66db311 86 /* User isn't allowed to provide something < min_len */
d108dac0 87 u16 min_len;
1f07e08f 88 } ptr;
d108dac0 89
1f07e08f 90 struct {
fac9658c
MB
91 /*
92 * higher bits mean the namespace and lower bits mean
93 * the type id within the namespace.
94 */
d108dac0
JG
95 u16 obj_type;
96 u8 access;
fac9658c 97 } obj;
d108dac0
JG
98
99 struct {
100 u8 num_elems;
101 } enum_def;
102 } u;
103
104 /* This weird split of the enum lets us remove some padding */
105 union {
494c5580 106 struct {
494c5580
MB
107 /*
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 */
d108dac0 112 const struct uverbs_attr_spec *ids;
494c5580 113 } enum_def;
d108dac0 114 } u2;
f43dbebf
MB
115};
116
9ed3e5f4
JG
117/*
118 * Information about the API is loaded into a radix tree. For IOCTL we start
119 * with a tuple of:
120 * object_id, attr_id, method_id
121 *
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.
126 *
127 * With 64 leafs per node this is a 3 level radix tree.
128 *
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.
131 */
132enum uapi_radix_data {
133 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT,
134
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,
138
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),
147
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),
155
156 /* This id guaranteed to not exist in the radix tree */
157 UVERBS_API_KEY_ERR = 0xFFFFFFFF,
158};
159
160static inline __attribute_const__ u32 uapi_key_obj(u32 id)
161{
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;
167 } else {
168 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE)
169 return UVERBS_API_KEY_ERR;
170 }
171
172 return id << UVERBS_API_OBJ_KEY_SHIFT;
173}
174
175static inline __attribute_const__ bool uapi_key_is_object(u32 key)
176{
177 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0;
178}
179
180static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id)
181{
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;
187 } else {
188 id++;
189 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE)
190 return UVERBS_API_KEY_ERR;
191 }
192
193 return id << UVERBS_API_METHOD_KEY_SHIFT;
194}
195
196static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key)
197{
198 return attr_key &
199 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK);
200}
201
202static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key)
203{
204 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
205 (key & UVERBS_API_ATTR_KEY_MASK) == 0;
206}
207
208static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key)
209{
210 /* 0 is the method slot itself */
211 return ioctl_method_key + 1;
212}
213
214static inline __attribute_const__ u32 uapi_key_attr(u32 id)
215{
216 /*
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.
221 */
222 if (id & UVERBS_API_NS_FLAG) {
223 id &= ~UVERBS_API_NS_FLAG;
224 id++;
225 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
226 return UVERBS_API_KEY_ERR;
227 id = (id << 1) | 0;
228 } else {
229 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1))
230 return UVERBS_API_KEY_ERR;
231 id = (id << 1) | 1;
232 }
233
234 return id;
235}
236
237static inline __attribute_const__ bool uapi_key_is_attr(u32 key)
238{
239 return (key & UVERBS_API_METHOD_KEY_MASK) != 0 &&
240 (key & UVERBS_API_ATTR_KEY_MASK) != 0;
241}
242
243/*
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
247 * uapi_key_attr().
248 */
249static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key)
250{
251 return attr_key - 1;
252}
253
5009010f
MB
254/*
255 * =======================================
256 * Verbs definitions
257 * =======================================
258 */
259
09e3ebf8
MB
260struct uverbs_attr_def {
261 u16 id;
262 struct uverbs_attr_spec attr;
263};
264
265struct uverbs_method_def {
266 u16 id;
267 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
268 u32 flags;
269 size_t num_attrs;
270 const struct uverbs_attr_def * const (*attrs)[];
e83f0ecd 271 int (*handler)(struct ib_uverbs_file *ufile,
09e3ebf8
MB
272 struct uverbs_attr_bundle *ctx);
273};
274
5009010f 275struct uverbs_object_def {
09e3ebf8 276 u16 id;
5009010f 277 const struct uverbs_obj_type *type_attrs;
09e3ebf8
MB
278 size_t num_methods;
279 const struct uverbs_method_def * const (*methods)[];
280};
281
282struct uverbs_object_tree_def {
283 size_t num_objects;
284 const struct uverbs_object_def * const (*objects)[];
5009010f
MB
285};
286
d108dac0
JG
287/*
288 * =======================================
289 * Attribute Specifications
290 * =======================================
291 */
c66db311 292
c66db311 293#define UVERBS_ATTR_SIZE(_min_len, _len) \
d108dac0 294 .u.ptr.min_len = _min_len, .u.ptr.len = _len
422e3d37 295
fd44e385
YH
296#define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0)
297
422e3d37
JG
298/*
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.
302 */
303#define UVERBS_ATTR_TYPE(_type) \
304 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
305/*
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.
310 */
311#define UVERBS_ATTR_STRUCT(_type, _last) \
312 .zero_trailing = 1, \
313 UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \
314 sizeof(_type))
540cd692
JG
315/*
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.
318 */
319#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
c66db311 320
d108dac0 321/* Must be used in the '...' of any UVERBS_ATTR */
83bb4442
JG
322#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
323#define UA_MANDATORY .mandatory = 1
83bb4442 324#define UA_OPTIONAL .mandatory = 0
d108dac0
JG
325
326#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
9a119cd5 327 (&(const struct uverbs_attr_def){ \
d108dac0
JG
328 .id = _attr_id, \
329 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
330 .u.obj.obj_type = _idr_type, \
331 .u.obj.access = _access, \
332 __VA_ARGS__ } })
333
334#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
9a119cd5 335 (&(const struct uverbs_attr_def){ \
d108dac0
JG
336 .id = (_attr_id) + \
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, \
342 __VA_ARGS__ } })
343
344#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
9a119cd5 345 (&(const struct uverbs_attr_def){ \
d108dac0
JG
346 .id = _attr_id, \
347 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
348 _type, \
349 __VA_ARGS__ } })
350
351#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
9a119cd5 352 (&(const struct uverbs_attr_def){ \
d108dac0
JG
353 .id = _attr_id, \
354 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
355 _type, \
356 __VA_ARGS__ } })
357
358/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
359#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
9a119cd5 360 (&(const struct uverbs_attr_def){ \
d108dac0
JG
361 .id = _attr_id, \
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), \
365 __VA_ARGS__ }, \
366 })
35410306 367
bccd0622
JG
368/*
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().
372 */
373#define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \
374 UVERBS_ATTR_PTR_IN( \
375 _attr_id, \
376 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \
377 !sizeof(_enum_type *)), \
378 sizeof(u64)), \
379 __VA_ARGS__)
380
6c61d2a5
JG
381/*
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
384 * spec.
385 */
386#define UVERBS_ATTR_UHW() \
9a119cd5 387 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
540cd692
JG
388 UVERBS_ATTR_MIN_SIZE(0), \
389 UA_OPTIONAL), \
9a119cd5 390 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
540cd692
JG
391 UVERBS_ATTR_MIN_SIZE(0), \
392 UA_OPTIONAL)
6c61d2a5 393
35410306 394/*
d108dac0
JG
395 * =======================================
396 * Declaration helpers
397 * =======================================
35410306 398 */
6c61d2a5
JG
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 }
09e3ebf8 408
fac9658c
MB
409/* =================================================
410 * Parsing infrastructure
411 * =================================================
412 */
413
3a863577 414
fac9658c 415struct uverbs_ptr_attr {
8762d149
MB
416 /*
417 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
418 * used.
419 */
420 union {
421 void *ptr;
422 u64 data;
423 };
fac9658c 424 u16 len;
6a1f444f 425 u16 uattr_idx;
494c5580 426 u8 enum_id;
fac9658c
MB
427};
428
f43dbebf
MB
429struct uverbs_obj_attr {
430 struct ib_uobject *uobject;
3a863577 431 const struct uverbs_api_attr *attr_elm;
f43dbebf
MB
432};
433
434struct uverbs_attr {
fac9658c
MB
435 union {
436 struct uverbs_ptr_attr ptr_attr;
437 struct uverbs_obj_attr obj_attr;
438 };
f43dbebf
MB
439};
440
f43dbebf 441struct uverbs_attr_bundle {
4b3dd2bb 442 struct ib_uverbs_file *ufile;
3a863577
JG
443 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN);
444 struct uverbs_attr attrs[];
f43dbebf
MB
445};
446
35410306
MB
447static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
448 unsigned int idx)
449{
3a863577
JG
450 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)),
451 attrs_bundle->attr_present);
35410306
MB
452}
453
41b2a71f
MB
454#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
455
d70724f1
MB
456static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
457 u16 idx)
458{
d70724f1
MB
459 if (!uverbs_attr_is_valid(attrs_bundle, idx))
460 return ERR_PTR(-ENOENT);
461
3a863577 462 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))];
d70724f1
MB
463}
464
494c5580
MB
465static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
466 u16 idx)
467{
468 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
469
470 if (IS_ERR(attr))
471 return PTR_ERR(attr);
472
473 return attr->ptr_attr.enum_id;
474}
475
be934cca
AL
476static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
477 u16 idx)
478{
f4602cbb 479 const struct uverbs_attr *attr;
be934cca 480
f4602cbb
JG
481 attr = uverbs_attr_get(attrs_bundle, idx);
482 if (IS_ERR(attr))
483 return ERR_CAST(attr);
be934cca 484
f4602cbb 485 return attr->obj_attr.uobject->object;
be934cca
AL
486}
487
3efa3812
MB
488static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
489 u16 idx)
490{
491 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
492
493 if (IS_ERR(attr))
494 return ERR_CAST(attr);
495
496 return attr->obj_attr.uobject;
497}
498
8762d149
MB
499static inline int
500uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
501{
502 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
503
504 if (IS_ERR(attr))
505 return PTR_ERR(attr);
506
507 return attr->ptr_attr.len;
508}
509
89d9e8d3
MB
510static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
511{
512 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
513}
514
8762d149
MB
515static inline void *uverbs_attr_get_alloced_ptr(
516 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
517{
518 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
519
520 if (IS_ERR(attr))
521 return (void *)attr;
522
523 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
524 attr->ptr_attr.ptr;
525}
526
89d9e8d3 527static inline int _uverbs_copy_from(void *to,
d70724f1 528 const struct uverbs_attr_bundle *attrs_bundle,
89d9e8d3
MB
529 size_t idx,
530 size_t size)
d70724f1
MB
531{
532 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
533
534 if (IS_ERR(attr))
535 return PTR_ERR(attr);
536
89d9e8d3
MB
537 /*
538 * Validation ensures attr->ptr_attr.len >= size. If the caller is
c66db311
MB
539 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
540 * uverbs_copy_from_or_zero.
89d9e8d3
MB
541 */
542 if (unlikely(size < attr->ptr_attr.len))
543 return -EINVAL;
544
545 if (uverbs_attr_ptr_is_inline(attr))
d70724f1 546 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
2f36028c
JG
547 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
548 attr->ptr_attr.len))
d70724f1
MB
549 return -EFAULT;
550
551 return 0;
552}
553
c66db311
MB
554static inline int _uverbs_copy_from_or_zero(void *to,
555 const struct uverbs_attr_bundle *attrs_bundle,
556 size_t idx,
557 size_t size)
558{
559 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
560 size_t min_size;
561
562 if (IS_ERR(attr))
563 return PTR_ERR(attr);
564
565 min_size = min_t(size_t, size, attr->ptr_attr.len);
566
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),
570 min_size))
571 return -EFAULT;
572
573 if (size > min_size)
574 memset(to + min_size, 0, size - min_size);
575
576 return 0;
577}
578
d70724f1 579#define uverbs_copy_from(to, attrs_bundle, idx) \
89d9e8d3 580 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
d70724f1 581
c66db311
MB
582#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
583 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
584
bccd0622
JG
585#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
586int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
587 size_t idx, u64 allowed_bits);
588int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
589 size_t idx, u64 allowed_bits);
6a1f444f
JG
590int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx,
591 const void *from, size_t size);
461bb2ee
JG
592__malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size,
593 gfp_t flags);
594
595static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
596 size_t size)
597{
598 return _uverbs_alloc(bundle, size, GFP_KERNEL);
599}
600
601static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
602 size_t size)
603{
604 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO);
605}
bccd0622
JG
606#else
607static inline int
608uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle,
609 size_t idx, u64 allowed_bits)
610{
611 return -EINVAL;
612}
613static inline int
614uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle,
615 size_t idx, u64 allowed_bits)
616{
617 return -EINVAL;
618}
6a1f444f
JG
619static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
620 size_t idx, const void *from, size_t size)
621{
622 return -EINVAL;
623}
461bb2ee
JG
624static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle,
625 size_t size)
626{
627 return ERR_PTR(-EINVAL);
628}
629static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle,
630 size_t size)
631{
632 return ERR_PTR(-EINVAL);
633}
bccd0622
JG
634#endif
635
118620d3 636#endif