| Message ID | 20260522095401.72915-3-phucduc.bui@gmail.com (mailing list archive) |
|---|---|
| State | New |
| Headers |
Return-Path: <linux-sunxi+bounces-23605-sunxi=pue.re@lists.linux.dev>
X-Original-To: noreply@patchwork.local
Delivered-To: noreply@patchwork.local
Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mxe881.netcup.net (Postfix) with ESMTPS id 287871C07FA
for <noreply@patchwork.local>; Fri, 22 May 2026 11:56:08 +0200 (CEST)
Authentication-Results: mxe881;
dkim=pass header.d=gmail.com;
spf=pass (sender IP is 172.234.253.10)
smtp.mailfrom=linux-sunxi+bounces-23605-noreply=patchwork.local@lists.linux.dev
smtp.helo=sea.lore.kernel.org
Received-SPF: pass (mxe881: domain of lists.linux.dev designates
172.234.253.10 as permitted sender) client-ip=172.234.253.10;
envelope-from=linux-sunxi+bounces-23605-noreply=patchwork.local@lists.linux.dev;
helo=sea.lore.kernel.org;
Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org
[100.90.174.1])
by sea.lore.kernel.org (Postfix) with ESMTP id 65572300F5D5
for <noreply@patchwork.local>; Fri, 22 May 2026 09:54:32 +0000 (UTC)
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 225FD3905E4;
Fri, 22 May 2026 09:54:32 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com
header.b="H9Jawl0d"
X-Original-To: linux-sunxi@lists.linux.dev
Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com
[209.85.210.180])
(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD0A83C3C01
for <linux-sunxi@lists.linux.dev>; Fri, 22 May 2026 09:54:30 +0000 (UTC)
Authentication-Results: smtp.subspace.kernel.org;
arc=none smtp.client-ip=209.85.210.180
ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1779443672; cv=none;
b=SGhmkxiY3Cp5/NH71NDsPncCBw9xWxHbSFKpP/7pOTIqmsZqPqfyUq8U+B9lUpUuC2c7RLQ3eW5xf1z/cdiZWi2OGJN6nkZ3xeys1CKp9L3zAwjtw3bOFLkcwvARplG0XZV37Q10dRkd3Mf4gqVWEJpTFmeUIi+KXY4jTVNewG8=
ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1779443672; c=relaxed/simple;
bh=FES/LPsuYUEIfYpCxaSWRQknC3l1jlyEQS86EeloiaE=;
h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:
MIME-Version;
b=lL/aXpZxUYiNWlVVpvXb6oXhVGh8mmqB/ipx5mxtPrMi8t8izGAVhpWXcqC5y5gmKnD3FmBZOPQr5RVniYHiBwKrCWNR7TC8pAYwgPi6/TdN/lzUxzXu3gCKJYHrDULzkPSQ35q9pJLXxRwgDyXdna35ZzJXCUxR5tJ2Khl37Ew=
ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass (p=none dis=none) header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com
header.b=H9Jawl0d; arc=none smtp.client-ip=209.85.210.180
Authentication-Results: smtp.subspace.kernel.org;
dmarc=pass (p=none dis=none) header.from=gmail.com
Authentication-Results: smtp.subspace.kernel.org;
spf=pass smtp.mailfrom=gmail.com
Received: by mail-pf1-f180.google.com with SMTP id
d2e1a72fcca58-837b39eb078so4832644b3a.2
for <linux-sunxi@lists.linux.dev>;
Fri, 22 May 2026 02:54:30 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1779443670; x=1780048470;
darn=lists.linux.dev;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:from:to:cc:subject:date
:message-id:reply-to;
bh=WsLsnv7a/QcqqVOvsqB3nlisoqKckWLxKBBgfnHU3Rw=;
b=H9Jawl0dPpJO+eyESNKlJCFsRED/P78nzrgZ9dC8Rx/9wEIz6ke1LtFWaO0gQ/ceui
BgdM5aEGSzyb1kHcyfJo2ta34GJ3ZsQDXxmGnP0422VB2oQcO2uROL8t0im5sr8fxX50
VCXg4iI9jUb+NuvHmsJWfKl35w9fGALUMGxxjKvqwkAJGArU97t6VexcejfdsDMal0fJ
hIU3ByImmbaQ2onKXOHfLNSflHVpdMzZ5hsUx/X1PysrPVQ//0sbCwsEVbJFdkKchVax
tQkwO99xRm9LBvyk80wPcm/6MW4h8mpFChfN+rGRh0ZcnjdsZ+SK71pYH621c5QK8ao1
qbRA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20251104; t=1779443670; x=1780048470;
h=content-transfer-encoding:mime-version:references:in-reply-to
:message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from
:to:cc:subject:date:message-id:reply-to;
bh=WsLsnv7a/QcqqVOvsqB3nlisoqKckWLxKBBgfnHU3Rw=;
b=TDkbaOj+8sOI9gA9SaS6xVAr4UIU3UPT2y/3YLFkTETZmI3UkW1tGZhpBz0nyJjQo9
s2BXF226K0H4BSdLBFHFiMnObNMn5lU8HqEXejbdpdbl3A1I5r3T/+LdbyCm2glL0HMK
Zk/UQ6I2lEPs5/StemPqM60e96o757fEqTq2xAYbA78vrQJyMZUyah5d1m8X2mGVRmYU
eRZhtLOOyLQzU2Se7Nyse/y8NTV9eJBTKLGeGcvf2RC1+yVfrQo8rCDyKgA0YaK8neyR
wKg7SI7QNkqBdTNqDHgP5NwNmxjnmJiEUzlh553E7VbNGjR9npdVSlmsSim/QiEwCGy5
Dlow==
X-Forwarded-Encrypted: i=1;
AFNElJ/CgU4s9Pi1wNwdMd9N8RUc/nY7VUFCoSVV/afRidguyW7WG+vC6ggrYdvtjfTKd1Ns6ET40QnwUfHYHg==@lists.linux.dev
X-Gm-Message-State: AOJu0YzQjxpXOyd1bbGkIGD3BlwHWjDvxaxaynyMtzr3rzjcDvl/syFK
MWtoHlrj1TKL5+V8WVu+N84Wbo//pnrk0maSu/pay6GF5dpoJgCohxGE
X-Gm-Gg: Acq92OFz+PUnjg9B+azZLuN1iPTr1KXWoPG75OeYX1zAPHh4ZFVA0Ir4h0GsYRtsOhX
bhF1jr3daidUCU3WQAJzjGe5gSnyVVNlWFXK68WdkA9eElXUcv7J5veXqN9t5eyncRo37H+3YG5
rzqHV36mQIyJVJzlufvS2LZGK3LUt28Ep0fjAunfTWI6trY/qynNL74fHZEQPtpYNBi77nKl/o6
AN8h7FCG7/v+zUpzmpawZOHyYqmJ4jjW+kyepKhtKAFLr30VMDUO4V1Ap513wu8x2Um0f6FCGDp
EUInpV6NQhREakGv3M8EqRU4Pi/N9Ku1YwY16HN048tbsmsMPRYX/ql+JYOonID0CHcTFVTrBtt
l6qz0E2bQb2PwYZ3Sq+Ekfpo33ShCRC6GyP5Jrze74FPqoH7SB8jOB4rkR3QeogF/ThQ1RtS1in
xdl4dEpyfJd+ZvLtKvXRccwibpp8FWAI2apIF+OPu36u54JecUlNcsrCiMl39aFUKMcQ6A
X-Received: by 2002:a05:6a00:2447:b0:835:4776:7d7b with SMTP id
d2e1a72fcca58-8415f65c44bmr3236104b3a.42.1779443669845;
Fri, 22 May 2026 02:54:29 -0700 (PDT)
Received: from phuc-desktop.. ([183.91.15.56])
by smtp.gmail.com with ESMTPSA id
d2e1a72fcca58-84164aed7c9sm1757366b3a.13.2026.05.22.02.54.26
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Fri, 22 May 2026 02:54:29 -0700 (PDT)
From: phucduc.bui@gmail.com
To: broonie@kernel.org
Cc: codekipper@gmail.com,
jernej.skrabec@gmail.com,
lgirdwood@gmail.com,
linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org,
linux-sound@vger.kernel.org,
linux-sunxi@lists.linux.dev,
nichen@iscas.ac.cn,
perex@perex.cz,
samuel@sholland.org,
tiwai@suse.com,
wens@kernel.org,
bui duc phuc <phucduc.bui@gmail.com>
Subject: [PATCH v2 2/3] ASoC: sunxi: sun4i-spdif: Resume device before
kcontrol register access
Date: Fri, 22 May 2026 16:54:00 +0700
Message-ID: <20260522095401.72915-3-phucduc.bui@gmail.com>
X-Mailer: git-send-email 2.43.0
In-Reply-To: <20260522095401.72915-1-phucduc.bui@gmail.com>
References: <20260522095401.72915-1-phucduc.bui@gmail.com>
Precedence: bulk
X-Mailing-List: linux-sunxi@lists.linux.dev
List-Id: <linux-sunxi.lists.linux.dev>
List-Subscribe: <mailto:linux-sunxi+subscribe@lists.linux.dev>
List-Unsubscribe: <mailto:linux-sunxi+unsubscribe@lists.linux.dev>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-Rspamd-Server: rspamd-worker-8404
X-Spamd-Result: default: False [-0.66 / 15.00];
BAYES_HAM(-5.50)[99.99%];
RBL_SENDERSCORE(2.00)[172.234.253.10:from];
SUSPICIOUS_RECIPS(1.50)[];
DMARC_POLICY_SOFTFAIL(1.00)[gmail.com : SPF not aligned (relaxed),
No valid DKIM,none];
R_MISSING_CHARSET(0.50)[];
MAILLIST(-0.15)[generic];
BAD_REP_POLICIES(0.10)[];
MIME_GOOD(-0.10)[text/plain];
HAS_LIST_UNSUB(-0.01)[];
FROM_NO_DN(0.00)[];
R_SPF_ALLOW(0.00)[+ip4:172.234.253.10];
RCPT_COUNT_TWELVE(0.00)[14];
DBL_BLOCKED_OPENRESOLVER(0.00)[sea.lore.kernel.org:rdns,sea.lore.kernel.org:helo];
TO_DN_SOME(0.00)[];
FUZZY_BLOCKED(0.00)[rspamd.com];
FORGED_SENDER_MAILLIST(0.00)[];
FREEMAIL_CC(0.00)[gmail.com,lists.infradead.org,vger.kernel.org,lists.linux.dev,iscas.ac.cn,perex.cz,sholland.org,suse.com,kernel.org];
TAGGED_RCPT(0.00)[];
PRECEDENCE_BULK(0.00)[];
FREEMAIL_FROM(0.00)[gmail.com];
RCVD_TLS_LAST(0.00)[];
MIME_TRACE(0.00)[0:+];
FORGED_RECIPIENTS_MAILLIST(0.00)[];
ARC_ALLOW(0.00)[subspace.kernel.org:s=arc-20240116:i=1];
MID_RHS_MATCH_FROM(0.00)[];
ASN(0.00)[asn:63949, ipnet:172.234.224.0/19, country:SG];
TAGGED_FROM(0.00)[bounces-23605-noreply=patchwork.local];
RCVD_COUNT_FIVE(0.00)[6];
FROM_NEQ_ENVFROM(0.00)[phucducbui@gmail.com,linux-sunxi@lists.linux.dev];
RCVD_VIA_SMTP_AUTH(0.00)[]
X-Rspamd-Queue-Id: 287871C07FA
X-MORS-Enabled: yes
X-MORS-DOMAIN: patchwork.local
X-MORS-HOSTING: hosting172546
X-MORS-USER: hosting172546
X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?=
|
| Series |
ASoC: sunxi: sun4i-spdif: Cleanup and runtime PM improvements
|
|
Commit Message
Bui Duc Phuc
May 22, 2026, 9:54 a.m. UTC
From: bui duc phuc <phucduc.bui@gmail.com> Accessing registers while the device is runtime-suspended may lead to invalid hardware accesses on systems where the APB bus clock is gated during runtime suspend. Ensure the device is resumed before accessing registers. Signed-off-by: bui duc phuc <phucduc.bui@gmail.com> --- sound/soc/sunxi/sun4i-spdif.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
Comments
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: Pre-existing issues: - [High] Uninitialized stack memory is copied to userspace when regmap_read() fails in sun4i_spdif_get_status(). - [High] IEC958 status bits configured via ALSA kcontrols are unconditionally overwritten by hw_params. -- commit 564921e7b2898d3b9cb384871d5d927721ed7c42 Author: bui duc phuc <phucduc.bui@gmail.com> ASoC: sunxi: sun4i-spdif: Resume device before kcontrol register access Accessing registers while the device is runtime-suspended may lead to invalid hardware accesses. Ensure the device is resumed before accessing registers. > diff --git a/sound/soc/sunxi/sun4i-spdif.c b/sound/soc/sunxi/sun4i-spdif.c > index 89eccc83a1301..f54eb14c9ed86 100644 > --- a/sound/soc/sunxi/sun4i-spdif.c > +++ b/sound/soc/sunxi/sun4i-spdif.c > @@ -428,6 +428,11 @@ static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol, > struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); > u8 *status = ucontrol->value.iec958.status; > unsigned int reg; > + int ret; > + > + ret = pm_runtime_resume_and_get(cpu_dai->dev); > + if (ret) > + return ret; > > scoped_guard(spinlock_irqsave, &host->lock) { > regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA0, ®); [Severity: High] This isn't a bug introduced by this patch, but does this code copy uninitialized stack memory to userspace if regmap_read() fails? The return value of regmap_read() isn't checked here. If it fails, reg remains unmodified, containing whatever was on the kernel stack. The code then copies it directly into status, which is part of the snd_ctl_elem_value structure returned to userspace. > status[0] = reg & 0xff; > status[1] = (reg >> 8) & 0xff; > status[2] = (reg >> 16) & 0xff; > status[3] = (reg >> 24) & 0xff; [ ... ] > @@ -453,8 +460,13 @@ static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol, > struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); > u8 *status = ucontrol->value.iec958.status; > unsigned int reg; > + int ret; > bool chg0, chg1; [ ... ] > reg = SUN4I_SPDIF_TXCFG_CHSTMODE; > if (status[0] & IEC958_AES0_NONAUDIO) > reg |= SUN4I_SPDIF_TXCFG_NONAUDIO; > > regmap_update_bits(host->regmap, SUN4I_SPDIF_TXCFG, > SUN4I_SPDIF_TXCFG_CHSTMODE | > SUN4I_SPDIF_TXCFG_NONAUDIO, reg); > } [Severity: High] This is a pre-existing issue, but is the NONAUDIO bit set here unconditionally overwritten later by sun4i_spdif_hw_params()? It looks like sun4i_spdif_hw_params() builds a new register configuration from scratch and uses regmap_write(host->regmap, SUN4I_SPDIF_TXCFG, reg_val). This would destroy the NONAUDIO bit previously configured here by the user via kcontrols, unless the stream has exactly 4 channels. Can this break standard IEC958 passthrough (AC3/DTS) which uses 2 channels and relies on the kcontrol to set the non-audio bit?
On Fri, May 22, 2026 at 12:54 PM <phucduc.bui@gmail.com> wrote: > > From: bui duc phuc <phucduc.bui@gmail.com> > > Accessing registers while the device is runtime-suspended > may lead to invalid hardware accesses on systems where the > APB bus clock is gated during runtime suspend. Did you actually reproduce the issue, or did you add the patch simply because Sashiko mentioned it? On sunxi, either it will hang the system because the bus transaction got ignored, or it won't as something else enabled the clock. And when you do add patches due to Sashiko raising an issue, please do mention it in the commit message. > Ensure the device is resumed before accessing registers. > > Signed-off-by: bui duc phuc <phucduc.bui@gmail.com> > --- > sound/soc/sunxi/sun4i-spdif.c | 14 ++++++++++++++ > 1 file changed, 14 insertions(+) > > diff --git a/sound/soc/sunxi/sun4i-spdif.c b/sound/soc/sunxi/sun4i-spdif.c > index 89eccc83a130..f54eb14c9ed8 100644 > --- a/sound/soc/sunxi/sun4i-spdif.c > +++ b/sound/soc/sunxi/sun4i-spdif.c > @@ -428,6 +428,11 @@ static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol, > struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); > u8 *status = ucontrol->value.iec958.status; > unsigned int reg; > + int ret; > + > + ret = pm_runtime_resume_and_get(cpu_dai->dev); > + if (ret) > + return ret; > > scoped_guard(spinlock_irqsave, &host->lock) { > regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA0, ®); > @@ -443,6 +448,8 @@ static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol, > status[5] = (reg >> 8) & 0x3; > } > > + pm_runtime_put(cpu_dai->dev); > + > return 0; > } > > @@ -453,8 +460,13 @@ static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol, > struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); > u8 *status = ucontrol->value.iec958.status; > unsigned int reg; > + int ret; > bool chg0, chg1; > > + ret = pm_runtime_resume_and_get(cpu_dai->dev); > + if (ret) > + return ret; > + > scoped_guard(spinlock_irqsave, &host->lock) { > reg = (u32)status[3] << 24; > reg |= (u32)status[2] << 16; > @@ -479,6 +491,8 @@ static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol, > SUN4I_SPDIF_TXCFG_NONAUDIO, reg); > } > > + pm_runtime_put(cpu_dai->dev); > + > return chg0 || chg1; > } > > -- > 2.43.0 >
Hi Chen-Yu, On Sat, May 23, 2026 at 2:19 AM Chen-Yu Tsai <wens@kernel.org> wrote: > And when you do add patches due to Sashiko raising an issue, please > do mention it in the commit message. > As mentioned in the v1 discussion , this issue was originally reported by Sashiko. I'll add the Reported-by tag in the next revision. v1 links: https://lore.kernel.org/all/20260513105003.81880-1-phucduc.bui@gmail.com/ > Did you actually reproduce the issue, or did you add the patch simply > because Sashiko mentioned it? > Since I lack Sunxi hardware, I couldn't reproduce it or perform runtime testing. But I did compile-test the patch. The patch aims to fix unsafe register accesses that occur before ensuring the device is runtime-resumed. > On sunxi, either it will hang the system because the bus transaction > got ignored, or it won't as something else enabled the clock. > If Sunxi's PM design already guarantees safe access here, feel free to reject the patch. Best Regards, Phuc
On Sat, May 23, 2026 at 4:55 PM Bui Duc Phuc <phucduc.bui@gmail.com> wrote: > > Hi Chen-Yu, > > On Sat, May 23, 2026 at 2:19 AM Chen-Yu Tsai <wens@kernel.org> wrote: > > And when you do add patches due to Sashiko raising an issue, please > > do mention it in the commit message. > > > > As mentioned in the v1 discussion , this issue was originally reported > by Sashiko. > I'll add the Reported-by tag in the next revision. > v1 links: > https://lore.kernel.org/all/20260513105003.81880-1-phucduc.bui@gmail.com/ > > > Did you actually reproduce the issue, or did you add the patch simply > > because Sashiko mentioned it? > > > Since I lack Sunxi hardware, I couldn't reproduce it or perform runtime testing. > But I did compile-test the patch. > The patch aims to fix unsafe register accesses that occur before ensuring the > device is runtime-resumed. When you submit a patch, it is expected that you already tested it. If you only compile tested it, please remember to say so in the footer (or mark the patch as RFT) so that others can test for you and the maintainer knows the status. And if possible, provide a scheme to test it. > > On sunxi, either it will hang the system because the bus transaction > > got ignored, or it won't as something else enabled the clock. > > > > If Sunxi's PM design already guarantees safe access here, > feel free to reject the patch. I can't say that it does. But since the only control that SPDIF gives is the IEC958 status, and that doesn't appear in the standard mixer apps, it's unlikely that a _user_ will trigger it. Plus the control was added after the basic structure of the driver was done, so there is definitely some possibility of a crash. But what you wrote in the commit message doesn't match the actual hardware behavior, like I wrote. ChenYu
Hi Chenyu, > When you submit a patch, it is expected that you already tested it. > If you only compile tested it, please remember to say so in the > footer (or mark the patch as RFT) so that others can test for you > and the maintainer knows the status. > > And if possible, provide a scheme to test it. > Thanks for the guidance. I’ll clearly mention the test status next time. > > I can't say that it does. But since the only control that SPDIF gives > is the IEC958 status, and that doesn't appear in the standard mixer apps, > it's unlikely that a _user_ will trigger it. Plus the control was added > after the basic structure of the driver was done, so there is definitely > some possibility of a crash. > > But what you wrote in the commit message doesn't match the actual hardware > behavior, like I wrote. Thanks for the clarification. I'll update the commit message to something like: "The kcontrols may access hardware registers while the device is runtime-suspended. Ensure the device is resumed before touching the registers." Best Regards, Phuc
Hi ,
I did some additional investigation into the regmap core functions
currently used by this driver, such as _regmap_update_bits():
https://elixir.bootlin.com/linux/v7.1-rc5/source/drivers/base/regmap/regmap.c#L3249
and _regmap_read():
https://elixir.bootlin.com/linux/v7.1-rc5/source/drivers/base/regmap/regmap.c#L2822
as well as the current sun4i_spdif_regmap_config definition:
https://elixir.bootlin.com/linux/v7.1-rc5/source/sound/soc/sunxi/sun4i-spdif.c#L526
Based on the current regmap core implementation, I think it might be
cleaner to leverage the existing regmap suspend protection mechanism:
if (map->cache_only)
return -EBUSY;
This would inherently prevent direct hardware register access and help
avoid potential crashes during suspend. To support this properly, we would
likely also need to enable a basic cache type (e.g. REGCACHE_FLAT) in
sun4i_spdif_regmap_config, since the driver currently
does not define a .cache_type.
A patch in this direction would look roughly like this:
static int sun4i_spdif_runtime_suspend(struct device *dev)
{
struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
clk_disable_unprepare(host->spdif_clk);
+regcache_cache_only(host->regmap, true);
clk_disable_unprepare(host->apb_clk);
}
static int sun4i_spdif_runtime_resume(struct device *dev)
{
struct sun4i_spdif_dev *host = dev_get_drvdata(dev);
clk_prepare_enable(host->apb_clk);
+ regcache_cache_only(host->regmap, false);
clk_prepare_enable(host->spdif_clk);
}
This approach would not only address the current kcontrol-related issue,
but could also provide a more generic safeguard against other runtime
suspend race conditions that we may not have considered yet.
However, one downside is that this solution depends on the current internal
behavior of the regmap core. In addition, if this driver starts using
.volatile_reg in the future, the current regmap implementation may no
longer provide sufficient protection during suspend.
For example, in the first branch of _regmap_update_bits(),
volatile register accesses bypass the cache_only check entirely and can
still hit the hardware bus directly, which could still lead to system crashes.
Because of that, the explicit pm_runtime resume handling proposed
in the current patch may still be the more robust solution in the long term.
Given our previous discussion about making the pm_runtime handling
more consistent between I2S and SPDIF:
https://lore.kernel.org/all/CAABR9nEFGOX5GnQ9qpJY-T-92dA_kDcVS+qBz1ior590G_x6gw@mail.gmail.com/
What are your thoughts on this direction?
Or would it be safer and simpler to keep the current patch as-is?
Best Regards,
Phuc
diff --git a/sound/soc/sunxi/sun4i-spdif.c b/sound/soc/sunxi/sun4i-spdif.c index 89eccc83a130..f54eb14c9ed8 100644 --- a/sound/soc/sunxi/sun4i-spdif.c +++ b/sound/soc/sunxi/sun4i-spdif.c @@ -428,6 +428,11 @@ static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol, struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); u8 *status = ucontrol->value.iec958.status; unsigned int reg; + int ret; + + ret = pm_runtime_resume_and_get(cpu_dai->dev); + if (ret) + return ret; scoped_guard(spinlock_irqsave, &host->lock) { regmap_read(host->regmap, SUN4I_SPDIF_TXCHSTA0, ®); @@ -443,6 +448,8 @@ static int sun4i_spdif_get_status(struct snd_kcontrol *kcontrol, status[5] = (reg >> 8) & 0x3; } + pm_runtime_put(cpu_dai->dev); + return 0; } @@ -453,8 +460,13 @@ static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol, struct sun4i_spdif_dev *host = snd_soc_dai_get_drvdata(cpu_dai); u8 *status = ucontrol->value.iec958.status; unsigned int reg; + int ret; bool chg0, chg1; + ret = pm_runtime_resume_and_get(cpu_dai->dev); + if (ret) + return ret; + scoped_guard(spinlock_irqsave, &host->lock) { reg = (u32)status[3] << 24; reg |= (u32)status[2] << 16; @@ -479,6 +491,8 @@ static int sun4i_spdif_set_status(struct snd_kcontrol *kcontrol, SUN4I_SPDIF_TXCFG_NONAUDIO, reg); } + pm_runtime_put(cpu_dai->dev); + return chg0 || chg1; }