public inbox for linux-arch@vger.kernel.org
 help / color / mirror / Atom feed
From: Alan Stern <stern@rowland.harvard.edu>
To: Mikulas Patocka <mpatocka@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	Will Deacon <will@kernel.org>,
	"Paul E. McKenney" <paulmck@kernel.org>,
	Ard Biesheuvel <ardb@kernel.org>,
	Alexander Viro <viro@zeniv.linux.org.uk>,
	Andrea Parri <parri.andrea@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	Nicholas Piggin <npiggin@gmail.com>,
	David Howells <dhowells@redhat.com>,
	Jade Alglave <j.alglave@ucl.ac.uk>,
	Luc Maranget <luc.maranget@inria.fr>,
	Akira Yokosawa <akiyks@gmail.com>,
	Daniel Lustig <dlustig@nvidia.com>,
	Joel Fernandes <joel@joelfernandes.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-arch <linux-arch@vger.kernel.org>,
	linux-fsdevel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] wait_bit: do read barrier after testing a bit
Date: Sun, 31 Jul 2022 20:27:51 -0400	[thread overview]
Message-ID: <YuceB1x8twgpM7Bl@rowland.harvard.edu> (raw)
In-Reply-To: <alpine.LRH.2.02.2207311639360.21350@file01.intranet.prod.int.rdu2.redhat.com>

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 <mpatocka@redhat.com>
> 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);
> 

  parent reply	other threads:[~2022-08-01  0:27 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-31 11:43 [PATCH] Add a read memory barrier to wait_on_buffer Mikulas Patocka
2022-07-31 12:00 ` Ard Biesheuvel
2022-07-31 13:41   ` Mikulas Patocka
2022-07-31 15:08     ` [PATCH v2] make buffer_locked provide an acquire semantics Mikulas Patocka
2022-07-31 16:51       ` Linus Torvalds
2022-07-31 17:30         ` Paul E. McKenney
2022-07-31 22:48           ` Matthew Wilcox
2022-08-01  3:20             ` Paul E. McKenney
2022-08-01 15:41           ` Will Deacon
2022-08-01 19:20             ` Paul E. McKenney
2022-08-02  8:54               ` Will Deacon
2022-08-02 13:49                 ` Paul E. McKenney
2022-08-02 15:29                   ` Paul E. McKenney
2022-07-31 20:39         ` Mikulas Patocka
2022-07-31 20:40           ` [PATCH v3 1/2] wait_bit: do read barrier after testing a bit Mikulas Patocka
2022-07-31 20:57             ` Linus Torvalds
2022-08-01 10:40               ` Mikulas Patocka
2022-08-01 10:43                 ` [PATCH v4 2/2] change buffer_locked, so that it has acquire semantics Mikulas Patocka
2022-08-01 14:37                   ` Matthew Wilcox
2022-08-01 15:01                     ` Mikulas Patocka
2022-08-05  3:22                       ` Matthew Wilcox
2022-08-07 11:37                         ` [PATCH v5] add barriers to buffer functions Mikulas Patocka
2022-08-07 14:50                           ` Matthew Wilcox
2022-08-08 14:26                             ` Mikulas Patocka
2022-08-08 14:40                               ` Matthew Wilcox
2022-08-08 14:57                                 ` Mikulas Patocka
2022-08-08 15:31                                   ` Paul E. McKenney
2022-08-08 15:39                                   ` Matthew Wilcox
2022-08-09 18:32                                     ` [PATCH v6] add barriers to buffer_uptodate and set_buffer_uptodate Mikulas Patocka
2022-08-09 19:44                                       ` Matthew Wilcox
2022-08-09 22:06                                       ` Linus Torvalds
2022-08-01 10:42               ` [PATCH v4 1/2] introduce test_bit_acquire and use it in wait_on_bit Mikulas Patocka
2022-08-01 15:54                 ` Will Deacon
2022-08-01 16:12                   ` Mikulas Patocka
2022-08-01 18:17                     ` Boqun Feng
2022-08-02  8:00                       ` David Laight
2022-08-02  8:40                     ` Will Deacon
2022-08-02 11:38                       ` Mikulas Patocka
2022-08-02 13:36                         ` Will Deacon
2022-08-02 15:57                           ` Mikulas Patocka
2022-08-01  0:27             ` Alan Stern [this message]
2022-07-31 20:43           ` [PATCH v3 2/2] make buffer_locked provide an acquire semantics Mikulas Patocka
2022-07-31 20:51             ` Linus Torvalds
2022-07-31 22:14             ` Matthew Wilcox
2022-07-31 22:31               ` Ard Biesheuvel
2022-07-31 22:48                 ` Ard Biesheuvel
2022-07-31 20:46           ` [PATCH v2] " Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YuceB1x8twgpM7Bl@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=akiyks@gmail.com \
    --cc=ardb@kernel.org \
    --cc=boqun.feng@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=dlustig@nvidia.com \
    --cc=j.alglave@ucl.ac.uk \
    --cc=joel@joelfernandes.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luc.maranget@inria.fr \
    --cc=mpatocka@redhat.com \
    --cc=npiggin@gmail.com \
    --cc=parri.andrea@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox