mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: seq_oss: fix waitqueue_active without memory barrier in snd-seq-oss
@ 2015-10-09  0:35 Kosuke Tatsukawa
  2015-10-09  7:46 ` Takashi Iwai
  0 siblings, 1 reply; 2+ messages in thread
From: Kosuke Tatsukawa @ 2015-10-09  0:35 UTC (permalink / raw)
  To: Jaroslav Kysela, Takashi Iwai, Markus Elfring, Mark Brown, Jie Yang
  Cc: alsa-devel, linux-kernel

snd_seq_oss_readq_put_event() seems to be missing a memory barrier which
might cause the waker to not notice the waiter and miss sending a
wake_up as in the following figure.

    snd_seq_oss_readq_put_event		    snd_seq_oss_readq_wait
------------------------------------------------------------------------
					/* wait_event_interruptible_timeout */
					 /* __wait_event_interruptible_timeout */
					  /* ___wait_event */
					  for (;;) {									 prepare_to_wait_event(&wq, &__wait,
					    state);
spin_lock_irqsave(&q->lock, flags);
if (waitqueue_active(&q->midi_sleep))
/* The CPU might reorder the test for
   the waitqueue up here, before
   prior writes complete */
					  if ((q->qlen>0 || q->head==q->tail)
					  ...
					  __ret = schedule_timeout(__ret)
if (q->qlen >= q->maxlen - 1) {
memcpy(&q->q[q->tail], ev, sizeof(*ev));
q->tail = (q->tail + 1) % q->maxlen;
q->qlen++;
------------------------------------------------------------------------

There are two other place in sound/core/seq/oss/ which have similar
code.  The attached patch removes the call to waitqueue_active() leaving
just wake_up() behind.  This fixes the problem because the call to
spin_lock_irqsave() in wake_up() will be an ACQUIRE operation.

I found this issue when I was looking through the linux source code
for places calling waitqueue_active() before wake_up*(), but without
preceding memory barriers, after sending a patch to fix a similar
issue in drivers/tty/n_tty.c  (Details about the original issue can be
found here: https://lkml.org/lkml/2015/9/28/849).

Signed-off-by: Kosuke Tatsukawa <tatsu@ab.jp.nec.com>
---
 sound/core/seq/oss/seq_oss_readq.c  |    6 ++----
 sound/core/seq/oss/seq_oss_writeq.c |    4 +---
 2 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/sound/core/seq/oss/seq_oss_readq.c b/sound/core/seq/oss/seq_oss_readq.c
index ccd8935..046cb58 100644
--- a/sound/core/seq/oss/seq_oss_readq.c
+++ b/sound/core/seq/oss/seq_oss_readq.c
@@ -91,8 +91,7 @@ snd_seq_oss_readq_clear(struct seq_oss_readq *q)
 		q->head = q->tail = 0;
 	}
 	/* if someone sleeping, wake'em up */
-	if (waitqueue_active(&q->midi_sleep))
-		wake_up(&q->midi_sleep);
+	wake_up(&q->midi_sleep);
 	q->input_time = (unsigned long)-1;
 }
 
@@ -138,8 +137,7 @@ snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
 	q->qlen++;
 
 	/* wake up sleeper */
-	if (waitqueue_active(&q->midi_sleep))
-		wake_up(&q->midi_sleep);
+	wake_up(&q->midi_sleep);
 
 	spin_unlock_irqrestore(&q->lock, flags);
 
diff --git a/sound/core/seq/oss/seq_oss_writeq.c b/sound/core/seq/oss/seq_oss_writeq.c
index d50338b..1f6788a 100644
--- a/sound/core/seq/oss/seq_oss_writeq.c
+++ b/sound/core/seq/oss/seq_oss_writeq.c
@@ -138,9 +138,7 @@ snd_seq_oss_writeq_wakeup(struct seq_oss_writeq *q, abstime_t time)
 	spin_lock_irqsave(&q->sync_lock, flags);
 	q->sync_time = time;
 	q->sync_event_put = 0;
-	if (waitqueue_active(&q->sync_sleep)) {
-		wake_up(&q->sync_sleep);
-	}
+	wake_up(&q->sync_sleep);
 	spin_unlock_irqrestore(&q->sync_lock, flags);
 }
 
-- 
1.7.1

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

* Re: [PATCH] ALSA: seq_oss: fix waitqueue_active without memory barrier in snd-seq-oss
  2015-10-09  0:35 [PATCH] ALSA: seq_oss: fix waitqueue_active without memory barrier in snd-seq-oss Kosuke Tatsukawa
@ 2015-10-09  7:46 ` Takashi Iwai
  0 siblings, 0 replies; 2+ messages in thread
From: Takashi Iwai @ 2015-10-09  7:46 UTC (permalink / raw)
  To: Kosuke Tatsukawa
  Cc: Jie Yang, Mark Brown, Jaroslav Kysela, Markus Elfring,
	alsa-devel, linux-kernel

On Fri, 09 Oct 2015 02:35:54 +0200,
Kosuke Tatsukawa wrote:
> 
> snd_seq_oss_readq_put_event() seems to be missing a memory barrier which
> might cause the waker to not notice the waiter and miss sending a
> wake_up as in the following figure.
> 
>     snd_seq_oss_readq_put_event		    snd_seq_oss_readq_wait
> ------------------------------------------------------------------------
> 					/* wait_event_interruptible_timeout */
> 					 /* __wait_event_interruptible_timeout */
> 					  /* ___wait_event */
> 					  for (;;) {									 prepare_to_wait_event(&wq, &__wait,
> 					    state);
> spin_lock_irqsave(&q->lock, flags);
> if (waitqueue_active(&q->midi_sleep))
> /* The CPU might reorder the test for
>    the waitqueue up here, before
>    prior writes complete */
> 					  if ((q->qlen>0 || q->head==q->tail)
> 					  ...
> 					  __ret = schedule_timeout(__ret)
> if (q->qlen >= q->maxlen - 1) {
> memcpy(&q->q[q->tail], ev, sizeof(*ev));
> q->tail = (q->tail + 1) % q->maxlen;
> q->qlen++;
> ------------------------------------------------------------------------
> 
> There are two other place in sound/core/seq/oss/ which have similar
> code.  The attached patch removes the call to waitqueue_active() leaving
> just wake_up() behind.  This fixes the problem because the call to
> spin_lock_irqsave() in wake_up() will be an ACQUIRE operation.
> 
> I found this issue when I was looking through the linux source code
> for places calling waitqueue_active() before wake_up*(), but without
> preceding memory barriers, after sending a patch to fix a similar
> issue in drivers/tty/n_tty.c  (Details about the original issue can be
> found here: https://lkml.org/lkml/2015/9/28/849).
> 
> Signed-off-by: Kosuke Tatsukawa <tatsu@ab.jp.nec.com>

Thanks, applied now.


Takashi

> ---
>  sound/core/seq/oss/seq_oss_readq.c  |    6 ++----
>  sound/core/seq/oss/seq_oss_writeq.c |    4 +---
>  2 files changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/sound/core/seq/oss/seq_oss_readq.c b/sound/core/seq/oss/seq_oss_readq.c
> index ccd8935..046cb58 100644
> --- a/sound/core/seq/oss/seq_oss_readq.c
> +++ b/sound/core/seq/oss/seq_oss_readq.c
> @@ -91,8 +91,7 @@ snd_seq_oss_readq_clear(struct seq_oss_readq *q)
>  		q->head = q->tail = 0;
>  	}
>  	/* if someone sleeping, wake'em up */
> -	if (waitqueue_active(&q->midi_sleep))
> -		wake_up(&q->midi_sleep);
> +	wake_up(&q->midi_sleep);
>  	q->input_time = (unsigned long)-1;
>  }
>  
> @@ -138,8 +137,7 @@ snd_seq_oss_readq_put_event(struct seq_oss_readq *q, union evrec *ev)
>  	q->qlen++;
>  
>  	/* wake up sleeper */
> -	if (waitqueue_active(&q->midi_sleep))
> -		wake_up(&q->midi_sleep);
> +	wake_up(&q->midi_sleep);
>  
>  	spin_unlock_irqrestore(&q->lock, flags);
>  
> diff --git a/sound/core/seq/oss/seq_oss_writeq.c b/sound/core/seq/oss/seq_oss_writeq.c
> index d50338b..1f6788a 100644
> --- a/sound/core/seq/oss/seq_oss_writeq.c
> +++ b/sound/core/seq/oss/seq_oss_writeq.c
> @@ -138,9 +138,7 @@ snd_seq_oss_writeq_wakeup(struct seq_oss_writeq *q, abstime_t time)
>  	spin_lock_irqsave(&q->sync_lock, flags);
>  	q->sync_time = time;
>  	q->sync_event_put = 0;
> -	if (waitqueue_active(&q->sync_sleep)) {
> -		wake_up(&q->sync_sleep);
> -	}
> +	wake_up(&q->sync_sleep);
>  	spin_unlock_irqrestore(&q->sync_lock, flags);
>  }
>  
> -- 
> 1.7.1
> 

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

end of thread, other threads:[~2015-10-09  7:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-10-09  0:35 [PATCH] ALSA: seq_oss: fix waitqueue_active without memory barrier in snd-seq-oss Kosuke Tatsukawa
2015-10-09  7:46 ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®