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, |
| 65 | /* Support extending attributes by length */ |
| 66 | UVERBS_ATTR_SPEC_F_MIN_SZ = 1U << 1, |
| 67 | }; |
| 68 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 69 | struct uverbs_attr_spec { |
| 70 | enum uverbs_attr_type type; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 71 | union { |
| 72 | u16 len; |
| 73 | struct { |
| 74 | /* |
| 75 | * higher bits mean the namespace and lower bits mean |
| 76 | * the type id within the namespace. |
| 77 | */ |
| 78 | u16 obj_type; |
| 79 | u8 access; |
| 80 | } obj; |
| 81 | }; |
| 82 | /* Combination of bits from enum UVERBS_ATTR_SPEC_F_XXXX */ |
| 83 | u8 flags; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | struct uverbs_attr_spec_hash { |
| 87 | size_t num_attrs; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 88 | unsigned long *mandatory_attrs_bitmask; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 89 | struct uverbs_attr_spec attrs[0]; |
| 90 | }; |
| 91 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 92 | struct uverbs_attr_bundle; |
| 93 | struct ib_uverbs_file; |
| 94 | |
| 95 | enum { |
| 96 | /* |
| 97 | * Action marked with this flag creates a context (or root for all |
| 98 | * objects). |
| 99 | */ |
| 100 | UVERBS_ACTION_FLAG_CREATE_ROOT = 1U << 0, |
| 101 | }; |
| 102 | |
| 103 | struct uverbs_method_spec { |
| 104 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 105 | u32 flags; |
| 106 | size_t num_buckets; |
| 107 | size_t num_child_attrs; |
| 108 | int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, |
| 109 | struct uverbs_attr_bundle *ctx); |
| 110 | struct uverbs_attr_spec_hash *attr_buckets[0]; |
| 111 | }; |
| 112 | |
| 113 | struct uverbs_method_spec_hash { |
| 114 | size_t num_methods; |
| 115 | struct uverbs_method_spec *methods[0]; |
| 116 | }; |
| 117 | |
| 118 | struct uverbs_object_spec { |
| 119 | const struct uverbs_obj_type *type_attrs; |
| 120 | size_t num_buckets; |
| 121 | struct uverbs_method_spec_hash *method_buckets[0]; |
| 122 | }; |
| 123 | |
| 124 | struct uverbs_object_spec_hash { |
| 125 | size_t num_objects; |
| 126 | struct uverbs_object_spec *objects[0]; |
| 127 | }; |
| 128 | |
| 129 | struct uverbs_root_spec { |
| 130 | size_t num_buckets; |
| 131 | struct uverbs_object_spec_hash *object_buckets[0]; |
| 132 | }; |
| 133 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 134 | /* |
| 135 | * ======================================= |
| 136 | * Verbs definitions |
| 137 | * ======================================= |
| 138 | */ |
| 139 | |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 140 | struct uverbs_attr_def { |
| 141 | u16 id; |
| 142 | struct uverbs_attr_spec attr; |
| 143 | }; |
| 144 | |
| 145 | struct uverbs_method_def { |
| 146 | u16 id; |
| 147 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 148 | u32 flags; |
| 149 | size_t num_attrs; |
| 150 | const struct uverbs_attr_def * const (*attrs)[]; |
| 151 | int (*handler)(struct ib_device *ib_dev, struct ib_uverbs_file *ufile, |
| 152 | struct uverbs_attr_bundle *ctx); |
| 153 | }; |
| 154 | |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 155 | struct uverbs_object_def { |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 156 | u16 id; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 157 | const struct uverbs_obj_type *type_attrs; |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 158 | size_t num_methods; |
| 159 | const struct uverbs_method_def * const (*methods)[]; |
| 160 | }; |
| 161 | |
| 162 | struct uverbs_object_tree_def { |
| 163 | size_t num_objects; |
| 164 | const struct uverbs_object_def * const (*objects)[]; |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 165 | }; |
| 166 | |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 167 | #define UA_FLAGS(_flags) .flags = _flags |
| 168 | #define __UVERBS_ATTR0(_id, _len, _type, ...) \ |
| 169 | ((const struct uverbs_attr_def) \ |
| 170 | {.id = _id, .attr = {.type = _type, {.len = _len}, .flags = 0, } }) |
| 171 | #define __UVERBS_ATTR1(_id, _len, _type, _flags) \ |
| 172 | ((const struct uverbs_attr_def) \ |
| 173 | {.id = _id, .attr = {.type = _type, {.len = _len}, _flags, } }) |
| 174 | #define __UVERBS_ATTR(_id, _len, _type, _flags, _n, ...) \ |
| 175 | __UVERBS_ATTR##_n(_id, _len, _type, _flags) |
| 176 | /* |
| 177 | * In new compiler, UVERBS_ATTR could be simplified by declaring it as |
| 178 | * [_id] = {.type = _type, .len = _len, ##__VA_ARGS__} |
| 179 | * But since we support older compilers too, we need the more complex code. |
| 180 | */ |
| 181 | #define UVERBS_ATTR(_id, _len, _type, ...) \ |
| 182 | __UVERBS_ATTR(_id, _len, _type, ##__VA_ARGS__, 1, 0) |
| 183 | #define UVERBS_ATTR_PTR_IN_SZ(_id, _len, ...) \ |
| 184 | UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_IN, ##__VA_ARGS__) |
| 185 | /* If sizeof(_type) <= sizeof(u64), this will be inlined rather than a pointer */ |
| 186 | #define UVERBS_ATTR_PTR_IN(_id, _type, ...) \ |
| 187 | UVERBS_ATTR_PTR_IN_SZ(_id, sizeof(_type), ##__VA_ARGS__) |
| 188 | #define UVERBS_ATTR_PTR_OUT_SZ(_id, _len, ...) \ |
| 189 | UVERBS_ATTR(_id, _len, UVERBS_ATTR_TYPE_PTR_OUT, ##__VA_ARGS__) |
| 190 | #define UVERBS_ATTR_PTR_OUT(_id, _type, ...) \ |
| 191 | UVERBS_ATTR_PTR_OUT_SZ(_id, sizeof(_type), ##__VA_ARGS__) |
| 192 | |
| 193 | /* |
| 194 | * In new compiler, UVERBS_ATTR_IDR (and FD) could be simplified by declaring |
| 195 | * it as |
| 196 | * {.id = _id, \ |
| 197 | * .attr {.type = __obj_class, \ |
| 198 | * .obj = {.obj_type = _idr_type, \ |
| 199 | * .access = _access \ |
| 200 | * }, ##__VA_ARGS__ } } |
| 201 | * But since we support older compilers too, we need the more complex code. |
| 202 | */ |
| 203 | #define ___UVERBS_ATTR_OBJ0(_id, _obj_class, _obj_type, _access, ...)\ |
| 204 | ((const struct uverbs_attr_def) \ |
| 205 | {.id = _id, \ |
| 206 | .attr = {.type = _obj_class, \ |
| 207 | {.obj = {.obj_type = _obj_type, .access = _access } },\ |
| 208 | .flags = 0} }) |
| 209 | #define ___UVERBS_ATTR_OBJ1(_id, _obj_class, _obj_type, _access, _flags)\ |
| 210 | ((const struct uverbs_attr_def) \ |
| 211 | {.id = _id, \ |
| 212 | .attr = {.type = _obj_class, \ |
| 213 | {.obj = {.obj_type = _obj_type, .access = _access} }, \ |
| 214 | _flags} }) |
| 215 | #define ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, _flags, \ |
| 216 | _n, ...) \ |
| 217 | ___UVERBS_ATTR_OBJ##_n(_id, _obj_class, _obj_type, _access, _flags) |
| 218 | #define __UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, ...) \ |
| 219 | ___UVERBS_ATTR_OBJ(_id, _obj_class, _obj_type, _access, \ |
| 220 | ##__VA_ARGS__, 1, 0) |
| 221 | #define UVERBS_ATTR_IDR(_id, _idr_type, _access, ...) \ |
| 222 | __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_IDR, _idr_type, _access,\ |
| 223 | ##__VA_ARGS__) |
| 224 | #define UVERBS_ATTR_FD(_id, _fd_type, _access, ...) \ |
| 225 | __UVERBS_ATTR_OBJ(_id, UVERBS_ATTR_TYPE_FD, _fd_type, \ |
| 226 | (_access) + BUILD_BUG_ON_ZERO( \ |
| 227 | (_access) != UVERBS_ACCESS_NEW && \ |
| 228 | (_access) != UVERBS_ACCESS_READ), \ |
| 229 | ##__VA_ARGS__) |
| 230 | #define DECLARE_UVERBS_ATTR_SPEC(_name, ...) \ |
| 231 | const struct uverbs_attr_def _name = __VA_ARGS__ |
| 232 | |
| 233 | #define _UVERBS_METHOD_ATTRS_SZ(...) \ |
| 234 | (sizeof((const struct uverbs_attr_def * const []){__VA_ARGS__}) /\ |
| 235 | sizeof(const struct uverbs_attr_def *)) |
| 236 | #define _UVERBS_METHOD(_id, _handler, _flags, ...) \ |
| 237 | ((const struct uverbs_method_def) { \ |
| 238 | .id = _id, \ |
| 239 | .flags = _flags, \ |
| 240 | .handler = _handler, \ |
| 241 | .num_attrs = _UVERBS_METHOD_ATTRS_SZ(__VA_ARGS__), \ |
| 242 | .attrs = &(const struct uverbs_attr_def * const []){__VA_ARGS__} }) |
| 243 | #define DECLARE_UVERBS_METHOD(_name, _id, _handler, ...) \ |
| 244 | const struct uverbs_method_def _name = \ |
| 245 | _UVERBS_METHOD(_id, _handler, 0, ##__VA_ARGS__) |
| 246 | #define DECLARE_UVERBS_CTX_METHOD(_name, _id, _handler, _flags, ...) \ |
| 247 | const struct uverbs_method_def _name = \ |
| 248 | _UVERBS_METHOD(_id, _handler, \ |
| 249 | UVERBS_ACTION_FLAG_CREATE_ROOT, \ |
| 250 | ##__VA_ARGS__) |
| 251 | #define _UVERBS_OBJECT_METHODS_SZ(...) \ |
| 252 | (sizeof((const struct uverbs_method_def * const []){__VA_ARGS__}) / \ |
| 253 | sizeof(const struct uverbs_method_def *)) |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 254 | #define _UVERBS_OBJECT(_id, _type_attrs, ...) \ |
| 255 | ((const struct uverbs_object_def) { \ |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 256 | .id = _id, \ |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 257 | .type_attrs = _type_attrs, \ |
| 258 | .num_methods = _UVERBS_OBJECT_METHODS_SZ(__VA_ARGS__), \ |
| 259 | .methods = &(const struct uverbs_method_def * const []){__VA_ARGS__} }) |
Matan Barak | 5009010 | 2017-08-03 16:06:58 +0300 | [diff] [blame] | 260 | #define DECLARE_UVERBS_OBJECT(_name, _id, _type_attrs, ...) \ |
| 261 | const struct uverbs_object_def _name = \ |
| 262 | _UVERBS_OBJECT(_id, _type_attrs, ##__VA_ARGS__) |
Matan Barak | 09e3ebf | 2017-08-03 16:06:59 +0300 | [diff] [blame] | 263 | #define _UVERBS_TREE_OBJECTS_SZ(...) \ |
| 264 | (sizeof((const struct uverbs_object_def * const []){__VA_ARGS__}) / \ |
| 265 | sizeof(const struct uverbs_object_def *)) |
| 266 | #define _UVERBS_OBJECT_TREE(...) \ |
| 267 | ((const struct uverbs_object_tree_def) { \ |
| 268 | .num_objects = _UVERBS_TREE_OBJECTS_SZ(__VA_ARGS__), \ |
| 269 | .objects = &(const struct uverbs_object_def * const []){__VA_ARGS__} }) |
| 270 | #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ |
| 271 | const struct uverbs_object_tree_def _name = \ |
| 272 | _UVERBS_OBJECT_TREE(__VA_ARGS__) |
| 273 | |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 274 | /* ================================================= |
| 275 | * Parsing infrastructure |
| 276 | * ================================================= |
| 277 | */ |
| 278 | |
| 279 | struct uverbs_ptr_attr { |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 280 | u64 data; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 281 | u16 len; |
| 282 | /* Combination of bits from enum UVERBS_ATTR_F_XXXX */ |
| 283 | u16 flags; |
| 284 | }; |
| 285 | |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 286 | struct uverbs_obj_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 287 | /* pointer to the kernel descriptor -> type, access, etc */ |
| 288 | const struct uverbs_obj_type *type; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 289 | struct ib_uobject *uobject; |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 290 | /* fd or id in idr of this object */ |
| 291 | int id; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 292 | }; |
| 293 | |
| 294 | struct uverbs_attr { |
Matan Barak | fac9658 | 2017-08-03 16:06:57 +0300 | [diff] [blame] | 295 | /* |
| 296 | * pointer to the user-space given attribute, in order to write the |
| 297 | * new uobject's id or update flags. |
| 298 | */ |
| 299 | struct ib_uverbs_attr __user *uattr; |
| 300 | union { |
| 301 | struct uverbs_ptr_attr ptr_attr; |
| 302 | struct uverbs_obj_attr obj_attr; |
| 303 | }; |
Matan Barak | f43dbeb | 2017-08-03 16:06:56 +0300 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | struct uverbs_attr_bundle_hash { |
| 307 | /* if bit i is set, it means attrs[i] contains valid information */ |
| 308 | unsigned long *valid_bitmap; |
| 309 | size_t num_attrs; |
| 310 | /* |
| 311 | * arrays of attributes, each element corresponds to the specification |
| 312 | * of the attribute in the same index. |
| 313 | */ |
| 314 | struct uverbs_attr *attrs; |
| 315 | }; |
| 316 | |
| 317 | struct uverbs_attr_bundle { |
| 318 | size_t num_buckets; |
| 319 | struct uverbs_attr_bundle_hash hash[]; |
| 320 | }; |
| 321 | |
| 322 | static inline bool uverbs_attr_is_valid_in_hash(const struct uverbs_attr_bundle_hash *attrs_hash, |
| 323 | unsigned int idx) |
| 324 | { |
| 325 | return test_bit(idx, attrs_hash->valid_bitmap); |
| 326 | } |
| 327 | |
Matan Barak | 3541030 | 2017-08-03 16:07:01 +0300 | [diff] [blame] | 328 | static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, |
| 329 | unsigned int idx) |
| 330 | { |
| 331 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 332 | |
| 333 | if (attrs_bundle->num_buckets <= idx_bucket) |
| 334 | return false; |
| 335 | |
| 336 | return uverbs_attr_is_valid_in_hash(&attrs_bundle->hash[idx_bucket], |
| 337 | idx & ~UVERBS_ID_NS_MASK); |
| 338 | } |
| 339 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 340 | static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, |
| 341 | u16 idx) |
| 342 | { |
| 343 | u16 idx_bucket = idx >> UVERBS_ID_NS_SHIFT; |
| 344 | |
| 345 | if (!uverbs_attr_is_valid(attrs_bundle, idx)) |
| 346 | return ERR_PTR(-ENOENT); |
| 347 | |
| 348 | return &attrs_bundle->hash[idx_bucket].attrs[idx & ~UVERBS_ID_NS_MASK]; |
| 349 | } |
| 350 | |
| 351 | 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] | 352 | size_t idx, const void *from, size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 353 | { |
| 354 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 355 | u16 flags; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 356 | size_t min_size; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 357 | |
| 358 | if (IS_ERR(attr)) |
| 359 | return PTR_ERR(attr); |
| 360 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 361 | min_size = min_t(size_t, attr->ptr_attr.len, size); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 362 | 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] | 363 | return -EFAULT; |
| 364 | |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 365 | flags = attr->ptr_attr.flags | UVERBS_ATTR_F_VALID_OUTPUT; |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 366 | if (put_user(flags, &attr->uattr->flags)) |
| 367 | return -EFAULT; |
| 368 | |
| 369 | return 0; |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 370 | } |
| 371 | |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 372 | static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) |
| 373 | { |
| 374 | return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); |
| 375 | } |
| 376 | |
| 377 | static inline int _uverbs_copy_from(void *to, |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 378 | const struct uverbs_attr_bundle *attrs_bundle, |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 379 | size_t idx, |
| 380 | size_t size) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 381 | { |
| 382 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 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 | /* |
| 388 | * Validation ensures attr->ptr_attr.len >= size. If the caller is |
| 389 | * using UVERBS_ATTR_SPEC_F_MIN_SZ then it must call copy_from with |
| 390 | * the right size. |
| 391 | */ |
| 392 | if (unlikely(size < attr->ptr_attr.len)) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | if (uverbs_attr_ptr_is_inline(attr)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 396 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
Jason Gunthorpe | 2f36028 | 2018-02-13 12:18:31 +0200 | [diff] [blame] | 397 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 398 | attr->ptr_attr.len)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 399 | return -EFAULT; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
Matan Barak | 89d9e8d | 2018-02-13 12:18:29 +0200 | [diff] [blame] | 405 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
Matan Barak | d70724f | 2017-08-03 16:07:04 +0300 | [diff] [blame] | 406 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 407 | /* ================================================= |
| 408 | * Definitions -> Specs infrastructure |
| 409 | * ================================================= |
| 410 | */ |
Matan Barak | a0aa309 | 2017-08-03 16:06:55 +0300 | [diff] [blame] | 411 | |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 412 | /* |
| 413 | * uverbs_alloc_spec_tree - Merges different common and driver specific feature |
| 414 | * into one parsing tree that every uverbs command will be parsed upon. |
| 415 | * |
| 416 | * @num_trees: Number of trees in the array @trees. |
| 417 | * @trees: Array of pointers to tree root definitions to merge. Each such tree |
| 418 | * possibly contains objects, methods and attributes definitions. |
| 419 | * |
| 420 | * Returns: |
| 421 | * uverbs_root_spec *: The root of the merged parsing tree. |
| 422 | * On error, we return an error code. Error is checked via IS_ERR. |
| 423 | * |
| 424 | * The following merges could take place: |
| 425 | * a. Two trees representing the same method with different handler |
| 426 | * -> We take the handler of the tree that its handler != NULL |
| 427 | * and its index in the trees array is greater. The incentive for that |
| 428 | * is that developers are expected to first merge common trees and then |
| 429 | * merge trees that gives specialized the behaviour. |
| 430 | * b. Two trees representing the same object with different |
| 431 | * type_attrs (struct uverbs_obj_type): |
| 432 | * -> We take the type_attrs of the tree that its type_attr != NULL |
| 433 | * and its index in the trees array is greater. This could be used |
| 434 | * in order to override the free function, allocation size, etc. |
| 435 | * c. Two trees representing the same method attribute (same id but possibly |
| 436 | * different attributes): |
| 437 | * -> ERROR (-ENOENT), we believe that's not the programmer's intent. |
| 438 | * |
| 439 | * An object without any methods is considered invalid and will abort the |
| 440 | * function with -ENOENT error. |
| 441 | */ |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 442 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 443 | struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 444 | const struct uverbs_object_tree_def **trees); |
| 445 | void uverbs_free_spec_tree(struct uverbs_root_spec *root); |
Matan Barak | 5242711 | 2017-08-03 16:07:06 +0300 | [diff] [blame] | 446 | #else |
| 447 | static inline struct uverbs_root_spec *uverbs_alloc_spec_tree(unsigned int num_trees, |
| 448 | const struct uverbs_object_tree_def **trees) |
| 449 | { |
| 450 | return NULL; |
| 451 | } |
| 452 | |
| 453 | static inline void uverbs_free_spec_tree(struct uverbs_root_spec *root) |
| 454 | { |
| 455 | } |
| 456 | #endif |
Matan Barak | 118620d | 2017-08-03 16:07:00 +0300 | [diff] [blame] | 457 | |
| 458 | #endif |