Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp4104648rdg; Wed, 18 Oct 2023 15:28:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPl2Vtedwe5yA4C5UktcAzCV5+vRzwtIZERFwatEx+DOpqz2MzyiGllwxNMtAjbjC++Y7H X-Received: by 2002:a17:902:f90f:b0:1c9:d46e:d52d with SMTP id kw15-20020a170902f90f00b001c9d46ed52dmr626559plb.64.1697668136234; Wed, 18 Oct 2023 15:28:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697668136; cv=none; d=google.com; s=arc-20160816; b=jW8fDenbdtI2ub/ML1lrAfTwJlo1LtA0qMdeuOC2eGV63zcvo9Ts6eRkCO5Cusdo7y GGTBfapVtsyy9fpGT5+d7hNqYNhnY/z+9hoLXBeqqE7ZjhVRqHaOdawpmH5PdcSuineq n69kN11dLSa3D7Rxf0tvUlyl6SI9Wtk5gr+jo1pFtkLGNGl0VhiVODGQqwDmnotsu+SF IiNVt4tYLEPwii58KnzUqD5W8tc5noRpdfgLPbIONkhmWUd+JwqDs5ro+sarNVWG8WTO eqZPPVkhweregiOvgFj9s1/JL0HbBXGQAePQqnVPtsoWlgBroKkPTas5HGrMiEQr7N0J 9qyQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:reply-to :message-id:subject:cc:to:from:date:dkim-signature; bh=0LR7IV8wsgL9FRyXZgB4znIN2qzlrusERMrTlENUhjI=; fh=GVs8dejKLKy6u0Fer3yJUZ34Im4kn4/OPlSeb0mCqk8=; b=MqBCGYuqbSR9rSikEa9MVUqNGlRJTiCuhvBpidHD+Blnkw/AnziaApIFvHUJ+Rub8W 14jogHZkkXNreLTp3UfLpu0czJT6VO7k0a1EGlcT6vg4Rhrt2Jy0zOyjeIMUk+hmi7x5 Z1zULGXvxZA+Rcz1+zkoGufdP5rnLs62yAFBSjvFbfOISjwxipxoLmJ12STefhRaxgSa 5VtB2JcfhAYcJuWxieO5pb7xe5S0hifQ0CdeaZwJrQiJhw6Eiy7vpYRAbnrZzTymAKxR gDh3IUexKdwnebAyRdo13j0hgxvECxPcNdB+YVGI4mQoIdxSmicXvhvun33hvj3jF37+ ikig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hKer3voR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id u5-20020a170902e5c500b001c434b3659bsi945410plf.354.2023.10.18.15.28.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 15:28:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hKer3voR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 435818132A75; Wed, 18 Oct 2023 15:28:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231217AbjJRW2f (ORCPT + 99 others); Wed, 18 Oct 2023 18:28:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229487AbjJRW2e (ORCPT ); Wed, 18 Oct 2023 18:28:34 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14B3B95 for ; Wed, 18 Oct 2023 15:28:33 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A32FFC433C7; Wed, 18 Oct 2023 22:28:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697668112; bh=kOr6kBKVTsDGo/Ewz8kI8OgtxzzOHafK+KurBxJCfV4=; h=Date:From:To:Cc:Subject:Reply-To:From; b=hKer3voRhWWEcxhPtyOE+NLlGnF6jr3xISPAMNr4ybU/hA5S6CXivbUBZjcGHBxB6 wuYohN9nefPMJfk4aJhNP/cR4rzEzuztuez/hfZL0pqRkkvoh6fcNb1iF4iqJvTdJt 3+UjEiB3JaB1dpqP48RZrOT21dOxB1zJpi6sr5d/0sUKUGuNS4V0vh8vC8ouNFAaFN /awoxU/kLjfhQeT9YUfQ7pL2luEG0xvBJWFQGoNzQVjX9VZqrfnINzPbXxomD/Cenz Eh03ihOpiIAtcMo8YBvFv3yQVjW7F+zBXj3atJK8gHCmkpjxNv3DL+iezwHfjJ6B2D FnBUUduSXSBQw== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id 3F028CE0BB0; Wed, 18 Oct 2023 15:28:32 -0700 (PDT) Date: Wed, 18 Oct 2023 15:28:32 -0700 From: "Paul E. McKenney" To: bpf@vger.kernel.org Cc: David Vernet , Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , linux-kernel@vger.kernel.org Subject: [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release() Message-ID: Reply-To: paulmck@kernel.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 18 Oct 2023 15:28:47 -0700 (PDT) bpf: Fold smp_mb__before_atomic() into atomic_set_release() The bpf_user_ringbuf_drain() BPF_CALL function uses an atomic_set() immediately preceded by smp_mb__before_atomic() so as to order storing of ring-buffer consumer and producer positions prior to the atomic_set() call's clearing of the ->busy flag, as follows: smp_mb__before_atomic(); atomic_set(&rb->busy, 0); Although this works given current architectures and implementations, and given that this only needs to order prior writes against a later write. However, it does so by accident because the smp_mb__before_atomic() is only guaranteed to work with read-modify-write atomic operations, and not at all with things like atomic_set() and atomic_read(). Note especially that smp_mb__before_atomic() will not, repeat *not*, order the prior write to "a" before the subsequent non-read-modify-write atomic read from "b", even on strongly ordered systems such as x86: WRITE_ONCE(a, 1); smp_mb__before_atomic(); r1 = atomic_read(&b); Therefore, replace the smp_mb__before_atomic() and atomic_set() with atomic_set_release() as follows: atomic_set_release(&rb->busy, 0); This is no slower (and sometimes is faster) than the original, and also provides a formal guarantee of ordering that the original lacks. Signed-off-by: Paul E. McKenney Acked-by: David Vernet Cc: Andrii Nakryiko Cc: Alexei Starovoitov Cc: Daniel Borkmann Cc: Martin KaFai Lau Cc: Song Liu Cc: Yonghong Song Cc: John Fastabend Cc: KP Singh Cc: Stanislav Fomichev Cc: Hao Luo Cc: Jiri Olsa Cc: diff --git a/kernel/bpf/ringbuf.c b/kernel/bpf/ringbuf.c index f045fde632e5..0ee653a936ea 100644 --- a/kernel/bpf/ringbuf.c +++ b/kernel/bpf/ringbuf.c @@ -770,8 +770,7 @@ BPF_CALL_4(bpf_user_ringbuf_drain, struct bpf_map *, map, /* Prevent the clearing of the busy-bit from being reordered before the * storing of any rb consumer or producer positions. */ - smp_mb__before_atomic(); - atomic_set(&rb->busy, 0); + atomic_set_release(&rb->busy, 0); if (flags & BPF_RB_FORCE_WAKEUP) irq_work_queue(&rb->work);