Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp1061041rwe; Thu, 25 Aug 2022 14:45:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5AQrDKDeOuBbX0T0haL2c91/jHkWXwJbpdj9sDTnaVS8ErmK7OkzBHuycX7EbNtGsscLOU X-Received: by 2002:a17:90b:2243:b0:1f7:3011:117e with SMTP id hk3-20020a17090b224300b001f73011117emr1056105pjb.3.1661463937491; Thu, 25 Aug 2022 14:45:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661463937; cv=none; d=google.com; s=arc-20160816; b=Sy/lb7gcsXBtWy/U75NVnp1qzjcuG7yv3H0qH1OrwWow98H+Q/SjsZ/NWDkhbKDyKR nRI801wernLFXojXuGiFe1oSJczKf0NM/cxAkKRgaSRsAoDmQckpxSUOzmmP2DavFaWu h20my5SiDv34D04MZqaFWPmca/m5iBewttLVVPSv4kFgAXgovHaxGT4poB61HqsSwFyG tbRPHIdNOhziLHr0Oa11Z+Jr9T7mDDcXbivjBlanpigdGXYW/sbimhFfzb6YoKVs+Bcy IJpYt9823PictgsamcpnKtro3+bcHfNOO1Ajg7S66o8IlLMsQ5h1lQOvV0Kgrslzh4bP mQiw== 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=42srWBnsAPoUudJYTrvi/uUaLSzYytOXCM57hFJ7wJk=; b=KPDyIn3sYEqOdLwzHOLk4IxlnqXiJeyH7wRAYCFI/pXtdCXOyPJWMLyQERDT9CMDa9 UCWbxYICjA5ZK1iPUFmM4EvJBjHzAXXrO/TTH7z5o1NAhkiTANHWGv3Sg694l7ogYYlk mPjeoZ/N/rPsHaq+2BJFzMRKULzSH5NFaZWeW5FGFW6RpdnrCploZkJVzjDTZwcLMUPU EO1tDO8E7kXSv+hhr9J5zSoW7tCRvGgjpz/Vb58J9RFJrq5035HL2LqEoRz680FJPiaP NoMt45S8xWWwhJjtKlLASJfsznHs6iaWeKN1/7IFwH1+4geRkBDATJE6oHR8G9Oy8EPR W5OQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NF9ajblA; 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 a7-20020a63cd47000000b0042b19761809si160986pgj.445.2022.08.25.14.45.16; Thu, 25 Aug 2022 14:45:37 -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=NF9ajblA; 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 S242557AbiHYVDw (ORCPT + 99 others); Thu, 25 Aug 2022 17:03:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42008 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233066AbiHYVDt (ORCPT ); Thu, 25 Aug 2022 17:03:49 -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 ESMTPS id D7245255BB for ; Thu, 25 Aug 2022 14:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1661461427; 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=42srWBnsAPoUudJYTrvi/uUaLSzYytOXCM57hFJ7wJk=; b=NF9ajblAURxWZncBh8QitNg0BK0ItIxz+gHnBX6JiwqAAzaJyQe7bNdUhtuf8zt63UkmMZ /JYWJomujlZcAXVLVBcfu1XP039hUcBhmUrnMxA9asOy0L7tKKvBsTo/NCzYYHxFBRRcJE Dap+UZka2Szsp23MD79SjgUvBu4/FA8= 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-447-HCg0bWV6PP-NG7vSYMZVOA-1; Thu, 25 Aug 2022 17:03:42 -0400 X-MC-Unique: HCg0bWV6PP-NG7vSYMZVOA-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E09D2101AA64; Thu, 25 Aug 2022 21:03:41 +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 96F98492CA2; Thu, 25 Aug 2022 21:03:41 +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 27PL3fii008281; Thu, 25 Aug 2022 17:03:41 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 27PL3eUt008277; Thu, 25 Aug 2022 17:03:40 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Thu, 25 Aug 2022 17:03:40 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Alan Stern , Andrea Parri , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Daniel Lustig , Joel Fernandes , linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org Subject: Re: [PATCH] wait_on_bit: add an acquire memory barrier In-Reply-To: Message-ID: References: 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.85 on 10.11.54.9 X-Spam-Status: No, score=-2.8 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,T_SCC_BODY_TEXT_LINE 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 Mon, 22 Aug 2022, Linus Torvalds wrote: > On Mon, Aug 22, 2022 at 10:08 AM Linus Torvalds > wrote: > > > > So why don't we just create a "test_bit_acquire()" and be done with > > it? We literally created clear_bit_unlock() for the opposite reason, > > and your comments about the new barrier hack even point to it. > > Here's a patch that is > > (a) almost entirely untested (I checked that one single case builds > and seems to generate the expected code) > > (b) needs some more loving > > but seems to superficially work. > > At a minimum this needs to be split into two (so the bitop and the > wait_on_bit parts split up), and that whole placement of > and generic_bit_test_acquire() need at least some > thinking about, but on the whole it seems reasonable. > > For example, it would make more sense to have this in > , but not all architectures include that, > and some do their own version. I didn't want to mess with > architecture-specific headers, so this illogically just uses > generic-non-atomic.h. > > Maybe just put it in directly? > > So I'm not at all claiming that this is a great patch. It definitely > needs more work, and a lot more testing. > > But I think this is at least the right _direction_ to take here. > > And yes, I think it also would have been better if > "clear_bit_unlock()" would have been called "clear_bit_release()", and > we'd have more consistent naming with our ordered atomics. But it's > probably not worth changing. > > Linus Hi Here I reworked your patch, so that test_bit_acquire is defined just like test_bit. There's some code duplication (in include/asm-generic/bitops/generic-non-atomic.h and in arch/x86/include/asm/bitops.h), but that duplication exists in the test_bit function too. I tested it on x86-64 and arm64. On x86-64 it generates the "bt" instruction for variable-bit test and "shr; and $1" for constant bit test. On arm64 it generates the "ldar" instruction for both constant and variable bit test. For me, the kernel 6.0-rc2 doesn't boot in an arm64 virtual machine at all (with or without this patch), so I only compile-tested it on arm64. I have to bisect it. Mikulas From: Mikulas Patocka There are several places in the kernel where wait_on_bit is not followed by a memory barrier (for example, in drivers/md/dm-bufio.c:new_read). On architectures with weak memory ordering, it may happen that memory accesses that follow wait_on_bit are reordered before wait_on_bit and they may return invalid data. Fix this class of bugs by introducing a new function "test_bit_acquire" that works like test_bit, but has acquire memory ordering semantics. Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org arch/x86/include/asm/bitops.h | 13 +++++++++++++ include/asm-generic/bitops/generic-non-atomic.h | 14 ++++++++++++++ include/asm-generic/bitops/instrumented-non-atomic.h | 12 ++++++++++++ include/asm-generic/bitops/non-atomic.h | 1 + include/asm-generic/bitops/non-instrumented-non-atomic.h | 1 + include/linux/bitops.h | 1 + include/linux/buffer_head.h | 2 +- include/linux/wait_bit.h | 8 ++++---- kernel/sched/wait_bit.c | 2 +- 9 files changed, 48 insertions(+), 6 deletions(-) Index: linux-2.6/include/asm-generic/bitops/generic-non-atomic.h =================================================================== --- linux-2.6.orig/include/asm-generic/bitops/generic-non-atomic.h +++ linux-2.6/include/asm-generic/bitops/generic-non-atomic.h @@ -4,6 +4,7 @@ #define __ASM_GENERIC_BITOPS_GENERIC_NON_ATOMIC_H #include +#include #ifndef _LINUX_BITOPS_H #error only can be included directly @@ -127,6 +128,18 @@ generic_test_bit(unsigned long nr, const return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1))); } +/** + * generic_test_bit - Determine whether a bit is set with acquire semantics + * @nr: bit number to test + * @addr: Address to start counting from + */ +static __always_inline bool +generic_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr) +{ + unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); + return 1UL & (smp_load_acquire(p) >> (nr & (BITS_PER_LONG-1))); +} + /* * const_*() definitions provide good compile-time optimizations when * the passed arguments can be resolved at compile time. @@ -137,6 +150,7 @@ generic_test_bit(unsigned long nr, const #define const___test_and_set_bit generic___test_and_set_bit #define const___test_and_clear_bit generic___test_and_clear_bit #define const___test_and_change_bit generic___test_and_change_bit +#define const_test_bit_acquire generic_test_bit_acquire /** * const_test_bit - Determine whether a bit is set Index: linux-2.6/include/asm-generic/bitops/non-atomic.h =================================================================== --- linux-2.6.orig/include/asm-generic/bitops/non-atomic.h +++ linux-2.6/include/asm-generic/bitops/non-atomic.h @@ -13,6 +13,7 @@ #define arch___test_and_change_bit generic___test_and_change_bit #define arch_test_bit generic_test_bit +#define arch_test_bit_acquire generic_test_bit_acquire #include Index: linux-2.6/include/linux/bitops.h =================================================================== --- linux-2.6.orig/include/linux/bitops.h +++ linux-2.6/include/linux/bitops.h @@ -59,6 +59,7 @@ extern unsigned long __sw_hweight64(__u6 #define __test_and_clear_bit(nr, addr) bitop(___test_and_clear_bit, nr, addr) #define __test_and_change_bit(nr, addr) bitop(___test_and_change_bit, nr, addr) #define test_bit(nr, addr) bitop(_test_bit, nr, addr) +#define test_bit_acquire(nr, addr) bitop(_test_bit_acquire, nr, addr) /* * Include this here because some architectures need generic_ffs/fls in Index: linux-2.6/include/linux/wait_bit.h =================================================================== --- linux-2.6.orig/include/linux/wait_bit.h +++ linux-2.6/include/linux/wait_bit.h @@ -71,7 +71,7 @@ static inline int wait_on_bit(unsigned long *word, int bit, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit_acquire(bit, word)) return 0; return out_of_line_wait_on_bit(word, bit, bit_wait, @@ -96,7 +96,7 @@ static inline int wait_on_bit_io(unsigned long *word, int bit, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit_acquire(bit, word)) return 0; return out_of_line_wait_on_bit(word, bit, bit_wait_io, @@ -123,7 +123,7 @@ wait_on_bit_timeout(unsigned long *word, unsigned long timeout) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit_acquire(bit, word)) return 0; return out_of_line_wait_on_bit_timeout(word, bit, bit_wait_timeout, @@ -151,7 +151,7 @@ wait_on_bit_action(unsigned long *word, unsigned mode) { might_sleep(); - if (!test_bit(bit, word)) + if (!test_bit_acquire(bit, word)) return 0; return out_of_line_wait_on_bit(word, bit, action, mode); } Index: linux-2.6/kernel/sched/wait_bit.c =================================================================== --- linux-2.6.orig/kernel/sched/wait_bit.c +++ linux-2.6/kernel/sched/wait_bit.c @@ -47,7 +47,7 @@ __wait_on_bit(struct wait_queue_head *wq prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode); if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) ret = (*action)(&wbq_entry->key, mode); - } while (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret); + } while (test_bit_acquire(wbq_entry->key.bit_nr, wbq_entry->key.flags) && !ret); finish_wait(wq_head, &wbq_entry->wq_entry); Index: linux-2.6/include/asm-generic/bitops/non-instrumented-non-atomic.h =================================================================== --- linux-2.6.orig/include/asm-generic/bitops/non-instrumented-non-atomic.h +++ linux-2.6/include/asm-generic/bitops/non-instrumented-non-atomic.h @@ -12,5 +12,6 @@ #define ___test_and_change_bit arch___test_and_change_bit #define _test_bit arch_test_bit +#define _test_bit_acquire arch_test_bit_acquire #endif /* __ASM_GENERIC_BITOPS_NON_INSTRUMENTED_NON_ATOMIC_H */ Index: linux-2.6/arch/x86/include/asm/bitops.h =================================================================== --- linux-2.6.orig/arch/x86/include/asm/bitops.h +++ linux-2.6/arch/x86/include/asm/bitops.h @@ -207,6 +207,12 @@ static __always_inline bool constant_tes (addr[nr >> _BITOPS_LONG_SHIFT])) != 0; } +static __always_inline bool constant_test_bit_acquire(long nr, const volatile unsigned long *addr) +{ + unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr); + return 1UL & (smp_load_acquire(p) >> (nr & (BITS_PER_LONG-1))); +} + static __always_inline bool variable_test_bit(long nr, volatile const unsigned long *addr) { bool oldbit; @@ -226,6 +232,13 @@ arch_test_bit(unsigned long nr, const vo variable_test_bit(nr, addr); } +static __always_inline bool +arch_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr) +{ + return __builtin_constant_p(nr) ? constant_test_bit_acquire(nr, addr) : + variable_test_bit(nr, addr); +} + /** * __ffs - find first set bit in word * @word: The word to search Index: linux-2.6/include/asm-generic/bitops/instrumented-non-atomic.h =================================================================== --- linux-2.6.orig/include/asm-generic/bitops/instrumented-non-atomic.h +++ linux-2.6/include/asm-generic/bitops/instrumented-non-atomic.h @@ -142,4 +142,16 @@ _test_bit(unsigned long nr, const volati return arch_test_bit(nr, addr); } +/** + * _test_bit_acquire - Determine whether a bit is set with acquire semantics + * @nr: bit number to test + * @addr: Address to start counting from + */ +static __always_inline bool +_test_bit_acquire(unsigned long nr, const volatile unsigned long *addr) +{ + instrument_atomic_read(addr + BIT_WORD(nr), sizeof(long)); + return arch_test_bit_acquire(nr, addr); +} + #endif /* _ASM_GENERIC_BITOPS_INSTRUMENTED_NON_ATOMIC_H */ Index: linux-2.6/include/linux/buffer_head.h =================================================================== --- linux-2.6.orig/include/linux/buffer_head.h +++ linux-2.6/include/linux/buffer_head.h @@ -156,7 +156,7 @@ static __always_inline int buffer_uptoda * make it consistent with folio_test_uptodate * pairs with smp_mb__before_atomic in set_buffer_uptodate */ - return (smp_load_acquire(&bh->b_state) & (1UL << BH_Uptodate)) != 0; + return test_bit_acquire(BH_Uptodate, &bh->b_state); } #define bh_offset(bh) ((unsigned long)(bh)->b_data & ~PAGE_MASK)