Skip to content

Commit 8354491

Browse files
tpetazzonidavem330
authored andcommitted
net: mvpp2: fix dma unmapping of TX buffers for fragments
Since commit 71ce391 ("net: mvpp2: enable proper per-CPU TX buffers unmapping"), we are not correctly DMA unmapping TX buffers for fragments. Indeed, the mvpp2_txq_inc_put() function only stores in the txq_cpu->tx_buffs[] array the physical address of the buffer to be DMA-unmapped when skb != NULL. In addition, when DMA-unmapping, we use skb_headlen(skb) to get the size to be unmapped. Both of this works fine for TX descriptors that are associated directly to a SKB, but not the ones that are used for fragments, with a NULL pointer as skb: - We have a NULL physical address when calling DMA unmap - skb_headlen(skb) crashes because skb is NULL This causes random crashes when fragments are used. To solve this problem, we need to: - Store the physical address of the buffer to be unmapped unconditionally, regardless of whether it is tied to a SKB or not. - Store the length of the buffer to be unmapped, which requires a new field. Instead of adding a third array to store the length of the buffer to be unmapped, and as suggested by David Miller, this commit refactors the tx_buffs[] and tx_skb[] arrays of 'struct mvpp2_txq_pcpu' into a separate structure 'mvpp2_txq_pcpu_buf', to which a 'size' field is added. Therefore, instead of having three arrays to allocate/free, we have a single one, which also improve data locality, reducing the impact on the CPU cache. Fixes: 71ce391 ("net: mvpp2: enable proper per-CPU TX buffers unmapping") Reported-by: Raphael G <[email protected]> Cc: Raphael G <[email protected]> Cc: [email protected] Signed-off-by: Thomas Petazzoni <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent f217bfd commit 8354491

File tree

1 file changed

+30
-29
lines changed
  • drivers/net/ethernet/marvell

1 file changed

+30
-29
lines changed

drivers/net/ethernet/marvell/mvpp2.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,17 @@ struct mvpp2_rx_desc {
770770
u32 reserved8;
771771
};
772772

773+
struct mvpp2_txq_pcpu_buf {
774+
/* Transmitted SKB */
775+
struct sk_buff *skb;
776+
777+
/* Physical address of transmitted buffer */
778+
dma_addr_t phys;
779+
780+
/* Size transmitted */
781+
size_t size;
782+
};
783+
773784
/* Per-CPU Tx queue control */
774785
struct mvpp2_txq_pcpu {
775786
int cpu;
@@ -785,11 +796,8 @@ struct mvpp2_txq_pcpu {
785796
/* Number of Tx DMA descriptors reserved for each CPU */
786797
int reserved_num;
787798

788-
/* Array of transmitted skb */
789-
struct sk_buff **tx_skb;
790-
791-
/* Array of transmitted buffers' physical addresses */
792-
dma_addr_t *tx_buffs;
799+
/* Infos about transmitted buffers */
800+
struct mvpp2_txq_pcpu_buf *buffs;
793801

794802
/* Index of last TX DMA descriptor that was inserted */
795803
int txq_put_index;
@@ -979,10 +987,11 @@ static void mvpp2_txq_inc_put(struct mvpp2_txq_pcpu *txq_pcpu,
979987
struct sk_buff *skb,
980988
struct mvpp2_tx_desc *tx_desc)
981989
{
982-
txq_pcpu->tx_skb[txq_pcpu->txq_put_index] = skb;
983-
if (skb)
984-
txq_pcpu->tx_buffs[txq_pcpu->txq_put_index] =
985-
tx_desc->buf_phys_addr;
990+
struct mvpp2_txq_pcpu_buf *tx_buf =
991+
txq_pcpu->buffs + txq_pcpu->txq_put_index;
992+
tx_buf->skb = skb;
993+
tx_buf->size = tx_desc->data_size;
994+
tx_buf->phys = tx_desc->buf_phys_addr;
986995
txq_pcpu->txq_put_index++;
987996
if (txq_pcpu->txq_put_index == txq_pcpu->size)
988997
txq_pcpu->txq_put_index = 0;
@@ -4401,17 +4410,16 @@ static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
44014410
int i;
44024411

44034412
for (i = 0; i < num; i++) {
4404-
dma_addr_t buf_phys_addr =
4405-
txq_pcpu->tx_buffs[txq_pcpu->txq_get_index];
4406-
struct sk_buff *skb = txq_pcpu->tx_skb[txq_pcpu->txq_get_index];
4413+
struct mvpp2_txq_pcpu_buf *tx_buf =
4414+
txq_pcpu->buffs + txq_pcpu->txq_get_index;
44074415

44084416
mvpp2_txq_inc_get(txq_pcpu);
44094417

4410-
dma_unmap_single(port->dev->dev.parent, buf_phys_addr,
4411-
skb_headlen(skb), DMA_TO_DEVICE);
4412-
if (!skb)
4418+
dma_unmap_single(port->dev->dev.parent, tx_buf->phys,
4419+
tx_buf->size, DMA_TO_DEVICE);
4420+
if (!tx_buf->skb)
44134421
continue;
4414-
dev_kfree_skb_any(skb);
4422+
dev_kfree_skb_any(tx_buf->skb);
44154423
}
44164424
}
44174425

@@ -4651,15 +4659,10 @@ static int mvpp2_txq_init(struct mvpp2_port *port,
46514659
for_each_present_cpu(cpu) {
46524660
txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
46534661
txq_pcpu->size = txq->size;
4654-
txq_pcpu->tx_skb = kmalloc(txq_pcpu->size *
4655-
sizeof(*txq_pcpu->tx_skb),
4656-
GFP_KERNEL);
4657-
if (!txq_pcpu->tx_skb)
4658-
goto error;
4659-
4660-
txq_pcpu->tx_buffs = kmalloc(txq_pcpu->size *
4661-
sizeof(dma_addr_t), GFP_KERNEL);
4662-
if (!txq_pcpu->tx_buffs)
4662+
txq_pcpu->buffs = kmalloc(txq_pcpu->size *
4663+
sizeof(struct mvpp2_txq_pcpu_buf),
4664+
GFP_KERNEL);
4665+
if (!txq_pcpu->buffs)
46634666
goto error;
46644667

46654668
txq_pcpu->count = 0;
@@ -4673,8 +4676,7 @@ static int mvpp2_txq_init(struct mvpp2_port *port,
46734676
error:
46744677
for_each_present_cpu(cpu) {
46754678
txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4676-
kfree(txq_pcpu->tx_skb);
4677-
kfree(txq_pcpu->tx_buffs);
4679+
kfree(txq_pcpu->buffs);
46784680
}
46794681

46804682
dma_free_coherent(port->dev->dev.parent,
@@ -4693,8 +4695,7 @@ static void mvpp2_txq_deinit(struct mvpp2_port *port,
46934695

46944696
for_each_present_cpu(cpu) {
46954697
txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
4696-
kfree(txq_pcpu->tx_skb);
4697-
kfree(txq_pcpu->tx_buffs);
4698+
kfree(txq_pcpu->buffs);
46984699
}
46994700

47004701
if (txq->descs)

0 commit comments

Comments
 (0)