[PATCH] nvmet-fc: simplify and fix nvmet_fc_alloc_hostport

amit.engel at dell.com amit.engel at dell.com
Thu Mar 4 08:12:15 GMT 2021


From: Amit Engel <amit.engel at dell.com>

Alloc newhost only if no host has been found in host_list
Add a missing tgtport release reference

Signed-off-by: Amit Engel <amit.engel at dell.com>
---
 drivers/nvme/target/fc.c | 45 +++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/drivers/nvme/target/fc.c b/drivers/nvme/target/fc.c
index d375745fc4ed..debee48080d8 100644
--- a/drivers/nvme/target/fc.c
+++ b/drivers/nvme/target/fc.c
@@ -1034,28 +1034,6 @@ nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
 	if (!nvmet_fc_tgtport_get(tgtport))
 		return ERR_PTR(-EINVAL);
 
-	newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
-	if (!newhost) {
-		spin_lock_irqsave(&tgtport->lock, flags);
-		list_for_each_entry(host, &tgtport->host_list, host_list) {
-			if (host->hosthandle == hosthandle && !host->invalid) {
-				if (nvmet_fc_hostport_get(host)) {
-					match = host;
-					break;
-				}
-			}
-		}
-		spin_unlock_irqrestore(&tgtport->lock, flags);
-		/* no allocation - release reference */
-		nvmet_fc_tgtport_put(tgtport);
-		return (match) ? match : ERR_PTR(-ENOMEM);
-	}
-
-	newhost->tgtport = tgtport;
-	newhost->hosthandle = hosthandle;
-	INIT_LIST_HEAD(&newhost->host_list);
-	kref_init(&newhost->ref);
-
 	spin_lock_irqsave(&tgtport->lock, flags);
 	list_for_each_entry(host, &tgtport->host_list, host_list) {
 		if (host->hosthandle == hosthandle && !host->invalid) {
@@ -1065,13 +1043,24 @@ nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
 			}
 		}
 	}
-	if (match) {
-		kfree(newhost);
-		newhost = NULL;
-		/* releasing allocation - release reference */
-		nvmet_fc_tgtport_put(tgtport);
-	} else
+	if (!match) {
+		newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
+		if (!newhost) {
+			/* no allocation - release reference */
+			nvmet_fc_tgtport_put(tgtport);
+			spin_unlock_irqrestore(&tgtport->lock, flags);
+			return ERR_PTR(-ENOMEM);
+		}
+
+		newhost->tgtport = tgtport;
+		newhost->hosthandle = hosthandle;
+		INIT_LIST_HEAD(&newhost->host_list);
+		kref_init(&newhost->ref);
+
 		list_add_tail(&newhost->host_list, &tgtport->host_list);
+	}
+	/* release reference */
+	nvmet_fc_tgtport_put(tgtport);
 	spin_unlock_irqrestore(&tgtport->lock, flags);
 
 	return (match) ? match : newhost;
-- 
2.18.2




More information about the Linux-nvme mailing list