@@ -108,7 +108,8 @@ void uverbs_uobject_put(struct ib_uobject *uobject)
108108 kref_put (& uobject -> ref , uverbs_uobject_free );
109109}
110110
111- static int uverbs_try_lock_object (struct ib_uobject * uobj , bool exclusive )
111+ static int uverbs_try_lock_object (struct ib_uobject * uobj ,
112+ enum rdma_lookup_mode mode )
112113{
113114 /*
114115 * When a shared access is required, we use a positive counter. Each
@@ -121,21 +122,29 @@ static int uverbs_try_lock_object(struct ib_uobject *uobj, bool exclusive)
121122 * concurrently, setting the counter to zero is enough for releasing
122123 * this lock.
123124 */
124- if (!exclusive )
125+ switch (mode ) {
126+ case UVERBS_LOOKUP_READ :
125127 return __atomic_add_unless (& uobj -> usecnt , 1 , -1 ) == -1 ?
126128 - EBUSY : 0 ;
127-
128- /* lock is either WRITE or DESTROY - should be exclusive */
129- return atomic_cmpxchg (& uobj -> usecnt , 0 , -1 ) == 0 ? 0 : - EBUSY ;
129+ case UVERBS_LOOKUP_WRITE :
130+ /* lock is either WRITE or DESTROY - should be exclusive */
131+ return atomic_cmpxchg (& uobj -> usecnt , 0 , -1 ) == 0 ? 0 : - EBUSY ;
132+ }
133+ return 0 ;
130134}
131135
132- static void assert_uverbs_usecnt (struct ib_uobject * uobj , bool exclusive )
136+ static void assert_uverbs_usecnt (struct ib_uobject * uobj ,
137+ enum rdma_lookup_mode mode )
133138{
134139#ifdef CONFIG_LOCKDEP
135- if (exclusive )
136- WARN_ON (atomic_read (& uobj -> usecnt ) != -1 );
137- else
140+ switch (mode ) {
141+ case UVERBS_LOOKUP_READ :
138142 WARN_ON (atomic_read (& uobj -> usecnt ) <= 0 );
143+ break ;
144+ case UVERBS_LOOKUP_WRITE :
145+ WARN_ON (atomic_read (& uobj -> usecnt ) != -1 );
146+ break ;
147+ }
139148#endif
140149}
141150
@@ -164,7 +173,7 @@ static int uverbs_destroy_uobject(struct ib_uobject *uobj,
164173 unsigned long flags ;
165174 int ret ;
166175
167- assert_uverbs_usecnt (uobj , true );
176+ assert_uverbs_usecnt (uobj , UVERBS_LOOKUP_WRITE );
168177
169178 if (uobj -> object ) {
170179 ret = uobj -> type -> type_class -> remove_commit (uobj , reason );
@@ -229,13 +238,13 @@ struct ib_uobject *__uobj_get_destroy(const struct uverbs_obj_type *type,
229238 struct ib_uobject * uobj ;
230239 int ret ;
231240
232- uobj = rdma_lookup_get_uobject (type , ufile , id , true );
241+ uobj = rdma_lookup_get_uobject (type , ufile , id , UVERBS_LOOKUP_WRITE );
233242 if (IS_ERR (uobj ))
234243 return uobj ;
235244
236245 ret = rdma_explicit_destroy (uobj );
237246 if (ret ) {
238- rdma_lookup_put_uobject (uobj , true );
247+ rdma_lookup_put_uobject (uobj , UVERBS_LOOKUP_WRITE );
239248 return ERR_PTR (ret );
240249 }
241250
@@ -256,7 +265,7 @@ int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
256265 if (IS_ERR (uobj ))
257266 return PTR_ERR (uobj );
258267
259- rdma_lookup_put_uobject (uobj , true );
268+ rdma_lookup_put_uobject (uobj , UVERBS_LOOKUP_WRITE );
260269 return success_res ;
261270}
262271
@@ -319,7 +328,8 @@ static int idr_add_uobj(struct ib_uobject *uobj)
319328/* Returns the ib_uobject or an error. The caller should check for IS_ERR. */
320329static struct ib_uobject *
321330lookup_get_idr_uobject (const struct uverbs_obj_type * type ,
322- struct ib_uverbs_file * ufile , s64 id , bool exclusive )
331+ struct ib_uverbs_file * ufile , s64 id ,
332+ enum rdma_lookup_mode mode )
323333{
324334 struct ib_uobject * uobj ;
325335 unsigned long idrno = id ;
@@ -349,9 +359,10 @@ lookup_get_idr_uobject(const struct uverbs_obj_type *type,
349359 return uobj ;
350360}
351361
352- static struct ib_uobject * lookup_get_fd_uobject (const struct uverbs_obj_type * type ,
353- struct ib_uverbs_file * ufile ,
354- s64 id , bool exclusive )
362+ static struct ib_uobject *
363+ lookup_get_fd_uobject (const struct uverbs_obj_type * type ,
364+ struct ib_uverbs_file * ufile , s64 id ,
365+ enum rdma_lookup_mode mode )
355366{
356367 struct file * f ;
357368 struct ib_uobject * uobject ;
@@ -362,7 +373,7 @@ static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *ty
362373 if (fdno != id )
363374 return ERR_PTR (- EINVAL );
364375
365- if (exclusive )
376+ if (mode != UVERBS_LOOKUP_READ )
366377 return ERR_PTR (- EOPNOTSUPP );
367378
368379 f = fget (fdno );
@@ -386,12 +397,12 @@ static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *ty
386397
387398struct ib_uobject * rdma_lookup_get_uobject (const struct uverbs_obj_type * type ,
388399 struct ib_uverbs_file * ufile , s64 id ,
389- bool exclusive )
400+ enum rdma_lookup_mode mode )
390401{
391402 struct ib_uobject * uobj ;
392403 int ret ;
393404
394- uobj = type -> type_class -> lookup_get (type , ufile , id , exclusive );
405+ uobj = type -> type_class -> lookup_get (type , ufile , id , mode );
395406 if (IS_ERR (uobj ))
396407 return uobj ;
397408
@@ -400,13 +411,13 @@ struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
400411 goto free ;
401412 }
402413
403- ret = uverbs_try_lock_object (uobj , exclusive );
414+ ret = uverbs_try_lock_object (uobj , mode );
404415 if (ret )
405416 goto free ;
406417
407418 return uobj ;
408419free :
409- uobj -> type -> type_class -> lookup_put (uobj , exclusive );
420+ uobj -> type -> type_class -> lookup_put (uobj , mode );
410421 uverbs_uobject_put (uobj );
411422 return ERR_PTR (ret );
412423}
@@ -643,32 +654,39 @@ void rdma_alloc_abort_uobject(struct ib_uobject *uobj)
643654 uverbs_destroy_uobject (uobj , RDMA_REMOVE_ABORT );
644655}
645656
646- static void lookup_put_idr_uobject (struct ib_uobject * uobj , bool exclusive )
657+ static void lookup_put_idr_uobject (struct ib_uobject * uobj ,
658+ enum rdma_lookup_mode mode )
647659{
648660}
649661
650- static void lookup_put_fd_uobject (struct ib_uobject * uobj , bool exclusive )
662+ static void lookup_put_fd_uobject (struct ib_uobject * uobj ,
663+ enum rdma_lookup_mode mode )
651664{
652665 struct file * filp = uobj -> object ;
653666
654- WARN_ON (exclusive );
667+ WARN_ON (mode != UVERBS_LOOKUP_READ );
655668 /* This indirectly calls uverbs_close_fd and free the object */
656669 fput (filp );
657670}
658671
659- void rdma_lookup_put_uobject (struct ib_uobject * uobj , bool exclusive )
672+ void rdma_lookup_put_uobject (struct ib_uobject * uobj ,
673+ enum rdma_lookup_mode mode )
660674{
661- assert_uverbs_usecnt (uobj , exclusive );
662- uobj -> type -> type_class -> lookup_put (uobj , exclusive );
675+ assert_uverbs_usecnt (uobj , mode );
676+ uobj -> type -> type_class -> lookup_put (uobj , mode );
663677 /*
664678 * In order to unlock an object, either decrease its usecnt for
665679 * read access or zero it in case of exclusive access. See
666680 * uverbs_try_lock_object for locking schema information.
667681 */
668- if (!exclusive )
682+ switch (mode ) {
683+ case UVERBS_LOOKUP_READ :
669684 atomic_dec (& uobj -> usecnt );
670- else
685+ break ;
686+ case UVERBS_LOOKUP_WRITE :
671687 atomic_set (& uobj -> usecnt , 0 );
688+ break ;
689+ }
672690
673691 /* Pairs with the kref obtained by type->lookup_get */
674692 uverbs_uobject_put (uobj );
@@ -710,7 +728,7 @@ void uverbs_close_fd(struct file *f)
710728 * method from being invoked. Meaning we can always get the
711729 * write lock here, or we have a kernel bug.
712730 */
713- WARN_ON (uverbs_try_lock_object (uobj , true ));
731+ WARN_ON (uverbs_try_lock_object (uobj , UVERBS_LOOKUP_WRITE ));
714732 uverbs_destroy_uobject (uobj , RDMA_REMOVE_CLOSE );
715733 up_read (& ufile -> hw_destroy_rwsem );
716734 }
@@ -807,7 +825,7 @@ static int __uverbs_cleanup_ufile(struct ib_uverbs_file *ufile,
807825 * if we hit this WARN_ON, that means we are
808826 * racing with a lookup_get.
809827 */
810- WARN_ON (uverbs_try_lock_object (obj , true ));
828+ WARN_ON (uverbs_try_lock_object (obj , UVERBS_LOOKUP_WRITE ));
811829 if (!uverbs_destroy_uobject (obj , reason ))
812830 ret = 0 ;
813831 }
@@ -890,10 +908,12 @@ uverbs_get_uobject_from_file(const struct uverbs_obj_type *type_attrs,
890908{
891909 switch (access ) {
892910 case UVERBS_ACCESS_READ :
893- return rdma_lookup_get_uobject (type_attrs , ufile , id , false);
911+ return rdma_lookup_get_uobject (type_attrs , ufile , id ,
912+ UVERBS_LOOKUP_READ );
894913 case UVERBS_ACCESS_DESTROY :
895914 case UVERBS_ACCESS_WRITE :
896- return rdma_lookup_get_uobject (type_attrs , ufile , id , true);
915+ return rdma_lookup_get_uobject (type_attrs , ufile , id ,
916+ UVERBS_LOOKUP_WRITE );
897917 case UVERBS_ACCESS_NEW :
898918 return rdma_alloc_begin_uobject (type_attrs , ufile );
899919 default :
@@ -916,13 +936,13 @@ int uverbs_finalize_object(struct ib_uobject *uobj,
916936
917937 switch (access ) {
918938 case UVERBS_ACCESS_READ :
919- rdma_lookup_put_uobject (uobj , false );
939+ rdma_lookup_put_uobject (uobj , UVERBS_LOOKUP_READ );
920940 break ;
921941 case UVERBS_ACCESS_WRITE :
922- rdma_lookup_put_uobject (uobj , true );
942+ rdma_lookup_put_uobject (uobj , UVERBS_LOOKUP_WRITE );
923943 break ;
924944 case UVERBS_ACCESS_DESTROY :
925- rdma_lookup_put_uobject (uobj , true );
945+ rdma_lookup_put_uobject (uobj , UVERBS_LOOKUP_WRITE );
926946 break ;
927947 case UVERBS_ACCESS_NEW :
928948 if (commit )
0 commit comments