Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1891627imn; Mon, 1 Aug 2022 04:11:41 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uYty7BL2+JGcASbpxwZ4iBCvll52YufNBMxSrrk4ogOKOMYWLhqARRkAUySc2P37sxBPPG X-Received: by 2002:a17:907:720f:b0:72f:1c62:8dac with SMTP id dr15-20020a170907720f00b0072f1c628dacmr11916924ejc.437.1659352300810; Mon, 01 Aug 2022 04:11:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659352300; cv=none; d=google.com; s=arc-20160816; b=J0Un+d9G8eO9OS2Glyk1aTFKnyHNyad7C/CiTTFPOBhmTz3w7sB8cRKKKaUFhCeXds Q7+/G6vZFGMkSqeRDUS2FENDkBbNZHFI2OXOo9EYIHfF9ZB7wOFKkvWudN3xVaKOqT9u 7lRnTxpPI1XbIKuDIkuUjatSviKC/e4eBaFBKXXxuRIPfyKv8Bkfix8GyDwyQkvYvF6H 7OusdHp0K5fPiOZ260h8nNZtiOSwSeRdY9ZA2eAaLbXiHqBEpUbhg4LRC//yaDOq11sa iG3sd/KDKuC44TSHG47bg06KhlMz2J4HgsdxKdZllRv5nYeD/tFCbHCoOBpkMO7W7cVK //HA== 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=k1SB3jWY6iy3jPM3BFAzl6DIaTlKQJipefko9OVG/m0=; b=hvlmUwrRclH5D1eUkHqgd3u5w9GG7mdgpc2Xb9R8ZqulUuzda8DT861Dep5CCOI5+3 bFpyixIB8gLxwxTfP32VsBmwH9N9IHJ1J6fGQjgdt8mGETuMfBteLnAFLj6Oj/WGvO/G RfJczdCp1qzhccZZLOYNXI/jc//IgZ96pGixkeuTJpjnryFoTYGfxJbLqq7ZmQN6OmK0 zj7UJn0bPeTDwUeLtHwHvJoqUf+3A/nbK8Lbub3j/WDBcj9/SilFtbnOv1+xasltiYM/ H5o2GHSa/bvzsXIIicdvrRl3zvUL1bxRHy8PbJVooTZSPUcv6I84opmbyUH/JOicRdb7 kq6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bgJ4ddz1; 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 b60-20020a509f42000000b004396851005fsi9817577edf.100.2022.08.01.04.11.15; Mon, 01 Aug 2022 04:11:40 -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=bgJ4ddz1; 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 S231196AbiHAKms (ORCPT + 99 others); Mon, 1 Aug 2022 06:42:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231296AbiHAKm3 (ORCPT ); Mon, 1 Aug 2022 06:42:29 -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 60DFE107 for ; Mon, 1 Aug 2022 03:42:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1659350542; 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=k1SB3jWY6iy3jPM3BFAzl6DIaTlKQJipefko9OVG/m0=; b=bgJ4ddz1WpiMPUmLjyVEHLnHIljYJ1XzikRYX5gux1uY5lPccVpfeqZy4rgdAqJh9p9kl3 7cF4+Krk5tu8qWt/kWXcwPDdUmQFkTo10qDenKqRqSLCXC7wcEZbWrwgFBX0U12KlFALPE 2QMbiC1XeVvZ4GvW1Pg/Of+zmZrPA9Y= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-53-wUxvdLfDNCm4w7kAkTguWg-1; Mon, 01 Aug 2022 06:42:16 -0400 X-MC-Unique: wUxvdLfDNCm4w7kAkTguWg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 61620280048A; Mon, 1 Aug 2022 10:42:15 +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 544D4C15D67; Mon, 1 Aug 2022 10:42:15 +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 271AgFew024213; Mon, 1 Aug 2022 06:42:15 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id 271AgFCC024209; Mon, 1 Aug 2022 06:42:15 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 1 Aug 2022 06:42:15 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Linus Torvalds cc: Will Deacon , "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: [PATCH v4 1/2] introduce test_bit_acquire and use it in wait_on_bit 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.8 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 wait_on_bit tests the bit without any memory barriers, consequently the code that follows wait_on_bit may be moved before testing the bit on architectures with weak memory ordering. When the code tests for some event using wait_on_bit and then performs a load operation, the load may be unexpectedly moved before wait_on_bit and it may return data that existed before the event occurred. Such bugs exist in fs/buffer.c:__wait_on_buffer, drivers/md/dm-bufio.c:new_read, drivers/media/usb/dvb-usb-v2/dvb_usb_core.c:dvb_usb_start_feed, drivers/bluetooth/btusb.c:btusb_mtk_hci_wmt_sync and perhaps in other places. We fix this class of bugs by adding a new function test_bit_acquire that reads the bit and provides acquire memory ordering semantics. Signed-off-by: Mikulas Patocka Cc: stable@vger.kernel.org --- arch/s390/include/asm/bitops.h | 10 ++++++++++ arch/x86/include/asm/bitops.h | 7 ++++++- include/asm-generic/bitops/instrumented-non-atomic.h | 11 +++++++++++ include/asm-generic/bitops/non-atomic.h | 13 +++++++++++++ include/linux/wait_bit.h | 8 ++++---- kernel/sched/wait_bit.c | 6 +++--- 6 files changed, 47 insertions(+), 8 deletions(-) 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; } static __always_inline bool variable_test_bit(long nr, volatile const unsigned long *addr) @@ -224,6 +226,9 @@ static __always_inline bool variable_tes ? constant_test_bit((nr), (addr)) \ : variable_test_bit((nr), (addr))) +#define arch_test_bit_acquire(nr, addr) \ + arch_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 2022-08-01 12:27:43.000000000 +0200 +++ linux-2.6/include/asm-generic/bitops/instrumented-non-atomic.h 2022-08-01 12:28:33.000000000 +0200 @@ -135,4 +135,15 @@ static __always_inline bool test_bit(lon 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(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/asm-generic/bitops/non-atomic.h =================================================================== --- linux-2.6.orig/include/asm-generic/bitops/non-atomic.h 2022-08-01 12:27:43.000000000 +0200 +++ linux-2.6/include/asm-generic/bitops/non-atomic.h 2022-08-01 12:27:43.000000000 +0200 @@ -119,4 +119,17 @@ arch_test_bit(unsigned int nr, const vol } #define test_bit arch_test_bit +/** + * arch_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 int +arch_test_bit_acquire(unsigned int nr, const volatile unsigned long *addr) +{ + unsigned val = smp_load_acquire(&addr[BIT_WORD(nr)]); + return 1UL & (val >> (nr & (BITS_PER_LONG-1))); +} +#define test_bit_acquire arch_test_bit_acquire + #endif /* _ASM_GENERIC_BITOPS_NON_ATOMIC_H_ */ Index: linux-2.6/arch/s390/include/asm/bitops.h =================================================================== --- linux-2.6.orig/arch/s390/include/asm/bitops.h 2022-08-01 12:27:43.000000000 +0200 +++ linux-2.6/arch/s390/include/asm/bitops.h 2022-08-01 12:27:43.000000000 +0200 @@ -184,6 +184,16 @@ static inline bool arch_test_bit(unsigne return *addr & mask; } +static inline bool arch_test_bit_acquire(unsigned long nr, + const volatile unsigned long *ptr) +{ + const volatile unsigned long *addr = __bitops_word(nr, ptr); + unsigned long val = smp_load_acquire(addr); + unsigned long mask = __bitops_mask(nr); + + return val & mask; +} + static inline bool arch_test_and_set_bit_lock(unsigned long nr, volatile unsigned long *ptr) { Index: linux-2.6/include/linux/wait_bit.h =================================================================== --- linux-2.6.orig/include/linux/wait_bit.h 2022-08-01 12:27:43.000000000 +0200 +++ linux-2.6/include/linux/wait_bit.h 2022-08-01 12:27:43.000000000 +0200 @@ -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 2022-08-01 12:27:43.000000000 +0200 +++ linux-2.6/kernel/sched/wait_bit.c 2022-08-01 12:27:43.000000000 +0200 @@ -25,7 +25,7 @@ int wake_bit_function(struct wait_queue_ if (wait_bit->key.flags != key->flags || wait_bit->key.bit_nr != key->bit_nr || - test_bit(key->bit_nr, key->flags)) + test_bit_acquire(key->bit_nr, key->flags)) return 0; return autoremove_wake_function(wq_entry, mode, sync, key); @@ -45,9 +45,9 @@ __wait_on_bit(struct wait_queue_head *wq do { prepare_to_wait(wq_head, &wbq_entry->wq_entry, mode); - if (test_bit(wbq_entry->key.bit_nr, wbq_entry->key.flags)) + if (test_bit_acquire(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);