Skip to content

Commit 173e762

Browse files
minakuba-moo
authored andcommitted
Revert "net: mirror skb frag ref/unref helpers"
This reverts commit a580ea9. This revert is to resolve Dragos's report of page_pool leak here: https://lore.kernel.org/lkml/20240424165646.1625690-2-dtatulea@nvidia.com/ The reverted patch interacts very badly with commit 2cc3aeb ("skbuff: Fix a potential race while recycling page_pool packets"). The reverted commit hopes that the pp_recycle + is_pp_page variables do not change between the skb_frag_ref and skb_frag_unref operation. If such a change occurs, the skb_frag_ref/unref will not operate on the same reference type. In the case of Dragos's report, the grabbed ref was a pp ref, but the unref was a page ref, because the pp_recycle setting on the skb was changed. Attempting to fix this issue on the fly is risky. Lets revert and I hope to reland this with better understanding and testing to ensure we don't regress some edge case while streamlining skb reffing. Fixes: a580ea9 ("net: mirror skb frag ref/unref helpers") Reported-by: Dragos Tatulea <dtatulea@nvidia.com> Signed-off-by: Mina Almasry <almasrymina@google.com> Link: https://lore.kernel.org/r/20240502175423.2456544-1-almasrymina@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 5bfadc5 commit 173e762

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

drivers/net/ethernet/chelsio/inline_crypto/ch_ktls/chcr_ktls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ static void chcr_ktls_copy_record_in_skb(struct sk_buff *nskb,
16591659
for (i = 0; i < record->num_frags; i++) {
16601660
skb_shinfo(nskb)->frags[i] = record->frags[i];
16611661
/* increase the frag ref count */
1662-
__skb_frag_ref(&skb_shinfo(nskb)->frags[i], nskb->pp_recycle);
1662+
__skb_frag_ref(&skb_shinfo(nskb)->frags[i]);
16631663
}
16641664

16651665
skb_shinfo(nskb)->nr_frags = record->num_frags;

drivers/net/ethernet/sun/cassini.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,7 +2000,7 @@ static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
20002000
skb->len += hlen - swivel;
20012001

20022002
skb_frag_fill_page_desc(frag, page->buffer, off, hlen - swivel);
2003-
__skb_frag_ref(frag, skb->pp_recycle);
2003+
__skb_frag_ref(frag);
20042004

20052005
/* any more data? */
20062006
if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
@@ -2024,7 +2024,7 @@ static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
20242024
frag++;
20252025

20262026
skb_frag_fill_page_desc(frag, page->buffer, 0, hlen);
2027-
__skb_frag_ref(frag, skb->pp_recycle);
2027+
__skb_frag_ref(frag);
20282028
RX_USED_ADD(page, hlen + cp->crc_size);
20292029
}
20302030

drivers/net/veth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ static void veth_xdp_get(struct xdp_buff *xdp)
717717
return;
718718

719719
for (i = 0; i < sinfo->nr_frags; i++)
720-
__skb_frag_ref(&sinfo->frags[i], false);
720+
__skb_frag_ref(&sinfo->frags[i]);
721721
}
722722

723723
static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,

include/linux/skbuff_ref.h

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,16 @@
88
#define _LINUX_SKBUFF_REF_H
99

1010
#include <linux/skbuff.h>
11-
#include <net/page_pool/helpers.h>
12-
13-
#ifdef CONFIG_PAGE_POOL
14-
static inline bool is_pp_page(struct page *page)
15-
{
16-
return (page->pp_magic & ~0x3UL) == PP_SIGNATURE;
17-
}
18-
19-
static inline bool napi_pp_get_page(struct page *page)
20-
{
21-
page = compound_head(page);
22-
23-
if (!is_pp_page(page))
24-
return false;
25-
26-
page_pool_ref_page(page);
27-
return true;
28-
}
29-
#endif
30-
31-
static inline void skb_page_ref(struct page *page, bool recycle)
32-
{
33-
#ifdef CONFIG_PAGE_POOL
34-
if (recycle && napi_pp_get_page(page))
35-
return;
36-
#endif
37-
get_page(page);
38-
}
3911

4012
/**
4113
* __skb_frag_ref - take an addition reference on a paged fragment.
4214
* @frag: the paged fragment
43-
* @recycle: skb->pp_recycle param of the parent skb. False if no parent skb.
4415
*
45-
* Takes an additional reference on the paged fragment @frag. Obtains the
46-
* correct reference count depending on whether skb->pp_recycle is set and
47-
* whether the frag is a page pool frag.
16+
* Takes an additional reference on the paged fragment @frag.
4817
*/
49-
static inline void __skb_frag_ref(skb_frag_t *frag, bool recycle)
18+
static inline void __skb_frag_ref(skb_frag_t *frag)
5019
{
51-
skb_page_ref(skb_frag_page(frag), recycle);
20+
get_page(skb_frag_page(frag));
5221
}
5322

5423
/**
@@ -60,7 +29,7 @@ static inline void __skb_frag_ref(skb_frag_t *frag, bool recycle)
6029
*/
6130
static inline void skb_frag_ref(struct sk_buff *skb, int f)
6231
{
63-
__skb_frag_ref(&skb_shinfo(skb)->frags[f], skb->pp_recycle);
32+
__skb_frag_ref(&skb_shinfo(skb)->frags[f]);
6433
}
6534

6635
bool napi_pp_put_page(struct page *page);

net/core/skbuff.c

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,11 @@ static void skb_clone_fraglist(struct sk_buff *skb)
904904
skb_get(list);
905905
}
906906

907+
static bool is_pp_page(struct page *page)
908+
{
909+
return (page->pp_magic & ~0x3UL) == PP_SIGNATURE;
910+
}
911+
907912
int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
908913
unsigned int headroom)
909914
{
@@ -1025,6 +1030,37 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data)
10251030
return napi_pp_put_page(virt_to_page(data));
10261031
}
10271032

1033+
/**
1034+
* skb_pp_frag_ref() - Increase fragment references of a page pool aware skb
1035+
* @skb: page pool aware skb
1036+
*
1037+
* Increase the fragment reference count (pp_ref_count) of a skb. This is
1038+
* intended to gain fragment references only for page pool aware skbs,
1039+
* i.e. when skb->pp_recycle is true, and not for fragments in a
1040+
* non-pp-recycling skb. It has a fallback to increase references on normal
1041+
* pages, as page pool aware skbs may also have normal page fragments.
1042+
*/
1043+
static int skb_pp_frag_ref(struct sk_buff *skb)
1044+
{
1045+
struct skb_shared_info *shinfo;
1046+
struct page *head_page;
1047+
int i;
1048+
1049+
if (!skb->pp_recycle)
1050+
return -EINVAL;
1051+
1052+
shinfo = skb_shinfo(skb);
1053+
1054+
for (i = 0; i < shinfo->nr_frags; i++) {
1055+
head_page = compound_head(skb_frag_page(&shinfo->frags[i]));
1056+
if (likely(is_pp_page(head_page)))
1057+
page_pool_ref_page(head_page);
1058+
else
1059+
page_ref_inc(head_page);
1060+
}
1061+
return 0;
1062+
}
1063+
10281064
static void skb_kfree_head(void *head, unsigned int end_offset)
10291065
{
10301066
if (end_offset == SKB_SMALL_HEAD_HEADROOM)
@@ -4160,7 +4196,7 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
41604196
to++;
41614197

41624198
} else {
4163-
__skb_frag_ref(fragfrom, skb->pp_recycle);
4199+
__skb_frag_ref(fragfrom);
41644200
skb_frag_page_copy(fragto, fragfrom);
41654201
skb_frag_off_copy(fragto, fragfrom);
41664202
skb_frag_size_set(fragto, todo);
@@ -4810,7 +4846,7 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
48104846
}
48114847

48124848
*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4813-
__skb_frag_ref(nskb_frag, nskb->pp_recycle);
4849+
__skb_frag_ref(nskb_frag);
48144850
size = skb_frag_size(nskb_frag);
48154851

48164852
if (pos < offset) {
@@ -5941,8 +5977,10 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
59415977
/* if the skb is not cloned this does nothing
59425978
* since we set nr_frags to 0.
59435979
*/
5944-
for (i = 0; i < from_shinfo->nr_frags; i++)
5945-
__skb_frag_ref(&from_shinfo->frags[i], from->pp_recycle);
5980+
if (skb_pp_frag_ref(from)) {
5981+
for (i = 0; i < from_shinfo->nr_frags; i++)
5982+
__skb_frag_ref(&from_shinfo->frags[i]);
5983+
}
59465984

59475985
to->truesize += delta;
59485986
to->len += len;

net/tls/tls_device_fallback.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static int fill_sg_in(struct scatterlist *sg_in,
278278
for (i = 0; remaining > 0; i++) {
279279
skb_frag_t *frag = &record->frags[i];
280280

281-
__skb_frag_ref(frag, false);
281+
__skb_frag_ref(frag);
282282
sg_set_page(sg_in + i, skb_frag_page(frag),
283283
skb_frag_size(frag), skb_frag_off(frag));
284284

0 commit comments

Comments
 (0)