Skip to contentSkip to conversation

net: cpsw: avoid alignment faults by taking NET_IP_ALIGN into account

PATCH Applied Ard Biesheuvel 5 participants 7 messages Jan 18, 2022 – Jan 19, 2022

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

PATCH 1/1 net: cpsw: avoid alignment faults by taking NET_IP_ALIGN into account

Both versions of the CPSW driver declare a CPSW_HEADROOM_NA macro that takes NET_IP_ALIGN into account, but fail to use it appropriately when storing incoming packets in memory. This results in the IPv4 source and destination addresses to appear misaligned in memory, which causes aligment faults that need to be fixed up in software.

So let's switch from CPSW_HEADROOM to CPSW_HEADROOM_NA where needed. This gets rid of any alignment faults on the RX path on a Beaglebone White.

Cc Grygorii Strashko <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
 drivers/net/ethernet/ti/cpsw.c      | 6 +++---
 drivers/net/ethernet/ti/cpsw_new.c  | 6 +++---
 drivers/net/ethernet/ti/cpsw_priv.c | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
drivers/net/ethernet/ti/cpsw.c +3 -3
index 33142d505fc8..03575c017500 100644
@@ -349,7 +349,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 	struct cpsw_common	*cpsw = ndev_to_cpsw(xmeta->ndev);
 	int			pkt_size = cpsw->rx_packet_max;
 	int			ret = 0, port, ch = xmeta->ch;
-	int			headroom = CPSW_HEADROOM;
+	int			headroom = CPSW_HEADROOM_NA;
 	struct net_device	*ndev = xmeta->ndev;
 	struct cpsw_priv	*priv;
 	struct page_pool	*pool;
@@ -392,7 +392,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 	}
 
 	if (priv->xdp_prog) {
-		int headroom = CPSW_HEADROOM, size = len;
+		int size = len;
 
 		xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
 		if (status & CPDMA_RX_VLAN_ENCAP) {
@@ -442,7 +442,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 	xmeta->ndev = ndev;
 	xmeta->ch = ch;
 
-	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
+	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
 				       pkt_size, 0);
 	if (ret < 0) {
drivers/net/ethernet/ti/cpsw_new.c +3 -3
index 279e261e4720..bd4b1528cf99 100644
@@ -283,7 +283,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 {
 	struct page *new_page, *page = token;
 	void *pa = page_address(page);
-	int headroom = CPSW_HEADROOM;
+	int headroom = CPSW_HEADROOM_NA;
 	struct cpsw_meta_xdp *xmeta;
 	struct cpsw_common *cpsw;
 	struct net_device *ndev;
@@ -336,7 +336,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 	}
 
 	if (priv->xdp_prog) {
-		int headroom = CPSW_HEADROOM, size = len;
+		int size = len;
 
 		xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
 		if (status & CPDMA_RX_VLAN_ENCAP) {
@@ -386,7 +386,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 	xmeta->ndev = ndev;
 	xmeta->ch = ch;
 
-	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
+	dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
 	ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
 				       pkt_size, 0);
 	if (ret < 0) {
drivers/net/ethernet/ti/cpsw_priv.c +1 -1
index 3537502e5e8b..ba220593e6db 100644
@@ -1122,7 +1122,7 @@ int cpsw_fill_rx_channels(struct cpsw_priv *priv)
 			xmeta->ndev = priv->ndev;
 			xmeta->ch = ch;
 
-			dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM;
+			dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM_NA;
 			ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch,
 							    page, dma,
 							    cpsw->rx_packet_max,
Signature · 1 line
2.30.2
#1 lore raw

Hi Ard,

Quoted 96 lines · Ard Biesheuvel

Both versions of the CPSW driver declare a CPSW_HEADROOM_NA macro that takes NET_IP_ALIGN into account, but fail to use it appropriately when storing incoming packets in memory. This results in the IPv4 source and destination addresses to appear misaligned in memory, which causes aligment faults that need to be fixed up in software.

So let's switch from CPSW_HEADROOM to CPSW_HEADROOM_NA where needed. This gets rid of any alignment faults on the RX path on a Beaglebone White.

Cc Grygorii Strashko <[email protected]>
Cc Ilias Apalodimas <[email protected]>
---
 drivers/net/ethernet/ti/cpsw.c      | 6 +++---
 drivers/net/ethernet/ti/cpsw_new.c  | 6 +++---
 drivers/net/ethernet/ti/cpsw_priv.c | 2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
drivers/net/ethernet/ti/cpsw.c +3 -3
index 33142d505fc8..03575c017500 100644
@@ -349,7 +349,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
        struct cpsw_common      *cpsw = ndev_to_cpsw(xmeta->ndev);
        int                     pkt_size = cpsw->rx_packet_max;
        int                     ret = 0, port, ch = xmeta->ch;
-       int                     headroom = CPSW_HEADROOM;
+       int                     headroom = CPSW_HEADROOM_NA;
        struct net_device       *ndev = xmeta->ndev;
        struct cpsw_priv        *priv;
        struct page_pool        *pool;
@@ -392,7 +392,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
        }
 
        if (priv->xdp_prog) {
-               int headroom = CPSW_HEADROOM, size = len;
+               int size = len;
 
                xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
                if (status & CPDMA_RX_VLAN_ENCAP) {
@@ -442,7 +442,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
        xmeta->ndev = ndev;
        xmeta->ch = ch;
 
-       dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
+       dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
        ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
                                       pkt_size, 0);
        if (ret < 0) {
drivers/net/ethernet/ti/cpsw_new.c +3 -3
index 279e261e4720..bd4b1528cf99 100644
@@ -283,7 +283,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
 {
        struct page *new_page, *page = token;
        void *pa = page_address(page);
-       int headroom = CPSW_HEADROOM;
+       int headroom = CPSW_HEADROOM_NA;
        struct cpsw_meta_xdp *xmeta;
        struct cpsw_common *cpsw;
        struct net_device *ndev;
@@ -336,7 +336,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
        }
 
        if (priv->xdp_prog) {
-               int headroom = CPSW_HEADROOM, size = len;
+               int size = len;
 
                xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]);
                if (status & CPDMA_RX_VLAN_ENCAP) {
@@ -386,7 +386,7 @@ static void cpsw_rx_handler(void *token, int len, int status)
        xmeta->ndev = ndev;
        xmeta->ch = ch;
 
-       dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM;
+       dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA;
        ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma,
                                       pkt_size, 0);
        if (ret < 0) {
drivers/net/ethernet/ti/cpsw_priv.c +1 -2
index 3537502e5e8b..ba220593e6db 100644
@@ -1122,7 +1122,7 @@ int cpsw_fill_rx_channels(struct cpsw_priv *priv)
                        xmeta->ndev = priv->ndev;
                        xmeta->ch = ch;
 
-                       dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM;
+                       dma = page_pool_get_dma_addr(page) + CPSW_HEADROOM_NA;
                        ret = cpdma_chan_idle_submit_mapped(cpsw->rxv[ch].ch,
                                                            page, dma,
                                                            cpsw->rx_packet_max,
--
Quoted 2 lines

2.30.2

This looks good to me.
Grygorii I assume cpdma is ok with potential unaligned accesses?

Thanks
/Ilias

maintainer
#2 lore raw
In reply to Ard Biesheuvel
Quoted 6 lines · On Tue, 18 Jan 2022 11:22:04 +0100 Ard Biesheuvel wrote:

Both versions of the CPSW driver declare a CPSW_HEADROOM_NA macro that takes NET_IP_ALIGN into account, but fail to use it appropriately when storing incoming packets in memory. This results in the IPv4 source and destination addresses to appear misaligned in memory, which causes aligment faults that need to be fixed up in software.

Quoted 7 lines

So let's switch from CPSW_HEADROOM to CPSW_HEADROOM_NA where needed. This gets rid of any alignment faults on the RX path on a Beaglebone White.

Cc Grygorii Strashko <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Fixes 9ed4050c0d75 ("net: ethernet: ti: cpsw: add XDP support")

right?

#3 lore raw
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 18 Jan 2022 11:22:04 +0100 Ard Biesheuvel wrote:

Both versions of the CPSW driver declare a CPSW_HEADROOM_NA macro that takes NET_IP_ALIGN into account, but fail to use it appropriately when storing incoming packets in memory. This results in the IPv4 source and destination addresses to appear misaligned in memory, which causes aligment faults that need to be fixed up in software.

So let's switch from CPSW_HEADROOM to CPSW_HEADROOM_NA where needed. This gets rid of any alignment faults on the RX path on a Beaglebone White.

Quoted 6 lines
Quoted 3 lines
Cc Grygorii Strashko <[email protected]>
Cc Ilias Apalodimas <[email protected]>
Fixes 9ed4050c0d75 ("net: ethernet: ti: cpsw: add XDP support")

I suspect so, as that patch removes a call to __netdev_alloc_skb_ip_align(), and replaces it with page_pool_dev_alloc_pages() et al

#4 lore raw
In reply to Ard Biesheuvel
This patch was applied to netdev/net.git (master) by David S. Miller <[email protected]>:

Hello:

This patch was applied to netdev/net.git (master)
by David S. Miller <[email protected]>:

Quoted 11 lines · On Tue, 18 Jan 2022 11:22:04 +0100 you wrote:

Both versions of the CPSW driver declare a CPSW_HEADROOM_NA macro that takes NET_IP_ALIGN into account, but fail to use it appropriately when storing incoming packets in memory. This results in the IPv4 source and destination addresses to appear misaligned in memory, which causes aligment faults that need to be fixed up in software.

So let's switch from CPSW_HEADROOM to CPSW_HEADROOM_NA where needed. This gets rid of any alignment faults on the RX path on a Beaglebone White.

[...]

Here is the summary with links:
- [net] net: cpsw: avoid alignment faults by taking NET_IP_ALIGN into account

    https://git.kernel.org/netdev/net/c/1771afd47430

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#6 lore raw
Quoted 3 lines · Alexey Smirnov

Doesn't CPSW_HEADROOM already include NET_IP_ALIGN and has actually more strict alignment (by sizeof(long)) than CPSW_HEADROOM_NA?

Yes, and that is exactly the problem. NET_IP_ALIGN is used to deliberately *misalign* the start of the packet in memory, so that the IP header appears aligned.