diff options
author | Vasily Averin <vvs@virtuozzo.com> | 2021-08-02 11:52:15 +0300 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2021-08-03 13:21:39 +0300 |
commit | f1260ff15a71b8fc122b2c9abd8a7abffb6e0168 (patch) | |
tree | 5296e2d92972ed29361756ad41d5c44d4b50f247 /net/core/skbuff.c | |
parent | fa976624ae7b6226d080ad21adb306ccb640a5ed (diff) | |
download | linux-f1260ff15a71b8fc122b2c9abd8a7abffb6e0168.tar.xz |
skbuff: introduce skb_expand_head()
Like skb_realloc_headroom(), new helper increases headroom of specified skb.
Unlike skb_realloc_headroom(), it does not allocate a new skb if possible;
copies skb->sk on new skb when as needed and frees original skb in case
of failures.
This helps to simplify ip[6]_finish_output2() and a few other similar cases.
Signed-off-by: Vasily Averin <vvs@virtuozzo.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core/skbuff.c')
-rw-r--r-- | net/core/skbuff.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/net/core/skbuff.c b/net/core/skbuff.c index fcbd977186b0..8bac7a1a81ba 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -1790,6 +1790,48 @@ struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom) EXPORT_SYMBOL(skb_realloc_headroom); /** + * skb_expand_head - reallocate header of &sk_buff + * @skb: buffer to reallocate + * @headroom: needed headroom + * + * Unlike skb_realloc_headroom, this one does not allocate a new skb + * if possible; copies skb->sk to new skb as needed + * and frees original skb in case of failures. + * + * It expect increased headroom and generates warning otherwise. + */ + +struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom) +{ + int delta = headroom - skb_headroom(skb); + + if (WARN_ONCE(delta <= 0, + "%s is expecting an increase in the headroom", __func__)) + return skb; + + /* pskb_expand_head() might crash, if skb is shared */ + if (skb_shared(skb)) { + struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC); + + if (likely(nskb)) { + if (skb->sk) + skb_set_owner_w(nskb, skb->sk); + consume_skb(skb); + } else { + kfree_skb(skb); + } + skb = nskb; + } + if (skb && + pskb_expand_head(skb, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) { + kfree_skb(skb); + skb = NULL; + } + return skb; +} +EXPORT_SYMBOL(skb_expand_head); + +/** * skb_copy_expand - copy and expand sk_buff * @skb: buffer to copy * @newheadroom: new free bytes at head |