Return-Path: Received: from mail-bn3nam01on0128.outbound.protection.outlook.com ([104.47.33.128]:55858 "EHLO NAM01-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753578AbeA1W1z (ORCPT ); Sun, 28 Jan 2018 17:27:55 -0500 From: Sasha Levin To: "linux-kernel@vger.kernel.org" , "stable@vger.kernel.org" CC: Jiada Wang , Mark Brown , "Sasha Levin" Subject: [PATCH AUTOSEL for 4.14 089/100] ASoC: rsnd: ssi: fix race condition in rsnd_ssi_pointer_update Date: Sun, 28 Jan 2018 22:26:57 +0000 Message-ID: <20180128222547.7398-89-alexander.levin@microsoft.com> References: <20180128222547.7398-1-alexander.levin@microsoft.com> In-Reply-To: <20180128222547.7398-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Jiada Wang [ Upstream commit 33f801366bdf3f8b67dfe325b84f4051a090d01e ] Currently there is race condition between set of byte_pos and wrap it around when new buffer starts. If .pointer is called in-between it will result in inconsistent pointer position be returned from .pointer callback. This patch increments buffer pointer atomically to avoid this issue. Signed-off-by: Jiada Wang Reviewed-by: Takashi Sakamoto Acked-by: Kuninori Morimoto Signed-off-by: Mark Brown Signed-off-by: Sasha Levin --- sound/soc/sh/rcar/ssi.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/sound/soc/sh/rcar/ssi.c b/sound/soc/sh/rcar/ssi.c index 2aef7c00cca1..f0fb85fda42d 100644 --- a/sound/soc/sh/rcar/ssi.c +++ b/sound/soc/sh/rcar/ssi.c @@ -449,25 +449,29 @@ static bool rsnd_ssi_pointer_update(struct rsnd_mod *= mod, int byte) { struct rsnd_ssi *ssi =3D rsnd_mod_to_ssi(mod); + bool ret =3D false; + int byte_pos; =20 - ssi->byte_pos +=3D byte; + byte_pos =3D ssi->byte_pos + byte; =20 - if (ssi->byte_pos >=3D ssi->next_period_byte) { + if (byte_pos >=3D ssi->next_period_byte) { struct snd_pcm_runtime *runtime =3D rsnd_io_to_runtime(io); =20 ssi->period_pos++; ssi->next_period_byte +=3D ssi->byte_per_period; =20 if (ssi->period_pos >=3D runtime->periods) { - ssi->byte_pos =3D 0; + byte_pos =3D 0; ssi->period_pos =3D 0; ssi->next_period_byte =3D ssi->byte_per_period; } =20 - return true; + ret =3D true; } =20 - return false; + WRITE_ONCE(ssi->byte_pos, byte_pos); + + return ret; } =20 /* @@ -838,7 +842,7 @@ static int rsnd_ssi_pointer(struct rsnd_mod *mod, struct rsnd_ssi *ssi =3D rsnd_mod_to_ssi(mod); struct snd_pcm_runtime *runtime =3D rsnd_io_to_runtime(io); =20 - *pointer =3D bytes_to_frames(runtime, ssi->byte_pos); + *pointer =3D bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos)); =20 return 0; } --=20 2.11.0