Skip to content

Commit a580ea9

Browse files
minakuba-moo
authored andcommitted
net: mirror skb frag ref/unref helpers
Refactor some of the skb frag ref/unref helpers for improved clarity. Implement napi_pp_get_page() to be the mirror counterpart of napi_pp_put_page(). Implement skb_page_ref() to be the mirror of skb_page_unref(). Improve __skb_frag_ref() to become a mirror counterpart of __skb_frag_unref(). Previously unref could handle pp & non-pp pages, while the ref could only handle non-pp pages. Now both the ref & unref helpers can correctly handle both pp & non-pp pages. Now that __skb_frag_ref() can handle both pp & non-pp pages, remove skb_pp_frag_ref(), and use __skb_frag_ref() instead. This lets us remove pp specific handling from skb_try_coalesce. Additionally, since __skb_frag_ref() can now handle both pp & non-pp pages, a latent issue in skb_shift() should now be fixed. Previously this function would do a non-pp ref & pp unref on potential pp frags (fragfrom). After this patch, skb_shift() should correctly do a pp ref/unref on pp frags. Signed-off-by: Mina Almasry <almasrymina@google.com> Reviewed-by: Dragos Tatulea <dtatulea@nvidia.com> Reviewed-by: Jacob Keller <jacob.e.keller@intel.com> Link: https://lore.kernel.org/r/20240410190505.1225848-3-almasrymina@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent f6d827b commit a580ea9

File tree

6 files changed

+44
-51
lines changed

6 files changed

+44
-51
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]);
1662+
__skb_frag_ref(&skb_shinfo(nskb)->frags[i], nskb->pp_recycle);
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);
2003+
__skb_frag_ref(frag, skb->pp_recycle);
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);
2027+
__skb_frag_ref(frag, skb->pp_recycle);
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]);
720+
__skb_frag_ref(&sinfo->frags[i], false);
721721
}
722722

723723
static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,

include/linux/skbuff_ref.h

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,47 @@
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+
}
1139

1240
/**
1341
* __skb_frag_ref - take an addition reference on a paged fragment.
1442
* @frag: the paged fragment
43+
* @recycle: skb->pp_recycle param of the parent skb. False if no parent skb.
1544
*
16-
* Takes an additional reference on the paged fragment @frag.
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.
1748
*/
18-
static inline void __skb_frag_ref(skb_frag_t *frag)
49+
static inline void __skb_frag_ref(skb_frag_t *frag, bool recycle)
1950
{
20-
get_page(skb_frag_page(frag));
51+
skb_page_ref(skb_frag_page(frag), recycle);
2152
}
2253

2354
/**
@@ -29,7 +60,7 @@ static inline void __skb_frag_ref(skb_frag_t *frag)
2960
*/
3061
static inline void skb_frag_ref(struct sk_buff *skb, int f)
3162
{
32-
__skb_frag_ref(&skb_shinfo(skb)->frags[f]);
63+
__skb_frag_ref(&skb_shinfo(skb)->frags[f], skb->pp_recycle);
3364
}
3465

3566
bool napi_pp_put_page(struct page *page);

net/core/skbuff.c

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

910-
static bool is_pp_page(struct page *page)
911-
{
912-
return (page->pp_magic & ~0x3UL) == PP_SIGNATURE;
913-
}
914-
915910
int skb_pp_cow_data(struct page_pool *pool, struct sk_buff **pskb,
916911
unsigned int headroom)
917912
{
@@ -1033,37 +1028,6 @@ static bool skb_pp_recycle(struct sk_buff *skb, void *data)
10331028
return napi_pp_put_page(virt_to_page(data));
10341029
}
10351030

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

41784142
} else {
4179-
__skb_frag_ref(fragfrom);
4143+
__skb_frag_ref(fragfrom, skb->pp_recycle);
41804144
skb_frag_page_copy(fragto, fragfrom);
41814145
skb_frag_off_copy(fragto, fragfrom);
41824146
skb_frag_size_set(fragto, todo);
@@ -4826,7 +4790,7 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
48264790
}
48274791

48284792
*nskb_frag = (i < 0) ? skb_head_frag_to_page_desc(frag_skb) : *frag;
4829-
__skb_frag_ref(nskb_frag);
4793+
__skb_frag_ref(nskb_frag, nskb->pp_recycle);
48304794
size = skb_frag_size(nskb_frag);
48314795

48324796
if (pos < offset) {
@@ -5957,10 +5921,8 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
59575921
/* if the skb is not cloned this does nothing
59585922
* since we set nr_frags to 0.
59595923
*/
5960-
if (skb_pp_frag_ref(from)) {
5961-
for (i = 0; i < from_shinfo->nr_frags; i++)
5962-
__skb_frag_ref(&from_shinfo->frags[i]);
5963-
}
5924+
for (i = 0; i < from_shinfo->nr_frags; i++)
5925+
__skb_frag_ref(&from_shinfo->frags[i], from->pp_recycle);
59645926

59655927
to->truesize += delta;
59665928
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);
281+
__skb_frag_ref(frag, false);
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)