Skip to content

Commit cf413df

Browse files
mfijalkoKernel Patches Daemon
authored andcommitted
xsk: remove @first_frag from xsk_build_skb()
Devices that set IFF_TX_SKB_NO_LINEAR will not execute branch that handles metadata, as we set @first_frag only for !IFF_TX_SKB_NO_LINEAR code in xsk_build_skb(). Same functionality can be achieved with checking if xsk_get_num_desc() returns 0. To replace current usage of @first_frag with XSKCB(skb)->num_descs check, pull out the code from xsk_set_destructor_arg() that initializes sk_buff::cb and call it before skb_store_bits() in branch that creates skb against first processed frag. This so error path has the XSKCB(skb)->num_descs initialized and can free skb in case skb_store_bits() failed. Signed-off-by: Maciej Fijalkowski <[email protected]>
1 parent 749affa commit cf413df

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

net/xdp/xsk.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,13 @@ static u32 xsk_get_num_desc(struct sk_buff *skb)
605605
return XSKCB(skb)->num_descs;
606606
}
607607

608+
static void xsk_init_cb(struct sk_buff *skb)
609+
{
610+
BUILD_BUG_ON(sizeof(struct xsk_addr_head) > sizeof(skb->cb));
611+
INIT_LIST_HEAD(&XSKCB(skb)->addrs_list);
612+
XSKCB(skb)->num_descs = 0;
613+
}
614+
608615
static void xsk_destruct_skb(struct sk_buff *skb)
609616
{
610617
struct xsk_tx_metadata_compl *compl = &skb_shinfo(skb)->xsk_meta;
@@ -620,9 +627,6 @@ static void xsk_destruct_skb(struct sk_buff *skb)
620627

621628
static void xsk_set_destructor_arg(struct sk_buff *skb, u64 addr)
622629
{
623-
BUILD_BUG_ON(sizeof(struct xsk_addr_head) > sizeof(skb->cb));
624-
INIT_LIST_HEAD(&XSKCB(skb)->addrs_list);
625-
XSKCB(skb)->num_descs = 0;
626630
skb_shinfo(skb)->destructor_arg = (void *)(uintptr_t)addr;
627631
}
628632

@@ -672,7 +676,7 @@ static struct sk_buff *xsk_build_skb_zerocopy(struct xdp_sock *xs,
672676
return ERR_PTR(err);
673677

674678
skb_reserve(skb, hr);
675-
679+
xsk_init_cb(skb);
676680
xsk_set_destructor_arg(skb, desc->addr);
677681
} else {
678682
xsk_addr = kmem_cache_zalloc(xsk_tx_generic_cache, GFP_KERNEL);
@@ -725,7 +729,6 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
725729
struct xsk_tx_metadata *meta = NULL;
726730
struct net_device *dev = xs->dev;
727731
struct sk_buff *skb = xs->skb;
728-
bool first_frag = false;
729732
int err;
730733

731734
if (dev->priv_flags & IFF_TX_SKB_NO_LINEAR) {
@@ -742,8 +745,6 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
742745
len = desc->len;
743746

744747
if (!skb) {
745-
first_frag = true;
746-
747748
hr = max(NET_SKB_PAD, L1_CACHE_ALIGN(dev->needed_headroom));
748749
tr = dev->needed_tailroom;
749750
skb = sock_alloc_send_skb(&xs->sk, hr + len + tr, 1, &err);
@@ -752,6 +753,7 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
752753

753754
skb_reserve(skb, hr);
754755
skb_put(skb, len);
756+
xsk_init_cb(skb);
755757

756758
err = skb_store_bits(skb, 0, buffer, len);
757759
if (unlikely(err))
@@ -797,7 +799,7 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
797799
list_add_tail(&xsk_addr->addr_node, &XSKCB(skb)->addrs_list);
798800
}
799801

800-
if (first_frag && desc->options & XDP_TX_METADATA) {
802+
if (!xsk_get_num_desc(skb) && desc->options & XDP_TX_METADATA) {
801803
if (unlikely(xs->pool->tx_metadata_len == 0)) {
802804
err = -EINVAL;
803805
goto free_err;
@@ -839,7 +841,7 @@ static struct sk_buff *xsk_build_skb(struct xdp_sock *xs,
839841
return skb;
840842

841843
free_err:
842-
if (first_frag && skb)
844+
if (skb && !xsk_get_num_desc(skb))
843845
kfree_skb(skb);
844846

845847
if (err == -EOVERFLOW) {

0 commit comments

Comments
 (0)