Skip to content

Commit d72411d

Browse files
Thomas-fourierkuba-moo
authored andcommitted
ethernet: atl1: Add missing DMA mapping error checks and count errors
The `dma_map_XXX()` functions can fail and must be checked using `dma_mapping_error()`. This patch adds proper error handling for all DMA mapping calls. In `atl1_alloc_rx_buffers()`, if DMA mapping fails, the buffer is deallocated and marked accordingly. In `atl1_tx_map()`, previously mapped buffers are unmapped and the packet is dropped on failure. If `atl1_xmit_frame()` drops the packet, increment the tx_error counter. Fixes: f3cc28c ("Add Attansic L1 ethernet driver.") Signed-off-by: Thomas Fourier <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent ba2f83e commit d72411d

File tree

1 file changed

+57
-22
lines changed
  • drivers/net/ethernet/atheros/atlx

1 file changed

+57
-22
lines changed

drivers/net/ethernet/atheros/atlx/atl1.c

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,14 +1861,21 @@ static u16 atl1_alloc_rx_buffers(struct atl1_adapter *adapter)
18611861
break;
18621862
}
18631863

1864-
buffer_info->alloced = 1;
1865-
buffer_info->skb = skb;
1866-
buffer_info->length = (u16) adapter->rx_buffer_len;
18671864
page = virt_to_page(skb->data);
18681865
offset = offset_in_page(skb->data);
18691866
buffer_info->dma = dma_map_page(&pdev->dev, page, offset,
18701867
adapter->rx_buffer_len,
18711868
DMA_FROM_DEVICE);
1869+
if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
1870+
kfree_skb(skb);
1871+
adapter->soft_stats.rx_dropped++;
1872+
break;
1873+
}
1874+
1875+
buffer_info->alloced = 1;
1876+
buffer_info->skb = skb;
1877+
buffer_info->length = (u16)adapter->rx_buffer_len;
1878+
18721879
rfd_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
18731880
rfd_desc->buf_len = cpu_to_le16(adapter->rx_buffer_len);
18741881
rfd_desc->coalese = 0;
@@ -2183,8 +2190,8 @@ static int atl1_tx_csum(struct atl1_adapter *adapter, struct sk_buff *skb,
21832190
return 0;
21842191
}
21852192

2186-
static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
2187-
struct tx_packet_desc *ptpd)
2193+
static bool atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
2194+
struct tx_packet_desc *ptpd)
21882195
{
21892196
struct atl1_tpd_ring *tpd_ring = &adapter->tpd_ring;
21902197
struct atl1_buffer *buffer_info;
@@ -2194,13 +2201,15 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
21942201
unsigned int nr_frags;
21952202
unsigned int f;
21962203
int retval;
2204+
u16 first_mapped;
21972205
u16 next_to_use;
21982206
u16 data_len;
21992207
u8 hdr_len;
22002208

22012209
buf_len -= skb->data_len;
22022210
nr_frags = skb_shinfo(skb)->nr_frags;
22032211
next_to_use = atomic_read(&tpd_ring->next_to_use);
2212+
first_mapped = next_to_use;
22042213
buffer_info = &tpd_ring->buffer_info[next_to_use];
22052214
BUG_ON(buffer_info->skb);
22062215
/* put skb in last TPD */
@@ -2216,6 +2225,8 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
22162225
buffer_info->dma = dma_map_page(&adapter->pdev->dev, page,
22172226
offset, hdr_len,
22182227
DMA_TO_DEVICE);
2228+
if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma))
2229+
goto dma_err;
22192230

22202231
if (++next_to_use == tpd_ring->count)
22212232
next_to_use = 0;
@@ -2242,6 +2253,9 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
22422253
page, offset,
22432254
buffer_info->length,
22442255
DMA_TO_DEVICE);
2256+
if (dma_mapping_error(&adapter->pdev->dev,
2257+
buffer_info->dma))
2258+
goto dma_err;
22452259
if (++next_to_use == tpd_ring->count)
22462260
next_to_use = 0;
22472261
}
@@ -2254,6 +2268,8 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
22542268
buffer_info->dma = dma_map_page(&adapter->pdev->dev, page,
22552269
offset, buf_len,
22562270
DMA_TO_DEVICE);
2271+
if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma))
2272+
goto dma_err;
22572273
if (++next_to_use == tpd_ring->count)
22582274
next_to_use = 0;
22592275
}
@@ -2277,6 +2293,9 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
22772293
buffer_info->dma = skb_frag_dma_map(&adapter->pdev->dev,
22782294
frag, i * ATL1_MAX_TX_BUF_LEN,
22792295
buffer_info->length, DMA_TO_DEVICE);
2296+
if (dma_mapping_error(&adapter->pdev->dev,
2297+
buffer_info->dma))
2298+
goto dma_err;
22802299

22812300
if (++next_to_use == tpd_ring->count)
22822301
next_to_use = 0;
@@ -2285,6 +2304,22 @@ static void atl1_tx_map(struct atl1_adapter *adapter, struct sk_buff *skb,
22852304

22862305
/* last tpd's buffer-info */
22872306
buffer_info->skb = skb;
2307+
2308+
return true;
2309+
2310+
dma_err:
2311+
while (first_mapped != next_to_use) {
2312+
buffer_info = &tpd_ring->buffer_info[first_mapped];
2313+
dma_unmap_page(&adapter->pdev->dev,
2314+
buffer_info->dma,
2315+
buffer_info->length,
2316+
DMA_TO_DEVICE);
2317+
buffer_info->dma = 0;
2318+
2319+
if (++first_mapped == tpd_ring->count)
2320+
first_mapped = 0;
2321+
}
2322+
return false;
22882323
}
22892324

22902325
static void atl1_tx_queue(struct atl1_adapter *adapter, u16 count,
@@ -2355,10 +2390,8 @@ static netdev_tx_t atl1_xmit_frame(struct sk_buff *skb,
23552390

23562391
len = skb_headlen(skb);
23572392

2358-
if (unlikely(skb->len <= 0)) {
2359-
dev_kfree_skb_any(skb);
2360-
return NETDEV_TX_OK;
2361-
}
2393+
if (unlikely(skb->len <= 0))
2394+
goto drop_packet;
23622395

23632396
nr_frags = skb_shinfo(skb)->nr_frags;
23642397
for (f = 0; f < nr_frags; f++) {
@@ -2371,10 +2404,9 @@ static netdev_tx_t atl1_xmit_frame(struct sk_buff *skb,
23712404
if (mss) {
23722405
if (skb->protocol == htons(ETH_P_IP)) {
23732406
proto_hdr_len = skb_tcp_all_headers(skb);
2374-
if (unlikely(proto_hdr_len > len)) {
2375-
dev_kfree_skb_any(skb);
2376-
return NETDEV_TX_OK;
2377-
}
2407+
if (unlikely(proto_hdr_len > len))
2408+
goto drop_packet;
2409+
23782410
/* need additional TPD ? */
23792411
if (proto_hdr_len != len)
23802412
count += (len - proto_hdr_len +
@@ -2406,23 +2438,26 @@ static netdev_tx_t atl1_xmit_frame(struct sk_buff *skb,
24062438
}
24072439

24082440
tso = atl1_tso(adapter, skb, ptpd);
2409-
if (tso < 0) {
2410-
dev_kfree_skb_any(skb);
2411-
return NETDEV_TX_OK;
2412-
}
2441+
if (tso < 0)
2442+
goto drop_packet;
24132443

24142444
if (!tso) {
24152445
ret_val = atl1_tx_csum(adapter, skb, ptpd);
2416-
if (ret_val < 0) {
2417-
dev_kfree_skb_any(skb);
2418-
return NETDEV_TX_OK;
2419-
}
2446+
if (ret_val < 0)
2447+
goto drop_packet;
24202448
}
24212449

2422-
atl1_tx_map(adapter, skb, ptpd);
2450+
if (!atl1_tx_map(adapter, skb, ptpd))
2451+
goto drop_packet;
2452+
24232453
atl1_tx_queue(adapter, count, ptpd);
24242454
atl1_update_mailbox(adapter);
24252455
return NETDEV_TX_OK;
2456+
2457+
drop_packet:
2458+
adapter->soft_stats.tx_errors++;
2459+
dev_kfree_skb_any(skb);
2460+
return NETDEV_TX_OK;
24262461
}
24272462

24282463
static int atl1_rings_clean(struct napi_struct *napi, int budget)

0 commit comments

Comments
 (0)