Skip to contentSkip to conversation

Fixing libfc memory leaks

PATCH RESEND Applied Igor Druzhinin 3 participants 7 messages Jan 14, 2020 – Feb 21, 2020

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

cover Fixing libfc memory leaks

Hi Hannes,

Could you take a look at those?
At least the first one causes noticeable memory decline over time for some of our deployments.

Igor Druzhinin (2):
scsi: libfc: free response frame from GPN_ID
scsi: libfc: drop extra rport reference in fc_rport_create()

 drivers/scsi/libfc/fc_disc.c  | 2 ++
 drivers/scsi/libfc/fc_rport.c | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH RESEND 1/2 scsi: libfc: free response frame from GPN_ID

fc_disc_gpn_id_resp() should be the last function using it so free it here to avoid memory leak.

---
 drivers/scsi/libfc/fc_disc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/libfc/fc_disc.c +2 -0
index 9c5f7c9..2b865c6 100644
@@ -628,6 +628,8 @@ static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
 	}
 out:
 	kref_put(&rdata->kref, fc_rport_destroy);
+	if (!IS_ERR(fp))
+		fc_frame_free(fp);
 }
 
 /**
Signature · 1 line
2.7.4
maintainer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH RESEND 1/2] scsi: libfc: free response frame from GPN_ID

Quoted 14 lines · Igor Druzhinin

fc_disc_gpn_id_resp() should be the last function using it so free it here to avoid memory leak.

---
 drivers/scsi/libfc/fc_disc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/libfc/fc_disc.c +0 -0
index 9c5f7c9..2b865c6 100644

drivers/scsi/libfc/fc_disc.c static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp, · line 635

Quoted 8 lines
drivers/scsi/libfc/fc_disc.c +2 -0
@@ -628,6 +628,8 @@ static void fc_disc_gpn_id_resp(struct fc_seq *sp, struct fc_frame *fp,
 	}
 out:
 	kref_put(&rdata->kref, fc_rport_destroy);
+	if (!IS_ERR(fp))
+		fc_frame_free(fp);
 }
 
 /**

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		           Kernel Storage Architect
[email protected]			                  +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
#3 lore raw

Re: [PATCH RESEND 1/2] scsi: libfc: free response frame from GPN_ID

In reply to Igor Druzhinin

Igor,

Quoted 2 lines

fc_disc_gpn_id_resp() should be the last function using it so free it here to avoid memory leak.

Applied to 5.6/scsi-fixes, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
#4 lore raw

PATCH RESEND 2/2 scsi: libfc: drop extra rport reference in fc_rport_create()

In reply to Igor Druzhinin

The callers of this function seem to assume the reference is not taken in case rport already exists. This results in one extra reference taken on each rport re-discovery that will eventually get to inability to free rport structure on port removal.

---
 drivers/scsi/libfc/fc_rport.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/libfc/fc_rport.c +3 -1
index da6e97d..a43f9dd 100644
@@ -133,8 +133,10 @@ struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
 	lockdep_assert_held(&lport->disc.disc_mutex);
 
 	rdata = fc_rport_lookup(lport, port_id);
-	if (rdata)
+	if (rdata) {
+		kref_put(&rdata->kref, fc_rport_destroy);
 		return rdata;
+	}
 
 	if (lport->rport_priv_size > 0)
 		rport_priv_size = lport->rport_priv_size;
Signature · 1 line
2.7.4
maintainer NAK
#5 lore raw

Re: [PATCH RESEND 2/2] scsi: libfc: drop extra rport reference in fc_rport_create()

Quoted 17 lines · Igor Druzhinin

The callers of this function seem to assume the reference is not taken in case rport already exists. This results in one extra reference taken on each rport re-discovery that will eventually get to inability to free rport structure on port removal.

---
 drivers/scsi/libfc/fc_rport.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/scsi/libfc/fc_rport.c +0 -0
index da6e97d..a43f9dd 100644
@@ -133,8 +133,10 @@ struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id)
 	lockdep_assert_held(&lport->disc.disc_mutex);

drivers/scsi/libfc/fc_rport.c struct fc_rport_priv *fc_rport_create(struct fc_lport *lport, u32 port_id) · line 142

Quoted 10 lines
drivers/scsi/libfc/fc_rport.c +3 -1
 
 	rdata = fc_rport_lookup(lport, port_id);
-	if (rdata)
+	if (rdata) {
+		kref_put(&rdata->kref, fc_rport_destroy);
 		return rdata;
+	}
 
 	if (lport->rport_priv_size > 0)
 		rport_priv_size = lport->rport_priv_size;

NAK.
The caller _does_ assume that a reference is taken once fc_rport_create() returns non-NULL.
And the caller is responsible to drop the reference once 'rdatat' isn't used anymore.
Any other usage is an error, but should be fixed in the caller, not here.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke		           Kernel Storage Architect
[email protected]			                  +49 911 74053 688
SUSE Software Solutions Germany GmbH, Maxfeldstr. 5, 90409 Nürnberg
HRB 36809 (AG Nürnberg), GF: Felix Imendörffer
#6 lore raw
In reply to Igor Druzhinin

Hannes,

Quoted 11 lines

Could you take a look at those?
At least the first one causes noticeable memory decline over time for some of our deployments.

Igor Druzhinin (2):
scsi: libfc: free response frame from GPN_ID
scsi: libfc: drop extra rport reference in fc_rport_create()

 drivers/scsi/libfc/fc_disc.c  | 2 ++
 drivers/scsi/libfc/fc_rport.c | 4 +++-
 2 files changed, 5 insertions(+), 1 deletion(-)

Please review!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering