Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2620422imn; Tue, 2 Aug 2022 09:37:00 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vh9btl0m1hRpxAfcSedhlT9BLN44nYOU1+ah2D+OPMjc7cfMXRWQLeIYlkim/VeXn6bZ5g X-Received: by 2002:aa7:8d94:0:b0:52b:a70e:ae89 with SMTP id i20-20020aa78d94000000b0052ba70eae89mr22210505pfr.23.1659458219973; Tue, 02 Aug 2022 09:36:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659458219; cv=none; d=google.com; s=arc-20160816; b=Gi7RU0+NAIhc+FM+EMYrSTv69VigVrgacfmjPwYxV7k96kBYBteNvNoHmifVJ9vp9o SpligA99EbIB67rdQKLNEtMS244/DmEixJL/B4VamUJC+xR369Yn8d30uFM+XbLovOUr d5h2MVF9wxGmK9aeGD3KBkU396IU2Vcgiy+y2LKO2u8GlnOLiLBLHqGLXT41fBj125U/ nprbIWZd9FvhBT+/Xc09bRK0BiNNI9U2pgHzb+NGCGxDC0jlsbl2M7d5z4R9ALrm603L kevI6znKCzrak+FASZLCUfonXB1F0CRBLlPCnNRziH3jkc32+U4UiPVneQqJ+k1eLa8l 5Ikg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date:dkim-signature; bh=mbpYAhPaugN+6AMKh/dWW9aTUmKw8fgpyefe3Fi/aYY=; b=Yc3Ea9+u8YB8UZ4hG0s/W2+4rVrboa5cZJEhk7vxd87RXkxkSsQxkV8ojF8Sld+WnI UlnS3JWxUA8wqD0PQjDIzCVgxzPNjC5s4efhrj/mwWsmWrcGycU+ubv38MDPZAAoZgeK WfhIIsXHH2XTDdC3bh9V4kqwrhW/zURiV0mchCkM4kPVwgIV+gwNqmsG+vOrFJ4JO5BS LrQyCaRAfCQIixHvlpfNrssduwlZS4a2KQZp/Z6jo71vjq55X0iC++MY5LD+etqBMgY9 29EPdW8dh1QB0X6YYR0jHac6HuZlxQBlTBT2x0mJ8QYmfWTeLEr4UdKMQ9mLu1VHCUiX ERUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gIutJo8k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b9-20020a170902d50900b0016ef3e9acd6si5676346plg.601.2022.08.02.09.36.46; Tue, 02 Aug 2022 09:36:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gIutJo8k; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237495AbiHBP5t (ORCPT + 99 others); Tue, 2 Aug 2022 11:57:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34150 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237354AbiHBP5r (ORCPT ); Tue, 2 Aug 2022 11:57:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5769FB20 for ; Tue, 2 Aug 2022 08:57:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659455865; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=mbpYAhPaugN+6AMKh/dWW9aTUmKw8fgpyefe3Fi/aYY=; b=gIutJo8kh/9jIiqsGZfEIWpimnwSSxW/y2itzHoYZ80iONHQ2PIMM7MMV1+tARqE0VuuPP JIrZQ7LgNFyhs7bQXvoU0S0SZomiKG6UT1Vk6LxKZOL8H6FGyAig9BfIUCvW/6GLUtEMmo b1Qx5hupnnr4aKtlkLAnr7Qwnw4beXg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-37-4_0Mpbu3NKef5BfpFsn6PA-1; Tue, 02 Aug 2022 11:57:34 -0400 X-MC-Unique: 4_0Mpbu3NKef5BfpFsn6PA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 50AC28037AE; Tue, 2 Aug 2022 15:57:33 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2498040CF916; Tue, 2 Aug 2022 15:57:33 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id 272FvXWb013148; Tue, 2 Aug 2022 11:57:33 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 272FvW1j013144; Tue, 2 Aug 2022 11:57:32 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Tue, 2 Aug 2022 11:57:32 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Will Deacon cc: Linus Torvalds , "Paul E. McKenney" , Ard Biesheuvel , Alexander Viro , Alan Stern , Andrea Parri , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Daniel Lustig , Joel Fernandes , Linux Kernel Mailing List , linux-arch , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v4 1/2] introduce test_bit_acquire and use it in wait_on_bit In-Reply-To: <20220802133652.GA27253@willie-the-truck> Message-ID: References: <20220801155421.GB26280@willie-the-truck> <20220802084015.GB26962@willie-the-truck> <20220802133652.GA27253@willie-the-truck> User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.84 on 10.11.54.1 X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2 Aug 2022, Will Deacon wrote: > On Tue, Aug 02, 2022 at 07:38:17AM -0400, Mikulas Patocka wrote: > > > > > > On Tue, 2 Aug 2022, Will Deacon wrote: > > > > > On Mon, Aug 01, 2022 at 12:12:47PM -0400, Mikulas Patocka wrote: > > > > On Mon, 1 Aug 2022, Will Deacon wrote: > > > > > On Mon, Aug 01, 2022 at 06:42:15AM -0400, Mikulas Patocka wrote: > > > > > > > > > > > Index: linux-2.6/arch/x86/include/asm/bitops.h > > > > > > =================================================================== > > > > > > --- linux-2.6.orig/arch/x86/include/asm/bitops.h 2022-08-01 12:27:43.000000000 +0200 > > > > > > +++ linux-2.6/arch/x86/include/asm/bitops.h 2022-08-01 12:27:43.000000000 +0200 > > > > > > @@ -203,8 +203,10 @@ arch_test_and_change_bit(long nr, volati > > > > > > > > > > > > static __always_inline bool constant_test_bit(long nr, const volatile unsigned long *addr) > > > > > > { > > > > > > - return ((1UL << (nr & (BITS_PER_LONG-1))) & > > > > > > + bool r = ((1UL << (nr & (BITS_PER_LONG-1))) & > > > > > > (addr[nr >> _BITOPS_LONG_SHIFT])) != 0; > > > > > > + barrier(); > > > > > > + return r; > > > > > > > > > > Hmm, I find it a bit weird to have a barrier() here given that 'addr' is > > > > > volatile and we don't need a barrier() like this in the definition of > > > > > READ_ONCE(), for example. > > > > > > > > gcc doesn't reorder two volatile accesses, but it can reorder non-volatile > > > > accesses around volatile accesses. > > > > > > > > The purpose of the compiler barrier is to make sure that the non-volatile > > > > accesses that follow test_bit are not reordered by the compiler before the > > > > volatile access to addr. > > > > > > If we need these accesses to be ordered reliably, then we need a CPU barrier > > > and that will additionally prevent the compiler reordering. So I still don't > > > think we need the barrier() here. > > > > This is x86-specific code. x86 has strong memory ordering, so we only care > > about compiler reordering. > > Indeed, but what I'm trying to say is that the _caller_ would have a memory > barrier in this case, and so there's no need for one in here. test_bit() does > not have ordering semantics. > > Will But constant_test_bit() is also used for test_bit_acquire(), and for test_bit_acquire(), the barrier is needed. Without the barrier, it doesn't have acquire semantics, because the compiler (not CPU) can move the following non-volatile accesses before the volatile access to "addr[nr >> _BITOPS_LONG_SHIFT]". See this piece of code in arch/x86/include/asm/bitops.h in the patch: +#define arch_test_bit_acquire(nr, addr) \ + arch_test_bit(nr, addr) We could split constant_test_bit() to two functions: constant_test_bit() and constant_test_bit_acquire() and put the barrier only in constant_test_bit_acquire(). But I chose not to do it because code duplication is bad and because the overhead of the compiler barrier is none. Mikulas