1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 2 /* 3 * Copyright (c) 2017, Mellanox Technologies inc. All rights reserved. 4 */ 5 6 #ifndef _UVERBS_IOCTL_ 7 #define _UVERBS_IOCTL_ 8 9 #include <rdma/uverbs_types.h> 10 #include <linux/uaccess.h> 11 #include <rdma/rdma_user_ioctl.h> 12 #include <rdma/ib_user_ioctl_verbs.h> 13 #include <rdma/ib_user_ioctl_cmds.h> 14 15 /* 16 * ======================================= 17 * Verbs action specifications 18 * ======================================= 19 */ 20 21 enum uverbs_attr_type { 22 UVERBS_ATTR_TYPE_NA, 23 UVERBS_ATTR_TYPE_PTR_IN, 24 UVERBS_ATTR_TYPE_PTR_OUT, 25 UVERBS_ATTR_TYPE_IDR, 26 UVERBS_ATTR_TYPE_FD, 27 UVERBS_ATTR_TYPE_RAW_FD, 28 UVERBS_ATTR_TYPE_ENUM_IN, 29 UVERBS_ATTR_TYPE_IDRS_ARRAY, 30 }; 31 32 enum uverbs_obj_access { 33 UVERBS_ACCESS_READ, 34 UVERBS_ACCESS_WRITE, 35 UVERBS_ACCESS_NEW, 36 UVERBS_ACCESS_DESTROY 37 }; 38 39 /* Specification of a single attribute inside the ioctl message */ 40 /* good size 16 */ 41 struct uverbs_attr_spec { 42 u8 type; 43 44 /* 45 * Support extending attributes by length. Allow the user to provide 46 * more bytes than ptr.len, but check that everything after is zero'd 47 * by the user. 48 */ 49 u8 zero_trailing:1; 50 /* 51 * Valid only for PTR_IN. Allocate and copy the data inside 52 * the parser 53 */ 54 u8 alloc_and_copy:1; 55 u8 mandatory:1; 56 /* True if this is from UVERBS_ATTR_UHW */ 57 u8 is_udata:1; 58 59 union { 60 struct { 61 /* Current known size to kernel */ 62 u16 len; 63 /* User isn't allowed to provide something < min_len */ 64 u16 min_len; 65 } ptr; 66 67 struct { 68 /* 69 * higher bits mean the namespace and lower bits mean 70 * the type id within the namespace. 71 */ 72 u16 obj_type; 73 u8 access; 74 } obj; 75 76 struct { 77 u8 num_elems; 78 } enum_def; 79 } u; 80 81 /* This weird split lets us remove some padding */ 82 union { 83 struct { 84 /* 85 * The enum attribute can select one of the attributes 86 * contained in the ids array. Currently only PTR_IN 87 * attributes are supported in the ids array. 88 */ 89 const struct uverbs_attr_spec *ids; 90 } enum_def; 91 92 struct { 93 /* 94 * higher bits mean the namespace and lower bits mean 95 * the type id within the namespace. 96 */ 97 u16 obj_type; 98 u16 min_len; 99 u16 max_len; 100 u8 access; 101 } objs_arr; 102 } u2; 103 }; 104 105 /* 106 * Information about the API is loaded into a radix tree. For IOCTL we start 107 * with a tuple of: 108 * object_id, attr_id, method_id 109 * 110 * Which is a 48 bit value, with most of the bits guaranteed to be zero. Based 111 * on the current kernel support this is compressed into 16 bit key for the 112 * radix tree. Since this compression is entirely internal to the kernel the 113 * below limits can be revised if the kernel gains additional data. 114 * 115 * With 64 leafs per node this is a 3 level radix tree. 116 * 117 * The tree encodes multiple types, and uses a scheme where OBJ_ID,0,0 returns 118 * the object slot, and OBJ_ID,METH_ID,0 and returns the method slot. 119 * 120 * This also encodes the tables for the write() and write() extended commands 121 * using the coding 122 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE,command # 123 * OBJ_ID,UVERBS_API_METHOD_IS_WRITE_EX,command_ex # 124 * ie the WRITE path is treated as a special method type in the ioctl 125 * framework. 126 */ 127 enum uapi_radix_data { 128 UVERBS_API_NS_FLAG = 1U << UVERBS_ID_NS_SHIFT, 129 130 UVERBS_API_ATTR_KEY_BITS = 6, 131 UVERBS_API_ATTR_KEY_MASK = GENMASK(UVERBS_API_ATTR_KEY_BITS - 1, 0), 132 UVERBS_API_ATTR_BKEY_LEN = (1 << UVERBS_API_ATTR_KEY_BITS) - 1, 133 UVERBS_API_WRITE_KEY_NUM = 1 << UVERBS_API_ATTR_KEY_BITS, 134 135 UVERBS_API_METHOD_KEY_BITS = 5, 136 UVERBS_API_METHOD_KEY_SHIFT = UVERBS_API_ATTR_KEY_BITS, 137 UVERBS_API_METHOD_KEY_NUM_CORE = 22, 138 UVERBS_API_METHOD_IS_WRITE = 30 << UVERBS_API_METHOD_KEY_SHIFT, 139 UVERBS_API_METHOD_IS_WRITE_EX = 31 << UVERBS_API_METHOD_KEY_SHIFT, 140 UVERBS_API_METHOD_KEY_NUM_DRIVER = 141 (UVERBS_API_METHOD_IS_WRITE >> UVERBS_API_METHOD_KEY_SHIFT) - 142 UVERBS_API_METHOD_KEY_NUM_CORE, 143 UVERBS_API_METHOD_KEY_MASK = GENMASK( 144 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT - 1, 145 UVERBS_API_METHOD_KEY_SHIFT), 146 147 UVERBS_API_OBJ_KEY_BITS = 5, 148 UVERBS_API_OBJ_KEY_SHIFT = 149 UVERBS_API_METHOD_KEY_BITS + UVERBS_API_METHOD_KEY_SHIFT, 150 UVERBS_API_OBJ_KEY_NUM_CORE = 20, 151 UVERBS_API_OBJ_KEY_NUM_DRIVER = 152 (1 << UVERBS_API_OBJ_KEY_BITS) - UVERBS_API_OBJ_KEY_NUM_CORE, 153 UVERBS_API_OBJ_KEY_MASK = GENMASK(31, UVERBS_API_OBJ_KEY_SHIFT), 154 155 /* This id guaranteed to not exist in the radix tree */ 156 UVERBS_API_KEY_ERR = 0xFFFFFFFF, 157 }; 158 159 static inline __attribute_const__ u32 uapi_key_obj(u32 id) 160 { 161 if (id & UVERBS_API_NS_FLAG) { 162 id &= ~UVERBS_API_NS_FLAG; 163 if (id >= UVERBS_API_OBJ_KEY_NUM_DRIVER) 164 return UVERBS_API_KEY_ERR; 165 id = id + UVERBS_API_OBJ_KEY_NUM_CORE; 166 } else { 167 if (id >= UVERBS_API_OBJ_KEY_NUM_CORE) 168 return UVERBS_API_KEY_ERR; 169 } 170 171 return id << UVERBS_API_OBJ_KEY_SHIFT; 172 } 173 174 static inline __attribute_const__ bool uapi_key_is_object(u32 key) 175 { 176 return (key & ~UVERBS_API_OBJ_KEY_MASK) == 0; 177 } 178 179 static inline __attribute_const__ u32 uapi_key_ioctl_method(u32 id) 180 { 181 if (id & UVERBS_API_NS_FLAG) { 182 id &= ~UVERBS_API_NS_FLAG; 183 if (id >= UVERBS_API_METHOD_KEY_NUM_DRIVER) 184 return UVERBS_API_KEY_ERR; 185 id = id + UVERBS_API_METHOD_KEY_NUM_CORE; 186 } else { 187 id++; 188 if (id >= UVERBS_API_METHOD_KEY_NUM_CORE) 189 return UVERBS_API_KEY_ERR; 190 } 191 192 return id << UVERBS_API_METHOD_KEY_SHIFT; 193 } 194 195 static inline __attribute_const__ u32 uapi_key_write_method(u32 id) 196 { 197 if (id >= UVERBS_API_WRITE_KEY_NUM) 198 return UVERBS_API_KEY_ERR; 199 return UVERBS_API_METHOD_IS_WRITE | id; 200 } 201 202 static inline __attribute_const__ u32 uapi_key_write_ex_method(u32 id) 203 { 204 if (id >= UVERBS_API_WRITE_KEY_NUM) 205 return UVERBS_API_KEY_ERR; 206 return UVERBS_API_METHOD_IS_WRITE_EX | id; 207 } 208 209 static inline __attribute_const__ u32 210 uapi_key_attr_to_ioctl_method(u32 attr_key) 211 { 212 return attr_key & 213 (UVERBS_API_OBJ_KEY_MASK | UVERBS_API_METHOD_KEY_MASK); 214 } 215 216 static inline __attribute_const__ bool uapi_key_is_ioctl_method(u32 key) 217 { 218 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 219 220 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 221 (key & UVERBS_API_ATTR_KEY_MASK) == 0; 222 } 223 224 static inline __attribute_const__ bool uapi_key_is_write_method(u32 key) 225 { 226 return (key & UVERBS_API_METHOD_KEY_MASK) == UVERBS_API_METHOD_IS_WRITE; 227 } 228 229 static inline __attribute_const__ bool uapi_key_is_write_ex_method(u32 key) 230 { 231 return (key & UVERBS_API_METHOD_KEY_MASK) == 232 UVERBS_API_METHOD_IS_WRITE_EX; 233 } 234 235 static inline __attribute_const__ u32 uapi_key_attrs_start(u32 ioctl_method_key) 236 { 237 /* 0 is the method slot itself */ 238 return ioctl_method_key + 1; 239 } 240 241 static inline __attribute_const__ u32 uapi_key_attr(u32 id) 242 { 243 /* 244 * The attr is designed to fit in the typical single radix tree node 245 * of 64 entries. Since allmost all methods have driver attributes we 246 * organize things so that the driver and core attributes interleave to 247 * reduce the length of the attributes array in typical cases. 248 */ 249 if (id & UVERBS_API_NS_FLAG) { 250 id &= ~UVERBS_API_NS_FLAG; 251 id++; 252 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 253 return UVERBS_API_KEY_ERR; 254 id = (id << 1) | 0; 255 } else { 256 if (id >= 1 << (UVERBS_API_ATTR_KEY_BITS - 1)) 257 return UVERBS_API_KEY_ERR; 258 id = (id << 1) | 1; 259 } 260 261 return id; 262 } 263 264 /* Only true for ioctl methods */ 265 static inline __attribute_const__ bool uapi_key_is_attr(u32 key) 266 { 267 unsigned int method = key & UVERBS_API_METHOD_KEY_MASK; 268 269 return method != 0 && method < UVERBS_API_METHOD_IS_WRITE && 270 (key & UVERBS_API_ATTR_KEY_MASK) != 0; 271 } 272 273 /* 274 * This returns a value in the range [0 to UVERBS_API_ATTR_BKEY_LEN), 275 * basically it undoes the reservation of 0 in the ID numbering. attr_key 276 * must already be masked with UVERBS_API_ATTR_KEY_MASK, or be the output of 277 * uapi_key_attr(). 278 */ 279 static inline __attribute_const__ u32 uapi_bkey_attr(u32 attr_key) 280 { 281 return attr_key - 1; 282 } 283 284 static inline __attribute_const__ u32 uapi_bkey_to_key_attr(u32 attr_bkey) 285 { 286 return attr_bkey + 1; 287 } 288 289 /* 290 * ======================================= 291 * Verbs definitions 292 * ======================================= 293 */ 294 295 struct uverbs_attr_def { 296 u16 id; 297 struct uverbs_attr_spec attr; 298 }; 299 300 struct uverbs_method_def { 301 u16 id; 302 /* Combination of bits from enum UVERBS_ACTION_FLAG_XXXX */ 303 u32 flags; 304 size_t num_attrs; 305 const struct uverbs_attr_def * const (*attrs)[]; 306 int (*handler)(struct uverbs_attr_bundle *attrs); 307 }; 308 309 struct uverbs_object_def { 310 u16 id; 311 const struct uverbs_obj_type *type_attrs; 312 size_t num_methods; 313 const struct uverbs_method_def * const (*methods)[]; 314 }; 315 316 enum uapi_definition_kind { 317 UAPI_DEF_END = 0, 318 UAPI_DEF_OBJECT_START, 319 UAPI_DEF_WRITE, 320 UAPI_DEF_CHAIN_OBJ_TREE, 321 UAPI_DEF_CHAIN, 322 UAPI_DEF_IS_SUPPORTED_FUNC, 323 UAPI_DEF_IS_SUPPORTED_DEV_FN, 324 }; 325 326 enum uapi_definition_scope { 327 UAPI_SCOPE_OBJECT = 1, 328 UAPI_SCOPE_METHOD = 2, 329 }; 330 331 struct uapi_definition { 332 u8 kind; 333 u8 scope; 334 union { 335 struct { 336 u16 object_id; 337 } object_start; 338 struct { 339 u16 command_num; 340 u8 is_ex:1; 341 u8 has_udata:1; 342 u8 has_resp:1; 343 u8 req_size; 344 u8 resp_size; 345 } write; 346 }; 347 348 union { 349 bool (*func_is_supported)(struct ib_device *device); 350 int (*func_write)(struct uverbs_attr_bundle *attrs); 351 const struct uapi_definition *chain; 352 const struct uverbs_object_def *chain_obj_tree; 353 size_t needs_fn_offset; 354 }; 355 }; 356 357 /* Define things connected to object_id */ 358 #define DECLARE_UVERBS_OBJECT(_object_id, ...) \ 359 { \ 360 .kind = UAPI_DEF_OBJECT_START, \ 361 .object_start = { .object_id = _object_id }, \ 362 }, \ 363 ##__VA_ARGS__ 364 365 /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 366 #define DECLARE_UVERBS_WRITE(_command_num, _func, _cmd_desc, ...) \ 367 { \ 368 .kind = UAPI_DEF_WRITE, \ 369 .scope = UAPI_SCOPE_OBJECT, \ 370 .write = { .is_ex = 0, .command_num = _command_num }, \ 371 .func_write = _func, \ 372 _cmd_desc, \ 373 }, \ 374 ##__VA_ARGS__ 375 376 /* Use in a var_args of DECLARE_UVERBS_OBJECT */ 377 #define DECLARE_UVERBS_WRITE_EX(_command_num, _func, _cmd_desc, ...) \ 378 { \ 379 .kind = UAPI_DEF_WRITE, \ 380 .scope = UAPI_SCOPE_OBJECT, \ 381 .write = { .is_ex = 1, .command_num = _command_num }, \ 382 .func_write = _func, \ 383 _cmd_desc, \ 384 }, \ 385 ##__VA_ARGS__ 386 387 /* 388 * Object is only supported if the function pointer named ibdev_fn in struct 389 * ib_device is not NULL. 390 */ 391 #define UAPI_DEF_OBJ_NEEDS_FN(ibdev_fn) \ 392 { \ 393 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 394 .scope = UAPI_SCOPE_OBJECT, \ 395 .needs_fn_offset = \ 396 offsetof(struct ib_device_ops, ibdev_fn) + \ 397 BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \ 398 ibdev_fn) != \ 399 sizeof(void *)), \ 400 } 401 402 /* 403 * Method is only supported if the function pointer named ibdev_fn in struct 404 * ib_device is not NULL. 405 */ 406 #define UAPI_DEF_METHOD_NEEDS_FN(ibdev_fn) \ 407 { \ 408 .kind = UAPI_DEF_IS_SUPPORTED_DEV_FN, \ 409 .scope = UAPI_SCOPE_METHOD, \ 410 .needs_fn_offset = \ 411 offsetof(struct ib_device_ops, ibdev_fn) + \ 412 BUILD_BUG_ON_ZERO(sizeof_field(struct ib_device_ops, \ 413 ibdev_fn) != \ 414 sizeof(void *)), \ 415 } 416 417 /* Call a function to determine if the entire object is supported or not */ 418 #define UAPI_DEF_IS_OBJ_SUPPORTED(_func) \ 419 { \ 420 .kind = UAPI_DEF_IS_SUPPORTED_FUNC, \ 421 .scope = UAPI_SCOPE_OBJECT, .func_is_supported = _func, \ 422 } 423 424 /* Include another struct uapi_definition in this one */ 425 #define UAPI_DEF_CHAIN(_def_var) \ 426 { \ 427 .kind = UAPI_DEF_CHAIN, .chain = _def_var, \ 428 } 429 430 /* Temporary until the tree base description is replaced */ 431 #define UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, _object_ptr, ...) \ 432 { \ 433 .kind = UAPI_DEF_CHAIN_OBJ_TREE, \ 434 .object_start = { .object_id = _object_enum }, \ 435 .chain_obj_tree = _object_ptr, \ 436 }, \ 437 ##__VA_ARGS__ 438 #define UAPI_DEF_CHAIN_OBJ_TREE_NAMED(_object_enum, ...) \ 439 UAPI_DEF_CHAIN_OBJ_TREE(_object_enum, \ 440 PTR_IF(IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS), \ 441 &UVERBS_OBJECT(_object_enum)), \ 442 ##__VA_ARGS__) 443 444 /* 445 * ======================================= 446 * Attribute Specifications 447 * ======================================= 448 */ 449 450 #define UVERBS_ATTR_SIZE(_min_len, _len) \ 451 .u.ptr.min_len = _min_len, .u.ptr.len = _len 452 453 #define UVERBS_ATTR_NO_DATA() UVERBS_ATTR_SIZE(0, 0) 454 455 /* 456 * Specifies a uapi structure that cannot be extended. The user must always 457 * supply the whole structure and nothing more. The structure must be declared 458 * in a header under include/uapi/rdma. 459 */ 460 #define UVERBS_ATTR_TYPE(_type) \ 461 .u.ptr.min_len = sizeof(_type), .u.ptr.len = sizeof(_type) 462 /* 463 * Specifies a uapi structure where the user must provide at least up to 464 * member 'last'. Anything after last and up until the end of the structure 465 * can be non-zero, anything longer than the end of the structure must be 466 * zero. The structure must be declared in a header under include/uapi/rdma. 467 */ 468 #define UVERBS_ATTR_STRUCT(_type, _last) \ 469 .zero_trailing = 1, \ 470 UVERBS_ATTR_SIZE(offsetofend(_type, _last), sizeof(_type)) 471 /* 472 * Specifies at least min_len bytes must be passed in, but the amount can be 473 * larger, up to the protocol maximum size. No check for zeroing is done. 474 */ 475 #define UVERBS_ATTR_MIN_SIZE(_min_len) UVERBS_ATTR_SIZE(_min_len, USHRT_MAX) 476 477 /* Must be used in the '...' of any UVERBS_ATTR */ 478 #define UA_ALLOC_AND_COPY .alloc_and_copy = 1 479 #define UA_MANDATORY .mandatory = 1 480 #define UA_OPTIONAL .mandatory = 0 481 482 /* 483 * min_len must be bigger than 0 and _max_len must be smaller than 4095. Only 484 * READ\WRITE accesses are supported. 485 */ 486 #define UVERBS_ATTR_IDRS_ARR(_attr_id, _idr_type, _access, _min_len, _max_len, \ 487 ...) \ 488 (&(const struct uverbs_attr_def){ \ 489 .id = (_attr_id) + \ 490 BUILD_BUG_ON_ZERO((_min_len) == 0 || \ 491 (_max_len) > \ 492 PAGE_SIZE / sizeof(void *) || \ 493 (_min_len) > (_max_len) || \ 494 (_access) == UVERBS_ACCESS_NEW || \ 495 (_access) == UVERBS_ACCESS_DESTROY), \ 496 .attr = { .type = UVERBS_ATTR_TYPE_IDRS_ARRAY, \ 497 .u2.objs_arr.obj_type = _idr_type, \ 498 .u2.objs_arr.access = _access, \ 499 .u2.objs_arr.min_len = _min_len, \ 500 .u2.objs_arr.max_len = _max_len, \ 501 __VA_ARGS__ } }) 502 503 /* 504 * Only for use with UVERBS_ATTR_IDR, allows any uobject type to be accepted, 505 * the user must validate the type of the uobject instead. 506 */ 507 #define UVERBS_IDR_ANY_OBJECT 0xFFFF 508 509 #define UVERBS_ATTR_IDR(_attr_id, _idr_type, _access, ...) \ 510 (&(const struct uverbs_attr_def){ \ 511 .id = _attr_id, \ 512 .attr = { .type = UVERBS_ATTR_TYPE_IDR, \ 513 .u.obj.obj_type = _idr_type, \ 514 .u.obj.access = _access, \ 515 __VA_ARGS__ } }) 516 517 #define UVERBS_ATTR_FD(_attr_id, _fd_type, _access, ...) \ 518 (&(const struct uverbs_attr_def){ \ 519 .id = (_attr_id) + \ 520 BUILD_BUG_ON_ZERO((_access) != UVERBS_ACCESS_NEW && \ 521 (_access) != UVERBS_ACCESS_READ), \ 522 .attr = { .type = UVERBS_ATTR_TYPE_FD, \ 523 .u.obj.obj_type = _fd_type, \ 524 .u.obj.access = _access, \ 525 __VA_ARGS__ } }) 526 527 #define UVERBS_ATTR_RAW_FD(_attr_id, ...) \ 528 (&(const struct uverbs_attr_def){ \ 529 .id = (_attr_id), \ 530 .attr = { .type = UVERBS_ATTR_TYPE_RAW_FD, __VA_ARGS__ } }) 531 532 #define UVERBS_ATTR_PTR_IN(_attr_id, _type, ...) \ 533 (&(const struct uverbs_attr_def){ \ 534 .id = _attr_id, \ 535 .attr = { .type = UVERBS_ATTR_TYPE_PTR_IN, \ 536 _type, \ 537 __VA_ARGS__ } }) 538 539 #define UVERBS_ATTR_PTR_OUT(_attr_id, _type, ...) \ 540 (&(const struct uverbs_attr_def){ \ 541 .id = _attr_id, \ 542 .attr = { .type = UVERBS_ATTR_TYPE_PTR_OUT, \ 543 _type, \ 544 __VA_ARGS__ } }) 545 546 /* _enum_arry should be a 'static const union uverbs_attr_spec[]' */ 547 #define UVERBS_ATTR_ENUM_IN(_attr_id, _enum_arr, ...) \ 548 (&(const struct uverbs_attr_def){ \ 549 .id = _attr_id, \ 550 .attr = { .type = UVERBS_ATTR_TYPE_ENUM_IN, \ 551 .u2.enum_def.ids = _enum_arr, \ 552 .u.enum_def.num_elems = ARRAY_SIZE(_enum_arr), \ 553 __VA_ARGS__ }, \ 554 }) 555 556 /* An input value that is a member in the enum _enum_type. */ 557 #define UVERBS_ATTR_CONST_IN(_attr_id, _enum_type, ...) \ 558 UVERBS_ATTR_PTR_IN( \ 559 _attr_id, \ 560 UVERBS_ATTR_SIZE( \ 561 sizeof(u64) + BUILD_BUG_ON_ZERO(!sizeof(_enum_type)), \ 562 sizeof(u64)), \ 563 __VA_ARGS__) 564 565 /* 566 * An input value that is a bitwise combination of values of _enum_type. 567 * This permits the flag value to be passed as either a u32 or u64, it must 568 * be retrieved via uverbs_get_flag(). 569 */ 570 #define UVERBS_ATTR_FLAGS_IN(_attr_id, _enum_type, ...) \ 571 UVERBS_ATTR_PTR_IN( \ 572 _attr_id, \ 573 UVERBS_ATTR_SIZE(sizeof(u32) + BUILD_BUG_ON_ZERO( \ 574 !sizeof(_enum_type *)), \ 575 sizeof(u64)), \ 576 __VA_ARGS__) 577 578 /* 579 * This spec is used in order to pass information to the hardware driver in a 580 * legacy way. Every verb that could get driver specific data should get this 581 * spec. 582 */ 583 #define UVERBS_ATTR_UHW() \ 584 UVERBS_ATTR_PTR_IN(UVERBS_ATTR_UHW_IN, \ 585 UVERBS_ATTR_MIN_SIZE(0), \ 586 UA_OPTIONAL, \ 587 .is_udata = 1), \ 588 UVERBS_ATTR_PTR_OUT(UVERBS_ATTR_UHW_OUT, \ 589 UVERBS_ATTR_MIN_SIZE(0), \ 590 UA_OPTIONAL, \ 591 .is_udata = 1) 592 593 /* 594 * Per-attribute UMEM descriptor. The payload is a single 595 * struct ib_uverbs_buffer_desc identifying a memory region backed by 596 * dma-buf or user virtual address. _access selects UA_OPTIONAL or 597 * UA_MANDATORY. Drivers obtain a umem from the attribute via the 598 * ib_umem_get_*() wrapper helpers. 599 */ 600 #define UVERBS_ATTR_UMEM(_attr_id, _access) \ 601 UVERBS_ATTR_PTR_IN(_attr_id, \ 602 UVERBS_ATTR_TYPE(struct ib_uverbs_buffer_desc), \ 603 _access) 604 605 /* 606 * Bit masks of the @flags / @optional_flags fields of struct 607 * ib_uverbs_buffer_desc that the kernel understands. @flags is strict: 608 * any bit outside the known mask makes the call fail with -EINVAL. 609 * @optional_flags is advisory: bits outside the known mask are silently 610 * dropped. Both masks are extended as new bits are introduced. 611 */ 612 #define IB_UVERBS_BUFFER_DESC_FLAGS_KNOWN_MASK 0U 613 #define IB_UVERBS_BUFFER_DESC_OPTIONAL_FLAGS_KNOWN_MASK 0U 614 615 /* ================================================= 616 * Parsing infrastructure 617 * ================================================= 618 */ 619 620 621 struct uverbs_ptr_attr { 622 /* 623 * If UVERBS_ATTR_SPEC_F_ALLOC_AND_COPY is set then the 'ptr' is 624 * used. 625 */ 626 union { 627 void *ptr; 628 u64 data; 629 }; 630 u16 len; 631 u16 uattr_idx; 632 u8 enum_id; 633 }; 634 635 struct uverbs_obj_attr { 636 struct ib_uobject *uobject; 637 const struct uverbs_api_attr *attr_elm; 638 }; 639 640 struct uverbs_objs_arr_attr { 641 struct ib_uobject **uobjects; 642 u16 len; 643 }; 644 645 struct uverbs_attr { 646 union { 647 struct uverbs_ptr_attr ptr_attr; 648 struct uverbs_obj_attr obj_attr; 649 struct uverbs_objs_arr_attr objs_arr_attr; 650 }; 651 }; 652 653 struct uverbs_attr_bundle { 654 struct_group_tagged(uverbs_attr_bundle_hdr, hdr, 655 struct ib_udata driver_udata; 656 struct ib_udata ucore; 657 struct ib_uverbs_file *ufile; 658 struct ib_ucontext *context; 659 struct ib_uobject *uobject; 660 const struct uverbs_api_ioctl_method *method_elm; 661 DECLARE_BITMAP(attr_present, UVERBS_API_ATTR_BKEY_LEN); 662 ); 663 struct uverbs_attr attrs[]; 664 }; 665 666 static inline bool uverbs_attr_is_valid(const struct uverbs_attr_bundle *attrs_bundle, 667 unsigned int idx) 668 { 669 return test_bit(uapi_bkey_attr(uapi_key_attr(idx)), 670 attrs_bundle->attr_present); 671 } 672 673 /** 674 * rdma_udata_to_drv_context - Helper macro to get the driver's context out of 675 * ib_udata which is embedded in uverbs_attr_bundle. 676 * 677 * If udata is not NULL this cannot fail. Otherwise a NULL udata will result 678 * in a NULL ucontext pointer, as a safety precaution. Callers should be using 679 * 'udata' to determine if the driver call is in user or kernel mode, not 680 * 'ucontext'. 681 * 682 */ 683 static inline struct uverbs_attr_bundle * 684 rdma_udata_to_uverbs_attr_bundle(struct ib_udata *udata) 685 { 686 return container_of(udata, struct uverbs_attr_bundle, driver_udata); 687 } 688 689 #define rdma_udata_to_drv_context(udata, drv_dev_struct, member) \ 690 (udata ? container_of(rdma_udata_to_uverbs_attr_bundle(udata)->context, \ 691 drv_dev_struct, member) : (drv_dev_struct *)NULL) 692 693 #define IS_UVERBS_COPY_ERR(_ret) ((_ret) && (_ret) != -ENOENT) 694 695 static inline const struct uverbs_attr *uverbs_attr_get(const struct uverbs_attr_bundle *attrs_bundle, 696 u16 idx) 697 { 698 if (!uverbs_attr_is_valid(attrs_bundle, idx)) 699 return ERR_PTR(-ENOENT); 700 701 return &attrs_bundle->attrs[uapi_bkey_attr(uapi_key_attr(idx))]; 702 } 703 704 static inline int uverbs_attr_get_enum_id(const struct uverbs_attr_bundle *attrs_bundle, 705 u16 idx) 706 { 707 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 708 709 if (IS_ERR(attr)) 710 return PTR_ERR(attr); 711 712 return attr->ptr_attr.enum_id; 713 } 714 715 static inline void *uverbs_attr_get_obj(const struct uverbs_attr_bundle *attrs_bundle, 716 u16 idx) 717 { 718 const struct uverbs_attr *attr; 719 720 attr = uverbs_attr_get(attrs_bundle, idx); 721 if (IS_ERR(attr)) 722 return ERR_CAST(attr); 723 724 return attr->obj_attr.uobject->object; 725 } 726 727 static inline struct ib_uobject *uverbs_attr_get_uobject(const struct uverbs_attr_bundle *attrs_bundle, 728 u16 idx) 729 { 730 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 731 732 if (IS_ERR(attr)) 733 return ERR_CAST(attr); 734 735 return attr->obj_attr.uobject; 736 } 737 738 static inline int 739 uverbs_attr_get_len(const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 740 { 741 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 742 743 if (IS_ERR(attr)) 744 return PTR_ERR(attr); 745 746 return attr->ptr_attr.len; 747 } 748 749 void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *attrs_bundle, 750 u16 idx); 751 752 /* 753 * uverbs_attr_ptr_get_array_size() - Get array size pointer by a ptr 754 * attribute. 755 * @attrs: The attribute bundle 756 * @idx: The ID of the attribute 757 * @elem_size: The size of the element in the array 758 */ 759 static inline int 760 uverbs_attr_ptr_get_array_size(struct uverbs_attr_bundle *attrs, u16 idx, 761 size_t elem_size) 762 { 763 int size = uverbs_attr_get_len(attrs, idx); 764 765 if (size < 0) 766 return size; 767 768 if (size % elem_size) 769 return -EINVAL; 770 771 return size / elem_size; 772 } 773 774 /** 775 * uverbs_attr_get_uobjs_arr() - Provides array's properties for attribute for 776 * UVERBS_ATTR_TYPE_IDRS_ARRAY. 777 * @arr: Returned pointer to array of pointers for uobjects or NULL if 778 * the attribute isn't provided. 779 * 780 * Return: The array length or 0 if no attribute was provided. 781 */ 782 static inline int uverbs_attr_get_uobjs_arr( 783 const struct uverbs_attr_bundle *attrs_bundle, u16 attr_idx, 784 struct ib_uobject ***arr) 785 { 786 const struct uverbs_attr *attr = 787 uverbs_attr_get(attrs_bundle, attr_idx); 788 789 if (IS_ERR(attr)) { 790 *arr = NULL; 791 return 0; 792 } 793 794 *arr = attr->objs_arr_attr.uobjects; 795 796 return attr->objs_arr_attr.len; 797 } 798 799 static inline bool uverbs_attr_ptr_is_inline(const struct uverbs_attr *attr) 800 { 801 return attr->ptr_attr.len <= sizeof(attr->ptr_attr.data); 802 } 803 804 static inline void *uverbs_attr_get_alloced_ptr( 805 const struct uverbs_attr_bundle *attrs_bundle, u16 idx) 806 { 807 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 808 809 if (IS_ERR(attr)) 810 return (void *)attr; 811 812 return uverbs_attr_ptr_is_inline(attr) ? (void *)&attr->ptr_attr.data : 813 attr->ptr_attr.ptr; 814 } 815 816 static inline int _uverbs_copy_from(void *to, 817 const struct uverbs_attr_bundle *attrs_bundle, 818 size_t idx, 819 size_t size) 820 { 821 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 822 823 if (IS_ERR(attr)) 824 return PTR_ERR(attr); 825 826 /* 827 * Validation ensures attr->ptr_attr.len >= size. If the caller is 828 * using UVERBS_ATTR_SPEC_F_MIN_SZ_OR_ZERO then it must call 829 * uverbs_copy_from_or_zero. 830 */ 831 if (unlikely(size < attr->ptr_attr.len)) 832 return -EINVAL; 833 834 if (uverbs_attr_ptr_is_inline(attr)) 835 memcpy(to, &attr->ptr_attr.data, attr->ptr_attr.len); 836 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 837 attr->ptr_attr.len)) 838 return -EFAULT; 839 840 return 0; 841 } 842 843 static inline int _uverbs_copy_from_or_zero(void *to, 844 const struct uverbs_attr_bundle *attrs_bundle, 845 size_t idx, 846 size_t size) 847 { 848 const struct uverbs_attr *attr = uverbs_attr_get(attrs_bundle, idx); 849 size_t min_size; 850 851 if (IS_ERR(attr)) 852 return PTR_ERR(attr); 853 854 min_size = min_t(size_t, size, attr->ptr_attr.len); 855 856 if (uverbs_attr_ptr_is_inline(attr)) 857 memcpy(to, &attr->ptr_attr.data, min_size); 858 else if (copy_from_user(to, u64_to_user_ptr(attr->ptr_attr.data), 859 min_size)) 860 return -EFAULT; 861 862 if (size > min_size) 863 memset(to + min_size, 0, size - min_size); 864 865 return 0; 866 } 867 868 #define uverbs_copy_from(to, attrs_bundle, idx) \ 869 _uverbs_copy_from(to, attrs_bundle, idx, sizeof(*to)) 870 871 #define uverbs_copy_from_or_zero(to, attrs_bundle, idx) \ 872 _uverbs_copy_from_or_zero(to, attrs_bundle, idx, sizeof(*to)) 873 874 static inline struct ib_ucontext * 875 ib_uverbs_get_ucontext(const struct uverbs_attr_bundle *attrs) 876 { 877 return ib_uverbs_get_ucontext_file(attrs->ufile); 878 } 879 880 #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS) 881 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 882 size_t idx, u64 allowed_bits); 883 int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 884 size_t idx, u64 allowed_bits); 885 int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, size_t idx, 886 const void *from, size_t size); 887 int uverbs_get_buffer_desc(const struct uverbs_attr_bundle *attrs_bundle, 888 u16 attr_id, struct ib_uverbs_buffer_desc *desc); 889 __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 890 gfp_t flags); 891 892 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 893 size_t size) 894 { 895 return _uverbs_alloc(bundle, size, GFP_KERNEL); 896 } 897 898 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 899 size_t size) 900 { 901 return _uverbs_alloc(bundle, size, GFP_KERNEL | __GFP_ZERO); 902 } 903 904 static inline __malloc void *uverbs_kcalloc(struct uverbs_attr_bundle *bundle, 905 size_t n, size_t size) 906 { 907 size_t bytes; 908 909 if (unlikely(check_mul_overflow(n, size, &bytes))) 910 return ERR_PTR(-EOVERFLOW); 911 return uverbs_zalloc(bundle, bytes); 912 } 913 914 int _uverbs_get_const_signed(s64 *to, 915 const struct uverbs_attr_bundle *attrs_bundle, 916 size_t idx, s64 lower_bound, u64 upper_bound, 917 s64 *def_val); 918 int _uverbs_get_const_unsigned(u64 *to, 919 const struct uverbs_attr_bundle *attrs_bundle, 920 size_t idx, u64 upper_bound, u64 *def_val); 921 int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 922 size_t idx, const void *from, size_t size); 923 924 int _ib_copy_validate_udata_in(struct ib_udata *udata, void *req, 925 size_t kernel_size, size_t minimum_size); 926 int _ib_respond_udata(struct ib_udata *udata, const void *src, size_t len); 927 int _ib_copy_validate_udata_cm_fail(struct ib_udata *udata, u64 req_cm, 928 u64 valid_cm); 929 #else 930 static inline int 931 uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 932 size_t idx, u64 allowed_bits) 933 { 934 return -EINVAL; 935 } 936 static inline int 937 uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 938 size_t idx, u64 allowed_bits) 939 { 940 return -EINVAL; 941 } 942 static inline int uverbs_copy_to(const struct uverbs_attr_bundle *attrs_bundle, 943 size_t idx, const void *from, size_t size) 944 { 945 return -EINVAL; 946 } 947 static inline int 948 uverbs_get_buffer_desc(const struct uverbs_attr_bundle *attrs_bundle, 949 u16 attr_id, struct ib_uverbs_buffer_desc *desc) 950 { 951 return -EINVAL; 952 } 953 static inline __malloc void *uverbs_alloc(struct uverbs_attr_bundle *bundle, 954 size_t size) 955 { 956 return ERR_PTR(-EINVAL); 957 } 958 static inline __malloc void *uverbs_zalloc(struct uverbs_attr_bundle *bundle, 959 size_t size) 960 { 961 return ERR_PTR(-EINVAL); 962 } 963 static inline int 964 _uverbs_get_const(s64 *to, const struct uverbs_attr_bundle *attrs_bundle, 965 size_t idx, s64 lower_bound, u64 upper_bound, 966 s64 *def_val) 967 { 968 return -EINVAL; 969 } 970 static inline int 971 uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 972 size_t idx, const void *from, size_t size) 973 { 974 return -EINVAL; 975 } 976 static inline int 977 _uverbs_get_const_signed(s64 *to, 978 const struct uverbs_attr_bundle *attrs_bundle, 979 size_t idx, s64 lower_bound, u64 upper_bound, 980 s64 *def_val) 981 { 982 return -EINVAL; 983 } 984 static inline int 985 _uverbs_get_const_unsigned(u64 *to, 986 const struct uverbs_attr_bundle *attrs_bundle, 987 size_t idx, u64 upper_bound, u64 *def_val) 988 { 989 return -EINVAL; 990 } 991 992 static inline int _ib_copy_validate_udata_in(struct ib_udata *udata, void *req, 993 size_t kernel_size, 994 size_t minimum_size) 995 { 996 return -EINVAL; 997 } 998 999 static inline int _ib_respond_udata(struct ib_udata *udata, const void *src, 1000 size_t len) 1001 { 1002 return -EINVAL; 1003 } 1004 1005 static inline int _ib_copy_validate_udata_cm_fail(struct ib_udata *udata, 1006 u64 req_cm, u64 valid_cm) 1007 { 1008 return -EINVAL; 1009 } 1010 #endif 1011 1012 #define uverbs_get_const_signed(_to, _attrs_bundle, _idx) \ 1013 ({ \ 1014 s64 _val; \ 1015 int _ret = \ 1016 _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \ 1017 type_min(typeof(*(_to))), \ 1018 type_max(typeof(*(_to))), NULL); \ 1019 (*(_to)) = _val; \ 1020 _ret; \ 1021 }) 1022 1023 #define uverbs_get_const_unsigned(_to, _attrs_bundle, _idx) \ 1024 ({ \ 1025 u64 _val; \ 1026 int _ret = \ 1027 _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \ 1028 type_max(typeof(*(_to))), NULL); \ 1029 (*(_to)) = _val; \ 1030 _ret; \ 1031 }) 1032 1033 #define uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, _default) \ 1034 ({ \ 1035 s64 _val; \ 1036 s64 _def_val = _default; \ 1037 int _ret = \ 1038 _uverbs_get_const_signed(&_val, _attrs_bundle, _idx, \ 1039 type_min(typeof(*(_to))), \ 1040 type_max(typeof(*(_to))), &_def_val); \ 1041 (*(_to)) = _val; \ 1042 _ret; \ 1043 }) 1044 1045 #define uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, _default) \ 1046 ({ \ 1047 u64 _val; \ 1048 u64 _def_val = _default; \ 1049 int _ret = \ 1050 _uverbs_get_const_unsigned(&_val, _attrs_bundle, _idx, \ 1051 type_max(typeof(*(_to))), &_def_val); \ 1052 (*(_to)) = _val; \ 1053 _ret; \ 1054 }) 1055 1056 #define uverbs_get_const(_to, _attrs_bundle, _idx) \ 1057 (is_signed_type(typeof(*(_to))) ? \ 1058 uverbs_get_const_signed(_to, _attrs_bundle, _idx) : \ 1059 uverbs_get_const_unsigned(_to, _attrs_bundle, _idx)) \ 1060 1061 #define uverbs_get_const_default(_to, _attrs_bundle, _idx, _default) \ 1062 (is_signed_type(typeof(*(_to))) ? \ 1063 uverbs_get_const_default_signed(_to, _attrs_bundle, _idx, \ 1064 _default) : \ 1065 uverbs_get_const_default_unsigned(_to, _attrs_bundle, _idx, \ 1066 _default)) 1067 1068 static inline int 1069 uverbs_get_raw_fd(int *to, const struct uverbs_attr_bundle *attrs_bundle, 1070 size_t idx) 1071 { 1072 return uverbs_get_const_signed(to, attrs_bundle, idx); 1073 } 1074 1075 /** 1076 * ib_copy_validate_udata_in - Copy and validate that the request structure is 1077 * compatible with this kernel 1078 * @_udata: The system calls ib_udata struct 1079 * @_req: The name of an on-stack structure that holds the driver data 1080 * @_end_member: The member in the struct that is the original end of struct 1081 * from the first kernel to introduce it. 1082 * 1083 * Check that the udata input request struct is properly formed for this kernel. 1084 * Then copy it into req 1085 */ 1086 #define ib_copy_validate_udata_in(_udata, _req, _end_member) \ 1087 _ib_copy_validate_udata_in(_udata, &(_req), sizeof(_req), \ 1088 offsetofend(typeof(_req), _end_member)) 1089 1090 /** 1091 * ib_copy_validate_udata_in_cm - Copy the req structure and check the comp_mask 1092 * @_udata: The system calls ib_udata struct 1093 * @_req: The name of an on-stack structure that holds the driver data 1094 * @_end_member: The member in the struct that is the original end of struct 1095 * from the first kernel to introduce it. 1096 * @_valid_cm: A bitmask of bits permitted in the comp_mask_field. 1097 * 1098 * Check that the udata input request struct is properly formed for this kernel. 1099 * Then copy it into req 1100 */ 1101 #define ib_copy_validate_udata_in_cm(_udata, _req, _end_member, _valid_cm) \ 1102 ({ \ 1103 typeof((_req).comp_mask) __valid_cm = _valid_cm; \ 1104 int ret = \ 1105 ib_copy_validate_udata_in(_udata, _req, _end_member); \ 1106 if (!ret && ((_req).comp_mask & ~__valid_cm)) \ 1107 ret = _ib_copy_validate_udata_cm_fail( \ 1108 _udata, (_req).comp_mask, __valid_cm); \ 1109 ret; \ 1110 }) 1111 1112 /** 1113 * ib_is_udata_in_empty - Check if the udata input buffer is all zeros 1114 * @udata: The system calls ib_udata struct 1115 * 1116 * This should be used if the driver does not currently define a driver data 1117 * struct. Returns 0 if the buffer is empty or all zeros, -EOPNOTSUPP if 1118 * non-zero data is present, or a negative error code on failure. 1119 */ 1120 static inline int ib_is_udata_in_empty(struct ib_udata *udata) 1121 { 1122 if (!udata || udata->inlen == 0) 1123 return 0; 1124 return _ib_copy_validate_udata_in(udata, NULL, 0, 0); 1125 } 1126 1127 /** 1128 * ib_respond_udata - Copy a driver data response to userspace 1129 * @_udata: The system calls ib_udata struct 1130 * @_rep: Kernel buffer containing the response driver data on the stack 1131 * 1132 * Copy driver data response structures back to userspace in a way that 1133 * is forwards and backwards compatible. Longer kernel structs are truncated, 1134 * userspace has made some kind of error if it needed the truncated information. 1135 * Shorter structs are zero padded. 1136 */ 1137 #define ib_respond_udata(_udata, _rep) \ 1138 _ib_respond_udata(_udata, &(_rep), sizeof(_rep)) 1139 1140 /** 1141 * ib_respond_empty_udata - Zero fill the response buffer to userspace 1142 * @_udata: The system calls ib_udata struct 1143 * 1144 * Used when there is no driver response data to return. Provides forward 1145 * compatability by zeroing any buffer the user may have provided. 1146 */ 1147 static inline int ib_respond_empty_udata(struct ib_udata *udata) 1148 { 1149 if (udata && udata->outlen && clear_user(udata->outbuf, udata->outlen)) 1150 return -EFAULT; 1151 return 0; 1152 } 1153 1154 #endif 1155