Skip to contentSkip to conversation

nvme-fc: fix reference leak in nvme_fc_nvme_discovery_store()

PATCH Applied Martin George 3 participants 3 messages Oct 23

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

PATCH 1/1 nvme-fc: fix reference leak in nvme_fc_nvme_discovery_store()

nvme_fc_nvme_discovery_store() takes an lport and rport reference before checking whether the rport is already queued on the local discovery list. On the retry path (goto restart), rports that were queued in an earlier pass are skipped by the list_empty() check, but the references taken for them in the current pass are never dropped, leaking one lport and one rport reference per retry.

Check list_empty() first and only take references for rports that are actually added to the local discovery list.

---
 drivers/nvme/host/fc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/nvme/host/fc.c +4 -3
index 48454cb7a0fc..03f6fffc9574 100644
@@ -3778,6 +3778,9 @@ static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
 restart:
 	list_for_each_entry(lport, &nvme_fc_lport_list, port_list) {
 		list_for_each_entry(rport, &lport->endp_list, endp_list) {
+			/* skip rports already queued for discovery */
+			if (!list_empty(&rport->disc_list))
+				continue;
 			if (!nvme_fc_lport_get(lport))
 				continue;
 			if (!nvme_fc_rport_get(rport)) {
@@ -3799,9 +3802,7 @@ static ssize_t nvme_fc_nvme_discovery_store(struct device *dev,
 				       "failures\n");
 				goto process_local_list;
 			}
-			if (list_empty(&rport->disc_list))
-				list_add_tail(&rport->disc_list,
-					      &local_disc_list);
+			list_add_tail(&rport->disc_list, &local_disc_list);
 		}
 	}
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 48 lines · Martin George

nvme_fc_nvme_discovery_store() takes an lport and rport reference before
checking whether the rport is already queued on the local discovery list. On the retry path (goto restart), rports that were queued in an earlier pass are skipped by the list_empty() check, but the references
taken for them in the current pass are never dropped, leaking one lport
and one rport reference per retry.

Check list_empty() first and only take references for rports that are actually added to the local discovery list.

---
 drivers/nvme/host/fc.c | 7 ++++---

 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/nvme/host/fc.c +0 -0
index 48454cb7a0fc..03f6fffc9574 100644
@@ -3778,6 +3778,9 @@ static ssize_t
 nvme_fc_nvme_discovery_store(struct device *dev,

 restart:
  list_for_each_entry(lport, &nvme_fc_lport_list, port_list) { list_for_each_entry(rport, &lport->endp_list, endp_list) {

drivers/nvme/host/fc.c +1 -0
+			/* skip rports already queued for discovery

*/

drivers/nvme/host/fc.c +2 -0
+			if (!list_empty(&rport->disc_list))
+				continue;

  if (!nvme_fc_lport_get(lport)) continue;
  if (!nvme_fc_rport_get(rport)) {

drivers/nvme/host/fc.c +1 -3
@@ -3799,9 +3802,7 @@ static ssize_t
 nvme_fc_nvme_discovery_store(struct device *dev,
  				       "failures\n");
  				goto process_local_list;
  			}
-			if (list_empty(&rport->disc_list))
-				list_add_tail(&rport->disc_list,
-					      &local_disc_list);
+			list_add_tail(&rport->disc_list,
 &local_disc_list);
  		}

  }

Quoted 1 line

This looks good to me.

maintainer Applied
#2 lore raw
In reply to Martin George
Quoted 9 lines · Martin George

nvme_fc_nvme_discovery_store() takes an lport and rport reference before checking whether the rport is already queued on the local discovery list. On the retry path (goto restart), rports that were queued in an earlier pass are skipped by the list_empty() check, but the references taken for them in the current pass are never dropped, leaking one lport and one rport reference per retry.

Check list_empty() first and only take references for rports that are actually added to the local discovery list.

Thanks, applied to nvme-7.4.