Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp54917rdb; Wed, 18 Oct 2023 18:07:50 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHm5uMt3y24ejW2rz5KGP1zWABrKtN+Aa2pZLYoHpM6fcLwa88xNcOOcvnOJbEDqMcEaITo X-Received: by 2002:a05:6870:4d13:b0:1e9:e0d7:3663 with SMTP id pn19-20020a0568704d1300b001e9e0d73663mr1168188oab.49.1697677670531; Wed, 18 Oct 2023 18:07:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697677670; cv=none; d=google.com; s=arc-20160816; b=WwWdDE1dNUgaF9Pw77m4aXkq+W+pBsS5/wxL5DKl9jod7fc79PhndASf8YMuJ0fhEI z1s4SgLsWEyHcaClTxlonGHGSdnSAZnSQgqfm5lCUuNta/bIxVrF0DhfqL5KhDrIsFpw ByYwXnXoW8Aykk6rwNu+qEy8lHp1NxJZm//1jAoN6lZqQYa+M86vC3nbFoUYXdal6XgA IoOmM5OGnFO//ACcmwOFp5xzUkkBRKV1VKyAESuCRbl+D6OGqkLzCn2R7zAuv4/bK3Ua wSOxYspLFaPMHjuijnPb7Pa+d/lUVKbr2w5f6y+boL6opmKX+tYr4K34TVOKf08W1OoM VY/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=eM1WsCwHLdXux/Rd6F7IsWbhOmisp1CuJMGMpdTriUU=; fh=8iO7Goiu/vAK4aORpKjnI7S9CG8WSXDp5MhWZldu41k=; b=N6M5zpmRkNKNEba+zSURr2XmgVIhKwFkkLQoUrWBaCE1k9SZDBfWk3hZhyeo/V0Tkn XmsHcLBJVsMxReyoQRruP/75iSC3qyL+h3KhDOAu4c3u0GKdnb1MG51OXrv6/YYGqtaC QWSROEUmkdSMATbHgq+z+NKjoqSmdigHQc8FmwRAMRYoz1Ni7lFeQ13x9FH4vSoeh5EI g7uouUirx/OLAvteJXGhLUQWYEtTLLvhF0gpG8mZpuTlLvs+4N0YtiBUtkwuQuyGhcZM AOBD35Eak0qe+Ff/iCfaSzNrCgwm0z3GvyuC140U2eqRl5lslJq6E1Fvk5bMmKAJ7n3W 7iBg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id f22-20020a633816000000b0055c95e91f67si3293350pga.155.2023.10.18.18.07.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 18:07:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 7932280EE775; Wed, 18 Oct 2023 18:07:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230373AbjJSBHU (ORCPT + 99 others); Wed, 18 Oct 2023 21:07:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231548AbjJSBHS (ORCPT ); Wed, 18 Oct 2023 21:07:18 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 822C8128; Wed, 18 Oct 2023 18:07:15 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4S9qLj5y8wz4f3l27; Thu, 19 Oct 2023 09:07:09 +0800 (CST) Received: from [10.174.176.117] (unknown [10.174.176.117]) by APP4 (Coremail) with SMTP id gCh0CgC3RdU7gTBlOLFZDQ--.58008S2; Thu, 19 Oct 2023 09:07:11 +0800 (CST) Subject: Re: [PATCH bpf] Fold smp_mb__before_atomic() into atomic_set_release() To: paulmck@kernel.org, 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 References: From: Hou Tao Message-ID: <722b64d7-281b-b4ab-4d4d-403abc41a36b@huaweicloud.com> Date: Thu, 19 Oct 2023 09:07:07 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-US X-CM-TRANSID: gCh0CgC3RdU7gTBlOLFZDQ--.58008S2 X-Coremail-Antispam: 1UD129KBjvJXoWxXr1rGFWUJFykuFyxWry7trb_yoWrGF4Upr 48Kr4UtrWDXr48JwnrJw4UZ34fJr4DA345Gr45JFy8Zr1UKr4jvF18Xr4jgr15Jr4kGr1j yr1UWryqv34UJrDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvIb4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_tr0E3s1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUJVWUGwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JM4IIrI8v6xkF7I0E8cxan2IY04v7Mxk0xIA0c2IE e2xFo4CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxV Aqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q 6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6x kF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWrZr1j6s0DMIIF0xvEx4A2jsIE 14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf 9x07UWE__UUUUU= X-CM-SenderInfo: xkrx3t3r6k3tpzhluzxrxghudrp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.1 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,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 groat.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 (groat.vger.email [0.0.0.0]); Wed, 18 Oct 2023 18:07:46 -0700 (PDT) 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 > 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); > > .