@@ -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-
554575static 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 )
0 commit comments