Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1643213pxb; Mon, 22 Feb 2021 07:20:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJw0MA+mp8SK3+i3uU7YqBOkgjP5zcwhyeN3Sqj/WsC+AdHEA1KZropPYtFo2V8V6eIzzN90 X-Received: by 2002:a17:906:254e:: with SMTP id j14mr5521940ejb.452.1614007206125; Mon, 22 Feb 2021 07:20:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614007206; cv=none; d=google.com; s=arc-20160816; b=dIgosdCgwhbH7KOaYrBPisFoAH0Bcwy2y6It+BbOBlcpyc+PNN79781ojSuxA9N2yL 6lmXAAUz6EMX0YfTo4B9OT+yKv1webT9hkq5TEWAIL11uG8krH244a9Mm5B8lxtG+hgk byJUnK1lY92RmIXm/1G4wkmo6Et07L/pXmpYFcsrUfDf1l3OM6Ux4YVzF+BnmVq6K1k4 6Ek+/JWskbUZjqz6IxJ74upRTr5ua5ehpcK8iCOUD3bVNx3j+9IPJsQjnQOhFkS7MFdD 1gto/HOzrZH63rbyXHjTFekUV3iX/DBPV8N8kDYCqC/M70BtBAQ4UJVV+y5OARkjDeiz ISDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=A4JqFHancUKdkg+evBj18KZrLiX5JyaG5qBYPZeye6s=; b=cOXKb48lDmt2mb+yXgCzzrrOXdw1RyKqQaU3KkV/RbnYaRjSHKTlhq//3W8Hoq2prA nePyJAPw9fxXrCih3nJN/oQDtBEZjfi0byuPzduVzJ+8PER/Z/WBncFlXu1wdNLTk3jc BjviihwXZexmLhhdRwYN3WVVEbqw156v/CrYjdwAQIQQtLM2oyjlnnGvBRbK52mATwDQ cFjwvef/lSRsf594bpKWr0/mp309S1o8akDV6W9vxxG++53MKXgPkoDdV+ytA7hLG3G5 MMoky+QDufMdl4Sg3yN6w/+YjZcP6nGUp4cmaYopVKuJC/Wyn/52u7Evq/U0iiGGQtH7 4bBw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gn28si11298936ejc.636.2021.02.22.07.19.41; Mon, 22 Feb 2021 07:20:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231486AbhBVPQl (ORCPT + 99 others); Mon, 22 Feb 2021 10:16:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231348AbhBVPPH (ORCPT ); Mon, 22 Feb 2021 10:15:07 -0500 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CBF6C061A27 for ; Mon, 22 Feb 2021 07:13:00 -0800 (PST) Received: from dude.hi.pengutronix.de ([2001:67c:670:100:1d::7]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lECtF-0007ds-15; Mon, 22 Feb 2021 16:12:49 +0100 Received: from ore by dude.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lECtE-00071U-AA; Mon, 22 Feb 2021 16:12:48 +0100 From: Oleksij Rempel To: mkl@pengutronix.de, "David S. Miller" , Jakub Kicinski , Oliver Hartkopp , Robin van der Gracht , Johannes Berg Cc: Oleksij Rempel , kernel@pengutronix.de, linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Dumazet , linux-wireless@vger.kernel.org Subject: [PATCH net v1 1/3] skbuff: skb_clone_sk_optional(): add function to always clone a skb and increase refcount on sk if valid Date: Mon, 22 Feb 2021 16:12:45 +0100 Message-Id: <20210222151247.24534-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210222151247.24534-1-o.rempel@pengutronix.de> References: <20210222151247.24534-1-o.rempel@pengutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::7 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-wireless@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org There already the function skb_clone_sk(), which clones the skb, but only if the sk is valid. There are several users in the networking stack, which always need a clone of a skb with the sk refcount incremented (but only if the sk is valid). This patch adds such a function. Signed-off-by: Oleksij Rempel --- include/linux/skbuff.h | 1 + net/core/skbuff.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h index 6d0a33d1c0db..99d552017508 100644 --- a/include/linux/skbuff.h +++ b/include/linux/skbuff.h @@ -3874,6 +3874,7 @@ static inline void skb_metadata_clear(struct sk_buff *skb) skb_metadata_set(skb, 0); } +struct sk_buff *skb_clone_sk_optional(struct sk_buff *skb); struct sk_buff *skb_clone_sk(struct sk_buff *skb); #ifdef CONFIG_NETWORK_PHY_TIMESTAMPING diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 545a472273a5..97341f173fb0 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -4671,6 +4671,33 @@ struct sk_buff *sock_dequeue_err_skb(struct sock *sk) } EXPORT_SYMBOL(sock_dequeue_err_skb); +/** + * skb_clone_sk_optional - create clone of skb, and take reference to socket if + * socket is referenced in original skb + * @skb: the skb to clone + * + * This function always creates a clone of a buffer. If it that holds a valid + * reference on sk_refcnt this is increased. + */ +struct sk_buff *skb_clone_sk_optional(struct sk_buff *skb) +{ + struct sock *sk = skb->sk; + struct sk_buff *clone; + + clone = skb_clone(skb, GFP_ATOMIC); + if (!clone) + return NULL; + + if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt)) + return clone; + + clone->sk = sk; + clone->destructor = sock_efree; + + return clone; +} +EXPORT_SYMBOL(skb_clone_sk_optional); + /** * skb_clone_sk - create clone of skb, and take reference to socket * @skb: the skb to clone -- 2.29.2