Skip to content

Commit 36ccdf8

Browse files
Björn Töpelborkmann
authored andcommitted
net, xsk: Avoid taking multiple skbuff references
Commit 642e450 ("xsk: Do not discard packet when NETDEV_TX_BUSY") addressed the problem that packets were discarded from the Tx AF_XDP ring, when the driver returned NETDEV_TX_BUSY. Part of the fix was bumping the skbuff reference count, so that the buffer would not be freed by dev_direct_xmit(). A reference count larger than one means that the skbuff is "shared", which is not the case. If the "shared" skbuff is sent to the generic XDP receive path, netif_receive_generic_xdp(), and pskb_expand_head() is entered the BUG_ON(skb_shared(skb)) will trigger. This patch adds a variant to dev_direct_xmit(), __dev_direct_xmit(), where a user can select the skbuff free policy. This allows AF_XDP to avoid bumping the reference count, but still keep the NETDEV_TX_BUSY behavior. Fixes: 642e450 ("xsk: Do not discard packet when NETDEV_TX_BUSY") Reported-by: Yonghong Song <yhs@fb.com> Signed-off-by: Björn Töpel <bjorn.topel@intel.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20201123175600.146255-1-bjorn.topel@gmail.com
1 parent 1786489 commit 36ccdf8

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

include/linux/netdevice.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2813,9 +2813,21 @@ u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
28132813
struct net_device *sb_dev);
28142814
u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
28152815
struct net_device *sb_dev);
2816+
28162817
int dev_queue_xmit(struct sk_buff *skb);
28172818
int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev);
2818-
int dev_direct_xmit(struct sk_buff *skb, u16 queue_id);
2819+
int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id);
2820+
2821+
static inline int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
2822+
{
2823+
int ret;
2824+
2825+
ret = __dev_direct_xmit(skb, queue_id);
2826+
if (!dev_xmit_complete(ret))
2827+
kfree_skb(skb);
2828+
return ret;
2829+
}
2830+
28192831
int register_netdevice(struct net_device *dev);
28202832
void unregister_netdevice_queue(struct net_device *dev, struct list_head *head);
28212833
void unregister_netdevice_many(struct list_head *head);

net/core/dev.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,7 +4180,7 @@ int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
41804180
}
41814181
EXPORT_SYMBOL(dev_queue_xmit_accel);
41824182

4183-
int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4183+
int __dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
41844184
{
41854185
struct net_device *dev = skb->dev;
41864186
struct sk_buff *orig_skb = skb;
@@ -4210,17 +4210,13 @@ int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
42104210
dev_xmit_recursion_dec();
42114211

42124212
local_bh_enable();
4213-
4214-
if (!dev_xmit_complete(ret))
4215-
kfree_skb(skb);
4216-
42174213
return ret;
42184214
drop:
42194215
atomic_long_inc(&dev->tx_dropped);
42204216
kfree_skb_list(skb);
42214217
return NET_XMIT_DROP;
42224218
}
4223-
EXPORT_SYMBOL(dev_direct_xmit);
4219+
EXPORT_SYMBOL(__dev_direct_xmit);
42244220

42254221
/*************************************************************************
42264222
* Receiver routines

net/xdp/xsk.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,7 @@ static int xsk_generic_xmit(struct sock *sk)
411411
skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
412412
skb->destructor = xsk_destruct_skb;
413413

414-
/* Hinder dev_direct_xmit from freeing the packet and
415-
* therefore completing it in the destructor
416-
*/
417-
refcount_inc(&skb->users);
418-
err = dev_direct_xmit(skb, xs->queue_id);
414+
err = __dev_direct_xmit(skb, xs->queue_id);
419415
if (err == NETDEV_TX_BUSY) {
420416
/* Tell user-space to retry the send */
421417
skb->destructor = sock_wfree;
@@ -429,12 +425,10 @@ static int xsk_generic_xmit(struct sock *sk)
429425
/* Ignore NET_XMIT_CN as packet might have been sent */
430426
if (err == NET_XMIT_DROP) {
431427
/* SKB completed but not sent */
432-
kfree_skb(skb);
433428
err = -EBUSY;
434429
goto out;
435430
}
436431

437-
consume_skb(skb);
438432
sent_frame = true;
439433
}
440434

0 commit comments

Comments
 (0)