ALSA: hda: potential hdac_stream locking issues?
Subsystems (3)
cover ALSA: hda: potential hdac_stream locking issues?
While reviewing the HDAudio DMA handling, I found a number of inconsistencies in how spin_locks are used. It's not clear what the HDaudio bus->reg_lock is supposed to protect. In most cases only the writes to specific boolean status flags are protected, and there are multiple cases of taking the lock after testing a status flag.
This patchset suggests a more consistent locking pattern, but it's entirely possible that the bus->reg_lock is only intented to protect register read/write access on the HDaudio bus, and not the status flags, and that this entire piece of code is completely over-engineered.
On the Intel side no one knows why this spinlock was used, the reasons are lost to history. I set the 'RFC' status on purpose in the hope that Takashi might recall what this lock is supposed to protect. The diff format makes this patchset difficult to review, it's recommended to apply the patches and look at entire functions with changes to get a better idea of the suggested changes.
Pierre-Louis Bossart (4):
ALSA: hda: hdac_stream: fix potential locking issue in
snd_hdac_stream_assign()
ALSA: hda: hdac_stream: reset assigned_key in stream_release()
ALSA: hda: hdac_ext_stream: fix potential locking issues
ASoC: SOF: Intel: hda-dai: fix potential locking issue
include/sound/hdaudio_ext.h | 2 ++ sound/hda/ext/hdac_ext_stream.c | 46 ++++++++++++++++++++------------- sound/hda/hdac_stream.c | 5 ++-- sound/soc/sof/intel/hda-dai.c | 7 ++--- 4 files changed, 37 insertions(+), 23 deletions(-)
Signature · 1 line
2.25.1
RFC PATCH 1/4 ALSA: hda: hdac_stream: fix potential locking issue in snd_hdac_stream_assign()
The fields 'opened', 'running', 'assigned_key' are all protected by a spinlock, but the spinlock is not taken when looking for a stream. This can result in a possible race between assign() and release().
Fix by taking the spinlock before walking through the bus stream list.
--- sound/hda/hdac_stream.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
sound/hda/hdac_stream.c +2 -2
@@ -296,6 +296,7 @@ struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus,int key = (substream->pcm->device <<16 ) | (substream->number <<2 ) | (substream->stream +1 ); +spin_lock_irq (&bus->reg_lock);list_for_each_entry (azx_dev, &bus->stream_list, list) {if (azx_dev->direction != substream->stream)continue ;
@@ -309,13 +310,12 @@ struct hdac_stream *snd_hdac_stream_assign(struct hdac_bus *bus, res = azx_dev; }if (res) { -spin_lock_irq (&bus->reg_lock); res->opened =1 ; res->running =0 ; res->assigned_key = key; res->substream = substream; -spin_unlock_irq (&bus->reg_lock); } +spin_unlock_irq (&bus->reg_lock);return res; }EXPORT_SYMBOL_GPL (snd_hdac_stream_assign);
Signature · 1 line
2.25.1
RFC PATCH 2/4 ALSA: hda: hdac_stream: reset assigned_key in stream_release()
In reply to Pierre-Louis BossartThe 'assigned_key' field is set in assign() and never reset. For symmetry set to zero in release().
--- sound/hda/hdac_stream.c | 1 + 1 file changed, 1 insertion(+)
sound/hda/hdac_stream.c +1 -0
@@ -333,6 +333,7 @@ void snd_hdac_stream_release(struct hdac_stream *azx_dev)spin_lock_irq (&bus->reg_lock); azx_dev->opened =0 ; azx_dev->running =0 ; + azx_dev->assigned_key =0 ; azx_dev->substream =NULL ;spin_unlock_irq (&bus->reg_lock); }
Signature · 1 line
2.25.1
Re: [RFC PATCH 2/4] ALSA: hda: hdac_stream: reset assigned_key in stream_release()
Quoted 3 lines · Pierre-Louis Bossart
The '
assigned_key' field is set inassign()and never reset. For symmetry set to zero inrelease().
This is intentional behavior. We want to assign to the same stream persistently unless it has to be reassigned to another.
thanks,
Takashi
Quoted 21 lines
Signed-off-by Pierre-Louis Bossart <[email protected]>--- sound/hda/hdac_stream.c | 1 + 1 file changed, 1 insertion(+)sound/hda/hdac_stream.c +1
-0@@ -333,6 +333,7 @@ void snd_hdac_stream_release(struct hdac_stream *azx_dev)spin_lock_irq (&bus->reg_lock); azx_dev->opened =0 ; azx_dev->running =0 ; + azx_dev->assigned_key =0 ; azx_dev->substream =NULL ;spin_unlock_irq (&bus->reg_lock); }Signature · 1 line
2.25.1
RFC PATCH 3/4 ALSA: hda: hdac_ext_stream: fix potential locking issues
In reply to Pierre-Louis BossartThe code for hdac_ext_stream seems inherited from hdac_stream, and similar locking issues are present: the use of the bus->reg_lock spinlock is inconsistent, with only writes to specific fields being protected.
Apply similar fix as in hdac_stream by protecting all accesses to 'link_locked' and 'decoupled' fields, with a new helper snd_hdac_ext_stream_decouple_locked() added to simplify code changes.
--- include/sound/hdaudio_ext.h | 2 ++ sound/hda/ext/hdac_ext_stream.c | 46 ++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 18 deletions(-)
include/sound/hdaudio_ext.h +2 -0
@@ -88,6 +88,8 @@ struct hdac_ext_stream *snd_hdac_ext_stream_assign(struct hdac_bus *bus,struct snd_pcm_substream *substream, int type);void snd_hdac_ext_stream_release (struct hdac_ext_stream *azx_dev,int type); +void snd_hdac_ext_stream_decouple_locked (struct hdac_bus *bus, +struct hdac_ext_stream *azx_dev,bool decouple);void snd_hdac_ext_stream_decouple (struct hdac_bus *bus,struct hdac_ext_stream *azx_dev,bool decouple);void snd_hdac_ext_stop_streams (struct hdac_bus *bus);
sound/hda/ext/hdac_ext_stream.c +28 -18
116 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH 4/4 ASoC: SOF: Intel: hda-dai: fix potential locking issue
In reply to Pierre-Louis BossartThe initial hdac_stream code was adapted a third time with the same locking issues. Move the spin_lock outside the loops and make sure the fields are protected on read/write.
--- sound/soc/sof/intel/hda-dai.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
sound/soc/sof/intel/hda-dai.c +4 -3
@@ -72,6 +72,7 @@ static struct hdac_ext_stream *return NULL ; } +spin_lock_irq (&bus->reg_lock);list_for_each_entry (stream, &bus->stream_list, list) {struct hdac_ext_stream *hstream =stream_to_hdac_ext_stream (stream);
@@ -111,12 +112,12 @@ static struct hdac_ext_stream ** is updated in snd_hdac_ext_stream_decouple(). */ if (!res->decoupled) -snd_hdac_ext_stream_decouple (bus, res, true); -spin_lock_irq (&bus->reg_lock); +snd_hdac_ext_stream_decouple_locked (bus, res, true); + res->link_locked =1 ; res->link_substream = substream; -spin_unlock_irq (&bus->reg_lock); } +spin_unlock_irq (&bus->reg_lock);return res; }
Signature · 1 line
2.25.1
Re: [RFC PATCH 4/4] ASoC: SOF: Intel: hda-dai: fix potential locking issue
Quoted 3 lines · Pierre-Louis Bossart
The initial
hdac_streamcode was adapted a third time with the same locking issues. Move thespin_lockoutside the loops and make sure the fields are protected on read/write.
Quoted 13 lines · Pierre-Louis Bossart
While reviewing the HDAudio DMA handling, I found a number of inconsistencies in how
spin_locksare used. It's not clear what the HDaudiobus->reg_lockis supposed to protect. In most cases only the writes to specific boolean status flags are protected, and there are multiple cases of taking the lock after testing a status flag.This patchset suggests a more consistent locking pattern, but it's entirely possible that the
bus->reg_lockis only intented to protect register read/write access on the HDaudio bus, and not the status flags, and that this entire piece of code is completely over-engineered.
Quoted 6 lines
On the Intel side no one knows why this spinlock was used, the reasons are lost to history. I set the 'RFC' status on purpose in the hope that Takashi might recall what this lock is supposed to protect. The diff format makes this patchset difficult to review, it's recommended to apply the patches and look at entire functions with changes to get a better idea of the suggested changes.
Oh well, the missing piece was the lock inside the loop in *_stream_assign(). It was lost while factoring out and converting to the HD-audio core code. (The lock wasn't taken on the whole loop at that time because the list itself was supposed to be static.)
In anyway, I applied all patches except for patch#2, as the fixes for spinlock look correct.
thanks,
Takashi