With the skb pointer piggy-backed on the TX BD, we have a simple and
efficient way to free the skb buffer when the frame has been transmitted.
But in order to avoid freeing the skb while there are still fragments from
the skb in use, we need to piggy-back on the TX BD of the skb, not the
first.
Without this, we are doing use-after-free on the DMA side, when the first
BD of a multi TX BD packet is seen as completed in xmit_done, and the
remaining BDs are still being processed.
Cc: stable@vger.kernel.org # v5.4+
Signed-off-by: Esben Haabendal <redacted>
---
drivers/net/ethernet/xilinx/ll_temac_main.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
@@ -915,6 +914,11 @@ temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)}cur_p->app0|=cpu_to_be32(STS_CTRL_APP0_EOP);+/* Mark last fragment with skb address, so it can be consumed+*intemac_start_xmit_done()+*/+ptr_to_txbd((void*)skb,cur_p);+tail_p=lp->tx_bd_p+sizeof(*lp->tx_bd_v)*lp->tx_bd_tail;lp->tx_bd_tail++;if(lp->tx_bd_tail>=lp->tx_bd_num)
Add a couple of memory-barriers to ensure correct ordering of read/write
access to TX BDs.
In xmit_done, we should ensure that reading the additional BD fields are
only done after STS_CTRL_APP0_CMPLT bit is set.
When xmit_done marks the BD as free by setting APP0=0, we need to ensure
that the other BD fields are reset first, so we avoid racing with the xmit
path, which writes to the same fields.
Finally, making sure to read APP0 of next BD after the current BD, ensures
that we see all available buffers.
Signed-off-by: Esben Haabendal <redacted>
---
drivers/net/ethernet/xilinx/ll_temac_main.c | 14 +++++++++++++-
1 file changed, 13 insertions(+), 1 deletion(-)
@@ -774,12 +774,15 @@ static void temac_start_xmit_done(struct net_device *ndev)stat=be32_to_cpu(cur_p->app0);while(stat&STS_CTRL_APP0_CMPLT){+/* Make sure that the other fields are read after bd is+*releasedbydma+*/+rmb();dma_unmap_single(ndev->dev.parent,be32_to_cpu(cur_p->phys),be32_to_cpu(cur_p->len),DMA_TO_DEVICE);skb=(structsk_buff*)ptr_from_txbd(cur_p);if(skb)dev_consume_skb_irq(skb);-cur_p->app0=0;cur_p->app1=0;cur_p->app2=0;cur_p->app3=0;
@@ -788,6 +791,12 @@ static void temac_start_xmit_done(struct net_device *ndev)ndev->stats.tx_packets++;ndev->stats.tx_bytes+=be32_to_cpu(cur_p->len);+/* app0 must be visible last, as it is used to flag+*availabilityofthebd+*/+smp_mb();+cur_p->app0=0;+lp->tx_bd_ci++;if(lp->tx_bd_ci>=lp->tx_bd_num)lp->tx_bd_ci=0;
@@ -814,6 +823,9 @@ static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)if(cur_p->app0)returnNETDEV_TX_BUSY;+/* Make sure to read next bd app0 after this one */+rmb();+tail++;if(tail>=lp->tx_bd_num)tail=0;
Just as the initial check, we need to ensure num_frag+1 buffers available,
as that is the number of buffers we are going to use.
This fixes a buffer overflow, which might be seen during heavy network
load. Complete lockup of TEMAC was reproducible within about 10 minutes of
a particular load.
Fixes: 84823ff80f74 ("net: ll_temac: Fix race condition causing TX hang")
Cc: stable@vger.kernel.org # v5.4+
Signed-off-by: Esben Haabendal <redacted>
---
drivers/net/ethernet/xilinx/ll_temac_main.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
@@ -861,7 +861,7 @@ temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)smp_mb();/* Space might have just been freed - check again */-if(temac_check_tx_bd_space(lp,num_frag))+if(temac_check_tx_bd_space(lp,num_frag+1))returnNETDEV_TX_BUSY;netif_wake_queue(ndev);
As documented in Documentation/networking/driver.rst, the ndo_start_xmit
method must not return NETDEV_TX_BUSY under any normal circumstances, and
as recommended, we simply stop the tx queue in advance, when there is a
risk that the next xmit would cause a NETDEV_TX_BUSY return.
Signed-off-by: Esben Haabendal <redacted>
---
drivers/net/ethernet/xilinx/ll_temac_main.c | 5 +++++
1 file changed, 5 insertions(+)
Hello:
This series was applied to netdev/net.git (refs/heads/master):
On Fri, 18 Jun 2021 12:52:23 +0200 you wrote:
With the skb pointer piggy-backed on the TX BD, we have a simple and
efficient way to free the skb buffer when the frame has been transmitted.
But in order to avoid freeing the skb while there are still fragments from
the skb in use, we need to piggy-back on the TX BD of the skb, not the
first.
Without this, we are doing use-after-free on the DMA side, when the first
BD of a multi TX BD packet is seen as completed in xmit_done, and the
remaining BDs are still being processed.
[...]