Skip to content

Commit 2cc3aeb

Browse files
apalosdavem330
authored andcommitted
skbuff: Fix a potential race while recycling page_pool packets
As Alexander points out, when we are trying to recycle a cloned/expanded SKB we might trigger a race. The recycling code relies on the pp_recycle bit to trigger, which we carry over to cloned SKBs. If that cloned SKB gets expanded or if we get references to the frags, call skb_release_data() and overwrite skb->head, we are creating separate instances accessing the same page frags. Since the skb_release_data() will first try to recycle the frags, there's a potential race between the original and cloned SKB, since both will have the pp_recycle bit set. Fix this by explicitly those SKBs not recyclable. The atomic_sub_return effectively limits us to a single release case, and when we are calling skb_release_data we are also releasing the option to perform the recycling, or releasing the pages from the page pool. Fixes: 6a5bcd8 ("page_pool: Allow drivers to hint on SKB recycling") Reported-by: Alexander Duyck <alexanderduyck@fb.com> Suggested-by: Alexander Duyck <alexanderduyck@fb.com> Reviewed-by: Alexander Duyck <alexanderduyck@fb.com> Acked-by: Jesper Dangaard Brouer <brouer@redhat.com> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 20192d9 commit 2cc3aeb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

net/core/skbuff.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ static void skb_release_data(struct sk_buff *skb)
663663
if (skb->cloned &&
664664
atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1,
665665
&shinfo->dataref))
666-
return;
666+
goto exit;
667667

668668
skb_zcopy_clear(skb, true);
669669

@@ -674,6 +674,17 @@ static void skb_release_data(struct sk_buff *skb)
674674
kfree_skb_list(shinfo->frag_list);
675675

676676
skb_free_head(skb);
677+
exit:
678+
/* When we clone an SKB we copy the reycling bit. The pp_recycle
679+
* bit is only set on the head though, so in order to avoid races
680+
* while trying to recycle fragments on __skb_frag_unref() we need
681+
* to make one SKB responsible for triggering the recycle path.
682+
* So disable the recycling bit if an SKB is cloned and we have
683+
* additional references to to the fragmented part of the SKB.
684+
* Eventually the last SKB will have the recycling bit set and it's
685+
* dataref set to 0, which will trigger the recycling
686+
*/
687+
skb->pp_recycle = 0;
677688
}
678689

679690
/*

0 commit comments

Comments
 (0)