mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] ASoC: hdmi-codec: ELD fixes for unplug and hotplug races
@ 2026-11-30  3:47 Lin Tzu Yang
  2026-11-30  3:47 ` [PATCH v2 1/2] ASoC: hdmi-codec: Clear the parsed ELD on unplug Lin Tzu Yang
  2026-11-30  3:48 ` [PATCH v2 2/2] ASoC: hdmi-codec: Hold the lock while the ELD is updated or read Lin Tzu Yang
  0 siblings, 2 replies; 3+ messages in thread
From: Lin Tzu Yang @ 2026-11-30  3:47 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown
  Cc: Jaroslav Kysela, Takashi Iwai, Dmitry Baryshkov, linux-sound,
	linux-kernel

Two fixes for how hdmi-codec handles the ELD across hotplug.

Patch 1 is v2 of "ASoC: hdmi-codec: Clear the parsed ELD on unplug".
Patch 2 was sent separately as "ASoC: hdmi-codec: Hold the lock while
the ELD is updated or read" and depends on patch 1, so they are now
sent together.

Changes in v2:
- Patch 1: clear the ELD before reporting the unplug, as Mark
  suggested, so userspace can't read the old ELD after the event.
- Patch 2: rebased on the new patch 1. The unplug path now clears the
  ELD under the lock and reports the jack after dropping it.
- Use my full name in Signed-off-by.

v1 of patch 1:
https://lore.kernel.org/all/20261126103757.655758-1-rongh7426@gmail.com/
v1 of patch 2:
https://lore.kernel.org/all/20261129083329.786322-1-rongh7426@gmail.com/

Lin Tzu Yang (2):
  ASoC: hdmi-codec: Clear the parsed ELD on unplug
  ASoC: hdmi-codec: Hold the lock while the ELD is updated or read

 sound/soc/codecs/hdmi-codec.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

-- 
2.51.1.windows.1


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

* [PATCH v2 1/2] ASoC: hdmi-codec: Clear the parsed ELD on unplug
  2026-11-30  3:47 [PATCH v2 0/2] ASoC: hdmi-codec: ELD fixes for unplug and hotplug races Lin Tzu Yang
@ 2026-11-30  3:47 ` Lin Tzu Yang
  2026-11-30  3:48 ` [PATCH v2 2/2] ASoC: hdmi-codec: Hold the lock while the ELD is updated or read Lin Tzu Yang
  1 sibling, 0 replies; 3+ messages in thread
From: Lin Tzu Yang @ 2026-11-30  3:47 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown
  Cc: Jaroslav Kysela, Takashi Iwai, Dmitry Baryshkov, linux-sound,
	linux-kernel

When the sink goes away, plugged_cb() clears the raw ELD but leaves
hcp->eld_parsed as it was. The eld#N procfs file prints eld_parsed, so
after an unplug it keeps showing the monitor that was last connected
until something is plugged in again.

On a Raspberry Pi 5 (vc4 HDMI), forcing the connector off through
/sys/class/drm/card1-HDMI-A-1/status leaves /proc/asound/card0/eld#0
at:

  monitor_name		WaveShare
  eld_version		[0x2] CEA-861D or below
  sad_count		1

Clear eld_parsed together with the raw ELD, as hdmi_codec_startup()
already does when it finds an empty ELD. eld_parsed is only used for the
procfs file and the log message in plugged_cb(), so nothing else
changes.

While at it, clear both before reporting the jack change rather than
after, so that userspace reacting to the unplug event doesn't read the
old ELD. The plug path already updates the ELD before reporting.

Tested on a Raspberry Pi 5 (Raspberry Pi OS kernel 6.18.39, with this
change applied to its snd-soc-hdmi-codec module), forcing HDMI-A-1 off
and back on twice:
- before: eld#0 still shows WaveShare after each unplug
- after: eld#0 shows an empty ELD (eld_version [0x0], sad_count 0) after
  each unplug and WaveShare again after each replug
- HDMI audio playback still works

This change and its description were written with the help of an AI
coding assistant (Claude); the testing above was done on real hardware.

Fixes: 0ecd24a6d8b2 ("ASoC: hdmi-codec: dump ELD through procfs")
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
v2: clear the ELD before the jack report instead of after (Mark).
    The reorder was only build-tested; the v1 test above still applies
    to the procfs result.

 sound/soc/codecs/hdmi-codec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sound/soc/codecs/hdmi-codec.c b/sound/soc/codecs/hdmi-codec.c
index bc59da2..7946f61 100644
--- a/sound/soc/codecs/hdmi-codec.c
+++ b/sound/soc/codecs/hdmi-codec.c
@@ -971,8 +971,9 @@ static void plugged_cb(struct device *dev, bool plugged)
 		}
 		hdmi_codec_jack_report(hcp, SND_JACK_AVOUT);
 	} else {
-		hdmi_codec_jack_report(hcp, 0);
 		memset(hcp->eld, 0, sizeof(hcp->eld));
+		memset(&hcp->eld_parsed, 0, sizeof(hcp->eld_parsed));
+		hdmi_codec_jack_report(hcp, 0);
 	}
 }
 
-- 
2.51.1.windows.1


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

* [PATCH v2 2/2] ASoC: hdmi-codec: Hold the lock while the ELD is updated or read
  2026-11-30  3:47 [PATCH v2 0/2] ASoC: hdmi-codec: ELD fixes for unplug and hotplug races Lin Tzu Yang
  2026-11-30  3:47 ` [PATCH v2 1/2] ASoC: hdmi-codec: Clear the parsed ELD on unplug Lin Tzu Yang
@ 2026-11-30  3:48 ` Lin Tzu Yang
  1 sibling, 0 replies; 3+ messages in thread
From: Lin Tzu Yang @ 2026-11-30  3:48 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown
  Cc: Jaroslav Kysela, Takashi Iwai, Dmitry Baryshkov, linux-sound,
	linux-kernel

plugged_cb() rewrites hcp->eld and hcp->eld_parsed without taking
hcp->lock, while the ELD control, the ELD proc file and hw_params read
them without it too. Only startup() takes the lock. A reader that runs
during a hotplug can see a half-copied or half-cleared ELD.

On a Raspberry Pi 5 (vc4) I toggled the connector between forced off
and on while reading the ELD control in a loop. In 120 seconds 3 of
about 266 million reads returned an ELD that was neither the monitor's
nor all zero: the header copied and the rest still zero, or the first
16 bytes cleared and the monitor name still there.

Take hcp->lock around the ELD update in plugged_cb() and around the
readers. None of the drivers that call plugged_cb() take a lock that
startup() also takes under hcp->lock, so this adds no lock inversion.

With this patch the same test gave no torn reads in about 255 million
reads, and repeated playback during hotplug still worked with no hung
tasks. Tested as an out-of-tree module on 6.18.39.

The problem was found by the sashiko review bot, and the patch was
written with the help of an AI coding assistant (Claude). I reproduced
it, tested it on hardware and reviewed the change.

Fixes: 25ce4f2b3593 ("ASoC: hdmi-codec: Get ELD in before reporting plugged event")
Fixes: 0ecd24a6d8b2 ("ASoC: hdmi-codec: dump ELD through procfs")
Signed-off-by: Lin Tzu Yang <rongh7426@gmail.com>
---
v2: rebased on v2 of patch 1; the unplug path takes the lock only
    around the memsets and reports the jack after dropping it. Build
    tested; runtime testing above was on v1.

 sound/soc/codecs/hdmi-codec.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/sound/soc/codecs/hdmi-codec.c b/sound/soc/codecs/hdmi-codec.c
index 7946f61..d3bbca2 100644
--- a/sound/soc/codecs/hdmi-codec.c
+++ b/sound/soc/codecs/hdmi-codec.c
@@ -318,6 +318,7 @@ static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
 
+	guard(mutex)(&hcp->lock);
 	memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
 
 	return 0;
@@ -526,7 +527,8 @@ static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
 
 	if (pcm_audio) {
 		/* Select a channel allocation that matches with ELD and pcm channels */
-		idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
+		scoped_guard(mutex, &hcp->lock)
+			idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
 
 		if (idx < 0) {
 			dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
@@ -841,6 +843,7 @@ static void print_eld_info(struct snd_info_entry *entry,
 {
 	struct hdmi_codec_priv *hcp = entry->private_data;
 
+	guard(mutex)(&hcp->lock);
 	snd_print_eld_info(&hcp->eld_parsed, buffer);
 }
 
@@ -960,6 +963,7 @@ static void plugged_cb(struct device *dev, bool plugged)
 
 	if (plugged) {
 		if (hcp->hcd.ops->get_eld) {
+			guard(mutex)(&hcp->lock);
 			hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
 					    hcp->eld, sizeof(hcp->eld));
 			ret = snd_parse_eld(dev, &hcp->eld_parsed,
@@ -971,8 +975,10 @@ static void plugged_cb(struct device *dev, bool plugged)
 		}
 		hdmi_codec_jack_report(hcp, SND_JACK_AVOUT);
 	} else {
-		memset(hcp->eld, 0, sizeof(hcp->eld));
-		memset(&hcp->eld_parsed, 0, sizeof(hcp->eld_parsed));
+		scoped_guard(mutex, &hcp->lock) {
+			memset(hcp->eld, 0, sizeof(hcp->eld));
+			memset(&hcp->eld_parsed, 0, sizeof(hcp->eld_parsed));
+		}
 		hdmi_codec_jack_report(hcp, 0);
 	}
 }
-- 
2.51.1.windows.1


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

end of thread, other threads:[~2026-11-30  3:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-30  3:47 [PATCH v2 0/2] ASoC: hdmi-codec: ELD fixes for unplug and hotplug races Lin Tzu Yang
2026-11-30  3:47 ` [PATCH v2 1/2] ASoC: hdmi-codec: Clear the parsed ELD on unplug Lin Tzu Yang
2026-11-30  3:48 ` [PATCH v2 2/2] ASoC: hdmi-codec: Hold the lock while the ELD is updated or read Lin Tzu Yang

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®