Commit 95f76fb6 authored by Jack Morgenstein's avatar Jack Morgenstein Committed by Ben Hutchings

net/mlx4_core: Fix deadlock when switching between polling and event fw commands

commit a7e1f049 upstream.

When switching from polling-based fw commands to event-based fw
commands, there is a race condition which could cause a fw command
in another task to hang: that task will keep waiting for the polling
sempahore, but may never be able to acquire it. This is due to
mlx4_cmd_use_events, which "down"s the sempahore back to 0.

During driver initialization, this is not a problem, since no other
tasks which invoke FW commands are active.

However, there is a problem if the driver switches to polling mode
and then back to event mode during normal operation.

The "test_interrupts" feature does exactly that.
Running "ethtool -t <eth device> offline" causes the PF driver to
temporarily switch to polling mode, and then back to event mode.
(Note that for VF drivers, such switching is not performed).

Fix this by adding a read-write semaphore for protection when
switching between modes.

Fixes: 225c7b1f ("IB/mlx4: Add a driver Mellanox ConnectX InfiniBand adapters")
Signed-off-by: default avatarJack Morgenstein <jackm@dev.mellanox.co.il>
Signed-off-by: default avatarMatan Barak <matanb@mellanox.com>
Signed-off-by: default avatarTariq Toukan <tariqt@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
[bwh: Backported to 3.16: adjust context, indentation]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent b8f01176
...@@ -606,14 +606,20 @@ int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param, ...@@ -606,14 +606,20 @@ int __mlx4_cmd(struct mlx4_dev *dev, u64 in_param, u64 *out_param,
return -EIO; return -EIO;
if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) { if (!mlx4_is_mfunc(dev) || (native && mlx4_is_master(dev))) {
int ret;
down_read(&mlx4_priv(dev)->cmd.switch_sem);
if (mlx4_priv(dev)->cmd.use_events) if (mlx4_priv(dev)->cmd.use_events)
return mlx4_cmd_wait(dev, in_param, out_param, ret = mlx4_cmd_wait(dev, in_param, out_param,
out_is_imm, in_modifier, out_is_imm, in_modifier,
op_modifier, op, timeout); op_modifier, op, timeout);
else else
return mlx4_cmd_poll(dev, in_param, out_param, ret = mlx4_cmd_poll(dev, in_param, out_param,
out_is_imm, in_modifier, out_is_imm, in_modifier,
op_modifier, op, timeout); op_modifier, op, timeout);
up_read(&mlx4_priv(dev)->cmd.switch_sem);
return ret;
} }
return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm, return mlx4_slave_cmd(dev, in_param, out_param, out_is_imm,
in_modifier, op_modifier, op, timeout); in_modifier, op_modifier, op, timeout);
...@@ -2092,6 +2098,7 @@ int mlx4_cmd_init(struct mlx4_dev *dev) ...@@ -2092,6 +2098,7 @@ int mlx4_cmd_init(struct mlx4_dev *dev)
{ {
struct mlx4_priv *priv = mlx4_priv(dev); struct mlx4_priv *priv = mlx4_priv(dev);
init_rwsem(&priv->cmd.switch_sem);
mutex_init(&priv->cmd.hcr_mutex); mutex_init(&priv->cmd.hcr_mutex);
mutex_init(&priv->cmd.slave_cmd_mutex); mutex_init(&priv->cmd.slave_cmd_mutex);
sema_init(&priv->cmd.poll_sem, 1); sema_init(&priv->cmd.poll_sem, 1);
...@@ -2188,6 +2195,7 @@ int mlx4_cmd_use_events(struct mlx4_dev *dev) ...@@ -2188,6 +2195,7 @@ int mlx4_cmd_use_events(struct mlx4_dev *dev)
if (!priv->cmd.context) if (!priv->cmd.context)
return -ENOMEM; return -ENOMEM;
down_write(&priv->cmd.switch_sem);
for (i = 0; i < priv->cmd.max_cmds; ++i) { for (i = 0; i < priv->cmd.max_cmds; ++i) {
priv->cmd.context[i].token = i; priv->cmd.context[i].token = i;
priv->cmd.context[i].next = i + 1; priv->cmd.context[i].next = i + 1;
...@@ -2207,6 +2215,7 @@ int mlx4_cmd_use_events(struct mlx4_dev *dev) ...@@ -2207,6 +2215,7 @@ int mlx4_cmd_use_events(struct mlx4_dev *dev)
down(&priv->cmd.poll_sem); down(&priv->cmd.poll_sem);
priv->cmd.use_events = 1; priv->cmd.use_events = 1;
up_write(&priv->cmd.switch_sem);
return err; return err;
} }
...@@ -2219,6 +2228,7 @@ void mlx4_cmd_use_polling(struct mlx4_dev *dev) ...@@ -2219,6 +2228,7 @@ void mlx4_cmd_use_polling(struct mlx4_dev *dev)
struct mlx4_priv *priv = mlx4_priv(dev); struct mlx4_priv *priv = mlx4_priv(dev);
int i; int i;
down_write(&priv->cmd.switch_sem);
priv->cmd.use_events = 0; priv->cmd.use_events = 0;
for (i = 0; i < priv->cmd.max_cmds; ++i) for (i = 0; i < priv->cmd.max_cmds; ++i)
...@@ -2227,6 +2237,7 @@ void mlx4_cmd_use_polling(struct mlx4_dev *dev) ...@@ -2227,6 +2237,7 @@ void mlx4_cmd_use_polling(struct mlx4_dev *dev)
kfree(priv->cmd.context); kfree(priv->cmd.context);
up(&priv->cmd.poll_sem); up(&priv->cmd.poll_sem);
up_write(&priv->cmd.switch_sem);
} }
struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev) struct mlx4_cmd_mailbox *mlx4_alloc_cmd_mailbox(struct mlx4_dev *dev)
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include <linux/timer.h> #include <linux/timer.h>
#include <linux/semaphore.h> #include <linux/semaphore.h>
#include <linux/workqueue.h> #include <linux/workqueue.h>
#include <linux/rwsem.h>
#include <linux/mlx4/device.h> #include <linux/mlx4/device.h>
#include <linux/mlx4/driver.h> #include <linux/mlx4/driver.h>
...@@ -598,6 +599,7 @@ struct mlx4_cmd { ...@@ -598,6 +599,7 @@ struct mlx4_cmd {
struct mutex slave_cmd_mutex; struct mutex slave_cmd_mutex;
struct semaphore poll_sem; struct semaphore poll_sem;
struct semaphore event_sem; struct semaphore event_sem;
struct rw_semaphore switch_sem;
int max_cmds; int max_cmds;
spinlock_t context_lock; spinlock_t context_lock;
int free_head; int free_head;
......
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