linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] char/random: fix data races at timer_rand_state
@ 2020-02-25 16:27 Qian Cai
  2020-02-28  4:11 ` Theodore Y. Ts'o
  0 siblings, 1 reply; 2+ messages in thread
From: Qian Cai @ 2020-02-25 16:27 UTC (permalink / raw)
  To: tytso; +Cc: elver, linux-kernel, Qian Cai

Fields in "struct timer_rand_state" could be accessed concurrently.
Lockless plain reads and writes result in data races. Fix them by adding
pairs of READ|WRITE_ONCE(). The data races were reported by KCSAN,

 BUG: KCSAN: data-race in add_timer_randomness / add_timer_randomness

 write to 0xffff9f320a0a01d0 of 8 bytes by interrupt on cpu 22:
  add_timer_randomness+0x100/0x190
  add_timer_randomness at drivers/char/random.c:1152
  add_disk_randomness+0x85/0x280
  scsi_end_request+0x43a/0x4a0
  scsi_io_completion+0xb7/0x7e0
  scsi_finish_command+0x1ed/0x2a0
  scsi_softirq_done+0x1c9/0x1d0
  blk_done_softirq+0x181/0x1d0
  __do_softirq+0xd9/0x57c
  irq_exit+0xa2/0xc0
  do_IRQ+0x8b/0x190
  ret_from_intr+0x0/0x42
  cpuidle_enter_state+0x15e/0x980
  cpuidle_enter+0x69/0xc0
  call_cpuidle+0x23/0x40
  do_idle+0x248/0x280
  cpu_startup_entry+0x1d/0x1f
  start_secondary+0x1b2/0x230
  secondary_startup_64+0xb6/0xc0

 no locks held by swapper/22/0.
 irq event stamp: 32871382
 _raw_spin_unlock_irqrestore+0x53/0x60
 _raw_spin_lock_irqsave+0x21/0x60
 _local_bh_enable+0x21/0x30
 irq_exit+0xa2/0xc0

 read to 0xffff9f320a0a01d0 of 8 bytes by interrupt on cpu 2:
  add_timer_randomness+0xe8/0x190
  add_disk_randomness+0x85/0x280
  scsi_end_request+0x43a/0x4a0
  scsi_io_completion+0xb7/0x7e0
  scsi_finish_command+0x1ed/0x2a0
  scsi_softirq_done+0x1c9/0x1d0
  blk_done_softirq+0x181/0x1d0
  __do_softirq+0xd9/0x57c
  irq_exit+0xa2/0xc0
  do_IRQ+0x8b/0x190
  ret_from_intr+0x0/0x42
  cpuidle_enter_state+0x15e/0x980
  cpuidle_enter+0x69/0xc0
  call_cpuidle+0x23/0x40
  do_idle+0x248/0x280
  cpu_startup_entry+0x1d/0x1f
  start_secondary+0x1b2/0x230
  secondary_startup_64+0xb6/0xc0

 no locks held by swapper/2/0.
 irq event stamp: 37846304
 _raw_spin_unlock_irqrestore+0x53/0x60
 _raw_spin_lock_irqsave+0x21/0x60
 _local_bh_enable+0x21/0x30
 irq_exit+0xa2/0xc0

 Reported by Kernel Concurrency Sanitizer on:
 Hardware name: HP ProLiant BL660c Gen9, BIOS I38 10/17/2018

Signed-off-by: Qian Cai <cai@lca.pw>
---
 drivers/char/random.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index c7f9584de2c8..85cabb17b23f 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1142,14 +1142,14 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
 	 * We take into account the first, second and third-order deltas
 	 * in order to make our estimate.
 	 */
-	delta = sample.jiffies - state->last_time;
-	state->last_time = sample.jiffies;
+	delta = sample.jiffies - READ_ONCE(state->last_time);
+	WRITE_ONCE(state->last_time, sample.jiffies);
 
-	delta2 = delta - state->last_delta;
-	state->last_delta = delta;
+	delta2 = delta - READ_ONCE(state->last_delta);
+	WRITE_ONCE(state->last_delta, delta);
 
-	delta3 = delta2 - state->last_delta2;
-	state->last_delta2 = delta2;
+	delta3 = delta2 - READ_ONCE(state->last_delta2);
+	WRITE_ONCE(state->last_delta2, delta2);
 
 	if (delta < 0)
 		delta = -delta;
-- 
1.8.3.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] char/random: fix data races at timer_rand_state
  2020-02-25 16:27 [PATCH] char/random: fix data races at timer_rand_state Qian Cai
@ 2020-02-28  4:11 ` Theodore Y. Ts'o
  0 siblings, 0 replies; 2+ messages in thread
From: Theodore Y. Ts'o @ 2020-02-28  4:11 UTC (permalink / raw)
  To: Qian Cai; +Cc: elver, linux-kernel

On Tue, Feb 25, 2020 at 11:27:04AM -0500, Qian Cai wrote:
> Fields in "struct timer_rand_state" could be accessed concurrently.
> Lockless plain reads and writes result in data races. Fix them by adding
> pairs of READ|WRITE_ONCE(). The data races were reported by KCSAN,
> 
>  BUG: KCSAN: data-race in add_timer_randomness / add_timer_randomness
> 
>  write to 0xffff9f320a0a01d0 of 8 bytes by interrupt on cpu 22:
>   add_timer_randomness+0x100/0x190
>   add_timer_randomness at drivers/char/random.c:1152
>   add_disk_randomness+0x85/0x280
>   scsi_end_request+0x43a/0x4a0
>   scsi_io_completion+0xb7/0x7e0
>   scsi_finish_command+0x1ed/0x2a0
>   scsi_softirq_done+0x1c9/0x1d0
>   blk_done_softirq+0x181/0x1d0
>   __do_softirq+0xd9/0x57c
>   irq_exit+0xa2/0xc0
>   do_IRQ+0x8b/0x190
>   ret_from_intr+0x0/0x42
>   cpuidle_enter_state+0x15e/0x980
>   cpuidle_enter+0x69/0xc0
>   call_cpuidle+0x23/0x40
>   do_idle+0x248/0x280
>   cpu_startup_entry+0x1d/0x1f
>   start_secondary+0x1b2/0x230
>   secondary_startup_64+0xb6/0xc0
> 
>  no locks held by swapper/22/0.
>  irq event stamp: 32871382
>  _raw_spin_unlock_irqrestore+0x53/0x60
>  _raw_spin_lock_irqsave+0x21/0x60
>  _local_bh_enable+0x21/0x30
>  irq_exit+0xa2/0xc0
> 
>  read to 0xffff9f320a0a01d0 of 8 bytes by interrupt on cpu 2:
>   add_timer_randomness+0xe8/0x190
>   add_disk_randomness+0x85/0x280
>   scsi_end_request+0x43a/0x4a0
>   scsi_io_completion+0xb7/0x7e0
>   scsi_finish_command+0x1ed/0x2a0
>   scsi_softirq_done+0x1c9/0x1d0
>   blk_done_softirq+0x181/0x1d0
>   __do_softirq+0xd9/0x57c
>   irq_exit+0xa2/0xc0
>   do_IRQ+0x8b/0x190
>   ret_from_intr+0x0/0x42
>   cpuidle_enter_state+0x15e/0x980
>   cpuidle_enter+0x69/0xc0
>   call_cpuidle+0x23/0x40
>   do_idle+0x248/0x280
>   cpu_startup_entry+0x1d/0x1f
>   start_secondary+0x1b2/0x230
>   secondary_startup_64+0xb6/0xc0
> 
>  no locks held by swapper/2/0.
>  irq event stamp: 37846304
>  _raw_spin_unlock_irqrestore+0x53/0x60
>  _raw_spin_lock_irqsave+0x21/0x60
>  _local_bh_enable+0x21/0x30
>  irq_exit+0xa2/0xc0
> 
>  Reported by Kernel Concurrency Sanitizer on:
>  Hardware name: HP ProLiant BL660c Gen9, BIOS I38 10/17/2018
> 
> Signed-off-by: Qian Cai <cai@lca.pw>

Thanks, applied.

					- Ted

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-02-28  4:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-25 16:27 [PATCH] char/random: fix data races at timer_rand_state Qian Cai
2020-02-28  4:11 ` Theodore Y. Ts'o

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).