1 /*
2 * Copyright (c) 2016, 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 <linux/file.h>
34 #include <linux/anon_inodes.h>
35 #include <linux/sched/mm.h>
36 #include <rdma/ib_verbs.h>
37 #include <rdma/uverbs_types.h>
38 #include <linux/rcupdate.h>
39 #include <rdma/uverbs_ioctl.h>
40 #include <rdma/rdma_user_ioctl.h>
41 #include "uverbs.h"
42 #include "core_priv.h"
43 #include "rdma_core.h"
44
45 static void release_ufile_idr_uobject(struct ib_uverbs_file *ufile);
46
ib_uverbs_release_file(struct kref * ref)47 void ib_uverbs_release_file(struct kref *ref)
48 {
49 struct ib_uverbs_file *file =
50 container_of(ref, struct ib_uverbs_file, ref);
51 struct ib_device *ib_dev;
52 int srcu_key;
53
54 release_ufile_idr_uobject(file);
55
56 srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
57 ib_dev = srcu_dereference(file->device->ib_dev,
58 &file->device->disassociate_srcu);
59 if (ib_dev && !ib_dev->ops.disassociate_ucontext)
60 module_put(ib_dev->ops.owner);
61 srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
62
63 if (refcount_dec_and_test(&file->device->refcount))
64 ib_uverbs_comp_dev(file->device);
65
66 if (file->default_async_file)
67 uverbs_uobject_put(&file->default_async_file->uobj);
68 put_device(&file->device->dev);
69
70 if (file->disassociate_page)
71 __free_pages(file->disassociate_page, 0);
72 mutex_destroy(&file->umap_lock);
73 mutex_destroy(&file->ucontext_lock);
74 kfree(file);
75 }
76 EXPORT_SYMBOL_NS_GPL(ib_uverbs_release_file, "rdma_core");
77
uverbs_uobject_free(struct kref * ref)78 static void uverbs_uobject_free(struct kref *ref)
79 {
80 kfree_rcu(container_of(ref, struct ib_uobject, ref), rcu);
81 }
82
83 /*
84 * In order to indicate we no longer needs this uobject, uverbs_uobject_put
85 * is called. When the reference count is decreased, the uobject is freed.
86 * For example, this is used when attaching a completion channel to a CQ.
87 */
uverbs_uobject_put(struct ib_uobject * uobject)88 void uverbs_uobject_put(struct ib_uobject *uobject)
89 {
90 kref_put(&uobject->ref, uverbs_uobject_free);
91 }
92 EXPORT_SYMBOL(uverbs_uobject_put);
93
uverbs_try_lock_object(struct ib_uobject * uobj,enum rdma_lookup_mode mode)94 int uverbs_try_lock_object(struct ib_uobject *uobj,
95 enum rdma_lookup_mode mode)
96 {
97 /*
98 * When a shared access is required, we use a positive counter. Each
99 * shared access request checks that the value != -1 and increment it.
100 * Exclusive access is required for operations like write or destroy.
101 * In exclusive access mode, we check that the counter is zero (nobody
102 * claimed this object) and we set it to -1. Releasing a shared access
103 * lock is done simply by decreasing the counter. As for exclusive
104 * access locks, since only a single one of them is allowed
105 * concurrently, setting the counter to zero is enough for releasing
106 * this lock.
107 */
108 switch (mode) {
109 case UVERBS_LOOKUP_READ:
110 return atomic_fetch_add_unless(&uobj->usecnt, 1, -1) == -1 ?
111 -EBUSY : 0;
112 case UVERBS_LOOKUP_WRITE:
113 /* lock is exclusive */
114 return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
115 case UVERBS_LOOKUP_DESTROY:
116 return 0;
117 }
118 return 0;
119 }
120 EXPORT_SYMBOL(uverbs_try_lock_object);
121
assert_uverbs_usecnt(struct ib_uobject * uobj,enum rdma_lookup_mode mode)122 static void assert_uverbs_usecnt(struct ib_uobject *uobj,
123 enum rdma_lookup_mode mode)
124 {
125 #ifdef CONFIG_LOCKDEP
126 switch (mode) {
127 case UVERBS_LOOKUP_READ:
128 WARN_ON(atomic_read(&uobj->usecnt) <= 0);
129 break;
130 case UVERBS_LOOKUP_WRITE:
131 WARN_ON(atomic_read(&uobj->usecnt) != -1);
132 break;
133 case UVERBS_LOOKUP_DESTROY:
134 break;
135 }
136 #endif
137 }
138
139 /*
140 * This must be called with the hw_destroy_rwsem locked for read or write,
141 * also the uobject itself must be locked for write.
142 *
143 * Upon return the HW object is guaranteed to be destroyed.
144 *
145 * For RDMA_REMOVE_ABORT, the hw_destroy_rwsem is not required to be held,
146 * however the type's allocat_commit function cannot have been called and the
147 * uobject cannot be on the uobjects_lists
148 *
149 * For RDMA_REMOVE_DESTROY the caller should be holding a kref (eg via
150 * rdma_lookup_get_uobject) and the object is left in a state where the caller
151 * needs to call rdma_lookup_put_uobject.
152 *
153 * For all other destroy modes this function internally unlocks the uobject
154 * and consumes the kref on the uobj.
155 */
uverbs_destroy_uobject(struct ib_uobject * uobj,enum rdma_remove_reason reason,struct uverbs_attr_bundle * attrs)156 static int uverbs_destroy_uobject(struct ib_uobject *uobj,
157 enum rdma_remove_reason reason,
158 struct uverbs_attr_bundle *attrs)
159 {
160 struct ib_uverbs_file *ufile = attrs->ufile;
161 unsigned long flags;
162 int ret;
163
164 lockdep_assert_held(&ufile->hw_destroy_rwsem);
165 assert_uverbs_usecnt(uobj, UVERBS_LOOKUP_WRITE);
166
167 if (reason == RDMA_REMOVE_ABORT) {
168 WARN_ON(!list_empty(&uobj->list));
169 WARN_ON(!uobj->context);
170 uobj->uapi_object->type_class->alloc_abort(uobj);
171 } else if (uobj->object) {
172 ret = uobj->uapi_object->type_class->destroy_hw(uobj, reason,
173 attrs);
174 if (ret)
175 /* Nothing to be done, wait till ucontext will clean it */
176 return ret;
177
178 uobj->object = NULL;
179 }
180
181 uobj->context = NULL;
182
183 /*
184 * For DESTROY the usecnt is not changed, the caller is expected to
185 * manage it via uobj_put_destroy(). Only DESTROY can remove the IDR
186 * handle.
187 */
188 if (reason != RDMA_REMOVE_DESTROY)
189 atomic_set(&uobj->usecnt, 0);
190 else
191 uobj->uapi_object->type_class->remove_handle(uobj);
192
193 if (!list_empty(&uobj->list)) {
194 spin_lock_irqsave(&ufile->uobjects_lock, flags);
195 list_del_init(&uobj->list);
196 spin_unlock_irqrestore(&ufile->uobjects_lock, flags);
197
198 /*
199 * Pairs with the get in rdma_alloc_commit_uobject(), could
200 * destroy uobj.
201 */
202 uverbs_uobject_put(uobj);
203 }
204
205 /*
206 * When aborting the stack kref remains owned by the core code, and is
207 * not transferred into the type. Pairs with the get in alloc_uobj
208 */
209 if (reason == RDMA_REMOVE_ABORT)
210 uverbs_uobject_put(uobj);
211
212 return 0;
213 }
214
215 /*
216 * This calls uverbs_destroy_uobject() using the RDMA_REMOVE_DESTROY
217 * sequence. It should only be used from command callbacks. On success the
218 * caller must pair this with uobj_put_destroy(). This
219 * version requires the caller to have already obtained an
220 * LOOKUP_DESTROY uobject kref.
221 */
uobj_destroy(struct ib_uobject * uobj,struct uverbs_attr_bundle * attrs)222 int uobj_destroy(struct ib_uobject *uobj, struct uverbs_attr_bundle *attrs)
223 {
224 struct ib_uverbs_file *ufile = attrs->ufile;
225 int ret;
226
227 down_read(&ufile->hw_destroy_rwsem);
228
229 /*
230 * Once the uobject is destroyed by RDMA_REMOVE_DESTROY then it is left
231 * write locked as the callers put it back with UVERBS_LOOKUP_DESTROY.
232 * This is because any other concurrent thread can still see the object
233 * in the xarray due to RCU. Leaving it locked ensures nothing else will
234 * touch it.
235 */
236 ret = uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE);
237 if (ret)
238 goto out_unlock;
239
240 ret = uverbs_destroy_uobject(uobj, RDMA_REMOVE_DESTROY, attrs);
241 if (ret) {
242 atomic_set(&uobj->usecnt, 0);
243 goto out_unlock;
244 }
245
246 out_unlock:
247 up_read(&ufile->hw_destroy_rwsem);
248 return ret;
249 }
250 EXPORT_SYMBOL_NS_GPL(uobj_destroy, "rdma_core");
251
252 /*
253 * uobj_get_destroy destroys the HW object and returns a handle to the uobj
254 * with a NULL object pointer. The caller must pair this with
255 * uobj_put_destroy().
256 */
__uobj_get_destroy(const struct uverbs_api_object * obj,u32 id,struct uverbs_attr_bundle * attrs)257 struct ib_uobject *__uobj_get_destroy(const struct uverbs_api_object *obj,
258 u32 id, struct uverbs_attr_bundle *attrs)
259 {
260 struct ib_uobject *uobj;
261 int ret;
262
263 uobj = rdma_lookup_get_uobject(obj, attrs->ufile, id,
264 UVERBS_LOOKUP_DESTROY, attrs);
265 if (IS_ERR(uobj))
266 return uobj;
267
268 ret = uobj_destroy(uobj, attrs);
269 if (ret) {
270 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
271 return ERR_PTR(ret);
272 }
273
274 return uobj;
275 }
276 EXPORT_SYMBOL_NS_GPL(__uobj_get_destroy, "rdma_core");
277
278 /*
279 * Does both uobj_get_destroy() and uobj_put_destroy(). Returns 0 on success
280 * (negative errno on failure). For use by callers that do not need the uobj.
281 */
__uobj_perform_destroy(const struct uverbs_api_object * obj,u32 id,struct uverbs_attr_bundle * attrs)282 int __uobj_perform_destroy(const struct uverbs_api_object *obj, u32 id,
283 struct uverbs_attr_bundle *attrs)
284 {
285 struct ib_uobject *uobj;
286
287 uobj = __uobj_get_destroy(obj, id, attrs);
288 if (IS_ERR(uobj))
289 return PTR_ERR(uobj);
290 uobj_put_destroy(uobj);
291 return 0;
292 }
293 EXPORT_SYMBOL_NS_GPL(__uobj_perform_destroy, "rdma_core");
294
295 /* alloc_uobj must be undone by uverbs_destroy_uobject() */
alloc_uobj(struct uverbs_attr_bundle * attrs,const struct uverbs_api_object * obj)296 static struct ib_uobject *alloc_uobj(struct uverbs_attr_bundle *attrs,
297 const struct uverbs_api_object *obj)
298 {
299 struct ib_uverbs_file *ufile = attrs->ufile;
300 struct ib_uobject *uobj;
301
302 if (!attrs->context) {
303 struct ib_ucontext *ucontext =
304 ib_uverbs_get_ucontext_file(ufile);
305
306 if (IS_ERR(ucontext))
307 return ERR_CAST(ucontext);
308 attrs->context = ucontext;
309 }
310
311 uobj = kzalloc(obj->type_attrs->obj_size, GFP_KERNEL);
312 if (!uobj)
313 return ERR_PTR(-ENOMEM);
314 /*
315 * user_handle should be filled by the handler,
316 * The object is added to the list in the commit stage.
317 */
318 uobj->ufile = ufile;
319 uobj->context = attrs->context;
320 INIT_LIST_HEAD(&uobj->list);
321 uobj->uapi_object = obj;
322 /*
323 * Allocated objects start out as write locked to deny any other
324 * syscalls from accessing them until they are committed. See
325 * rdma_alloc_commit_uobject
326 */
327 atomic_set(&uobj->usecnt, -1);
328 kref_init(&uobj->ref);
329
330 return uobj;
331 }
332
idr_add_uobj(struct ib_uobject * uobj)333 static int idr_add_uobj(struct ib_uobject *uobj)
334 {
335 /*
336 * We start with allocating an idr pointing to NULL. This represents an
337 * object which isn't initialized yet. We'll replace it later on with
338 * the real object once we commit.
339 */
340 return xa_alloc(&uobj->ufile->idr, &uobj->id, NULL, xa_limit_32b,
341 GFP_KERNEL);
342 }
343
344 /* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
345 static struct ib_uobject *
lookup_get_idr_uobject(const struct uverbs_api_object * obj,struct ib_uverbs_file * ufile,s64 id,enum rdma_lookup_mode mode)346 lookup_get_idr_uobject(const struct uverbs_api_object *obj,
347 struct ib_uverbs_file *ufile, s64 id,
348 enum rdma_lookup_mode mode)
349 {
350 struct ib_uobject *uobj;
351
352 if (id < 0 || id > ULONG_MAX)
353 return ERR_PTR(-EINVAL);
354
355 rcu_read_lock();
356 /*
357 * The idr_find is guaranteed to return a pointer to something that
358 * isn't freed yet, or NULL, as the free after idr_remove goes through
359 * kfree_rcu(). However the object may still have been released and
360 * kfree() could be called at any time.
361 */
362 uobj = xa_load(&ufile->idr, id);
363 if (!uobj || !kref_get_unless_zero(&uobj->ref))
364 uobj = ERR_PTR(-ENOENT);
365 rcu_read_unlock();
366 return uobj;
367 }
368
369 static struct ib_uobject *
lookup_get_fd_uobject(const struct uverbs_api_object * obj,struct ib_uverbs_file * ufile,s64 id,enum rdma_lookup_mode mode)370 lookup_get_fd_uobject(const struct uverbs_api_object *obj,
371 struct ib_uverbs_file *ufile, s64 id,
372 enum rdma_lookup_mode mode)
373 {
374 const struct uverbs_obj_fd_type *fd_type;
375 struct file *f;
376 struct ib_uobject *uobject;
377 int fdno = id;
378
379 if (fdno != id)
380 return ERR_PTR(-EINVAL);
381
382 if (mode != UVERBS_LOOKUP_READ)
383 return ERR_PTR(-EOPNOTSUPP);
384
385 if (!obj->type_attrs)
386 return ERR_PTR(-EIO);
387 fd_type =
388 container_of(obj->type_attrs, struct uverbs_obj_fd_type, type);
389
390 f = fget(fdno);
391 if (!f)
392 return ERR_PTR(-EBADF);
393
394 uobject = f->private_data;
395 /*
396 * fget(id) ensures we are not currently running
397 * uverbs_uobject_fd_release(), and the caller is expected to ensure
398 * that release is never done while a call to lookup is possible.
399 */
400 if (f->f_op != fd_type->fops || uobject->ufile != ufile) {
401 fput(f);
402 return ERR_PTR(-EBADF);
403 }
404
405 uverbs_uobject_get(uobject);
406 return uobject;
407 }
408
rdma_lookup_get_uobject(const struct uverbs_api_object * obj,struct ib_uverbs_file * ufile,s64 id,enum rdma_lookup_mode mode,struct uverbs_attr_bundle * attrs)409 struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_api_object *obj,
410 struct ib_uverbs_file *ufile, s64 id,
411 enum rdma_lookup_mode mode,
412 struct uverbs_attr_bundle *attrs)
413 {
414 struct ib_uobject *uobj;
415 int ret;
416
417 if (obj == ERR_PTR(-ENOMSG)) {
418 /* must be UVERBS_IDR_ANY_OBJECT, see uapi_get_object() */
419 uobj = lookup_get_idr_uobject(NULL, ufile, id, mode);
420 if (IS_ERR(uobj))
421 return uobj;
422 } else {
423 if (IS_ERR(obj))
424 return ERR_PTR(-EINVAL);
425
426 uobj = obj->type_class->lookup_get(obj, ufile, id, mode);
427 if (IS_ERR(uobj))
428 return uobj;
429
430 if (uobj->uapi_object != obj) {
431 ret = -EINVAL;
432 goto free;
433 }
434 }
435
436 /*
437 * If we have been disassociated block every command except for
438 * DESTROY based commands.
439 */
440 if (mode != UVERBS_LOOKUP_DESTROY &&
441 !srcu_dereference(ufile->device->ib_dev,
442 &ufile->device->disassociate_srcu)) {
443 ret = -EIO;
444 goto free;
445 }
446
447 ret = uverbs_try_lock_object(uobj, mode);
448 if (ret)
449 goto free;
450 if (attrs)
451 attrs->context = uobj->context;
452
453 return uobj;
454 free:
455 uobj->uapi_object->type_class->lookup_put(uobj, mode);
456 uverbs_uobject_put(uobj);
457 return ERR_PTR(ret);
458 }
459 EXPORT_SYMBOL_NS_GPL(rdma_lookup_get_uobject, "rdma_core");
460
461 static struct ib_uobject *
alloc_begin_idr_uobject(const struct uverbs_api_object * obj,struct uverbs_attr_bundle * attrs)462 alloc_begin_idr_uobject(const struct uverbs_api_object *obj,
463 struct uverbs_attr_bundle *attrs)
464 {
465 int ret;
466 struct ib_uobject *uobj;
467
468 uobj = alloc_uobj(attrs, obj);
469 if (IS_ERR(uobj))
470 return uobj;
471
472 ret = idr_add_uobj(uobj);
473 if (ret)
474 goto uobj_put;
475
476 ret = ib_rdmacg_try_charge(&uobj->cg_obj, uobj->context->device,
477 RDMACG_RESOURCE_HCA_OBJECT);
478 if (ret)
479 goto remove;
480
481 return uobj;
482
483 remove:
484 xa_erase(&attrs->ufile->idr, uobj->id);
485 uobj_put:
486 uverbs_uobject_put(uobj);
487 return ERR_PTR(ret);
488 }
489
490 static struct ib_uobject *
alloc_begin_fd_uobject(const struct uverbs_api_object * obj,struct uverbs_attr_bundle * attrs)491 alloc_begin_fd_uobject(const struct uverbs_api_object *obj,
492 struct uverbs_attr_bundle *attrs)
493 {
494 const struct uverbs_obj_fd_type *fd_type;
495 int new_fd;
496 struct ib_uobject *uobj, *ret;
497 struct file *filp;
498
499 uobj = alloc_uobj(attrs, obj);
500 if (IS_ERR(uobj))
501 return uobj;
502
503 fd_type =
504 container_of(obj->type_attrs, struct uverbs_obj_fd_type, type);
505 if (WARN_ON(fd_type->fops &&
506 fd_type->fops->release != &uverbs_uobject_fd_release)) {
507 ret = ERR_PTR(-EINVAL);
508 goto err_fd;
509 }
510
511 new_fd = get_unused_fd_flags(O_CLOEXEC);
512 if (new_fd < 0) {
513 ret = ERR_PTR(new_fd);
514 goto err_fd;
515 }
516
517 if (fd_type->fops) {
518 /* Note that uverbs_uobject_fd_release() is called during abort */
519 filp = anon_inode_getfile(fd_type->name, fd_type->fops, NULL,
520 fd_type->flags);
521 if (IS_ERR(filp)) {
522 ret = ERR_CAST(filp);
523 goto err_getfile;
524 }
525 uobj->object = filp;
526 }
527
528 uobj->id = new_fd;
529 return uobj;
530
531 err_getfile:
532 put_unused_fd(new_fd);
533 err_fd:
534 uverbs_uobject_put(uobj);
535 return ret;
536 }
537
rdma_alloc_begin_uobject(const struct uverbs_api_object * obj,struct uverbs_attr_bundle * attrs)538 struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
539 struct uverbs_attr_bundle *attrs)
540 {
541 struct ib_uverbs_file *ufile = attrs->ufile;
542 struct ib_uobject *ret;
543
544 if (IS_ERR(obj))
545 return ERR_PTR(-EINVAL);
546
547 /*
548 * The hw_destroy_rwsem is held across the entire object creation and
549 * released during rdma_alloc_commit_uobject or
550 * rdma_alloc_abort_uobject
551 */
552 if (!down_read_trylock(&ufile->hw_destroy_rwsem))
553 return ERR_PTR(-EIO);
554
555 ret = obj->type_class->alloc_begin(obj, attrs);
556 if (IS_ERR(ret)) {
557 up_read(&ufile->hw_destroy_rwsem);
558 return ret;
559 }
560 return ret;
561 }
562 EXPORT_SYMBOL_NS_GPL(rdma_alloc_begin_uobject, "rdma_core");
563
alloc_abort_idr_uobject(struct ib_uobject * uobj)564 static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
565 {
566 ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
567 RDMACG_RESOURCE_HCA_OBJECT);
568
569 xa_erase(&uobj->ufile->idr, uobj->id);
570 }
571
destroy_hw_idr_uobject(struct ib_uobject * uobj,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)572 static int __must_check destroy_hw_idr_uobject(struct ib_uobject *uobj,
573 enum rdma_remove_reason why,
574 struct uverbs_attr_bundle *attrs)
575 {
576 const struct uverbs_obj_idr_type *idr_type =
577 container_of(uobj->uapi_object->type_attrs,
578 struct uverbs_obj_idr_type, type);
579 int ret = idr_type->destroy_object(uobj, why, attrs);
580
581 if (ret)
582 return ret;
583
584 if (why == RDMA_REMOVE_ABORT)
585 return 0;
586
587 ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
588 RDMACG_RESOURCE_HCA_OBJECT);
589
590 return 0;
591 }
592
remove_handle_idr_uobject(struct ib_uobject * uobj)593 static void remove_handle_idr_uobject(struct ib_uobject *uobj)
594 {
595 xa_erase(&uobj->ufile->idr, uobj->id);
596 /* Matches the kref in alloc_commit_idr_uobject */
597 uverbs_uobject_put(uobj);
598 }
599
alloc_abort_fd_uobject(struct ib_uobject * uobj)600 static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
601 {
602 struct file *filp = uobj->object;
603
604 if (filp)
605 fput(filp);
606
607 put_unused_fd(uobj->id);
608 }
609
destroy_hw_fd_uobject(struct ib_uobject * uobj,enum rdma_remove_reason why,struct uverbs_attr_bundle * attrs)610 static int __must_check destroy_hw_fd_uobject(struct ib_uobject *uobj,
611 enum rdma_remove_reason why,
612 struct uverbs_attr_bundle *attrs)
613 {
614 const struct uverbs_obj_fd_type *fd_type = container_of(
615 uobj->uapi_object->type_attrs, struct uverbs_obj_fd_type, type);
616
617 fd_type->destroy_object(uobj, why);
618 return 0;
619 }
620
remove_handle_fd_uobject(struct ib_uobject * uobj)621 static void remove_handle_fd_uobject(struct ib_uobject *uobj)
622 {
623 }
624
alloc_commit_idr_uobject(struct ib_uobject * uobj)625 static void alloc_commit_idr_uobject(struct ib_uobject *uobj)
626 {
627 struct ib_uverbs_file *ufile = uobj->ufile;
628 void *old;
629
630 /*
631 * We already allocated this XArray entry with a NULL pointer, so
632 * this shouldn't fail.
633 *
634 * NOTE: Storing the uobj transfers our kref on uobj to the XArray.
635 * It will be put by remove_handle_idr_uobject()
636 */
637 old = xa_store(&ufile->idr, uobj->id, uobj, GFP_KERNEL);
638 WARN_ON(old != NULL);
639 }
640
swap_idr_uobjects(struct ib_uobject * obj_old,struct ib_uobject * obj_new)641 static void swap_idr_uobjects(struct ib_uobject *obj_old,
642 struct ib_uobject *obj_new)
643 {
644 struct ib_uverbs_file *ufile = obj_old->ufile;
645 void *old;
646
647 /*
648 * New must be an object that been allocated but not yet committed, this
649 * moves the pre-committed state to obj_old, new still must be comitted.
650 */
651 old = xa_cmpxchg(&ufile->idr, obj_old->id, obj_old, XA_ZERO_ENTRY,
652 GFP_KERNEL);
653 if (WARN_ON(old != obj_old))
654 return;
655
656 swap(obj_old->id, obj_new->id);
657
658 old = xa_cmpxchg(&ufile->idr, obj_old->id, NULL, obj_old, GFP_KERNEL);
659 WARN_ON(old != NULL);
660 }
661
alloc_commit_fd_uobject(struct ib_uobject * uobj)662 static void alloc_commit_fd_uobject(struct ib_uobject *uobj)
663 {
664 int fd = uobj->id;
665 struct file *filp = uobj->object;
666
667 /* Matching put will be done in uverbs_uobject_fd_release() */
668 kref_get(&uobj->ufile->ref);
669
670 /* This shouldn't be used anymore. Use the file object instead */
671 uobj->id = 0;
672
673 if (!filp->private_data) {
674 /*
675 * NOTE: Once we install the file we loose ownership of our kref on
676 * uobj. It will be put by uverbs_uobject_fd_release()
677 */
678 filp->private_data = uobj;
679 }
680
681 fd_install(fd, filp);
682 }
683
684 /*
685 * In all cases rdma_alloc_commit_uobject() consumes the kref to uobj and the
686 * caller can no longer assume uobj is valid. If this function fails it
687 * destroys the uboject, including the attached HW object.
688 */
rdma_alloc_commit_uobject(struct ib_uobject * uobj,struct uverbs_attr_bundle * attrs)689 void rdma_alloc_commit_uobject(struct ib_uobject *uobj,
690 struct uverbs_attr_bundle *attrs)
691 {
692 struct ib_uverbs_file *ufile = attrs->ufile;
693
694 /* kref is held so long as the uobj is on the uobj list. */
695 uverbs_uobject_get(uobj);
696 spin_lock_irq(&ufile->uobjects_lock);
697 list_add(&uobj->list, &ufile->uobjects);
698 spin_unlock_irq(&ufile->uobjects_lock);
699
700 /* matches atomic_set(-1) in alloc_uobj */
701 atomic_set(&uobj->usecnt, 0);
702
703 /* alloc_commit consumes the uobj kref */
704 uobj->uapi_object->type_class->alloc_commit(uobj);
705
706 /* Matches the down_read in rdma_alloc_begin_uobject */
707 up_read(&ufile->hw_destroy_rwsem);
708 }
709 EXPORT_SYMBOL_NS_GPL(rdma_alloc_commit_uobject, "rdma_core");
710
711 /*
712 * new_uobj will be assigned to the handle currently used by to_uobj, and
713 * to_uobj will be destroyed.
714 *
715 * Upon return the caller must do:
716 * rdma_alloc_commit_uobject(new_uobj)
717 * uobj_put_destroy(to_uobj)
718 *
719 * to_uobj must have a write get but the put mode switches to destroy once
720 * this is called.
721 */
rdma_assign_uobject(struct ib_uobject * to_uobj,struct ib_uobject * new_uobj,struct uverbs_attr_bundle * attrs)722 void rdma_assign_uobject(struct ib_uobject *to_uobj, struct ib_uobject *new_uobj,
723 struct uverbs_attr_bundle *attrs)
724 {
725 assert_uverbs_usecnt(new_uobj, UVERBS_LOOKUP_WRITE);
726
727 if (WARN_ON(to_uobj->uapi_object != new_uobj->uapi_object ||
728 !to_uobj->uapi_object->type_class->swap_uobjects))
729 return;
730
731 to_uobj->uapi_object->type_class->swap_uobjects(to_uobj, new_uobj);
732
733 /*
734 * If this fails then the uobject is still completely valid (though with
735 * a new ID) and we leak it until context close.
736 */
737 uverbs_destroy_uobject(to_uobj, RDMA_REMOVE_DESTROY, attrs);
738 }
739 EXPORT_SYMBOL_NS_GPL(rdma_assign_uobject, "rdma_core");
740
741 /*
742 * This consumes the kref for uobj. It is up to the caller to unwind the HW
743 * object and anything else connected to uobj before calling this.
744 */
rdma_alloc_abort_uobject(struct ib_uobject * uobj,struct uverbs_attr_bundle * attrs,bool hw_obj_valid)745 void rdma_alloc_abort_uobject(struct ib_uobject *uobj,
746 struct uverbs_attr_bundle *attrs,
747 bool hw_obj_valid)
748 {
749 struct ib_uverbs_file *ufile = uobj->ufile;
750 int ret;
751
752 if (hw_obj_valid) {
753 ret = uobj->uapi_object->type_class->destroy_hw(
754 uobj, RDMA_REMOVE_ABORT, attrs);
755 /*
756 * If the driver couldn't destroy the object then go ahead and
757 * commit it. Leaking objects that can't be destroyed is only
758 * done during FD close after the driver has a few more tries to
759 * destroy it.
760 */
761 if (WARN_ON(ret))
762 return rdma_alloc_commit_uobject(uobj, attrs);
763 }
764
765 uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT, attrs);
766
767 /* Matches the down_read in rdma_alloc_begin_uobject */
768 up_read(&ufile->hw_destroy_rwsem);
769 }
770 EXPORT_SYMBOL_NS_GPL(rdma_alloc_abort_uobject, "rdma_core");
771
lookup_put_idr_uobject(struct ib_uobject * uobj,enum rdma_lookup_mode mode)772 static void lookup_put_idr_uobject(struct ib_uobject *uobj,
773 enum rdma_lookup_mode mode)
774 {
775 }
776
lookup_put_fd_uobject(struct ib_uobject * uobj,enum rdma_lookup_mode mode)777 static void lookup_put_fd_uobject(struct ib_uobject *uobj,
778 enum rdma_lookup_mode mode)
779 {
780 struct file *filp = uobj->object;
781
782 WARN_ON(mode != UVERBS_LOOKUP_READ);
783 /*
784 * This indirectly calls uverbs_uobject_fd_release() and free the
785 * object
786 */
787 fput(filp);
788 }
789
rdma_lookup_put_uobject(struct ib_uobject * uobj,enum rdma_lookup_mode mode)790 void rdma_lookup_put_uobject(struct ib_uobject *uobj,
791 enum rdma_lookup_mode mode)
792 {
793 assert_uverbs_usecnt(uobj, mode);
794 /*
795 * In order to unlock an object, either decrease its usecnt for
796 * read access or zero it in case of exclusive access. See
797 * uverbs_try_lock_object for locking schema information.
798 */
799 switch (mode) {
800 case UVERBS_LOOKUP_READ:
801 atomic_dec(&uobj->usecnt);
802 break;
803 case UVERBS_LOOKUP_WRITE:
804 atomic_set(&uobj->usecnt, 0);
805 break;
806 case UVERBS_LOOKUP_DESTROY:
807 break;
808 }
809
810 uobj->uapi_object->type_class->lookup_put(uobj, mode);
811 /* Pairs with the kref obtained by type->lookup_get */
812 uverbs_uobject_put(uobj);
813 }
814 EXPORT_SYMBOL_NS_GPL(rdma_lookup_put_uobject, "rdma_core");
815
setup_ufile_idr_uobject(struct ib_uverbs_file * ufile)816 void setup_ufile_idr_uobject(struct ib_uverbs_file *ufile)
817 {
818 xa_init_flags(&ufile->idr, XA_FLAGS_ALLOC);
819 }
820 EXPORT_SYMBOL_NS_GPL(setup_ufile_idr_uobject, "rdma_core");
821
release_ufile_idr_uobject(struct ib_uverbs_file * ufile)822 static void release_ufile_idr_uobject(struct ib_uverbs_file *ufile)
823 {
824 struct ib_uobject *entry;
825 unsigned long id;
826
827 /*
828 * At this point uverbs_cleanup_ufile() is guaranteed to have run, and
829 * there are no HW objects left, however the xarray is still populated
830 * with anything that has not been cleaned up by userspace. Since the
831 * kref on ufile is 0, nothing is allowed to call lookup_get.
832 *
833 * This is an optimized equivalent to remove_handle_idr_uobject
834 */
835 xa_for_each(&ufile->idr, id, entry) {
836 WARN_ON(entry->object);
837 uverbs_uobject_put(entry);
838 }
839
840 xa_destroy(&ufile->idr);
841 }
842
843 const struct uverbs_obj_type_class uverbs_idr_class = {
844 .alloc_begin = alloc_begin_idr_uobject,
845 .lookup_get = lookup_get_idr_uobject,
846 .alloc_commit = alloc_commit_idr_uobject,
847 .alloc_abort = alloc_abort_idr_uobject,
848 .lookup_put = lookup_put_idr_uobject,
849 .destroy_hw = destroy_hw_idr_uobject,
850 .remove_handle = remove_handle_idr_uobject,
851 .swap_uobjects = swap_idr_uobjects,
852 };
853 EXPORT_SYMBOL(uverbs_idr_class);
854
uverbs_uobject_release(struct ib_uobject * uobj)855 int uverbs_uobject_release(struct ib_uobject *uobj)
856 {
857 struct ib_uverbs_file *ufile;
858
859 ufile = uobj->ufile;
860
861 if (down_read_trylock(&ufile->hw_destroy_rwsem)) {
862 struct uverbs_attr_bundle attrs = {
863 .context = uobj->context,
864 .ufile = ufile,
865 };
866
867 /*
868 * lookup_get_fd_uobject holds the kref on the struct file any
869 * time a FD uobj is locked, which prevents this release
870 * method from being invoked. Meaning we can always get the
871 * write lock here, or we have a kernel bug.
872 */
873 WARN_ON(uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE));
874 uverbs_destroy_uobject(uobj, RDMA_REMOVE_CLOSE, &attrs);
875 up_read(&ufile->hw_destroy_rwsem);
876 }
877
878 /* Matches the get in alloc_commit_fd_uobject() */
879 kref_put(&ufile->ref, ib_uverbs_release_file);
880
881 /* Pairs with filp->private_data in alloc_begin_fd_uobject */
882 uverbs_uobject_put(uobj);
883 return 0;
884 }
885 EXPORT_SYMBOL_NS_GPL(uverbs_uobject_release, "rdma_core");
886
887 /*
888 * Users of UVERBS_TYPE_ALLOC_FD should set this function as the struct
889 * file_operations release method.
890 */
uverbs_uobject_fd_release(struct inode * inode,struct file * filp)891 int uverbs_uobject_fd_release(struct inode *inode, struct file *filp)
892 {
893 void (*release_cleanup)(struct ib_uobject *uobj) = NULL;
894 struct ib_uobject *uobj = filp->private_data;
895 const struct uverbs_obj_type *type_attrs;
896 int ret;
897
898 /*
899 * This can only happen if the fput came from alloc_abort_fd_uobject()
900 */
901 if (!uobj)
902 return 0;
903
904 /*
905 * uverbs_disassociate_api() can NULL type_attrs after disassociate, but
906 * it won't if release_cleanup is used.
907 */
908 type_attrs = READ_ONCE(uobj->uapi_object->type_attrs);
909 if (type_attrs)
910 release_cleanup = container_of(type_attrs,
911 struct uverbs_obj_fd_type, type)
912 ->release_cleanup;
913 if (release_cleanup)
914 uverbs_uobject_get(uobj);
915
916 ret = uverbs_uobject_release(uobj);
917
918 if (release_cleanup) {
919 release_cleanup(uobj);
920 uverbs_uobject_put(uobj);
921 }
922
923 return ret;
924 }
925 EXPORT_SYMBOL(uverbs_uobject_fd_release);
926
__uverbs_cleanup_ufile(struct ib_uverbs_file * ufile,enum rdma_remove_reason reason)927 int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
928 enum rdma_remove_reason reason)
929 {
930 struct uverbs_attr_bundle attrs = { .ufile = ufile };
931 struct ib_ucontext *ucontext = ufile->ucontext;
932 struct ib_device *ib_dev = ucontext->device;
933 struct ib_uobject *obj, *next_obj;
934 int ret = -EINVAL;
935
936 if (ib_dev->ops.ufile_hw_cleanup)
937 ib_dev->ops.ufile_hw_cleanup(ufile);
938
939 /*
940 * This shouldn't run while executing other commands on this
941 * context. Thus, the only thing we should take care of is
942 * releasing a FD while traversing this list. The FD could be
943 * closed and released from the _release fop of this FD.
944 * In order to mitigate this, we add a lock.
945 * We take and release the lock per traversal in order to let
946 * other threads (which might still use the FDs) chance to run.
947 */
948 list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
949 attrs.context = obj->context;
950 /*
951 * if we hit this WARN_ON, that means we are
952 * racing with a lookup_get.
953 */
954 WARN_ON(uverbs_try_lock_object(obj, UVERBS_LOOKUP_WRITE));
955 if (reason == RDMA_REMOVE_DRIVER_FAILURE)
956 obj->object = NULL;
957 if (!uverbs_destroy_uobject(obj, reason, &attrs))
958 ret = 0;
959 else
960 atomic_set(&obj->usecnt, 0);
961 }
962
963 if (reason == RDMA_REMOVE_DRIVER_FAILURE) {
964 WARN_ON(!list_empty(&ufile->uobjects));
965 return 0;
966 }
967 return ret;
968 }
969 EXPORT_SYMBOL_NS_GPL(__uverbs_cleanup_ufile, "rdma_core");
970
971 const struct uverbs_obj_type_class uverbs_fd_class = {
972 .alloc_begin = alloc_begin_fd_uobject,
973 .lookup_get = lookup_get_fd_uobject,
974 .alloc_commit = alloc_commit_fd_uobject,
975 .alloc_abort = alloc_abort_fd_uobject,
976 .lookup_put = lookup_put_fd_uobject,
977 .destroy_hw = destroy_hw_fd_uobject,
978 .remove_handle = remove_handle_fd_uobject,
979 };
980 EXPORT_SYMBOL(uverbs_fd_class);
981
982 struct ib_uobject *
uverbs_get_uobject_from_file(u16 object_id,enum uverbs_obj_access access,s64 id,struct uverbs_attr_bundle * attrs)983 uverbs_get_uobject_from_file(u16 object_id, enum uverbs_obj_access access,
984 s64 id, struct uverbs_attr_bundle *attrs)
985 {
986 const struct uverbs_api_object *obj =
987 uapi_get_object(attrs->ufile->device->uapi, object_id);
988
989 switch (access) {
990 case UVERBS_ACCESS_READ:
991 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
992 UVERBS_LOOKUP_READ, attrs);
993 case UVERBS_ACCESS_DESTROY:
994 /* Actual destruction is done inside uverbs_handle_method */
995 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
996 UVERBS_LOOKUP_DESTROY, attrs);
997 case UVERBS_ACCESS_WRITE:
998 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
999 UVERBS_LOOKUP_WRITE, attrs);
1000 case UVERBS_ACCESS_NEW:
1001 return rdma_alloc_begin_uobject(obj, attrs);
1002 default:
1003 WARN_ON(true);
1004 return ERR_PTR(-EOPNOTSUPP);
1005 }
1006 }
1007 EXPORT_SYMBOL_NS_GPL(uverbs_get_uobject_from_file, "rdma_core");
1008
uverbs_finalize_object(struct ib_uobject * uobj,enum uverbs_obj_access access,bool hw_obj_valid,bool commit,struct uverbs_attr_bundle * attrs)1009 void uverbs_finalize_object(struct ib_uobject *uobj,
1010 enum uverbs_obj_access access, bool hw_obj_valid,
1011 bool commit, struct uverbs_attr_bundle *attrs)
1012 {
1013 /*
1014 * refcounts should be handled at the object level and not at the
1015 * uobject level. Refcounts of the objects themselves are done in
1016 * handlers.
1017 */
1018
1019 switch (access) {
1020 case UVERBS_ACCESS_READ:
1021 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
1022 break;
1023 case UVERBS_ACCESS_WRITE:
1024 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
1025 break;
1026 case UVERBS_ACCESS_DESTROY:
1027 if (uobj)
1028 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
1029 break;
1030 case UVERBS_ACCESS_NEW:
1031 if (commit)
1032 rdma_alloc_commit_uobject(uobj, attrs);
1033 else
1034 rdma_alloc_abort_uobject(uobj, attrs, hw_obj_valid);
1035 break;
1036 default:
1037 WARN_ON(true);
1038 }
1039 }
1040 EXPORT_SYMBOL_NS_GPL(uverbs_finalize_object, "rdma_core");
1041
1042 /**
1043 * rdma_uattrs_has_raw_cap() - Returns whether a rdma device linked to the
1044 * uverbs attributes file has CAP_NET_RAW
1045 * capability or not.
1046 *
1047 * @attrs: Pointer to uverbs attributes
1048 *
1049 * Returns true if a rdma device's owning user namespace has CAP_NET_RAW
1050 * capability, otherwise false.
1051 */
rdma_uattrs_has_raw_cap(const struct uverbs_attr_bundle * attrs)1052 bool rdma_uattrs_has_raw_cap(const struct uverbs_attr_bundle *attrs)
1053 {
1054 struct ib_uverbs_file *ufile = attrs->ufile;
1055 struct ib_ucontext *ucontext;
1056 bool has_cap = false;
1057 int srcu_key;
1058
1059 srcu_key = srcu_read_lock(&ufile->device->disassociate_srcu);
1060 ucontext = ib_uverbs_get_ucontext_file(ufile);
1061 if (IS_ERR(ucontext))
1062 goto out;
1063 has_cap = rdma_dev_has_raw_cap(ucontext->device);
1064
1065 out:
1066 srcu_read_unlock(&ufile->device->disassociate_srcu, srcu_key);
1067 return has_cap;
1068 }
1069 EXPORT_SYMBOL(rdma_uattrs_has_raw_cap);
1070
1071 MODULE_DESCRIPTION("InfiniBand uverbs objects");
1072 MODULE_LICENSE("Dual BSD/GPL");
1073