blob: 0b46ef8f0b4c54227677591702c77fb4611be239 [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 Barak1f07e08f2018-03-19 15:02:35 +020064/* Specification of a single attribute inside the ioctl message */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030065/* good size 16 */
Matan Barakf43dbeb2017-08-03 16:06:56 +030066struct uverbs_attr_spec {
Jason Gunthorped108dac2018-07-04 08:50:25 +030067 u8 type;
Jason Gunthorpe83bb4442018-07-04 08:50:29 +030068
69 /*
70 * Support extending attributes by length, validate all
71 * unknown size == zero
72 */
73 u8 min_sz_or_zero:1;
74 /*
75 * Valid only for PTR_IN. Allocate and copy the data inside
76 * the parser
77 */
78 u8 alloc_and_copy:1;
79 u8 mandatory:1;
Jason Gunthorped108dac2018-07-04 08:50:25 +030080
Matan Barakfac96582017-08-03 16:06:57 +030081 union {
Matan Barakfac96582017-08-03 16:06:57 +030082 struct {
Matan Barakc66db312018-03-19 15:02:36 +020083 /* Current known size to kernel */
Jason Gunthorped108dac2018-07-04 08:50:25 +030084 u16 len;
Matan Barakc66db312018-03-19 15:02:36 +020085 /* User isn't allowed to provide something < min_len */
Jason Gunthorped108dac2018-07-04 08:50:25 +030086 u16 min_len;
Matan Barak1f07e08f2018-03-19 15:02:35 +020087 } ptr;
Jason Gunthorped108dac2018-07-04 08:50:25 +030088
Matan Barak1f07e08f2018-03-19 15:02:35 +020089 struct {
Matan Barakfac96582017-08-03 16:06:57 +030090 /*
91 * higher bits mean the namespace and lower bits mean
92 * the type id within the namespace.
93 */
Jason Gunthorped108dac2018-07-04 08:50:25 +030094 u16 obj_type;
95 u8 access;
Matan Barakfac96582017-08-03 16:06:57 +030096 } obj;
Jason Gunthorped108dac2018-07-04 08:50:25 +030097
Matan Barak494c5582018-03-28 09:27:42 +030098 struct {
Jason Gunthorped108dac2018-07-04 08:50:25 +030099 u8 num_elems;
100 } enum_def;
101 } u;
102
103 /* This weird split of the enum lets us remove some padding */
104 union {
105 struct {
Matan Barak494c5582018-03-28 09:27:42 +0300106 /*
107 * The enum attribute can select one of the attributes
108 * contained in the ids array. Currently only PTR_IN
109 * attributes are supported in the ids array.
110 */
Jason Gunthorped108dac2018-07-04 08:50:25 +0300111 const struct uverbs_attr_spec *ids;
Matan Barak494c5582018-03-28 09:27:42 +0300112 } enum_def;
Jason Gunthorped108dac2018-07-04 08:50:25 +0300113 } u2;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300114};
115
116struct uverbs_attr_spec_hash {
117 size_t num_attrs;
Matan Barakfac96582017-08-03 16:06:57 +0300118 unsigned long *mandatory_attrs_bitmask;
Matan Barakf43dbeb2017-08-03 16:06:56 +0300119 struct uverbs_attr_spec attrs[0];
120};
121
Matan Barakfac96582017-08-03 16:06:57 +0300122struct uverbs_attr_bundle;
123struct ib_uverbs_file;
124
125enum {
126 /*
127 * Action marked with this flag creates a context (or root for all
128 * objects).
129 */
130 UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
131};
132
133struct uverbs_method_spec {
134 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
135 u32 flags;
136 size_t num_buckets;
137 size_t num_child_attrs;
138 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
139 struct uverbs_attr_bundle *ctx);
140 struct uverbs_attr_spec_hash *attr_buckets[0];
141};
142
143struct uverbs_method_spec_hash {
144 size_t num_methods;
145 struct uverbs_method_spec *methods[0];
146};
147
148struct uverbs_object_spec {
149 const struct uverbs_obj_type *type_attrs;
150 size_t num_buckets;
151 struct uverbs_method_spec_hash *method_buckets[0];
152};
153
154struct uverbs_object_spec_hash {
155 size_t num_objects;
156 struct uverbs_object_spec *objects[0];
157};
158
159struct uverbs_root_spec {
160 size_t num_buckets;
161 struct uverbs_object_spec_hash *object_buckets[0];
162};
163
Matan Barak50090102017-08-03 16:06:58 +0300164/*
165 * =======================================
166 * Verbs definitions
167 * =======================================
168 */
169
Matan Barak09e3ebf2017-08-03 16:06:59 +0300170struct uverbs_attr_def {
171 u16 id;
172 struct uverbs_attr_spec attr;
173};
174
175struct uverbs_method_def {
176 u16 id;
177 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
178 u32 flags;
179 size_t num_attrs;
180 const struct uverbs_attr_def * const (*attrs)[];
181 int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
182 struct uverbs_attr_bundle *ctx);
183};
184
Matan Barak50090102017-08-03 16:06:58 +0300185struct uverbs_object_def {
Matan Barak09e3ebf2017-08-03 16:06:59 +0300186 u16 id;
Matan Barak50090102017-08-03 16:06:58 +0300187 const struct uverbs_obj_type *type_attrs;
Matan Barak09e3ebf2017-08-03 16:06:59 +0300188 size_t num_methods;
189 const struct uverbs_method_def * const (*methods)[];
190};
191
192struct uverbs_object_tree_def {
193 size_t num_objects;
194 const struct uverbs_object_def * const (*objects)[];
Matan Barak50090102017-08-03 16:06:58 +0300195};
196
Jason Gunthorped108dac2018-07-04 08:50:25 +0300197/*
198 * =======================================
199 * Attribute Specifications
200 * =======================================
201 */
Matan Barakc66db312018-03-19 15:02:36 +0200202
Jason Gunthorped108dac2018-07-04 08:50:25 +0300203/* Use in the _type parameter for attribute specifications */
Matan Barakc66db312018-03-19 15:02:36 +0200204#define UVERBS_ATTR_TYPE(_type) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300205 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type)
Matan Barakc66db312018-03-19 15:02:36 +0200206#define UVERBS_ATTR_STRUCT(_type, _last) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300207 .u.ptr.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .u.ptr.len = sizeof(_type)
Matan Barakc66db312018-03-19 15:02:36 +0200208#define UVERBS_ATTR_SIZE(_min_len, _len) \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300209 .u.ptr.min_len = _min_len, .u.ptr.len = _len
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300210/*
211 * Specifies at least min_len bytes must be passed in, but the amount can be
212 * larger, up to the protocol maximum size. No check for zeroing is done.
213 */
214#define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX)
Matan Barakc66db312018-03-19 15:02:36 +0200215
Jason Gunthorped108dac2018-07-04 08:50:25 +0300216/* Must be used in the '...' of any UVERBS_ATTR */
Jason Gunthorpe83bb4442018-07-04 08:50:29 +0300217#define UA_ALLOC_AND_COPY .alloc_and_copy = 1
218#define UA_MANDATORY .mandatory = 1
219#define UA_MIN_SZ_OR_ZERO .min_sz_or_zero = 1
220#define UA_OPTIONAL .mandatory = 0
Jason Gunthorped108dac2018-07-04 08:50:25 +0300221
222#define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300223 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300224 .id = _attr_id, \
225 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \
226 .u.obj.obj_type = _idr_type, \
227 .u.obj.access = _access, \
228 __VA_ARGS__ } })
229
230#define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300231 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300232 .id = (_attr_id) + \
233 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \
234 (_access) != UVERBS_ACCESS_READ), \
235 .attr = { .type = UVERBS_ATTR_TYPE_FD, \
236 .u.obj.obj_type = _fd_type, \
237 .u.obj.access = _access, \
238 __VA_ARGS__ } })
239
240#define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300241 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300242 .id = _attr_id, \
243 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \
244 _type, \
245 __VA_ARGS__ } })
246
247#define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300248 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300249 .id = _attr_id, \
250 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \
251 _type, \
252 __VA_ARGS__ } })
253
254/* _enum_arry should be a 'static const union uverbs_attr_spec[]' */
255#define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300256 (&(const struct uverbs_attr_def){ \
Jason Gunthorped108dac2018-07-04 08:50:25 +0300257 .id = _attr_id, \
258 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \
259 .u2.enum_def.ids = _enum_arr, \
260 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \
261 __VA_ARGS__ }, \
262 })
Matan Barak35410302017-08-03 16:07:01 +0300263
264/*
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300265 * This spec is used in order to pass information to the hardware driver in a
266 * legacy way. Every verb that could get driver specific data should get this
267 * spec.
268 */
269#define UVERBS_ATTR_UHW() \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300270 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300271 UVERBS_ATTR_MIN_SIZE(0), \
272 UA_OPTIONAL), \
Jason Gunthorpe9a119cd2018-07-04 08:50:28 +0300273 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \
Jason Gunthorpe540cd692018-07-04 08:50:30 +0300274 UVERBS_ATTR_MIN_SIZE(0), \
275 UA_OPTIONAL)
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300276
277/*
Jason Gunthorped108dac2018-07-04 08:50:25 +0300278 * =======================================
279 * Declaration helpers
280 * =======================================
Matan Barak35410302017-08-03 16:07:01 +0300281 */
Jason Gunthorpe6c61d2a2018-07-04 08:50:27 +0300282
283#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
284 static const struct uverbs_object_def *const _name##_ptr[] = { \
285 __VA_ARGS__, \
286 }; \
287 static const struct uverbs_object_tree_def _name = { \
288 .num_objects = ARRAY_SIZE(_name##_ptr), \
289 .objects = &_name##_ptr, \
290 }
Matan Barak09e3ebf2017-08-03 16:06:59 +0300291
Matan Barakfac96582017-08-03 16:06:57 +0300292/* =================================================
293 * Parsing infrastructure
294 * =================================================
295 */
296
297struct uverbs_ptr_attr {
Matan Barak8762d142018-06-17 12:59:52 +0300298 /*
299 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is
300 * used.
301 */
302 union {
303 void *ptr;
304 u64 data;
305 };
Matan Barakfac96582017-08-03 16:06:57 +0300306 u16 len;
307 /* Combination of bits from enum UVERBS_ATTR_F_XXXX */
308 u16 flags;
Matan Barak494c5582018-03-28 09:27:42 +0300309 u8 enum_id;
Matan Barakfac96582017-08-03 16:06:57 +0300310};
311
Matan Barakf43dbeb2017-08-03 16:06:56 +0300312struct uverbs_obj_attr {
313 struct ib_uobject *uobject;
314};
315
316struct uverbs_attr {
Matan Barakfac96582017-08-03 16:06:57 +0300317 /*
318 * pointer to the user-space given attribute, in order to write the
319 * new uobject's id or update flags.
320 */
321 struct ib_uverbs_attr __user *uattr;
322 union {
323 struct uverbs_ptr_attr ptr_attr;
324 struct uverbs_obj_attr obj_attr;
325 };
Matan Barakf43dbeb2017-08-03 16:06:56 +0300326};
327
328struct uverbs_attr_bundle_hash {
329 /* if bit i is set, it means attrs[i] contains valid information */
330 unsigned long *valid_bitmap;
331 size_t num_attrs;
332 /*
333 * arrays of attributes, each element corresponds to the specification
334 * of the attribute in the same index.
335 */
336 struct uverbs_attr *attrs;
337};
338
339struct uverbs_attr_bundle {
340 size_t num_buckets;
341 struct uverbs_attr_bundle_hash hash[];
342};
343
344static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
345 unsigned int idx)
346{
347 return test_bit(idx, attrs_hash->valid_bitmap);
348}
349
Matan Barak35410302017-08-03 16:07:01 +0300350static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
351 unsigned int idx)
352{
353 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
354
355 if (attrs_bundle->num_buckets <= idx_bucket)
356 return false;
357
358 return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
359 idx & ~UVERBS_ID_NS_MASK);
360}
361
Matan Barak41b2a712018-03-19 15:02:38 +0200362#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
363
Matan Barakd70724f2017-08-03 16:07:04 +0300364static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
365 u16 idx)
366{
367 u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
368
369 if (!uverbs_attr_is_valid(attrs_bundle, idx))
370 return ERR_PTR(-ENOENT);
371
372 return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
373}
374
Matan Barak494c5582018-03-28 09:27:42 +0300375static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
376 u16 idx)
377{
378 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
379
380 if (IS_ERR(attr))
381 return PTR_ERR(attr);
382
383 return attr->ptr_attr.enum_id;
384}
385
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300386static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
387 u16 idx)
388{
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600389 const struct uverbs_attr *attr;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300390
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600391 attr = uverbs_attr_get(attrs_bundle, idx);
392 if (IS_ERR(attr))
393 return ERR_CAST(attr);
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300394
Jason Gunthorpef4602cb2018-05-22 15:56:51 -0600395 return attr->obj_attr.uobject->object;
Ariel Levkovichbe934cc2018-04-05 18:53:25 +0300396}
397
Matan Barak3efa3812018-05-31 16:43:28 +0300398static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle,
399 u16 idx)
400{
401 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
402
403 if (IS_ERR(attr))
404 return ERR_CAST(attr);
405
406 return attr->obj_attr.uobject;
407}
408
Matan Barak8762d142018-06-17 12:59:52 +0300409static inline int
410uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
411{
412 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
413
414 if (IS_ERR(attr))
415 return PTR_ERR(attr);
416
417 return attr->ptr_attr.len;
418}
419
Matan Barakd70724f2017-08-03 16:07:04 +0300420static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200421 size_t idx, const void *from, size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300422{
423 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
424 u16 flags;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200425 size_t min_size;
Matan Barakd70724f2017-08-03 16:07:04 +0300426
427 if (IS_ERR(attr))
428 return PTR_ERR(attr);
429
Matan Barak89d9e8d2018-02-13 12:18:29 +0200430 min_size = min_t(size_t, attr->ptr_attr.len, size);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200431 if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
Matan Barak89d9e8d2018-02-13 12:18:29 +0200432 return -EFAULT;
433
Matan Barakd70724f2017-08-03 16:07:04 +0300434 flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
Matan Barak89d9e8d2018-02-13 12:18:29 +0200435 if (put_user(flags, &attr->uattr->flags))
436 return -EFAULT;
437
438 return 0;
Matan Barakd70724f2017-08-03 16:07:04 +0300439}
440
Matan Barak89d9e8d2018-02-13 12:18:29 +0200441static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
442{
443 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
444}
445
Matan Barak8762d142018-06-17 12:59:52 +0300446static inline void *uverbs_attr_get_alloced_ptr(
447 const struct uverbs_attr_bundle *attrs_bundle, u16 idx)
448{
449 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
450
451 if (IS_ERR(attr))
452 return (void *)attr;
453
454 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data :
455 attr->ptr_attr.ptr;
456}
457
Matan Barak89d9e8d2018-02-13 12:18:29 +0200458static inline int _uverbs_copy_from(void *to,
Matan Barakd70724f2017-08-03 16:07:04 +0300459 const struct uverbs_attr_bundle *attrs_bundle,
Matan Barak89d9e8d2018-02-13 12:18:29 +0200460 size_t idx,
461 size_t size)
Matan Barakd70724f2017-08-03 16:07:04 +0300462{
463 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
464
465 if (IS_ERR(attr))
466 return PTR_ERR(attr);
467
Matan Barak89d9e8d2018-02-13 12:18:29 +0200468 /*
469 * Validation ensures attr->ptr_attr.len >= size. If the caller is
Matan Barakc66db312018-03-19 15:02:36 +0200470 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
471 * uverbs_copy_from_or_zero.
Matan Barak89d9e8d2018-02-13 12:18:29 +0200472 */
473 if (unlikely(size < attr->ptr_attr.len))
474 return -EINVAL;
475
476 if (uverbs_attr_ptr_is_inline(attr))
Matan Barakd70724f2017-08-03 16:07:04 +0300477 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
Jason Gunthorpe2f360282018-02-13 12:18:31 +0200478 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
479 attr->ptr_attr.len))
Matan Barakd70724f2017-08-03 16:07:04 +0300480 return -EFAULT;
481
482 return 0;
483}
484
Matan Barakc66db312018-03-19 15:02:36 +0200485static inline int _uverbs_copy_from_or_zero(void *to,
486 const struct uverbs_attr_bundle *attrs_bundle,
487 size_t idx,
488 size_t size)
489{
490 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
491 size_t min_size;
492
493 if (IS_ERR(attr))
494 return PTR_ERR(attr);
495
496 min_size = min_t(size_t, size, attr->ptr_attr.len);
497
498 if (uverbs_attr_ptr_is_inline(attr))
499 memcpy(to, &attr->ptr_attr.data, min_size);
500 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
501 min_size))
502 return -EFAULT;
503
504 if (size > min_size)
505 memset(to + min_size, 0, size - min_size);
506
507 return 0;
508}
509
Matan Barakd70724f2017-08-03 16:07:04 +0300510#define uverbs_copy_from(to, attrs_bundle, idx) \
Matan Barak89d9e8d2018-02-13 12:18:29 +0200511 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
Matan Barakd70724f2017-08-03 16:07:04 +0300512
Matan Barakc66db312018-03-19 15:02:36 +0200513#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
514 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
515
Matan Barak118620d2017-08-03 16:07:00 +0300516/* =================================================
517 * Definitions -> Specs infrastructure
518 * =================================================
519 */
Matan Baraka0aa3092017-08-03 16:06:55 +0300520
Matan Barak118620d2017-08-03 16:07:00 +0300521/*
522 * uverbs_alloc_spec_tree - Merges different common and driver specific feature
523 * into one parsing tree that every uverbs command will be parsed upon.
524 *
525 * @num_trees: Number of trees in the array @trees.
526 * @trees: Array of pointers to tree root definitions to merge. Each such tree
527 * possibly contains objects, methods and attributes definitions.
528 *
529 * Returns:
530 * uverbs_root_spec *: The root of the merged parsing tree.
531 * On error, we return an error code. Error is checked via IS_ERR.
532 *
533 * The following merges could take place:
534 * a. Two trees representing the same method with different handler
535 * -> We take the handler of the tree that its handler != NULL
536 * and its index in the trees array is greater. The incentive for that
537 * is that developers are expected to first merge common trees and then
538 * merge trees that gives specialized the behaviour.
539 * b. Two trees representing the same object with different
540 * type_attrs (struct uverbs_obj_type):
541 * -> We take the type_attrs of the tree that its type_attr != NULL
542 * and its index in the trees array is greater. This could be used
543 * in order to override the free function, allocation size, etc.
544 * c. Two trees representing the same method attribute (same id but possibly
545 * different attributes):
546 * -> ERROR (-ENOENT), we believe that's not the programmer's intent.
547 *
548 * An object without any methods is considered invalid and will abort the
549 * function with -ENOENT error.
550 */
Matan Barak52427112017-08-03 16:07:06 +0300551#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
Matan Barak118620d2017-08-03 16:07:00 +0300552struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
553 const struct uverbs_object_tree_def **trees);
554void uverbs_free_spec_tree(struct uverbs_root_spec *root);
Matan Barak52427112017-08-03 16:07:06 +0300555#else
556static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
557 const struct uverbs_object_tree_def **trees)
558{
559 return NULL;
560}
561
562static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
563{
564}
565#endif
Matan Barak118620d2017-08-03 16:07:00 +0300566
567#endif