fd_type =
container_of(obj->type_attrs, struct uverbs_obj_fd_type, type);
- if (WARN_ON(fd_type->fops && fd_type->fops->release != &uverbs_uobject_fd_release &&
- fd_type->fops->release != &uverbs_async_event_release)) {
+ if (WARN_ON(fd_type->fops &&
+ fd_type->fops->release != &uverbs_uobject_fd_release)) {
ret = ERR_PTR(-EINVAL);
goto err_fd;
}
*/
int uverbs_uobject_fd_release(struct inode *inode, struct file *filp)
{
+ void (*release_cleanup)(struct ib_uobject *uobj) = NULL;
+ struct ib_uobject *uobj = filp->private_data;
+ const struct uverbs_obj_type *type_attrs;
+ int ret;
+
/*
* This can only happen if the fput came from alloc_abort_fd_uobject()
*/
- if (!filp->private_data)
+ if (!uobj)
return 0;
- return uverbs_uobject_release(filp->private_data);
+ /*
+ * uverbs_disassociate_api() can NULL type_attrs after disassociate, but
+ * it won't if release_cleanup is used.
+ */
+ type_attrs = READ_ONCE(uobj->uapi_object->type_attrs);
+ if (type_attrs)
+ release_cleanup = container_of(type_attrs,
+ struct uverbs_obj_fd_type, type)
+ ->release_cleanup;
+ if (release_cleanup)
+ uverbs_uobject_get(uobj);
+
+ ret = uverbs_uobject_release(uobj);
+
+ if (release_cleanup) {
+ release_cleanup(uobj);
+ uverbs_uobject_put(uobj);
+ }
+
+ return ret;
}
EXPORT_SYMBOL(uverbs_uobject_fd_release);
void ib_uverbs_init_async_event_file(struct ib_uverbs_async_event_file *ev_file);
void ib_uverbs_free_event_queue(struct ib_uverbs_event_queue *event_queue);
void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res);
-int uverbs_async_event_release(struct inode *inode, struct file *filp);
int ib_alloc_ucontext(struct uverbs_attr_bundle *attrs);
int ib_init_ucontext(struct uverbs_attr_bundle *attrs);
.owner = THIS_MODULE,
.read = ib_uverbs_async_event_read,
.poll = ib_uverbs_async_event_poll,
- .release = uverbs_async_event_release,
+ .release = uverbs_uobject_fd_release,
.fasync = ib_uverbs_async_event_fasync,
};
NULL, NULL);
}
-int uverbs_async_event_release(struct inode *inode, struct file *filp)
+static void uverbs_async_event_free_event_queue(struct ib_uobject *uobj)
{
struct ib_uverbs_async_event_file *event_file;
- struct ib_uobject *uobj = filp->private_data;
- int ret;
-
- if (!uobj)
- return uverbs_uobject_fd_release(inode, filp);
event_file =
container_of(uobj, struct ib_uverbs_async_event_file, uobj);
* release. The user knows it has reached the end of the event stream
* when it sees IB_EVENT_DEVICE_FATAL.
*/
- uverbs_uobject_get(uobj);
- ret = uverbs_uobject_fd_release(inode, filp);
ib_uverbs_free_event_queue(&event_file->ev_queue);
- uverbs_uobject_put(uobj);
- return ret;
}
DECLARE_UVERBS_NAMED_METHOD(
DECLARE_UVERBS_NAMED_OBJECT(
UVERBS_OBJECT_ASYNC_EVENT,
- UVERBS_TYPE_ALLOC_FD(sizeof(struct ib_uverbs_async_event_file),
- uverbs_async_event_destroy_uobj,
- &uverbs_async_event_fops,
- "[infinibandevent]",
- O_RDONLY),
+ UVERBS_TYPE_ALLOC_FD_RELEASE(sizeof(struct ib_uverbs_async_event_file),
+ uverbs_async_event_destroy_uobj,
+ uverbs_async_event_free_event_queue,
+ &uverbs_async_event_fops,
+ "[infinibandevent]",
+ O_RDONLY),
&UVERBS_METHOD(UVERBS_METHOD_ASYNC_EVENT_ALLOC));
const struct uapi_definition uverbs_def_obj_async_fd[] = {
if (uapi_key_is_object(iter.index)) {
struct uverbs_api_object *object_elm =
rcu_dereference_protected(*slot, true);
+ const struct uverbs_obj_type *type_attrs =
+ object_elm->type_attrs;
/*
* Some type_attrs are in the driver module. We don't
* bother to keep track of which since there should be
* no use of this after disassociate.
+ *
+ * release_cleanup is the exception because
+ * uverbs_uobject_fd_release() needs it. In this case
+ * the module reference held by the fops will guarentee
+ * the type_class remains valid too.
*/
+ if (type_attrs &&
+ type_attrs->type_class == &uverbs_fd_class &&
+ container_of(type_attrs, struct uverbs_obj_fd_type,
+ type)->release_cleanup)
+ continue;
+
object_elm->type_attrs = NULL;
} else if (uapi_key_is_attr(iter.index)) {
struct uverbs_api_attr *elm =
struct uverbs_obj_type type;
void (*destroy_object)(struct ib_uobject *uobj,
enum rdma_remove_reason why);
+ void (*release_cleanup)(struct ib_uobject *uobj);
const struct file_operations *fops;
const char *name;
int flags;
#define UVERBS_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - \
sizeof(char))
-#define UVERBS_TYPE_ALLOC_FD(_obj_size, _destroy_object, _fops, _name, _flags) \
+#define UVERBS_TYPE_ALLOC_FD_RELEASE(_obj_size, _destroy_object, \
+ _release_cleanup, _fops, _name, _flags) \
((&((const struct uverbs_obj_fd_type) \
{.type = { \
.type_class = &uverbs_fd_class, \
sizeof(struct ib_uobject)), \
}, \
.destroy_object = _destroy_object, \
+ .release_cleanup = _release_cleanup, \
.fops = _fops, \
.name = _name, \
.flags = _flags}))->type)
+#define UVERBS_TYPE_ALLOC_FD(_obj_size, _destroy_object, _fops, _name, _flags) \
+ UVERBS_TYPE_ALLOC_FD_RELEASE(_obj_size, _destroy_object, NULL, \
+ _fops, _name, _flags)
#define UVERBS_TYPE_ALLOC_IDR_SZ(_size, _destroy_object) \
((&((const struct uverbs_obj_idr_type) \
{.type = { \