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