1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
* Copyright (c) 2017, Mellanox Technologies inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _UVERBS_IOCTL_
#define _UVERBS_IOCTL_
#include <rdma/uverbs_types.h>
#include <linux/uaccess.h>
#include <rdma/rdma_user_ioctl.h>
#include <rdma/ib_user_ioctl_verbs.h>
#include <rdma/ib_user_ioctl_cmds.h>
/*
* =======================================
* Verbs action specifications
* =======================================
*/
enum uverbs_attr_type {
UVERBS_ATTR_TYPE_NA,
UVERBS_ATTR_TYPE_PTR_IN,
UVERBS_ATTR_TYPE_PTR_OUT,
UVERBS_ATTR_TYPE_IDR,
UVERBS_ATTR_TYPE_FD,
UVERBS_ATTR_TYPE_ENUM_IN,
};
enum uverbs_obj_access {
UVERBS_ACCESS_READ,
UVERBS_ACCESS_WRITE,
UVERBS_ACCESS_NEW,
UVERBS_ACCESS_DESTROY
};
enum {
UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0,
/* Support extending attributes by length, validate all unknown size == zero */
UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1,
};
/* Specification of a single attribute inside the ioctl message */
struct uverbs_attr_spec {
union {
/* Header shared by all following union members - to reduce space. */
struct {
enum uverbs_attr_type type;
/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
u8 flags;
};
struct {
enum uverbs_attr_type type;
/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
u8 flags;
/* Current known size to kernel */
u16 len;
/* User isn't allowed to provide something < min_len */
u16 min_len;
} ptr;
struct {
enum uverbs_attr_type type;
/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
u8 flags;
/*
* higher bits mean the namespace and lower bits mean
* the type id within the namespace.
*/
u16 obj_type;
u8 access;
} obj;
struct {
enum uverbs_attr_type type;
/* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */
u8 flags;
u8 num_elems;
/*
* The enum attribute can select one of the attributes
* contained in the ids array. Currently only PTR_IN
* attributes are supported in the ids array.
*/
const struct uverbs_attr_spec *ids;
} enum_def;
};
};
struct uverbs_attr_spec_hash {
size_t num_attrs;
unsigned long *mandatory_attrs_bitmask;
struct uverbs_attr_spec attrs[0];
};
struct uverbs_attr_bundle;
struct ib_uverbs_file;
enum {
/*
* Action marked with this flag creates a context (or root for all
* objects).
*/
UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0,
};
struct uverbs_method_spec {
/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
u32 flags;
size_t num_buckets;
size_t num_child_attrs;
int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
struct uverbs_attr_bundle *ctx);
struct uverbs_attr_spec_hash *attr_buckets[0];
};
struct uverbs_method_spec_hash {
size_t num_methods;
struct uverbs_method_spec *methods[0];
};
struct uverbs_object_spec {
const struct uverbs_obj_type *type_attrs;
size_t num_buckets;
struct uverbs_method_spec_hash *method_buckets[0];
};
struct uverbs_object_spec_hash {
size_t num_objects;
struct uverbs_object_spec *objects[0];
};
struct uverbs_root_spec {
size_t num_buckets;
struct uverbs_object_spec_hash *object_buckets[0];
};
/*
* =======================================
* Verbs definitions
* =======================================
*/
struct uverbs_attr_def {
u16 id;
struct uverbs_attr_spec attr;
};
struct uverbs_method_def {
u16 id;
/* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */
u32 flags;
size_t num_attrs;
const struct uverbs_attr_def * const (*attrs)[];
int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile,
struct uverbs_attr_bundle *ctx);
};
struct uverbs_object_def {
u16 id;
const struct uverbs_obj_type *type_attrs;
size_t num_methods;
const struct uverbs_method_def * const (*methods)[];
};
struct uverbs_object_tree_def {
size_t num_objects;
const struct uverbs_object_def * const (*objects)[];
};
#define UA_FLAGS(_flags) .flags = _flags
#define __UVERBS_ATTR0(_id, _type, _fld, _attr, ...) \
((const struct uverbs_attr_def) \
{.id = _id, .attr = {{._fld = {.type = _type, _attr, .flags = 0, } }, } })
#define __UVERBS_ATTR1(_id, _type, _fld, _attr, _extra1, ...) \
((const struct uverbs_attr_def) \
{.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1 } },} })
#define __UVERBS_ATTR2(_id, _type, _fld, _attr, _extra1, _extra2) \
((const struct uverbs_attr_def) \
{.id = _id, .attr = {{._fld = {.type = _type, _attr, _extra1, _extra2 } },} })
#define __UVERBS_ATTR(_id, _type, _fld, _attr, _extra1, _extra2, _n, ...) \
__UVERBS_ATTR##_n(_id, _type, _fld, _attr, _extra1, _extra2)
#define UVERBS_ATTR_TYPE(_type) \
.min_len = sizeof(_type), .len = sizeof(_type)
#define UVERBS_ATTR_STRUCT(_type, _last) \
.min_len = ((uintptr_t)(&((_type *)0)->_last + 1)), .len = sizeof(_type)
#define UVERBS_ATTR_SIZE(_min_len, _len) \
.min_len = _min_len, .len = _len
/*
* In new compiler, UVERBS_ATTR could be simplified by declaring it as
* [_id] = {.type = _type, .len = _len, ##__VA_ARGS__}
* But since we support older compilers too, we need the more complex code.
*/
#define UVERBS_ATTR(_id, _type, _fld, _attr, ...) \
__UVERBS_ATTR(_id, _type, _fld, _attr, ##__VA_ARGS__, 2, 1, 0)
#define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \
UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_IN, ptr, _len, ##__VA_ARGS__)
/* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */
#define UVERBS_ATTR_PTR_IN(_id, _type, ...) \
UVERBS_ATTR_PTR_IN_SZ(_id, _type, ##__VA_ARGS__)
#define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \
UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_OUT, ptr, _len, ##__VA_ARGS__)
#define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \
UVERBS_ATTR_PTR_OUT_SZ(_id, _type, ##__VA_ARGS__)
#define UVERBS_ATTR_ENUM_IN(_id, _enum_arr, ...) \
UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_ENUM_IN, enum_def, \
.ids = (_enum_arr), \
.num_elems = ARRAY_SIZE(_enum_arr), ##__VA_ARGS__)
/*
* In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring
* it as
* {.id = _id, \
* .attr {.type = __obj_class, \
* .obj = {.obj_type = _idr_type, \
* .access = _access \
* }, ##__VA_ARGS__ } }
* But since we support older compilers too, we need the more complex code.
*/
#define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\
((const struct uverbs_attr_def) \
{.id = _id, \
.attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
.access = _access, .flags = 0 } }, } })
#define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\
((const struct uverbs_attr_def) \
{.id = _id, \
.attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \
.access = _access, _flags} }, } })
#define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \
_n, ...) \
___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags)
#define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \
___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \
##__VA_ARGS__, 1, 0)
#define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \
__UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\
##__VA_ARGS__)
#define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \
__UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \
(_access) + BUILD_BUG_ON_ZERO( \
(_access) != UVERBS_ACCESS_NEW && \
(_access) != UVERBS_ACCESS_READ), \
##__VA_ARGS__)
#define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \
const struct uverbs_attr_def _name = __VA_ARGS__
#define DECLARE_UVERBS_ENUM(_name, ...) \
const struct uverbs_enum_spec _name = { \
.len = ARRAY_SIZE(((struct uverbs_attr_spec[]){__VA_ARGS__})),\
.ids = {__VA_ARGS__}, \
}
#define _UVERBS_METHOD_ATTRS_SZ(...) \
(sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\
sizeof(const struct uverbs_attr_def *))
#define _UVERBS_METHOD(_id, _handler, _flags, ...) \
((const struct uverbs_method_def) { \
.id = _id, \
.flags = _flags, \
.handler = _handler, \
.num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \
.attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} })
#define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \
const struct uverbs_method_def _name = \
_UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__)
#define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \
const struct uverbs_method_def _name = \
_UVERBS_METHOD(_id, _handler, \
UVERBS_ACTION_FLAG_CREATE_ROOT, \
##__VA_ARGS__)
#define _UVERBS_OBJECT_METHODS_SZ(...) \
(sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \
sizeof(const struct uverbs_method_def *))
#define _UVERBS_OBJECT(_id, _type_attrs, ...) \
((const struct uverbs_object_def) { \
.id = _id, \
.type_attrs = _type_attrs, \
.num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \
.methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} })
#define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \
const struct uverbs_object_def _name = \
_UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__)
#define _UVERBS_TREE_OBJECTS_SZ(...) \
(sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \
sizeof(const struct uverbs_object_def *))
#define _UVERBS_OBJECT_TREE(...) \
((const struct uverbs_object_tree_def) { \
.num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \
.objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} })
#define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \
const struct uverbs_object_tree_def _name = \
_UVERBS_OBJECT_TREE(__VA_ARGS__)
/* =================================================
* Parsing infrastructure
* =================================================
*/
struct uverbs_ptr_attr {
u64 data;
u16 len;
/* Combination of bits from enum UVERBS_ATTR_F_XXXX */
u16 flags;
u8 enum_id;
};
struct uverbs_obj_attr {
/* pointer to the kernel descriptor -> type, access, etc */
const struct uverbs_obj_type *type;
struct ib_uobject *uobject;
/* fd or id in idr of this object */
int id;
};
struct uverbs_attr {
/*
* pointer to the user-space given attribute, in order to write the
* new uobject's id or update flags.
*/
struct ib_uverbs_attr __user *uattr;
union {
struct uverbs_ptr_attr ptr_attr;
struct uverbs_obj_attr obj_attr;
};
};
struct uverbs_attr_bundle_hash {
/* if bit i is set, it means attrs[i] contains valid information */
unsigned long *valid_bitmap;
size_t num_attrs;
/*
* arrays of attributes, each element corresponds to the specification
* of the attribute in the same index.
*/
struct uverbs_attr *attrs;
};
struct uverbs_attr_bundle {
size_t num_buckets;
struct uverbs_attr_bundle_hash hash[];
};
static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash,
unsigned int idx)
{
return test_bit(idx, attrs_hash->valid_bitmap);
}
static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle,
unsigned int idx)
{
u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
if (attrs_bundle->num_buckets <= idx_bucket)
return false;
return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket],
idx & ~UVERBS_ID_NS_MASK);
}
#define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT)
static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle,
u16 idx)
{
u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT;
if (!uverbs_attr_is_valid(attrs_bundle, idx))
return ERR_PTR(-ENOENT);
return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK];
}
static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle,
u16 idx)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
if (IS_ERR(attr))
return PTR_ERR(attr);
return attr->ptr_attr.enum_id;
}
static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle,
u16 idx)
{
struct ib_uobject *uobj =
uverbs_attr_get(attrs_bundle, idx)->obj_attr.uobject;
if (IS_ERR(uobj))
return uobj;
return uobj->object;
}
static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle,
size_t idx, const void *from, size_t size)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
u16 flags;
size_t min_size;
if (IS_ERR(attr))
return PTR_ERR(attr);
min_size = min_t(size_t, attr->ptr_attr.len, size);
if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size))
return -EFAULT;
flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT;
if (put_user(flags, &attr->uattr->flags))
return -EFAULT;
return 0;
}
static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr)
{
return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data);
}
static inline int _uverbs_copy_from(void *to,
const struct uverbs_attr_bundle *attrs_bundle,
size_t idx,
size_t size)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
if (IS_ERR(attr))
return PTR_ERR(attr);
/*
* Validation ensures attr->ptr_attr.len >= size. If the caller is
* using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call
* uverbs_copy_from_or_zero.
*/
if (unlikely(size < attr->ptr_attr.len))
return -EINVAL;
if (uverbs_attr_ptr_is_inline(attr))
memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len);
else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
attr->ptr_attr.len))
return -EFAULT;
return 0;
}
static inline int _uverbs_copy_from_or_zero(void *to,
const struct uverbs_attr_bundle *attrs_bundle,
size_t idx,
size_t size)
{
const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx);
size_t min_size;
if (IS_ERR(attr))
return PTR_ERR(attr);
min_size = min_t(size_t, size, attr->ptr_attr.len);
if (uverbs_attr_ptr_is_inline(attr))
memcpy(to, &attr->ptr_attr.data, min_size);
else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data),
min_size))
return -EFAULT;
if (size > min_size)
memset(to + min_size, 0, size - min_size);
return 0;
}
#define uverbs_copy_from(to, attrs_bundle, idx) \
_uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to))
#define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \
_uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to))
/* =================================================
* Definitions -> Specs infrastructure
* =================================================
*/
/*
* uverbs_alloc_spec_tree - Merges different common and driver specific feature
* into one parsing tree that every uverbs command will be parsed upon.
*
* @num_trees: Number of trees in the array @trees.
* @trees: Array of pointers to tree root definitions to merge. Each such tree
* possibly contains objects, methods and attributes definitions.
*
* Returns:
* uverbs_root_spec *: The root of the merged parsing tree.
* On error, we return an error code. Error is checked via IS_ERR.
*
* The following merges could take place:
* a. Two trees representing the same method with different handler
* -> We take the handler of the tree that its handler != NULL
* and its index in the trees array is greater. The incentive for that
* is that developers are expected to first merge common trees and then
* merge trees that gives specialized the behaviour.
* b. Two trees representing the same object with different
* type_attrs (struct uverbs_obj_type):
* -> We take the type_attrs of the tree that its type_attr != NULL
* and its index in the trees array is greater. This could be used
* in order to override the free function, allocation size, etc.
* c. Two trees representing the same method attribute (same id but possibly
* different attributes):
* -> ERROR (-ENOENT), we believe that's not the programmer's intent.
*
* An object without any methods is considered invalid and will abort the
* function with -ENOENT error.
*/
#if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
const struct uverbs_object_tree_def **trees);
void uverbs_free_spec_tree(struct uverbs_root_spec *root);
#else
static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees,
const struct uverbs_object_tree_def **trees)
{
return NULL;
}
static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root)
{
}
#endif
#endif
|