Blob Blame History Raw
From c8e4c23976554fb9dda1658bd1a3914b202815cd Mon Sep 17 00:00:00 2001
From: Bart Van Assche <bvanassche@acm.org>
Date: Thu, 25 Aug 2022 14:38:57 -0700
Subject: [PATCH 1/1] RDMA/srp: Rework the srp_add_port() error path
Git-commit: c8e4c23976554fb9dda1658bd1a3914b202815cd
Patch-mainline: v6.1-rc1
References: git-fixes

device_register() always calls device_initialize() so calling device_del()
is safe even if device_register() fails. Implement the following advice
from the comment block above device_register(): "NOTE: _Never_ directly free
@dev after calling this function, even if it returned an error! Always use
put_device() to give up the reference initialized in this function instead."
Keep the kfree() call in the error path since srp_release_dev() does not
free the host.

Link: https://lore.kernel.org/r/20220825213900.864587-2-bvanassche@acm.org
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Signed-off-by: Leon Romanovsky <leon@kernel.org>
Acked-by: Nicolas Morey-Chaisemartin <nmoreychaisemartin@suse.com>
---
 drivers/infiniband/ulp/srp/ib_srp.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/drivers/infiniband/ulp/srp/ib_srp.c b/drivers/infiniband/ulp/srp/ib_srp.c
index 7720ea270ed8..8fd6a88f7a9c 100644
--- a/drivers/infiniband/ulp/srp/ib_srp.c
+++ b/drivers/infiniband/ulp/srp/ib_srp.c
@@ -3909,20 +3909,19 @@ static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
 		     port);
 
 	if (device_register(&host->dev))
-		goto free_host;
+		goto put_host;
 	if (device_create_file(&host->dev, &dev_attr_add_target))
-		goto err_class;
+		goto put_host;
 	if (device_create_file(&host->dev, &dev_attr_ibdev))
-		goto err_class;
+		goto put_host;
 	if (device_create_file(&host->dev, &dev_attr_port))
-		goto err_class;
+		goto put_host;
 
 	return host;
 
-err_class:
-	device_unregister(&host->dev);
-
-free_host:
+put_host:
+	device_del(&host->dev);
+	put_device(&host->dev);
 	kfree(host);
 
 	return NULL;
-- 
2.38.0.1.gee35aeee4b76