From patchwork Fri Jul 16 07:02:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 478359 Delivered-To: patch@linaro.org Received: by 2002:a02:c94a:0:0:0:0:0 with SMTP id u10csp1080949jao; Fri, 16 Jul 2021 00:02:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwD+Q5LN/v/NPsDXQh4K/q5IzOU3pSMR/pTT8y8wywqOsHkbz3b7BWNIF8haotFDRsJfVmf X-Received: by 2002:a92:1e03:: with SMTP id e3mr5739921ile.264.1626418966925; Fri, 16 Jul 2021 00:02:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626418966; cv=none; d=google.com; s=arc-20160816; b=OPkHbL2T5mGMu7lYWShcV+gGYqWExHgXQ/ylPqqmi2dgdwK+AZEQNYeUEodBic3R98 rubfy8soGR5UUCU5tDE1m0ASl/tRhQ4LuDJzZ/BVubR2lR9MCLWr7H1/8BW2rrjtSxSq qKZdPw2Q89FQHKBglFfb4B6IaO+xjKHw9YK+V4F3hDqG2HDSneHpEgfrGYx0gct2j5wb jkuONrUvAEFfROywKkgJjy7pT1CLMOXq+FaUik6WxhyXfTR5o2y4g2NxqveEEpDQuNFJ olUvKgmv4EYOB5P8qUTIUPLueMWD6BKe+rM+LSRw+B8eTsYVNFFpFXRkRmhq5LwWXQnj JBdg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=ob+65EC7EGHk7ZpDwd2fjysmCAANNiwOFYwvnrx8W6A=; b=c/biRxHxWZmO7AhkP9LipZGZBL0DaNIF9kFN3xT4K5hm7YuPKUy5b6cf5dWi64KZGZ GTVtRm0ecYHNOx0gGZHtUhc6oKoF4RCAkp/nTtK1XJtgKrkpey8o2mBIoCZwBp8b4dwO 1/mguNI713Bkz2D4AxHqaOOxzq7pDoQoqInd0eiBjQ4D8qeOgac15nt9I/x9LhibHA+s ePkpbBN5TJavpAEl+LDsleFkeTD3rIBECMK65RnKn3+EE1hJGaTKI2roT4YIg1A7gVDc bpHMxF1BexpEEh5ZrUbB5Tj9erbS/DpvNyS9/oLH7RTQlGCRJe4a/JmbhLGtWsSkn1Vs Admw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WpUScbBh; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g5si8705146ilb.152.2021.07.16.00.02.46; Fri, 16 Jul 2021 00:02:46 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-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=@linaro.org header.s=google header.b=WpUScbBh; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235506AbhGPHFf (ORCPT + 8 others); Fri, 16 Jul 2021 03:05:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234986AbhGPHFd (ORCPT ); Fri, 16 Jul 2021 03:05:33 -0400 Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAD69C061760 for ; Fri, 16 Jul 2021 00:02:37 -0700 (PDT) Received: by mail-ed1-x536.google.com with SMTP id w14so11590303edc.8 for ; Fri, 16 Jul 2021 00:02:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ob+65EC7EGHk7ZpDwd2fjysmCAANNiwOFYwvnrx8W6A=; b=WpUScbBh4ND6IRyPfMqPuzTj7+TZmXY/9OtflZD5oeF8YGH3wY0LBc1zTchljv7HGt eC7oICPrBirfEObqC7S/yhLtt/3NhAParkUbv73w1a3xCeetjLS29n23WFgEmGEe7gWa YHCPMQm5N9sCs3f5xRximY96BxorpuvQwxQIBlaIKiChPpNTFq9m8NOnrd0/epKliPKS F3yiKzIwdNKJS46YvfwjJSJBFJaQftvIpOy6n5s+GZi3ySSVXMFnCcIQfjDNtvxKzvUp SSUk/pcUKNtg3hnq/1b6TSbtc/N3nkoNuttw9RcRFhag8cbpFb/Hq1H6t02Tia2uLTG+ 6vDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=ob+65EC7EGHk7ZpDwd2fjysmCAANNiwOFYwvnrx8W6A=; b=LeFy4txxrSibzK+RdR8hsPzb45pqFTUxsS4T/YuaiGN99JuaCf7CCPsMR2GfkY6fXC vYtxN8dhp6J1kEPr56/cybV2Xj0A8yk5Fkotrlke3jfHP92xxLVRoHafnDwQPEdehHcg SQlNfEb7JIqmy4czUBtBayYAhqjiaQfSEnXz9zX8jlMNZXTc664P6QKWAXaO6FIa1/sz 4Au/m1ipQSi8TC/9GC3EMewXVOcxVHDJ6H2fbZHIY3bqQjIr/cTSF9ZYbop70eiWWC+G hjP3oDR1NNHGilKaIK21FjQEYFvqbpr7Fs5jbdTUe2AfBUkeYMv8+YvuFUw8bben4ieY +/6w== X-Gm-Message-State: AOAM531OfCCLbs6pv+znsFlIaMaVy/XSN8KOAe7qfp72RyuNiRHkyIby i94lcpzTF6nekv17sMqOrIkjtWtJyoYwqg== X-Received: by 2002:a05:6402:d2:: with SMTP id i18mr12528374edu.17.1626418956198; Fri, 16 Jul 2021 00:02:36 -0700 (PDT) Received: from localhost.localdomain (ppp-94-66-243-35.home.otenet.gr. [94.66.243.35]) by smtp.gmail.com with ESMTPSA id cq22sm3313698edb.77.2021.07.16.00.02.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Jul 2021 00:02:35 -0700 (PDT) From: Ilias Apalodimas To: netdev@vger.kernel.org Cc: linyunsheng@huawei.com, Ilias Apalodimas , Alexander Duyck , Jesper Dangaard Brouer , "David S. Miller" , Jakub Kicinski , Alexander Lobakin , Jonathan Lemon , Willem de Bruijn , Miaohe Lin , Guillaume Nault , Cong Wang , Matteo Croce , linux-kernel@vger.kernel.org Subject: [PATCH 1/1 v3] skbuff: Fix a potential race while recycling page_pool packets Date: Fri, 16 Jul 2021 10:02:18 +0300 Message-Id: <20210716070222.106422-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.32.0.rc0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org As Alexander points out, when we are trying to recycle a cloned/expanded SKB we might trigger a race. The recycling code relies on the pp_recycle bit to trigger, which we carry over to cloned SKBs. If that cloned SKB gets expanded or if we get references to the frags, call skb_release_data() and overwrite skb->head, we are creating separate instances accessing the same page frags. Since the skb_release_data() will first try to recycle the frags, there's a potential race between the original and cloned SKB, since both will have the pp_recycle bit set. Fix this by explicitly those SKBs not recyclable. The atomic_sub_return effectively limits us to a single release case, and when we are calling skb_release_data we are also releasing the option to perform the recycling, or releasing the pages from the page pool. Fixes: 6a5bcd84e886 ("page_pool: Allow drivers to hint on SKB recycling") Reported-by: Alexander Duyck Suggested-by: Alexander Duyck Reviewed-by: Alexander Duyck Acked-by: Jesper Dangaard Brouer Signed-off-by: Ilias Apalodimas --- Changes since v1: - Set the recycle bit to 0 during skb_release_data instead of the individual fucntions triggering the issue, in order to catch all cases Changes since v2: - Add a comment explaining why we need to reset the recycling bit net/core/skbuff.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) -- 2.32.0.rc0 diff --git a/net/core/skbuff.c b/net/core/skbuff.c index 12aabcda6db2..8ec5c1136692 100644 --- a/net/core/skbuff.c +++ b/net/core/skbuff.c @@ -663,7 +663,7 @@ static void skb_release_data(struct sk_buff *skb) if (skb->cloned && atomic_sub_return(skb->nohdr ? (1 << SKB_DATAREF_SHIFT) + 1 : 1, &shinfo->dataref)) - return; + goto exit; skb_zcopy_clear(skb, true); @@ -674,6 +674,17 @@ static void skb_release_data(struct sk_buff *skb) kfree_skb_list(shinfo->frag_list); skb_free_head(skb); +exit: + /* When we clone an SKB we copy the reycling bit. The pp_recycle + * bit is only set on the head though, so in order to avoid races + * while trying to recycle fragments on __skb_frag_unref() we need + * to make one SKB responsible for triggering the recycle path. + * So disable the recycling bit if an SKB is cloned and we have + * additional references to to the fragmented part of the SKB. + * Eventually the last SKB will have the recycling bit set and it's + * dataref set to 0, which will trigger the recycling + */ + skb->pp_recycle = 0; } /*