Skip to content

Commit cf8966b

Browse files
matanb10dledford
authored andcommitted
IB/core: Add support for fd objects
The completion channel we use in verbs infrastructure is FD based. Previously, we had a separate way to manage this object. Since we strive for a single way to manage any kind of object in this infrastructure, we conceptually treat all objects as subclasses of ib_uobject. This commit adds the necessary mechanism to support FD based objects like their IDR counterparts. FD objects release need to be synchronized with context release. We use the cleanup_mutex on the uverbs_file for that. Signed-off-by: Matan Barak <[email protected]> Reviewed-by: Yishai Hadas <[email protected]> Signed-off-by: Doug Ledford <[email protected]>
1 parent f48b726 commit cf8966b

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed

drivers/infiniband/core/rdma_core.c

Lines changed: 176 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,37 @@ static struct ib_uobject *lookup_get_idr_uobject(const struct uverbs_obj_type *t
153153
return uobj;
154154
}
155155

156+
static struct ib_uobject *lookup_get_fd_uobject(const struct uverbs_obj_type *type,
157+
struct ib_ucontext *ucontext,
158+
int id, bool write)
159+
{
160+
struct file *f;
161+
struct ib_uobject *uobject;
162+
const struct uverbs_obj_fd_type *fd_type =
163+
container_of(type, struct uverbs_obj_fd_type, type);
164+
165+
if (write)
166+
return ERR_PTR(-EOPNOTSUPP);
167+
168+
f = fget(id);
169+
if (!f)
170+
return ERR_PTR(-EBADF);
171+
172+
uobject = f->private_data;
173+
/*
174+
* fget(id) ensures we are not currently running uverbs_close_fd,
175+
* and the caller is expected to ensure that uverbs_close_fd is never
176+
* done while a call top lookup is possible.
177+
*/
178+
if (f->f_op != fd_type->fops) {
179+
fput(f);
180+
return ERR_PTR(-EBADF);
181+
}
182+
183+
uverbs_uobject_get(uobject);
184+
return uobject;
185+
}
186+
156187
struct ib_uobject *rdma_lookup_get_uobject(const struct uverbs_obj_type *type,
157188
struct ib_ucontext *ucontext,
158189
int id, bool write)
@@ -211,6 +242,46 @@ static struct ib_uobject *alloc_begin_idr_uobject(const struct uverbs_obj_type *
211242
return ERR_PTR(ret);
212243
}
213244

245+
static struct ib_uobject *alloc_begin_fd_uobject(const struct uverbs_obj_type *type,
246+
struct ib_ucontext *ucontext)
247+
{
248+
const struct uverbs_obj_fd_type *fd_type =
249+
container_of(type, struct uverbs_obj_fd_type, type);
250+
int new_fd;
251+
struct ib_uobject *uobj;
252+
struct ib_uobject_file *uobj_file;
253+
struct file *filp;
254+
255+
new_fd = get_unused_fd_flags(O_CLOEXEC);
256+
if (new_fd < 0)
257+
return ERR_PTR(new_fd);
258+
259+
uobj = alloc_uobj(ucontext, type);
260+
if (IS_ERR(uobj)) {
261+
put_unused_fd(new_fd);
262+
return uobj;
263+
}
264+
265+
uobj_file = container_of(uobj, struct ib_uobject_file, uobj);
266+
filp = anon_inode_getfile(fd_type->name,
267+
fd_type->fops,
268+
uobj_file,
269+
fd_type->flags);
270+
if (IS_ERR(filp)) {
271+
put_unused_fd(new_fd);
272+
uverbs_uobject_put(uobj);
273+
return (void *)filp;
274+
}
275+
276+
uobj_file->uobj.id = new_fd;
277+
uobj_file->uobj.object = filp;
278+
uobj_file->ufile = ucontext->ufile;
279+
INIT_LIST_HEAD(&uobj->list);
280+
kref_get(&uobj_file->ufile->ref);
281+
282+
return uobj;
283+
}
284+
214285
struct ib_uobject *rdma_alloc_begin_uobject(const struct uverbs_obj_type *type,
215286
struct ib_ucontext *ucontext)
216287
{
@@ -246,6 +317,39 @@ static int __must_check remove_commit_idr_uobject(struct ib_uobject *uobj,
246317
return ret;
247318
}
248319

320+
static void alloc_abort_fd_uobject(struct ib_uobject *uobj)
321+
{
322+
struct ib_uobject_file *uobj_file =
323+
container_of(uobj, struct ib_uobject_file, uobj);
324+
struct file *filp = uobj->object;
325+
int id = uobj_file->uobj.id;
326+
327+
/* Unsuccessful NEW */
328+
fput(filp);
329+
put_unused_fd(id);
330+
}
331+
332+
static int __must_check remove_commit_fd_uobject(struct ib_uobject *uobj,
333+
enum rdma_remove_reason why)
334+
{
335+
const struct uverbs_obj_fd_type *fd_type =
336+
container_of(uobj->type, struct uverbs_obj_fd_type, type);
337+
struct ib_uobject_file *uobj_file =
338+
container_of(uobj, struct ib_uobject_file, uobj);
339+
int ret = fd_type->context_closed(uobj_file, why);
340+
341+
if (why == RDMA_REMOVE_DESTROY && ret)
342+
return ret;
343+
344+
if (why == RDMA_REMOVE_DURING_CLEANUP) {
345+
alloc_abort_fd_uobject(uobj);
346+
return ret;
347+
}
348+
349+
uobj_file->uobj.context = NULL;
350+
return ret;
351+
}
352+
249353
static void lockdep_check(struct ib_uobject *uobj, bool write)
250354
{
251355
#ifdef CONFIG_LOCKDEP
@@ -314,6 +418,19 @@ static void alloc_commit_idr_uobject(struct ib_uobject *uobj)
314418
spin_unlock(&uobj->context->ufile->idr_lock);
315419
}
316420

421+
static void alloc_commit_fd_uobject(struct ib_uobject *uobj)
422+
{
423+
struct ib_uobject_file *uobj_file =
424+
container_of(uobj, struct ib_uobject_file, uobj);
425+
426+
uverbs_uobject_add(&uobj_file->uobj);
427+
fd_install(uobj_file->uobj.id, uobj->object);
428+
/* This shouldn't be used anymore. Use the file object instead */
429+
uobj_file->uobj.id = 0;
430+
/* Get another reference as we export this to the fops */
431+
uverbs_uobject_get(&uobj_file->uobj);
432+
}
433+
317434
int rdma_alloc_commit_uobject(struct ib_uobject *uobj)
318435
{
319436
/* Cleanup is running. Calling this should have been impossible */
@@ -352,6 +469,15 @@ static void lookup_put_idr_uobject(struct ib_uobject *uobj, bool write)
352469
{
353470
}
354471

472+
static void lookup_put_fd_uobject(struct ib_uobject *uobj, bool write)
473+
{
474+
struct file *filp = uobj->object;
475+
476+
WARN_ON(write);
477+
/* This indirectly calls uverbs_close_fd and free the object */
478+
fput(filp);
479+
}
480+
355481
void rdma_lookup_put_uobject(struct ib_uobject *uobj, bool write)
356482
{
357483
lockdep_check(uobj, write);
@@ -392,6 +518,39 @@ const struct uverbs_obj_type_class uverbs_idr_class = {
392518
.needs_kfree_rcu = true,
393519
};
394520

521+
static void _uverbs_close_fd(struct ib_uobject_file *uobj_file)
522+
{
523+
struct ib_ucontext *ucontext;
524+
struct ib_uverbs_file *ufile = uobj_file->ufile;
525+
int ret;
526+
527+
mutex_lock(&uobj_file->ufile->cleanup_mutex);
528+
529+
/* uobject was either already cleaned up or is cleaned up right now anyway */
530+
if (!uobj_file->uobj.context ||
531+
!down_read_trylock(&uobj_file->uobj.context->cleanup_rwsem))
532+
goto unlock;
533+
534+
ucontext = uobj_file->uobj.context;
535+
ret = _rdma_remove_commit_uobject(&uobj_file->uobj, RDMA_REMOVE_CLOSE,
536+
true);
537+
up_read(&ucontext->cleanup_rwsem);
538+
if (ret)
539+
pr_warn("uverbs: unable to clean up uobject file in uverbs_close_fd.\n");
540+
unlock:
541+
mutex_unlock(&ufile->cleanup_mutex);
542+
}
543+
544+
void uverbs_close_fd(struct file *f)
545+
{
546+
struct ib_uobject_file *uobj_file = f->private_data;
547+
struct kref *uverbs_file_ref = &uobj_file->ufile->ref;
548+
549+
_uverbs_close_fd(uobj_file);
550+
uverbs_uobject_put(&uobj_file->uobj);
551+
kref_put(uverbs_file_ref, ib_uverbs_release_file);
552+
}
553+
395554
void uverbs_cleanup_ucontext(struct ib_ucontext *ucontext, bool device_removed)
396555
{
397556
enum rdma_remove_reason reason = device_removed ?
@@ -412,7 +571,13 @@ void uverbs_cleanup_ucontext(struct ib_ucontext *ucontext, bool device_removed)
412571

413572
/*
414573
* This shouldn't run while executing other commands on this
415-
* context.
574+
* context. Thus, the only thing we should take care of is
575+
* releasing a FD while traversing this list. The FD could be
576+
* closed and released from the _release fop of this FD.
577+
* In order to mitigate this, we add a lock.
578+
* We take and release the lock per order traversal in order
579+
* to let other threads (which might still use the FDs) chance
580+
* to run.
416581
*/
417582
mutex_lock(&ucontext->uobjects_lock);
418583
list_for_each_entry_safe(obj, next_obj, &ucontext->uobjects,
@@ -448,3 +613,13 @@ void uverbs_initialize_ucontext(struct ib_ucontext *ucontext)
448613
init_rwsem(&ucontext->cleanup_rwsem);
449614
}
450615

616+
const struct uverbs_obj_type_class uverbs_fd_class = {
617+
.alloc_begin = alloc_begin_fd_uobject,
618+
.lookup_get = lookup_get_fd_uobject,
619+
.alloc_commit = alloc_commit_fd_uobject,
620+
.alloc_abort = alloc_abort_fd_uobject,
621+
.lookup_put = lookup_put_fd_uobject,
622+
.remove_commit = remove_commit_fd_uobject,
623+
.needs_kfree_rcu = false,
624+
};
625+

drivers/infiniband/core/rdma_core.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ void uverbs_uobject_get(struct ib_uobject *uobject);
6767
*/
6868
void uverbs_uobject_put(struct ib_uobject *uobject);
6969

70+
/* Indicate this fd is no longer used by this consumer, but its memory isn't
71+
* necessarily released yet. When the last reference is put, we release the
72+
* memory. After this call is executed, calling uverbs_uobject_get isn't
73+
* allowed.
74+
* This must be called from the release file_operations of the file!
75+
*/
76+
void uverbs_close_fd(struct file *f);
77+
7078
#endif /* RDMA_CORE_H */

drivers/infiniband/core/uverbs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
193193
struct ib_ucq_object *uobj);
194194
void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
195195
struct ib_uevent_object *uobj);
196+
void ib_uverbs_release_file(struct kref *ref);
196197

197198
void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context);
198199
void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr);

drivers/infiniband/core/uverbs_main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
233233
complete(&dev->comp);
234234
}
235235

236-
static void ib_uverbs_release_file(struct kref *ref)
236+
void ib_uverbs_release_file(struct kref *ref)
237237
{
238238
struct ib_uverbs_file *file =
239239
container_of(ref, struct ib_uverbs_file, ref);
@@ -1132,7 +1132,9 @@ static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
11321132
* (e.g mmput).
11331133
*/
11341134
ib_dev->disassociate_ucontext(ucontext);
1135+
mutex_lock(&file->cleanup_mutex);
11351136
ib_uverbs_cleanup_ucontext(file, ucontext, true);
1137+
mutex_unlock(&file->cleanup_mutex);
11361138
}
11371139

11381140
mutex_lock(&uverbs_dev->lists_mutex);

include/rdma/ib_verbs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,12 @@ struct ib_uobject {
14211421
const struct uverbs_obj_type *type;
14221422
};
14231423

1424+
struct ib_uobject_file {
1425+
struct ib_uobject uobj;
1426+
/* ufile contains the lock between context release and file close */
1427+
struct ib_uverbs_file *ufile;
1428+
};
1429+
14241430
struct ib_udata {
14251431
const void __user *inbuf;
14261432
void __user *outbuf;

include/rdma/uverbs_types.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ void rdma_alloc_abort_uobject(struct ib_uobject *uobj);
129129
int __must_check rdma_remove_commit_uobject(struct ib_uobject *uobj);
130130
int rdma_alloc_commit_uobject(struct ib_uobject *uobj);
131131

132+
struct uverbs_obj_fd_type {
133+
/*
134+
* In fd based objects, uverbs_obj_type_ops points to generic
135+
* fd operations. In order to specialize the underlying types (e.g.
136+
* completion_channel), we use fops, name and flags for fd creation.
137+
* context_closed is called when the context is closed either when
138+
* the driver is removed or the process terminated.
139+
*/
140+
struct uverbs_obj_type type;
141+
int (*context_closed)(struct ib_uobject_file *uobj_file,
142+
enum rdma_remove_reason why);
143+
const struct file_operations *fops;
144+
const char *name;
145+
int flags;
146+
};
147+
132148
extern const struct uverbs_obj_type_class uverbs_idr_class;
133149

134150
#define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \

0 commit comments

Comments
 (0)