Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp122615rdb; Wed, 18 Oct 2023 21:54:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFSNvRu37POrEMMv6rzmz62TkBcDRbJ33ZMwXq1IgNz3Y3PkbpAr6TPtWI1HHqYKs1ISFz8 X-Received: by 2002:a05:6870:9a87:b0:1ea:2c8b:e18e with SMTP id hp7-20020a0568709a8700b001ea2c8be18emr1687866oab.8.1697691297142; Wed, 18 Oct 2023 21:54:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697691297; cv=none; d=google.com; s=arc-20160816; b=fiqAHCBWic6G/aielxxJY4RvzatHw6R7p9wWugXp9p54qRqCXoGgk/bOM/96DVTH5E /cyKW6U6SCbvKgsUmCOMn5Pn34oG7eJ++5ZC2zT4WvohrtdYIoCa+ykQ/BvH0f8Krru0 cH+SVRYsQxxZJvo61GWhx1rSpHZuX6LtKFDFLVyHSO8xGORwXgfV8vcHwRHPg+Z/gWll 9bzLSeZwU8ZccpFSinrWzIlY99NcqtOtQ4AAaLLYIlCZsVucFG1q3BBvCZ37NlftT7zI KHsvUci/M6dXCJTzMrbrJYvvQRqI+QBnMRZH0S3zfvOKEGas4HwmzDzlTtZgzssb3foc QcHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=eTQWr8J3a4VoyQCMxNU6LqRe3uvOE2Kj/RoY+GmchAo=; fh=xY6rE6GJK/+QscIugAP8+3qTC/ZjuHn7v3nQJWVD79g=; b=CyeQbYHjSzI7EID5Dx4yLvGADgzpkA++RQJkYId0ZYWsLh/mLnhbUxEUU8m2cqkcrK 9pW2cItGU1HmHn7QFS12yQCceudhYcCP8lCgIEZri6NH8czrB4hvt/L7gAHT/1FT+Sqh NrCX8twVG9APOoMo59gxof5SOkGCcrIuOi5PTbfbfEtitcsf87HCg+LyGqXFCk6lUA8F KbtVRfc/EPjyF87UIez7HowDevuNGukjIsXea6uvsr/mqOe4QnFIZkx9iT03Jeb1k7te R1CKNIfJ0XGObK9iDfFPwEjOWMzgz3tEZ253CWZb54ZYRgOM/HIkyZxUAMl/kYPK2vYE RGYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GwbdbaWa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id n185-20020a6327c2000000b00573f7b6999csi3476132pgn.440.2023.10.18.21.54.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 21:54:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=GwbdbaWa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 68F698135427; Wed, 18 Oct 2023 21:54:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232583AbjJSEyp (ORCPT + 99 others); Thu, 19 Oct 2023 00:54:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbjJSEyo (ORCPT ); Thu, 19 Oct 2023 00:54:44 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 19D4B9F for ; Wed, 18 Oct 2023 21:54:42 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 646F4C433C7; Thu, 19 Oct 2023 04:54:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697691281; bh=3lWzFUlU/97b/dHwAa2C06N+ewC9NXooDEcnsNFfjJo=; h=Date:From:To:Cc:Subject:Reply-To:References:In-Reply-To:From; b=GwbdbaWanBnKpKWHI+R6KDatN9lE/1ktRQZf8n7y6cfIiUf9sVuGcti1EQrRuuXQn arhNQyhIYyHxw2NgiKDTa6S8K8vac2V+qxrSlpBsCXArusAemLg+rS+FLKAi7fzdvj qZLb4Q9JodZBQWZbIuEtDokTW1Gptt6mpFk3xiEvLGsqs+3KGlwgz3ise84xdFoIxu dS/LYDeCDP19ZVJOH4NrzYA7Ybge4NUWMKthkK3VwQjFHMbsYa0/MfnZkxZMxgWYj2 GU9vqyD7c41CEERak5SLGddt7gxtNFHEyWsm1Hb40coRt9cbydjXn/1S/xv/38A0VW QFoviAAZPdBDg== Received: by paulmck-ThinkPad-P17-Gen-1.home (Postfix, from userid 1000) id E308FCE0F15; Wed, 18 Oct 2023 21:54:40 -0700 (PDT) Date: Wed, 18 Oct 2023 21:54:40 -0700 From: "Paul E. McKenney" To: Hou Tao Cc: bpf@vger.kernel.org, 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: Re: [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release() Message-ID: Reply-To: paulmck@kernel.org References: <722b64d7-281b-b4ab-4d4d-403abc41a36b@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <722b64d7-281b-b4ab-4d4d-403abc41a36b@huaweicloud.com> 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 agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 18 Oct 2023 21:54:54 -0700 (PDT) On Thu, Oct 19, 2023 at 09:07:07AM +0800, Hou Tao wrote: > Hi Paul, > > On 10/19/2023 6:28 AM, Paul E. McKenney wrote: > > 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); > > The reason is smp_mb__before_atomic() is defined as noop and > atomic_read() in x86-64 is just READ_ONCE(), right ? The real reason is that smp_mb__before_atomic() is not defined to do anything unless followed by an atomic read-modify-write operation, and atomic_read(), atomic_64read(), atomic_set(), and so on are not read-modify-write operations. As you point out, one implementation consequence of this is that smp_mb__before_atomic() is nothingness on x86. > And it seems that I also used smp_mb__before_atomic() in a wrong way for > patch [1]. The memory order in the posted patch is > > process X??? ??? ??? ??? ??? ??? ??? ??? ??? process Y > ??? atomic64_dec_and_test(&map->usercnt) > ??? READ_ONCE(timer->timer) > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? timer->time = t The above two lines are supposed to be accessing the same field, correct? If so, process Y's store really should be WRITE_ONCE(). > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? // it won't work > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? smp_mb__before_atomic() > ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? atomic64_read(&map->usercnt) > > For the problem, it seems I need to replace smp_mb__before_atomic() by > smp_mb() to fix the memory order, right ? Yes, because smp_mb() will order the prior store against that later load. Thanx, Paul > Regards, > Hou > > [1]: > https://lore.kernel.org/bpf/20231017125717.241101-2-houtao@huaweicloud.com/ > ???????????????????????????????????????????????????????????????? > > > > > 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); > > > > . >