Skip to contentSkip to conversation

nvmet-fc: simplify and fix nvmet_fc_alloc_hostport

PATCH In review [email protected] 2 participants 2 messages Mar 3, 2021 – Mar 4, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet-fc: simplify and fix nvmet_fc_alloc_hostport

From: Amit Engel <[email protected]>

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

---
 drivers/nvme/target/fc.c | 45 +++++++++++++++-------------------------
 1 file changed, 17 insertions(+), 28 deletions(-)
drivers/nvme/target/fc.c +17 -28
index d375745fc4ed..debee48080d8 100644
@@ -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;
Signature · 7 lines
2.18.2


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme