nvme-fabrics: error out to unlock the mutex
authorChaitanya Kulkarni <kch@nvidia.com>
Fri, 2 Jun 2023 05:37:13 +0000 (22:37 -0700)
committerKeith Busch <kbusch@kernel.org>
Mon, 12 Jun 2023 17:36:59 +0000 (10:36 -0700)
Currently, in the nvmf_host_add() function, if the nvmf_host_alloc()
call failed to allocate memory for the host, the code would directly
return -ENOMEM without unlocking the nvmf_hosts_mutex. This could
lead to potential issues with mutex synchronization.

Fix that error handling mechanism by jumping to the out_unlock label
when nvmf_host_alloc() fails. This ensures that the mutex is unlocked
before returning the error code. The updated code enhances avoids
possible deadlocks.

Fixes: f0cebf82004d ("nvme-fabrics: prevent overriding of existing host")
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Julia Lawall <julia.lawall@inria.fr>
Closes: https://lore.kernel.org/r/202306020909.MTUEBeIa-lkp@intel.com/
Signed-off-by: Chaitanya Kulkarni <kch@nvidia.com>
Reviewed-by: Julia Lawall <julia.lawall@inria.fr>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/fabrics.c

index b1fa27b6091735dbd5885e53f0400479de6ad881..c4345d1d98aa92385d8bc349693679cb07a48963 100644 (file)
@@ -92,8 +92,10 @@ static struct nvmf_host *nvmf_host_add(const char *hostnqn, uuid_t *id)
        }
 
        host = nvmf_host_alloc(hostnqn, id);
-       if (!host)
-               return ERR_PTR(-ENOMEM);
+       if (!host) {
+               host = ERR_PTR(-ENOMEM);
+               goto out_unlock;
+       }
 
        list_add_tail(&host->list, &nvmf_hosts);
 out_unlock: