Message ID | ec86d38e-cfb4-44aa-8fdb-6c925922d93c@paulmck-laptop |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2010:b0:403:3b70:6f57 with SMTP id fe16csp31596vqb; Wed, 18 Oct 2023 15:28:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHyoMNJqMbM8Ms1hJBzaSI/28+cKx30jNuEhAzEBfseCvwX4ypWKjvCzRh9YGvN2Wb6O0RW X-Received: by 2002:a05:6a20:a110:b0:155:2359:2194 with SMTP id q16-20020a056a20a11000b0015523592194mr530651pzk.46.1697668135374; Wed, 18 Oct 2023 15:28:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697668135; cv=none; d=google.com; s=arc-20160816; b=jAtSoc1yHPFjWleu1jyuYds2NSzwtslavHusJf81uU29uj7wZU9MX4iJhuD/x8vyvk MjX2ENBfw2COwUd40LeVNw8Hn+tjTAgOXMSvZthKAkMqYgjDgC8wLWlGbmRd4OXfF91S LGhqMtnU+40d/hTrsE5/5u5jwvayA4f01fQyXXYjchDUPZX9z6FlqdKWAHUZo0eZCDIQ gUaZHiVW7T5Oz1/A8ExHaztFCcwjOAP23tJf+MhQ3JrLqzBgnvZUCILeppQA5XzA26qJ Bqi/9hU+MB8ozJMabz9jRsviRUhul+kLf5KQcfjt/YE96HEDdJPoPm3DVi/K/gm6u6fV eofw== 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:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id 33-20020a17090a09a400b002768cfbe6desi806910pjo.112.2023.10.18.15.28.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 15:28:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; 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:1 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 morse.vger.email (Postfix) with ESMTP id 730A081E1411; Wed, 18 Oct 2023 15:28:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231567AbjJRW2g (ORCPT <rfc822;zwp10758@gmail.com> + 24 others); Wed, 18 Oct 2023 18:28:36 -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 <rfc822;linux-kernel@vger.kernel.org>); 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 <linux-kernel@vger.kernel.org>; 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" <paulmck@kernel.org> To: bpf@vger.kernel.org Cc: David Vernet <void@manifault.com>, Andrii Nakryiko <andrii@kernel.org>, Alexei Starovoitov <ast@kernel.org>, Daniel Borkmann <daniel@iogearbox.net>, Martin KaFai Lau <martin.lau@linux.dev>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, John Fastabend <john.fastabend@gmail.com>, KP Singh <kpsingh@kernel.org>, Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>, linux-kernel@vger.kernel.org Subject: [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release() Message-ID: <ec86d38e-cfb4-44aa-8fdb-6c925922d93c@paulmck-laptop> 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 morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Wed, 18 Oct 2023 15:28:52 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780134062762650633 X-GMAIL-MSGID: 1780134062762650633 |
Series |
[bpf] Fold smp_mb__before_atomic() into atomic_set_release()
|
|
Commit Message
Paul E. McKenney
Oct. 18, 2023, 10:28 p.m. UTC
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 <paulmck@kernel.org> Acked-by: David Vernet <void@manifault.com> Cc: Andrii Nakryiko <andrii@kernel.org> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Daniel Borkmann <daniel@iogearbox.net> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: Song Liu <song@kernel.org> Cc: Yonghong Song <yonghong.song@linux.dev> Cc: John Fastabend <john.fastabend@gmail.com> Cc: KP Singh <kpsingh@kernel.org> Cc: Stanislav Fomichev <sdf@google.com> Cc: Hao Luo <haoluo@google.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: <bpf@vger.kernel.org>
Comments
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 ? 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 // 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 ? 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 <paulmck@kernel.org> > Acked-by: David Vernet <void@manifault.com> > Cc: Andrii Nakryiko <andrii@kernel.org> > Cc: Alexei Starovoitov <ast@kernel.org> > Cc: Daniel Borkmann <daniel@iogearbox.net> > Cc: Martin KaFai Lau <martin.lau@linux.dev> > Cc: Song Liu <song@kernel.org> > Cc: Yonghong Song <yonghong.song@linux.dev> > Cc: John Fastabend <john.fastabend@gmail.com> > Cc: KP Singh <kpsingh@kernel.org> > Cc: Stanislav Fomichev <sdf@google.com> > Cc: Hao Luo <haoluo@google.com> > Cc: Jiri Olsa <jolsa@kernel.org> > Cc: <bpf@vger.kernel.org> > > 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); > > .
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 <paulmck@kernel.org> > > Acked-by: David Vernet <void@manifault.com> > > Cc: Andrii Nakryiko <andrii@kernel.org> > > Cc: Alexei Starovoitov <ast@kernel.org> > > Cc: Daniel Borkmann <daniel@iogearbox.net> > > Cc: Martin KaFai Lau <martin.lau@linux.dev> > > Cc: Song Liu <song@kernel.org> > > Cc: Yonghong Song <yonghong.song@linux.dev> > > Cc: John Fastabend <john.fastabend@gmail.com> > > Cc: KP Singh <kpsingh@kernel.org> > > Cc: Stanislav Fomichev <sdf@google.com> > > Cc: Hao Luo <haoluo@google.com> > > Cc: Jiri Olsa <jolsa@kernel.org> > > Cc: <bpf@vger.kernel.org> > > > > 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); > > > > . >
Hi Paul, On 10/19/2023 12:54 PM, Paul E. McKenney wrote: > 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. I see. Thanks for explanation. It seems I did not read Documentation/atomic_t.txt carefully, it said: The barriers: smp_mb__{before,after}_atomic() only apply to the RMW atomic ops and can be used to augment/upgrade the ordering inherent to the op. > > 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(). Yes. These two processes are accessing the same field (namely timer->timer). Is WRITE_ONCE(xx) still necessary when the write of timer->time in process Y is protected by a spin-lock ? > >> // 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. Thanks. Will fix the patch. Regards, Hou > > 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 <paulmck@kernel.org> >>> Acked-by: David Vernet <void@manifault.com> >>> Cc: Andrii Nakryiko <andrii@kernel.org> >>> Cc: Alexei Starovoitov <ast@kernel.org> >>> Cc: Daniel Borkmann <daniel@iogearbox.net> >>> Cc: Martin KaFai Lau <martin.lau@linux.dev> >>> Cc: Song Liu <song@kernel.org> >>> Cc: Yonghong Song <yonghong.song@linux.dev> >>> Cc: John Fastabend <john.fastabend@gmail.com> >>> Cc: KP Singh <kpsingh@kernel.org> >>> Cc: Stanislav Fomichev <sdf@google.com> >>> Cc: Hao Luo <haoluo@google.com> >>> Cc: Jiri Olsa <jolsa@kernel.org> >>> Cc: <bpf@vger.kernel.org> >>> >>> 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); >>> >>> .
On Thu, Oct 19, 2023 at 02:20:35PM +0800, Hou Tao wrote: > Hi Paul, > > On 10/19/2023 12:54 PM, Paul E. McKenney wrote: > > 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. > > I see. Thanks for explanation. It seems I did not read > Documentation/atomic_t.txt carefully, it said: > > The barriers: > > smp_mb__{before,after}_atomic() > > only apply to the RMW atomic ops and can be used to augment/upgrade the > ordering inherent to the op. That is the place! > > 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(). > > Yes. These two processes are accessing the same field (namely > timer->timer). Is WRITE_ONCE(xx) still necessary when the write of > timer->time in process Y is protected by a spin-lock ? If there is any possibility of a concurrent reader, that is, a reader not holding that same lock, then yes, you should use WRITE_ONCE(). Compilers can do pretty vicious things to unmarked reads and writes. But don't take my word for it, here are a few writeups: o "Who's afraid of a big bad optimizing compiler?" (series) https://lwn.net/Articles/793253, https://lwn.net/Articles/799218 o "An introduction to lockless algorithms" (Paolo Bonzini series) https://lwn.net/Articles/844224, https://lwn.net/Articles/846700, https://lwn.net/Articles/847481, https://lwn.net/Articles/847973, https://lwn.net/Articles/849237, https://lwn.net/Articles/850202 o "Is Parallel Programming Hard, And, If So, What Can You Do About It?" Section 4.3.4 ("Accessing Shared Variables") https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/ perfbook.html > >> // 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. > > Thanks. Will fix the patch. Very good! Thanx, Paul > Regards, > Hou > > > > 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 <paulmck@kernel.org> > >>> Acked-by: David Vernet <void@manifault.com> > >>> Cc: Andrii Nakryiko <andrii@kernel.org> > >>> Cc: Alexei Starovoitov <ast@kernel.org> > >>> Cc: Daniel Borkmann <daniel@iogearbox.net> > >>> Cc: Martin KaFai Lau <martin.lau@linux.dev> > >>> Cc: Song Liu <song@kernel.org> > >>> Cc: Yonghong Song <yonghong.song@linux.dev> > >>> Cc: John Fastabend <john.fastabend@gmail.com> > >>> Cc: KP Singh <kpsingh@kernel.org> > >>> Cc: Stanislav Fomichev <sdf@google.com> > >>> Cc: Hao Luo <haoluo@google.com> > >>> Cc: Jiri Olsa <jolsa@kernel.org> > >>> Cc: <bpf@vger.kernel.org> > >>> > >>> 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); > >>> > >>> . >
Hi Paul, On 10/19/2023 10:25 PM, Paul E. McKenney wrote: > On Thu, Oct 19, 2023 at 02:20:35PM +0800, Hou Tao wrote: >> Hi Paul, >> >> On 10/19/2023 12:54 PM, Paul E. McKenney wrote: >>> 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. >> I see. Thanks for explanation. It seems I did not read >> Documentation/atomic_t.txt carefully, it said: >> >> The barriers: >> >> smp_mb__{before,after}_atomic() >> >> only apply to the RMW atomic ops and can be used to augment/upgrade the >> ordering inherent to the op. > That is the place! > >>> 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(). >> Yes. These two processes are accessing the same field (namely >> timer->timer). Is WRITE_ONCE(xx) still necessary when the write of >> timer->time in process Y is protected by a spin-lock ? > If there is any possibility of a concurrent reader, that is, a reader > not holding that same lock, then yes, you should use WRITE_ONCE(). Got it. Will do. > > Compilers can do pretty vicious things to unmarked reads and writes. > But don't take my word for it, here are a few writeups: > > o "Who's afraid of a big bad optimizing compiler?" (series) > https://lwn.net/Articles/793253, https://lwn.net/Articles/799218 > > o "An introduction to lockless algorithms" (Paolo Bonzini series) > https://lwn.net/Articles/844224, https://lwn.net/Articles/846700, > https://lwn.net/Articles/847481, https://lwn.net/Articles/847973, > https://lwn.net/Articles/849237, https://lwn.net/Articles/850202 > > o "Is Parallel Programming Hard, And, If So, What Can You Do About It?" > Section 4.3.4 ("Accessing Shared Variables") > https://mirrors.edge.kernel.org/pub/linux/kernel/people/paulmck/perfbook/ > perfbook.html Thanks for these excellent articles. Will read these articles carefully this time. Regards, Hou > >>>> // 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. >> Thanks. Will fix the patch. > Very good! > > Thanx, Paul > >> Regards, >> Hou >>> 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 <paulmck@kernel.org> >>>>> Acked-by: David Vernet <void@manifault.com> >>>>> Cc: Andrii Nakryiko <andrii@kernel.org> >>>>> Cc: Alexei Starovoitov <ast@kernel.org> >>>>> Cc: Daniel Borkmann <daniel@iogearbox.net> >>>>> Cc: Martin KaFai Lau <martin.lau@linux.dev> >>>>> Cc: Song Liu <song@kernel.org> >>>>> Cc: Yonghong Song <yonghong.song@linux.dev> >>>>> Cc: John Fastabend <john.fastabend@gmail.com> >>>>> Cc: KP Singh <kpsingh@kernel.org> >>>>> Cc: Stanislav Fomichev <sdf@google.com> >>>>> Cc: Hao Luo <haoluo@google.com> >>>>> Cc: Jiri Olsa <jolsa@kernel.org> >>>>> Cc: <bpf@vger.kernel.org> >>>>> >>>>> 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); >>>>> >>>>> .
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);