Skip to contentSkip to conversation

sata: ahsata: Fix resource leak

PATCH New Peng Fan 3 participants 12 messages May 3, 2020 – May 8, 2020 libata

lore
Share & tools
lore

Peng Fan

author
#0 lore raw

PATCH 1/4 sata: ahsata: Fix resource leak

From: Ye Li <[email protected]>

Fix coverity issue CID 3606684: Resource leak (RESOURCE_LEAK)
leaked_storage: Variable uc_priv going out of scope leaks the storage it points to

---
 drivers/ata/dwc_ahsata.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/ata/dwc_ahsata.c +5 -0
index c2e28fe518..a775214792 100644
@@ -847,6 +847,9 @@ static int ahci_init_one(int pdev)
 	struct ahci_uc_priv *uc_priv = NULL;
 
 	uc_priv = malloc(sizeof(struct ahci_uc_priv));
+	if (!uc_priv)
+		return -ENOMEM;
+
 	memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
 	uc_priv->dev = pdev;
 
@@ -871,6 +874,8 @@ static int ahci_init_one(int pdev)
 	return 0;
 
 err_out:
+	if (uc_priv)
+		free(uc_priv);
 	return rc;
 }
 
Signature · 1 line
2.16.4
author
#1 lore raw

PATCH 2/4 sata: ahsata: Fix wrong operand for checking SERR DIAG_X

From: Ye Li <[email protected]>

Fix coverity issue CID 3261683: Wrong operator used (CONSTANT_EXPRESSION_RESULT) operator_confusion:
({...; __v;}) | 67108864 is always 1/true regardless of the values of its operand. This occurs as the logical operand of !

When DIAG_X is set, the PHY COMINIT signal is detected, so should use '&' to check whether it is set.

---
 drivers/ata/dwc_ahsata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/ata/dwc_ahsata.c +1 -1
index a775214792..4b37a02338 100644
@@ -223,7 +223,7 @@ static int ahci_host_init(struct ahci_uc_priv *uc_priv)
 
 		/* Wait for COMINIT bit 26 (DIAG_X) in SERR */
 		timeout = 1000;
-		while (!(readl(&port_mmio->serr) | SATA_PORT_SERR_DIAG_X)
+		while (!(readl(&port_mmio->serr) & SATA_PORT_SERR_DIAG_X)
 			&& --timeout)
 			;
 		if (timeout <= 0) {
Signature · 1 line
2.16.4
#2 lore raw

PATCH 2/4 sata: ahsata: Fix wrong operand for checking SERR DIAG_X

Quoted 8 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 3261683: Wrong operator used (CONSTANT_EXPRESSION_RESULT) operator_confusion:
({...; __v;}) | 67108864 is always 1/true regardless of the values of its operand. This occurs as the logical operand of !

Quoted 8 lines

When DIAG_X is set, the PHY COMINIT signal is detected, so should use '&' to check whether it is set.

---
 drivers/ata/dwc_ahsata.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw

PATCH 2/4 sata: ahsata: Fix wrong operand for checking SERR DIAG_X

In reply to Peng Fan
Quoted 7 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 3261683: Wrong operator used (CONSTANT_EXPRESSION_RESULT) operator_confusion:
({...; __v;}) | 67108864 is always 1/true regardless of the values of its operand. This occurs as the logical operand of !

Quoted 6 lines

When DIAG_X is set, the PHY COMINIT signal is detected, so should use '&' to check whether it is set.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200508/631fdc45/attachment.sig>
author
#4 lore raw

PATCH 3/4 sata: dwc_ahsata: Fix incorrect free

In reply to Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 43665: Free of address-of expression (BAD_FREE)
incorrect_free: free frees incorrect pointer pp.

pp points the port array field of struct ahci_uc_priv, should not free it.

---
 drivers/ata/dwc_ahsata.c | 1 -
 1 file changed, 1 deletion(-)
drivers/ata/dwc_ahsata.c +0 -1
index 4b37a02338..82fbb50da6 100644
@@ -450,7 +450,6 @@ static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
 
 	mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
 	if (!mem) {
-		free(pp);
 		printf("No mem for table!\n");
 		return -ENOMEM;
 	}
Signature · 1 line
2.16.4
#5 lore raw

PATCH 3/4 sata: dwc_ahsata: Fix incorrect free

Quoted 6 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 43665: Free of address-of expression (BAD_FREE)
incorrect_free: free frees incorrect pointer pp.

Quoted 8 lines

pp points the port array field of struct ahci_uc_priv, should not free it.

---
 drivers/ata/dwc_ahsata.c | 1 -
 1 file changed, 1 deletion(-)
#6 lore raw

PATCH 3/4 sata: dwc_ahsata: Fix incorrect free

In reply to Peng Fan
Quoted 11 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 43665: Free of address-of expression (BAD_FREE)
incorrect_free: free frees incorrect pointer pp.

pp points the port array field of struct ahci_uc_priv, should not free it.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200508/b6f3ddba/attachment.sig>
author
#7 lore raw

PATCH 4/4 sata: dwc_ahsata: Fix memory issue in reset_sata

In reply to Peng Fan

From: Ye Li <[email protected]>

The reset_sata should reset the sata device info and free the probe_ent memory. Otherwise, it will cause memory leak if we init the sata again.

---
 drivers/ata/dwc_ahsata.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/ata/dwc_ahsata.c +3 -0
index 82fbb50da6..2bc1de8b98 100644
@@ -918,6 +918,9 @@ int reset_sata(int dev)
 	while (readl(&host_mmio->ghc) & SATA_HOST_GHC_HR)
 		udelay(100);
 
+	free(uc_priv);
+	memset(&sata_dev_desc[dev], 0, sizeof(struct blk_desc));
+
 	return 0;
 }
 
Signature · 1 line
2.16.4
#8 lore raw

PATCH 4/4 sata: dwc_ahsata: Fix memory issue in reset_sata

Quoted 8 lines · Peng Fan

From: Ye Li <[email protected]>

The reset_sata should reset the sata device info and free the probe_ent memory. Otherwise, it will cause memory leak if we init the sata again.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200508/2fb8e862/attachment.sig>
#9 lore raw

PATCH 1/4 sata: ahsata: Fix resource leak

In reply to Peng Fan
Quoted 11 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 3606684: Resource leak (RESOURCE_LEAK)
leaked_storage: Variable uc_priv going out of scope leaks the storage it points to

---
 drivers/ata/dwc_ahsata.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 16 lines
drivers/ata/dwc_ahsata.c +3 -0
index c2e28fe518..a775214792 100644
@@ -847,6 +847,9 @@ static int ahci_init_one(int pdev)
        struct ahci_uc_priv *uc_priv = NULL;
 
        uc_priv = malloc(sizeof(struct ahci_uc_priv));
+       if (!uc_priv)
+               return -ENOMEM;
+
        memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
        uc_priv->dev = pdev;
 

drivers/ata/dwc_ahsata.c static int ahci_init_one(int pdev) · line 878

Quoted 5 lines
drivers/ata/dwc_ahsata.c +2 -0
@@ -871,6 +874,8 @@ static int ahci_init_one(int pdev)
        return 0;
 
 err_out:
+       if (uc_priv)
+               free(uc_priv);

Seems like you can avoid the if() since it is always allocated at this point?

The migration date for SATA was over 6 months ago so really this code should not be used at this point.

Regards,
Simon

author
#10 lore raw

PATCH 1/4 sata: ahsata: Fix resource leak

Hi Simon,

Quoted 36 lines

Subject: Re: [PATCH 1/4] sata: ahsata: Fix resource leak

Quoted 7 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 3606684: Resource leak (RESOURCE_LEAK)
leaked_storage: Variable uc_priv going out of scope leaks the storage it points to

Quoted 5 lines
---
 drivers/ata/dwc_ahsata.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 18 lines
drivers/ata/dwc_ahsata.c index renamed from drivers/ata/dwc_ahsata.c +0 -0

c2e28fe518..a775214792 100644

drivers/ata/dwc_ahsata.c +3 -0
@@ -847,6 +847,9 @@ static int ahci_init_one(int pdev)
        struct ahci_uc_priv *uc_priv = NULL;
 
        uc_priv = malloc(sizeof(struct ahci_uc_priv));
+       if (!uc_priv)
+               return -ENOMEM;
+
        memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
        uc_priv->dev = pdev;
 
@@ -871,6 +874,8 @@ static int ahci_init_one(int pdev)
        return 0;
 
Quoted 8 lines

drivers/ata/dwc_ahsata.c static int ahci_init_one(int pdev) · line 878

Quoted 3 lines
drivers/ata/dwc_ahsata.c +2 -0
 err_out:
+       if (uc_priv)
+               free(uc_priv);

Seems like you can avoid the if() since it is always allocated at this point?

The migration date for SATA was over 6 months ago so really this code should not be used at this point.

This is to upstream nxp downstream patch. Since this is bug fix, I hope it is ok to be in until the driver is removed.

I'll also try to convert to use DM sata.

Thanks,
Peng.

Quoted 3 lines

Regards,
Simon

#11 lore raw

PATCH 1/4 sata: ahsata: Fix resource leak

In reply to Peng Fan
Quoted 8 lines · Peng Fan

From: Ye Li <[email protected]>

Fix coverity issue CID 3606684: Resource leak (RESOURCE_LEAK)
leaked_storage: Variable uc_priv going out of scope leaks the storage it points to

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20200508/9860c3bc/attachment.sig>