Skip to contentSkip to conversation

net: designware: Disable DMA on removal

PATCH In review Simon Glass 4 participants 13 messages Apr 5, 2025 – Apr 7, 2025

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

PATCH 1/3 net: designware: Disable DMA on removal

At present, removing the device frees struct dw_eth_dev but does not disable DMA. When DMA is active, packets can be received even if the driver is not active.

While it is possible that the memory used by the struct may remain untouched after removal, any other allocation may reuse that memory and result in DMA writing to random addresses.

In most case U-Boot does this removal in announce_and_cleanup() immediately before jumping to the OS. No further allocations are done after that point.

But with EFI_LOADER this function is not called before jumping to the EFI app. U-Boot continues to run while GRUB starts and finishes, as well as while Linux is starting up. During this time, devices cannot be removed, as they are in use. U-Boot completes the removal when efi_exit_boot_services() is called. It seems that this function does more allocations after calling dm_remove_devices_active() although I can't figure out where.

Fix this by disabling DMA when the driver is removed.

---

 drivers/net/designware.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/designware.c +1 -0
index 07b0f49ef58..eebf14bd51a 100644
@@ -809,6 +809,7 @@ static int designware_eth_remove(struct udevice *dev)
 {
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 
+	_dw_eth_halt(priv);
 	free(priv->phydev);
 	mdio_unregister(priv->bus);
 	mdio_free(priv->bus);
Signature · 4 lines
2.43.0

base-commit: bcfa94be955ef263d37918ef4b8f898ee8964c61
branch: eth
#1 lore raw

PATCH 2/3 designware: Use the remove() method with related drivers

Several drivers make use of the designware Ethernet driver but do not implement the remove() method. Add this so that DMA is stopped when the OS is booted to avoid memory corruption, etc.

---

 drivers/net/designware.c    |  2 +-
 drivers/net/designware.h    | 12 ++++++++++++
 drivers/net/dwmac_meson8b.c |  6 ++++++
 drivers/net/dwmac_s700.c    |  6 ++++++
 drivers/net/dwmac_socfpga.c |  6 ++++++
 5 files changed, 31 insertions(+), 1 deletion(-)
drivers/net/designware.c +1 -1
index eebf14bd51a..d4d31925fca 100644
@@ -805,7 +805,7 @@ clk_err:
 	return err;
 }
 
-static int designware_eth_remove(struct udevice *dev)
+int designware_eth_remove(struct udevice *dev)
 {
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 
drivers/net/designware.h +12 -0
index e47101ccaf6..8c9d0190e03 100644
@@ -247,6 +247,18 @@ struct dw_eth_dev {
 
 int designware_eth_of_to_plat(struct udevice *dev);
 int designware_eth_probe(struct udevice *dev);
+
+/**
+ * designware_eth_remove() - Remove the device
+ *
+ * Disables DMA and marks the device as remove. This must be called before
+ * booting an OS, to ensure that DMA is inactive.
+ *
+ * @dev: Device to remove
+ * Return 0 if OK, -ve on error
+ */
+int designware_eth_remove(struct udevice *dev);
+
 extern const struct eth_ops designware_eth_ops;
 
 struct dw_eth_pdata {
drivers/net/dwmac_meson8b.c +6 -0
index fde4aabbace..b2152f8da9b 100644
@@ -145,6 +145,11 @@ static int dwmac_meson8b_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_meson8b_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_meson8b_ids[] = {
 	{ .compatible = "amlogic,meson-gxbb-dwmac", .data = (ulong)dwmac_setup_gx },
 	{ .compatible = "amlogic,meson-g12a-dwmac", .data = (ulong)dwmac_setup_axg },
@@ -158,6 +163,7 @@ U_BOOT_DRIVER(dwmac_meson8b) = {
 	.of_match	= dwmac_meson8b_ids,
 	.of_to_plat = dwmac_meson8b_of_to_plat,
 	.probe		= dwmac_meson8b_probe,
+	.remove		= dwmac_meson8b_remove,
 	.ops		= &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct dwmac_meson8b_plat),
drivers/net/dwmac_s700.c +6 -0
index 969d247b4f3..cfb37c3aa71 100644
@@ -44,6 +44,11 @@ static int dwmac_s700_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_s700_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static int dwmac_s700_of_to_plat(struct udevice *dev)
 {
 	return designware_eth_of_to_plat(dev);
@@ -60,6 +65,7 @@ U_BOOT_DRIVER(dwmac_s700) = {
 	.of_match = dwmac_s700_ids,
 	.of_to_plat = dwmac_s700_of_to_plat,
 	.probe  = dwmac_s700_probe,
+	.remove	= dwmac_s700_remove,
 	.ops    = &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct eth_pdata),
drivers/net/dwmac_socfpga.c +6 -0
index a9e2d8c0972..8e7a9975d28 100644
@@ -130,6 +130,11 @@ static int dwmac_socfpga_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_socfpga_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_socfpga_ids[] = {
 	{ .compatible = "altr,socfpga-stmmac" },
 	{ }
@@ -141,6 +146,7 @@ U_BOOT_DRIVER(dwmac_socfpga) = {
 	.of_match	= dwmac_socfpga_ids,
 	.of_to_plat = dwmac_socfpga_of_to_plat,
 	.probe		= dwmac_socfpga_probe,
+	.remove		= dwmac_socfpga_remove,
 	.ops		= &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct dwmac_socfpga_plat),
Signature · 4 lines
2.43.0

base-commit: bcfa94be955ef263d37918ef4b8f898ee8964c61
branch: eth
#2 lore raw

Re: [PATCH 2/3] designware: Use the remove() method with related drivers

Hi Simon,

Quoted 3 lines · Simon Glass

Several drivers make use of the designware Ethernet driver but do not implement the remove() method. Add this so that DMA is stopped when the OS is booted to avoid memory corruption, etc.

The designware Ethernet driver core should not need the remove() ops as the eth_ops.stop() ops already should stop DMA. And the eth-uclass pre_remove() ops already call the eth_ops.stop() ops.

All these drivers seem to use designware_eth_ops and thus have correct stop() ops configured and should really not need the remove() ops.

Side note, this also misses the gmac rockchip driver that uses its own eth_ops instead of designware_eth_ops, however it also use same stop() ops as all these drivers.

Quoted 65 lines

---

 drivers/net/designware.c    |  2 +-
 drivers/net/designware.h    | 12 ++++++++++++
 drivers/net/dwmac_meson8b.c |  6 ++++++
 drivers/net/dwmac_s700.c    |  6 ++++++
 drivers/net/dwmac_socfpga.c |  6 ++++++
 5 files changed, 31 insertions(+), 1 deletion(-)
drivers/net/designware.c +1 -1
index eebf14bd51a..d4d31925fca 100644
@@ -805,7 +805,7 @@ clk_err:
 	return err;
 }
 
-static int designware_eth_remove(struct udevice *dev)
+int designware_eth_remove(struct udevice *dev)
 {
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 
drivers/net/designware.h +12 -0
index e47101ccaf6..8c9d0190e03 100644
@@ -247,6 +247,18 @@ struct dw_eth_dev {
 
 int designware_eth_of_to_plat(struct udevice *dev);
 int designware_eth_probe(struct udevice *dev);
+
+/**
+ * designware_eth_remove() - Remove the device
+ *
+ * Disables DMA and marks the device as remove. This must be called before
+ * booting an OS, to ensure that DMA is inactive.
+ *
+ * @dev: Device to remove
+ * Return 0 if OK, -ve on error
+ */
+int designware_eth_remove(struct udevice *dev);
+
 extern const struct eth_ops designware_eth_ops;
 
 struct dw_eth_pdata {
drivers/net/dwmac_meson8b.c +5 -0
index fde4aabbace..b2152f8da9b 100644
@@ -145,6 +145,11 @@ static int dwmac_meson8b_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_meson8b_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_meson8b_ids[] = {
 	{ .compatible = "amlogic,meson-gxbb-dwmac", .data = (ulong)dwmac_setup_gx },
 	{ .compatible = "amlogic,meson-g12a-dwmac", .data = (ulong)dwmac_setup_axg },

drivers/net/dwmac_meson8b.c U_BOOT_DRIVER(dwmac_meson8b) = { · line 166

Quoted 4 lines
drivers/net/dwmac_meson8b.c +1 -0
@@ -158,6 +163,7 @@ U_BOOT_DRIVER(dwmac_meson8b) = {
 	.of_match	= dwmac_meson8b_ids,
 	.of_to_plat = dwmac_meson8b_of_to_plat,
 	.probe		= dwmac_meson8b_probe,
+	.remove		= dwmac_meson8b_remove,

There is no reason to add the dwmac_meson8b_remove function above, you could just use designware_eth_remove directly here.

Same for remaining drivers below.

Regards,
Jonas

Quoted 51 lines
drivers/net/dwmac_meson8b.c +0 -0
 	.ops		= &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct dwmac_meson8b_plat),
drivers/net/dwmac_s700.c +6 -0
index 969d247b4f3..cfb37c3aa71 100644
@@ -44,6 +44,11 @@ static int dwmac_s700_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_s700_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static int dwmac_s700_of_to_plat(struct udevice *dev)
 {
 	return designware_eth_of_to_plat(dev);
@@ -60,6 +65,7 @@ U_BOOT_DRIVER(dwmac_s700) = {
 	.of_match = dwmac_s700_ids,
 	.of_to_plat = dwmac_s700_of_to_plat,
 	.probe  = dwmac_s700_probe,
+	.remove	= dwmac_s700_remove,
 	.ops    = &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct eth_pdata),
drivers/net/dwmac_socfpga.c +6 -0
index a9e2d8c0972..8e7a9975d28 100644
@@ -130,6 +130,11 @@ static int dwmac_socfpga_probe(struct udevice *dev)
 	return designware_eth_probe(dev);
 }
 
+static int dwmac_socfpga_remove(struct udevice *dev)
+{
+	return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_socfpga_ids[] = {
 	{ .compatible = "altr,socfpga-stmmac" },
 	{ }
@@ -141,6 +146,7 @@ U_BOOT_DRIVER(dwmac_socfpga) = {
 	.of_match	= dwmac_socfpga_ids,
 	.of_to_plat = dwmac_socfpga_of_to_plat,
 	.probe		= dwmac_socfpga_probe,
+	.remove		= dwmac_socfpga_remove,
 	.ops		= &designware_eth_ops,
 	.priv_auto	= sizeof(struct dw_eth_dev),
 	.plat_auto	= sizeof(struct dwmac_socfpga_plat),
#3 lore raw

Re: [PATCH 2/3] designware: Use the remove() method with related drivers

Hi Jonas,

Quoted 8 lines · Jonas Karlman

Hi Simon,

Quoted 3 lines · Simon Glass

Several drivers make use of the designware Ethernet driver but do not implement the remove() method. Add this so that DMA is stopped when the OS is booted to avoid memory corruption, etc.

Quoted 7 lines

The designware Ethernet driver core should not need the remove() ops as the eth_ops.stop() ops already should stop DMA. And the eth-uclass pre_remove() ops already call the eth_ops.stop() ops.

All these drivers seem to use designware_eth_ops and thus have correct stop() ops configured and should really not need the remove() ops.

That's interesting. I wrote that code about 9 years ago so perhaps can be forgiven for forgetting.

Very few drivers set the DM_FLAG_ACTIVE_DMA flag, certainly not the designware ones. So how would they be removed?

Quoted 3 lines

Side note, this also misses the gmac rockchip driver that uses its own eth_ops instead of designware_eth_ops, however it also use same stop() ops as all these drivers.

Hmm yes I saw that but then apparently forgot to include it.

I can perhaps redo this patch to add the DMA flag.

Quoted 74 lines
Quoted 66 lines

---

 drivers/net/designware.c    |  2 +-
 drivers/net/designware.h    | 12 ++++++++++++
 drivers/net/dwmac_meson8b.c |  6 ++++++
 drivers/net/dwmac_s700.c    |  6 ++++++
 drivers/net/dwmac_socfpga.c |  6 ++++++
 5 files changed, 31 insertions(+), 1 deletion(-)
drivers/net/designware.c +1 -1
index eebf14bd51a..d4d31925fca 100644
@@ -805,7 +805,7 @@ clk_err:
      return err;
 }
 
-static int designware_eth_remove(struct udevice *dev)
+int designware_eth_remove(struct udevice *dev)
 {
      struct dw_eth_dev *priv = dev_get_priv(dev);
 
drivers/net/designware.h +12 -0
index e47101ccaf6..8c9d0190e03 100644
@@ -247,6 +247,18 @@ struct dw_eth_dev {
 
 int designware_eth_of_to_plat(struct udevice *dev);
 int designware_eth_probe(struct udevice *dev);
+
+/**
+ * designware_eth_remove() - Remove the device
+ *
+ * Disables DMA and marks the device as remove. This must be called before
+ * booting an OS, to ensure that DMA is inactive.
+ *
+ * @dev: Device to remove
+ * Return 0 if OK, -ve on error
+ */
+int designware_eth_remove(struct udevice *dev);
+
 extern const struct eth_ops designware_eth_ops;
 
 struct dw_eth_pdata {
drivers/net/dwmac_meson8b.c +5 -0
index fde4aabbace..b2152f8da9b 100644
@@ -145,6 +145,11 @@ static int dwmac_meson8b_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
+static int dwmac_meson8b_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_meson8b_ids[] = {
      { .compatible = "amlogic,meson-gxbb-dwmac", .data = (ulong)dwmac_setup_gx },
      { .compatible = "amlogic,meson-g12a-dwmac", .data = (ulong)dwmac_setup_axg },

drivers/net/dwmac_meson8b.c U_BOOT_DRIVER(dwmac_meson8b) = { · line 166

Quoted 4 lines
drivers/net/dwmac_meson8b.c +1 -0
@@ -158,6 +163,7 @@ U_BOOT_DRIVER(dwmac_meson8b) = {
      .of_match       = dwmac_meson8b_ids,
      .of_to_plat = dwmac_meson8b_of_to_plat,
      .probe          = dwmac_meson8b_probe,
+     .remove         = dwmac_meson8b_remove,

There is no reason to add the dwmac_meson8b_remove function above, you could just use designware_eth_remove directly here.

Quoted 1 line

Same for remaining drivers below.

Yes, I wasn't sure which way to go on that. I'll remove them.

Quoted 39 lines

Regards,
Jonas

Quoted 35 lines
drivers/net/dwmac_meson8b.c +0 -0
      .ops            = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct dwmac_meson8b_plat),
drivers/net/dwmac_s700.c +6 -0
index 969d247b4f3..cfb37c3aa71 100644
@@ -44,6 +44,11 @@ static int dwmac_s700_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
+static int dwmac_s700_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static int dwmac_s700_of_to_plat(struct udevice *dev)
 {
      return designware_eth_of_to_plat(dev);
@@ -60,6 +65,7 @@ U_BOOT_DRIVER(dwmac_s700) = {
      .of_match = dwmac_s700_ids,
      .of_to_plat = dwmac_s700_of_to_plat,
      .probe  = dwmac_s700_probe,
+     .remove = dwmac_s700_remove,
      .ops    = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct eth_pdata),
drivers/net/dwmac_socfpga.c +0 -0
index a9e2d8c0972..8e7a9975d28 100644
@@ -130,6 +130,11 @@ static int dwmac_socfpga_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
Quoted 17 lines
Quoted 9 lines
drivers/net/dwmac_socfpga.c +5 -0
+static int dwmac_socfpga_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_socfpga_ids[] = {
      { .compatible = "altr,socfpga-stmmac" },
      { }

drivers/net/dwmac_socfpga.c U_BOOT_DRIVER(dwmac_socfpga) = { · line 152

Quoted 7 lines
drivers/net/dwmac_socfpga.c +1 -0
@@ -141,6 +146,7 @@ U_BOOT_DRIVER(dwmac_socfpga) = {
      .of_match       = dwmac_socfpga_ids,
      .of_to_plat = dwmac_socfpga_of_to_plat,
      .probe          = dwmac_socfpga_probe,
+     .remove         = dwmac_socfpga_remove,
      .ops            = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct dwmac_socfpga_plat),

Regards,
Simon

#4 lore raw

Re: [PATCH 2/3] designware: Use the remove() method with related drivers

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Jonas,

Quoted 8 lines · Jonas Karlman

Hi Simon,

Quoted 3 lines · Simon Glass

Several drivers make use of the designware Ethernet driver but do not implement the remove() method. Add this so that DMA is stopped when the OS is booted to avoid memory corruption, etc.

Quoted 7 lines

The designware Ethernet driver core should not need the remove() ops as the eth_ops.stop() ops already should stop DMA. And the eth-uclass pre_remove() ops already call the eth_ops.stop() ops.

All these drivers seem to use designware_eth_ops and thus have correct stop() ops configured and should really not need the remove() ops.

Quoted 5 lines

That's interesting. I wrote that code about 9 years ago so perhaps can be forgiven for forgetting.

Very few drivers set the DM_FLAG_ACTIVE_DMA flag, certainly not the designware ones. So how would they be removed?

Ahh, I see, remove() is only called for devices with ACTIVE_DMA or OS_PREPARE. So I guess that was probably the issue all along?

There is some confusion that dm_remove_devices_active() does not remove device_active()=true devices and instead only remove ACTIVE_DMA and/or OS_PREPARE flagged devices.

Quoted 8 lines
Quoted 4 lines

Side note, this also misses the gmac rockchip driver that uses its own eth_ops instead of designware_eth_ops, however it also use same stop() ops as all these drivers.

Hmm yes I saw that but then apparently forgot to include it.

I can perhaps redo this patch to add the DMA flag.

Maybe eth_post_probe() or similar should add the OS_PREPARE or ACTIVE_DMA flag for all eth-uclass, to ensure remove() and thus stop() gets called when dm_remove_devices_active() is called.

There is probably no need to add all these remove() ops, as they would still not have been called by dm_remove_devices_active().

Regards,
Jonas

Quoted 138 lines
Quoted 75 lines
Quoted 67 lines

---

 drivers/net/designware.c    |  2 +-
 drivers/net/designware.h    | 12 ++++++++++++
 drivers/net/dwmac_meson8b.c |  6 ++++++
 drivers/net/dwmac_s700.c    |  6 ++++++
 drivers/net/dwmac_socfpga.c |  6 ++++++
 5 files changed, 31 insertions(+), 1 deletion(-)
drivers/net/designware.c +1 -1
index eebf14bd51a..d4d31925fca 100644
@@ -805,7 +805,7 @@ clk_err:
      return err;
 }
 
-static int designware_eth_remove(struct udevice *dev)
+int designware_eth_remove(struct udevice *dev)
 {
      struct dw_eth_dev *priv = dev_get_priv(dev);
 
drivers/net/designware.h +12 -0
index e47101ccaf6..8c9d0190e03 100644
@@ -247,6 +247,18 @@ struct dw_eth_dev {
 
 int designware_eth_of_to_plat(struct udevice *dev);
 int designware_eth_probe(struct udevice *dev);
+
+/**
+ * designware_eth_remove() - Remove the device
+ *
+ * Disables DMA and marks the device as remove. This must be called before
+ * booting an OS, to ensure that DMA is inactive.
+ *
+ * @dev: Device to remove
+ * Return 0 if OK, -ve on error
+ */
+int designware_eth_remove(struct udevice *dev);
+
 extern const struct eth_ops designware_eth_ops;
 
 struct dw_eth_pdata {
drivers/net/dwmac_meson8b.c +5 -0
index fde4aabbace..b2152f8da9b 100644
@@ -145,6 +145,11 @@ static int dwmac_meson8b_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
+static int dwmac_meson8b_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_meson8b_ids[] = {
      { .compatible = "amlogic,meson-gxbb-dwmac", .data = (ulong)dwmac_setup_gx },
      { .compatible = "amlogic,meson-g12a-dwmac", .data = (ulong)dwmac_setup_axg },

drivers/net/dwmac_meson8b.c U_BOOT_DRIVER(dwmac_meson8b) = { · line 166

Quoted 4 lines
drivers/net/dwmac_meson8b.c +1 -0
@@ -158,6 +163,7 @@ U_BOOT_DRIVER(dwmac_meson8b) = {
      .of_match       = dwmac_meson8b_ids,
      .of_to_plat = dwmac_meson8b_of_to_plat,
      .probe          = dwmac_meson8b_probe,
+     .remove         = dwmac_meson8b_remove,

There is no reason to add the dwmac_meson8b_remove function above, you could just use designware_eth_remove directly here.

Quoted 1 line

Same for remaining drivers below.

Yes, I wasn't sure which way to go on that. I'll remove them.

Quoted 39 lines

Regards,
Jonas

Quoted 35 lines
drivers/net/dwmac_meson8b.c +0 -0
      .ops            = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct dwmac_meson8b_plat),
drivers/net/dwmac_s700.c +6 -0
index 969d247b4f3..cfb37c3aa71 100644
@@ -44,6 +44,11 @@ static int dwmac_s700_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
+static int dwmac_s700_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static int dwmac_s700_of_to_plat(struct udevice *dev)
 {
      return designware_eth_of_to_plat(dev);
@@ -60,6 +65,7 @@ U_BOOT_DRIVER(dwmac_s700) = {
      .of_match = dwmac_s700_ids,
      .of_to_plat = dwmac_s700_of_to_plat,
      .probe  = dwmac_s700_probe,
+     .remove = dwmac_s700_remove,
      .ops    = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct eth_pdata),
drivers/net/dwmac_socfpga.c +0 -0
index a9e2d8c0972..8e7a9975d28 100644
@@ -130,6 +130,11 @@ static int dwmac_socfpga_probe(struct udevice *dev)
      return designware_eth_probe(dev);
 }
 
Quoted 17 lines
Quoted 9 lines
drivers/net/dwmac_socfpga.c +5 -0
+static int dwmac_socfpga_remove(struct udevice *dev)
+{
+     return designware_eth_remove(dev);
+}
+
 static const struct udevice_id dwmac_socfpga_ids[] = {
      { .compatible = "altr,socfpga-stmmac" },
      { }

drivers/net/dwmac_socfpga.c U_BOOT_DRIVER(dwmac_socfpga) = { · line 152

Quoted 7 lines
drivers/net/dwmac_socfpga.c +1 -0
@@ -141,6 +146,7 @@ U_BOOT_DRIVER(dwmac_socfpga) = {
      .of_match       = dwmac_socfpga_ids,
      .of_to_plat = dwmac_socfpga_of_to_plat,
      .probe          = dwmac_socfpga_probe,
+     .remove         = dwmac_socfpga_remove,
      .ops            = &designware_eth_ops,
      .priv_auto      = sizeof(struct dw_eth_dev),
      .plat_auto      = sizeof(struct dwmac_socfpga_plat),

Regards,
Simon

#5 lore raw

Re: [PATCH 2/3] designware: Use the remove() method with related drivers

Hi Jonas,

Quoted 23 lines · Jonas Karlman

Hi Simon,

Quoted 19 lines · Simon Glass

Hi Jonas,

Quoted 15 lines · Jonas Karlman

Hi Simon,

Quoted 3 lines · Simon Glass

Several drivers make use of the designware Ethernet driver but do not implement the remove() method. Add this so that DMA is stopped when the OS is booted to avoid memory corruption, etc.

The designware Ethernet driver core should not need the remove() ops as the eth_ops.stop() ops already should stop DMA. And the eth-uclass pre_remove() ops already call the eth_ops.stop() ops.

All these drivers seem to use designware_eth_ops and thus have correct stop() ops configured and should really not need the remove() ops.

Quoted 8 lines
Quoted 5 lines

That's interesting. I wrote that code about 9 years ago so perhaps can be forgiven for forgetting.

Very few drivers set the DM_FLAG_ACTIVE_DMA flag, certainly not the designware ones. So how would they be removed?

Ahh, I see, remove() is only called for devices with ACTIVE_DMA or OS_PREPARE. So I guess that was probably the issue all along?

I suppose so, yes.

Quoted 10 lines

There is some confusion that dm_remove_devices_active() does not remove device_active()=true devices and instead only remove ACTIVE_DMA and/or OS_PREPARE flagged devices.

Quoted 5 lines
Quoted 4 lines

Side note, this also misses the gmac rockchip driver that uses its own eth_ops instead of designware_eth_ops, however it also use same stop() ops as all these drivers.

Quoted 7 lines
Quoted 3 lines

Hmm yes I saw that but then apparently forgot to include it.

I can perhaps redo this patch to add the DMA flag.

Maybe eth_post_probe() or similar should add the OS_PREPARE or ACTIVE_DMA flag for all eth-uclass, to ensure remove() and thus stop() gets called when dm_remove_devices_active() is called.

But not all network drivers use DMA. It should really be set by the driver.

Quoted 3 lines

There is probably no need to add all these remove() ops, as they would still not have been called by dm_remove_devices_active().

Well, since we can rely on the eth uclass, that is correct, but I still think it is better to add it, as an example to other drivers.

Regards,
Simon

#6 lore raw

PATCH 3/3 efi_loader: Move device-removal later in exit-boot-services

In reply to Simon Glass

This removal should be the last thing done, so that U-Boot does no more memory allocations afterwards. Move it and add a comment.

Note that the TCG2 log is updated after this call, but I cannot see any allocations there.

---

 lib/efi_loader/efi_boottime.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
lib/efi_loader/efi_boottime.c +13 -8
index ffe43accd1e..e525662f82f 100644
@@ -2250,14 +2250,6 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 			list_del(&evt->link);
 	}
 
-	if (!efi_st_keep_devices) {
-		bootm_disable_interrupts();
-		if (IS_ENABLED(CONFIG_USB_DEVICE))
-			udc_disconnect();
-		board_quiesce_devices();
-		dm_remove_devices_active();
-	}
-
 	/* Patch out unsupported runtime function */
 	efi_runtime_detach();
 
@@ -2279,6 +2271,19 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 	/* Give the payload some time to boot */
 	efi_set_watchdog(0);
 	schedule();
+
+	/*
+	 * this should be the last thing done, to avoid memory allocations
+	 * between removing devices and the OS taking over
+	 */
+	if (!efi_st_keep_devices) {
+		bootm_disable_interrupts();
+		if (IS_ENABLED(CONFIG_USB_DEVICE))
+			udc_disconnect();
+		board_quiesce_devices();
+		dm_remove_devices_active();
+	}
+
 out:
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
 		if (ret != EFI_SUCCESS)
Signature · 4 lines
2.43.0

base-commit: bcfa94be955ef263d37918ef4b8f898ee8964c61
branch: eth
#7 lore raw

Re: [PATCH 3/3] efi_loader: Move device-removal later in exit-boot-services

Am 6. April 2025 00:12:47 MESZ schrieb Simon Glass <[email protected]>:

Quoted 7 lines

This removal should be the last thing done, so that U-Boot does no more memory allocations afterwards. Move it and add a comment.

Note that the TCG2 log is updated after this call, but I cannot see any allocations there.

This commit message fails to describe which problem has been observed and you try to solve.

A link to the original communication would be helpful.

Furthermore, please, rebase it on my patch adding eth_halt().
<https://lore.kernel.org/u-boot/CAFLszTgp38yBXnSigfT-mEFU=rqyAKjp0WBQGgHP_N3vRxCq=Q@mail.gmail.com/T/#m1e55cc094f575797da3b4aed6b369544f8d6f8f4>

Best regards

Heinrich

Quoted 46 lines

---

lib/efi_loader/efi_boottime.c | 21 +++++++++++++--------

1 file changed, 13 insertions(+), 8 deletions(-)
lib/efi_loader/efi_boottime.c +13 -8
index ffe43accd1e..e525662f82f 100644
@@ -2250,14 +2250,6 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 			list_del(&evt->link);
 	}
 
-	if (!efi_st_keep_devices) {
-		bootm_disable_interrupts();
-		if (IS_ENABLED(CONFIG_USB_DEVICE))
-			udc_disconnect();
-		board_quiesce_devices();
-		dm_remove_devices_active();
-	}
-
 	/* Patch out unsupported runtime function */
 	efi_runtime_detach();
 
@@ -2279,6 +2271,19 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 	/* Give the payload some time to boot */
 	efi_set_watchdog(0);
 	schedule();
+
+	/*
+	 * this should be the last thing done, to avoid memory allocations
+	 * between removing devices and the OS taking over
+	 */
+	if (!efi_st_keep_devices) {
+		bootm_disable_interrupts();
+		if (IS_ENABLED(CONFIG_USB_DEVICE))
+			udc_disconnect();
+		board_quiesce_devices();
+		dm_remove_devices_active();
+	}
+

out:

	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
		if (ret != EFI_SUCCESS)
#8 lore raw

Re: [PATCH 3/3] efi_loader: Move device-removal later in exit-boot-services

Hi Heinrich,

Quoted 5 lines · Heinrich Schuchardt

Am 6. April 2025 00:12:47 MESZ schrieb Simon Glass <[email protected]>:

Quoted 3 lines

This removal should be the last thing done, so that U-Boot does no more memory allocations afterwards. Move it and add a comment.

Quoted 8 lines
Quoted 4 lines

Note that the TCG2 log is updated after this call, but I cannot see any allocations there.

This commit message fails to describe which problem has been observed and you try to solve.

A link to the original communication would be helpful.

OK I'll add those.

Quoted 3 lines

Furthermore, please, rebase it on my patch adding eth_halt().
<https://lore.kernel.org/u-boot/CAFLszTgp38yBXnSigfT-mEFU=rqyAKjp0WBQGgHP_N3vRxCq=Q@mail.gmail.com/T/#m1e55cc094f575797da3b4aed6b369544f8d6f8f4>

I see that has been applied to -master but I would like to target my series for -next

I would rather that we fix the drivers that add your work-around, but doing both is OK too.

Regards,
Simon

Quoted 52 lines

Best regards

Heinrich

Quoted 46 lines

---

lib/efi_loader/efi_boottime.c | 21 +++++++++++++--------

1 file changed, 13 insertions(+), 8 deletions(-)
lib/efi_loader/efi_boottime.c +13 -8
index ffe43accd1e..e525662f82f 100644
@@ -2250,14 +2250,6 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
                      list_del(&evt->link);
      }
 
-      if (!efi_st_keep_devices) {
-              bootm_disable_interrupts();
-              if (IS_ENABLED(CONFIG_USB_DEVICE))
-                      udc_disconnect();
-              board_quiesce_devices();
-              dm_remove_devices_active();
-      }
-
      /* Patch out unsupported runtime function */
      efi_runtime_detach();
 
@@ -2279,6 +2271,19 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
      /* Give the payload some time to boot */
      efi_set_watchdog(0);
      schedule();
+
+      /*
+       * this should be the last thing done, to avoid memory allocations
+       * between removing devices and the OS taking over
+       */
+      if (!efi_st_keep_devices) {
+              bootm_disable_interrupts();
+              if (IS_ENABLED(CONFIG_USB_DEVICE))
+                      udc_disconnect();
+              board_quiesce_devices();
+              dm_remove_devices_active();
+      }
+

out:

      if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
              if (ret != EFI_SUCCESS)
#9 lore raw

Re: [PATCH 3/3] efi_loader: Move device-removal later in exit-boot-services

In reply to Simon Glass
Quoted 2 lines

From: Simon Glass <[email protected]>
Date: Sun, 6 Apr 2025 10:12:47 +1200

Hi Simon,

Quoted 2 lines

This removal should be the last thing done, so that U-Boot does no more memory allocations afterwards. Move it and add a comment.

I really don't see what problem this is solving. If DMA is stopped before device driver memory is freed there is no problem with overwriting memory. And if DMA isn't stopped, it may overwrite memory as soon as the OS starts using it.

All you're doing here is causing a conflict with the change that Heinrich is proposing.

Quoted 56 lines

Note that the TCG2 log is updated after this call, but I cannot see any allocations there.

---

 lib/efi_loader/efi_boottime.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
lib/efi_loader/efi_boottime.c +13 -8
index ffe43accd1e..e525662f82f 100644
@@ -2250,14 +2250,6 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 			list_del(&evt->link);
 	}
 
-	if (!efi_st_keep_devices) {
-		bootm_disable_interrupts();
-		if (IS_ENABLED(CONFIG_USB_DEVICE))
-			udc_disconnect();
-		board_quiesce_devices();
-		dm_remove_devices_active();
-	}
-
 	/* Patch out unsupported runtime function */
 	efi_runtime_detach();
 
@@ -2279,6 +2271,19 @@ static efi_status_t EFIAPI efi_exit_boot_services(efi_handle_t image_handle,
 	/* Give the payload some time to boot */
 	efi_set_watchdog(0);
 	schedule();
+
+	/*
+	 * this should be the last thing done, to avoid memory allocations
+	 * between removing devices and the OS taking over
+	 */
+	if (!efi_st_keep_devices) {
+		bootm_disable_interrupts();
+		if (IS_ENABLED(CONFIG_USB_DEVICE))
+			udc_disconnect();
+		board_quiesce_devices();
+		dm_remove_devices_active();
+	}
+
 out:
 	if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
 		if (ret != EFI_SUCCESS)
Signature · 4 lines
2.43.0

base-commit: bcfa94be955ef263d37918ef4b8f898ee8964c61
branch: eth
Acked-by on v1 1/3
#10 lore raw
In reply to Simon Glass

Am 6. April 2025 00:12:45 MESZ schrieb Simon Glass <[email protected]>:

Quoted 20 lines

At present, removing the device frees struct dw_eth_dev but does not disable DMA. When DMA is active, packets can be received even if the driver is not active.

While it is possible that the memory used by the struct may remain untouched after removal, any other allocation may reuse that memory and result in DMA writing to random addresses.

In most case U-Boot does this removal in announce_and_cleanup() immediately before jumping to the OS. No further allocations are done after that point.

But with EFI_LOADER this function is not called before jumping to the EFI app. U-Boot continues to run while GRUB starts and finishes, as well as while Linux is starting up. During this time, devices cannot be removed, as they are in use. U-Boot completes the removal when efi_exit_boot_services() is called. It seems that this function does more allocations after calling dm_remove_devices_active() although I can't figure out where.

Quoted 4 lines

Fix this by disabling DMA when the driver is removed.

LGTM

Quoted 17 lines

---

drivers/net/designware.c | 1 +

1 file changed, 1 insertion(+)
drivers/net/designware.c +1 -0
index 07b0f49ef58..eebf14bd51a 100644
@@ -809,6 +809,7 @@ static int designware_eth_remove(struct udevice *dev)
 {
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 
+	_dw_eth_halt(priv);
 	free(priv->phydev);
 	mdio_unregister(priv->bus);
 	mdio_free(priv->bus);
#11 lore raw
In reply to Simon Glass

Hi Simon,

Quoted 34 lines · Simon Glass

At present, removing the device frees struct dw_eth_dev but does not disable DMA. When DMA is active, packets can be received even if the driver is not active.

While it is possible that the memory used by the struct may remain untouched after removal, any other allocation may reuse that memory and result in DMA writing to random addresses.

In most case U-Boot does this removal in announce_and_cleanup() immediately before jumping to the OS. No further allocations are done after that point.

But with EFI_LOADER this function is not called before jumping to the EFI app. U-Boot continues to run while GRUB starts and finishes, as well as while Linux is starting up. During this time, devices cannot be removed, as they are in use. U-Boot completes the removal when efi_exit_boot_services() is called. It seems that this function does more allocations after calling dm_remove_devices_active() although I can't figure out where.

Fix this by disabling DMA when the driver is removed.

---

 drivers/net/designware.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/designware.c +0 -0
index 07b0f49ef58..eebf14bd51a 100644

drivers/net/designware.c static int designware_eth_remove(struct udevice *dev) · line 812

Quoted 4 lines
drivers/net/designware.c +1 -0
@@ -809,6 +809,7 @@ static int designware_eth_remove(struct udevice *dev)
 {
 	struct dw_eth_dev *priv = dev_get_priv(dev);
 
+	_dw_eth_halt(priv);

This should not really be needed, eth-uclass already call the stop() ops that in turn call _dw_eth_halt() from the eth-uclass pre_remove() ops.

So ethernet should already have halted when remove() is called, calling it here will probably only hide any real issue.

Regards,
Jonas

Quoted 3 lines
drivers/net/designware.c +0 -0
 	free(priv->phydev);
 	mdio_unregister(priv->bus);
 	mdio_free(priv->bus);
#12 lore raw

Hi Jonas,

Quoted 41 lines · Jonas Karlman

Hi Simon,

Quoted 37 lines · Simon Glass

At present, removing the device frees struct dw_eth_dev but does not disable DMA. When DMA is active, packets can be received even if the driver is not active.

While it is possible that the memory used by the struct may remain untouched after removal, any other allocation may reuse that memory and result in DMA writing to random addresses.

In most case U-Boot does this removal in announce_and_cleanup() immediately before jumping to the OS. No further allocations are done after that point.

But with EFI_LOADER this function is not called before jumping to the EFI app. U-Boot continues to run while GRUB starts and finishes, as well as while Linux is starting up. During this time, devices cannot be removed, as they are in use. U-Boot completes the removal when efi_exit_boot_services() is called. It seems that this function does more allocations after calling dm_remove_devices_active() although I can't figure out where.

Fix this by disabling DMA when the driver is removed.

---

 drivers/net/designware.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/designware.c +0 -0
index 07b0f49ef58..eebf14bd51a 100644
@@ -809,6 +809,7 @@ static int designware_eth_remove(struct udevice *dev)
 {
      struct dw_eth_dev *priv = dev_get_priv(dev);
 
Quoted 7 lines

drivers/net/designware.c static int designware_eth_remove(struct udevice *dev) · line 812

Quoted 1 line
drivers/net/designware.c +1 -0
+     _dw_eth_halt(priv);

This should not really be needed, eth-uclass already call the stop() ops that in turn call _dw_eth_halt() from the eth-uclass pre_remove() ops.

So ethernet should already have halted when remove() is called, calling it here will probably only hide any real issue.

Yes that seems right, thanks.

Regards,
Simon