blob: faaaec7be36a657f0bcccb3ef49c3027ef1c918d [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,
54};
55
Matan Baraka0aa3092017-08-03 16:06:55 +030056enum uverbs_obj_access {
57 UVERBS_ACCESS_READ,
58 UVERBS_ACCESS_WRITE,
59 UVERBS_ACCESS_NEW,
60 UVERBS_ACCESS_DESTROY
61};
62
Matan Barakfac96582017-08-03 16:06:57 +030063enum {
64 UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
Matan Barakc66db312018-03-19 15:02:36 +020065 /* Support extending attributes by length, validate all unknown size == zero */
66 UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1,
Matan Barakfac96582017-08-03 16:06:57 +030067};
68
Matan Barak1f07e08f2018-03-19 15:02:35 +020069/* Specification of a single attribute inside the ioctl message */
Matan Barakf43dbeb2017-08-03 16:06:56 +030070struct uverbs_attr_spec {
Matan Barakfac96582017-08-03 16:06:57 +030071 union {
Matan Barak1f07e08f2018-03-19 15:02:35 +020072 /* Header shared by all following union members - to reduce space. */
Matan Barakfac96582017-08-03 16:06:57 +030073 struct {
Matan Barak1f07e08f2018-03-19 15:02:35 +020074 enum uverbs_attr_type type;
75 /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
76 u8 flags;
77 };
78 struct {
79 enum uverbs_attr_type type;
80 /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
81 u8 flags;
Matan Barakc66db312018-03-19 15:02:36 +020082 /* Current known size to kernel */
Matan Barak1f07e08f2018-03-19 15:02:35 +020083 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020084 /* User isn't allowed to provide something < min_len */
85 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020086 } ptr;
87 struct {
88 enum uverbs_attr_type type;
89 /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
90 u8 flags;
Matan Barakfac96582017-08-03 16:06:57 +030091 /*
92 * higher bits mean the namespace and lower bits mean
93 * the type id within the namespace.
94 */
95 u16 obj_type;
96 u8 access;
97 } obj;
98 };
Matan Barakf43dbeb2017-08-03 16:06:56 +030099};
100
101struct uverbs_attr_spec_hash {
102 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +0300103 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300104 struct uverbs_attr_spec attrs[0];
105};
106
Matan Barakfac96582017-08-03 16:06:57 +0300107struct uverbs_attr_bundle;
108struct ib_uverbs_file;
109
110enum {
111 /*
112 * Action marked with this flag creates a context (or root for all
113 * objects).
114 */
115 UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
116};
117
118struct uverbs_method_spec {
119 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
120 u32 flags;
121 size_t num_buckets;
122 size_t num_child_attrs;
123 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
124 struct uverbs_attr_bundle *ctx);
125 struct uverbs_attr_spec_hash *attr_buckets[0];
126};
127
128struct uverbs_method_spec_hash {
129 size_t num_methods;
130 struct uverbs_method_spec *methods[0];
131};
132
133struct uverbs_object_spec {
134 const struct uverbs_obj_type *type_attrs;
135 size_t num_buckets;
136 struct uverbs_method_spec_hash *method_buckets[0];
137};
138
139struct uverbs_object_spec_hash {
140 size_t num_objects;
141 struct uverbs_object_spec *objects[0];
142};
143
144struct uverbs_root_spec {
145 size_t num_buckets;
146 struct uverbs_object_spec_hash *object_buckets[0];
147};
148
Matan Barak50090102017-08-03 16:06:58 +0300149/*
150 * =======================================
151 * Verbs definitions
152 * =======================================
153 */
154
Matan Barak09e3ebf2017-08-03 16:06:59 +0300155struct uverbs_attr_def {
156 u16 id;
157 struct uverbs_attr_spec attr;
158};
159
160struct uverbs_method_def {
161 u16 id;
162 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
163 u32 flags;
164 size_t num_attrs;
165 const struct uverbs_attr_def * const (*attrs)[];
166 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
167 struct uverbs_attr_bundle *ctx);
168};
169
Matan Barak50090102017-08-03 16:06:58 +0300170struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300171 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300172 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300173 size_t num_methods;
174 const struct uverbs_method_def * const (*methods)[];
175};
176
177struct uverbs_object_tree_def {
178 size_t num_objects;
179 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300180};
181
Matan Barak35410302017-08-03 16:07:01 +0300182#define UA_FLAGS(_flags) .flags = _flags
Matan Barakc66db312018-03-19 15:02:36 +0200183#define __UVERBS_ATTR0(_id, _type, _fld, _attr, ...) \
Matan Barak35410302017-08-03 16:07:01 +0300184 ((const struct uverbs_attr_def) \
Matan Barakc66db312018-03-19 15:02:36 +0200185 {.id = _id, .attr = {{._fld = {.type = _type, _attr, .flags = 0, } }, } })
186#define __UVERBS_ATTR1(_id, _type, _fld, _attr, _extra1, ...) \
Matan Barak35410302017-08-03 16:07:01 +0300187 ((const struct uverbs_attr_def) \
Matan Barakc66db312018-03-19 15:02:36 +0200188 {.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1 } },} })
189#define __UVERBS_ATTR2(_id, _type, _fld, _attr, _extra1, _extra2) \
190 ((const struct uverbs_attr_def) \
191 {.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1, _extra2 } },} })
192#define __UVERBS_ATTR(_id, _type, _fld, _attr, _extra1, _extra2, _n, ...) \
193 __UVERBS_ATTR##_n(_id, _type, _fld, _attr, _extra1, _extra2)
194
195#define UVERBS_ATTR_TYPE(_type) \
196 .min_len = sizeof(_type), .len = sizeof(_type)
197#define UVERBS_ATTR_STRUCT(_type, _last) \
198 .min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .len = sizeof(_type)
199#define UVERBS_ATTR_SIZE(_min_len, _len) \
200 .min_len = _min_len, .len = _len
201
Matan Barak35410302017-08-03 16:07:01 +0300202/*
203 * In new compiler, UVERBS_ATTR could be simplified by declaring it as
204 * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
205 * But since we support older compilers too, we need the more complex code.
206 */
Matan Barakc66db312018-03-19 15:02:36 +0200207#define UVERBS_ATTR(_id, _type, _fld, _attr, ...) \
208 __UVERBS_ATTR(_id, _type, _fld, _attr, ##__VA_ARGS__, 2, 1, 0)
Matan Barak35410302017-08-03 16:07:01 +0300209#define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \
Matan Barakc66db312018-03-19 15:02:36 +0200210 UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_IN, ptr, _len, ##__VA_ARGS__)
Matan Barak35410302017-08-03 16:07:01 +0300211/* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
212#define UVERBS_ATTR_PTR_IN(_id, _type, ...) \
Matan Barakc66db312018-03-19 15:02:36 +0200213 UVERBS_ATTR_PTR_IN_SZ(_id, _type, ##__VA_ARGS__)
Matan Barak35410302017-08-03 16:07:01 +0300214#define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \
Matan Barakc66db312018-03-19 15:02:36 +0200215 UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_OUT, ptr, _len, ##__VA_ARGS__)
Matan Barak35410302017-08-03 16:07:01 +0300216#define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \
Matan Barakc66db312018-03-19 15:02:36 +0200217 UVERBS_ATTR_PTR_OUT_SZ(_id, _type, ##__VA_ARGS__)
Matan Barak35410302017-08-03 16:07:01 +0300218
219/*
220 * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
221 * it as
222 * {.id = _id, \
223 * .attr {.type = __obj_class, \
224 * .obj = {.obj_type = _idr_type, \
225 * .access = _access \
226 * }, ##__VA_ARGS__ } }
227 * But since we support older compilers too, we need the more complex code.
228 */
229#define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
230 ((const struct uverbs_attr_def) \
231 {.id = _id, \
Matan Barak1f07e08f2018-03-19 15:02:35 +0200232 .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
233 .access = _access, .flags = 0 } }, } })
Matan Barak35410302017-08-03 16:07:01 +0300234#define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
235 ((const struct uverbs_attr_def) \
236 {.id = _id, \
Matan Barak1f07e08f2018-03-19 15:02:35 +0200237 .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
238 .access = _access, _flags} }, } })
Matan Barak35410302017-08-03 16:07:01 +0300239#define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
240 _n, ...) \
241 ___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
242#define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \
243 ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \
244 ##__VA_ARGS__, 1, 0)
245#define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \
246 __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
247 ##__VA_ARGS__)
248#define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \
249 __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \
250 (_access) + BUILD_BUG_ON_ZERO( \
251 (_access) != UVERBS_ACCESS_NEW && \
252 (_access) != UVERBS_ACCESS_READ), \
253 ##__VA_ARGS__)
254#define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \
255 const struct uverbs_attr_def _name = __VA_ARGS__
256
257#define _UVERBS_METHOD_ATTRS_SZ(...) \
258 (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
259 sizeof(const struct uverbs_attr_def *))
260#define _UVERBS_METHOD(_id, _handler, _flags, ...) \
261 ((const struct uverbs_method_def) { \
262 .id = _id, \
263 .flags = _flags, \
264 .handler = _handler, \
265 .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \
266 .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
267#define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \
268 const struct uverbs_method_def _name = \
269 _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
270#define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \
271 const struct uverbs_method_def _name = \
272 _UVERBS_METHOD(_id, _handler, \
273 UVERBS_ACTION_FLAG_CREATE_ROOT, \
274 ##__VA_ARGS__)
275#define _UVERBS_OBJECT_METHODS_SZ(...) \
276 (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
277 sizeof(const struct uverbs_method_def *))
Matan Barak50090102017-08-03 16:06:58 +0300278#define _UVERBS_OBJECT(_id, _type_attrs, ...) \
279 ((const struct uverbs_object_def) { \
Matan Barak09e3ebf2017-08-03 16:06:59 +0300280 .id = _id, \
Matan Barak35410302017-08-03 16:07:01 +0300281 .type_attrs = _type_attrs, \
282 .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \
283 .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
Matan Barak50090102017-08-03 16:06:58 +0300284#define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
285 const struct uverbs_object_def _name = \
286 _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
Matan Barak09e3ebf2017-08-03 16:06:59 +0300287#define _UVERBS_TREE_OBJECTS_SZ(...) \
288 (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
289 sizeof(const struct uverbs_object_def *))
290#define _UVERBS_OBJECT_TREE(...) \
291 ((const struct uverbs_object_tree_def) { \
292 .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
293 .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
294#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
295 const struct uverbs_object_tree_def _name = \
296 _UVERBS_OBJECT_TREE(__VA_ARGS__)
297
Matan Barakfac96582017-08-03 16:06:57 +0300298/* =================================================
299 * Parsing infrastructure
300 * =================================================
301 */
302
303struct uverbs_ptr_attr {
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200304 u64 data;
Matan Barakfac96582017-08-03 16:06:57 +0300305 u16 len;
306 /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
307 u16 flags;
308};
309
Matan Barakf43dbeb2017-08-03 16:06:56 +0300310struct uverbs_obj_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300311 /* pointer to the kernel descriptor -> type, access, etc */
312 const struct uverbs_obj_type *type;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300313 struct ib_uobject *uobject;
Matan Barakfac96582017-08-03 16:06:57 +0300314 /* fd or id in idr of this object */
315 int id;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300316};
317
318struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300319 /*
320 * pointer to the user-space given attribute, in order to write the
321 * new uobject's id or update flags.
322 */
323 struct ib_uverbs_attr __user *uattr;
324 union {
325 struct uverbs_ptr_attr ptr_attr;
326 struct uverbs_obj_attr obj_attr;
327 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300328};
329
330struct uverbs_attr_bundle_hash {
331 /* if bit i is set, it means attrs[i] contains valid information */
332 unsigned long *valid_bitmap;
333 size_t num_attrs;
334 /*
335 * arrays of attributes, each element corresponds to the specification
336 * of the attribute in the same index.
337 */
338 struct uverbs_attr *attrs;
339};
340
341struct uverbs_attr_bundle {
342 size_t num_buckets;
343 struct uverbs_attr_bundle_hash hash[];
344};
345
346static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
347 unsigned int idx)
348{
349 return test_bit(idx, attrs_hash->valid_bitmap);
350}
351
Matan Barak35410302017-08-03 16:07:01 +0300352static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
353 unsigned int idx)
354{
355 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
356
357 if (attrs_bundle->num_buckets <= idx_bucket)
358 return false;
359
360 return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
361 idx & ~UVERBS_ID_NS_MASK);
362}
363
Matan Barak41b2a712018-03-19 15:02:38 +0200364#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
365
Matan Barakd70724f2017-08-03 16:07:04 +0300366static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
367 u16 idx)
368{
369 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
370
371 if (!uverbs_attr_is_valid(attrs_bundle, idx))
372 return ERR_PTR(-ENOENT);
373
374 return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
375}
376
377static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200378 size_t idx, const void *from, size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300379{
380 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
381 u16 flags;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200382 size_t min_size;
Matan Barakd70724f2017-08-03 16:07:04 +0300383
384 if (IS_ERR(attr))
385 return PTR_ERR(attr);
386
Matan Barak89d9e8d2018-02-13 12:18:29 +0200387 min_size = min_t(size_t, attr->ptr_attr.len, size);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200388 if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
Matan Barak89d9e8d2018-02-13 12:18:29 +0200389 return -EFAULT;
390
Matan Barakd70724f2017-08-03 16:07:04 +0300391 flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200392 if (put_user(flags, &attr->uattr->flags))
393 return -EFAULT;
394
395 return 0;
Matan Barakd70724f2017-08-03 16:07:04 +0300396}
397
Matan Barak89d9e8d2018-02-13 12:18:29 +0200398static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
399{
400 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
401}
402
403static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300404 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200405 size_t idx,
406 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300407{
408 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
409
410 if (IS_ERR(attr))
411 return PTR_ERR(attr);
412
Matan Barak89d9e8d2018-02-13 12:18:29 +0200413 /*
414 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200415 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
416 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200417 */
418 if (unlikely(size < attr->ptr_attr.len))
419 return -EINVAL;
420
421 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300422 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200423 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
424 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300425 return -EFAULT;
426
427 return 0;
428}
429
Matan Barakc66db312018-03-19 15:02:36 +0200430static inline int _uverbs_copy_from_or_zero(void *to,
431 const struct uverbs_attr_bundle *attrs_bundle,
432 size_t idx,
433 size_t size)
434{
435 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
436 size_t min_size;
437
438 if (IS_ERR(attr))
439 return PTR_ERR(attr);
440
441 min_size = min_t(size_t, size, attr->ptr_attr.len);
442
443 if (uverbs_attr_ptr_is_inline(attr))
444 memcpy(to, &attr->ptr_attr.data, min_size);
445 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
446 min_size))
447 return -EFAULT;
448
449 if (size > min_size)
450 memset(to + min_size, 0, size - min_size);
451
452 return 0;
453}
454
Matan Barakd70724f2017-08-03 16:07:04 +0300455#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200456 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300457
Matan Barakc66db312018-03-19 15:02:36 +0200458#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
459 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
460
Matan Barak118620d2017-08-03 16:07:00 +0300461/* =================================================
462 * Definitions -> Specs infrastructure
463 * =================================================
464 */
Matan Baraka0aa3092017-08-03 16:06:55 +0300465
Matan Barak118620d2017-08-03 16:07:00 +0300466/*
467 * uverbs_alloc_spec_tree - Merges different common and driver specific feature
468 * into one parsing tree that every uverbs command will be parsed upon.
469 *
470 * @num_trees: Number of trees in the array @trees.
471 * @trees: Array of pointers to tree root definitions to merge. Each such tree
472 * possibly contains objects, methods and attributes definitions.
473 *
474 * Returns:
475 * uverbs_root_spec *: The root of the merged parsing tree.
476 * On error, we return an error code. Error is checked via IS_ERR.
477 *
478 * The following merges could take place:
479 * a. Two trees representing the same method with different handler
480 * -> We take the handler of the tree that its handler != NULL
481 * and its index in the trees array is greater. The incentive for that
482 * is that developers are expected to first merge common trees and then
483 * merge trees that gives specialized the behaviour.
484 * b. Two trees representing the same object with different
485 * type_attrs (struct uverbs_obj_type):
486 * -> We take the type_attrs of the tree that its type_attr != NULL
487 * and its index in the trees array is greater. This could be used
488 * in order to override the free function, allocation size, etc.
489 * c. Two trees representing the same method attribute (same id but possibly
490 * different attributes):
491 * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
492 *
493 * An object without any methods is considered invalid and will abort the
494 * function with -ENOENT error.
495 */
Matan Barak52427112017-08-03 16:07:06 +0300496#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Matan Barak118620d2017-08-03 16:07:00 +0300497struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
498 const struct uverbs_object_tree_def **trees);
499void uverbs_free_spec_tree(struct uverbs_root_spec *root);
Matan Barak52427112017-08-03 16:07:06 +0300500#else
501static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
502 const struct uverbs_object_tree_def **trees)
503{
504 return NULL;
505}
506
507static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
508{
509}
510#endif
Matan Barak118620d2017-08-03 16:07:00 +0300511
512#endif