From patchwork Fri Nov 18 04:38:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Dumazet X-Patchwork-Id: 13047717 X-Patchwork-Delegate: kuba@kernel.org Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA9AFC433FE for ; Fri, 18 Nov 2022 04:38:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235148AbiKREiz (ORCPT ); Thu, 17 Nov 2022 23:38:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234907AbiKREix (ORCPT ); Thu, 17 Nov 2022 23:38:53 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53B3725C4D for ; Thu, 17 Nov 2022 20:38:52 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id a5-20020a25af05000000b006e450a5e507so3559314ybh.22 for ; Thu, 17 Nov 2022 20:38:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=RoUc7dP0RXfISFU7AKB7lYqLgTf31EwetHS4oqxqHjM=; b=OD3DZxV/YuDyLUvfhy7pPFQBJgVGwb6Z5zrHTSs3Pt/3p18YsqUphoyFvJ6+8gUOZ1 xjqFaLxl/Ed1XLMUL0ce7XnFE57qXMDMHhjp4dy93Q0rnWjil/VLDnu5Nu6Bh1+wqYPY D18+O23RyJlsYUFneYlQXQ3bA09KPKpjibRO2Cga5qgWGgHLH3GP2VgHxMwadc5+tSVC aydEKoHhIjAnpggRFhKOaWyoWx6LlGYMEAJf2NQA5bdikAVHQkO48oe3xjkgkd/EWejn tX5LviQrBqSIcRjsBSR+HlmPl/cJKgQ/bAdsCjKDBDuq0wx3pITpCmL/z39jyN9d/8UQ CLQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=RoUc7dP0RXfISFU7AKB7lYqLgTf31EwetHS4oqxqHjM=; b=IgZTP7I+A1Lrl7cOjT1hS4exoUOqI/thsy+pD5OKacfrPLQsCPm/qAFcmEpzuOvRQP /JqVy2ph7NmPomiov7otgsDijsZx3pSjUsoH8aJanwe8s0vTANXDiA9iJPxZhEehwKi6 mC1iO8r+YlZL+kJOc1CbS02GMIcnlG4XJEZBnA1WT86Oq1L2VpcnFLD1K7T2BRZRvPhZ cde82uqhyACBbt+1j22CXL5uzm+ge60IRMYX79ppwwRzdos6j3BFxdC7aUoxJUvzf7v+ exTNdwhHtp9b66nxNcwllWhsU9rQtOm1800NGs+JIvONaRIAT7tf/fCITjj2v60B+5Sg y1Lw== X-Gm-Message-State: ANoB5plq9SpnvkgiAdFQEUgPqv1glXdatSR3ujsqeuU+QanPvAhlubx6 QDzpvgoXeM4jTzSb3ighNaMtsi0ORQVuTA== X-Google-Smtp-Source: AA0mqf4FnAIxWHuin4dUlFK5hxdbtHN7hWqTYnqdk/CkmG+ql9OpjzKldgB7vwVsVTwpEQPNh68Zj16L+Iv0Qg== X-Received: from edumazet1.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:395a]) (user=edumazet job=sendgmr) by 2002:a81:f80f:0:b0:38e:e541:d8ca with SMTP id z15-20020a81f80f000000b0038ee541d8camr4841853ywm.283.1668746331622; Thu, 17 Nov 2022 20:38:51 -0800 (PST) Date: Fri, 18 Nov 2022 04:38:43 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.38.1.584.g0f3c55d4c2-goog Message-ID: <20221118043843.3703186-1-edumazet@google.com> Subject: [PATCH net-next] net: fix __sock_gen_cookie() From: Eric Dumazet To: "David S . Miller" , Jakub Kicinski , Paolo Abeni Cc: netdev@vger.kernel.org, eric.dumazet@gmail.com, Eric Dumazet , coverity-bot Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org I was mistaken how atomic64_try_cmpxchg(&sk_cookie, &res, new) is working. I was assuming @res would contain the final sk_cookie value, regardless of the success of our cmpxchg() We could do something like: if (atomic64_try_cmpxchg(&sk_cookie, &res, new) res = new; But we can avoid a conditional and read sk_cookie again. atomic64_cmpxchg(&sk_cookie, res, new); res = atomic64_read(&sk_cookie); Reported-by: coverity-bot Addresses-Coverity-ID: 1527347 ("Error handling issues") Fixes: 4ebf802cf1c6 ("net: __sock_gen_cookie() cleanup") Signed-off-by: Eric Dumazet --- net/core/sock_diag.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/net/core/sock_diag.c b/net/core/sock_diag.c index b11593cae5a09b15a10d6ba35bccc22263cb8fc8..b1e29e18d1d60cb5c87c884652f547c083ba81cd 100644 --- a/net/core/sock_diag.c +++ b/net/core/sock_diag.c @@ -30,7 +30,10 @@ u64 __sock_gen_cookie(struct sock *sk) if (!res) { u64 new = gen_cookie_next(&sock_cookie); - atomic64_try_cmpxchg(&sk->sk_cookie, &res, new); + atomic64_cmpxchg(&sk->sk_cookie, res, new); + + /* Another thread might have changed sk_cookie before us. */ + res = atomic64_read(&sk->sk_cookie); } return res; }