Commit 09e328e4 authored by Jason Gunthorpe's avatar Jason Gunthorpe

RDMA/ucma: Fix the locking of ctx->file

ctx->file is changed under the file->mut lock by ucma_migrate_id(), which
is impossible to lock correctly. Instead change ctx->file under the
handler_lock and ctx_table lock and revise all places touching ctx->file
to use this locking when reading ctx->file.

Link: https://lore.kernel.org/r/20200818120526.702120-9-leon@kernel.orgSigned-off-by: default avatarLeon Romanovsky <leonro@mellanox.com>
Signed-off-by: default avatarJason Gunthorpe <jgg@nvidia.com>
parent 308571de
...@@ -547,6 +547,7 @@ static void ucma_cleanup_mc_events(struct ucma_multicast *mc) ...@@ -547,6 +547,7 @@ static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
{ {
struct ucma_event *uevent, *tmp; struct ucma_event *uevent, *tmp;
rdma_lock_handler(mc->ctx->cm_id);
mutex_lock(&mc->ctx->file->mut); mutex_lock(&mc->ctx->file->mut);
list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) { list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
if (uevent->mc != mc) if (uevent->mc != mc)
...@@ -556,6 +557,7 @@ static void ucma_cleanup_mc_events(struct ucma_multicast *mc) ...@@ -556,6 +557,7 @@ static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
kfree(uevent); kfree(uevent);
} }
mutex_unlock(&mc->ctx->file->mut); mutex_unlock(&mc->ctx->file->mut);
rdma_unlock_handler(mc->ctx->cm_id);
} }
/* /*
...@@ -1600,7 +1602,7 @@ static ssize_t ucma_leave_multicast(struct ucma_file *file, ...@@ -1600,7 +1602,7 @@ static ssize_t ucma_leave_multicast(struct ucma_file *file,
mc = xa_load(&multicast_table, cmd.id); mc = xa_load(&multicast_table, cmd.id);
if (!mc) if (!mc)
mc = ERR_PTR(-ENOENT); mc = ERR_PTR(-ENOENT);
else if (mc->ctx->file != file) else if (READ_ONCE(mc->ctx->file) != file)
mc = ERR_PTR(-EINVAL); mc = ERR_PTR(-EINVAL);
else if (!refcount_inc_not_zero(&mc->ctx->ref)) else if (!refcount_inc_not_zero(&mc->ctx->ref))
mc = ERR_PTR(-ENXIO); mc = ERR_PTR(-ENXIO);
...@@ -1692,6 +1694,7 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file, ...@@ -1692,6 +1694,7 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
goto file_put; goto file_put;
} }
rdma_lock_handler(ctx->cm_id);
cur_file = ctx->file; cur_file = ctx->file;
if (cur_file == new_file) { if (cur_file == new_file) {
resp.events_reported = ctx->events_reported; resp.events_reported = ctx->events_reported;
...@@ -1718,6 +1721,7 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file, ...@@ -1718,6 +1721,7 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
&resp, sizeof(resp))) &resp, sizeof(resp)))
ret = -EFAULT; ret = -EFAULT;
rdma_unlock_handler(ctx->cm_id);
ucma_put_ctx(ctx); ucma_put_ctx(ctx);
file_put: file_put:
fdput(f); fdput(f);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment