net/mlx5: Avoid possible free of command entry while timeout comp handler
authorEran Ben Elisha <eranbe@mellanox.com>
Tue, 4 Aug 2020 07:40:21 +0000 (10:40 +0300)
committerSaeed Mahameed <saeedm@nvidia.com>
Fri, 2 Oct 2020 17:59:54 +0000 (10:59 -0700)
commit50b2412b7e7862c5af0cbf4b10d93bc5c712d021
tree4dffecf021047d22054a51dea6cf028b8211d54c
parent432161ea26d6d5e5c3f7306d9407d26ed1e1953e
net/mlx5: Avoid possible free of command entry while timeout comp handler

Upon command completion timeout, driver simulates a forced command
completion. In a rare case where real interrupt for that command arrives
simultaneously, it might release the command entry while the forced
handler might still access it.

Fix that by adding an entry refcount, to track current amount of allowed
handlers. Command entry to be released only when this refcount is
decremented to zero.

Command refcount is always initialized to one. For callback commands,
command completion handler is the symmetric flow to decrement it. For
non-callback commands, it is wait_func().

Before ringing the doorbell, increment the refcount for the real completion
handler. Once the real completion handler is called, it will decrement it.

For callback commands, once the delayed work is scheduled, increment the
refcount. Upon callback command completion handler, we will try to cancel
the timeout callback. In case of success, we need to decrement the callback
refcount as it will never run.

In addition, gather the entry index free and the entry free into a one
flow for all command types release.

Fixes: e126ba97dba9 ("mlx5: Add driver for Mellanox Connect-IB adapters")
Signed-off-by: Eran Ben Elisha <eranbe@mellanox.com>
Reviewed-by: Moshe Shemesh <moshe@mellanox.com>
Signed-off-by: Saeed Mahameed <saeedm@nvidia.com>
drivers/net/ethernet/mellanox/mlx5/core/cmd.c
include/linux/mlx5/driver.h