blob: 392936ad25baf8cca08fdd8ad5b6095e3aa0a918 [file] [log] [blame]
Matan Baraka0aa3092017-08-03 16:06:55 +03001/*
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>
Matan Barak35410302017-08-03 16:07:01 +030037#include <linux/uaccess.h>
38#include <rdma/rdma_user_ioctl.h>
Matan Barakd70724f2017-08-03 16:07:04 +030039#include <rdma/ib_user_ioctl_verbs.h>
Matan Barak1f7ff9d2018-03-19 15:02:33 +020040#include <rdma/ib_user_ioctl_cmds.h>
Matan Baraka0aa3092017-08-03 16:06:55 +030041
42/*
43 * =======================================
44 * Verbs action specifications
45 * =======================================
46 */
47
Matan Barakf43dbeb2017-08-03 16:06:56 +030048enum uverbs_attr_type {
49 UVERBS_ATTR_TYPE_NA,
Matan Barakfac96582017-08-03 16:06:57 +030050 UVERBS_ATTR_TYPE_PTR_IN,
51 UVERBS_ATTR_TYPE_PTR_OUT,
Matan Barakf43dbeb2017-08-03 16:06:56 +030052 UVERBS_ATTR_TYPE_IDR,
53 UVERBS_ATTR_TYPE_FD,
Matan Barak494c5582018-03-28 09:27:42 +030054 UVERBS_ATTR_TYPE_ENUM_IN,
Matan Barakf43dbeb2017-08-03 16:06:56 +030055};
56
Matan Baraka0aa3092017-08-03 16:06:55 +030057enum uverbs_obj_access {
58 UVERBS_ACCESS_READ,
59 UVERBS_ACCESS_WRITE,
60 UVERBS_ACCESS_NEW,
61 UVERBS_ACCESS_DESTROY
62};
63
Matan Barakfac96582017-08-03 16:06:57 +030064enum {
65 UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
Matan Barakc66db312018-03-19 15:02:36 +020066 /* Support extending attributes by length, validate all unknown size == zero */
67 UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1,
Matan Barak8762d142018-06-17 12:59:52 +030068 /*
69 * Valid only for PTR_IN. Allocate and copy the data inside the parser
70 */
71 UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY = 1U << 2,
Matan Barakfac96582017-08-03 16:06:57 +030072};
73
Matan Barak1f07e08f2018-03-19 15:02:35 +020074/* Specification of a single attribute inside the ioctl message */
Matan Barakf43dbeb2017-08-03 16:06:56 +030075struct uverbs_attr_spec {
Jason Gunthorped108dac2018-07-04 08:50:25 +030076 u8 type;
77 u8 flags;
78
Matan Barakfac96582017-08-03 16:06:57 +030079 union {
Matan Barakfac96582017-08-03 16:06:57 +030080 struct {
Matan Barakc66db312018-03-19 15:02:36 +020081 /* Current known size to kernel */
Jason Gunthorped108dac2018-07-04 08:50:25 +030082 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020083 /* User isn't allowed to provide something < min_len */
Jason Gunthorped108dac2018-07-04 08:50:25 +030084 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020085 } ptr;
Jason Gunthorped108dac2018-07-04 08:50:25 +030086
Matan Barak1f07e08f2018-03-19 15:02:35 +020087 struct {
Matan Barakfac96582017-08-03 16:06:57 +030088 /*
89 * higher bits mean the namespace and lower bits mean
90 * the type id within the namespace.
91 */
Jason Gunthorped108dac2018-07-04 08:50:25 +030092 u16 obj_type;
93 u8 access;
Matan Barakfac96582017-08-03 16:06:57 +030094 } obj;
Jason Gunthorped108dac2018-07-04 08:50:25 +030095
Matan Barak494c5582018-03-28 09:27:42 +030096 struct {
Jason Gunthorped108dac2018-07-04 08:50:25 +030097 u8 num_elems;
98 } enum_def;
99 } u;
100
101 /* This weird split of the enum lets us remove some padding */
102 union {
103 struct {
Matan Barak494c5582018-03-28 09:27:42 +0300104 /*
105 * The enum attribute can select one of the attributes
106 * contained in the ids array. Currently only PTR_IN
107 * attributes are supported in the ids array.
108 */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300109 const struct uverbs_attr_spec *ids;
Matan Barak494c5582018-03-28 09:27:42 +0300110 } enum_def;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300111 } u2;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300112};
113
114struct uverbs_attr_spec_hash {
115 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +0300116 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300117 struct uverbs_attr_spec attrs[0];
118};
119
Matan Barakfac96582017-08-03 16:06:57 +0300120struct uverbs_attr_bundle;
121struct ib_uverbs_file;
122
123enum {
124 /*
125 * Action marked with this flag creates a context (or root for all
126 * objects).
127 */
128 UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
129};
130
131struct uverbs_method_spec {
132 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
133 u32 flags;
134 size_t num_buckets;
135 size_t num_child_attrs;
136 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
137 struct uverbs_attr_bundle *ctx);
138 struct uverbs_attr_spec_hash *attr_buckets[0];
139};
140
141struct uverbs_method_spec_hash {
142 size_t num_methods;
143 struct uverbs_method_spec *methods[0];
144};
145
146struct uverbs_object_spec {
147 const struct uverbs_obj_type *type_attrs;
148 size_t num_buckets;
149 struct uverbs_method_spec_hash *method_buckets[0];
150};
151
152struct uverbs_object_spec_hash {
153 size_t num_objects;
154 struct uverbs_object_spec *objects[0];
155};
156
157struct uverbs_root_spec {
158 size_t num_buckets;
159 struct uverbs_object_spec_hash *object_buckets[0];
160};
161
Matan Barak50090102017-08-03 16:06:58 +0300162/*
163 * =======================================
164 * Verbs definitions
165 * =======================================
166 */
167
Matan Barak09e3ebf2017-08-03 16:06:59 +0300168struct uverbs_attr_def {
169 u16 id;
170 struct uverbs_attr_spec attr;
171};
172
173struct uverbs_method_def {
174 u16 id;
175 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
176 u32 flags;
177 size_t num_attrs;
178 const struct uverbs_attr_def * const (*attrs)[];
179 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
180 struct uverbs_attr_bundle *ctx);
181};
182
Matan Barak50090102017-08-03 16:06:58 +0300183struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300184 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300185 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300186 size_t num_methods;
187 const struct uverbs_method_def * const (*methods)[];
188};
189
190struct uverbs_object_tree_def {
191 size_t num_objects;
192 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300193};
194
Jason Gunthorped108dac2018-07-04 08:50:25 +0300195/*
196 * =======================================
197 * Attribute Specifications
198 * =======================================
199 */
Matan Barakc66db312018-03-19 15:02:36 +0200200
Jason Gunthorped108dac2018-07-04 08:50:25 +0300201/* Use in the _type parameter for attribute specifications */
Matan Barakc66db312018-03-19 15:02:36 +0200202#define UVERBS_ATTR_TYPE(_type) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300203 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
Matan Barakc66db312018-03-19 15:02:36 +0200204#define UVERBS_ATTR_STRUCT(_type, _last) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300205 .u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type)
Matan Barakc66db312018-03-19 15:02:36 +0200206#define UVERBS_ATTR_SIZE(_min_len, _len) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300207 .u.ptr.min_len = _min_len, .u.ptr.len = _len
Matan Barak2d9c1bd2018-06-17 12:59:53 +0300208#define UVERBS_ATTR_MIN_SIZE(_min_len) \
209 UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
Matan Barakc66db312018-03-19 15:02:36 +0200210
Jason Gunthorped108dac2018-07-04 08:50:25 +0300211/* Must be used in the '...' of any UVERBS_ATTR */
212#define UA_FLAGS(_flags) .flags = _flags
213
214#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
215 ((const struct uverbs_attr_def){ \
216 .id = _attr_id, \
217 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
218 .u.obj.obj_type = _idr_type, \
219 .u.obj.access = _access, \
220 __VA_ARGS__ } })
221
222#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
223 ((const struct uverbs_attr_def){ \
224 .id = (_attr_id) + \
225 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
226 (_access) != UVERBS_ACCESS_READ), \
227 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
228 .u.obj.obj_type = _fd_type, \
229 .u.obj.access = _access, \
230 __VA_ARGS__ } })
231
232#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
233 ((const struct uverbs_attr_def){ \
234 .id = _attr_id, \
235 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
236 _type, \
237 __VA_ARGS__ } })
238
239#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
240 ((const struct uverbs_attr_def){ \
241 .id = _attr_id, \
242 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
243 _type, \
244 __VA_ARGS__ } })
245
246/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
247#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
248 ((const struct uverbs_attr_def){ \
249 .id = _attr_id, \
250 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
251 .u2.enum_def.ids = _enum_arr, \
252 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
253 __VA_ARGS__ }, \
254 })
Matan Barak35410302017-08-03 16:07:01 +0300255
256/*
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300257 * This spec is used in order to pass information to the hardware driver in a
258 * legacy way. Every verb that could get driver specific data should get this
259 * spec.
260 */
261#define UVERBS_ATTR_UHW() \
262 &UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
263 UVERBS_ATTR_SIZE(0, USHRT_MAX), \
264 UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO)), \
265 &UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
266 UVERBS_ATTR_SIZE(0, USHRT_MAX), \
267 UA_FLAGS(UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO))
268
269/*
Jason Gunthorped108dac2018-07-04 08:50:25 +0300270 * =======================================
271 * Declaration helpers
272 * =======================================
Matan Barak35410302017-08-03 16:07:01 +0300273 */
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300274
275#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
276 static const struct uverbs_object_def *const _name##_ptr[] = { \
277 __VA_ARGS__, \
278 }; \
279 static const struct uverbs_object_tree_def _name = { \
280 .num_objects = ARRAY_SIZE(_name##_ptr), \
281 .objects = &_name##_ptr, \
282 }
Matan Barak09e3ebf2017-08-03 16:06:59 +0300283
Matan Barakfac96582017-08-03 16:06:57 +0300284/* =================================================
285 * Parsing infrastructure
286 * =================================================
287 */
288
289struct uverbs_ptr_attr {
Matan Barak8762d142018-06-17 12:59:52 +0300290 /*
291 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
292 * used.
293 */
294 union {
295 void *ptr;
296 u64 data;
297 };
Matan Barakfac96582017-08-03 16:06:57 +0300298 u16 len;
299 /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
300 u16 flags;
Matan Barak494c5582018-03-28 09:27:42 +0300301 u8 enum_id;
Matan Barakfac96582017-08-03 16:06:57 +0300302};
303
Matan Barakf43dbeb2017-08-03 16:06:56 +0300304struct uverbs_obj_attr {
305 struct ib_uobject *uobject;
306};
307
308struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300309 /*
310 * pointer to the user-space given attribute, in order to write the
311 * new uobject's id or update flags.
312 */
313 struct ib_uverbs_attr __user *uattr;
314 union {
315 struct uverbs_ptr_attr ptr_attr;
316 struct uverbs_obj_attr obj_attr;
317 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300318};
319
320struct uverbs_attr_bundle_hash {
321 /* if bit i is set, it means attrs[i] contains valid information */
322 unsigned long *valid_bitmap;
323 size_t num_attrs;
324 /*
325 * arrays of attributes, each element corresponds to the specification
326 * of the attribute in the same index.
327 */
328 struct uverbs_attr *attrs;
329};
330
331struct uverbs_attr_bundle {
332 size_t num_buckets;
333 struct uverbs_attr_bundle_hash hash[];
334};
335
336static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
337 unsigned int idx)
338{
339 return test_bit(idx, attrs_hash->valid_bitmap);
340}
341
Matan Barak35410302017-08-03 16:07:01 +0300342static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
343 unsigned int idx)
344{
345 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
346
347 if (attrs_bundle->num_buckets <= idx_bucket)
348 return false;
349
350 return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
351 idx & ~UVERBS_ID_NS_MASK);
352}
353
Matan Barak41b2a712018-03-19 15:02:38 +0200354#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
355
Matan Barakd70724f2017-08-03 16:07:04 +0300356static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
357 u16 idx)
358{
359 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
360
361 if (!uverbs_attr_is_valid(attrs_bundle, idx))
362 return ERR_PTR(-ENOENT);
363
364 return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
365}
366
Matan Barak494c5582018-03-28 09:27:42 +0300367static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
368 u16 idx)
369{
370 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
371
372 if (IS_ERR(attr))
373 return PTR_ERR(attr);
374
375 return attr->ptr_attr.enum_id;
376}
377
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300378static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
379 u16 idx)
380{
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600381 const struct uverbs_attr *attr;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300382
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600383 attr = uverbs_attr_get(attrs_bundle, idx);
384 if (IS_ERR(attr))
385 return ERR_CAST(attr);
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300386
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600387 return attr->obj_attr.uobject->object;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300388}
389
Matan Barak3efa3812018-05-31 16:43:28 +0300390static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
391 u16 idx)
392{
393 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
394
395 if (IS_ERR(attr))
396 return ERR_CAST(attr);
397
398 return attr->obj_attr.uobject;
399}
400
Matan Barak8762d142018-06-17 12:59:52 +0300401static inline int
402uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
403{
404 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
405
406 if (IS_ERR(attr))
407 return PTR_ERR(attr);
408
409 return attr->ptr_attr.len;
410}
411
Matan Barakd70724f2017-08-03 16:07:04 +0300412static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200413 size_t idx, const void *from, size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300414{
415 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
416 u16 flags;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200417 size_t min_size;
Matan Barakd70724f2017-08-03 16:07:04 +0300418
419 if (IS_ERR(attr))
420 return PTR_ERR(attr);
421
Matan Barak89d9e8d2018-02-13 12:18:29 +0200422 min_size = min_t(size_t, attr->ptr_attr.len, size);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200423 if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
Matan Barak89d9e8d2018-02-13 12:18:29 +0200424 return -EFAULT;
425
Matan Barakd70724f2017-08-03 16:07:04 +0300426 flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200427 if (put_user(flags, &attr->uattr->flags))
428 return -EFAULT;
429
430 return 0;
Matan Barakd70724f2017-08-03 16:07:04 +0300431}
432
Matan Barak89d9e8d2018-02-13 12:18:29 +0200433static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
434{
435 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
436}
437
Matan Barak8762d142018-06-17 12:59:52 +0300438static inline void *uverbs_attr_get_alloced_ptr(
439 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
440{
441 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
442
443 if (IS_ERR(attr))
444 return (void *)attr;
445
446 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
447 attr->ptr_attr.ptr;
448}
449
Matan Barak89d9e8d2018-02-13 12:18:29 +0200450static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300451 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200452 size_t idx,
453 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300454{
455 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
456
457 if (IS_ERR(attr))
458 return PTR_ERR(attr);
459
Matan Barak89d9e8d2018-02-13 12:18:29 +0200460 /*
461 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200462 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
463 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200464 */
465 if (unlikely(size < attr->ptr_attr.len))
466 return -EINVAL;
467
468 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300469 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200470 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
471 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300472 return -EFAULT;
473
474 return 0;
475}
476
Matan Barakc66db312018-03-19 15:02:36 +0200477static inline int _uverbs_copy_from_or_zero(void *to,
478 const struct uverbs_attr_bundle *attrs_bundle,
479 size_t idx,
480 size_t size)
481{
482 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
483 size_t min_size;
484
485 if (IS_ERR(attr))
486 return PTR_ERR(attr);
487
488 min_size = min_t(size_t, size, attr->ptr_attr.len);
489
490 if (uverbs_attr_ptr_is_inline(attr))
491 memcpy(to, &attr->ptr_attr.data, min_size);
492 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
493 min_size))
494 return -EFAULT;
495
496 if (size > min_size)
497 memset(to + min_size, 0, size - min_size);
498
499 return 0;
500}
501
Matan Barakd70724f2017-08-03 16:07:04 +0300502#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200503 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300504
Matan Barakc66db312018-03-19 15:02:36 +0200505#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
506 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
507
Matan Barak118620d2017-08-03 16:07:00 +0300508/* =================================================
509 * Definitions -> Specs infrastructure
510 * =================================================
511 */
Matan Baraka0aa3092017-08-03 16:06:55 +0300512
Matan Barak118620d2017-08-03 16:07:00 +0300513/*
514 * uverbs_alloc_spec_tree - Merges different common and driver specific feature
515 * into one parsing tree that every uverbs command will be parsed upon.
516 *
517 * @num_trees: Number of trees in the array @trees.
518 * @trees: Array of pointers to tree root definitions to merge. Each such tree
519 * possibly contains objects, methods and attributes definitions.
520 *
521 * Returns:
522 * uverbs_root_spec *: The root of the merged parsing tree.
523 * On error, we return an error code. Error is checked via IS_ERR.
524 *
525 * The following merges could take place:
526 * a. Two trees representing the same method with different handler
527 * -> We take the handler of the tree that its handler != NULL
528 * and its index in the trees array is greater. The incentive for that
529 * is that developers are expected to first merge common trees and then
530 * merge trees that gives specialized the behaviour.
531 * b. Two trees representing the same object with different
532 * type_attrs (struct uverbs_obj_type):
533 * -> We take the type_attrs of the tree that its type_attr != NULL
534 * and its index in the trees array is greater. This could be used
535 * in order to override the free function, allocation size, etc.
536 * c. Two trees representing the same method attribute (same id but possibly
537 * different attributes):
538 * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
539 *
540 * An object without any methods is considered invalid and will abort the
541 * function with -ENOENT error.
542 */
Matan Barak52427112017-08-03 16:07:06 +0300543#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Matan Barak118620d2017-08-03 16:07:00 +0300544struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
545 const struct uverbs_object_tree_def **trees);
546void uverbs_free_spec_tree(struct uverbs_root_spec *root);
Matan Barak52427112017-08-03 16:07:06 +0300547#else
548static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
549 const struct uverbs_object_tree_def **trees)
550{
551 return NULL;
552}
553
554static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
555{
556}
557#endif
Matan Barak118620d2017-08-03 16:07:00 +0300558
559#endif