From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796131804; b=NKW8xbhufy+gjauqtoSjE4PYvw8NXAWNMhH6Zj4yiyN2uZ1VLRCU594xWuk/aqbTowVh Ummfp9yq+ZqeOOiC8cA1XxQDFUtMWfZIZCZWC63dnfXWPZDS/ivk3S6IgQF08GsNx2bfn gW4C20SS2w0tl3rKUWST8cFlWgMclRtryKc7yWUPgjyDk/qAFDeYKN24He6CAOkVll5lg eXukmplp5PNdPw5nZ5czfjRLB1UJLgl6jt9EUptXUxyFa9IW8eg+fYzvjQtXF3Fw2rwbY 0DGAKE6w6GxehyseWA4Er4IiRRLjLvCeN/iAsEQzWmakdOjVZw+x13hysyPEkZnWqdA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796131804; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=5XUYd0b5uzEiQRsXR+LHCnoaKMm9C62ZOl6icqFmFwo=; b=o0KpZWP297gw8raYagytg+o47hCeeeyUMabrtxWJBt+k0JOCPZKMmbKgRLP2KwNBuVcr EkYNWh1TDJoUhkx7+KkRJWHd1YKpXVSXia++VEidBuHzE/qm6k7uGGQqKvUO/ivDUHRZp CiqBBLNXVopU5WM8Hh8lZ5z5chkZjj9EaZnxfXUrTi0b0GxmGS+EKJKS1YN3q5M78+t/4 xkDF8SkrwJB/UEkdbwexR2IA2pyLETMvxQO2zP3TB3Bxg5c3UBe1osYtYN8UjWBDZ7Bws +TUCiPnzL9apd1eddXOm+p/dfMbykmb5VXiVejyQkv1lVRFu/wlWYs/B9yyObl+0EGg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dnrz9NgR; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 732A0606A327 for ; Tue, 1 Dec 2026 13:30:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=dnrz9NgR Received: by smtp.kernel.org (Postfix) with ESMTPSA id C94881F00899; Tue, 1 Dec 2026 13:30:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796131803; bh=5XUYd0b5uzEiQRsXR+LHCnoaKMm9C62ZOl6icqFmFwo=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=dnrz9NgRmdRlk2SzKCmCL+2svy3UFyIFpqvfVaye8Fo9ldK2m30cOtH5P1UxgUJqz CPL2pE948iXP3X4HQrfW89XeTRp1kUn0amDcOLRKw3xrm7Bm77ib3S1a+tMbZcdrSA ufnCV0nxLzvmv6VdlvaEShmMBwlkmzoVr64td/1onkLV16T6RS6x/0IfjWCWMtyQeM LLzZmW0JBNK/1UOoYTMExgJmgQ/mNWQvtzJQowaogiGnnbs6Yqw1hmkfs9A3O0vn2N Prs9pSCaN5uLR9o5hotOR5DAmRzBPm5fkXe9aj5tUWi9AB4M2b+B13DyZKpUHhDhOS 6YAP/ze/Z/ZbA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: ALSA: hda/hdmi: hold pcm_lock for the unlocked hdmi_present_sense() callers (Data race) Reply-To: sashiko-reviews@lists.linux.dev To: =?utf-8?b?5b6Q5pmX57+U?= Cc: linux-sound@vger.kernel.org In-Reply-To: <4336415a.5017.1a231c1ccad.Coremail.xuhx@hust.edu.cn> References: <4c7f9772.192e1.1a17d5c2ed5.Coremail.xuhx@hust.edu.cn> <2026102727-data-sequel-ce41@gregkh> <4336415a.5017.1a231c1ccad.Coremail.xuhx@hust.edu.cn> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Tue, 01 Dec 2026 13:30:03 +0000 X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Thank you for your contribution! Sashiko AI review found 4 potential issues to consider: - [High] ALSA: hda/hdmi: deadlock acquiring pcm_lock in snd_hda_hdmi_generic_resume() - [High] ALSA: hda/hdmi: missing Signed-off-by trailer in commit message - [Medium] ALSA: hda/hdmi: narrow pcm_lock scope in snd_hda_hdmi_generic_build_controls() - [Low] ALSA: hda/hdmi: raw bug-report sections and duplicate text in commit message -- commit 3fad97f4628b9f9df44103d1e3999fd12ad5e055 Author: =E5=BE=90=E6=99=97=E7=BF=94 > ALSA: hda/hdmi: hold pcm_lock for the unlocked hdmi_present_sense() calle= rs (Data race) >=20 > hdmi_present_sense() parses display ELD data into a single shared > scratch buffer, struct hdmi_spec::temp_eld (sound/hda/codecs/hdmi/ > hdmi_local.h:143, one instance per codec), and update_eld() then > copies that buffer into the per-pin sink_eld and drives PCM-slot > attachment from it. The parsing helpers (hdmi_present_sense_via_verbs() > and sync_eld_via_acomp()) only take the *per-pin* mutex around the > fill-and-consume window (hdmi.c:1269/1383) - the buffer itself is > per-spec, so two pins parsed concurrently on different CPUs overwrite > each other's data while each holds a *different* mutex. The outer > spec->pcm_lock exists for exactly this serialization and IS taken by > the DRM hotplug notification path (snd_hda_hdmi_check_presence_and_report= (), > hdmi.c:592) and by the ELD repoll work (hdmi_repoll_eld(), > hdmi.c:1428) - but two callers do not take it: >=20 > - snd_hda_hdmi_generic_build_controls() (hdmi.c:1960, reached from > codec probe, i.e. from the HDA reconfig path) and > - snd_hda_hdmi_generic_resume() (hdmi.c:2094, wired as > patch_ops.resume and reached on every runtime/system resume). >=20 > Consequences when a hotplug notification or a repoll races the > unlocked probe/resume call (multi-pin HDMI/DP codecs are the norm on > Intel desktop parts): update_eld() on pin A reads pin B's ELD data > mid-overwrite (cross-pin ELD mismatch - the wrong monitor > capabilities/EDID are published through the ELD kcontrol and > snd_jack_report()), torn eld_size/eld_valid reads give the > sink_eld memcpy a mismatched length, and the unlocked > hdmi_attach_hda_pcm()/hdmi_pcm_setup_pin() inside update_eld() do an > unlocked hdmi_find_pcm_slot() bitmap scan plus bare writes of > per_pin->pcm_idx/per_pin->pcm/spec->pcm_bitmap (hdmi.c:1013-1035, > 1066-1107), which can double-bind the same PCM slot to two pins > against the pcm_lock-holding PCM open/close paths (hdmi.c:1656/1739). >=20 > The detailed description: >=20 > * Test environment / affected version (still present as of 7.4-rc5) >=20 > Source-verified on Linux 7.3.0 and RE-CHECKED on Linux 7.4.0-rc5 - > both vanilla mainline trees. Re-check result: the 7.4-rc5 diff to > sound/hda/codecs/hdmi/hdmi.c is 5 lines (a cvt_nids array bound fix > and a typo) and does not touch spec->temp_eld, the unlocked call > sites (now at hdmi.c:1961/2095) or the lock-domain mismatch - the > race is unchanged in 7.4-rc5. The proposed patch applies cleanly > to 7.4-rc5 (patch --dry-run passes). The audited files are > byte-identical between the report baseline and the tested tree. > This mail is source-analysis based (see the environment note at > the end). >=20 > * Mechanism >=20 > Lock domain vs data ownership mismatch: >=20 > - temp_eld is per-spec (hdmi_local.h:143) but its fill-and-consume > window is guarded only by per_pin->lock (hdmi.c:1269 for the > verb path, 1383 for the audio-component path) - different pins > hold different mutexes over the same buffer; >=20 > - the pcm_lock-holding callers: snd_hda_hdmi_check_presence_and_repor= t() > (hdmi.c:584-595, L592; entered from the DRM audio component > pin_eld_notify callback and from unsolicited events) and > hdmi_repoll_eld() (hdmi.c:1412-1430, L1428; 300ms retry work, > scheduled from update_eld() at hdmi.c:1154-1157); >=20 > - the unlocked callers: snd_hda_hdmi_generic_build_controls() > (hdmi.c:1950-1961, L1960 - codec probe/reconfig) and > snd_hda_hdmi_generic_resume() (hdmi.c:2083-2095, L2094 - wired > as generichdmi_codec_ops.resume, hdmi.c:2328; runtime/system > resume); >=20 > - the notify path cannot be excluded on the resume/probe side: > intel_pin_eld_notify() only filters PM_EVENT_SUSPEND > (intelhdmi.c:169-170), so a modeset/hotplug notification > concurrent with a runtime resume or a reconfig reaches the > same code. >=20 > Races: >=20 > 1. cross-pin overwrite: thread A (notify, pcm_lock + > per_pin1->lock) fills temp_eld for pin1 while thread B > (resume, per_pin2->lock only) overwrites it for pin2; A's > update_eld() then copies pin2's ELD into pin1's sink_eld > (hdmi.c:1197-1213) and publishes it via the ELD control event > (L1226-1230) and snd_jack_report() (L1232-1235); >=20 > 2. torn fields: eld_size/eld_valid/eld_buffer are written > non-atomically; a racing update_eld() can memcpy with a > mismatched length (bounded by ELD_MAX_SIZE, so this is state > corruption, not memory unsafety - the reason this is reported > as a functional data race rather than a security issue); >=20 > 3. PCM slot binding: hdmi_attach_hda_pcm() does an unlocked > hdmi_find_pcm_slot() scan (hdmi.c:1001-1011) and bare > set_bit/clear_bit on spec->pcm_bitmap plus bare writes of > per_pin->pcm_idx/pcm (L1032-1034); two concurrent update_eld() > calls can select the same slot for two pins, and the bare > writes race the pcm_lock-holding open/close paths that base > stream configuration on per_pin->pcm. >=20 > * Trigger model >=20 > HDMI/DP codec with at least two pins (standard on Intel desktop > HDA), plus a display hotplug/modeset (DRM audio component notify) > or an ELD repoll landing inside a probe (reconfig) or a > runtime/system resume. The repoll mechanism itself (300ms retries > while the sink ELD is not ready, hdmi.c:1154-1157) widens the > window. Consequence: wrong ELD published to user space, wrong > jack state, PCM misrouting (audio silent/routed to the wrong > monitor); the pcm slot double-bind can wedge subsequent stream > setup. >=20 > * Fix >=20 > Take spec->pcm_lock around the two unlocked hdmi_present_sense() > call sites (build_controls and generic_resume), matching the lock > domain the notify/repoll callers already use. Lock order is > unchanged: pcm_lock is taken before per_pin->lock exactly as in > snd_hda_hdmi_check_presence_and_report() and hdmi_repoll_eld(). > A per-pin scratch buffer would be the belt-and-braces alternative; > the two-line lock fix is the minimal, order-safe change. >=20 > The patch is regenerated against 7.4-rc5 (patch --dry-run passes > cleanly) and reviewed by inspection; it has not been runtime- > validated (see the environment note). Happy to test any > counter-proposal. >=20 > * Patch >=20 > hdmi_present_sense() parses the display ELD into the single shared > spec->temp_eld scratch buffer; the DRM notify and ELD repoll paths > hold spec->pcm_lock around it, but snd_hda_hdmi_generic_build_controls() > (probe/reconfig) and snd_hda_hdmi_generic_resume() call it with only > the per-pin lock. Two pins parsed concurrently share one buffer > while holding different per-pin mutexes, so one pin's update_eld() > copies the other pin's ELD into its sink_eld (cross-pin mismatch, > torn size/valid reads) and the unlocked slot-attach path races the > pcm_lock-protected PCM open/close state. >=20 > Take pcm_lock around the two call sites; the lock order > (pcm_lock -> per_pin->lock) is the one the existing callers already > use. [Severity: High] The commit message is missing a Signed-off-by: trailer. [Severity: Low] This isn't a bug, but the commit message includes raw bug-report sections ("* Test environment", "* Mechanism", "* Trigger model", "* Fix", "* Patch") that repeat the description three times along with conversational email notes. Could this be trimmed to a single concise changelog? > diff --git a/sound/hda/codecs/hdmi/hdmi.c b/sound/hda/codecs/hdmi/hdmi.c > index 355fd3e731836..123672c80a338 100644 > --- a/sound/hda/codecs/hdmi/hdmi.c > +++ b/sound/hda/codecs/hdmi/hdmi.c > @@ -1958,7 +1958,8 @@ int snd_hda_hdmi_generic_build_controls(struct hda_= codec *codec) > } > =20 > pin_eld->eld_valid =3D false; > - hdmi_present_sense(per_pin, 0); > + scoped_guard(mutex, &spec->pcm_lock) > + hdmi_present_sense(per_pin, 0); > } > =20 > /* add channel maps */ [Severity: Medium] Should spec->pcm_lock also cover the rest of this loop body in snd_hda_hdmi_generic_build_controls()? Just above this hunk, the loop runs: sound/hda/codecs/hdmi/hdmi.c:snd_hda_hdmi_generic_build_controls() { ... if (spec->static_pcm_mapping) { hdmi_attach_hda_pcm(spec, per_pin); hdmi_pcm_setup_pin(spec, per_pin); } pin_eld->eld_valid =3D false; ... } Because the audio component pin_eld_notify callback is already registered during codec->ops.probe(), snd_hda_hdmi_check_presence_and_report() can run concurrently with snd_hda_hdmi_generic_build_controls(). Leaving hdmi_attach_hda_pcm(), hdmi_pcm_setup_pin() and pin_eld->eld_valid =3D false outside spec->pcm_lock allows update_eld() on the notify path to race on spec->pcm_bitmap and per_pin->pcm, or to set pin_eld->eld_valid =3D true right after it is cleared here so that hdmi_present_sense() sees no ELD change and skips initial jack and control reporting (regressing ca76282b6faf ("ALSA: hda/hdmi: fix race in monitor detection during probe")). > @@ -2092,7 +2093,8 @@ int snd_hda_hdmi_generic_resume(struct hda_codec *c= odec) > =20 > for (pin_idx =3D 0; pin_idx < spec->num_pins; pin_idx++) { > struct hdmi_spec_per_pin *per_pin =3D get_pin(spec, pin_idx); > - hdmi_present_sense(per_pin, 1); > + scoped_guard(mutex, &spec->pcm_lock) > + hdmi_present_sense(per_pin, 1); > } > return 0; > } [Severity: High] Does taking spec->pcm_lock in snd_hda_hdmi_generic_resume() deadlock when hdmi_present_sense() powers up a runtime-suspended codec? Both snd_hda_hdmi_check_presence_and_report() and hdmi_repoll_eld() take spec->pcm_lock before calling hdmi_present_sense(). When the codec is in runtime suspend, hdmi_present_sense() wakes it synchronously via CLASS(snd_hda_power_pm, pm)(codec) (either directly in hdmi_present_sense_via_verbs(), or in silent_stream_enable() / silent_stream_disable() called from sync_eld_via_acomp()), which runs the codec's runtime resume callback on the same thread: snd_hda_hdmi_check_presence_and_report() guard(mutex)(&spec->pcm_lock) hdmi_present_sense() hdmi_present_sense_via_verbs() CLASS(snd_hda_power_pm, pm)(codec) ... pm_runtime_get_sync() ... snd_hda_hdmi_generic_resume() scoped_guard(mutex, &spec->pcm_lock) <-- self-deadlock As the comment in silent_stream_enable() notes, runtime power-up invokes snd_hda_hdmi_generic_resume() -> hdmi_present_sense(), so snd_hda_hdmi_generic_resume() cannot acquire a mutex that callers hold across snd_hda_power_up_pm(). --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/4336415a.5017.1a231= c1ccad.Coremail.xuhx@hust.edu.cn?part=3D1