Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp1682924imn; Sun, 31 Jul 2022 17:43:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR7W9wde7ezrjDnMqbMZoceXXnPKTp2qcOVxROngocU2VZ0CdpTKE0SmukMXFH9M4ge1slZ6 X-Received: by 2002:a63:ec04:0:b0:41c:1149:4523 with SMTP id j4-20020a63ec04000000b0041c11494523mr2220802pgh.62.1659314613003; Sun, 31 Jul 2022 17:43:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659314612; cv=none; d=google.com; s=arc-20160816; b=wryAOn6dLvDdy/Xz0zPS9UE2kjjHkluAkHeIP1TzxU8qr7oJurdKC6wGXhP+ta2JC0 bUiKWO89Udpj4WnhmyEUTS4s8ogFb4IjlrdMGwNHxwRxcLrvhVLH0La2rja9fskJ27Fe w4UIhrcw/2CcaElN/YpB/yKJ8NC9FuVBQj+q+uTGVmOLuP42E0Z9w3CXYR7xLJHy/fNR 0ANFENZ+mcMg1qrcRoaAdtknuZL0SgSrvZQAZxPgVksaoMg/0IMTqOfG+VvSwPrbX/Mx efAz2n7f4DkMFxd5eskzji4V6hIPYrDuP1jsb/QbO9BHVN2Qp4idtDF97TaF2oPq93/g iTPg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=kluiP9ghhvFshE893iyx4sG2FuOoF3I+R8QohWqZSyE=; b=QMYEm84PujyzilXnI0PpksASKm8/bXjPXIDOU64zgIUEJd+Zx26dwrP8IIeyZ0KjWA OQPnCddJUEHFu+l3v2roENPl+zXgdEE9cT1YA9ww9rRYvImjT9XbViyTryJ1XEakvFYk 6pL+FOPVUSi8kYlAPbhO97RCHCUIzs5sgj+aI34LyAjTQ7vHZLysR9MDQteYBkkHGabT UPOLLMYSbSJdxxYHQkSBNPWJqbuzDWFXMOQb3zoOScetX2ErSDIqCs8N490WOmbnezpC 1nl+xx0W4+9neokeq9Gl1doCTYd//JJ2sF1q1UXjl9ZVfBeytcX79MNv6Muw1vmfxbq9 H37A== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x8-20020a17090a8a8800b001f061a4d77fsi14258266pjn.42.2022.07.31.17.42.59; Sun, 31 Jul 2022 17:43:32 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233233AbiHAA14 (ORCPT + 99 others); Sun, 31 Jul 2022 20:27:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232846AbiHAA1y (ORCPT ); Sun, 31 Jul 2022 20:27:54 -0400 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id C36016571 for ; Sun, 31 Jul 2022 17:27:52 -0700 (PDT) Received: (qmail 554189 invoked by uid 1000); 31 Jul 2022 20:27:51 -0400 Date: Sun, 31 Jul 2022 20:27:51 -0400 From: Alan Stern To: Mikulas Patocka Cc: Linus Torvalds , Will Deacon , "Paul E. McKenney" , Ard Biesheuvel , Alexander Viro , 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 v3 1/2] wait_bit: do read barrier after testing a bit Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no 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 Sun, Jul 31, 2022 at 04:40:59PM -0400, Mikulas Patocka wrote: > 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 read barrier after test_bit(). > > Signed-off-by: Mikulas Patocka > Cc: stable@vger.kernel.org > > 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,8 +71,10 @@ static inline int > wait_on_bit(unsigned long *word, int bit, unsigned mode) > { > might_sleep(); > - if (!test_bit(bit, word)) > + if (!test_bit(bit, word)) { > + smp_rmb(); Any new code using smp_rmb or an acquire access should always include a comment that explains where the matching smp_wmb or release access is. Alan Stern > return 0; > + } > return out_of_line_wait_on_bit(word, bit, > bit_wait, > mode); > @@ -96,8 +98,10 @@ static inline int > wait_on_bit_io(unsigned long *word, int bit, unsigned mode) > { > might_sleep(); > - if (!test_bit(bit, word)) > + if (!test_bit(bit, word)) { > + smp_rmb(); > return 0; > + } > return out_of_line_wait_on_bit(word, bit, > bit_wait_io, > mode); > @@ -123,8 +127,10 @@ wait_on_bit_timeout(unsigned long *word, > unsigned long timeout) > { > might_sleep(); > - if (!test_bit(bit, word)) > + if (!test_bit(bit, word)) { > + smp_rmb(); > return 0; > + } > return out_of_line_wait_on_bit_timeout(word, bit, > bit_wait_timeout, > mode, timeout); > @@ -151,8 +157,10 @@ wait_on_bit_action(unsigned long *word, > unsigned mode) > { > might_sleep(); > - if (!test_bit(bit, word)) > + if (!test_bit(bit, word)) { > + smp_rmb(); > 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 > @@ -51,6 +51,8 @@ __wait_on_bit(struct wait_queue_head *wq > > finish_wait(wq_head, &wbq_entry->wq_entry); > > + smp_rmb(); > + > return ret; > } > EXPORT_SYMBOL(__wait_on_bit); >