Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 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> |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 37 | #include <linux/uaccess.h> |
| 38 | #include <rdma/rdma_user_ioctl.h> |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 39 | #include <rdma/ib_user_ioctl_verbs.h> |
Matan Barak | 1f7ff9d | 2018-03-19 15:02:33 +0200 | [diff] [blame] | 40 | #include <rdma/ib_user_ioctl_cmds.h> |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * ======================================= |
| 44 | * Verbs action specifications |
| 45 | * ======================================= |
| 46 | */ |
| 47 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 48 | enum uverbs_attr_type { |
| 49 | UVERBS_ATTR_TYPE_NA, |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 50 | UVERBS_ATTR_TYPE_PTR_IN, |
| 51 | UVERBS_ATTR_TYPE_PTR_OUT, |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 52 | UVERBS_ATTR_TYPE_IDR, |
| 53 | UVERBS_ATTR_TYPE_FD, |
| 54 | }; |
| 55 | |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 56 | enum uverbs_obj_access { |
| 57 | UVERBS_ACCESS_READ, |
| 58 | UVERBS_ACCESS_WRITE, |
| 59 | UVERBS_ACCESS_NEW, |
| 60 | UVERBS_ACCESS_DESTROY |
| 61 | }; |
| 62 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 63 | enum { |
| 64 | UVERBS_ATTR_SPEC_F_MANDATORY = 1U << 0, |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 65 | /* Support extending attributes by length, validate all unknown size == zero */ |
| 66 | UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO = 1U << 1, |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 67 | }; |
| 68 | |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 69 | /* Specification of a single attribute inside the ioctl message */ |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 70 | struct uverbs_attr_spec { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 71 | union { |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 72 | /* Header shared by all following union members - to reduce space. */ |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 73 | struct { |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 74 | 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 Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 82 | /* Current known size to kernel */ |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 83 | u16 len; |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 84 | /* User isn't allowed to provide something < min_len */ |
| 85 | u16 min_len; |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 86 | } ptr; |
| 87 | struct { |
| 88 | enum uverbs_attr_type type; |
| 89 | /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */ |
| 90 | u8 flags; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 91 | /* |
| 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 Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | struct uverbs_attr_spec_hash { |
| 102 | size_t num_attrs; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 103 | unsigned long *mandatory_attrs_bitmask; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 104 | struct uverbs_attr_spec attrs[0]; |
| 105 | }; |
| 106 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 107 | struct uverbs_attr_bundle; |
| 108 | struct ib_uverbs_file; |
| 109 | |
| 110 | enum { |
| 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 | |
| 118 | struct 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 | |
| 128 | struct uverbs_method_spec_hash { |
| 129 | size_t num_methods; |
| 130 | struct uverbs_method_spec *methods[0]; |
| 131 | }; |
| 132 | |
| 133 | struct 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 | |
| 139 | struct uverbs_object_spec_hash { |
| 140 | size_t num_objects; |
| 141 | struct uverbs_object_spec *objects[0]; |
| 142 | }; |
| 143 | |
| 144 | struct uverbs_root_spec { |
| 145 | size_t num_buckets; |
| 146 | struct uverbs_object_spec_hash *object_buckets[0]; |
| 147 | }; |
| 148 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 149 | /* |
| 150 | * ======================================= |
| 151 | * Verbs definitions |
| 152 | * ======================================= |
| 153 | */ |
| 154 | |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 155 | struct uverbs_attr_def { |
| 156 | u16 id; |
| 157 | struct uverbs_attr_spec attr; |
| 158 | }; |
| 159 | |
| 160 | struct 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 Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 170 | struct uverbs_object_def { |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 171 | u16 id; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 172 | const struct uverbs_obj_type *type_attrs; |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 173 | size_t num_methods; |
| 174 | const struct uverbs_method_def * const (*methods)[]; |
| 175 | }; |
| 176 | |
| 177 | struct uverbs_object_tree_def { |
| 178 | size_t num_objects; |
| 179 | const struct uverbs_object_def * const (*objects)[]; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 180 | }; |
| 181 | |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 182 | #define UA_FLAGS(_flags) .flags = _flags |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 183 | #define __UVERBS_ATTR0(_id, _type, _fld, _attr, ...) \ |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 184 | ((const struct uverbs_attr_def) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 185 | {.id = _id, .attr = {{._fld = {.type = _type, _attr, .flags = 0, } }, } }) |
| 186 | #define __UVERBS_ATTR1(_id, _type, _fld, _attr, _extra1, ...) \ |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 187 | ((const struct uverbs_attr_def) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 188 | {.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 Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 202 | /* |
| 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 Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 207 | #define UVERBS_ATTR(_id, _type, _fld, _attr, ...) \ |
| 208 | __UVERBS_ATTR(_id, _type, _fld, _attr, ##__VA_ARGS__, 2, 1, 0) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 209 | #define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 210 | UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_IN, ptr, _len, ##__VA_ARGS__) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 211 | /* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */ |
| 212 | #define UVERBS_ATTR_PTR_IN(_id, _type, ...) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 213 | UVERBS_ATTR_PTR_IN_SZ(_id, _type, ##__VA_ARGS__) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 214 | #define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 215 | UVERBS_ATTR(_id, UVERBS_ATTR_TYPE_PTR_OUT, ptr, _len, ##__VA_ARGS__) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 216 | #define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \ |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 217 | UVERBS_ATTR_PTR_OUT_SZ(_id, _type, ##__VA_ARGS__) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 218 | |
| 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 Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 232 | .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \ |
| 233 | .access = _access, .flags = 0 } }, } }) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 234 | #define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\ |
| 235 | ((const struct uverbs_attr_def) \ |
| 236 | {.id = _id, \ |
Matan Barak | 1f07e08f | 2018-03-19 15:02:35 +0200 | [diff] [blame] | 237 | .attr = { {.obj = {.type = _obj_class, .obj_type = _obj_type, \ |
| 238 | .access = _access, _flags} }, } }) |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 239 | #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 Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 278 | #define _UVERBS_OBJECT(_id, _type_attrs, ...) \ |
| 279 | ((const struct uverbs_object_def) { \ |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 280 | .id = _id, \ |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 281 | .type_attrs = _type_attrs, \ |
| 282 | .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \ |
| 283 | .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} }) |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 284 | #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \ |
| 285 | const struct uverbs_object_def _name = \ |
| 286 | _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__) |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 287 | #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 Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 298 | /* ================================================= |
| 299 | * Parsing infrastructure |
| 300 | * ================================================= |
| 301 | */ |
| 302 | |
| 303 | struct uverbs_ptr_attr { |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 304 | u64 data; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 305 | u16 len; |
| 306 | /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ |
| 307 | u16 flags; |
| 308 | }; |
| 309 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 310 | struct uverbs_obj_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 311 | /* pointer to the kernel descriptor -> type, access, etc */ |
| 312 | const struct uverbs_obj_type *type; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 313 | struct ib_uobject *uobject; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 314 | /* fd or id in idr of this object */ |
| 315 | int id; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 316 | }; |
| 317 | |
| 318 | struct uverbs_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 319 | /* |
| 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 Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | struct 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 | |
| 341 | struct uverbs_attr_bundle { |
| 342 | size_t num_buckets; |
| 343 | struct uverbs_attr_bundle_hash hash[]; |
| 344 | }; |
| 345 | |
| 346 | static 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 Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 352 | static 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 Barak | 41b2a71 | 2018-03-19 15:02:38 +0200 | [diff] [blame^] | 364 | #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) |
| 365 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 366 | static 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 | |
| 377 | static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 378 | size_t idx, const void *from, size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 379 | { |
| 380 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 381 | u16 flags; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 382 | size_t min_size; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 383 | |
| 384 | if (IS_ERR(attr)) |
| 385 | return PTR_ERR(attr); |
| 386 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 387 | min_size = min_t(size_t, attr->ptr_attr.len, size); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 388 | if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 389 | return -EFAULT; |
| 390 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 391 | flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 392 | if (put_user(flags, &attr->uattr->flags)) |
| 393 | return -EFAULT; |
| 394 | |
| 395 | return 0; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 396 | } |
| 397 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 398 | static 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 | |
| 403 | static inline int _uverbs_copy_from(void *to, |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 404 | const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 405 | size_t idx, |
| 406 | size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 407 | { |
| 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 Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 413 | /* |
| 414 | * Validation ensures attr->ptr_attr.len >= size. If the caller is |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 415 | * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call |
| 416 | * uverbs_copy_from_or_zero. |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 417 | */ |
| 418 | if (unlikely(size < attr->ptr_attr.len)) |
| 419 | return -EINVAL; |
| 420 | |
| 421 | if (uverbs_attr_ptr_is_inline(attr)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 422 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 423 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 424 | attr->ptr_attr.len)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 425 | return -EFAULT; |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 430 | static 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 Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 455 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 456 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 457 | |
Matan Barak | c66db31 | 2018-03-19 15:02:36 +0200 | [diff] [blame] | 458 | #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ |
| 459 | _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) |
| 460 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 461 | /* ================================================= |
| 462 | * Definitions -> Specs infrastructure |
| 463 | * ================================================= |
| 464 | */ |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 465 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 466 | /* |
| 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 Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 496 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 497 | struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 498 | const struct uverbs_object_tree_def **trees); |
| 499 | void uverbs_free_spec_tree(struct uverbs_root_spec *root); |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 500 | #else |
| 501 | static 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 | |
| 507 | static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) |
| 508 | { |
| 509 | } |
| 510 | #endif |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 511 | |
| 512 | #endif |