Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [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> |
| 37 | #include <linux/uaccess.h> |
| 38 | #include <rdma/rdma_user_ioctl.h> |
| 39 | #include <rdma/ib_user_ioctl_verbs.h> |
| 40 | #include <rdma/ib_user_ioctl_cmds.h> |
| 41 | |
| 42 | /* |
| 43 | * ======================================= |
| 44 | * Verbs action specifications |
| 45 | * ======================================= |
| 46 | */ |
| 47 | |
| 48 | enum uverbs_attr_type { |
| 49 | UVERBS_ATTR_TYPE_NA, |
| 50 | UVERBS_ATTR_TYPE_PTR_IN, |
| 51 | UVERBS_ATTR_TYPE_PTR_OUT, |
| 52 | UVERBS_ATTR_TYPE_IDR, |
| 53 | UVERBS_ATTR_TYPE_FD, |
| 54 | UVERBS_ATTR_TYPE_ENUM_IN, |
| 55 | }; |
| 56 | |
| 57 | enum uverbs_obj_access { |
| 58 | UVERBS_ACCESS_READ, |
| 59 | UVERBS_ACCESS_WRITE, |
| 60 | UVERBS_ACCESS_NEW, |
| 61 | UVERBS_ACCESS_DESTROY |
| 62 | }; |
| 63 | |
| 64 | /* Specification of a single attribute inside the ioctl message */ |
| 65 | /* good size 16 */ |
| 66 | struct uverbs_attr_spec { |
| 67 | u8 type; |
| 68 | |
| 69 | /* |
| 70 | * Support extending attributes by length. Allow the user to provide |
| 71 | * more bytes than ptr.len, but check that everything after is zero'd |
| 72 | * by the user. |
| 73 | */ |
| 74 | u8 zero_trailing:1; |
| 75 | /* |
| 76 | * Valid only for PTR_IN. Allocate and copy the data inside |
| 77 | * the parser |
| 78 | */ |
| 79 | u8 alloc_and_copy:1; |
| 80 | u8 mandatory:1; |
| 81 | |
| 82 | union { |
| 83 | struct { |
| 84 | /* Current known size to kernel */ |
| 85 | u16 len; |
| 86 | /* User isn't allowed to provide something < min_len */ |
| 87 | u16 min_len; |
| 88 | } ptr; |
| 89 | |
| 90 | struct { |
| 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 | |
| 99 | struct { |
| 100 | u8 num_elems; |
| 101 | } enum_def; |
| 102 | } u; |
| 103 | |
| 104 | /* This weird split of the enum lets us remove some padding */ |
| 105 | union { |
| 106 | struct { |
| 107 | /* |
| 108 | * The enum attribute can select one of the attributes |
| 109 | * contained in the ids array. Currently only PTR_IN |
| 110 | * attributes are supported in the ids array. |
| 111 | */ |
| 112 | const struct uverbs_attr_spec *ids; |
| 113 | } enum_def; |
| 114 | } u2; |
| 115 | }; |
| 116 | |
| 117 | /* |
| 118 | * Information about the API is loaded into a radix tree. For IOCTL we start |
| 119 | * with a tuple of: |
| 120 | * object_id, attr_id, method_id |
| 121 | * |
| 122 | * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based |
| 123 | * on the current kernel support this is compressed into 16 bit key for the |
| 124 | * radix tree. Since this compression is entirely internal to the kernel the |
| 125 | * below limits can be revised if the kernel gains additional data. |
| 126 | * |
| 127 | * With 64 leafs per node this is a 3 level radix tree. |
| 128 | * |
| 129 | * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns |
| 130 | * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. |
| 131 | */ |
| 132 | enum uapi_radix_data { |
| 133 | UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, |
| 134 | |
| 135 | UVERBS_API_ATTR_KEY_BITS = 6, |
| 136 | UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), |
| 137 | UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, |
| 138 | |
| 139 | UVERBS_API_METHOD_KEY_BITS = 5, |
| 140 | UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, |
| 141 | UVERBS_API_METHOD_KEY_NUM_CORE = 24, |
| 142 | UVERBS_API_METHOD_KEY_NUM_DRIVER = (1 << UVERBS_API_METHOD_KEY_BITS) - |
| 143 | UVERBS_API_METHOD_KEY_NUM_CORE, |
| 144 | UVERBS_API_METHOD_KEY_MASK = GENMASK( |
| 145 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, |
| 146 | UVERBS_API_METHOD_KEY_SHIFT), |
| 147 | |
| 148 | UVERBS_API_OBJ_KEY_BITS = 5, |
| 149 | UVERBS_API_OBJ_KEY_SHIFT = |
| 150 | UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, |
| 151 | UVERBS_API_OBJ_KEY_NUM_CORE = 24, |
| 152 | UVERBS_API_OBJ_KEY_NUM_DRIVER = |
| 153 | (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, |
| 154 | UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), |
| 155 | |
| 156 | /* This id guaranteed to not exist in the radix tree */ |
| 157 | UVERBS_API_KEY_ERR = 0xFFFFFFFF, |
| 158 | }; |
| 159 | |
| 160 | static inline __attribute_const__ u32 uapi_key_obj(u32 id) |
| 161 | { |
| 162 | if (id & UVERBS_API_NS_FLAG) { |
| 163 | id &= ~UVERBS_API_NS_FLAG; |
| 164 | if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) |
| 165 | return UVERBS_API_KEY_ERR; |
| 166 | id = id + UVERBS_API_OBJ_KEY_NUM_CORE; |
| 167 | } else { |
| 168 | if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) |
| 169 | return UVERBS_API_KEY_ERR; |
| 170 | } |
| 171 | |
| 172 | return id << UVERBS_API_OBJ_KEY_SHIFT; |
| 173 | } |
| 174 | |
| 175 | static inline __attribute_const__ bool uapi_key_is_object(u32 key) |
| 176 | { |
| 177 | return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; |
| 178 | } |
| 179 | |
| 180 | static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) |
| 181 | { |
| 182 | if (id & UVERBS_API_NS_FLAG) { |
| 183 | id &= ~UVERBS_API_NS_FLAG; |
| 184 | if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) |
| 185 | return UVERBS_API_KEY_ERR; |
| 186 | id = id + UVERBS_API_METHOD_KEY_NUM_CORE; |
| 187 | } else { |
| 188 | id++; |
| 189 | if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) |
| 190 | return UVERBS_API_KEY_ERR; |
| 191 | } |
| 192 | |
| 193 | return id << UVERBS_API_METHOD_KEY_SHIFT; |
| 194 | } |
| 195 | |
| 196 | static inline __attribute_const__ u32 uapi_key_attr_to_method(u32 attr_key) |
| 197 | { |
| 198 | return attr_key & |
| 199 | (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); |
| 200 | } |
| 201 | |
| 202 | static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) |
| 203 | { |
| 204 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && |
| 205 | (key & UVERBS_API_ATTR_KEY_MASK) == 0; |
| 206 | } |
| 207 | |
| 208 | static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) |
| 209 | { |
| 210 | /* 0 is the method slot itself */ |
| 211 | return ioctl_method_key + 1; |
| 212 | } |
| 213 | |
| 214 | static inline __attribute_const__ u32 uapi_key_attr(u32 id) |
| 215 | { |
| 216 | /* |
| 217 | * The attr is designed to fit in the typical single radix tree node |
| 218 | * of 64 entries. Since allmost all methods have driver attributes we |
| 219 | * organize things so that the driver and core attributes interleave to |
| 220 | * reduce the length of the attributes array in typical cases. |
| 221 | */ |
| 222 | if (id & UVERBS_API_NS_FLAG) { |
| 223 | id &= ~UVERBS_API_NS_FLAG; |
| 224 | id++; |
| 225 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) |
| 226 | return UVERBS_API_KEY_ERR; |
| 227 | id = (id << 1) | 0; |
| 228 | } else { |
| 229 | if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) |
| 230 | return UVERBS_API_KEY_ERR; |
| 231 | id = (id << 1) | 1; |
| 232 | } |
| 233 | |
| 234 | return id; |
| 235 | } |
| 236 | |
| 237 | static inline __attribute_const__ bool uapi_key_is_attr(u32 key) |
| 238 | { |
| 239 | return (key & UVERBS_API_METHOD_KEY_MASK) != 0 && |
| 240 | (key & UVERBS_API_ATTR_KEY_MASK) != 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), |
| 245 | * basically it undoes the reservation of 0 in the ID numbering. attr_key |
| 246 | * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of |
| 247 | * uapi_key_attr(). |
| 248 | */ |
| 249 | static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) |
| 250 | { |
| 251 | return attr_key - 1; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * ======================================= |
| 256 | * Verbs definitions |
| 257 | * ======================================= |
| 258 | */ |
| 259 | |
| 260 | struct uverbs_attr_def { |
| 261 | u16 id; |
| 262 | struct uverbs_attr_spec attr; |
| 263 | }; |
| 264 | |
| 265 | struct uverbs_method_def { |
| 266 | u16 id; |
| 267 | /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ |
| 268 | u32 flags; |
| 269 | size_t num_attrs; |
| 270 | const struct uverbs_attr_def * const (*attrs)[]; |
| 271 | int (*handler)(struct ib_uverbs_file *ufile, |
| 272 | struct uverbs_attr_bundle *ctx); |
| 273 | }; |
| 274 | |
| 275 | struct uverbs_object_def { |
| 276 | u16 id; |
| 277 | const struct uverbs_obj_type *type_attrs; |
| 278 | size_t num_methods; |
| 279 | const struct uverbs_method_def * const (*methods)[]; |
| 280 | }; |
| 281 | |
| 282 | struct uverbs_object_tree_def { |
| 283 | size_t num_objects; |
| 284 | const struct uverbs_object_def * const (*objects)[]; |
| 285 | }; |
| 286 | |
| 287 | /* |
| 288 | * ======================================= |
| 289 | * Attribute Specifications |
| 290 | * ======================================= |
| 291 | */ |
| 292 | |
| 293 | #define UVERBS_ATTR_SIZE(_min_len, _len) \ |
| 294 | .u.ptr.min_len = _min_len, .u.ptr.len = _len |
| 295 | |
| 296 | #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) |
| 297 | |
| 298 | /* |
| 299 | * Specifies a uapi structure that cannot be extended. The user must always |
| 300 | * supply the whole structure and nothing more. The structure must be declared |
| 301 | * in a header under include/uapi/rdma. |
| 302 | */ |
| 303 | #define UVERBS_ATTR_TYPE(_type) \ |
| 304 | .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) |
| 305 | /* |
| 306 | * Specifies a uapi structure where the user must provide at least up to |
| 307 | * member 'last'. Anything after last and up until the end of the structure |
| 308 | * can be non-zero, anything longer than the end of the structure must be |
| 309 | * zero. The structure must be declared in a header under include/uapi/rdma. |
| 310 | */ |
| 311 | #define UVERBS_ATTR_STRUCT(_type, _last) \ |
| 312 | .zero_trailing = 1, \ |
| 313 | UVERBS_ATTR_SIZE(((uintptr_t)(&((_type *)0)->_last + 1)), \ |
| 314 | sizeof(_type)) |
| 315 | /* |
| 316 | * Specifies at least min_len bytes must be passed in, but the amount can be |
| 317 | * larger, up to the protocol maximum size. No check for zeroing is done. |
| 318 | */ |
| 319 | #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) |
| 320 | |
| 321 | /* Must be used in the '...' of any UVERBS_ATTR */ |
| 322 | #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 |
| 323 | #define UA_MANDATORY .mandatory = 1 |
| 324 | #define UA_OPTIONAL .mandatory = 0 |
| 325 | |
| 326 | #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ |
| 327 | (&(const struct uverbs_attr_def){ \ |
| 328 | .id = _attr_id, \ |
| 329 | .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ |
| 330 | .u.obj.obj_type = _idr_type, \ |
| 331 | .u.obj.access = _access, \ |
| 332 | __VA_ARGS__ } }) |
| 333 | |
| 334 | #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ |
| 335 | (&(const struct uverbs_attr_def){ \ |
| 336 | .id = (_attr_id) + \ |
| 337 | BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ |
| 338 | (_access) != UVERBS_ACCESS_READ), \ |
| 339 | .attr = { .type = UVERBS_ATTR_TYPE_FD, \ |
| 340 | .u.obj.obj_type = _fd_type, \ |
| 341 | .u.obj.access = _access, \ |
| 342 | __VA_ARGS__ } }) |
| 343 | |
| 344 | #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ |
| 345 | (&(const struct uverbs_attr_def){ \ |
| 346 | .id = _attr_id, \ |
| 347 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ |
| 348 | _type, \ |
| 349 | __VA_ARGS__ } }) |
| 350 | |
| 351 | #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ |
| 352 | (&(const struct uverbs_attr_def){ \ |
| 353 | .id = _attr_id, \ |
| 354 | .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ |
| 355 | _type, \ |
| 356 | __VA_ARGS__ } }) |
| 357 | |
| 358 | /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ |
| 359 | #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ |
| 360 | (&(const struct uverbs_attr_def){ \ |
| 361 | .id = _attr_id, \ |
| 362 | .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ |
| 363 | .u2.enum_def.ids = _enum_arr, \ |
| 364 | .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ |
| 365 | __VA_ARGS__ }, \ |
| 366 | }) |
| 367 | |
| 368 | /* |
| 369 | * An input value that is a bitwise combination of values of _enum_type. |
| 370 | * This permits the flag value to be passed as either a u32 or u64, it must |
| 371 | * be retrieved via uverbs_get_flag(). |
| 372 | */ |
| 373 | #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ |
| 374 | UVERBS_ATTR_PTR_IN( \ |
| 375 | _attr_id, \ |
| 376 | UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ |
| 377 | !sizeof(_enum_type *)), \ |
| 378 | sizeof(u64)), \ |
| 379 | __VA_ARGS__) |
| 380 | |
| 381 | /* |
| 382 | * This spec is used in order to pass information to the hardware driver in a |
| 383 | * legacy way. Every verb that could get driver specific data should get this |
| 384 | * spec. |
| 385 | */ |
| 386 | #define UVERBS_ATTR_UHW() \ |
| 387 | UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ |
| 388 | UVERBS_ATTR_MIN_SIZE(0), \ |
| 389 | UA_OPTIONAL), \ |
| 390 | UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ |
| 391 | UVERBS_ATTR_MIN_SIZE(0), \ |
| 392 | UA_OPTIONAL) |
| 393 | |
| 394 | /* |
| 395 | * ======================================= |
| 396 | * Declaration helpers |
| 397 | * ======================================= |
| 398 | */ |
| 399 | |
| 400 | #define DECLARE_UVERBS_OBJECT_TREE(_name, ...) \ |
| 401 | static const struct uverbs_object_def *const _name##_ptr[] = { \ |
| 402 | __VA_ARGS__, \ |
| 403 | }; \ |
| 404 | static const struct uverbs_object_tree_def _name = { \ |
| 405 | .num_objects = ARRAY_SIZE(_name##_ptr), \ |
| 406 | .objects = &_name##_ptr, \ |
| 407 | } |
| 408 | |
| 409 | /* ================================================= |
| 410 | * Parsing infrastructure |
| 411 | * ================================================= |
| 412 | */ |
| 413 | |
| 414 | |
| 415 | struct uverbs_ptr_attr { |
| 416 | /* |
| 417 | * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is |
| 418 | * used. |
| 419 | */ |
| 420 | union { |
| 421 | void *ptr; |
| 422 | u64 data; |
| 423 | }; |
| 424 | u16 len; |
| 425 | u16 uattr_idx; |
| 426 | u8 enum_id; |
| 427 | }; |
| 428 | |
| 429 | struct uverbs_obj_attr { |
| 430 | struct ib_uobject *uobject; |
| 431 | const struct uverbs_api_attr *attr_elm; |
| 432 | }; |
| 433 | |
| 434 | struct uverbs_attr { |
| 435 | union { |
| 436 | struct uverbs_ptr_attr ptr_attr; |
| 437 | struct uverbs_obj_attr obj_attr; |
| 438 | }; |
| 439 | }; |
| 440 | |
| 441 | struct uverbs_attr_bundle { |
| 442 | struct ib_uverbs_file *ufile; |
| 443 | DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); |
| 444 | struct uverbs_attr attrs[]; |
| 445 | }; |
| 446 | |
| 447 | static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, |
| 448 | unsigned int idx) |
| 449 | { |
| 450 | return test_bit(uapi_bkey_attr(uapi_key_attr(idx)), |
| 451 | attrs_bundle->attr_present); |
| 452 | } |
| 453 | |
| 454 | #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) |
| 455 | |
| 456 | static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, |
| 457 | u16 idx) |
| 458 | { |
| 459 | if (!uverbs_attr_is_valid(attrs_bundle, idx)) |
| 460 | return ERR_PTR(-ENOENT); |
| 461 | |
| 462 | return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; |
| 463 | } |
| 464 | |
| 465 | static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, |
| 466 | u16 idx) |
| 467 | { |
| 468 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 469 | |
| 470 | if (IS_ERR(attr)) |
| 471 | return PTR_ERR(attr); |
| 472 | |
| 473 | return attr->ptr_attr.enum_id; |
| 474 | } |
| 475 | |
| 476 | static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, |
| 477 | u16 idx) |
| 478 | { |
| 479 | const struct uverbs_attr *attr; |
| 480 | |
| 481 | attr = uverbs_attr_get(attrs_bundle, idx); |
| 482 | if (IS_ERR(attr)) |
| 483 | return ERR_CAST(attr); |
| 484 | |
| 485 | return attr->obj_attr.uobject->object; |
| 486 | } |
| 487 | |
| 488 | static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, |
| 489 | u16 idx) |
| 490 | { |
| 491 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 492 | |
| 493 | if (IS_ERR(attr)) |
| 494 | return ERR_CAST(attr); |
| 495 | |
| 496 | return attr->obj_attr.uobject; |
| 497 | } |
| 498 | |
| 499 | static inline int |
| 500 | uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 501 | { |
| 502 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 503 | |
| 504 | if (IS_ERR(attr)) |
| 505 | return PTR_ERR(attr); |
| 506 | |
| 507 | return attr->ptr_attr.len; |
| 508 | } |
| 509 | |
| 510 | static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) |
| 511 | { |
| 512 | return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); |
| 513 | } |
| 514 | |
| 515 | static inline void *uverbs_attr_get_alloced_ptr( |
| 516 | const struct uverbs_attr_bundle *attrs_bundle, u16 idx) |
| 517 | { |
| 518 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 519 | |
| 520 | if (IS_ERR(attr)) |
| 521 | return (void *)attr; |
| 522 | |
| 523 | return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : |
| 524 | attr->ptr_attr.ptr; |
| 525 | } |
| 526 | |
| 527 | static inline int _uverbs_copy_from(void *to, |
| 528 | const struct uverbs_attr_bundle *attrs_bundle, |
| 529 | size_t idx, |
| 530 | size_t size) |
| 531 | { |
| 532 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 533 | |
| 534 | if (IS_ERR(attr)) |
| 535 | return PTR_ERR(attr); |
| 536 | |
| 537 | /* |
| 538 | * Validation ensures attr->ptr_attr.len >= size. If the caller is |
| 539 | * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call |
| 540 | * uverbs_copy_from_or_zero. |
| 541 | */ |
| 542 | if (unlikely(size < attr->ptr_attr.len)) |
| 543 | return -EINVAL; |
| 544 | |
| 545 | if (uverbs_attr_ptr_is_inline(attr)) |
| 546 | memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); |
| 547 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 548 | attr->ptr_attr.len)) |
| 549 | return -EFAULT; |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static inline int _uverbs_copy_from_or_zero(void *to, |
| 555 | const struct uverbs_attr_bundle *attrs_bundle, |
| 556 | size_t idx, |
| 557 | size_t size) |
| 558 | { |
| 559 | const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); |
| 560 | size_t min_size; |
| 561 | |
| 562 | if (IS_ERR(attr)) |
| 563 | return PTR_ERR(attr); |
| 564 | |
| 565 | min_size = min_t(size_t, size, attr->ptr_attr.len); |
| 566 | |
| 567 | if (uverbs_attr_ptr_is_inline(attr)) |
| 568 | memcpy(to, &attr->ptr_attr.data, min_size); |
| 569 | else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), |
| 570 | min_size)) |
| 571 | return -EFAULT; |
| 572 | |
| 573 | if (size > min_size) |
| 574 | memset(to + min_size, 0, size - min_size); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | #define uverbs_copy_from(to, attrs_bundle, idx) \ |
| 580 | _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) |
| 581 | |
| 582 | #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ |
| 583 | _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) |
| 584 | |
| 585 | #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) |
| 586 | int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 587 | size_t idx, u64 allowed_bits); |
| 588 | int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 589 | size_t idx, u64 allowed_bits); |
| 590 | int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, |
| 591 | const void *from, size_t size); |
| 592 | __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, |
| 593 | gfp_t flags); |
| 594 | |
| 595 | static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, |
| 596 | size_t size) |
| 597 | { |
| 598 | return _uverbs_alloc(bundle, size, GFP_KERNEL); |
| 599 | } |
| 600 | |
| 601 | static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, |
| 602 | size_t size) |
| 603 | { |
| 604 | return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); |
| 605 | } |
| 606 | #else |
| 607 | static inline int |
| 608 | uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 609 | size_t idx, u64 allowed_bits) |
| 610 | { |
| 611 | return -EINVAL; |
| 612 | } |
| 613 | static inline int |
| 614 | uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, |
| 615 | size_t idx, u64 allowed_bits) |
| 616 | { |
| 617 | return -EINVAL; |
| 618 | } |
| 619 | static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, |
| 620 | size_t idx, const void *from, size_t size) |
| 621 | { |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, |
| 625 | size_t size) |
| 626 | { |
| 627 | return ERR_PTR(-EINVAL); |
| 628 | } |
| 629 | static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, |
| 630 | size_t size) |
| 631 | { |
| 632 | return ERR_PTR(-EINVAL); |
| 633 | } |
| 634 | #endif |
| 635 | |
| 636 | #endif |