diff mbox series

ASoC: codecs: Fix atomicity violation in snd_soc_component_get_drvdata()

Message ID 20240930101216.23723-1-chenqiuji666@gmail.com (mailing list archive)
State New, archived
Headers show
Series ASoC: codecs: Fix atomicity violation in snd_soc_component_get_drvdata() | expand

Commit Message

Qiu-ji Chen Sept. 30, 2024, 10:12 a.m. UTC
An atomicity violation occurs when the validity of the variables 
da7219->clk_src and da7219->mclk_rate is being assessed. Since the entire 
assessment is not protected by a lock, the da7219 variable might still be 
in flux during the assessment, rendering this check invalid.

To fix this issue, we recommend adding a lock before the block 
if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq)) so that 
the legitimacy check for da7219->clk_src and da7219->mclk_rate is 
protected by the lock, ensuring the validity of the check.

This possible bug is found by an experimental static analysis tool
developed by our team. This tool analyzes the locking APIs
to extract function pairs that can be concurrently executed, and then
analyzes the instructions in the paired functions to identify possible
concurrency bugs including data races and atomicity violations.

Fixes: 6d817c0e9fd7 ("ASoC: codecs: Add da7219 codec driver")
Cc: stable@vger.kernel.org
Signed-off-by: Qiu-ji Chen <chenqiuji666@gmail.com>
---
 sound/soc/codecs/da7219.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

Comments

Mark Brown Nov. 5, 2024, 4:38 p.m. UTC | #1
On Mon, 30 Sep 2024 18:12:16 +0800, Qiu-ji Chen wrote:
> An atomicity violation occurs when the validity of the variables
> da7219->clk_src and da7219->mclk_rate is being assessed. Since the entire
> assessment is not protected by a lock, the da7219 variable might still be
> in flux during the assessment, rendering this check invalid.
> 
> To fix this issue, we recommend adding a lock before the block
> if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq)) so that
> the legitimacy check for da7219->clk_src and da7219->mclk_rate is
> protected by the lock, ensuring the validity of the check.
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/1] ASoC: codecs: Fix atomicity violation in snd_soc_component_get_drvdata()
      commit: 1157733344651ca505e259d6554591ff156922fa

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark
diff mbox series

Patch

diff --git a/sound/soc/codecs/da7219.c b/sound/soc/codecs/da7219.c
index 311ea7918b31..e2da3e317b5a 100644
--- a/sound/soc/codecs/da7219.c
+++ b/sound/soc/codecs/da7219.c
@@ -1167,17 +1167,20 @@  static int da7219_set_dai_sysclk(struct snd_soc_dai *codec_dai,
 	struct da7219_priv *da7219 = snd_soc_component_get_drvdata(component);
 	int ret = 0;
 
-	if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq))
+	mutex_lock(&da7219->pll_lock);
+
+	if ((da7219->clk_src == clk_id) && (da7219->mclk_rate == freq)) {
+		mutex_unlock(&da7219->pll_lock);
 		return 0;
+	}
 
 	if ((freq < 2000000) || (freq > 54000000)) {
+		mutex_unlock(&da7219->pll_lock);
 		dev_err(codec_dai->dev, "Unsupported MCLK value %d\n",
 			freq);
 		return -EINVAL;
 	}
 
-	mutex_lock(&da7219->pll_lock);
-
 	switch (clk_id) {
 	case DA7219_CLKSRC_MCLK_SQR:
 		snd_soc_component_update_bits(component, DA7219_PLL_CTRL,