Skip to contentSkip to conversation

net: ibm: emac: Clear MAL descriptors without memset

PATCH Superseded Rosen Penev 3 participants 5 messages May 17 – May 21

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ibm: emac: Clear MAL descriptors without memset

Clear MAL descriptor rings with explicit field stores instead of memset(). The descriptor rings are carved from MAL coherent DMA memory, which may be mapped uncached on 32-bit powerpc. The optimized memset() path can use dcbz there and trigger an alignment warning.

The skb tracking arrays remain ordinary CPU memory and still use memset().

Assisted-by Codex:GPT-5.5
---
 drivers/net/ethernet/ibm/emac/core.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +13 -2
index 1fd7cb50c568..7ab5c944f5de 100644
@@ -1161,6 +1161,17 @@ static void emac_clean_rx_ring(struct emac_instance *dev)
 	}
 }
 
+static void emac_clear_mal_desc(struct mal_descriptor *desc, int count)
+{
+	int i;
+
+	for (i = 0; i < count; i++) {
+		desc[i].ctrl = 0;
+		desc[i].data_len = 0;
+		desc[i].data_ptr = 0;
+	}
+}
+
 static int
 __emac_prepare_rx_skb(struct sk_buff *skb, struct emac_instance *dev, int slot)
 {
@@ -3121,8 +3132,8 @@ static int emac_probe(struct platform_device *ofdev)
 	DBG(dev, "rx_desc %p" NL, dev->rx_desc);
 
 	/* Clean rings */
-	memset(dev->tx_desc, 0, NUM_TX_BUFF * sizeof(struct mal_descriptor));
-	memset(dev->rx_desc, 0, NUM_RX_BUFF * sizeof(struct mal_descriptor));
+	emac_clear_mal_desc(dev->tx_desc, NUM_TX_BUFF);
+	emac_clear_mal_desc(dev->rx_desc, NUM_RX_BUFF);
 	memset(dev->tx_skb, 0, NUM_TX_BUFF * sizeof(struct sk_buff *));
 	memset(dev->rx_skb, 0, NUM_RX_BUFF * sizeof(struct sk_buff *));
 
Signature · 1 line
2.54.0
maintainer
#1 lore raw
Quoted 19 lines · Rosen Penev

Clear MAL descriptor rings with explicit field stores instead of memset(). The descriptor rings are carved from MAL coherent DMA memory, which may be mapped uncached on 32-bit powerpc. The optimized memset() path can use dcbz there and trigger an alignment warning.

The skb tracking arrays remain ordinary CPU memory and still use memset().

Assisted-by Codex:GPT-5.5
---
 drivers/net/ethernet/ibm/emac/core.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +0 -0
index 1fd7cb50c568..7ab5c944f5de 100644
@@ -1161,6 +1161,17 @@ static void emac_clean_rx_ring(struct emac_instance *dev)
 	}

drivers/net/ethernet/ibm/emac/core.c static void emac_clean_rx_ring(struct emac_instance *dev) · line 1171

Quoted 10 lines
drivers/net/ethernet/ibm/emac/core.c +8 -0
 }
 
+static void emac_clear_mal_desc(struct mal_descriptor *desc, int count)
+{
+	int i;
+
+	for (i = 0; i < count; i++) {
+		desc[i].ctrl = 0;
+		desc[i].data_len = 0;
+		desc[i].data_ptr = 0;

Sashiko noted that the compiler could still optimize the above to a memset(). Perhaps a barrier() is needed here?

/P

#2 lore raw
Quoted 8 lines · Paolo Abeni
Quoted 4 lines · Rosen Penev

Clear MAL descriptor rings with explicit field stores instead of memset(). The descriptor rings are carved from MAL coherent DMA memory, which may be mapped uncached on 32-bit powerpc. The optimized memset() path can use dcbz there and trigger an alignment warning.

Sashiko noted that the compiler could still optimize the above to a memset(). Perhaps a barrier() is needed here?

Maybe my understanding is wrong, but the compiler should have the knowledge about if the addresses are aligned or not, and so if it can call the optimised memset, or needs to keep with individual byte accesses?

Has this patch been in user for a while, a port from downstream OpenWRT or something?

Andrew

#4 lore raw
In reply to Paolo Abeni
Quoted 23 lines · Paolo Abeni
Quoted 21 lines · Rosen Penev

Clear MAL descriptor rings with explicit field stores instead of memset(). The descriptor rings are carved from MAL coherent DMA memory, which may be mapped uncached on 32-bit powerpc. The optimized memset() path can use dcbz there and trigger an alignment warning.

The skb tracking arrays remain ordinary CPU memory and still use memset().

Assisted-by Codex:GPT-5.5
---
 drivers/net/ethernet/ibm/emac/core.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/net/ethernet/ibm/emac/core.c +0 -0
index 1fd7cb50c568..7ab5c944f5de 100644
@@ -1161,6 +1161,17 @@ static void emac_clean_rx_ring(struct emac_instance *dev)
      }
 }
 
Quoted 11 lines

drivers/net/ethernet/ibm/emac/core.c static void emac_clean_rx_ring(struct emac_instance *dev) · line 1171

Quoted 8 lines
drivers/net/ethernet/ibm/emac/core.c +8 -0
+static void emac_clear_mal_desc(struct mal_descriptor *desc, int count)
+{
+     int i;
+
+     for (i = 0; i < count; i++) {
+             desc[i].ctrl = 0;
+             desc[i].data_len = 0;
+             desc[i].data_ptr = 0;

Sashiko noted that the compiler could still optimize the above to a memset(). Perhaps a barrier() is needed here?

The canonical way to do this is WRITE_ONCE() AFAIK.

In practice it seems it's not an issue. That is

https://lore.kernel.org/all/2e3acfe63d289c6fba366e16973c9ab8369e8b75.1631803922.git.christophe.leroy@csgroup.eu/

no longer triggers.

Quoted 3 lines

/P

#3 lore raw
In reply to Andrew Lunn
Quoted 11 lines · Andrew Lunn
Quoted 8 lines · Paolo Abeni
Quoted 4 lines · Rosen Penev

Clear MAL descriptor rings with explicit field stores instead of memset(). The descriptor rings are carved from MAL coherent DMA memory, which may be mapped uncached on 32-bit powerpc. The optimized memset() path can use dcbz there and trigger an alignment warning.

Sashiko noted that the compiler could still optimize the above to a memset(). Perhaps a barrier() is needed here?

Quoted 7 lines

Maybe my understanding is wrong, but the compiler should have the knowledge about if the addresses are aligned or not, and so if it can call the optimised memset, or needs to keep with individual byte accesses?

Has this patch been in user for a while, a port from downstream OpenWRT or something?

Yes and no. These devices are currently on kernel 6.12, where the warnings are present. In the transition to 6.18, I'm fixing them.

Quoted 2 lines

Andrew