Received: by 2002:a9a:4c47:0:b029:116:c383:538 with SMTP id u7csp961235lko; Tue, 13 Jul 2021 13:58:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqipvgOafKMaIgO2n5oITFN3xU1Rycnvi1gBgy1EW1EjdgDY/YAugVauWSR6b9qDumivrP X-Received: by 2002:a05:6638:35a8:: with SMTP id v40mr5915954jal.126.1626209935214; Tue, 13 Jul 2021 13:58:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626209935; cv=none; d=google.com; s=arc-20160816; b=pPl9SF9FDx0+2XYzhAMM1mIBchn1flDayi++tHDej/rCVvWv1sxIHwNISx/e2YPCVw rGS4iiSojw1qQ2AArj9AClgt6JpIKO6uIgS73FWmE+PmBFcV7LnjUGJACHlcCzYWtj5v hsGtGDP/6QHHZQSoEVdqh4EbK+3d0/aEDGU8+wl4b+02gbkWi0oPAAGv5HggYNKidOow 99zsihcrwsq8eQjSlEogxnNVIDP2GQUFJu8UzBp8WTTvbHX8/+25pQ9io0BY4s905GY3 uQU7oxx9eySx/4xGkcFLF+QltdzVgMbKgmIQH9yGuC9wBBd1fB+tRUmLvnreEMcA5v8E YYCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:subject:from:dkim-signature; bh=wzD7p3eMscQSgryl/aNBA5sYqn+D84GYHaWrmA8jwMw=; b=LWAIiiczC7jbIw4PZ7wlWo3avMnN5c16CKE5I2SsWdkQ64our1vluSrFQccHxQBFBh Szz70DesmSypxS23OEIX8m/dKTpZlYY/wqpxj9NCEP3gA0v73drh6ISbZ+xR8b8dX5Jh s7Mioi4FMAqtkIHfU7QvO5paei9Su+sd5hq3LUG7kv1lm368Z2UqbSwg/oCru3uGRZZM uOdJcB1vPMeZZ9k90043UGiHL6dBL8C6oD6OzN7gKG1YZjK3Jn40Kl6TpkPrNsLfjkvj DrEVwi86RF0XuSJqCmQQkFuTy+L04/7TBBVOFJ+iL+gOX1lC9dms3Kf6owjWQnPsFiU6 378w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=yTpcUoq9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q3si25824736ilu.88.2021.07.13.13.58.42; Tue, 13 Jul 2021 13:58:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@virtuozzo.com header.s=relay header.b=yTpcUoq9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235658AbhGMVAs (ORCPT + 99 others); Tue, 13 Jul 2021 17:00:48 -0400 Received: from relay.sw.ru ([185.231.240.75]:56684 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235570AbhGMVAq (ORCPT ); Tue, 13 Jul 2021 17:00:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=Content-Type:MIME-Version:Date:Message-ID:Subject :From; bh=wzD7p3eMscQSgryl/aNBA5sYqn+D84GYHaWrmA8jwMw=; b=yTpcUoq9HpBnTWTwb39 2vH3hZ8vDeWwIkIZPntvKC2/F9UNEKAl8bKI9/W66QzpWO7novbG/xioZUJkXoZj3VyAwAr7gcDre ZD53NHUeyKJ+CgHHlSwveDs9d0qZkHkltk+2KKsu+3fygQ5QT6Z0E1k6et7mNghTvXGrF6kza9A=; Received: from [10.93.0.56] by relay.sw.ru with esmtp (Exim 4.94.2) (envelope-from ) id 1m3PTW-003sYb-2W; Tue, 13 Jul 2021 23:57:54 +0300 From: Vasily Averin Subject: [PATCH NET v2 1/7] skbuff: introduce skb_expand_head() To: "David S. Miller" , Hideaki YOSHIFUJI , David Ahern , Jakub Kicinski , Eric Dumazet Cc: netdev@vger.kernel.org, Joerg Reuter , Ralf Baechle , linux-hams@vger.kernel.org, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , bpf@vger.kernel.org, linux-kernel@vger.kernel.org References: <55c9e2ae-b060-baa2-460c-90eb3e9ded5c@virtuozzo.com> Message-ID: Date: Tue, 13 Jul 2021 23:57:53 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- include/linux/skbuff.h | 1 + net/core/skbuff.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index dbf820a..0003307 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -1174,6 +1174,7 @@ static inline struct sk_buff *__pskb_copy(struct sk_buff *skb, int headroom, int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail, gfp_t gfp_mask); struct sk_buff *skb_realloc_headroom(struct sk_buff *skb, unsigned int headroom); +struct sk_buff *skb_expand_head(struct sk_buff *skb, unsigned int headroom); struct sk_buff *skb_copy_expand(const struct sk_buff *skb, int newheadroom, int newtailroom, gfp_t priority); int __must_check skb_to_sgvec_nomark(struct sk_buff *skb, struct scatterlist *sg, diff --git a/net/core/skbuff.c b/net/core/skbuff.c index bbc3b4b..a7997c2 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -1769,6 +1769,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 -- 1.8.3.1