This source file includes following definitions.
- uverbs_uobject_get
- uverbs_uobject_free
- uverbs_uobject_put
- uverbs_try_lock_object
- assert_uverbs_usecnt
- uverbs_destroy_uobject
- uobj_destroy
- __uobj_get_destroy
- __uobj_perform_destroy
- alloc_uobj
- idr_add_uobj
- lookup_get_idr_uobject
- lookup_get_fd_uobject
- rdma_lookup_get_uobject
- alloc_begin_idr_uobject
- alloc_begin_fd_uobject
- rdma_alloc_begin_uobject
- alloc_abort_idr_uobject
- destroy_hw_idr_uobject
- remove_handle_idr_uobject
- alloc_abort_fd_uobject
- destroy_hw_fd_uobject
- remove_handle_fd_uobject
- alloc_commit_idr_uobject
- alloc_commit_fd_uobject
- rdma_alloc_commit_uobject
- rdma_alloc_abort_uobject
- lookup_put_idr_uobject
- lookup_put_fd_uobject
- rdma_lookup_put_uobject
- setup_ufile_idr_uobject
- release_ufile_idr_uobject
- uverbs_close_fd
- ufile_destroy_ucontext
- __uverbs_cleanup_ufile
- uverbs_destroy_ufile_hw
- uverbs_get_uobject_from_file
- uverbs_finalize_object
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 void uverbs_uobject_get(struct ib_uobject *uobject)
46 {
47 kref_get(&uobject->ref);
48 }
49
50 static void uverbs_uobject_free(struct kref *ref)
51 {
52 struct ib_uobject *uobj =
53 container_of(ref, struct ib_uobject, ref);
54
55 if (uobj->uapi_object->type_class->needs_kfree_rcu)
56 kfree_rcu(uobj, rcu);
57 else
58 kfree(uobj);
59 }
60
61 void uverbs_uobject_put(struct ib_uobject *uobject)
62 {
63 kref_put(&uobject->ref, uverbs_uobject_free);
64 }
65
66 static int uverbs_try_lock_object(struct ib_uobject *uobj,
67 enum rdma_lookup_mode mode)
68 {
69
70
71
72
73
74
75
76
77
78
79
80 switch (mode) {
81 case UVERBS_LOOKUP_READ:
82 return atomic_fetch_add_unless(&uobj->usecnt, 1, -1) == -1 ?
83 -EBUSY : 0;
84 case UVERBS_LOOKUP_WRITE:
85
86 return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
87 case UVERBS_LOOKUP_DESTROY:
88 return 0;
89 }
90 return 0;
91 }
92
93 static void assert_uverbs_usecnt(struct ib_uobject *uobj,
94 enum rdma_lookup_mode mode)
95 {
96 #ifdef CONFIG_LOCKDEP
97 switch (mode) {
98 case UVERBS_LOOKUP_READ:
99 WARN_ON(atomic_read(&uobj->usecnt) <= 0);
100 break;
101 case UVERBS_LOOKUP_WRITE:
102 WARN_ON(atomic_read(&uobj->usecnt) != -1);
103 break;
104 case UVERBS_LOOKUP_DESTROY:
105 break;
106 }
107 #endif
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 static int uverbs_destroy_uobject(struct ib_uobject *uobj,
128 enum rdma_remove_reason reason,
129 struct uverbs_attr_bundle *attrs)
130 {
131 struct ib_uverbs_file *ufile = attrs->ufile;
132 unsigned long flags;
133 int ret;
134
135 lockdep_assert_held(&ufile->hw_destroy_rwsem);
136 assert_uverbs_usecnt(uobj, UVERBS_LOOKUP_WRITE);
137
138 if (uobj->object) {
139 ret = uobj->uapi_object->type_class->destroy_hw(uobj, reason,
140 attrs);
141 if (ret) {
142 if (ib_is_destroy_retryable(ret, reason, uobj))
143 return ret;
144
145
146 WARN(true,
147 "ib_uverbs: failed to remove uobject id %d, driver err=%d",
148 uobj->id, ret);
149 }
150
151 uobj->object = NULL;
152 }
153
154 if (reason == RDMA_REMOVE_ABORT) {
155 WARN_ON(!list_empty(&uobj->list));
156 WARN_ON(!uobj->context);
157 uobj->uapi_object->type_class->alloc_abort(uobj);
158 }
159
160 uobj->context = NULL;
161
162
163
164
165
166
167 if (reason != RDMA_REMOVE_DESTROY)
168 atomic_set(&uobj->usecnt, 0);
169 else
170 uobj->uapi_object->type_class->remove_handle(uobj);
171
172 if (!list_empty(&uobj->list)) {
173 spin_lock_irqsave(&ufile->uobjects_lock, flags);
174 list_del_init(&uobj->list);
175 spin_unlock_irqrestore(&ufile->uobjects_lock, flags);
176
177
178
179
180
181 uverbs_uobject_put(uobj);
182 }
183
184
185
186
187
188 if (reason == RDMA_REMOVE_ABORT)
189 uverbs_uobject_put(uobj);
190
191 return 0;
192 }
193
194
195
196
197
198
199
200
201 int uobj_destroy(struct ib_uobject *uobj, struct uverbs_attr_bundle *attrs)
202 {
203 struct ib_uverbs_file *ufile = attrs->ufile;
204 int ret;
205
206 down_read(&ufile->hw_destroy_rwsem);
207
208
209
210
211
212
213
214
215 ret = uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE);
216 if (ret)
217 goto out_unlock;
218
219 ret = uverbs_destroy_uobject(uobj, RDMA_REMOVE_DESTROY, attrs);
220 if (ret) {
221 atomic_set(&uobj->usecnt, 0);
222 goto out_unlock;
223 }
224
225 out_unlock:
226 up_read(&ufile->hw_destroy_rwsem);
227 return ret;
228 }
229
230
231
232
233
234
235 struct ib_uobject *__uobj_get_destroy(const struct uverbs_api_object *obj,
236 u32 id, struct uverbs_attr_bundle *attrs)
237 {
238 struct ib_uobject *uobj;
239 int ret;
240
241 uobj = rdma_lookup_get_uobject(obj, attrs->ufile, id,
242 UVERBS_LOOKUP_DESTROY, attrs);
243 if (IS_ERR(uobj))
244 return uobj;
245
246 ret = uobj_destroy(uobj, attrs);
247 if (ret) {
248 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
249 return ERR_PTR(ret);
250 }
251
252 return uobj;
253 }
254
255
256
257
258
259 int __uobj_perform_destroy(const struct uverbs_api_object *obj, u32 id,
260 struct uverbs_attr_bundle *attrs)
261 {
262 struct ib_uobject *uobj;
263
264 uobj = __uobj_get_destroy(obj, id, attrs);
265 if (IS_ERR(uobj))
266 return PTR_ERR(uobj);
267 uobj_put_destroy(uobj);
268 return 0;
269 }
270
271
272 static struct ib_uobject *alloc_uobj(struct ib_uverbs_file *ufile,
273 const struct uverbs_api_object *obj)
274 {
275 struct ib_uobject *uobj;
276 struct ib_ucontext *ucontext;
277
278 ucontext = ib_uverbs_get_ucontext_file(ufile);
279 if (IS_ERR(ucontext))
280 return ERR_CAST(ucontext);
281
282 uobj = kzalloc(obj->type_attrs->obj_size, GFP_KERNEL);
283 if (!uobj)
284 return ERR_PTR(-ENOMEM);
285
286
287
288
289 uobj->ufile = ufile;
290 uobj->context = ucontext;
291 INIT_LIST_HEAD(&uobj->list);
292 uobj->uapi_object = obj;
293
294
295
296
297
298 atomic_set(&uobj->usecnt, -1);
299 kref_init(&uobj->ref);
300
301 return uobj;
302 }
303
304 static int idr_add_uobj(struct ib_uobject *uobj)
305 {
306
307
308
309
310
311 return xa_alloc(&uobj->ufile->idr, &uobj->id, NULL, xa_limit_32b,
312 GFP_KERNEL);
313 }
314
315
316 static struct ib_uobject *
317 lookup_get_idr_uobject(const struct uverbs_api_object *obj,
318 struct ib_uverbs_file *ufile, s64 id,
319 enum rdma_lookup_mode mode)
320 {
321 struct ib_uobject *uobj;
322
323 if (id < 0 || id > ULONG_MAX)
324 return ERR_PTR(-EINVAL);
325
326 rcu_read_lock();
327
328
329
330
331
332
333 uobj = xa_load(&ufile->idr, id);
334 if (!uobj || !kref_get_unless_zero(&uobj->ref))
335 uobj = ERR_PTR(-ENOENT);
336 rcu_read_unlock();
337 return uobj;
338 }
339
340 static struct ib_uobject *
341 lookup_get_fd_uobject(const struct uverbs_api_object *obj,
342 struct ib_uverbs_file *ufile, s64 id,
343 enum rdma_lookup_mode mode)
344 {
345 const struct uverbs_obj_fd_type *fd_type;
346 struct file *f;
347 struct ib_uobject *uobject;
348 int fdno = id;
349
350 if (fdno != id)
351 return ERR_PTR(-EINVAL);
352
353 if (mode != UVERBS_LOOKUP_READ)
354 return ERR_PTR(-EOPNOTSUPP);
355
356 if (!obj->type_attrs)
357 return ERR_PTR(-EIO);
358 fd_type =
359 container_of(obj->type_attrs, struct uverbs_obj_fd_type, type);
360
361 f = fget(fdno);
362 if (!f)
363 return ERR_PTR(-EBADF);
364
365 uobject = f->private_data;
366
367
368
369
370
371 if (f->f_op != fd_type->fops || uobject->ufile != ufile) {
372 fput(f);
373 return ERR_PTR(-EBADF);
374 }
375
376 uverbs_uobject_get(uobject);
377 return uobject;
378 }
379
380 struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_api_object *obj,
381 struct ib_uverbs_file *ufile, s64 id,
382 enum rdma_lookup_mode mode,
383 struct uverbs_attr_bundle *attrs)
384 {
385 struct ib_uobject *uobj;
386 int ret;
387
388 if (obj == ERR_PTR(-ENOMSG)) {
389
390 uobj = lookup_get_idr_uobject(NULL, ufile, id, mode);
391 if (IS_ERR(uobj))
392 return uobj;
393 } else {
394 if (IS_ERR(obj))
395 return ERR_PTR(-EINVAL);
396
397 uobj = obj->type_class->lookup_get(obj, ufile, id, mode);
398 if (IS_ERR(uobj))
399 return uobj;
400
401 if (uobj->uapi_object != obj) {
402 ret = -EINVAL;
403 goto free;
404 }
405 }
406
407
408
409
410
411 if (mode != UVERBS_LOOKUP_DESTROY &&
412 !srcu_dereference(ufile->device->ib_dev,
413 &ufile->device->disassociate_srcu)) {
414 ret = -EIO;
415 goto free;
416 }
417
418 ret = uverbs_try_lock_object(uobj, mode);
419 if (ret)
420 goto free;
421 if (attrs)
422 attrs->context = uobj->context;
423
424 return uobj;
425 free:
426 uobj->uapi_object->type_class->lookup_put(uobj, mode);
427 uverbs_uobject_put(uobj);
428 return ERR_PTR(ret);
429 }
430
431 static struct ib_uobject *
432 alloc_begin_idr_uobject(const struct uverbs_api_object *obj,
433 struct ib_uverbs_file *ufile)
434 {
435 int ret;
436 struct ib_uobject *uobj;
437
438 uobj = alloc_uobj(ufile, obj);
439 if (IS_ERR(uobj))
440 return uobj;
441
442 ret = idr_add_uobj(uobj);
443 if (ret)
444 goto uobj_put;
445
446 ret = ib_rdmacg_try_charge(&uobj->cg_obj, uobj->context->device,
447 RDMACG_RESOURCE_HCA_OBJECT);
448 if (ret)
449 goto remove;
450
451 return uobj;
452
453 remove:
454 xa_erase(&ufile->idr, uobj->id);
455 uobj_put:
456 uverbs_uobject_put(uobj);
457 return ERR_PTR(ret);
458 }
459
460 static struct ib_uobject *
461 alloc_begin_fd_uobject(const struct uverbs_api_object *obj,
462 struct ib_uverbs_file *ufile)
463 {
464 int new_fd;
465 struct ib_uobject *uobj;
466
467 new_fd = get_unused_fd_flags(O_CLOEXEC);
468 if (new_fd < 0)
469 return ERR_PTR(new_fd);
470
471 uobj = alloc_uobj(ufile, obj);
472 if (IS_ERR(uobj)) {
473 put_unused_fd(new_fd);
474 return uobj;
475 }
476
477 uobj->id = new_fd;
478 uobj->ufile = ufile;
479
480 return uobj;
481 }
482
483 struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_api_object *obj,
484 struct ib_uverbs_file *ufile,
485 struct uverbs_attr_bundle *attrs)
486 {
487 struct ib_uobject *ret;
488
489 if (IS_ERR(obj))
490 return ERR_PTR(-EINVAL);
491
492
493
494
495
496
497 if (!down_read_trylock(&ufile->hw_destroy_rwsem))
498 return ERR_PTR(-EIO);
499
500 ret = obj->type_class->alloc_begin(obj, ufile);
501 if (IS_ERR(ret)) {
502 up_read(&ufile->hw_destroy_rwsem);
503 return ret;
504 }
505 if (attrs)
506 attrs->context = ret->context;
507 return ret;
508 }
509
510 static void alloc_abort_idr_uobject(struct ib_uobject *uobj)
511 {
512 ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
513 RDMACG_RESOURCE_HCA_OBJECT);
514
515 xa_erase(&uobj->ufile->idr, uobj->id);
516 }
517
518 static int __must_check destroy_hw_idr_uobject(struct ib_uobject *uobj,
519 enum rdma_remove_reason why,
520 struct uverbs_attr_bundle *attrs)
521 {
522 const struct uverbs_obj_idr_type *idr_type =
523 container_of(uobj->uapi_object->type_attrs,
524 struct uverbs_obj_idr_type, type);
525 int ret = idr_type->destroy_object(uobj, why, attrs);
526
527
528
529
530
531
532 if (ib_is_destroy_retryable(ret, why, uobj))
533 return ret;
534
535 if (why == RDMA_REMOVE_ABORT)
536 return 0;
537
538 ib_rdmacg_uncharge(&uobj->cg_obj, uobj->context->device,
539 RDMACG_RESOURCE_HCA_OBJECT);
540
541 return 0;
542 }
543
544 static void remove_handle_idr_uobject(struct ib_uobject *uobj)
545 {
546 xa_erase(&uobj->ufile->idr, uobj->id);
547
548 uverbs_uobject_put(uobj);
549 }
550
551 static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
552 {
553 put_unused_fd(uobj->id);
554 }
555
556 static int __must_check destroy_hw_fd_uobject(struct ib_uobject *uobj,
557 enum rdma_remove_reason why,
558 struct uverbs_attr_bundle *attrs)
559 {
560 const struct uverbs_obj_fd_type *fd_type = container_of(
561 uobj->uapi_object->type_attrs, struct uverbs_obj_fd_type, type);
562 int ret = fd_type->context_closed(uobj, why);
563
564 if (ib_is_destroy_retryable(ret, why, uobj))
565 return ret;
566
567 return 0;
568 }
569
570 static void remove_handle_fd_uobject(struct ib_uobject *uobj)
571 {
572 }
573
574 static int alloc_commit_idr_uobject(struct ib_uobject *uobj)
575 {
576 struct ib_uverbs_file *ufile = uobj->ufile;
577 void *old;
578
579
580
581
582
583
584
585
586 old = xa_store(&ufile->idr, uobj->id, uobj, GFP_KERNEL);
587 WARN_ON(old != NULL);
588
589 return 0;
590 }
591
592 static int alloc_commit_fd_uobject(struct ib_uobject *uobj)
593 {
594 const struct uverbs_obj_fd_type *fd_type = container_of(
595 uobj->uapi_object->type_attrs, struct uverbs_obj_fd_type, type);
596 int fd = uobj->id;
597 struct file *filp;
598
599
600
601
602
603
604
605 filp = anon_inode_getfile(fd_type->name,
606 fd_type->fops,
607 uobj,
608 fd_type->flags);
609 if (IS_ERR(filp))
610 return PTR_ERR(filp);
611
612 uobj->object = filp;
613
614
615 kref_get(&uobj->ufile->ref);
616
617
618 uobj->id = 0;
619
620
621
622
623
624 fd_install(fd, filp);
625
626 return 0;
627 }
628
629
630
631
632
633
634 int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj,
635 struct uverbs_attr_bundle *attrs)
636 {
637 struct ib_uverbs_file *ufile = attrs->ufile;
638 int ret;
639
640
641 ret = uobj->uapi_object->type_class->alloc_commit(uobj);
642 if (ret) {
643 uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT, attrs);
644 up_read(&ufile->hw_destroy_rwsem);
645 return ret;
646 }
647
648
649 uverbs_uobject_get(uobj);
650 spin_lock_irq(&ufile->uobjects_lock);
651 list_add(&uobj->list, &ufile->uobjects);
652 spin_unlock_irq(&ufile->uobjects_lock);
653
654
655 atomic_set(&uobj->usecnt, 0);
656
657
658 up_read(&ufile->hw_destroy_rwsem);
659
660 return 0;
661 }
662
663
664
665
666
667 void rdma_alloc_abort_uobject(struct ib_uobject *uobj,
668 struct uverbs_attr_bundle *attrs)
669 {
670 struct ib_uverbs_file *ufile = uobj->ufile;
671
672 uobj->object = NULL;
673 uverbs_destroy_uobject(uobj, RDMA_REMOVE_ABORT, attrs);
674
675
676 up_read(&ufile->hw_destroy_rwsem);
677 }
678
679 static void lookup_put_idr_uobject(struct ib_uobject *uobj,
680 enum rdma_lookup_mode mode)
681 {
682 }
683
684 static void lookup_put_fd_uobject(struct ib_uobject *uobj,
685 enum rdma_lookup_mode mode)
686 {
687 struct file *filp = uobj->object;
688
689 WARN_ON(mode != UVERBS_LOOKUP_READ);
690
691 fput(filp);
692 }
693
694 void rdma_lookup_put_uobject(struct ib_uobject *uobj,
695 enum rdma_lookup_mode mode)
696 {
697 assert_uverbs_usecnt(uobj, mode);
698
699
700
701
702
703 switch (mode) {
704 case UVERBS_LOOKUP_READ:
705 atomic_dec(&uobj->usecnt);
706 break;
707 case UVERBS_LOOKUP_WRITE:
708 atomic_set(&uobj->usecnt, 0);
709 break;
710 case UVERBS_LOOKUP_DESTROY:
711 break;
712 }
713
714 uobj->uapi_object->type_class->lookup_put(uobj, mode);
715
716 uverbs_uobject_put(uobj);
717 }
718
719 void setup_ufile_idr_uobject(struct ib_uverbs_file *ufile)
720 {
721 xa_init_flags(&ufile->idr, XA_FLAGS_ALLOC);
722 }
723
724 void release_ufile_idr_uobject(struct ib_uverbs_file *ufile)
725 {
726 struct ib_uobject *entry;
727 unsigned long id;
728
729
730
731
732
733
734
735
736
737 xa_for_each(&ufile->idr, id, entry) {
738 WARN_ON(entry->object);
739 uverbs_uobject_put(entry);
740 }
741
742 xa_destroy(&ufile->idr);
743 }
744
745 const struct uverbs_obj_type_class uverbs_idr_class = {
746 .alloc_begin = alloc_begin_idr_uobject,
747 .lookup_get = lookup_get_idr_uobject,
748 .alloc_commit = alloc_commit_idr_uobject,
749 .alloc_abort = alloc_abort_idr_uobject,
750 .lookup_put = lookup_put_idr_uobject,
751 .destroy_hw = destroy_hw_idr_uobject,
752 .remove_handle = remove_handle_idr_uobject,
753
754
755
756
757
758
759
760
761
762
763
764
765
766 .needs_kfree_rcu = true,
767 };
768 EXPORT_SYMBOL(uverbs_idr_class);
769
770 void uverbs_close_fd(struct file *f)
771 {
772 struct ib_uobject *uobj = f->private_data;
773 struct ib_uverbs_file *ufile = uobj->ufile;
774 struct uverbs_attr_bundle attrs = {
775 .context = uobj->context,
776 .ufile = ufile,
777 };
778
779 if (down_read_trylock(&ufile->hw_destroy_rwsem)) {
780
781
782
783
784
785
786 WARN_ON(uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE));
787 uverbs_destroy_uobject(uobj, RDMA_REMOVE_CLOSE, &attrs);
788 up_read(&ufile->hw_destroy_rwsem);
789 }
790
791
792 kref_put(&ufile->ref, ib_uverbs_release_file);
793
794
795 uverbs_uobject_put(uobj);
796 }
797 EXPORT_SYMBOL(uverbs_close_fd);
798
799
800
801
802
803 static void ufile_destroy_ucontext(struct ib_uverbs_file *ufile,
804 enum rdma_remove_reason reason)
805 {
806 struct ib_ucontext *ucontext = ufile->ucontext;
807 struct ib_device *ib_dev = ucontext->device;
808
809
810
811
812
813
814 if (reason == RDMA_REMOVE_DRIVER_REMOVE) {
815 uverbs_user_mmap_disassociate(ufile);
816 if (ib_dev->ops.disassociate_ucontext)
817 ib_dev->ops.disassociate_ucontext(ucontext);
818 }
819
820 ib_rdmacg_uncharge(&ucontext->cg_obj, ib_dev,
821 RDMACG_RESOURCE_HCA_HANDLE);
822
823 rdma_restrack_del(&ucontext->res);
824
825 ib_dev->ops.dealloc_ucontext(ucontext);
826 kfree(ucontext);
827
828 ufile->ucontext = NULL;
829 }
830
831 static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
832 enum rdma_remove_reason reason)
833 {
834 struct ib_uobject *obj, *next_obj;
835 int ret = -EINVAL;
836 struct uverbs_attr_bundle attrs = { .ufile = ufile };
837
838
839
840
841
842
843
844
845
846
847 list_for_each_entry_safe(obj, next_obj, &ufile->uobjects, list) {
848 attrs.context = obj->context;
849
850
851
852
853 WARN_ON(uverbs_try_lock_object(obj, UVERBS_LOOKUP_WRITE));
854 if (!uverbs_destroy_uobject(obj, reason, &attrs))
855 ret = 0;
856 else
857 atomic_set(&obj->usecnt, 0);
858 }
859 return ret;
860 }
861
862
863
864
865
866
867
868
869
870 void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
871 enum rdma_remove_reason reason)
872 {
873 if (reason == RDMA_REMOVE_CLOSE) {
874
875
876
877
878
879
880
881
882 if (!mutex_trylock(&ufile->ucontext_lock))
883 return;
884
885 } else {
886 mutex_lock(&ufile->ucontext_lock);
887 }
888
889 down_write(&ufile->hw_destroy_rwsem);
890
891
892
893
894
895 if (!ufile->ucontext)
896 goto done;
897
898 ufile->ucontext->closing = true;
899 ufile->ucontext->cleanup_retryable = true;
900 while (!list_empty(&ufile->uobjects))
901 if (__uverbs_cleanup_ufile(ufile, reason)) {
902
903
904
905
906 break;
907 }
908
909 ufile->ucontext->cleanup_retryable = false;
910 if (!list_empty(&ufile->uobjects))
911 __uverbs_cleanup_ufile(ufile, reason);
912
913 ufile_destroy_ucontext(ufile, reason);
914
915 done:
916 up_write(&ufile->hw_destroy_rwsem);
917 mutex_unlock(&ufile->ucontext_lock);
918 }
919
920 const struct uverbs_obj_type_class uverbs_fd_class = {
921 .alloc_begin = alloc_begin_fd_uobject,
922 .lookup_get = lookup_get_fd_uobject,
923 .alloc_commit = alloc_commit_fd_uobject,
924 .alloc_abort = alloc_abort_fd_uobject,
925 .lookup_put = lookup_put_fd_uobject,
926 .destroy_hw = destroy_hw_fd_uobject,
927 .remove_handle = remove_handle_fd_uobject,
928 .needs_kfree_rcu = false,
929 };
930 EXPORT_SYMBOL(uverbs_fd_class);
931
932 struct ib_uobject *
933 uverbs_get_uobject_from_file(u16 object_id, enum uverbs_obj_access access,
934 s64 id, struct uverbs_attr_bundle *attrs)
935 {
936 const struct uverbs_api_object *obj =
937 uapi_get_object(attrs->ufile->device->uapi, object_id);
938
939 switch (access) {
940 case UVERBS_ACCESS_READ:
941 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
942 UVERBS_LOOKUP_READ, attrs);
943 case UVERBS_ACCESS_DESTROY:
944
945 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
946 UVERBS_LOOKUP_DESTROY, attrs);
947 case UVERBS_ACCESS_WRITE:
948 return rdma_lookup_get_uobject(obj, attrs->ufile, id,
949 UVERBS_LOOKUP_WRITE, attrs);
950 case UVERBS_ACCESS_NEW:
951 return rdma_alloc_begin_uobject(obj, attrs->ufile, attrs);
952 default:
953 WARN_ON(true);
954 return ERR_PTR(-EOPNOTSUPP);
955 }
956 }
957
958 int uverbs_finalize_object(struct ib_uobject *uobj,
959 enum uverbs_obj_access access, bool commit,
960 struct uverbs_attr_bundle *attrs)
961 {
962 int ret = 0;
963
964
965
966
967
968
969
970 switch (access) {
971 case UVERBS_ACCESS_READ:
972 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
973 break;
974 case UVERBS_ACCESS_WRITE:
975 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
976 break;
977 case UVERBS_ACCESS_DESTROY:
978 if (uobj)
979 rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
980 break;
981 case UVERBS_ACCESS_NEW:
982 if (commit)
983 ret = rdma_alloc_commit_uobject(uobj, attrs);
984 else
985 rdma_alloc_abort_uobject(uobj, attrs);
986 break;
987 default:
988 WARN_ON(true);
989 ret = -EOPNOTSUPP;
990 }
991
992 return ret;
993 }