Skip to content

Commit 7452a3c

Browse files
committed
IB/uverbs: Allow RDMA_REMOVE_DESTROY to work concurrently with disassociate
After all the recent structural changes this is now straightfoward, hoist the hw_destroy_rwsem up out of rdma_destroy_explicit and wrap it around the uobject write lock as well as the destroy. This is necessary as obtaining a write lock concurrently with uverbs_destroy_ufile_hw() will cause malfunction. After this change none of the destroy callbacks require the disassociate_srcu lock to be correct. This requires introducing a new lookup mode, UVERBS_LOOKUP_DESTROY as the IOCTL interface needs to hold an unlocked kref until all command verification is completed. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent 9867f5c commit 7452a3c

4 files changed

Lines changed: 63 additions & 24 deletions

File tree

‎drivers/infiniband/core/rdma_core.c‎

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ static int uverbs_try_lock_object(struct ib_uobject *uobj,
127127
return __atomic_add_unless(&uobj->usecnt, 1, -1) == -1 ?
128128
-EBUSY : 0;
129129
case UVERBS_LOOKUP_WRITE:
130-
/* lock is either WRITE or DESTROY - should be exclusive */
130+
/* lock is exclusive */
131131
return atomic_cmpxchg(&uobj->usecnt, 0, -1) == 0 ? 0 : -EBUSY;
132+
case UVERBS_LOOKUP_DESTROY:
133+
return 0;
132134
}
133135
return 0;
134136
}
@@ -144,6 +146,8 @@ static void assert_uverbs_usecnt(struct ib_uobject *uobj,
144146
case UVERBS_LOOKUP_WRITE:
145147
WARN_ON(atomic_read(&uobj->usecnt) != -1);
146148
break;
149+
case UVERBS_LOOKUP_DESTROY:
150+
break;
147151
}
148152
#endif
149153
}
@@ -227,6 +231,35 @@ static int uverbs_destroy_uobject(struct ib_uobject *uobj,
227231
return 0;
228232
}
229233

234+
/*
235+
* This calls uverbs_destroy_uobject() using the RDMA_REMOVE_DESTROY
236+
* sequence. It should only be used from command callbacks. On success the
237+
* caller must pair this with rdma_lookup_put_uobject(LOOKUP_WRITE). This
238+
* version requires the caller to have already obtained an
239+
* LOOKUP_DESTROY uobject kref.
240+
*/
241+
int uobj_destroy(struct ib_uobject *uobj)
242+
{
243+
struct ib_uverbs_file *ufile = uobj->ufile;
244+
int ret;
245+
246+
down_read(&ufile->hw_destroy_rwsem);
247+
248+
ret = uverbs_try_lock_object(uobj, UVERBS_LOOKUP_WRITE);
249+
if (ret)
250+
goto out_unlock;
251+
252+
ret = uverbs_destroy_uobject(uobj, RDMA_REMOVE_DESTROY);
253+
if (ret) {
254+
atomic_set(&uobj->usecnt, 0);
255+
goto out_unlock;
256+
}
257+
258+
out_unlock:
259+
up_read(&ufile->hw_destroy_rwsem);
260+
return ret;
261+
}
262+
230263
/*
231264
* uobj_get_destroy destroys the HW object and returns a handle to the uobj
232265
* with a NULL object pointer. The caller must pair this with
@@ -238,13 +271,13 @@ struct ib_uobject *__uobj_get_destroy(const struct uverbs_obj_type *type,
238271
struct ib_uobject *uobj;
239272
int ret;
240273

241-
uobj = rdma_lookup_get_uobject(type, ufile, id, UVERBS_LOOKUP_WRITE);
274+
uobj = rdma_lookup_get_uobject(type, ufile, id, UVERBS_LOOKUP_DESTROY);
242275
if (IS_ERR(uobj))
243276
return uobj;
244277

245-
ret = rdma_explicit_destroy(uobj);
278+
ret = uobj_destroy(uobj);
246279
if (ret) {
247-
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
280+
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
248281
return ERR_PTR(ret);
249282
}
250283

@@ -265,6 +298,11 @@ int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
265298
if (IS_ERR(uobj))
266299
return PTR_ERR(uobj);
267300

301+
/*
302+
* FIXME: After destroy this is not safe. We no longer hold the rwsem
303+
* so disassociation could have completed and unloaded the module that
304+
* backs the uobj->type pointer.
305+
*/
268306
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
269307
return success_res;
270308
}
@@ -534,23 +572,6 @@ static int __must_check remove_commit_fd_uobject(struct ib_uobject *uobj,
534572
return 0;
535573
}
536574

537-
int rdma_explicit_destroy(struct ib_uobject *uobject)
538-
{
539-
int ret;
540-
struct ib_uverbs_file *ufile = uobject->ufile;
541-
542-
/* Cleanup is running. Calling this should have been impossible */
543-
if (!down_read_trylock(&ufile->hw_destroy_rwsem)) {
544-
WARN(true, "ib_uverbs: Cleanup is running while removing an uobject\n");
545-
return 0;
546-
}
547-
548-
ret = uverbs_destroy_uobject(uobject, RDMA_REMOVE_DESTROY);
549-
550-
up_read(&ufile->hw_destroy_rwsem);
551-
return ret;
552-
}
553-
554575
static int alloc_commit_idr_uobject(struct ib_uobject *uobj)
555576
{
556577
struct ib_uverbs_file *ufile = uobj->ufile;
@@ -686,6 +707,8 @@ void rdma_lookup_put_uobject(struct ib_uobject *uobj,
686707
case UVERBS_LOOKUP_WRITE:
687708
atomic_set(&uobj->usecnt, 0);
688709
break;
710+
case UVERBS_LOOKUP_DESTROY:
711+
break;
689712
}
690713

691714
/* Pairs with the kref obtained by type->lookup_get */
@@ -911,6 +934,9 @@ uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
911934
return rdma_lookup_get_uobject(type_attrs, ufile, id,
912935
UVERBS_LOOKUP_READ);
913936
case UVERBS_ACCESS_DESTROY:
937+
/* Actual destruction is done inside uverbs_handle_method */
938+
return rdma_lookup_get_uobject(type_attrs, ufile, id,
939+
UVERBS_LOOKUP_DESTROY);
914940
case UVERBS_ACCESS_WRITE:
915941
return rdma_lookup_get_uobject(type_attrs, ufile, id,
916942
UVERBS_LOOKUP_WRITE);
@@ -942,7 +968,8 @@ int uverbs_finalize_object(struct ib_uobject *uobj,
942968
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
943969
break;
944970
case UVERBS_ACCESS_DESTROY:
945-
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
971+
if (uobj)
972+
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_DESTROY);
946973
break;
947974
case UVERBS_ACCESS_NEW:
948975
if (commit)

‎drivers/infiniband/core/rdma_core.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const struct uverbs_method_spec *uverbs_get_method(const struct uverbs_object_sp
5252
void uverbs_destroy_ufile_hw(struct ib_uverbs_file *ufile,
5353
enum rdma_remove_reason reason);
5454

55+
int uobj_destroy(struct ib_uobject *uobj);
56+
5557
/*
5658
* uverbs_uobject_get is called in order to increase the reference count on
5759
* an uobject. This is useful when a handler wants to keep the uobject's memory

‎drivers/infiniband/core/uverbs_ioctl.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,18 @@ static int uverbs_handle_method(struct ib_uverbs_attr __user *uattr_ptr,
349349
* not get to manipulate the HW objects.
350350
*/
351351
if (destroy_attr) {
352-
ret = rdma_explicit_destroy(destroy_attr->uobject);
352+
ret = uobj_destroy(destroy_attr->uobject);
353353
if (ret)
354354
goto cleanup;
355355
}
356356

357357
ret = method_spec->handler(ibdev, ufile, attr_bundle);
358358

359+
if (destroy_attr) {
360+
uobj_put_destroy(destroy_attr->uobject);
361+
destroy_attr->uobject = NULL;
362+
}
363+
359364
cleanup:
360365
finalize_ret = uverbs_finalize_attrs(attr_bundle,
361366
method_spec->attr_buckets,

‎include/rdma/uverbs_types.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ struct uverbs_obj_type;
4141
enum rdma_lookup_mode {
4242
UVERBS_LOOKUP_READ,
4343
UVERBS_LOOKUP_WRITE,
44+
/*
45+
* Destroy is like LOOKUP_WRITE, except that the uobject is not
46+
* locked. uobj_destroy is used to convert a LOOKUP_DESTROY lock into
47+
* a LOOKUP_WRITE lock.
48+
*/
49+
UVERBS_LOOKUP_DESTROY,
4450
};
4551

4652
/*
@@ -129,7 +135,6 @@ struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
129135
struct ib_uverbs_file *ufile);
130136
void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
131137
int __must_check rdma_alloc_commit_uobject(struct ib_uobject *uobj);
132-
int rdma_explicit_destroy(struct ib_uobject *uobject);
133138

134139
struct uverbs_obj_fd_type {
135140
/*

0 commit comments

Comments
 (0)