Skip to content

Commit 9867f5c

Browse files
committed
IB/uverbs: Convert 'bool exclusive' into an enum
This is more readable, and future patches will need a 3rd lookup type. Signed-off-by: Jason Gunthorpe <jgg@mellanox.com>
1 parent 87ad80a commit 9867f5c

3 files changed

Lines changed: 75 additions & 48 deletions

File tree

‎drivers/infiniband/core/rdma_core.c‎

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
320329
static struct ib_uobject *
321330
lookup_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

387398
struct 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;
408419
free:
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)

‎include/rdma/uverbs_std_types.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ static inline const struct uverbs_object_tree_def *uverbs_default_get_objects(vo
5858

5959
#define uobj_get_read(_type, _id, _ufile) \
6060
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
61-
_uobj_check_id(_id), false)
61+
_uobj_check_id(_id), UVERBS_LOOKUP_READ)
6262

6363
#define ufd_get_read(_type, _fdnum, _ufile) \
6464
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
65-
(_fdnum)*typecheck(s32, _fdnum), false)
65+
(_fdnum)*typecheck(s32, _fdnum), \
66+
UVERBS_LOOKUP_READ)
6667

6768
static inline void *_uobj_get_obj_read(struct ib_uobject *uobj)
6869
{
@@ -76,7 +77,7 @@ static inline void *_uobj_get_obj_read(struct ib_uobject *uobj)
7677

7778
#define uobj_get_write(_type, _id, _ufile) \
7879
rdma_lookup_get_uobject(uobj_get_type(_type), _ufile, \
79-
_uobj_check_id(_id), true)
80+
_uobj_check_id(_id), UVERBS_LOOKUP_WRITE)
8081

8182
int __uobj_perform_destroy(const struct uverbs_obj_type *type, u32 id,
8283
struct ib_uverbs_file *ufile, int success_res);
@@ -92,20 +93,20 @@ struct ib_uobject *__uobj_get_destroy(const struct uverbs_obj_type *type,
9293

9394
static inline void uobj_put_destroy(struct ib_uobject *uobj)
9495
{
95-
rdma_lookup_put_uobject(uobj, true);
96+
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
9697
}
9798

9899
static inline void uobj_put_read(struct ib_uobject *uobj)
99100
{
100-
rdma_lookup_put_uobject(uobj, false);
101+
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_READ);
101102
}
102103

103104
#define uobj_put_obj_read(_obj) \
104105
uobj_put_read((_obj)->uobject)
105106

106107
static inline void uobj_put_write(struct ib_uobject *uobj)
107108
{
108-
rdma_lookup_put_uobject(uobj, true);
109+
rdma_lookup_put_uobject(uobj, UVERBS_LOOKUP_WRITE);
109110
}
110111

111112
static inline int __must_check uobj_alloc_commit(struct ib_uobject *uobj,

‎include/rdma/uverbs_types.h‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838

3939
struct uverbs_obj_type;
4040

41+
enum rdma_lookup_mode {
42+
UVERBS_LOOKUP_READ,
43+
UVERBS_LOOKUP_WRITE,
44+
};
45+
4146
/*
4247
* The following sequences are valid:
4348
* Success flow:
@@ -78,8 +83,8 @@ struct uverbs_obj_type_class {
7883

7984
struct ib_uobject *(*lookup_get)(const struct uverbs_obj_type *type,
8085
struct ib_uverbs_file *ufile, s64 id,
81-
bool exclusive);
82-
void (*lookup_put)(struct ib_uobject *uobj, bool exclusive);
86+
enum rdma_lookup_mode mode);
87+
void (*lookup_put)(struct ib_uobject *uobj, enum rdma_lookup_mode mode);
8388
/* This does not consume the kref on uobj */
8489
int __must_check (*remove_commit)(struct ib_uobject *uobj,
8590
enum rdma_remove_reason why);
@@ -116,9 +121,10 @@ struct uverbs_obj_idr_type {
116121
};
117122

118123
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
119-
struct ib_uverbs_file *ufile,
120-
s64 id, bool exclusive);
121-
void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool exclusive);
124+
struct ib_uverbs_file *ufile, s64 id,
125+
enum rdma_lookup_mode mode);
126+
void rdma_lookup_put_uobject(struct ib_uobject *uobj,
127+
enum rdma_lookup_mode mode);
122128
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
123129
struct ib_uverbs_file *ufile);
124130
void rdma_alloc_abort_uobject(struct ib_uobject *uobj);

0 commit comments

Comments
 (0)