From patchwork Fri May 13 09:38:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572959 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE21AC433EF for ; Fri, 13 May 2022 09:39:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346611AbiEMJjU (ORCPT ); Fri, 13 May 2022 05:39:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379100AbiEMJjO (ORCPT ); Fri, 13 May 2022 05:39:14 -0400 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB4DC2A7697 for ; Fri, 13 May 2022 02:39:11 -0700 (PDT) Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D995TX031470; Fri, 13 May 2022 09:39:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=hAlaZ1Ec3j09fxN+1irCKE9kEKdJiJKmKAtlmNxklJw=; b=sX5xQEYDKU1Fl9MkpoAah8fQHjF+/w3Fu8lpPcDvtjwjDw9WNlE94zZm61iVCsxCg5lC oRGk9DQCYvR00BiMtmA5gzoIkYb/Uag9tKtLgEEHDfzOBFlGqlVS3wQ2bX2PPNu0VFIr n1UX1WwLfWJG7oitwjorAq1MccGA5c/Rhif7r6LZImjiPF11H9lVDKVOCrgfYhlp68Cl Ei29a6/F63oBNFEdeW20gN5dWlaF+XfdmazFRrfxAmqXvTBc93D9XfKZwnwZL1jAObqk uuNExMAQud5UVcIql3rMIHuO/y8RA1U9quyhURqAG/3NqgxRpu5wHmgrmVo9bC5uIn5G QA== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2042.outbound.protection.outlook.com [104.47.73.42]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwdyyvq41-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 09:39:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ST7/gRoKWTZBMpnOy/bTdpv/TyoPH6gH+ADkm4LIvwh2gFuk5Dm99BcVSqkshTqlFRd+OI4gSOs62dPcnzLmNdfpofVTUxWHL1hHvBscyz4XUi7pAr3Ten4O6ff1LGv3v4JcvozZy/ZKXQKFFEwbGL1Mzd3+JWjq/WF6vybYXRg5E5pnjAE9kNilNTsgeOYcNMuw8FeNYO5tNwAY43bh0CSSjLYv1mMsGnkmYpmKN87brzzxh4XTCo1bqgRa6TO5Jky/Uts8+bYq4FwYPW9oHUW1Gb9rBeV5O4MjKJiL54EmgveLI1mEUQt6GxJs2mVtIJF0W6443tIR1xL6SLM1dQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=hAlaZ1Ec3j09fxN+1irCKE9kEKdJiJKmKAtlmNxklJw=; b=AK4NZa4r+t0WOozriqvhArByrsoPFbkPhCK4AXgd8bW8MsMw3kqQNp3luZkEGf0bsp7I1G2/XiD8BCxJj4guJQbBFUeKHH2HUPyeCa6Xu+FbAsPFEnkt0ib4JfOQOxAofOSj2DLv3DeoxZgMoivcSSVdq8a2AB4FWx8Z711yilD2EEl1o7B/ar9xRRYG2VKhwoptuXkDH1sEog3exKnIuPJ2OcyH3u2RWHejnNv6gcGyYnF+aL9TuokvK4R0DIY/ungILt5ZWNHbeU6RWdm5G/Ie5ZnJ5eUfD6DKtafyKvbFEerddqKPq4CeHUqzafJMfgdKQIGM5uO8mjGocO+rRw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) by SA0PR11MB4527.namprd11.prod.outlook.com (2603:10b6:806:72::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Fri, 13 May 2022 09:38:58 +0000 Received: from DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842]) by DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842%5]) with mapi id 15.20.5250.015; Fri, 13 May 2022 09:38:58 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Hu Jiahui , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.14 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Date: Fri, 13 May 2022 12:38:28 +0300 Message-Id: <20220513093832.1434697-1-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 X-ClientProxiedBy: VI1PR02CA0047.eurprd02.prod.outlook.com (2603:10a6:802:14::18) To DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7ef3e6d7-1674-4594-e9a3-08da34c46763 X-MS-TrafficTypeDiagnostic: SA0PR11MB4527:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9OQTrquKOh84U3meuxl3UDUtNQ8cSLQddhvi7zUVKdWWoIKMGQmqH/H7cP2N4/RtM4atCLkhyyBIoLgX9J3myay8V+b5+L34I7ggWM92g5WBEfm3bnMzH3j2j8qKdWi6Qp3gr2KhMqfBLfCCskQbOOD90mM74gj9l+58gNhBeGu3YSzxjveCGceUykEKaSTCNpp7mMmsqO9A4SLX1JFA3h4laUDLE8KU6AiKzQBYe4+kUjQzNkVPBAexv8cVq0dPJxsliYW3unsyEP6MxVZPCjTEWd/KVRCBDZhnNBSHFGUO8XlsjyEKYuRBo6M/El++co0XgmDeT6BLw0h1gAquPRFS/aLITmT9xfO94aS8ATsWi2m1IDRG1btbhjw3cBiiU8oNFh3eE7JqooUfsMxSJOwuT7TyQKIXj1ZZ79PXRBwjOHmTpIc4DY+wKPJFvnx3zxBM0WIUzNbYat3VUeGoENy5bM1rkTKUVmPVn4kEZ+44yGZCKcG5GxdfuAS6qpMAAuEApMVrCRfqSLfSYKx83Ba+Um5rKCFViMa4zGqaMuP2Jk5Mu4qDRJIkyqhYBaz16mBzK/TeIUZ2rZSQsOs6QuyyKmW+yY6TTq7GhwQj8VP0JZ1wUTbLPnXJI9BDkVhUpU7PgQmWOks8Y+U+yq7T2nFIk7hSo7Dwg0Y2jC+hNxGoqMO4RTJqYM/mk5Rz2LmbmIOK7i2kFIhHFl8VPAtsEGxsHJQgyKPiOOnZV0X6xoqAh3+VgGujW3bPlD4br1Ee576Y1QCXTzAHPmC1swJFvLxVbjQwUZU6Q0CADm5ts0U= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5327.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(38350700002)(316002)(66476007)(8676002)(4326008)(54906003)(6916009)(8936002)(66946007)(6512007)(26005)(1076003)(2616005)(107886003)(6506007)(52116002)(6486002)(508600001)(6666004)(186003)(966005)(86362001)(38100700002)(36756003)(5660300002)(44832011)(2906002)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: phogzH6SQjtuk3iPpgx5uYF5++fakDBGvDptKZiLSVCzqQIHCLDA+JNd9x5ikzJqfAlhVP5H57dN2lKDockcdKKpatp4E0wCCtxvloydQBlPRH4T8bay179qEoERCDC6AxedPswmi5SIKNOxAfU9QXIPOehxwFsN8Ms+8DYCbqwtOUQrH/MSbHb5Mh0ySyZbbBh1FrSaZZzj5Kki+cn33qKltFr0EfQPlu/oR+VHLdrFUQUgHvV0MJLX1YAkkG9z/+r+6Cz3qpOIBEFp4qSkCcX4/QN8+ZIESp/7BFTvoj5RGNg0WIMBpikyjenlJ7HQVhf2SBWpISLXyY1lALF7cx24p/F+Lwl+/72XR//qqFLenUdDSgPCqIa10s5pzgkkYPLi6bRClYvaKaDWK9407jXkhoq3ojWaxU071Y2gI7OkUTyeYJWXAC8hs07hPocl3eNwKJ8i7drNG/LrS3PuMOCizJGurmwhweAN0rzrYoPiVWkTpGe4ZxPGuT1IBEH1x6Uv9UQvlLKgS9XQ6h0jnAcg3O9vBTYvqwwEqcp7GzRJD9LiBf7CxVnr3phnOJ+b9+uG10nPXqkwIPDAOAJhFf0Ada6hptDKa0xfhSd7F4IS6EKLbscroeYGirBowIcQRiUGfZ5qcJt/Ehwd1rGXkQ/UdFGMdYxPM7X8w7VFTIq8eOzoac2Mo7DDAKTAIakJ8NLcV3DAQEs5LQkHskt7wGgFimp6mL9HRvbHUJKly9YevjRsWmrcw0IA4k2epNjeLgf3FQDVrUjNVArDiRq8rEmrjpqxkvyZ21W1jS8eLtMKtSgwh1xd2le0jJxaHA0yCQAPmPTYY67ocHUFBsIT2yjYwFjMSt7zgUcn8emFGteoibgcseLoHiCJMtaEIp5Jxd2H3mCsvw7EqBNsXg98b5KftlueY7pkux26bknUU4ciwR+k+Ls0wQvUknKv7BGkDILdyrIaKqh8WgUQ5Zt+gYirMIWhp7z+DRpagJB047S0C2Nk4yjCvFneqoXrPMh55bBdaF1oIjqKoKt7iFxmwOmQ6lWihz+erc9c3zqQKhbuz4Cw1wswLybxz2odUfeIBqCEa3GV14TlUDoWioU+ScZBnawYxQZVbxRnQ23te/RkKM3XJGcl+hHkk25ZMRuWBKXKAzCXUMVA/DCwJc8tayfgCKDu3E0zmQuDWt0HPGmKLzaTcEF8oCJrTQ+HnB7JaYaJrD9ocemuoqDrYig32cePUB2zhG0YSTgcChu+p2LQjLQvNGM5Hle1eP8Vwlhkh83DWRl+UpJl/h/TXrcHJhg9hpjLIVEmLfNU4k1vg6yen1I1Ukh9qHx0OZOR9UBBRpFBNhpuoFgJYm2dKOd49iQBYpyUkoWgE1xxrlBhraCzUyeArn8lcPtRfwAWkB4GlovTdRu4Vc/d0sD8AwExR9xhGWqRvUiPbWnk4JMaVclW58c4HhXoEfI9JQUBljGvApluoDlaGNWmyaDmhyTjeNWENvDJW9S1pX44WRBiZXCrJeO1JSbICDzugFfEVs+NlBQanIMzIUqFo+wJwumfXDQgf1HK4/Ro4VQ6xcYrxW2oLF7jZzT6Jy8S7+YnkNXC4cTs08YQHuiyXiid1PAmD1qOhRyDjvsONYtqAXosJn3E+VyEN0I4GdqYAqei1Ggeg7IrT0cby/cM6DdpC9+MZBQ7diRkzZjoox5mekwfetQllUowJuqhsSqXv7vWjDlSx6wLEJ80BSSzACyfCKMc7muNS85UCrGWOTbPTzmzSaA= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7ef3e6d7-1674-4594-e9a3-08da34c46763 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 09:38:58.1212 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: LOCFTLBlmDffgGv6mRJoSLkFzsy/a81hOIHmtXIPGl4vJj8qvVqM8kYrrpVRiCdzJox7uVWNip5lECQseptWNF79y+QQ8j5RyZBy0SLPySs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4527 X-Proofpoint-GUID: iLkHpBGzyLboWLU-EtbvQ8JOo1Eq47Aa X-Proofpoint-ORIG-GUID: iLkHpBGzyLboWLU-EtbvQ8JOo1Eq47Aa X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-13_04,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 spamscore=0 impostorscore=0 clxscore=1015 mlxlogscore=911 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130041 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Takashi Iwai commit 92ee3c60ec9fe64404dc035e7c41277d74aa26cb upstream. Currently we have neither proper check nor protection against the concurrent calls of PCM hw_params and hw_free ioctls, which may result in a UAF. Since the existing PCM stream lock can't be used for protecting the whole ioctl operations, we need a new mutex to protect those racy calls. This patch introduced a new mutex, runtime->buffer_mutex, and applies it to both hw_params and hw_free ioctl code paths. Along with it, the both functions are slightly modified (the mmap_count check is moved into the state-check block) for code simplicity. Reported-by: Hu Jiahui Cc: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-2-tiwai@suse.de Signed-off-by: Takashi Iwai [OP: backport to 4.14: adjusted context] Signed-off-by: Ovidiu Panait --- include/sound/pcm.h | 1 + sound/core/pcm.c | 2 ++ sound/core/pcm_native.c | 55 +++++++++++++++++++++++++++-------------- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/include/sound/pcm.h b/include/sound/pcm.h index 24febf9e177c..aff851485b4a 100644 --- a/include/sound/pcm.h +++ b/include/sound/pcm.h @@ -396,6 +396,7 @@ struct snd_pcm_runtime { wait_queue_head_t sleep; /* poll sleep */ wait_queue_head_t tsleep; /* transfer sleep */ struct fasync_struct *fasync; + struct mutex buffer_mutex; /* protect for buffer changes */ /* -- private section -- */ void *private_data; diff --git a/sound/core/pcm.c b/sound/core/pcm.c index a228bf933110..c62240d0f5b7 100644 --- a/sound/core/pcm.c +++ b/sound/core/pcm.c @@ -1032,6 +1032,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, init_waitqueue_head(&runtime->tsleep); runtime->status->state = SNDRV_PCM_STATE_OPEN; + mutex_init(&runtime->buffer_mutex); substream->runtime = runtime; substream->private_data = pcm->private_data; @@ -1063,6 +1064,7 @@ void snd_pcm_detach_substream(struct snd_pcm_substream *substream) substream->runtime = NULL; if (substream->timer) spin_unlock_irq(&substream->timer->lock); + mutex_destroy(&runtime->buffer_mutex); kfree(runtime); put_pid(substream->pid); substream->pid = NULL; diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index c530d008fe01..b4ba16e31e5c 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -634,33 +634,40 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, return 0; } +#if IS_ENABLED(CONFIG_SND_PCM_OSS) +#define is_oss_stream(substream) ((substream)->oss.oss) +#else +#define is_oss_stream(substream) false +#endif + static int snd_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { struct snd_pcm_runtime *runtime; - int err, usecs; + int err = 0, usecs; unsigned int bits; snd_pcm_uframes_t frames; if (PCM_RUNTIME_CHECK(substream)) return -ENXIO; runtime = substream->runtime; + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); switch (runtime->status->state) { case SNDRV_PCM_STATE_OPEN: case SNDRV_PCM_STATE_SETUP: case SNDRV_PCM_STATE_PREPARED: + if (!is_oss_stream(substream) && + atomic_read(&substream->mmap_count)) + err = -EBADFD; break; default: - snd_pcm_stream_unlock_irq(substream); - return -EBADFD; + err = -EBADFD; + break; } snd_pcm_stream_unlock_irq(substream); -#if IS_ENABLED(CONFIG_SND_PCM_OSS) - if (!substream->oss.oss) -#endif - if (atomic_read(&substream->mmap_count)) - return -EBADFD; + if (err) + goto unlock; params->rmask = ~0U; err = snd_pcm_hw_refine(substream, params); @@ -737,14 +744,19 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream, if ((usecs = period_to_usecs(runtime)) >= 0) pm_qos_add_request(&substream->latency_pm_qos_req, PM_QOS_CPU_DMA_LATENCY, usecs); - return 0; + err = 0; _error: - /* hardware might be unusable from this time, - so we force application to retry to set - the correct hardware parameter settings */ - snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); - if (substream->ops->hw_free != NULL) - substream->ops->hw_free(substream); + if (err) { + /* hardware might be unusable from this time, + * so we force application to retry to set + * the correct hardware parameter settings + */ + snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); + if (substream->ops->hw_free != NULL) + substream->ops->hw_free(substream); + } + unlock: + mutex_unlock(&runtime->buffer_mutex); return err; } @@ -777,22 +789,27 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream) if (PCM_RUNTIME_CHECK(substream)) return -ENXIO; runtime = substream->runtime; + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); switch (runtime->status->state) { case SNDRV_PCM_STATE_SETUP: case SNDRV_PCM_STATE_PREPARED: + if (atomic_read(&substream->mmap_count)) + result = -EBADFD; break; default: - snd_pcm_stream_unlock_irq(substream); - return -EBADFD; + result = -EBADFD; + break; } snd_pcm_stream_unlock_irq(substream); - if (atomic_read(&substream->mmap_count)) - return -EBADFD; + if (result) + goto unlock; if (substream->ops->hw_free) result = substream->ops->hw_free(substream); snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); pm_qos_remove_request(&substream->latency_pm_qos_req); + unlock: + mutex_unlock(&runtime->buffer_mutex); return result; } From patchwork Fri May 13 09:38:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572958 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BC2ABC433EF for ; Fri, 13 May 2022 09:39:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379071AbiEMJjl (ORCPT ); Fri, 13 May 2022 05:39:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353324AbiEMJjk (ORCPT ); Fri, 13 May 2022 05:39:40 -0400 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E283F291CDC for ; Fri, 13 May 2022 02:39:39 -0700 (PDT) Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D995TW031470; Fri, 13 May 2022 09:39:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=kdrKI17HIpBtIJjKWY0f6q/o7sWErb2cowe9LGu9AeU=; b=YSS0th4WA2qMCVvkseBbnreWpTbBgng81jrBf0Vhc1N9CprAGy8ERfu6eYDnmk6VlQ/N 7Gp7uQm0zDZeFpZ1Ggb/ZHcjBlenUTpKyPSG9Utlu1SrBRIfo5l9qUHV+7w7KYeWDi/k oFn2hNh69ls+GbyNhy45xiRzFr3Kdt/38OuQjkYSQ4AjTSgI3Pj2FOjur5tRrVs8zPYk U8B1dn9AoO3d1JvPRhtJjS8EtXZGfVUc0299nZ+XD7IgVZHUFkTl72qPrNWWp3CchO15 XkpFePLVxcCjNy8lv3Bt85JW8AiCXF7oCNGm0fYoJvIuLJ58CMz5OJxWImexafTBSPBF 6A== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2042.outbound.protection.outlook.com [104.47.73.42]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwdyyvq41-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 09:39:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hcg6AhukSuBXHUlEzydjtJoc01rLvhbQpBFlQtaTXraE67qUEJMntlHCK7mCFl7hsO8ejdJlvq01b2268I6vXjbuvlbQTjl7aqOo6TYOPKWIhWLTLsAflvJ9pbPhZwW9m1+6kBekq5z1ta4UnIDp0JtddWzKky/xrMfCTHqCT46SiYhA9C/80B6ocmplFlvDoPI9y4Nu/X5BNn5vW3X129AUyTG2nbDOoy2eCLHQG9wKgjSF3WZWaseOfBH2N9XEhq03UyObzWEewbnzGOptKovPHm4ezus8v5FR2ZxCxUCFn1NukEoilv3QqvHb3oNKPx/y11/I6tDXSs+yc0v7bw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kdrKI17HIpBtIJjKWY0f6q/o7sWErb2cowe9LGu9AeU=; b=WzbrARfOtNNJ118edmMIKiMrJeB7hc6u7yKwvOmrtxt566uhUi4fvQRZUohwwcPih2iqR5oz1UVyDAaOO5SCBhBghJxKY790bdepbYY5Sl2Ig1Jvpq4TQmxkk9jtP7P4rL8dJsf+5EAgqjWA9sGJFhRehQKPrxqMMbGsXvqJGd/nBzkN+lGrkjlvAnYbC1QX4GH7oZjj9zW8zrMhbhjT/e7PjktkT4CD2cO5o2w1Kq65mPCJDrAxVeqhyPcm0gP/nZkVZFVH89ULc0X5aSOECOD6kn07ogfg141MxHx/9rgm/b5tFnw7qlSr0flYprmCjU5b6VIbm5edMyno1gre2g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) by SA0PR11MB4527.namprd11.prod.outlook.com (2603:10b6:806:72::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Fri, 13 May 2022 09:38:59 +0000 Received: from DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842]) by DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842%5]) with mapi id 15.20.5250.015; Fri, 13 May 2022 09:38:59 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.14 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 13 May 2022 12:38:29 +0300 Message-Id: <20220513093832.1434697-2-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513093832.1434697-1-ovidiu.panait@windriver.com> References: <20220513093832.1434697-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR02CA0047.eurprd02.prod.outlook.com (2603:10a6:802:14::18) To DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 29f54305-c4c0-4b7c-f1fe-08da34c46837 X-MS-TrafficTypeDiagnostic: SA0PR11MB4527:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: g6ICp52GF09e+8W5yUAt3hYA8oUo7FAh62PtR0Hp4ETNWS05RRZMMs5jNq5txuKATBrtiqzU8MCTXsahzwRQQ0Yw69edrGjiMkS4jQrkHiTY8DBIy9AWjPj4yML4DJ11PdIAnTfKqYRRNgINwR+v+x5dmZlepV5VFCjTa/Vq+ZXWqQ93JVJdX5z+KFv1yvPIMArnYBdt6VCHjr8dFtLtMV9+HUsa38/egl2762Lk18edWdpKNUN7yfYtafkhcfVtXERj2Y9QL/uJ8bqBorJ7RKf+/Hg5dm7EviKYeuxm0unieprY1li3FGf4i87GYuxPiz9DJTo5xJ87bC2nItSuK0+SRkjVIMdNGkt30Gld0Qq/yQSVmDo1nDVrj/8/EY9xB4xs7D7Ji6UBQBfGuM0fGeEscgYoLzIyjp/XiaU1xdiKhWOMk83emqUOFbNgvZeouR5Ig5HPSJAqx9/OqznQV3MkbpHahxdh6sZIr76Dl4qRbzV3ujqb5ymYoADxAfJZIxeVApD3NV+0CidT57okOOov56TWLJBFwO/FVKqWabWKxQqmmqdEpdzPy7mUfQ7bjQ1+qlWtbAzU+veDbyS+U5Om6xxCR453XC5H3fak81IeijYrJ9yOgchZlLV8xB6zpIjyVYfVI+eAIhqP7BgtzOCQ9jaK0bNUwBhgaFFxlettoc5lFkJt94r96489jsfBmb/8Q2xTheUOfNKbjdoNMqlOfJ8gY28zy25vuq14KFyqHWkx9xRXt8/u3jQZyDIctYtapV+FhWPi04ld6VFMFiyz+vGcGS7o/2n9jrioAM0= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5327.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(38350700002)(316002)(66476007)(8676002)(4326008)(54906003)(6916009)(8936002)(66946007)(6512007)(26005)(1076003)(2616005)(107886003)(6506007)(52116002)(6486002)(508600001)(6666004)(186003)(966005)(86362001)(38100700002)(36756003)(5660300002)(44832011)(2906002)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: g68Lb5/YEiqhBg5YGYSGdIHzRgn41JjqIlZwHULu5msBAhTiKQOjr/ebiGPsN9EuIfjNzibL6btBO7VHvXdYqkLXws05ZSWdLOnUe9iot3iYiimyHnThw3y79FPY+RccvSftpzf9kv6Ezy+9QIME1ZWBdtuO3N4w3UIjvvJEu1HsISQJoZFtsoMQoIMGtbNLnWjW6po52OS6dtfht6DB3gkEnQpJ1UdAub4wMJCN0QsXnJEDVYdrSPX0YmoOqyt6frH2KdpuUDbSEXjKOhdHDCT0xca01nnPFeKJ/tXjGT1DO6qHfWi4SqP5p/mqyylhSQFdlEwEVhiu6Fb44GX0SpbyUTZS5L2QLrCUfOREdhAry+Emw6O1AdWni4eSqRE8or2ryyL7ngGQNsHBMOy2NCe7+ZMUvddWadeCqdbTxxNwJqniZ/XBJtnedi/hK8WTYptMYbBevXhWssgnZd9kl/NoBVUIa84DB6prbOSicZaQy7xc5VFhnblCiUd13vwFDsmR16jBJQKUUi6XrPEMf4ZWS+HpLvGVHZZtU+vu89TIWRqv1a1IXvKMcUIaDyapbaEVqMjGP3tjOzHIXJq0QmtzySx5/+MxwXbQbnVrkuQCczKAq9mTEp0hE+rCuWBxR9L1u6EDorqnrKu2F0tfRgwiMX7ivDKbX0UqzRyDkv6BtIv67IbvtnzjJV2c1R4bO+WeFH9GChwFiwPmyx4C2OQ+HKNWc8BTHHR9Ffr6/3on6NXdJHp1dH5Y2z3It0/ZB8a6n/enwS8MoElSHLJk7gRDy9xkUVSNAGX9fxPh5CkpFEm+55CBvfDjvWCnWxfVtGyd1Mc0udLEO1IEI0Xx2d8LXdzOyH9sd/zNmPjYgXOwA0Ci4nYe72avIrapIcVE6GFUmWoxSkl6Je2a3YoKzGgDgki1xAiaWyUcFjFMMmphN+OMCZbIVVaOB3QoVNzmI1u/vRUgebs9AdY2j7LO8RJIWGAOxzQcWfBOXbG5Mxy+z0tXabGQbaWh4fwC9HBvBG5trHhgpNxsOwM3dPn9NxtMvD9OfZNyFnnNPqx1vqhf/UB3khp4xz+zQzPA4bNMSLi5F8PQCQEp15oS/W3rTRVs8Yi6/GnTVQrQzryWNlWnd/pNltBTP4dVb8vyCveIsasLVF55Ca6YlY4NJQyve7xc06+1wULk9cyAuM8ID8e/5GzHBFR6mQzoqEjazXfelcqWLW3B46omd0niLP5FaLyqOVh9MvRc4OLPa/LbEO4Y6mPuEL80P+7RuOlqBfwZEkzE1qTTYEzszK+vOru2TD8uP+fSseFx04QK7BK/9W5FkXZGwThQ+1/qf2UJ7hxhYhv2f/DJ+TAiFDpCAx+7HChSeU5blCWvxtXjMJEdY8+i8elnkJAuBCY1KGxMdGwGjHcWsJYjrJlnanyixi5TakY7ccoBjNdCZRS5xtJVmOMyQ8yLclSc5LZOIX/kOldUuRB3nVJNeObF9TD4A9M1Jllmsnh6Bonp2yS7MJOHxWkAEySbZRFSQXasRADyy9mdz0Rq1NOOf3Z976jt/lZfzKEW1EYiQnxh6dhV+J7PS5LSVIT8IVKgUaho5GYRLsfdw58kz+1yxiSKT7JAHssCxVvk/dYKRu6XFOIQKZZcxgWdHLMcg21SgIipeff4x4d0SjfO2phcP/7IF9DjRlWJUg1HlWv/jT6ShA/qEmzaSxRxvNcepD6drHUXeSgk2KXOOyprRTxB30snlr4eMVL4WtVPDt8REvN3abAePO3eSkU= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 29f54305-c4c0-4b7c-f1fe-08da34c46837 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 09:38:59.3858 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 6sgiCZ46ULhdEMUk6yMRq0sxPYcVq1h3AEkwMc3ckItLYb+idj35dXUKmHcusekamb+mJUqvF/2Riwv04LF7PlNW7RDNAoR+4huY6hDwe4Y= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4527 X-Proofpoint-GUID: onw0EU9sLFGjeIE9-guAy9i980rQt6Gg X-Proofpoint-ORIG-GUID: onw0EU9sLFGjeIE9-guAy9i980rQt6Gg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-13_04,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 spamscore=0 impostorscore=0 clxscore=1015 mlxlogscore=948 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130041 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Takashi Iwai commit dca947d4d26dbf925a64a6cfb2ddbc035e831a3d upstream. In the current PCM design, the read/write syscalls (as well as the equivalent ioctls) are allowed before the PCM stream is running, that is, at PCM PREPARED state. Meanwhile, we also allow to re-issue hw_params and hw_free ioctl calls at the PREPARED state that may change or free the buffers, too. The problem is that there is no protection against those mix-ups. This patch applies the previously introduced runtime->buffer_mutex to the read/write operations so that the concurrent hw_params or hw_free call can no longer interfere during the operation. The mutex is unlocked before scheduling, so we don't take it too long. Cc: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-3-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Ovidiu Panait --- sound/core/pcm_lib.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index 82a7387ba9d2..9eacad3e2f1b 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1878,9 +1878,11 @@ static int wait_for_avail(struct snd_pcm_substream *substream, if (avail >= runtime->twake) break; snd_pcm_stream_unlock_irq(substream); + mutex_unlock(&runtime->buffer_mutex); tout = schedule_timeout(wait_time); + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); set_current_state(TASK_INTERRUPTIBLE); switch (runtime->status->state) { @@ -2174,6 +2176,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, nonblock = !!(substream->f_flags & O_NONBLOCK); + mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); err = pcm_accessible_state(runtime); if (err < 0) @@ -2259,6 +2262,7 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, if (xfer > 0 && err >= 0) snd_pcm_update_state(substream, runtime); snd_pcm_stream_unlock_irq(substream); + mutex_unlock(&runtime->buffer_mutex); return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; } EXPORT_SYMBOL(__snd_pcm_lib_xfer); From patchwork Fri May 13 09:38:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572547 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16C1AC433EF for ; Fri, 13 May 2022 09:39:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379083AbiEMJjR (ORCPT ); Fri, 13 May 2022 05:39:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379096AbiEMJjO (ORCPT ); Fri, 13 May 2022 05:39:14 -0400 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DAFF92A7694 for ; Fri, 13 May 2022 02:39:11 -0700 (PDT) Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D995TY031470; Fri, 13 May 2022 09:39:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=Fl5jxP+4RcvMaN+oZc9wGpsBx4viLnvJHLkgU0mym8s=; b=XR0fydEP5L1ZUdrdLuFUCIslyXp3q09F3Mf4AE7UYpmHernmdbACsRYb3r+a1DklTzva gZVHVW3tIcGbkNyeGO0PcUWsCHjC1WFsCFR4R/LgKBFwdzdCQDvbJPB5cNllvAamJ/at anI1QVrRzaI+c4wSAfQvBHweeRz1JrXUHZmdrkB+rFh+pXr4GAdh8Cm3DBsUvNRlsIN0 PeSTPkzvg1QBOShHrIru/WQ1tV2K5sMMoSiRI+Lt5wk3YokQFmtFbus/kHETqpHvGMm5 8fsyftRLhOTj6aw81HofyvEMqyxpNOJNWiAMT8vrDh//a0XDYqvd16xPwgpjqR1zlgCt bg== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2042.outbound.protection.outlook.com [104.47.73.42]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwdyyvq41-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 09:39:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gOFSZGdUzeiNUDyeaMZmBtLWrb3dkgBpqzwbyPc4QVkmo/XCaUTMMNbdsBA/buD54PZ6Qbqg8YOjtSQxNPWUfp7Y/X1CQjwhKrN24UJi6VIQ6VNpgJOO+0PZ+yZjVBtYpuM62TJrAJSi7Ly1Gv3d83z1P2HvpclBJGXVhIHzlubDIpq53akagDnJIiYY/+t6SKFOrUIswrYaxj5L/HGtVmdE0j1uwF9FchxtqBqZca3kwe5S2m91Mdz4jxcySTtPsUIF1wJQmFrnA+lwzAhSs+BL3dTp/18Fetayp5zOcGKzXzSFtWnNyVV1qHDM2GQD9eG4hEeeIKb04tAduLhJeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Fl5jxP+4RcvMaN+oZc9wGpsBx4viLnvJHLkgU0mym8s=; b=c/ZtVRaS/0l4lEG3COybR39r2nZ6Q3kiAxw18zzn6V5RC//g4AuDB24FoQU1Tz0KqxnD6y0M5M+7+d3cA60ZIaxkKvnqREOFujAB1wD5mlNdDER37mFN9FsaZo70MCr9LfYvkWXNFkRzj5JkADzQK+tl2TeVY6/vHgRc6u08yHbSKjuWxvu1Md3MAh8z6XmgH/sVnN84r+4dk5DumbU7qgf5KAoUAu/WjP6gVivPH8WquqQ84qOF9PYtih9jxXWBoRcEaIqGCp9Tt1vgsfeVl9MpLPsv7eRP0Tjz6Vnlw3jfXFkqQSKN+4/3dtE4GozkDsOBbW6w5qhZ1qI4XyGeYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) by SA0PR11MB4527.namprd11.prod.outlook.com (2603:10b6:806:72::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Fri, 13 May 2022 09:39:00 +0000 Received: from DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842]) by DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842%5]) with mapi id 15.20.5250.015; Fri, 13 May 2022 09:39:00 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.14 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Fri, 13 May 2022 12:38:30 +0300 Message-Id: <20220513093832.1434697-3-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513093832.1434697-1-ovidiu.panait@windriver.com> References: <20220513093832.1434697-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR02CA0047.eurprd02.prod.outlook.com (2603:10a6:802:14::18) To DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dbcdae3d-8fe7-4e04-1151-08da34c468f2 X-MS-TrafficTypeDiagnostic: SA0PR11MB4527:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HUy2+YzjnP5DSdtTnQi9swc84wx6NxE51Ngzi+ox8dzxZcywmI4n8CIYy2iJ09MF17UYn3H1zod7P5TkfjMKL23vucqXHMZLTEQP9TGw5yM8sTire9I0XqBqjcN6ljpcqY3xcnTJrWRbl+eEo/yzFIVoJHexDtKA+1n66T9qFQkcLlFzMbVUALjKOwCTb6nMDJe+P2S7Q81WCaNSIdi9qkXguV0gtSyy/icdpoJNfcd4XwIiwvlShnwJryjFmFa6PqZxgh967oCEKInf2nFchDj/oC4MDgifk/6QhmZ6nX9CBdjA5oOhKbqQbNGb3NiWEuga4HXcn4X4fiUPDgIzCnVtO+eJI8Zlr3STmJ7zm1IwvDusBInHf24X+HIW1AklytrwOc/+6DGacJkNWVew28aoC5JoocaBDmVtOw23ki30KC5IUbCHm3/JUnoWco2CBiIhfcfIW3UY+Z6jmIkO4NIS3ViS20LSP3oXriNEPcwyFdPL8/2KvOf/XqNATvRdgnO2sR4jRq6ek7iFdPXC3tSnzTkuxO8ut/9VU3Lof89vSh45/lH2Bo3KyLq965mKnNyBaH6OZn3mvaqnZce15Mk0Q7krM7m/ookEgRQ+BqVBZ4lGbocOjmkKeAp0gcDr/aI82PbdvfIbfa7ssJ5ljQ9o5nXbbo/L8B+UtskkmfRye7wYoTsny5eFEpz0abxzvW+PfTZ10nSCmweINoQ524PT9kL+dh0FfqXBu0u49lj0qYCqsooxpBnvPKX/p2KxlZk/rOJYe36WS03Fr/frYi8+cbY+UZ+dwdclxEqS9cY= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5327.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(38350700002)(316002)(66476007)(8676002)(4326008)(54906003)(6916009)(8936002)(66946007)(6512007)(26005)(1076003)(2616005)(107886003)(6506007)(52116002)(6486002)(508600001)(6666004)(186003)(966005)(86362001)(38100700002)(36756003)(5660300002)(44832011)(2906002)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: lLt5D6jpGsyIJOkegVwiMeDvfpmr4HLMmYGtYKhjLkXVU3fHMN9fXAnJ2N1+WMEjPJ+xmGpdG41pGPrPnD225JtXRNxoD97PoJZfs6/9FXtwFOZsi5xHRF0R7FS1N7DLuvZXK5QygUf0vzOJzGPeiipXzAuUJ6ICq2XbTYJthaiLRfNJ1XtMWuB6Nkrs5NMh1oKPy1MRtiixBQ5FSKJUq4UOYB4FXreyt8WyGEBhVWhHXRW2dZt7ru4YKM7gXSkNz7KGUfXto17fcO77B/eVlroFiPurvdsOsoCT45V/vEm5mt0VBYVIRKsRLOrjxynB0hP83nC3issWs7n+qm96+QkUGzlYCX5rShCU/9AG98/txvXCM63krqiQvawY2ypNOcylmDHEyJmY0vhUBaDY/oN9f4at7MJqK6RR7SuJ1O2Ejqk1M4wlsoXkKIYu5Hane2fouRvV5goFVKx9QbxyVUHHPbTW/zojeEo+kHuy2ZxbOTk1Us7fBoL6LTFz25un67nuH9UjJZceu7Z5FXZunchQYMa3aFCLOOglD6Osv1Fs8Dpt1tmE29Ol3s1zSdsQU/XHZrnp5C/v4tdkMY0mp2wudQyZbpy7CITnIFoGQdy+a5AIiMtFd0rCyXLrh4rVrwi1wZJuMUEppOVdyAUvv/E6MKXdG/+kf3LC9+/nPNPH6XZlTNA6H9hJBcljLwjayFAsP22te9/Z0DujUal0Lzk11aZgysu5CRZ+wIRtsiPvzFGK0Hr7RCmjxXQLvnAqqWwoBHpyyD2SZliDdEDFsdMLAZGfTfU5IncYw/yTGhAG4DyHDu2XNYf3D6y5HEjuxb2lsqd31zLZ37AAVWuhLxmapddGXeeCS0VfzflYuJ6cz/viA/EqE4EC80kmGGIkTrIAJUg3r1kbJEsi5hxmBz+JC9IQiN2S/C9OhrzCTaqLSmVk4gV8OXmCi5hSxtDKKytiqUjblG3Som1CCjF9N7oGCm31hmoWTnvIhxB0s0CmrNyudyD4YGSfJybV/cux62g0I+X4JlFFVgYx8iUBEugD75KBjaEn5+w9B64r1vkUEYaQ6/NkNDOdOaj+zDevq7o2zj48MAc4+OZU8b2x7X/bN0cNv11GYbWGIcqkHc80swVf1GwBKUSSwy77EilUni50FZUqk9js8PWFMcCEwaj/lYI2AswgadA073tpsiJ5HDGkfnuQUtuPYbV0o/l+guXwR6uLc8SBR+0nf12nqYQfgxhT6yU3KOdBxOZ3nlY6bCQqOoCYyBwjy+z/y5514m9U3jOfHhBwVREG/qVngUuzthogzA+AO4nG+HtB1Dg1DYcf9iv2rRdqTCGP1cDC5H8MGq/QXEi1iM9D4Sc0brWdlWu01sdt7PTKmHCiqOVKGFOE0x/wZUBKN2aEnYwPoPe/9X8Sjsu8CJebJq/uRrp+AujNmrudacrpOdOlI1LyoMrp+SoEsbqystrce0mH68aP1k9s2XNiphg8pfSBLVEcqqwrOoR2HDWhOg9vsXoWGNsoLcW1dMNE01abGDrBFQlPanouoBbaDL+u1SJQB0wcGu5hK4+gH9CeHAeNEnc5gQQxY8vGTIQDLu40UYT0r7yShDxWEA8Tlob85bNMn9ajEbV5aD93Vld6HTaVFaNfENtxTa91lf1bzGuX49yb5t7Iaxvr4zd+ZxjpQRgh2F/9xyLAXCNZt5V44G/KegEEvpfnAO/7n0ACt6Orow08/+eAS924WkQ1gOQUGoSeOUiThQbHsmlnosk0WiHSjzA= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: dbcdae3d-8fe7-4e04-1151-08da34c468f2 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 09:39:00.6212 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qy1BHD70YW5ShJDhvkIC+t/So0wuTXOSP6YIspk8fL0+ssNPuYGinN4b7omdIDw/zuontW9H0aNcJjyx4zXSX37WzIA4z2zWfTuat+qiVk0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4527 X-Proofpoint-GUID: DTxPE3eFHw7grRAReVoxVeOC4uE1guYO X-Proofpoint-ORIG-GUID: DTxPE3eFHw7grRAReVoxVeOC4uE1guYO X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-13_04,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 spamscore=0 impostorscore=0 clxscore=1015 mlxlogscore=896 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130041 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Takashi Iwai commit 3c3201f8c7bb77eb53b08a3ca8d9a4ddc500b4c0 upstream. Like the previous fixes to hw_params and hw_free ioctl races, we need to paper over the concurrent prepare ioctl calls against hw_params and hw_free, too. This patch implements the locking with the existing runtime->buffer_mutex for prepare ioctls. Unlike the previous case for snd_pcm_hw_hw_params() and snd_pcm_hw_free(), snd_pcm_prepare() is performed to the linked streams, hence the lock can't be applied simply on the top. For tracking the lock in each linked substream, we modify snd_pcm_action_group() slightly and apply the buffer_mutex for the case stream_lock=false (formerly there was no lock applied) there. Cc: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-4-tiwai@suse.de Signed-off-by: Takashi Iwai [OP: backport to 4.14: adjusted context] Signed-off-by: Ovidiu Panait --- sound/core/pcm_native.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index b4ba16e31e5c..35613a52ce1f 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -1046,15 +1046,17 @@ struct action_ops { */ static int snd_pcm_action_group(const struct action_ops *ops, struct snd_pcm_substream *substream, - int state, int do_lock) + int state, int stream_lock) { struct snd_pcm_substream *s = NULL; struct snd_pcm_substream *s1; int res = 0, depth = 1; snd_pcm_group_for_each_entry(s, substream) { - if (do_lock && s != substream) { - if (s->pcm->nonatomic) + if (s != substream) { + if (!stream_lock) + mutex_lock_nested(&s->runtime->buffer_mutex, depth); + else if (s->pcm->nonatomic) mutex_lock_nested(&s->self_group.mutex, depth); else spin_lock_nested(&s->self_group.lock, depth); @@ -1082,18 +1084,18 @@ static int snd_pcm_action_group(const struct action_ops *ops, ops->post_action(s, state); } _unlock: - if (do_lock) { - /* unlock streams */ - snd_pcm_group_for_each_entry(s1, substream) { - if (s1 != substream) { - if (s1->pcm->nonatomic) - mutex_unlock(&s1->self_group.mutex); - else - spin_unlock(&s1->self_group.lock); - } - if (s1 == s) /* end */ - break; + /* unlock streams */ + snd_pcm_group_for_each_entry(s1, substream) { + if (s1 != substream) { + if (!stream_lock) + mutex_unlock(&s1->runtime->buffer_mutex); + else if (s1->pcm->nonatomic) + mutex_unlock(&s1->self_group.mutex); + else + spin_unlock(&s1->self_group.lock); } + if (s1 == s) /* end */ + break; } return res; } @@ -1174,10 +1176,12 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops, int res; down_read(&snd_pcm_link_rwsem); + mutex_lock(&substream->runtime->buffer_mutex); if (snd_pcm_stream_linked(substream)) res = snd_pcm_action_group(ops, substream, state, 0); else res = snd_pcm_action_single(ops, substream, state); + mutex_unlock(&substream->runtime->buffer_mutex); up_read(&snd_pcm_link_rwsem); return res; } From patchwork Fri May 13 09:38:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572960 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9542C433F5 for ; Fri, 13 May 2022 09:39:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379081AbiEMJjT (ORCPT ); Fri, 13 May 2022 05:39:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379105AbiEMJjO (ORCPT ); Fri, 13 May 2022 05:39:14 -0400 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DB6B92A76A1 for ; Fri, 13 May 2022 02:39:11 -0700 (PDT) Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D9Snxf011490; Fri, 13 May 2022 09:39:03 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=ffuGkn6ZjJdXWbOG3FM0V/UDcMqJWt7seRT5QsLbNq8=; b=i2owYen5UpJBIx5cD9EuPU2/yE6og2f/sRF9TC1qsodIFjXw8CpEtA2tudivGnKDv6T0 QA1nyLloJClXymTvzKzJn+N8nEaK+kLTLcrTw5d5KK+8MiWzIfKO7JRiG1RVNW9dKKdE n9pqfpZeMtXzxNr/kcCnv0AHrlg/WiQ3iNZMRGdoq/rhPLT2T/PL9prPve1lJMOKGoxr Nopgbriij3Zw0/y2S3Bi1WgKNwtmBoYg57I9a7YcTQuUhGkZ2e18orrjex9mFTdzFLNl brgD8W6TD9Y2oFTTT671O3JfvTSOLVOO87rmT8vnpPho4uMSyhpIdZcqLxpjxAlXB+fl ZA== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2044.outbound.protection.outlook.com [104.47.73.44]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwdyyvq42-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 09:39:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=en1OGY2F5N2hJFnhQ9olwHZ+vWZl47yBqxeRBiiGBdtXXua6yocL9bIjz3dKwnGaJNpzWt22Ck+JKqgRXjSxkkvfwgXiQNGHJivi8hcdyif2sDSjX6P6r4O1SDZ24PovK0htutgJ/2VK9pFEaa9x9xm78WtruziQnQtT9beQbcabelLkhepivq/Gc6WuzSZK/QU/7xNMUHQwjdgcezK4vZZC5c5RBq80L2IHsm2Inv256Jn6cmjQVosnudWK0UJy3rOTG7JkCFOkcqYVNR4wejZ3G9ruRG7UYpfjSjk+n5r7dhVQS1c3dTAre2djPd2JwGf1cSneNc4W3fbXcbLwfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ffuGkn6ZjJdXWbOG3FM0V/UDcMqJWt7seRT5QsLbNq8=; b=Lc5enzEAByZ0Rz96k6xvF4nK/XzXQNN05Vv5O9Q/e0ovfJQS9r0hekyz160SntISh1DqFjLTOnPdtabcTxmrCX70Ihu/e2j6P1c51iOrvtBP/E5byHCAKMIzWB5GmWGcECF9uHj6J9skuR+t/hHkwOHpqEamld9Ho2dZTI3PIC8xXnScNk90e1+ZaEKBk3Bx6S16dmd0VnztPd8JoeZdrcYvTjGg1n4nu/vh0agyLGxU+baB6jlYWx+MwsNBkLOdAGivVNcgZpjhDUsHFTOF3aNG0Y6zpQSlXD9+y5WSPo5nwDVQP480c//XGnkWTqSwo3QKe68pcqBv+UP1JPe69w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) by SA0PR11MB4527.namprd11.prod.outlook.com (2603:10b6:806:72::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Fri, 13 May 2022 09:39:01 +0000 Received: from DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842]) by DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842%5]) with mapi id 15.20.5250.015; Fri, 13 May 2022 09:39:01 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.14 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes Date: Fri, 13 May 2022 12:38:31 +0300 Message-Id: <20220513093832.1434697-4-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513093832.1434697-1-ovidiu.panait@windriver.com> References: <20220513093832.1434697-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR02CA0047.eurprd02.prod.outlook.com (2603:10a6:802:14::18) To DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5ecae668-cd7c-4bf2-f916-08da34c469b0 X-MS-TrafficTypeDiagnostic: SA0PR11MB4527:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nWir0vrCSJ1jmxT4jBnlwjFsHYwAmJgFnGmud5m7k2GOUIpd708vPU/x1qvkYchGpeF9RxydF1fOu0AJv1NKeRnN35JQuzQAOmXxk2HIaOmUwNQMnLXyQrQigaYzekXUAjhKFuUCq2Eq6Lehx3cPaVd14Cv/YMNJRqVxpFQzPCeVK7g05++InJmhwOJtgRfFNXd+aH63w+Di0UMqY4dhi5aSzgdvoEXeVlGhwMMIPd7eKN0sHckKFBIyNiZwidb3/DS0U7R86GjWry8i8tEPcZbnJ0atb2LNiAlp7H7PFAm7Bv1WJoUcarim/manXuHhVPEXeNMecOv2P7u16DRgI1KAk04idJLNoxrodGCPDqKHkRxsAHCRQ+3ud5UUlkdWfhp93dk6P+aMAncBAneaDTt7HXB75OVv4QWRLmhswq1t8N1k/kJYRml10yCFHkboHI7I8D4uskpvD5sLbX9nfcJCUNJXaC47+f8PxfiflvoUG1cPdoIuBWoHfT6S2cxPyDFuJ35o3TmxMaqUkUBonTs8u2KBQxSS8V33W89uEI4iRlHXFS4NA7uIJ0jpnye6yU4AQLDdDq2Aq3KLvONhzRZf8M8x8hCERzvi7NqiVEcn7btsgWx4RW5hEzNNnHQLLiMc41djYVjijO2WkGYRnChT81v54iEV7PFGcTvCN90N3sSowMK3Roy7cwhMb3lm0l/2+YotXydkevvwJ7VQAaudyk7VefLFD25oh0yDexGjdc1ZgGIiGAvjK1blM88GkoUfdg+DTZ799YIVcgoNc0UAPFXguVOF1HVZ1UjELtw= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5327.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(38350700002)(316002)(66476007)(8676002)(4326008)(54906003)(6916009)(8936002)(66946007)(6512007)(26005)(1076003)(2616005)(107886003)(6506007)(52116002)(6486002)(508600001)(6666004)(186003)(966005)(86362001)(38100700002)(36756003)(5660300002)(44832011)(2906002)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 4n9pTRYNYTACW2bd1AB6bxcEqgXDNPLJpu2Y1vFbHKb9V1qei/tz0YK/uoVLu0f4ry8bWloVuSycIzKKoEgvnAnW2FLx4ONrt+rUPbJz3pd1ZZE8IGNXh3qPM3KydaLmIdZ2a1/v1Mx8Vown/Y6qwMMdYQGXILU0h+JwA2ZFt2ZX1mrgS2nq8Mh1fJotnaO/3KlmQBaD+rctBT16JIipwdKb3fDv6BkHca96ymV0fF/w2mGAe0N9yw2E5/tFHT1b5BsPaqEivQghXxtp+fS4skJbMP0asEPkIAkdy2JXnFH5fw2YHtmAu8kQ1aZr29DrtYkKcIdC/Zhj62rh/o2VbWX40vJdw6UWk8VePI10eSLijyd8MeYgPHeKwQoCIZ0AnqfpO+P4VpZoxkfE/YYVPS2Ux5WsaoNxtRafvGyC0nt3AZHXMOW420+jh1IkKZ0k0rSpv0ysJMFa+l+S4gDdOg0jsoDCtfk6TJ5rWxgd9NvP4Y5CSEpYL026Xq4CGt+736tOCFXpdFJWuWWnqgNP2OIlyuc9oXzyVBzOe/iNdGldeOMnvricYT0QNAyS3UCZ3oRhstDmQcAdzLgeO6n7rVwqwuCISLuiQfKZFO0+MSD5e5saz7qJNAwh9bWlbPVVzujupYIL2CT9h5LWRXxLhIAt620YUzPxNMbBjhciZt4Zas1OPEc9MDUryqBGZ35UPtuSgKdCQ3bXHzwGizbpxuEf1+8kDtf+8JPgAC5RpZNV4fLcZmC/JRoUqsvC4ZCx1BV60eruy8hbmvuA7zmWk3DvW3ahJZw7HFe1DdV3wqqQ13uBH0Jvcjh6xUoo+ittT1rjUiJBQtvfBCnsrdrjvtmtBT6MPuFmBSx/FH6SsfJpMH7YvD8PimIcFDPFmcqykF1ymWT3SPfnOw4alwmM6rL4egGcyGX5c1amSvPu2Wlk2JlKPl8/N1oTBx90SXZIL4yenDfT7u5pRKFAQyvjpZh3436qga9oLa88m9uHPlFPfmCcC3mq9RANPbTIvRGM1mHrLXZCV36gyjid8gqMMiqueZaOph4EyW0g8yYDW2bisIvc795S1+jx4+5MIR75Uca7LKCN3cRK83Pg1H4qdRcas1vETe6SHE676+Jzb7r2Z1mBE7aitdky0fXIpFzEYkaXoN+mxs/MDfr2khangxe+0HtlGQbW4uqQLrKIcO/Y85P1FnCd7k2wceGNwVoIhcoKRTCdPgRYSroKFQzSd3oTGPM6H7ob+D8mNCxaW3MZQcsfZpWxZYXDlmxGFYvFQQe5um4q88+cfNnCHtHT3cCzvVm4yHodiEqW4XBumMip+GogiwB3OSCKgNgpBOeQPu0GqhVtF4wv+Lb44qvq3YqIM7WLA5Bkj24Ppzeun4BWiadTV6oy4Kqk9rS82kutjZGuJW4iw1TB/IETF/7oFsFLb8Oe4CbJItyKkDEkMCJyZdE2pdMse1e5J6K9jv56PEOQmQcAy6LSb+eK+zgDJ0/TyLOPi5zm+v/2CYGcL8NFsIj1dversrjx90DRnuqU+3TnO7PErDFjwwX9hha3AIbDTHi8c94dOdIQmbPAEj9NC/blp0hhUP6BcDOHCjWX1nGXzsX8vQWL7gNbYpoIiiXkCTZ1LvTBpsY0H+R9tSZQeB1AHgTiEI/x+6cNC1vSI1WeKZkSkiBKkgMdw1gWfMymPCM4fUUuhj5PjS/FzHknSkhxhU3V++KxbNwtJDnIO771gPwKvbs31OLJHoWxS0VdpR0BS6lZlIzUD2pcePQ= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5ecae668-cd7c-4bf2-f916-08da34c469b0 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 09:39:01.8567 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: bTwxxXYkQRyMRJxSItY2Btb8eSIume+zTQWbBwr5a1iXGf8uqk19pWGkw1vNDuT/Qk3SO2kG/fD/g6KkmPQHEG+T/yedm+PRmbFhGJANhzA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4527 X-Proofpoint-GUID: fa2qDdVADRLmcxSHlESlFYfZN0-E2tra X-Proofpoint-ORIG-GUID: fa2qDdVADRLmcxSHlESlFYfZN0-E2tra X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-13_04,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 spamscore=0 impostorscore=0 clxscore=1015 mlxlogscore=695 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130041 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Takashi Iwai commit 69534c48ba8ce552ce383b3dfdb271ffe51820c3 upstream. We have no protection against concurrent PCM buffer preallocation changes via proc files, and it may potentially lead to UAF or some weird problem. This patch applies the PCM open_mutex to the proc write operation for avoiding the racy proc writes and the PCM stream open (and further operations). Cc: Reviewed-by: Jaroslav Kysela Link: https://lore.kernel.org/r/20220322170720.3529-5-tiwai@suse.de Signed-off-by: Takashi Iwai [OP: backport to 4.14: adjusted context] Signed-off-by: Ovidiu Panait --- sound/core/pcm_memory.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sound/core/pcm_memory.c b/sound/core/pcm_memory.c index ae33e456708c..adc5f3c2379b 100644 --- a/sound/core/pcm_memory.c +++ b/sound/core/pcm_memory.c @@ -160,19 +160,20 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry, size_t size; struct snd_dma_buffer new_dmab; + mutex_lock(&substream->pcm->open_mutex); if (substream->runtime) { buffer->error = -EBUSY; - return; + goto unlock; } if (!snd_info_get_line(buffer, line, sizeof(line))) { snd_info_get_str(str, line, sizeof(str)); size = simple_strtoul(str, NULL, 10) * 1024; if ((size != 0 && size < 8192) || size > substream->dma_max) { buffer->error = -EINVAL; - return; + goto unlock; } if (substream->dma_buffer.bytes == size) - return; + goto unlock; memset(&new_dmab, 0, sizeof(new_dmab)); new_dmab.dev = substream->dma_buffer.dev; if (size > 0) { @@ -180,7 +181,7 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry, substream->dma_buffer.dev.dev, size, &new_dmab) < 0) { buffer->error = -ENOMEM; - return; + goto unlock; } substream->buffer_bytes_max = size; } else { @@ -192,6 +193,8 @@ static void snd_pcm_lib_preallocate_proc_write(struct snd_info_entry *entry, } else { buffer->error = -EINVAL; } + unlock: + mutex_unlock(&substream->pcm->open_mutex); } static inline void preallocate_info_init(struct snd_pcm_substream *substream) From patchwork Fri May 13 09:38:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572546 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D48FC433F5 for ; Fri, 13 May 2022 09:39:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379090AbiEMJjV (ORCPT ); Fri, 13 May 2022 05:39:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379108AbiEMJjO (ORCPT ); Fri, 13 May 2022 05:39:14 -0400 Received: from mx0b-0064b401.pphosted.com (mx0b-0064b401.pphosted.com [205.220.178.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51CCB2A76AC for ; Fri, 13 May 2022 02:39:12 -0700 (PDT) Received: from pps.filterd (m0250811.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D9Uv0L016177; Fri, 13 May 2022 09:39:04 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=windriver.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=PPS06212021; bh=TzPDLTM/WdrZ51fxAxIpZIxs3MABjmnavIxOPklJNX8=; b=buLnk2ZXtcv+N0rafOlIj1EZx0AkZnVEdBpz4BboaGolJG4FBO5VN5qFH9VOnzl8qA9M ETMJa1e1AWC3/ubNn9/AEyOr0pKq2IJPCbOIY7e84mIt84n2CGPKA2VmjewX3PqlSziL w45pjjeQt/amrNlFf0wcPRPqHePnud7wsiAbfBVCsXk1oS68IumxSvbZS/pS8aKMpnN+ ATc+YTo5RGQ+oNT6SvNF/EqloIQzkd32EaRr8Am3bZmmQYpXUTb0j5OTHDH6hsl+PMQP uc59Ibk/4ksyCkGVQs8r5WPGmCp7oFO40GiYjKcyrRCiz/f9EQ6rD/hCqgTr4CjiCA0x tA== Received: from nam04-dm6-obe.outbound.protection.outlook.com (mail-dm6nam08lp2048.outbound.protection.outlook.com [104.47.73.48]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwdyyvq43-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 09:39:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=X2a1Dax+1ZV5GESi0f295rezPKnFbzRhJH/2J5nYzw0mkaFFtoHIMOgfGlCWSaP7DJCCOcznZ2Rd5cpsnbmeNLMtgrLx4rck9Yh7f4At18zNvloQXSiReCrpydQ5YBHbf5xR4zdiViWdBcMQZeKIzPYcNZiECxKUG7a4pE+BISJfbi2jWxjuySsuA1kSj0UI5uIyZ8nNfHuKTACfRxJM+G7H22t8fw7fLx94brqOvszI4c/77RZlGLi18kptWCYFKYTZ42xAxUDZJnV0RxPWAa/k/KgBL1UQWevQGNXiCHv7IRkBNCnJ8UKYf37LNVuIClPtZfI6AKIavfVFhZk2AA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TzPDLTM/WdrZ51fxAxIpZIxs3MABjmnavIxOPklJNX8=; b=ZqkMfAz3nsk0wwKOzPUlbvA0gGsX4hOW/F82hhWnHNOCEIhks5PkDd79+SaG+7LWI3/UVq04W3e2u1kD0ZdZqCJoTJGPDXAn11V9suWwo7f7CwdoZQlyQPnbjiVoFAoBEv5BU26xsq/GRSgAa227cYk6qMqbl6+AHsCw/O+3wbSTLw2Kw9Bt5XR7GehS9rQZN2BjUON3zZzMM0z5SX/Zc7VbCQi2fE5n977kht+sP14Ilr+eMg3OhpHowIuM/e5UI/+4htQ8uxpEy4TvbluFhusJqIImKTZ2olGBavF41BnJfiAlUGwsH7n0sAdlQnqMhN0bG65/kZdTj9oSzZJLnA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=windriver.com; dmarc=pass action=none header.from=windriver.com; dkim=pass header.d=windriver.com; arc=none Received: from DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) by SA0PR11MB4527.namprd11.prod.outlook.com (2603:10b6:806:72::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Fri, 13 May 2022 09:39:03 +0000 Received: from DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842]) by DM4PR11MB5327.namprd11.prod.outlook.com ([fe80::1d93:1511:5d6:9842%5]) with mapi id 15.20.5250.015; Fri, 13 May 2022 09:39:03 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com, Ovidiu Panait Subject: [PATCH 4.14 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock Date: Fri, 13 May 2022 12:38:32 +0300 Message-Id: <20220513093832.1434697-5-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513093832.1434697-1-ovidiu.panait@windriver.com> References: <20220513093832.1434697-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR02CA0047.eurprd02.prod.outlook.com (2603:10a6:802:14::18) To DM4PR11MB5327.namprd11.prod.outlook.com (2603:10b6:5:392::22) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3c5e0d4e-2091-4cb8-e936-08da34c46a6a X-MS-TrafficTypeDiagnostic: SA0PR11MB4527:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JiQrzj5TbVYsMX8U/Ms13+DM+csFPj7oGYzLuT0sNgLDGMJFSTv1zSSL3BuPH151MusicayKp9Em/C7sRx0b9XMR/iAa0yee+ktBV2BH4clJob7t7Wp0v/XoaFeJMaEbltgThSzGUlzQ9R8E+bYC+pzdh9+mqQnwjXJW04JZk0M4/+yB0fRni2wUFhIb+7euvJpX/9wbRQTyLKYlT+oFpIwdFBB4Em8kFFCBsC/nFOJjU78D26tbvgI5YGCi+eY0XbIgo92tIF98G5M5Vj6cRqz/cdV0V534u+juDmA4bOS4fJZskjEoovyVVe4OyWuwsgPCSLCicNmuwni9QZZBpWbvc2Nuy+bP0rv4TTZaiLs+4+2DAs+lP1B2alFUvJkL6uhyaGQEYtX7Oz5HkIm8JCOqDgylc/O30ehnWmtZCDVWoYkA8fRJrXH8a4Lgdfw9mjkCCDbX45gtNPt/vNdBOUYU5sCXyKzNjg7wbJsyxwEN/TKTCbGAP3ZV1OzJhoyQ1c3Nn1DEkwXaYvD7ROryFG4Ljg9mmu79EwmcrAL66mF6Z2QM9Im7hO5+b+ROauHTgcnBUD0HlB3m9x8rp/62UiWWk25Ti4W6gCTF+tm2o8r+Eyjgg2zzi14++aIYGQsH57tzSlup4vBZCCuqkoZscjYGuuXrxUy8VyI+hITVo7jjZ9u4RLQGP6p8uKoWmP49XGswAJzRf+fxjTwJcooB7SUbqyqE91XKFqysIg2z8EksK4/vv+GwV1s2evVofZE0dQwT17TRAzny7f8vzSLqr98+9p6yN8T1JoDQ9AsPebyWF0VHc44RRVdZCvAqmaoLDGsUCNUjFNDbE9daxoBdhQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:DM4PR11MB5327.namprd11.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(83380400001)(38350700002)(316002)(66476007)(8676002)(4326008)(54906003)(6916009)(8936002)(66946007)(6512007)(26005)(1076003)(2616005)(107886003)(6506007)(52116002)(6486002)(508600001)(6666004)(186003)(966005)(86362001)(38100700002)(36756003)(5660300002)(44832011)(2906002)(66556008); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8BpH95v8T1dkJ+1SmjE2/wNZjPEJUf7EjwAhsXTTYVIGGD88ubgPtnGQEcEhNB3XclibS7x9ZGfhxKs4aAiEcRmQNRw3tj8wofn+IL6yg1UvhcelICEse30zw+BiGfe03hByxibSFwcdC/0buY/XJrBW0cSqpISCje8HkU8iwcvxlWT7SImv50AjgP9JaYK6Vw4TDLCImpJYcSeAv769puymdTV36XxEqZUyc8SOz+jxASkgPZh+8jcD6VDYg7NIkNqHimRnrOGaD6U8gG+7KiT0odrO6N4nCX1OiZNCYKaWojC6eDgx/fQ8e0iMThCymlvZicoM066hnWOiFKtKK1QzdOagobXcpKNt9lXLxyiQC5tFlEvwQiHk7wDWqX9rFzoPxlCKDc2MTN9tjRR/v7Nbg9r1QrdAVS9kAwtI1MPZ6WvIoHDfpxehX+x4VYnzZpnV4ldaO1aXDps/FF/9c6I6CNeJaFQKLG9X6oTZsvA5QCuPjLMx4WPRbuAWEiG4tqR7SulkD51Vv313tLOzIhIkxc+1lpoHaqRqIcOnyTKbI1hl4SPYq51Xje8foCHnrmGpqL4NE+BHO4KQppj+dT8xKyeI7+QpOlssJhaJOXUu28r6JYqcflyZb1xqtd2XH+9S5U5DTRSCJTcejgUbwAHl0zh66S+ZpGLL1bUahmdykWS+Ojd5ksFf+hz3WNZrisCChsiXaSk5Hwm2FZbYQKOFGdI2Rf6+BGTXw+eUQVF48tLlSxdKaB30wnSm9qW7b1KKUuCCMnkNm8o501qWDRs28Xm8NXefhW1Sd0riHl98YSA/sGZrJUcDKHri+EQaQ+ZlWuf8x4VwkPwOOE5/llrxPquRX6DqXq1UY/1WgUULni3aRdvrgQ3yBm9Yj7WY4CMVuTkCFFY9p3744MX+2g31NaDAsKk4DYzbJsZMNvtdyU4y/fEzy5n8dCMqrqTU0bQffOtQzCkkshrjSPK5aT4TdNUzGOQSJHCXody8fWYcO+4trA/5+W4lnDi2xNVptYhxZnXCUI/CzcHvucdcMci+qLaJKH5YcIpgmJstSMoocko0qdO3xA8Y2HaHhTm3Gy19sXPpZC0FnxD2pp4tp69b8p9WfJ5xzx4ziiGdKuPPsilntxHG2o0bUh8wea6y1ck7SF0BRSnyLTp1ToTdU/jSNeLA52D+hcc6U91MlYD7Z7VoafbVKU0jXvUAAy/DTwTjFW0e7Up65UOFnmMEaNWZXSxYDWjxkhKzs9b1Sdu0yAEXE2oOTTT9yWauSX1iBYHihR26368OwDlw9Yma2TsLOV9hZ6Jap6L9Zhrcg6NAz12eck863cLOhPuqFFlCRaxJRr8GsPw6PZbbzxLlxakfVQ9VTj78aKK0YBtryk+4bapvF5fXF24C5moMU1zu+nLBkygyr8G1KotecdWeg+2L6L7beZ4yFzyhm2+mT8rl6Ec4J2F+Sp6K6u0uTRyWQbzsY5djw9R9kjCkl2gMkO1ZVxrWXKzMk4jhDzJM1Wvl+CWrBmhQHDrTPS3fQEyC+ThkCXY9guCTpREdvwmYJiLoVBIsA3hBTGdqa+OFTdDbnTZN/O+/PtB2/cAl4toCVky5lFpGtLDEQZqhIPlqhCdaiRwRUzfwWcZLTP8COSOGPwlbA7vN9ZN7N7f3cCN/qmQuIE0Q5TnsFLwJsDJQaLu62kcpoxjiaXPtvbgHTqxxZn6htOZ5BKH7BpbU9vpjz60kMks9Vua0bWwgP142Pff4QuRbt09UOfd23t0Z/zA= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3c5e0d4e-2091-4cb8-e936-08da34c46a6a X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 09:39:03.0909 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 8ddb2873-a1ad-4a18-ae4e-4644631433be X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hOATS5A+3m89hjRPXZWopflIbOtJbsGoQhi7jDKu1vvIAZPUsA7fG8nHBRM4+kjCR7FAi4Y1kr87cKV028OdQoV+5JIYpH4on6C1hO+lfqY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR11MB4527 X-Proofpoint-GUID: FFE7MJE9sKAuFG0q10VTObGWDWUw6bHe X-Proofpoint-ORIG-GUID: FFE7MJE9sKAuFG0q10VTObGWDWUw6bHe X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.858,Hydra:6.0.486,FMLib:17.11.64.514 definitions=2022-05-13_04,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 phishscore=0 mlxscore=0 spamscore=0 impostorscore=0 clxscore=1015 mlxlogscore=999 adultscore=0 priorityscore=1501 malwarescore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130041 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Takashi Iwai commit bc55cfd5718c7c23e5524582e9fa70b4d10f2433 upstream. syzbot caught a potential deadlock between the PCM runtime->buffer_mutex and the mm->mmap_lock. It was brought by the recent fix to cover the racy read/write and other ioctls, and in that commit, I overlooked a (hopefully only) corner case that may take the revert lock, namely, the OSS mmap. The OSS mmap operation exceptionally allows to re-configure the parameters inside the OSS mmap syscall, where mm->mmap_mutex is already held. Meanwhile, the copy_from/to_user calls at read/write operations also take the mm->mmap_lock internally, hence it may lead to a AB/BA deadlock. A similar problem was already seen in the past and we fixed it with a refcount (in commit b248371628aa). The former fix covered only the call paths with OSS read/write and OSS ioctls, while we need to cover the concurrent access via both ALSA and OSS APIs now. This patch addresses the problem above by replacing the buffer_mutex lock in the read/write operations with a refcount similar as we've used for OSS. The new field, runtime->buffer_accessing, keeps the number of concurrent read/write operations. Unlike the former buffer_mutex protection, this protects only around the copy_from/to_user() calls; the other codes are basically protected by the PCM stream lock. The refcount can be a negative, meaning blocked by the ioctls. If a negative value is seen, the read/write aborts with -EBUSY. In the ioctl side, OTOH, they check this refcount, too, and set to a negative value for blocking unless it's already being accessed. Reported-by: syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com Fixes: dca947d4d26d ("ALSA: pcm: Fix races among concurrent read/write and buffer changes") Cc: Link: https://lore.kernel.org/r/000000000000381a0d05db622a81@google.com Link: https://lore.kernel.org/r/20220330120903.4738-1-tiwai@suse.de Signed-off-by: Takashi Iwai [OP: backport to 4.14: adjusted context] Signed-off-by: Ovidiu Panait --- include/sound/pcm.h | 1 + sound/core/pcm.c | 1 + sound/core/pcm_lib.c | 9 +++++---- sound/core/pcm_native.c | 39 ++++++++++++++++++++++++++++++++------- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/include/sound/pcm.h b/include/sound/pcm.h index aff851485b4a..0a4770cc3691 100644 --- a/include/sound/pcm.h +++ b/include/sound/pcm.h @@ -397,6 +397,7 @@ struct snd_pcm_runtime { wait_queue_head_t tsleep; /* transfer sleep */ struct fasync_struct *fasync; struct mutex buffer_mutex; /* protect for buffer changes */ + atomic_t buffer_accessing; /* >0: in r/w operation, <0: blocked */ /* -- private section -- */ void *private_data; diff --git a/sound/core/pcm.c b/sound/core/pcm.c index c62240d0f5b7..de48803f2be2 100644 --- a/sound/core/pcm.c +++ b/sound/core/pcm.c @@ -1033,6 +1033,7 @@ int snd_pcm_attach_substream(struct snd_pcm *pcm, int stream, runtime->status->state = SNDRV_PCM_STATE_OPEN; mutex_init(&runtime->buffer_mutex); + atomic_set(&runtime->buffer_accessing, 0); substream->runtime = runtime; substream->private_data = pcm->private_data; diff --git a/sound/core/pcm_lib.c b/sound/core/pcm_lib.c index 9eacad3e2f1b..82d4b72dcb64 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1878,11 +1878,9 @@ static int wait_for_avail(struct snd_pcm_substream *substream, if (avail >= runtime->twake) break; snd_pcm_stream_unlock_irq(substream); - mutex_unlock(&runtime->buffer_mutex); tout = schedule_timeout(wait_time); - mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); set_current_state(TASK_INTERRUPTIBLE); switch (runtime->status->state) { @@ -2176,7 +2174,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, nonblock = !!(substream->f_flags & O_NONBLOCK); - mutex_lock(&runtime->buffer_mutex); snd_pcm_stream_lock_irq(substream); err = pcm_accessible_state(runtime); if (err < 0) @@ -2229,10 +2226,15 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, snd_pcm_stream_unlock_irq(substream); return -EINVAL; } + if (!atomic_inc_unless_negative(&runtime->buffer_accessing)) { + err = -EBUSY; + goto _end_unlock; + } snd_pcm_stream_unlock_irq(substream); err = writer(substream, appl_ofs, data, offset, frames, transfer); snd_pcm_stream_lock_irq(substream); + atomic_dec(&runtime->buffer_accessing); if (err < 0) goto _end_unlock; err = pcm_accessible_state(runtime); @@ -2262,7 +2264,6 @@ snd_pcm_sframes_t __snd_pcm_lib_xfer(struct snd_pcm_substream *substream, if (xfer > 0 && err >= 0) snd_pcm_update_state(substream, runtime); snd_pcm_stream_unlock_irq(substream); - mutex_unlock(&runtime->buffer_mutex); return xfer > 0 ? (snd_pcm_sframes_t)xfer : err; } EXPORT_SYMBOL(__snd_pcm_lib_xfer); diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c index 35613a52ce1f..c57d82402220 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -634,6 +634,24 @@ static int snd_pcm_hw_params_choose(struct snd_pcm_substream *pcm, return 0; } +/* acquire buffer_mutex; if it's in r/w operation, return -EBUSY, otherwise + * block the further r/w operations + */ +static int snd_pcm_buffer_access_lock(struct snd_pcm_runtime *runtime) +{ + if (!atomic_dec_unless_positive(&runtime->buffer_accessing)) + return -EBUSY; + mutex_lock(&runtime->buffer_mutex); + return 0; /* keep buffer_mutex, unlocked by below */ +} + +/* release buffer_mutex and clear r/w access flag */ +static void snd_pcm_buffer_access_unlock(struct snd_pcm_runtime *runtime) +{ + mutex_unlock(&runtime->buffer_mutex); + atomic_inc(&runtime->buffer_accessing); +} + #if IS_ENABLED(CONFIG_SND_PCM_OSS) #define is_oss_stream(substream) ((substream)->oss.oss) #else @@ -644,14 +662,16 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) { struct snd_pcm_runtime *runtime; - int err = 0, usecs; + int err, usecs; unsigned int bits; snd_pcm_uframes_t frames; if (PCM_RUNTIME_CHECK(substream)) return -ENXIO; runtime = substream->runtime; - mutex_lock(&runtime->buffer_mutex); + err = snd_pcm_buffer_access_lock(runtime); + if (err < 0) + return err; snd_pcm_stream_lock_irq(substream); switch (runtime->status->state) { case SNDRV_PCM_STATE_OPEN: @@ -756,7 +776,7 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream, substream->ops->hw_free(substream); } unlock: - mutex_unlock(&runtime->buffer_mutex); + snd_pcm_buffer_access_unlock(runtime); return err; } @@ -789,7 +809,9 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream) if (PCM_RUNTIME_CHECK(substream)) return -ENXIO; runtime = substream->runtime; - mutex_lock(&runtime->buffer_mutex); + result = snd_pcm_buffer_access_lock(runtime); + if (result < 0) + return result; snd_pcm_stream_lock_irq(substream); switch (runtime->status->state) { case SNDRV_PCM_STATE_SETUP: @@ -809,7 +831,7 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream) snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN); pm_qos_remove_request(&substream->latency_pm_qos_req); unlock: - mutex_unlock(&runtime->buffer_mutex); + snd_pcm_buffer_access_unlock(runtime); return result; } @@ -1176,12 +1198,15 @@ static int snd_pcm_action_nonatomic(const struct action_ops *ops, int res; down_read(&snd_pcm_link_rwsem); - mutex_lock(&substream->runtime->buffer_mutex); + res = snd_pcm_buffer_access_lock(substream->runtime); + if (res < 0) + goto unlock; if (snd_pcm_stream_linked(substream)) res = snd_pcm_action_group(ops, substream, state, 0); else res = snd_pcm_action_single(ops, substream, state); - mutex_unlock(&substream->runtime->buffer_mutex); + snd_pcm_buffer_access_unlock(substream->runtime); + unlock: up_read(&snd_pcm_link_rwsem); return res; }