ALSA: pcm: oss: use proper stream lock for runtime->state access
PATCH v2 1/1 ALSA: pcm: oss: use proper stream lock for runtime->state access
__snd_pcm_set_state() writes runtime->state under the PCM stream lock. However, the OSS I/O functions snd_pcm_oss_write3(), snd_pcm_oss_read3(), snd_pcm_oss_writev3() and snd_pcm_oss_readv3() read runtime->state without holding the stream lock, only holding oss.params_lock (a different mutex that does not synchronize with the stream lock).
Since __snd_pcm_set_state() is called from IRQ context (e.g., snd_pcm_period_elapsed -> snd_pcm_update_state -> __snd_pcm_xrun -> snd_pcm_stop -> snd_pcm_post_stop) while the OSS read/write paths run in process context, these are concurrent accesses that constitute a data race.
Rather than using READ_ONCE()/WRITE_ONCE() barriers, introduce a snd_pcm_get_state() helper that reads runtime->state under the stream lock, matching the locking discipline used elsewhere in the PCM layer. Also export snd_pcm_set_state() for completeness.
Use snd_pcm_get_state() in all four OSS I/O functions, caching the result in a local variable where the same snapshot is used for multiple comparisons to avoid taking the lock repeatedly.
--- include/sound/pcm.h | 4 ++++ sound/core/oss/pcm_oss.c | 44 +++++++++++++++++++++++----------------- sound/core/pcm_native.c | 23 +++++++++++++++++++-- 3 files changed, 50 insertions(+), 21 deletions(-)
include/sound/pcm.h +4 -0
@@ -729,6 +729,10 @@ static inline void __snd_pcm_set_state(struct snd_pcm_runtime *runtime, runtime->status->state = state;/* copy for mmap */ } +void snd_pcm_set_state (struct snd_pcm_substream *substream, +snd_pcm_state_t state); +snd_pcm_state_t snd_pcm_get_state (struct snd_pcm_substream *substream); +/** * bytes_to_samples - Unit conversion of the size from bytes to samples * @runtime: PCM runtime instance
sound/core/oss/pcm_oss.c +25 -19
116 lines not on this page · raw message
sound/core/pcm_native.c +21 -2
@@ -618,13 +618,32 @@ static int period_to_usecs(struct snd_pcm_runtime *runtime)return usecs; } -static void snd_pcm_set_state (struct snd_pcm_substream *substream, -snd_pcm_state_t state) +/** +* snd_pcm_set_state - Set the PCM runtime state with stream lock +* @substream: PCM substream +* @state: state to set +*/ +void snd_pcm_set_state (struct snd_pcm_substream *substream, +snd_pcm_state_t state) {guard (pcm_stream_lock_irq)(substream);if (substream->runtime->state !=SNDRV_PCM_STATE_DISCONNECTED )__snd_pcm_set_state (substream->runtime, state); } +EXPORT_SYMBOL_GPL (snd_pcm_set_state); + +/** +* snd_pcm_get_state - Read the PCM runtime state with stream lock +* @substream: PCM substream +* +* Return: the current PCM state +*/ +snd_pcm_state_t snd_pcm_get_state (struct snd_pcm_substream *substream) +{ +guard (pcm_stream_lock_irqsave)(substream); +return substream->runtime->state; +} +EXPORT_SYMBOL_GPL (snd_pcm_get_state);static inline void snd_pcm_timer_notify (struct snd_pcm_substream *substream,int event)
Signature · 1 line
2.34.1
Takashi Iwai
AppliedQuoted 18 lines · Cen Zhang
__snd_pcm_set_state()writesruntime->stateunder the PCM stream lock. However, the OSS I/O functionssnd_pcm_oss_write3(),snd_pcm_oss_read3(),snd_pcm_oss_writev3()andsnd_pcm_oss_readv3()readruntime->statewithout holding the stream lock, only holding oss.params_lock (a different mutex that does not synchronize with the stream lock).Since
__snd_pcm_set_state()is called from IRQ context (e.g.,snd_pcm_period_elapsed->snd_pcm_update_state->__snd_pcm_xrun->snd_pcm_stop->snd_pcm_post_stop) while the OSS read/write paths run in process context, these are concurrent accesses that constitute a data race.Rather than using READ_ONCE()/WRITE_ONCE() barriers, introduce a
snd_pcm_get_state()helper that readsruntime->stateunder the stream lock, matching the locking discipline used elsewhere in the PCM layer. Also exportsnd_pcm_set_state()for completeness.
Quoted 5 lines
Use
snd_pcm_get_state()in all four OSS I/O functions, caching the result in a local variable where the same snapshot is used for multiple comparisons to avoid taking the lock repeatedly.Signed-off-by Cen Zhang <[email protected]>
Applied to for-next branch now. Thanks.
Takashi