]> www.infradead.org Git - nvme.git/commitdiff
nvmet-fc: take tgtport reference only once
authorDaniel Wagner <wagi@kernel.org>
Tue, 8 Apr 2025 15:29:09 +0000 (17:29 +0200)
committerChristoph Hellwig <hch@lst.de>
Wed, 9 Apr 2025 11:03:56 +0000 (13:03 +0200)
The reference counting code can be simplified. Instead taking a tgtport
refrerence at the beginning of nvmet_fc_alloc_hostport and put it back
if not a new hostport object is allocated, only take it when a new
hostport object is allocated.

Signed-off-by: Daniel Wagner <wagi@kernel.org>
Reviewed-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/target/fc.c

index 42613280c06e82a0236520d93470ec6fdede37ea..61e9eea3bee430bfdef541bfe0d1f2538a49d9eb 100644 (file)
@@ -1018,33 +1018,24 @@ nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
        struct nvmet_fc_hostport *newhost, *match = NULL;
        unsigned long flags;
 
+       /*
+        * Caller holds a reference on tgtport.
+        */
+
        /* if LLDD not implemented, leave as NULL */
        if (!hosthandle)
                return NULL;
 
-       /*
-        * take reference for what will be the newly allocated hostport if
-        * we end up using a new allocation
-        */
-       if (!nvmet_fc_tgtport_get(tgtport))
-               return ERR_PTR(-EINVAL);
-
        spin_lock_irqsave(&tgtport->lock, flags);
        match = nvmet_fc_match_hostport(tgtport, hosthandle);
        spin_unlock_irqrestore(&tgtport->lock, flags);
 
-       if (match) {
-               /* no new allocation - release reference */
-               nvmet_fc_tgtport_put(tgtport);
+       if (match)
                return match;
-       }
 
        newhost = kzalloc(sizeof(*newhost), GFP_KERNEL);
-       if (!newhost) {
-               /* no new allocation - release reference */
-               nvmet_fc_tgtport_put(tgtport);
+       if (!newhost)
                return ERR_PTR(-ENOMEM);
-       }
 
        spin_lock_irqsave(&tgtport->lock, flags);
        match = nvmet_fc_match_hostport(tgtport, hosthandle);
@@ -1053,6 +1044,7 @@ nvmet_fc_alloc_hostport(struct nvmet_fc_tgtport *tgtport, void *hosthandle)
                kfree(newhost);
                newhost = match;
        } else {
+               nvmet_fc_tgtport_get(tgtport);
                newhost->tgtport = tgtport;
                newhost->hosthandle = hosthandle;
                INIT_LIST_HEAD(&newhost->host_list);