1 From 65e1119397a202a42d4448990ba3d20ea0d83d42 Mon Sep 17 00:00:00 2001
2 From: Sasha Levin <sashal@kernel.org>
3 Date: Sun, 5 Dec 2021 12:07:49 +0200
4 Subject: net/mlx5: Set command entry semaphore up once got index free
6 From: Moshe Shemesh <moshe@nvidia.com>
8 [ Upstream commit 8e715cd613a1e872b9d918e912d90b399785761a ]
10 Avoid a race where command work handler may fail to allocate command
11 entry index, by holding the command semaphore down till command entry
14 Fixes: 410bd754cd73 ("net/mlx5: Add retry mechanism to the command entry index allocation")
15 Signed-off-by: Moshe Shemesh <moshe@nvidia.com>
16 Reviewed-by: Eran Ben Elisha <eranbe@nvidia.com>
17 Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
18 Signed-off-by: Sasha Levin <sashal@kernel.org>
20 drivers/net/ethernet/mellanox/mlx5/core/cmd.c | 15 ++++++---------
21 1 file changed, 6 insertions(+), 9 deletions(-)
23 diff --git a/drivers/net/ethernet/mellanox/mlx5/core/cmd.c b/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
24 index 2e55e00888715..20e3f8cd074a1 100644
25 --- a/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
26 +++ b/drivers/net/ethernet/mellanox/mlx5/core/cmd.c
27 @@ -147,8 +147,12 @@ static void cmd_ent_put(struct mlx5_cmd_work_ent *ent)
28 if (!refcount_dec_and_test(&ent->refcnt))
32 - cmd_free_index(ent->cmd, ent->idx);
33 + if (ent->idx >= 0) {
34 + struct mlx5_cmd *cmd = ent->cmd;
36 + cmd_free_index(cmd, ent->idx);
37 + up(ent->page_queue ? &cmd->pages_sem : &cmd->sem);
42 @@ -1582,8 +1586,6 @@ static void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u64 vec, bool force
43 vector = vec & 0xffffffff;
44 for (i = 0; i < (1 << cmd->log_sz); i++) {
45 if (test_bit(i, &vector)) {
46 - struct semaphore *sem;
48 ent = cmd->ent_arr[i];
50 /* if we already completed the command, ignore it */
51 @@ -1606,10 +1608,6 @@ static void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u64 vec, bool force
52 dev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)
55 - if (ent->page_queue)
56 - sem = &cmd->pages_sem;
59 ent->ts2 = ktime_get_ns();
60 memcpy(ent->out->first.data, ent->lay->out, sizeof(ent->lay->out));
61 dump_command(dev, ent, 0);
62 @@ -1663,7 +1661,6 @@ static void mlx5_cmd_comp_handler(struct mlx5_core_dev *dev, u64 vec, bool force