Skip to content

Commit d0bf4a9

Browse files
edumazetdavem330
authored andcommitted
net: cleanup and document skb fclone layout
Lets use a proper structure to clearly document and implement skb fast clones. Then, we might experiment more easily alternative layouts. This patch adds a new skb_fclone_busy() helper, used by tcp and xfrm, to stop leaking of implementation details. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b248230 commit d0bf4a9

4 files changed

Lines changed: 47 additions & 28 deletions

File tree

‎include/linux/skbuff.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,31 @@ struct sk_buff *alloc_skb_with_frags(unsigned long header_len,
781781
int *errcode,
782782
gfp_t gfp_mask);
783783

784+
/* Layout of fast clones : [skb1][skb2][fclone_ref] */
785+
struct sk_buff_fclones {
786+
struct sk_buff skb1;
787+
788+
struct sk_buff skb2;
789+
790+
atomic_t fclone_ref;
791+
};
792+
793+
/**
794+
* skb_fclone_busy - check if fclone is busy
795+
* @skb: buffer
796+
*
797+
* Returns true is skb is a fast clone, and its clone is not freed.
798+
*/
799+
static inline bool skb_fclone_busy(const struct sk_buff *skb)
800+
{
801+
const struct sk_buff_fclones *fclones;
802+
803+
fclones = container_of(skb, struct sk_buff_fclones, skb1);
804+
805+
return skb->fclone == SKB_FCLONE_ORIG &&
806+
fclones->skb2.fclone == SKB_FCLONE_CLONE;
807+
}
808+
784809
static inline struct sk_buff *alloc_skb_fclone(unsigned int size,
785810
gfp_t priority)
786811
{

‎net/core/skbuff.c‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,16 @@ struct sk_buff *__alloc_skb(unsigned int size, gfp_t gfp_mask,
257257
kmemcheck_annotate_variable(shinfo->destructor_arg);
258258

259259
if (flags & SKB_ALLOC_FCLONE) {
260-
struct sk_buff *child = skb + 1;
261-
atomic_t *fclone_ref = (atomic_t *) (child + 1);
260+
struct sk_buff_fclones *fclones;
262261

263-
kmemcheck_annotate_bitfield(child, flags1);
262+
fclones = container_of(skb, struct sk_buff_fclones, skb1);
263+
264+
kmemcheck_annotate_bitfield(&fclones->skb2, flags1);
264265
skb->fclone = SKB_FCLONE_ORIG;
265-
atomic_set(fclone_ref, 1);
266+
atomic_set(&fclones->fclone_ref, 1);
266267

267-
child->fclone = SKB_FCLONE_UNAVAILABLE;
268-
child->pfmemalloc = pfmemalloc;
268+
fclones->skb2.fclone = SKB_FCLONE_UNAVAILABLE;
269+
fclones->skb2.pfmemalloc = pfmemalloc;
269270
}
270271
out:
271272
return skb;
@@ -524,31 +525,29 @@ static void skb_release_data(struct sk_buff *skb)
524525
*/
525526
static void kfree_skbmem(struct sk_buff *skb)
526527
{
527-
struct sk_buff *other;
528-
atomic_t *fclone_ref;
528+
struct sk_buff_fclones *fclones;
529529

530530
switch (skb->fclone) {
531531
case SKB_FCLONE_UNAVAILABLE:
532532
kmem_cache_free(skbuff_head_cache, skb);
533533
break;
534534

535535
case SKB_FCLONE_ORIG:
536-
fclone_ref = (atomic_t *) (skb + 2);
537-
if (atomic_dec_and_test(fclone_ref))
538-
kmem_cache_free(skbuff_fclone_cache, skb);
536+
fclones = container_of(skb, struct sk_buff_fclones, skb1);
537+
if (atomic_dec_and_test(&fclones->fclone_ref))
538+
kmem_cache_free(skbuff_fclone_cache, fclones);
539539
break;
540540

541541
case SKB_FCLONE_CLONE:
542-
fclone_ref = (atomic_t *) (skb + 1);
543-
other = skb - 1;
542+
fclones = container_of(skb, struct sk_buff_fclones, skb2);
544543

545544
/* The clone portion is available for
546545
* fast-cloning again.
547546
*/
548547
skb->fclone = SKB_FCLONE_UNAVAILABLE;
549548

550-
if (atomic_dec_and_test(fclone_ref))
551-
kmem_cache_free(skbuff_fclone_cache, other);
549+
if (atomic_dec_and_test(&fclones->fclone_ref))
550+
kmem_cache_free(skbuff_fclone_cache, fclones);
552551
break;
553552
}
554553
}
@@ -859,17 +858,18 @@ EXPORT_SYMBOL_GPL(skb_copy_ubufs);
859858

860859
struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
861860
{
862-
struct sk_buff *n;
861+
struct sk_buff_fclones *fclones = container_of(skb,
862+
struct sk_buff_fclones,
863+
skb1);
864+
struct sk_buff *n = &fclones->skb2;
863865

864866
if (skb_orphan_frags(skb, gfp_mask))
865867
return NULL;
866868

867-
n = skb + 1;
868869
if (skb->fclone == SKB_FCLONE_ORIG &&
869870
n->fclone == SKB_FCLONE_UNAVAILABLE) {
870-
atomic_t *fclone_ref = (atomic_t *) (n + 1);
871871
n->fclone = SKB_FCLONE_CLONE;
872-
atomic_inc(fclone_ref);
872+
atomic_inc(&fclones->fclone_ref);
873873
} else {
874874
if (skb_pfmemalloc(skb))
875875
gfp_mask |= __GFP_MEMALLOC;
@@ -3240,8 +3240,7 @@ void __init skb_init(void)
32403240
SLAB_HWCACHE_ALIGN|SLAB_PANIC,
32413241
NULL);
32423242
skbuff_fclone_cache = kmem_cache_create("skbuff_fclone_cache",
3243-
(2*sizeof(struct sk_buff)) +
3244-
sizeof(atomic_t),
3243+
sizeof(struct sk_buff_fclones),
32453244
0,
32463245
SLAB_HWCACHE_ALIGN|SLAB_PANIC,
32473246
NULL);

‎net/ipv4/tcp_output.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,10 +2110,7 @@ bool tcp_schedule_loss_probe(struct sock *sk)
21102110
static bool skb_still_in_host_queue(const struct sock *sk,
21112111
const struct sk_buff *skb)
21122112
{
2113-
const struct sk_buff *fclone = skb + 1;
2114-
2115-
if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
2116-
fclone->fclone == SKB_FCLONE_CLONE)) {
2113+
if (unlikely(skb_fclone_busy(skb))) {
21172114
NET_INC_STATS_BH(sock_net(sk),
21182115
LINUX_MIB_TCPSPURIOUS_RTX_HOSTQUEUES);
21192116
return true;

‎net/xfrm/xfrm_policy.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,10 +1961,8 @@ static int xdst_queue_output(struct sock *sk, struct sk_buff *skb)
19611961
struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
19621962
struct xfrm_policy *pol = xdst->pols[0];
19631963
struct xfrm_policy_queue *pq = &pol->polq;
1964-
const struct sk_buff *fclone = skb + 1;
19651964

1966-
if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1967-
fclone->fclone == SKB_FCLONE_CLONE)) {
1965+
if (unlikely(skb_fclone_busy(skb))) {
19681966
kfree_skb(skb);
19691967
return 0;
19701968
}

0 commit comments

Comments
 (0)