Skip to contentSkip to conversation

dfu: dfu_sf: avoid double free of SPI device

PATCH New Heinrich Schuchardt 3 participants 3 messages Mar 18, 2021 – Mar 25, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dfu: dfu_sf: avoid double free of SPI device

Multiple DFU entities may share the same SPI device. We must make sure that the SPI device is only freed once.

When using the driver model it is not necessary to free the SPI device.

---
 drivers/dfu/dfu_sf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/dfu/dfu_sf.c +17 -1
index 76b629a334..8f8c425977 100644
@@ -87,7 +87,23 @@ static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
 
 static void dfu_free_entity_sf(struct dfu_entity *dfu)
 {
-	spi_flash_free(dfu->data.sf.dev);
+	/*
+	 * In the DM case it is not necessary to free the SPI device.
+	 * For the non-DM case we must ensure that the the SPI device is only
+	 * freed once.
+	 */
+	if (!CONFIG_IS_ENABLED(DM_SPI_FLASH)) {
+		struct spi_flash *dev = dfu->data.sf.dev;
+
+		if (!dev)
+			return;
+		dfu->data.sf.dev = NULL;
+		list_for_each_entry(dfu, &dfu_list, list) {
+			if (dfu->data.sf.dev == dev)
+				dfu->data.sf.dev = NULL;
+		}
+		spi_flash_free(dev);
+	}
 }
 
 static struct spi_flash *parse_dev(char *devstr)

--
2.30.2

#1 lore raw

PATCH 1/1 dfu: dfu_sf: avoid double free of SPI device

Quoted 40 lines · Heinrich Schuchardt

Multiple DFU entities may share the same SPI device. We must make sure that the SPI device is only freed once.

When using the driver model it is not necessary to free the SPI device.

---
 drivers/dfu/dfu_sf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/dfu/dfu_sf.c +17 -2
index 76b629a334..8f8c425977 100644
@@ -87,7 +87,23 @@ static unsigned int dfu_polltimeout_sf(struct dfu_entity *dfu)
 
 static void dfu_free_entity_sf(struct dfu_entity *dfu)
 {
-	spi_flash_free(dfu->data.sf.dev);
+	/*
+	 * In the DM case it is not necessary to free the SPI device.
+	 * For the non-DM case we must ensure that the the SPI device is only
+	 * freed once.
+	 */
+	if (!CONFIG_IS_ENABLED(DM_SPI_FLASH)) {
+		struct spi_flash *dev = dfu->data.sf.dev;
+
+		if (!dev)
+			return;
+		dfu->data.sf.dev = NULL;
+		list_for_each_entry(dfu, &dfu_list, list) {
+			if (dfu->data.sf.dev == dev)
+				dfu->data.sf.dev = NULL;
+		}
+		spi_flash_free(dev);
+	}
 }
 
 static struct spi_flash *parse_dev(char *devstr)
--
Quoted 2 lines

2.30.2

Dear Lukasz,

fixing the reported issue is prerequisite for merging a patch series by Jose.

Could you, please, provide for feedback this patch.

Best regards

Heinrich

#2 lore raw

PATCH 1/1 dfu: dfu_sf: avoid double free of SPI device

Quoted 49 lines · On Wed, 24 Mar 2021 18:21:37 +0100 Heinrich Schuchardt <[email protected]>…
Quoted 44 lines · Heinrich Schuchardt

Multiple DFU entities may share the same SPI device. We must make sure that the SPI device is only freed once.

When using the driver model it is not necessary to free the SPI device.

---
 drivers/dfu/dfu_sf.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
drivers/dfu/dfu_sf.c +2 -1
index 76b629a334..8f8c425977 100644
@@ -87,7 +87,23 @@ static unsigned int dfu_polltimeout_sf(struct
 dfu_entity *dfu)
 
 static void dfu_free_entity_sf(struct dfu_entity *dfu)
 {
-	spi_flash_free(dfu->data.sf.dev);
+	/*
+	 * In the DM case it is not necessary to free the SPI

device.

drivers/dfu/dfu_sf.c +1 -0
+	 * For the non-DM case we must ensure that the the SPI

device is only

drivers/dfu/dfu_sf.c +14 -1
+	 * freed once.
+	 */
+	if (!CONFIG_IS_ENABLED(DM_SPI_FLASH)) {
+		struct spi_flash *dev = dfu->data.sf.dev;
+
+		if (!dev)
+			return;
+		dfu->data.sf.dev = NULL;
+		list_for_each_entry(dfu, &dfu_list, list) {
+			if (dfu->data.sf.dev == dev)
+				dfu->data.sf.dev = NULL;
+		}
+		spi_flash_free(dev);
+	}
 }
 
 static struct spi_flash *parse_dev(char *devstr)
--
Quoted 2 lines

2.30.2

Dear Lukasz,

Quoted 8 lines

fixing the reported issue is prerequisite for merging a patch series by Jose.

Could you, please, provide for feedback this patch.

Best regards

Heinrich

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: lukma at denx.de -------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210325/0c3367b7/attachment.sig>