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 #include <rdma/rdma_user_ioctl.h> 34 #include <rdma/uverbs_ioctl.h> 35 #include "rdma_core.h" 36 #include "uverbs.h" 37 38 /* 39 * Each method has an absolute minimum amount of memory it needs to allocate, 40 * precompute that amount and determine if the onstack memory can be used or 41 * if allocation is need. 42 */ 43 void uapi_compute_bundle_size(struct uverbs_api_ioctl_method *method_elm, 44 unsigned int num_attrs) 45 { 46 struct bundle_priv *pbundle; 47 struct uverbs_attr_bundle *bundle; 48 size_t bundle_size = 49 offsetof(struct bundle_priv, internal_buffer) + 50 sizeof(*bundle->attrs) * method_elm->key_bitmap_len + 51 sizeof(*pbundle->uattrs) * num_attrs; 52 53 method_elm->use_stack = bundle_size <= sizeof(*pbundle); 54 method_elm->bundle_size = 55 ALIGN(bundle_size + 256, sizeof(*pbundle->internal_buffer)); 56 57 /* Do not want order-2 allocations for this. */ 58 WARN_ON_ONCE(method_elm->bundle_size > PAGE_SIZE); 59 } 60 61 /** 62 * _uverbs_alloc() - Quickly allocate memory for use with a bundle 63 * @bundle: The bundle 64 * @size: Number of bytes to allocate 65 * @flags: Allocator flags 66 * 67 * The bundle allocator is intended for allocations that are connected with 68 * processing the system call related to the bundle. The allocated memory is 69 * always freed once the system call completes, and cannot be freed any other 70 * way. 71 * 72 * This tries to use a small pool of pre-allocated memory for performance. 73 */ 74 __malloc void *_uverbs_alloc(struct uverbs_attr_bundle *bundle, size_t size, 75 gfp_t flags) 76 { 77 struct bundle_priv *pbundle = 78 container_of(&bundle->hdr, struct bundle_priv, bundle); 79 size_t new_used; 80 void *res; 81 82 if (check_add_overflow(size, pbundle->internal_used, &new_used)) 83 return ERR_PTR(-EOVERFLOW); 84 85 if (new_used > pbundle->internal_avail) { 86 struct bundle_alloc_head *buf; 87 88 buf = kvmalloc_flex(*buf, data, size, flags); 89 if (!buf) 90 return ERR_PTR(-ENOMEM); 91 buf->next = pbundle->allocated_mem; 92 pbundle->allocated_mem = buf; 93 return buf->data; 94 } 95 96 res = (void *)pbundle->internal_buffer + pbundle->internal_used; 97 pbundle->internal_used = 98 ALIGN(new_used, sizeof(*pbundle->internal_buffer)); 99 if (want_init_on_alloc(flags)) 100 memset(res, 0, size); 101 return res; 102 } 103 EXPORT_SYMBOL(_uverbs_alloc); 104 105 static bool uverbs_is_attr_cleared(const struct ib_uverbs_attr *uattr, 106 u16 len) 107 { 108 if (uattr->len > sizeof_field(struct ib_uverbs_attr, data)) 109 return ib_is_buffer_cleared(u64_to_user_ptr(uattr->data) + len, 110 uattr->len - len); 111 112 return !memchr_inv((const void *)&uattr->data + len, 113 0, uattr->len - len); 114 } 115 116 static int uverbs_set_output(const struct uverbs_attr_bundle *bundle, 117 const struct uverbs_attr *attr) 118 { 119 struct bundle_priv *pbundle = 120 container_of(&bundle->hdr, struct bundle_priv, bundle); 121 u16 flags; 122 123 flags = pbundle->uattrs[attr->ptr_attr.uattr_idx].flags | 124 UVERBS_ATTR_F_VALID_OUTPUT; 125 if (put_user(flags, 126 &pbundle->user_attrs[attr->ptr_attr.uattr_idx].flags)) 127 return -EFAULT; 128 return 0; 129 } 130 131 static int uverbs_process_idrs_array(struct bundle_priv *pbundle, 132 const struct uverbs_api_attr *attr_uapi, 133 struct uverbs_objs_arr_attr *attr, 134 struct ib_uverbs_attr *uattr, 135 u32 attr_bkey) 136 { 137 struct uverbs_attr_bundle *bundle = 138 container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr); 139 const struct uverbs_attr_spec *spec = &attr_uapi->spec; 140 size_t array_len; 141 u32 *idr_vals; 142 int ret = 0; 143 size_t i; 144 145 if (uattr->attr_data.reserved) 146 return -EINVAL; 147 148 if (uattr->len % sizeof(u32)) 149 return -EINVAL; 150 151 array_len = uattr->len / sizeof(u32); 152 if (array_len < spec->u2.objs_arr.min_len || 153 array_len > spec->u2.objs_arr.max_len) 154 return -EINVAL; 155 156 attr->uobjects = 157 uverbs_alloc(bundle, 158 array_size(array_len, sizeof(*attr->uobjects))); 159 if (IS_ERR(attr->uobjects)) 160 return PTR_ERR(attr->uobjects); 161 162 /* 163 * Since idr is 4B and *uobjects is >= 4B, we can use attr->uobjects 164 * to store idrs array and avoid additional memory allocation. The 165 * idrs array is offset to the end of the uobjects array so we will be 166 * able to read idr and replace with a pointer. 167 */ 168 idr_vals = (u32 *)(attr->uobjects + array_len) - array_len; 169 170 if (uattr->len > sizeof(uattr->data)) { 171 ret = copy_from_user(idr_vals, u64_to_user_ptr(uattr->data), 172 uattr->len); 173 if (ret) 174 return -EFAULT; 175 } else { 176 memcpy(idr_vals, &uattr->data, uattr->len); 177 } 178 179 for (i = 0; i != array_len; i++) { 180 attr->uobjects[i] = uverbs_get_uobject_from_file( 181 spec->u2.objs_arr.obj_type, spec->u2.objs_arr.access, 182 idr_vals[i], bundle); 183 if (IS_ERR(attr->uobjects[i])) { 184 ret = PTR_ERR(attr->uobjects[i]); 185 break; 186 } 187 } 188 189 attr->len = i; 190 __set_bit(attr_bkey, pbundle->spec_finalize); 191 return ret; 192 } 193 194 static void uverbs_free_idrs_array(const struct uverbs_api_attr *attr_uapi, 195 struct uverbs_objs_arr_attr *attr, 196 bool commit, 197 struct uverbs_attr_bundle *attrs) 198 { 199 const struct uverbs_attr_spec *spec = &attr_uapi->spec; 200 size_t i; 201 202 for (i = 0; i != attr->len; i++) 203 uverbs_finalize_object(attr->uobjects[i], 204 spec->u2.objs_arr.access, false, commit, 205 attrs); 206 } 207 208 static int uverbs_process_attr(struct bundle_priv *pbundle, 209 const struct uverbs_api_attr *attr_uapi, 210 struct ib_uverbs_attr *uattr, u32 attr_bkey) 211 { 212 const struct uverbs_attr_spec *spec = &attr_uapi->spec; 213 struct uverbs_attr_bundle *bundle = 214 container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr); 215 struct uverbs_attr *e = &bundle->attrs[attr_bkey]; 216 const struct uverbs_attr_spec *val_spec = spec; 217 struct uverbs_obj_attr *o_attr; 218 219 switch (spec->type) { 220 case UVERBS_ATTR_TYPE_ENUM_IN: 221 if (uattr->attr_data.enum_data.elem_id >= spec->u.enum_def.num_elems) 222 return -EOPNOTSUPP; 223 224 if (uattr->attr_data.enum_data.reserved) 225 return -EINVAL; 226 227 val_spec = &spec->u2.enum_def.ids[uattr->attr_data.enum_data.elem_id]; 228 229 /* Currently we only support PTR_IN based enums */ 230 if (val_spec->type != UVERBS_ATTR_TYPE_PTR_IN) 231 return -EOPNOTSUPP; 232 233 e->ptr_attr.enum_id = uattr->attr_data.enum_data.elem_id; 234 fallthrough; 235 case UVERBS_ATTR_TYPE_PTR_IN: 236 /* Ensure that any data provided by userspace beyond the known 237 * struct is zero. Userspace that knows how to use some future 238 * longer struct will fail here if used with an old kernel and 239 * non-zero content, making ABI compat/discovery simpler. 240 */ 241 if (uattr->len > val_spec->u.ptr.len && 242 val_spec->zero_trailing && 243 !uverbs_is_attr_cleared(uattr, val_spec->u.ptr.len)) 244 return -EOPNOTSUPP; 245 246 fallthrough; 247 case UVERBS_ATTR_TYPE_PTR_OUT: 248 if (uattr->len < val_spec->u.ptr.min_len || 249 (!val_spec->zero_trailing && 250 uattr->len > val_spec->u.ptr.len)) 251 return -EINVAL; 252 253 if (spec->type != UVERBS_ATTR_TYPE_ENUM_IN && 254 uattr->attr_data.reserved) 255 return -EINVAL; 256 257 e->ptr_attr.uattr_idx = uattr - pbundle->uattrs; 258 e->ptr_attr.len = uattr->len; 259 260 if (val_spec->alloc_and_copy && !uverbs_attr_ptr_is_inline(e)) { 261 void *p; 262 263 p = uverbs_alloc(bundle, uattr->len); 264 if (IS_ERR(p)) 265 return PTR_ERR(p); 266 267 e->ptr_attr.ptr = p; 268 269 if (copy_from_user(p, u64_to_user_ptr(uattr->data), 270 uattr->len)) 271 return -EFAULT; 272 } else { 273 e->ptr_attr.data = uattr->data; 274 } 275 break; 276 277 case UVERBS_ATTR_TYPE_IDR: 278 case UVERBS_ATTR_TYPE_FD: 279 if (uattr->attr_data.reserved) 280 return -EINVAL; 281 282 if (uattr->len != 0) 283 return -EINVAL; 284 285 o_attr = &e->obj_attr; 286 o_attr->attr_elm = attr_uapi; 287 288 /* 289 * The type of uattr->data is u64 for UVERBS_ATTR_TYPE_IDR and 290 * s64 for UVERBS_ATTR_TYPE_FD. We can cast the u64 to s64 291 * here without caring about truncation as we know that the 292 * IDR implementation today rejects negative IDs 293 */ 294 o_attr->uobject = uverbs_get_uobject_from_file( 295 spec->u.obj.obj_type, spec->u.obj.access, 296 uattr->data_s64, bundle); 297 if (IS_ERR(o_attr->uobject)) 298 return PTR_ERR(o_attr->uobject); 299 __set_bit(attr_bkey, pbundle->uobj_finalize); 300 301 if (spec->u.obj.access == UVERBS_ACCESS_NEW) { 302 unsigned int uattr_idx = uattr - pbundle->uattrs; 303 s64 id = o_attr->uobject->id; 304 305 /* Copy the allocated id to the user-space */ 306 if (put_user(id, &pbundle->user_attrs[uattr_idx].data)) 307 return -EFAULT; 308 } 309 310 break; 311 312 case UVERBS_ATTR_TYPE_RAW_FD: 313 if (uattr->attr_data.reserved || uattr->len != 0 || 314 uattr->data_s64 < INT_MIN || uattr->data_s64 > INT_MAX) 315 return -EINVAL; 316 /* _uverbs_get_const_signed() is the accessor */ 317 e->ptr_attr.data = uattr->data_s64; 318 break; 319 320 case UVERBS_ATTR_TYPE_IDRS_ARRAY: 321 return uverbs_process_idrs_array(pbundle, attr_uapi, 322 &e->objs_arr_attr, uattr, 323 attr_bkey); 324 default: 325 return -EOPNOTSUPP; 326 } 327 328 return 0; 329 } 330 331 /* 332 * We search the radix tree with the method prefix and now we want to fast 333 * search the suffix bits to get a particular attribute pointer. It is not 334 * totally clear to me if this breaks the radix tree encasulation or not, but 335 * it uses the iter data to determine if the method iter points at the same 336 * chunk that will store the attribute, if so it just derefs it directly. By 337 * construction in most kernel configs the method and attrs will all fit in a 338 * single radix chunk, so in most cases this will have no search. Other cases 339 * this falls back to a full search. 340 */ 341 static void __rcu **uapi_get_attr_for_method(struct bundle_priv *pbundle, 342 u32 attr_key) 343 { 344 void __rcu **slot; 345 346 if (likely(attr_key < pbundle->radix_slots_len)) { 347 void *entry; 348 349 slot = pbundle->radix_slots + attr_key; 350 entry = rcu_dereference_raw(*slot); 351 if (likely(!radix_tree_is_internal_node(entry) && entry)) 352 return slot; 353 } 354 355 return radix_tree_lookup_slot(pbundle->radix, 356 pbundle->method_key | attr_key); 357 } 358 359 static int uverbs_set_attr(struct bundle_priv *pbundle, 360 struct ib_uverbs_attr *uattr) 361 { 362 u32 attr_key = uapi_key_attr(uattr->attr_id); 363 u32 attr_bkey = uapi_bkey_attr(attr_key); 364 const struct uverbs_api_attr *attr; 365 void __rcu **slot; 366 int ret; 367 368 slot = uapi_get_attr_for_method(pbundle, attr_key); 369 if (!slot) { 370 /* 371 * Kernel does not support the attribute but user-space says it 372 * is mandatory 373 */ 374 if (uattr->flags & UVERBS_ATTR_F_MANDATORY) 375 return -EPROTONOSUPPORT; 376 return 0; 377 } 378 attr = rcu_dereference_protected(*slot, true); 379 380 /* Reject duplicate attributes from user-space */ 381 if (test_bit(attr_bkey, pbundle->bundle.attr_present)) 382 return -EINVAL; 383 384 ret = uverbs_process_attr(pbundle, attr, uattr, attr_bkey); 385 if (ret) 386 return ret; 387 388 __set_bit(attr_bkey, pbundle->bundle.attr_present); 389 390 return 0; 391 } 392 393 static int ib_uverbs_run_method(struct bundle_priv *pbundle, 394 unsigned int num_attrs) 395 { 396 int (*handler)(struct uverbs_attr_bundle *attrs); 397 struct uverbs_attr_bundle *bundle = 398 container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr); 399 size_t uattrs_size = array_size(sizeof(*pbundle->uattrs), num_attrs); 400 unsigned int destroy_bkey = bundle->method_elm->destroy_bkey; 401 unsigned int i; 402 int ret; 403 404 /* See uverbs_disassociate_api() */ 405 handler = srcu_dereference( 406 bundle->method_elm->handler, 407 &pbundle->bundle.ufile->device->disassociate_srcu); 408 if (!handler) 409 return -EIO; 410 411 pbundle->uattrs = uverbs_alloc(bundle, uattrs_size); 412 if (IS_ERR(pbundle->uattrs)) 413 return PTR_ERR(pbundle->uattrs); 414 if (copy_from_user(pbundle->uattrs, pbundle->user_attrs, uattrs_size)) 415 return -EFAULT; 416 417 for (i = 0; i != num_attrs; i++) { 418 ret = uverbs_set_attr(pbundle, &pbundle->uattrs[i]); 419 if (unlikely(ret)) 420 return ret; 421 } 422 423 /* User space did not provide all the mandatory attributes */ 424 if (unlikely(!bitmap_subset(bundle->method_elm->attr_mandatory, 425 pbundle->bundle.attr_present, 426 bundle->method_elm->key_bitmap_len))) 427 return -EINVAL; 428 429 if (bundle->method_elm->has_udata) 430 uverbs_fill_udata(bundle, &pbundle->bundle.driver_udata, 431 UVERBS_ATTR_UHW_IN, UVERBS_ATTR_UHW_OUT); 432 else 433 pbundle->bundle.driver_udata = (struct ib_udata){}; 434 435 if (destroy_bkey != UVERBS_API_ATTR_BKEY_LEN) { 436 struct uverbs_obj_attr *destroy_attr = &bundle->attrs[destroy_bkey].obj_attr; 437 438 ret = uobj_destroy(destroy_attr->uobject, bundle); 439 if (ret) 440 return ret; 441 __clear_bit(destroy_bkey, pbundle->uobj_finalize); 442 443 ret = handler(bundle); 444 uobj_put_destroy(destroy_attr->uobject); 445 } else { 446 ret = handler(bundle); 447 } 448 449 /* 450 * Until the drivers are revised to use the bundle directly we have to 451 * assume that the driver wrote to its UHW_OUT and flag userspace 452 * appropriately. 453 */ 454 if (!ret && bundle->method_elm->has_udata) { 455 const struct uverbs_attr *attr = 456 uverbs_attr_get(bundle, UVERBS_ATTR_UHW_OUT); 457 458 if (!IS_ERR(attr)) 459 ret = uverbs_set_output(bundle, attr); 460 } 461 462 /* 463 * EPROTONOSUPPORT is ONLY to be returned if the ioctl framework can 464 * not invoke the method because the request is not supported. No 465 * other cases should return this code. 466 */ 467 if (WARN_ON_ONCE(ret == -EPROTONOSUPPORT)) 468 return -EINVAL; 469 470 return ret; 471 } 472 473 static void bundle_destroy(struct bundle_priv *pbundle, bool commit) 474 { 475 unsigned int key_bitmap_len = pbundle->bundle.method_elm->key_bitmap_len; 476 struct uverbs_attr_bundle *bundle = 477 container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr); 478 struct bundle_alloc_head *memblock; 479 unsigned int i; 480 481 /* fast path for simple uobjects */ 482 i = -1; 483 while ((i = find_next_bit(pbundle->uobj_finalize, key_bitmap_len, 484 i + 1)) < key_bitmap_len) { 485 struct uverbs_attr *attr = &bundle->attrs[i]; 486 487 uverbs_finalize_object( 488 attr->obj_attr.uobject, 489 attr->obj_attr.attr_elm->spec.u.obj.access, 490 test_bit(i, pbundle->uobj_hw_obj_valid), 491 commit, bundle); 492 } 493 494 i = -1; 495 while ((i = find_next_bit(pbundle->spec_finalize, key_bitmap_len, 496 i + 1)) < key_bitmap_len) { 497 struct uverbs_attr *attr = &bundle->attrs[i]; 498 const struct uverbs_api_attr *attr_uapi; 499 void __rcu **slot; 500 501 slot = uapi_get_attr_for_method( 502 pbundle, 503 pbundle->method_key | uapi_bkey_to_key_attr(i)); 504 if (WARN_ON(!slot)) 505 continue; 506 507 attr_uapi = rcu_dereference_protected(*slot, true); 508 509 if (attr_uapi->spec.type == UVERBS_ATTR_TYPE_IDRS_ARRAY) { 510 uverbs_free_idrs_array(attr_uapi, &attr->objs_arr_attr, 511 commit, bundle); 512 } 513 } 514 515 for (memblock = pbundle->allocated_mem; memblock;) { 516 struct bundle_alloc_head *tmp = memblock; 517 518 memblock = memblock->next; 519 kvfree(tmp); 520 } 521 } 522 523 static int ib_uverbs_cmd_verbs(struct ib_uverbs_file *ufile, 524 struct ib_uverbs_ioctl_hdr *hdr, 525 struct ib_uverbs_attr __user *user_attrs) 526 { 527 const struct uverbs_api_ioctl_method *method_elm; 528 struct uverbs_api *uapi = ufile->device->uapi; 529 struct radix_tree_iter attrs_iter; 530 struct bundle_priv *pbundle; 531 struct bundle_priv onstack; 532 void __rcu **slot; 533 int ret; 534 535 if (unlikely(hdr->driver_id != uapi->driver_id)) 536 return -EINVAL; 537 538 slot = radix_tree_iter_lookup( 539 &uapi->radix, &attrs_iter, 540 uapi_key_obj(hdr->object_id) | 541 uapi_key_ioctl_method(hdr->method_id)); 542 if (unlikely(!slot)) 543 return -EPROTONOSUPPORT; 544 method_elm = rcu_dereference_protected(*slot, true); 545 546 if (!method_elm->use_stack) { 547 pbundle = kmalloc(method_elm->bundle_size, GFP_KERNEL); 548 if (!pbundle) 549 return -ENOMEM; 550 pbundle->internal_avail = 551 method_elm->bundle_size - 552 offsetof(struct bundle_priv, internal_buffer); 553 pbundle->alloc_head.next = NULL; 554 pbundle->allocated_mem = container_of(&pbundle->alloc_head, 555 struct bundle_alloc_head, hdr); 556 } else { 557 pbundle = &onstack; 558 pbundle->internal_avail = sizeof(pbundle->internal_buffer); 559 pbundle->allocated_mem = NULL; 560 } 561 562 /* Space for the pbundle->bundle.attrs flex array */ 563 pbundle->bundle.method_elm = method_elm; 564 pbundle->method_key = attrs_iter.index; 565 pbundle->bundle.ufile = ufile; 566 pbundle->bundle.context = NULL; /* only valid if bundle has uobject */ 567 pbundle->radix = &uapi->radix; 568 pbundle->radix_slots = slot; 569 pbundle->radix_slots_len = radix_tree_chunk_size(&attrs_iter); 570 pbundle->user_attrs = user_attrs; 571 572 pbundle->internal_used = ALIGN( 573 pbundle->bundle.method_elm->key_bitmap_len * 574 sizeof(*container_of(&pbundle->bundle, 575 struct uverbs_attr_bundle, hdr) 576 ->attrs), 577 sizeof(*pbundle->internal_buffer)); 578 memset(pbundle->bundle.attr_present, 0, 579 sizeof(pbundle->bundle.attr_present)); 580 memset(pbundle->uobj_finalize, 0, sizeof(pbundle->uobj_finalize)); 581 memset(pbundle->spec_finalize, 0, sizeof(pbundle->spec_finalize)); 582 memset(pbundle->uobj_hw_obj_valid, 0, 583 sizeof(pbundle->uobj_hw_obj_valid)); 584 585 ret = ib_uverbs_run_method(pbundle, hdr->num_attrs); 586 bundle_destroy(pbundle, ret == 0); 587 return ret; 588 } 589 590 long ib_uverbs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 591 { 592 struct ib_uverbs_file *file = filp->private_data; 593 struct ib_uverbs_ioctl_hdr __user *user_hdr = 594 (struct ib_uverbs_ioctl_hdr __user *)arg; 595 struct ib_uverbs_ioctl_hdr hdr; 596 int srcu_key; 597 int err; 598 599 if (unlikely(cmd != RDMA_VERBS_IOCTL)) 600 return -ENOIOCTLCMD; 601 602 err = copy_from_user(&hdr, user_hdr, sizeof(hdr)); 603 if (err) 604 return -EFAULT; 605 606 if (hdr.length > PAGE_SIZE || 607 hdr.length != struct_size(&hdr, attrs, hdr.num_attrs)) 608 return -EINVAL; 609 610 if (hdr.reserved1 || hdr.reserved2) 611 return -EPROTONOSUPPORT; 612 613 srcu_key = srcu_read_lock(&file->device->disassociate_srcu); 614 err = ib_uverbs_cmd_verbs(file, &hdr, user_hdr->attrs); 615 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key); 616 return err; 617 } 618 619 int uverbs_get_flags64(u64 *to, const struct uverbs_attr_bundle *attrs_bundle, 620 size_t idx, u64 allowed_bits) 621 { 622 const struct uverbs_attr *attr; 623 u64 flags; 624 625 attr = uverbs_attr_get(attrs_bundle, idx); 626 /* Missing attribute means 0 flags */ 627 if (IS_ERR(attr)) { 628 *to = 0; 629 return 0; 630 } 631 632 /* 633 * New userspace code should use 8 bytes to pass flags, but we 634 * transparently support old userspaces that were using 4 bytes as 635 * well. 636 */ 637 if (attr->ptr_attr.len == 8) 638 flags = attr->ptr_attr.data; 639 else if (attr->ptr_attr.len == 4) 640 flags = *(u32 *)&attr->ptr_attr.data; 641 else 642 return -EINVAL; 643 644 if (flags & ~allowed_bits) 645 return -EINVAL; 646 647 *to = flags; 648 return 0; 649 } 650 EXPORT_SYMBOL(uverbs_get_flags64); 651 652 int uverbs_get_flags32(u32 *to, const struct uverbs_attr_bundle *attrs_bundle, 653 size_t idx, u64 allowed_bits) 654 { 655 u64 flags; 656 int ret; 657 658 ret = uverbs_get_flags64(&flags, attrs_bundle, idx, allowed_bits); 659 if (ret) 660 return ret; 661 662 if (flags > U32_MAX) 663 return -EINVAL; 664 *to = flags; 665 666 return 0; 667 } 668 EXPORT_SYMBOL(uverbs_get_flags32); 669 670 /* 671 * Fill a ib_udata struct (core or uhw) using the given attribute IDs. 672 * This is primarily used to convert the UVERBS_ATTR_UHW() into the 673 * ib_udata format used by the drivers. 674 */ 675 void uverbs_fill_udata(struct uverbs_attr_bundle *bundle, 676 struct ib_udata *udata, unsigned int attr_in, 677 unsigned int attr_out) 678 { 679 struct bundle_priv *pbundle = 680 container_of(&bundle->hdr, struct bundle_priv, bundle); 681 struct uverbs_attr_bundle *bundle_aux = 682 container_of(&pbundle->bundle, struct uverbs_attr_bundle, hdr); 683 const struct uverbs_attr *in = 684 uverbs_attr_get(bundle_aux, attr_in); 685 const struct uverbs_attr *out = 686 uverbs_attr_get(bundle_aux, attr_out); 687 688 if (!IS_ERR(in)) { 689 udata->inlen = in->ptr_attr.len; 690 if (uverbs_attr_ptr_is_inline(in)) 691 udata->inbuf = 692 &pbundle->user_attrs[in->ptr_attr.uattr_idx] 693 .data; 694 else 695 udata->inbuf = u64_to_user_ptr(in->ptr_attr.data); 696 } else { 697 udata->inbuf = NULL; 698 udata->inlen = 0; 699 } 700 701 if (!IS_ERR(out)) { 702 udata->outbuf = u64_to_user_ptr(out->ptr_attr.data); 703 udata->outlen = out->ptr_attr.len; 704 } else { 705 udata->outbuf = NULL; 706 udata->outlen = 0; 707 } 708 } 709 710 int uverbs_copy_to(const struct uverbs_attr_bundle *bundle, size_t idx, 711 const void *from, size_t size) 712 { 713 const struct uverbs_attr *attr = uverbs_attr_get(bundle, idx); 714 size_t min_size; 715 716 if (IS_ERR(attr)) 717 return PTR_ERR(attr); 718 719 min_size = min_t(size_t, attr->ptr_attr.len, size); 720 if (copy_to_user(u64_to_user_ptr(attr->ptr_attr.data), from, min_size)) 721 return -EFAULT; 722 723 return uverbs_set_output(bundle, attr); 724 } 725 EXPORT_SYMBOL(uverbs_copy_to); 726 727 728 /* 729 * This is only used if the caller has directly used copy_to_use to write the 730 * data. It signals to user space that the buffer is filled in. 731 */ 732 int uverbs_output_written(const struct uverbs_attr_bundle *bundle, size_t idx) 733 { 734 const struct uverbs_attr *attr = uverbs_attr_get(bundle, idx); 735 736 if (IS_ERR(attr)) 737 return PTR_ERR(attr); 738 739 return uverbs_set_output(bundle, attr); 740 } 741 742 int _uverbs_get_const_signed(s64 *to, 743 const struct uverbs_attr_bundle *attrs_bundle, 744 size_t idx, s64 lower_bound, u64 upper_bound, 745 s64 *def_val) 746 { 747 const struct uverbs_attr *attr; 748 749 attr = uverbs_attr_get(attrs_bundle, idx); 750 if (IS_ERR(attr)) { 751 if ((PTR_ERR(attr) != -ENOENT) || !def_val) 752 return PTR_ERR(attr); 753 754 *to = *def_val; 755 } else { 756 *to = attr->ptr_attr.data; 757 } 758 759 if (*to < lower_bound || (*to > 0 && (u64)*to > upper_bound)) 760 return -EINVAL; 761 762 return 0; 763 } 764 EXPORT_SYMBOL(_uverbs_get_const_signed); 765 766 int _uverbs_get_const_unsigned(u64 *to, 767 const struct uverbs_attr_bundle *attrs_bundle, 768 size_t idx, u64 upper_bound, u64 *def_val) 769 { 770 const struct uverbs_attr *attr; 771 772 attr = uverbs_attr_get(attrs_bundle, idx); 773 if (IS_ERR(attr)) { 774 if ((PTR_ERR(attr) != -ENOENT) || !def_val) 775 return PTR_ERR(attr); 776 777 *to = *def_val; 778 } else { 779 *to = attr->ptr_attr.data; 780 } 781 782 if (*to > upper_bound) 783 return -EINVAL; 784 785 return 0; 786 } 787 EXPORT_SYMBOL(_uverbs_get_const_unsigned); 788 789 int uverbs_copy_to_struct_or_zero(const struct uverbs_attr_bundle *bundle, 790 size_t idx, const void *from, size_t size) 791 { 792 const struct uverbs_attr *attr = uverbs_attr_get(bundle, idx); 793 794 if (IS_ERR(attr)) 795 return PTR_ERR(attr); 796 797 if (size < attr->ptr_attr.len) { 798 if (clear_user(u64_to_user_ptr(attr->ptr_attr.data) + size, 799 attr->ptr_attr.len - size)) 800 return -EFAULT; 801 } 802 return uverbs_copy_to(bundle, idx, from, size); 803 } 804 EXPORT_SYMBOL(uverbs_copy_to_struct_or_zero); 805 806 /* Once called an abort will call through to the type's destroy_hw() */ 807 void uverbs_finalize_uobj_create(const struct uverbs_attr_bundle *bundle, 808 u16 idx) 809 { 810 struct bundle_priv *pbundle = 811 container_of(&bundle->hdr, struct bundle_priv, bundle); 812 813 __set_bit(uapi_bkey_attr(uapi_key_attr(idx)), 814 pbundle->uobj_hw_obj_valid); 815 } 816 EXPORT_SYMBOL(uverbs_finalize_uobj_create); 817