Commit 2f76e829 authored by Roland Dreier's avatar Roland Dreier

[IB] umad: avoid potential deadlock when unregistering MAD agents

ib_unregister_mad_agent() completes all pending MAD sends and waits
for the agent's send_handler routine to return.  umad's send_handler()
calls queue_packet(), which does down_read() on the port mutex to look
up the agent ID.  This means that the port mutex cannot be held for
writing while calling ib_unregister_mad_agent(), or else it will
deadlock.  This patch fixes all the calls to ib_unregister_mad_agent()
in the umad module to avoid this deadlock.
Signed-off-by: default avatarRoland Dreier <rolandd@cisco.com>
parent 1732b0ef
...@@ -505,8 +505,6 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) ...@@ -505,8 +505,6 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
goto out; goto out;
} }
file->agent[agent_id] = agent;
file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE); file->mr[agent_id] = ib_get_dma_mr(agent->qp->pd, IB_ACCESS_LOCAL_WRITE);
if (IS_ERR(file->mr[agent_id])) { if (IS_ERR(file->mr[agent_id])) {
ret = -ENOMEM; ret = -ENOMEM;
...@@ -519,14 +517,15 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) ...@@ -519,14 +517,15 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
goto err_mr; goto err_mr;
} }
file->agent[agent_id] = agent;
ret = 0; ret = 0;
goto out; goto out;
err_mr: err_mr:
ib_dereg_mr(file->mr[agent_id]); ib_dereg_mr(file->mr[agent_id]);
err: err:
file->agent[agent_id] = NULL;
ib_unregister_mad_agent(agent); ib_unregister_mad_agent(agent);
out: out:
...@@ -536,27 +535,33 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg) ...@@ -536,27 +535,33 @@ static int ib_umad_reg_agent(struct ib_umad_file *file, unsigned long arg)
static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg) static int ib_umad_unreg_agent(struct ib_umad_file *file, unsigned long arg)
{ {
struct ib_mad_agent *agent = NULL;
struct ib_mr *mr = NULL;
u32 id; u32 id;
int ret = 0; int ret = 0;
down_write(&file->port->mutex); if (get_user(id, (u32 __user *) arg))
return -EFAULT;
if (get_user(id, (u32 __user *) arg)) { down_write(&file->port->mutex);
ret = -EFAULT;
goto out;
}
if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) { if (id < 0 || id >= IB_UMAD_MAX_AGENTS || !file->agent[id]) {
ret = -EINVAL; ret = -EINVAL;
goto out; goto out;
} }
ib_dereg_mr(file->mr[id]); agent = file->agent[id];
ib_unregister_mad_agent(file->agent[id]); mr = file->mr[id];
file->agent[id] = NULL; file->agent[id] = NULL;
out: out:
up_write(&file->port->mutex); up_write(&file->port->mutex);
if (agent) {
ib_unregister_mad_agent(agent);
ib_dereg_mr(mr);
}
return ret; return ret;
} }
...@@ -623,16 +628,16 @@ static int ib_umad_close(struct inode *inode, struct file *filp) ...@@ -623,16 +628,16 @@ static int ib_umad_close(struct inode *inode, struct file *filp)
struct ib_umad_packet *packet, *tmp; struct ib_umad_packet *packet, *tmp;
int i; int i;
down_write(&file->port->mutex);
for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i) for (i = 0; i < IB_UMAD_MAX_AGENTS; ++i)
if (file->agent[i]) { if (file->agent[i]) {
ib_dereg_mr(file->mr[i]);
ib_unregister_mad_agent(file->agent[i]); ib_unregister_mad_agent(file->agent[i]);
ib_dereg_mr(file->mr[i]);
} }
list_for_each_entry_safe(packet, tmp, &file->recv_list, list) list_for_each_entry_safe(packet, tmp, &file->recv_list, list)
kfree(packet); kfree(packet);
down_write(&file->port->mutex);
list_del(&file->port_list); list_del(&file->port_list);
up_write(&file->port->mutex); up_write(&file->port->mutex);
......
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