From patchwork Fri May 13 08:00:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572549 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 4CEE3C433EF for ; Fri, 13 May 2022 08:01:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356176AbiEMIBd (ORCPT ); Fri, 13 May 2022 04:01:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378012AbiEMIB1 (ORCPT ); Fri, 13 May 2022 04:01:27 -0400 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AA9621820 for ; Fri, 13 May 2022 01:01:26 -0700 (PDT) Received: from pps.filterd (m0250810.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D7kSO1030554; Fri, 13 May 2022 01:00:43 -0700 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=Bqy9Nu1LIJo0MzQSUNg1hMgounrAcf/WsFd5kmV4B78=; b=ZTDMpkRW7zsbPiWEzqniwOzjLwLJok5azCghME/+ddjBzfIdkqOa8hFuQmLvQUrunbCH ugb+xjswGwU6erSFNjfm9P+mCkVB3H0b6zPWgkdR+DmxyaransMDw347X6UbkLbkEyA5 FKG61a95XCBlAm9Tl+51oCIHQfHZ6wnQjwJrNu9efywfAjWujHP2h3X8zta1nWFxTwJX qg3IJHY0ipvRjrJEgqfxA9/ZoDXGFFja+qz6r6cJe51duOX0n/MwuX38ouFb2YkyZyXZ AhtZssM9VFKvGG3hWibolxNrMyEZ63ZszBak7e8Hvmq72s4bzxuta2yokH3fGaPH8+oE qQ== Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fxde5kx1k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 01:00:42 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aWmIirr9NQZMkC59ReHGOU9nGEhlJrR/jwSqj2gQab7HmBnN10NaX8dO3xoEr1/WsSz1E9TvpNMQv1v2R9x7CEQpwBwXPxL30p29Nn/qF1U9wPnERhbf7kXwEeDRQ5OiVU9qvhupSQbYrafisdGtK7/oNLAzLrYi2UCu76OkFGNhKEcM+l+WFr75UODt6hNoTEvimzTqwRoRkkOwJXMTu6WS5E+uvnkDroZlNuVtzNva2H7e819CnJbSYm0QJaAjboIuyozHRI4r4lbTO60dm7+0EpDsK8CWG8yaI5U7dMoEJNW7QwySmSTym6uLdCC6hDH8HUPDTNuypodHZhdjTg== 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=Bqy9Nu1LIJo0MzQSUNg1hMgounrAcf/WsFd5kmV4B78=; b=Jygj+WsY4SXnUZGScbk4RZXhdm9+oip71g9eloV/Ycs0t9cziduLMba5Rn8OFvorBck3JCry9zo0d5jd0T4zIFsMPzE/lMBxfw9seqUeZnuIKNZtAwQyHwnJIJnDfp4vckE1BkFiZnkGVEGMIk0THDz4VMqdwCRUBsdpXpTtClybCrLXig90dWXMQYbdlS3Uou6rTy7JmpF5sOC8IPgOEjXBQMVA3O+MiZvXIL/Cg9pZCxfHdzAEHeSqiTytYnyxiSIaFF16hhRnLmiCJqxvewqpSG/A9u0CVccY2uKuqlekG+6Ft+pKg8vQS0o/QDDHHWmHnrmdkIvZj35oZNmOGg== 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 BYAPR11MB3175.namprd11.prod.outlook.com (2603:10b6:a03:7c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Fri, 13 May 2022 08:00:39 +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 08:00:39 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Hu Jiahui , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.19 1/5] ALSA: pcm: Fix races among concurrent hw_params and hw_free calls Date: Fri, 13 May 2022 11:00:14 +0300 Message-Id: <20220513080018.1302825-1-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 X-ClientProxiedBy: VI1PR10CA0104.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:28::33) 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: 5db2352e-4de8-42f5-67be-08da34b6ab70 X-MS-TrafficTypeDiagnostic: BYAPR11MB3175: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: VHJD30/UVFvHrolqGsY3nnT5tppLLn2FgLkqUngnTlkLjog3E2yQ1X2PqJBBaO15Q3laoQxI/TXs05nJ+Gh00l9lNro95tnrFbXievdjbHuhem57uHqY2u9Kg9/bBTBRvrZkDf1G7/El1I1EdJ7fAUk/YecavRREPRTYNrr1BM5/e7lwNYTG+5V9nsLhjFvNAdKVZ6CH+IFjQm1+J0fnpL+VMtOhpUpAwOuqUIp8QzLLlIjW5+NuzqXlOZ6AqblooPyx+MUgXUDnBuqDVKgVCOWda5ogqOWqaYMC0P7ifBoa53fJDcxIQ40z4xOFJJrvyUg/Opmb0OcCO1XZuvA/+WGiTRkZWSO5pRGZir5QyrHx/ZW5zebN12c5UORPYyiSm2wbdaaFYu7s0fhpf1eqHgUqyCGxaJ76YmhzO/4Gf7RgnU/T6vGg4KG8Kc481xwjy9UWbpeoBWzSt+n8wwj1JAimK/v5GwTkcJnqzNmSRTEXwKw1rQNWvIJFaMgU0RFzl9UAsdNVGb8MXzuwKn2BhiPhPZlY8IfHDBEzfAe4PcoNAoYh7PpfSaFgSGsdCEOv+Q7M3yCK5Zm/ej4pHglxOqX9zBvvkNipa9rVNzxZJneitAEC1ubqKO6b41S/fkzTne/qLnEqdIOkm08Igi5d1Vnc01iwY0Yhud+lx08R1rWecLDa2DRR4bSSa6YSeM9bTD6SWz1Xj8ebgDucZBsSoHOfhYBbf2gZWHj7ey4jI9TJQEaDwT5YlKc0wFmUtR9lcfkNH6cXjtEUUnJLdi1wEngVxFy0hz0kc0nDnFThJ2U= 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)(38350700002)(38100700002)(54906003)(2906002)(4326008)(83380400001)(8936002)(44832011)(5660300002)(66476007)(6916009)(66556008)(8676002)(316002)(36756003)(26005)(6512007)(6506007)(86362001)(2616005)(6486002)(966005)(508600001)(107886003)(186003)(1076003)(52116002)(6666004)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m57Rv6i+8mvPQ+OC98BCcZ1OmP35Wb8ZZjOwzqHzUxzEdYCnF3J6CyVuYnGqApMZiMl9LMu49q74+24bQ/ChpUSS9RRoJNS3VpbdaONxNNbggHksqRWWCqZPU1KfytqBwPYZ5H4ZM7d+Kgd3qpPtXoSM1/Kr2lcMONOrPobuerjhZv39XR+ax41ZTsGljWoBHKjZ8pZg7LF4ubitPcg3Nf/jbDZpb+wFusdQJFIOgR9+JdOMfAPhMtBjDuRCZv6wH2ElcYy5Uo7bzuwQpAcAfVNWk/VESy0kia/J5t5XyyyskX/2gUF+oVM8TEv7Bcx+CxWB/lP7ad23VxeMTTb49+eivYhF2DKBKJWS5vlIz2WXfqj81G0NowTTjIH5ChJsjA0KNT21mcK1lILAOAbjEj5yN5gbHbaHngGAFtuzmaxt/cFdI/3ztI6FnATjSZf8LINy6PkBYqMigPe2r6VeK3eOtp3fl6dkyexB3REhIdkaaa/Ds5TfcrE768PjOmZuePL09nGM/hXTVRUE/Pymq4GhfRObodASjf1SKunkR/ltGsZfiTx4HecjOiUqGvk7LKLQmD+YY1NIm8KqctJmejlGbT4m/tuCduacjhYYs2qF8HYT/Rg0Sn43o6YbyHv9eVK+nBeKM9tjDmkWns1z4VaVRWDAmdLJT1s5crYQ8ETbRFOsyICx/FW4MNpOgZY3gsUY4gY4SUBiwyVVXhq4e26AVFFo4yXGVxVDQv/GjbhlRLjlHxk5UrTVSbwNBd0fgVTqYy29vSccvVgt15zLdcYKeWCJD/oopE23Ebl2aBnUweb4r4EUTFK8U9M7NqkYonBJfl7JmJ0lt+i9MMYl/oZzOQ2H/mHCkPv97yNcwkUDLYLRQ3oun9TFT3d8rvwv5g0LPspeVD7IIguACNFnbSDQuJJ5uaQWBhXx/mgvcvCeTBsSxk0iKbrv4kv10bcWvocwvx51URiviXQ0nsZ++8GOjyGdxGv9Dmy0YjjlX9NA0eJfahLuRk9Ruw7fNlg/7eADgSuO6htcKN8GmNgQKvt0pjee2HDcZCJLQnGuJu9z7qn+hpYo61JAwqzzl9u8QeNXtHEADzr/D6SHbhEjHFQWq+343EaxN1msToJIP95SEbt11/l5CBWhJPn5Nppu+GqqtEtL0835gSyNjSbh255sch/6rIcVIjgAhkhEysP1WIoE6TxYo2XKAryHBf0VsdM29QuRheQBGf6lCaRD2JcBT8X4DkhxP03m6ZucnKP95ASimsvLyPNk6IhsAcjfGpQWgnPPeHTPtJ9DF5xUg0ATr9Wl3A/J1Fba74k+sv36cUqiFhm6SPsOn+hc9UEbE43Nl3Z/+4xjHwSBM9U5eceEum8Jdzga+f14AgDSTbNYkAZ40nvJkKv1KLnGq9yXJbNcJDSa19ZrQIboaVksH7+Wbm+SN0rB2+aJ5MbFh+sP33X8QdQ4OzYt10pTzi3j0vPzYuxdcDrd1h0iKWF+jPhlWj/6FraA8+Zdvv1MeLwM32p4bEl/3SJmHM5SG81ktEE5k8vItq3MZACPDLjCt0IHNKZrioW4TkTgMj0CSkoclmoU2uTiKhZ88DADvBfl8gPfKTRIMxnmwDBR9ddSKkSIim3+RHnf05V0eL4SzsgaqJBRsVq1ec12q+0rf808UHCJaHHp+GmOP+N4P5pn+Z90dT0adIK6g80IX/d9Ufz2hX2N5qUXmD5WZpmyq60w1BFOwt/clnjB1OyGYWyyB4iyvwhm/3ZhqvV68PWYegM= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5db2352e-4de8-42f5-67be-08da34b6ab70 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 08:00:39.3340 (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: QFPcOw9bRXjxYSBGU90uofPu3V3pvoFv9+G3OhJjDUefro81tPeeMHDcivmZX5pP650tnJOcm05Vc/mWYnCaym2bnEqusJcc1m7M/10t2I0= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR11MB3175 X-Proofpoint-GUID: Ssv4lmxW3OtzHLbGfe0pQqBleOZT0_JA X-Proofpoint-ORIG-GUID: Ssv4lmxW3OtzHLbGfe0pQqBleOZT0_JA 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_03,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxscore=0 malwarescore=0 clxscore=1015 bulkscore=0 mlxlogscore=911 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130034 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.19: 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 d6bd3caf6878..0871e16cd04b 100644 --- a/include/sound/pcm.h +++ b/include/sound/pcm.h @@ -404,6 +404,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 b6ed38dec435..a11365dc5349 100644 --- a/sound/core/pcm.c +++ b/sound/core/pcm.c @@ -1031,6 +1031,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; @@ -1062,6 +1063,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 c92eca627840..2b46a2ebefe0 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -666,33 +666,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); @@ -769,14 +776,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; } @@ -809,22 +821,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 08:00:15 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572550 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 3D368C433F5 for ; Fri, 13 May 2022 08:01:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378001AbiEMIBb (ORCPT ); Fri, 13 May 2022 04:01:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353020AbiEMIBA (ORCPT ); Fri, 13 May 2022 04:01:00 -0400 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ECBC17054 for ; Fri, 13 May 2022 01:00:58 -0700 (PDT) Received: from pps.filterd (m0250810.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D7kSO2030554; Fri, 13 May 2022 01:00:43 -0700 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=rs9lnBVcwYfyA1gJXC0lZK+MVjkvqdpjpU36AL421l8=; b=l8OuXYBuhMqTPnsWPN7HE/dLQ1SNCNH8l4+VTSnzY/TL6zPVehgN+6AUOtsfUrrAbOiX 7VIRe7ivPwYf4G0G49gYvE65KSip9rgq0bvpWRWL90p0nvrQg5tQbXGhCR+aUxRUE3/Q SiIi3dmdU+9h9X5EFvY6ZGWEKcN1FNqHeUqvJUr1/CT+FAMB6wbAi3WeQYmGMd1Jk+c9 dp3ZRsqdynRMy1Sp+a/aotw/9sk8f4LKZTxwZvq83Tze3M3ArJhR19ohizp1Zh3jQX+I LJCe49gbdwCj4nC0IO8CaENws9s3zE2HDDXJLuQy1z4CIpbHhvvTLdkW7BGUNSKkU3oe LA== Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fxde5kx1k-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 01:00:43 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Brk1gS2wsBI4S6qPmtYHtVnSqPvvn+4b17U2d0m2sXdov1XtIZIM0q5Kfd6Hw16j4DalruDMoMmq7AsZWjtZa9Yzp0NP7mAAp0yA8Eve7PQipLKEnUAT7Xo0bckt5PsGMq/1AeydBe4bK8TrGNgQ5+/ubBGj5kYyKz2e5+j1A5xhKiGfMjNiNPsd0MShbI821Ogpfscn1uB9zJ520bsQd53TXKUhwywbMB26TPppI+qPFn+N0pRtI3PzrVapAfL0cFAgWI2hV1+dd7SbDhyk0naGjFQG/omeAEmmCLahqjJF1LhAv07xTHzqk6heYEO2lYo8F6vdQTZqNXq5ken+4w== 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=rs9lnBVcwYfyA1gJXC0lZK+MVjkvqdpjpU36AL421l8=; b=oHMjApR2bnsK0CyOPjd5UX99SDON87xrKL0XMsWZcE92tUVWlLQzYbjQd4RRrgrtz+bJIy6ORYbWAh7oROhYif2RqDQbP3A6ZfxEVDFD+1Sm/cjugswzb6xF/PtWt5iGNdVaU5SZfEXjMjt2WcyCI9mp+QjYdEe1SnUC6gbhNFoO8w6vA+Z4XJ1ZbxkgcknJRUXgzvcDN039ytL8m68ggfx5Ck8X+RlI1wQukn8xLFzKYPEPB5KvwAT9y09F+5Mo6XL4heDdlC9HPsyGzsVNsL3GsKpecJUcywK0h9aEPyi72ecsJLb1JZAa5JY0wMnN3Hp73iaaO18JuavEcvdJdA== 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 BYAPR11MB3175.namprd11.prod.outlook.com (2603:10b6:a03:7c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Fri, 13 May 2022 08:00:40 +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 08:00:40 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.19 2/5] ALSA: pcm: Fix races among concurrent read/write and buffer changes Date: Fri, 13 May 2022 11:00:15 +0300 Message-Id: <20220513080018.1302825-2-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513080018.1302825-1-ovidiu.panait@windriver.com> References: <20220513080018.1302825-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR10CA0104.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:28::33) 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: 5a3cc2f3-fe98-46cf-2c8a-08da34b6ac42 X-MS-TrafficTypeDiagnostic: BYAPR11MB3175: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: ysds9KHUzHwsZZrWUaeIS+7EKYPoqs35ItZ3oghM5es/bys0C7MS90gMbmgJHo7Z32KMrKnDW8ixOpyLi2z6YShA+VvqCBH6ImokZ8ISUg2MVWcZcw1BMW3gmubnR25PbJusZ+sow+f3QGIhsUDADc2LCdfV0HYb2sd7oCPdQhnGrUoOAqXDW9SzrraTmF20AkG+kM0Ws4Sat0ZenJinLMvE5GIs7RMe5P2TJ960Vd4XGNayW5DyoBSpOxKrbaQ5EmMqfuVUoZRQpm4JGdcxEbF4uJV8sk8Jl0hjDIUTAHyFulwNO0u8QORB0ij7UYM9KY68BsywPlFei0VcHZ16kEyZSq0P84UGX3l6IjO7lDHxGgeByxvQghYypJLZ/kWFqFveI5qknMOemmkVuoPk2l/g1ERQgP/m3hgSSCvWtGoK+R5UU7GSUWla784ry16FKFISb14wXkGglW/sfu+0HrLTAXXPkTzWdqEB3sGJ3yk8ikAPSYHgwQmqsB1T19VX806WUdY1suQvIJ4DcebaxkUhjw9b2vh5i2ApyhfJ4gmyFsOffDkDBtAkhy2haVzWp3UsTFaBPXqhcrITi6CrDP3atOMVxiXwdv7yCSyoRFPOBcZdcuyZc0UeJSxOj5w6vM7N1Lbl1MHhOQiDY6qRnITCOtMbVbo2XOanYFFme3sLgksIY9OlsQfogBI8mt5BosVrGbQVl+g8c6Q/w6Zb9inb3c3EhcysMOy5d5Ip97eK3jMBnCSmUvpOidcIaAO7w7T/tyCvXvgfis0mQFPGBfAmr3TtuwDyl2iXdNfwuJI= 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)(38350700002)(38100700002)(54906003)(2906002)(4326008)(83380400001)(8936002)(44832011)(5660300002)(66476007)(6916009)(66556008)(8676002)(316002)(36756003)(26005)(6512007)(6506007)(86362001)(2616005)(6486002)(966005)(508600001)(107886003)(186003)(1076003)(52116002)(6666004)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 2y7vYXUFBjYYvQJEEs/0D49zcEd+AeyjE1LUZgPInN5m23T36GJ45XADI2FEMCMp2uIkMYRi+2shVnHbDxzC/5krbmMNT77kmxJtBGC1mdIs3jsIA1WrT16K1M8U3BOANaRoITcWOJqUIjZsb/T0tpX5seX8QOQZlyb6oYWLxa8QoSvtVULAckEl9uGy4hKBMgZS3NWKkntrkz89OT5DEq7fElDsatbceRGk7huffiOU9NZ2vzR0aPzAkiqtr6cmWY9wPHG9/cfPcVi1ZOoGcUq7lN3ewybGW8mMSUlKJCYw4+YixStNvfm2sPMNAvKxV9WuF1L6l5F95VWc6E7uLO02UYfiFYtRw4WW/5L48YKZIdUahtaBzTOPlRhSewTcGrZB6wweDAUb7wncscAOSBHWeiRpggnElzkf27kzQqNryjJKuhQJzJdbRsBejWPgupjRF/IPiuYwyy4MkM7jVqJCj3zbf8et7acGEynZpoRWaQrOkykmnFnHDTMZLpJTKvZ2DeTvGuGR1IMwK8TKDZRzxlaseecZxWn3RUgg5BOCEEqAFQeP9irlEE6y9W1KD70L+T3kDv7YFtCtvvsshh8mWNfMCNSFH0G58e+zSx9vGX8kH+R57MDBmAXO+bICZrKOurPiWwNs5snq3mFMKcLbUPrjxKnM9fevv/eE6CrnQqqBlhQY4/a/WwHujrdZHujjxmlnI7SvsOcCoedpRYGiABmgirOSISVwfp+E/uA/zpaQh3xnmsosWfgTPHJkzPy2Qn4UncIIDMDFJ6OhEpugkE58sJelhxKKLXAU6VOELU5CArMVNpa6I1br81oOCIMyhbM0vHYgpow7tUNo+JsP8EXniOqbDigOlEyfJTns9wXfdof8KB1wCIxT+geaN/gNkRMfG2XAKEtaSV60XHV2dTEP39oNr8tiQMOS5R0CD+H+Y1am8YLYYJL4bc8FDRDb1pqN+CaGj7rvWKxVL65XDsO3zDxqrBt049Hn1T9zJAvBlhM/oaTrh87fG2hAaShsyn+ZsIwZh4GJ2EBA9G7qnysx/YrBH42qZ9blE3aBG5jazU2J12Xr3Wv07OUiDIMphem6GSU4ZehprmNQVDAoYndTbh3xdHOVUkmUuxkuE2RqFKJ+EjNXuPn0XYFq9/4dJ18pDvQX7ZcXcbW/i3N2/caz9qw8S///ttb/oBNIRdd5eF+R+oUVdliUyVHGImCZxlpxo40TihRUsmTNwS20Qtwkq8clmQsISgDieBcLI9N5x8fkqgNy2lI0e9F0z005XbjkE7d6IySZRqpwjPUsGhZfcYWqrz8fAbTBPzGq0ykXHfxM+reC5I8P2sIPLUoxDoBMeN5HfgksQVIB7n+BlZ+ffjHPOb8sxmhiBDlNgaVsHAZN2zc5rWOSWJ2DANxDm0HXUGyEwJU9YtI0G9JuH3GvSVSMYJpme/oHGCLh0cr+931/7EPkMVoBF0YZL3CtRvJcziSMicRcGUst+9BTOvqDKcAKG3Y3qY3+ZUwsWq5ha75YmO2J0KUsOgeXFslxQxf8+4tlAlQpiSFdiXc6X3STPkSa4duOneiDksjkU/vKlRkyo5hg6ZJm5Yik+kkPsfvMiUJX9ewnmMSBJqjpHSTVXJwnEcHBBvigaOc7c1W8dHjgDzz2hLZWMgg8Vy02BNz65FAZkcz9QGcSCAzNA/8tso6NxkawmSYtAdnIwNkWt693rYyqJt+ntRd2Ojc4Ics9S7F0dWJ3af+gxBy+TjnMcwMOPdt4lP7l8e4= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5a3cc2f3-fe98-46cf-2c8a-08da34b6ac42 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 08:00:40.6631 (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: gfWpEr8YVS00JTb/aPBRYYnOn3w/eEaFZCxxHGxCBfQsyvdcfExljEC4V9bFUhqEAKnzBsX/XWmbPTmhIZEwroEqrFpXa98+AJc1JsDdatQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR11MB3175 X-Proofpoint-GUID: Nga9uRGHyi4EXgkyvUeJGNFOjW_WXPbf X-Proofpoint-ORIG-GUID: Nga9uRGHyi4EXgkyvUeJGNFOjW_WXPbf 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_03,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxscore=0 malwarescore=0 clxscore=1015 bulkscore=0 mlxlogscore=948 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130034 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 da454eeee5c9..552c642ee53a 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1876,9 +1876,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) { @@ -2172,6 +2174,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) @@ -2254,6 +2257,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 08:00:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572961 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 DCC92C433F5 for ; Fri, 13 May 2022 08:01:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359590AbiEMIBe (ORCPT ); Fri, 13 May 2022 04:01:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378015AbiEMIB3 (ORCPT ); Fri, 13 May 2022 04:01:29 -0400 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BED71237CF for ; Fri, 13 May 2022 01:01:27 -0700 (PDT) Received: from pps.filterd (m0250810.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D7kSO3030554; Fri, 13 May 2022 01:00:44 -0700 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=+Vahm+pHURcSThrFckQF7RPEUQ/fHPUPgqN/Tq6ZJSQ=; b=O8Zm0F5+xcr0kFpzJBVi+tqAnDItXmLY2XJJPe8Wo92F9GCKBBtFbhRnNDtNIJNKUXlG MtYAuP4knnyagJNrznKpQMnC1jIkfVY2INEI3m8BfuKgrld3impsKIOX5yam9+nGa4ny 3yP8bRnN3+PaMlXgNhatqE9zN22xz8162/IMq0PnSBBK1xeOuCFCwHsQT5GOEm/70QFM sMEkdR4Z0cisPX7XKKlN05I8Ux4/F1/8wQIAOjjTChuOJALSgmGDsh1xZme3LiD30Jbd BscmtKls1zl3vV/RZ+GLcqWHiFu5scnPxqQXCi9PlIrH2Iryq+HZWniyZxI2jA23COt3 zA== Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2169.outbound.protection.outlook.com [104.47.58.169]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fxde5kx1k-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 01:00:44 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HzSdSogcBswj7xCtaLsz+EOiO99BXXCGGuHvKCTbPSy2Zj1jLHxKGBjKczrGkQZCr8WEhsixXg9Gcj4qFaxRF1iNUGBEXWLc202sYyJNEPetgeo0MMey2CIz1IUw8sGTAwCLb1yBSldBmrMlpu4pIAQRHo+x3C7FDH8WRhIrpbLEjB1wlFn5BB1HxdEO/cizQXrdK1xaU/3JWYg7Pyo/Of4BrIAS3VfySdBOl0F59Yg5E8B9AuY7XE+CMq6RUa3jNKuSqfSUTcdfs/6PZKK+JG2lpaxuQxzRQheYcBXU78WP0j7xcCAYCeV/vhKMeieze3rUYH/g5Qf5bKla9HSPZg== 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=+Vahm+pHURcSThrFckQF7RPEUQ/fHPUPgqN/Tq6ZJSQ=; b=h42fFQQxQ8He1YgZXW/8ivdqbnoYFdlPklF5X9qQjSnMQl4xK5szXJxYaTM8u0QTdZbhmwZs2Fg70A36FNsFWWTjtZYvXTK1qNelIdqeXLKCNkdeHEjobT5rXX3LTvo3rqcvgW50b5De+cFG+wTLRaaB1tdMuxlgPTe2f+W8S49zFt80x+HKZIFog8q2gIqBlJL58lU8jFYhsJXJMYp7iCdb0+wPVYzuJvpwZn2KZ9zCe0q1IeM+vnZX337vZU3N7rNp1UpAEgggTfHBYl73glnZdtlScYqslkxWafwlNfH4S4LozuAiI+ZER3kpYeL+d3cee1i9/Ixj04pB2MdRuA== 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 BYAPR11MB3175.namprd11.prod.outlook.com (2603:10b6:a03:7c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Fri, 13 May 2022 08:00:42 +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 08:00:42 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.19 3/5] ALSA: pcm: Fix races among concurrent prepare and hw_params/hw_free calls Date: Fri, 13 May 2022 11:00:16 +0300 Message-Id: <20220513080018.1302825-3-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513080018.1302825-1-ovidiu.panait@windriver.com> References: <20220513080018.1302825-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR10CA0104.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:28::33) 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: 06c554be-2621-424c-341d-08da34b6ad08 X-MS-TrafficTypeDiagnostic: BYAPR11MB3175: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: 51lZRlmojgxvvyx1gm9nLmy7Ktu/WSBFrbvlDlgtFnvPuyCg6kgPHPY+CdYUd6EQqEnZEHzBe0N4I1ttUV6HNz5kgVA4IWnIJ5Ke9tZ81gb8hleoaEg24UwjY1KvRkneCJfTu7PL4k2l1G0VEQ3BHEmc6vmSzPICm/4XsLUSIK+GmUgogN7LxN1+U/o4VZO+txtO4nyfegVrvmwJRm9etdCSdL++DygsuTNmUok43P26+zCIs0FJbKbu17jon2EiWbS2YOGVXTT7L9an6RHFXCUqU0iZ6rtTPVF18rK4fKanU38bWUnCtsr8VdG1/gROHBIICU+o2mLRu0zJ/G1Tb6cilj4tDRYMuxO2IE3FQiPFDfFeVHG639l5OCWl7tpBxxqVETfh+AfKElrLhPDH0yvo1N7d4pwcoAo3jTZyOMjin6/ko2HJJ+Zcld1J6aXyqwo5AbUqD4g5TjjG8kb3gry5gmAzH8XYVMkGlqRatn8YErflKXQQ7LXgcsSrX63QZO1vh6l6wKFv6ftIwGw+oc5qzis7fKcMuZsufh5HNpK+wDXBAUzN/czzHYiHdkNGhjU20Y2z5O3wuI2yuQowgmqcabG838W25kApg62j6+DsGZqiT4bhuFRqmpUagCePtvsARx68myugUmaF40e6bIvPmMPvWlC3dl9sKuMUbi73sjrXPQEycP8JBOEkUiAWekhqwuYrzKBO/Ck4nmpDryRteDbIDmY7qlpdhms9/vgxwaZYRLnaOIZ5Z4V9BlCEMI1Wuys+3LPTGvd6FXQj+ohsrUWyJimKujTPxAUIo2k= 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)(38350700002)(38100700002)(54906003)(2906002)(4326008)(83380400001)(8936002)(44832011)(5660300002)(66476007)(6916009)(66556008)(8676002)(316002)(36756003)(26005)(6512007)(6506007)(86362001)(2616005)(6486002)(966005)(508600001)(107886003)(186003)(1076003)(52116002)(6666004)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RdpGM72kflaLZNRBOMJsQgGRhXWG60wn/SuEAuc33Ajw4xDm9NCmkF6XXxoWxM1ETT9608y5cdCuitZ6rABmho2osoYoUdj1o06SlIUiVGLjvfebLRN5Qcxj4QCDqveH0kFm5TjWOErTcfxb4GOVEhkR7eGHkCYmf8S8eugnyhyiN+YkU/YyryE+NLCvAT9QZXRIDjDzvufCljNE05GyH3QNBb1zIBxIPo+xezih2MDEnkh0vbTK0LSnfvTMbreI8t5oOTkSnX0a+aZTRRI0jQCXh0MdL6uwwqcYeqd7SuJhL3IvPwI7BeAY5phTKJ0AQ4vHpJZ40HG5t51OGvQX4xR10AFeANW0kW++EUkmQdU2OQQJZDHADE1tSTjrgClLI5IeiZN8JZmYzY+iylfvNTspyj15Ct6A60s0p36YzXQQ7STdd2cS6rlEOkGe/VkxSYD/VrCkEOEf3HjXlxQfZ4+mEaHATyAMa/o7IW3x6pOKx/9Mxo5uPLUHka/zOqKOrYG94+G/9SOsW9O4gK/F3aNCDS3/yi3SemXYk/Olus3DW9WyDtNae8YL73Q4taAc2Yxk5sp4NEMm63UCqh1YG4GNrOkYV8fK+qbXIRD6RCreYAsGlERY96T2RaeJFBRmMJgKOKPYr++mOwwsg5ysULsiIre3fUpdARTH395/HnZl5wFE9Mv9PCGTPs8UhAXCgyxTpvKVzgdIqbKTU7rj/Q9QZqy7MbKkf7uGxzaFAi2bl9iIqklrU/vFwb3XFxxDIE5rGRMYxojLRSgvQbNWkCYwYsNVkYTUljGb9dzn9MwdLthE99E5kWZLpDi8UN9jO35J8kZ/WimRJJMeJJaM5t6U1684UKTnvwVdzI/5KcfVptTILeDoepk4e8/0brCwQIq4fKTnZzk5uGP5YZUyG4wvvqVsccT0QPvz1mcPP45aP673TY/dPvASmVfgVIg1xKgqK/IAsW/OiPe+uQCdXfqiopnDLyI70B82W4Wxlxw1ydKaT+hdGLTUDDOAfemYYaWNg9YQs58jnKBEHacj19zmN2sKZ28viBlam6XvoelOOWKR46i6fcWkeCBmXOQ/3Ul0I1/R8ozn0mTkCw+xRIt+Hk+01iezU2wVd/5MsMZ70Hi2tr5KAzSeOEJb7g/ZUF6L1VfYP7QWXLizEpOodRVZOryULE0MKmaoLWs/Y/zDbipSIRYsLNEIXvmKUTv6WabpRau+H40BK5FUD75Wlbxzrt2Z6CQsGbOaG7wdm6SB+qjSvr3jL7o70gO/KusdfpdBPps/ZZCQ+FiHUSI6HPcgpdq5vP8QFgFndHT7dn/9ho1gqFpnkt79JaiW41LUkWv7nwIz41HXg4PvBMqjsahJwCs/iUFyi2YEO0z6gxGFUDX1XkdBydzQBWZr2/rmfh1XMuW+x2/W898u2wsSh85dfG4Jf6ITcOM0v9DN4oBS0KzQe+Eq0d0z0BlOOb/fX3Gju/8VguX4Eknbg63ITOUVmmBpXNm/Ohwfu5ij2lIL86Z8zJD2L927N69sAKjZ6VOFWu0D382OujDYdiJwhprn22viNDDPR/mKd2lyhjxE0z3JP3VnrRrfNsyK4ngAnVd1vJjjyIn0fxAK1mnf+bg7HErfkNWdHX1HatBuBJgrYujjud/uClWewvwmNhd/T+XVlDZo9hKZMDZhn6dYrTrb5cgc+nT+KMtBCj7VfUJ5JHkQyBZYvggLgnwyUuEIK0WMMK6oU+AA2H5KEtqlLRGYWkx+gvf2NTbsFQQB4mI= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 06c554be-2621-424c-341d-08da34b6ad08 X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 08:00:41.9284 (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: /YtBBJQkki0auQK09C5vG08ePyi9xhNk1iXDaN1JZGBr6xVU3KNBJE/7CDRvKRSC36tM0j+2379azwq4olTN8Zoj3clRSm7elLb5Bb1YFtQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR11MB3175 X-Proofpoint-GUID: gJaARvf2YfExwdacvHOKt6zc_U4Rsm1g X-Proofpoint-ORIG-GUID: gJaARvf2YfExwdacvHOKt6zc_U4Rsm1g 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_03,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxscore=0 malwarescore=0 clxscore=1015 bulkscore=0 mlxlogscore=896 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130034 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.19: 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 2b46a2ebefe0..f2508f1f13a3 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -1078,15 +1078,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); @@ -1114,18 +1116,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; } @@ -1206,10 +1208,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 08:00:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572548 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 5680AC433FE for ; Fri, 13 May 2022 08:01:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377985AbiEMIBg (ORCPT ); Fri, 13 May 2022 04:01:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378014AbiEMIB3 (ORCPT ); Fri, 13 May 2022 04:01:29 -0400 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76F2822BDF for ; Fri, 13 May 2022 01:01:27 -0700 (PDT) Received: from pps.filterd (m0250809.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D7ijlI002190; Fri, 13 May 2022 01:00:45 -0700 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=XdCq9R5UVt+WRYC8xjjCQfjg0WtbbFBu7rBmAT4ec6g=; b=X5MudyP01d3B9rvHNc+hkd37N0MiGK2uhYh1R75ATaXLU5DNconyW86JX778ZWSkbI69 dtlGKqf9b8CicRClWfAPlp+dJjkBQarap8tb1wkgtnblDt/9lejkUyCRT7T3u8jDAKUl jJfoMGYBa6HI71noPEKCcXj6VkDcIZX/vdONwWEi8r+vBx3z182hR/8HPT4wZr6l3drW 99h3SmAIdD/hoVhueE7X476ofvLFmRzeX+XMDqwuqOgC1FJb/X1uJ+KDu5LcMdAr2Kp3 xjz8LZQuLprqZjKuZEAcoaxoQYOeDNdZpTjBHOLE4CbEkctbt/4PMGBL83t5ouCUbwQ9 jg== Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2168.outbound.protection.outlook.com [104.47.58.168]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fwr9k4eah-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 01:00:45 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KXafLykeI0cnAZrxd3x3D6dGOYzzILsQKXjS523gNLg97C2TLioG44XVUczERpexdBHfMbTDg6sabptG5oX1ytmCIqtk2r08L6rxITU6aSXMPinnqlMH8sFgiDm/RhHRtxa39sZdrdNAwDe5k0Ido0pnTxoUuW8GG+Z62ebySM6lGhktKdh8kh76q19pQSDaNbON9WYuklATr44kOxITQ6yQAXrqkys+4WGDRrbZOdqe7wajxlpzmW0qMi8iNHTDFVtHbYpg5CropEdBHlSk4It9GGqKU+5mJ6lBRpEClFCMGpqBV2K45WHga+7J6wtB8+BeMvlYKyfOjHg3OegjEA== 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=XdCq9R5UVt+WRYC8xjjCQfjg0WtbbFBu7rBmAT4ec6g=; b=Ix58juphMglQztz9r3j0rG5gS6q+n0sV9ISP/7067Nfk7HzjPiLgwQ5B+jdcOtdcpeJtHF9hr9E/6PXYaqEryR+3odMYbWap3+XlJrLuCpwTYcABgDkN1Zioy0LsPbF5wIjNb4F69d3zoWS1CfnDxfkIqmgIQOSWgs+pkVDuzJ9dcLdBI/J5ICqbPNudEhJjvLZNNJzPCNBW+mypaKzyzbBUYzeWjJl0OHrmi0DGHvknQdSl5hiQ+WB6QAFkqWe8yRPp9uDamhKIvHovSTLUN5/zY5ou8NvrE3QZd+yvN1vGAYNGQjSBu5SNsbSXwAaxyKFG1fQNhmpmD4sUdFoV6w== 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 BYAPR11MB3175.namprd11.prod.outlook.com (2603:10b6:a03:7c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Fri, 13 May 2022 08:00:43 +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 08:00:43 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , Jaroslav Kysela , Ovidiu Panait Subject: [PATCH 4.19 4/5] ALSA: pcm: Fix races among concurrent prealloc proc writes Date: Fri, 13 May 2022 11:00:17 +0300 Message-Id: <20220513080018.1302825-4-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513080018.1302825-1-ovidiu.panait@windriver.com> References: <20220513080018.1302825-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR10CA0104.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:28::33) 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: 69d73d71-044e-42f5-e4f6-08da34b6adcb X-MS-TrafficTypeDiagnostic: BYAPR11MB3175: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: kum2VTzJeCP1D01TSYlIajaf41y2QskkvdvL0IzvDiIeqmd4XnJOaE5E5+WCD0CrjJU/iK3pNly0UXDfLUaA/WikxwUGF9qRXZEVHo5VAg+XaKc++y6Vhhttbpx5MdHwkVK40VE/aFeyeIuuP+cnnsnflemP+i2+nci+CXHARjE/ZS0F8dwAz36DEejohC/mS4HIR9AwM2F9YwOyKz0yGV+VLrcZMRHWie9AFsuKhVPdBD3pO9KA7F6qhUL/up3rK9tm6RsIZvYGGFGOkG81Ru5W3r5bCAYYmBHhVgSdKgRxjywxFIt6vtzhpDRQRXBpjZNH487Ds4q8ue/ow9ZWdGVjbMbOw3S7LP4U12jWQSrotmWfQr26xWBEj/NF0enuGw9EQIqiJjSzWYirUaQc51f1rgOsRXC7S1JJVRIqE7H6rAQpR9RdT6Hzge7nCxYgjqM/pcDCH6By2ZqddI9bA2O9eagGXqBEZM04fKIrX1nwxNXoovgF8LYxf5fE3KmI6+0eNbMdiB3pRVL8GsDqfMX6+3RxT/Gam6tWrVrbeHKR15QwhKMgZuEtaDLJzBlpKH2/qazHTI5BDk8uPIUN6zFIXqBtVsyamfg5hEB4p16qlmQG4xx7DS71ZWG12qq19gIksQDmzWY7zQlBtaLXPb7slktXUOB+QI8x9skqhquaXVYfngjEE8wzQA6TeXYfg1Aa7tnwUld5Uj3SQwTn0dEQbAmsNYfLhyEIfTruNz3mEU/LNvcA20rkXsEUiL1i2QXlKsR9eh4twHVBjT0nzCRtMthRZCeI/SoGBD1Ea/o= 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)(38350700002)(38100700002)(54906003)(2906002)(4326008)(83380400001)(8936002)(44832011)(5660300002)(66476007)(6916009)(66556008)(8676002)(316002)(36756003)(26005)(6512007)(6506007)(86362001)(2616005)(6486002)(966005)(508600001)(107886003)(186003)(1076003)(52116002)(6666004)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XV6VD54rQvDgM85pNH1tyATMTZha7MGYsLG1L8a174KoJtibryaWnHS/7jjp9Dt+TmESNfA5Fqi2fkeyxVzUjRMNs9OoQiswcXdDinYEn+cz7TobpScXyvzYlaGTURe9O4WUub73errspMpv1rXDQIS8YoT8Hz6Bp6PWGyxd8ieDhWpC2E/IRa9N6Twuj/DruuBibmCFkaNYAn25NBcRkQ2acFqXNfqOptHSWoLa1M/BXPwyGnu0Tk7RWPLILSajLSomkCc4i5XPuRtsj/Nt2wOMckmiCTcV4wiWNMK2j+8y4O9L1eOKhe856/8JmTKFqvzFp0hVbYXjFWVik2kFMMuNbKze5OpGY99YPEa1T7nuBlqjcUNttvMQN8cYQR306rp+89BMOiG530cTouI04/BQ0Fu+bsCcCsztepJynBfImsRnlB0l2S0ijNi1OALRKxXxo+JtNnqXAdm5UnKrXjtvfqzo5+EMjxf5EAT5Y7FZcDeQphRSIWVpryPNQgs3YVJ8FZ/TeSv97kaeS6V3sLp2g+xMAAvcwssOYePy9lwECDs5rO9brlVnMotDi0qzyjIbtMf+AHyRXPdN30UDpxSJ8nlkh7rBZ+dvjOeRdOczRUXx/zo6T7mWoaeHw7d3A9Xu3/Z4KCkd4RYunrjJUMJNKn4KILRj83WPWoi0X3ZHsodkXIB/P1mKHazw0jss5s6s8hAtMZOpsbFOqZx72T4vM8uKmx2vyYPYFFjVRYolbSw4fjNeeQPLEdA1kmpWeBSt/Xg4+vJcL+C8eSOxLWK/Fj6CzgyMxHA3BB/UcxNVB1qCfdyXMdtdzj9lfuVvg66/9cKEbNSnQf9k4Q8OBCqkDOmg6onDAp5A1EiXjQK8It//7q651Ocwk3Djl5/sa28hPz3m4rJm3yhEVLGP02VQJZF1zrKWFsdGJ0TIZAgFwU2OvTZWUzEQ+6Zuo4+5B+HzCyfOBwQxerghGDYi3+1/oSEd5Dho3g32exiRLk0VIdNiVNAFIYQ1ocJBa142Aivfw6EFEKhuEwpQta6lHMtJYzguZyEyyFxGhj6oFoZgH4xzb1vlc3tol5+o2typRzn+QiTlFKoPpYF6DJ1xVXR8Bx6+3eThg2RN+RKsob0nxNcDqfvjyQXdl4CmDxbNrILUxx3U/TWmk2sr0vJmuheoZd9Icmz1/dnERzJXHxJ0ezN3jMEQkSlrqYP2/vHt2A8DeA6Aez/3jNsOovYkai0M+f1mOwREL2uKqzsz2vfd2A9cJSYYuwJ5GWIZ33hwTKswLNFPAACgMqo6dk7zI7/Jvk4bbNS/0VdfmsWypOeNxOnShg4R/0rqXRSITtYURTXuerGV/MwzWSgvnR5yR9sZmAxajGnpqaeB2B0rxfORSTuVa2rW+hKc9VgPGHy7cXvIwZBsmYyc1rE0+lbVkyEszsGtjOOhyF9yA25JafFFSzbm3oj6RwZH1uctDObSHOeFCqrsfH195DQyii5iev9CDtWD54qWESnDgPPjLZ4CBH9DLj4HdjjUAH8a5K8/ljTU10x38rxjXpHDDCthFx/OOKrasZxLxJnTaxB9I8j24JwAQXFZ2hIiECLXRl6EDte6o5g2yTvtsv6l12G95SDN2mQ6CrOtrbVL7C4MUTSKM0FBmtnB0phUn02HfrGUSUsboMHKVL6CeuAw2WUiHDr+m+fZnOYfyhAjNc+9rn/5cnHkJgOPUzW5V9ppZskoiFbUMuikTsRin0Ed5jw286/4SmvjqDd8CZgcLsXlrSo= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 69d73d71-044e-42f5-e4f6-08da34b6adcb X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 08:00:43.2108 (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: YB5KVYTEezFIk53HUo85hgZBhKDThyhAbb9wsbI8SolM9js2YA72I3jJr/sLcQ0yepIc72vyL5epdA+o0wuphLM8oMZCPHPbxkHEM6Wyks8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR11MB3175 X-Proofpoint-GUID: qNVh3RhTVJhQs5nTW1walZFCs22g7gES X-Proofpoint-ORIG-GUID: qNVh3RhTVJhQs5nTW1walZFCs22g7gES 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_03,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 priorityscore=1501 impostorscore=0 spamscore=0 mlxlogscore=695 phishscore=0 malwarescore=0 suspectscore=0 clxscore=1015 mlxscore=0 lowpriorityscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130034 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.19: 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 4b5356a10315..48e5f0091ce4 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 08:00:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ovidiu Panait X-Patchwork-Id: 572962 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 D3529C433FE for ; Fri, 13 May 2022 08:01:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353020AbiEMIBc (ORCPT ); Fri, 13 May 2022 04:01:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48092 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356176AbiEMIBB (ORCPT ); Fri, 13 May 2022 04:01:01 -0400 Received: from mx0a-0064b401.pphosted.com (mx0a-0064b401.pphosted.com [205.220.166.238]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF80417066 for ; Fri, 13 May 2022 01:00:59 -0700 (PDT) Received: from pps.filterd (m0250810.ppops.net [127.0.0.1]) by mx0a-0064b401.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 24D7mNvO001193; Fri, 13 May 2022 01:00:50 -0700 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=eQoweuhyfoCahwpXygv6rhorNqntwgo3zylxd6EIxmg=; b=TpkelA5PcOD3yQUT4c6/kM16jNW2S0r7ozQdkWfDjT3QAuJtvlFwlwedvemWkrb4EW50 Vh5+BUSr3a45ksOlk/PGsTH4hu73uuQPJrYbZ9qTfkPdmzeyBtViArjrC8Q4Qub4TRU0 VAFpodrfMEyKLBzwttToV0EvS4lbEBoemdHAPokmxnOmpqwbIBASH53f6Xt8gR24mS1g 0eT2UOy7tIYJIklWRZc1GcALGeGu9Z0xCHenYtDJ8Kj8X1wgCHd/W2uoa4R/Kzvu7us1 fwtlbtNBOBX1q9jWCtxiwh2/yVoO9JzCVeDQtWTqVbNTrQSKKXdvItJj7Ab0Y5vXrgXM 1Q== Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2170.outbound.protection.outlook.com [104.47.58.170]) by mx0a-0064b401.pphosted.com (PPS) with ESMTPS id 3fxde5kx1m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 13 May 2022 01:00:50 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oSUvIzgYPyjxgQThw0OfEtOBWFk/9KNpS/9T8Rqz3y+fQ4XxnQ48otwsOQ68A1kvwWkZgq3KhAllUGZ05HucEWyFz6lZqCfTKU2EJOEsWL6/6UlmAYIrJ+Cr/v6X8rsBEgAYQ6av4cYlOyJqWZarmfZBA07JXeyup40GXWf+c6Rmg4A9u8s8tAFXX+4scKcxwRZFsohLhO/bG9sBHJfD8jc1Yp7/6ADRp+RXIIaFVdzWMtwvS24sPxiISZ7dKW9/D7iOLaGmHF1X6G5AYms605usaCILncdLmvTP1N8jCcO9FPLINEyi92lZektz0y5ndu+06zRkSAeFpyijAZg/yA== 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=eQoweuhyfoCahwpXygv6rhorNqntwgo3zylxd6EIxmg=; b=PnOpN7sTsmHj3bCMSVgfp/ybwcwbVEgv3FF3FKtcgO3Vjjt7ZPbpdBPsdy7/4x0ArNeYJp0Kg36kZdXLb/ZX4afi6715zuKS2hV/WNGQtEedT7sAzgalev63dP1/t7wnCrvI5/iAXhjDnFKYV7XkWi5wddBuR8OdwiXag31bH0m1tGYHLcMVpDoOJr03/TZ8DCgntCTazz3BYVam2uwk6L4T+J84VY7pThAiPegrH+5g0gT6f8ejxGraVUuqja+Zl3bHvq0xVCo/hlMdRXHkFxEKlz1rbCVm/8mR/jimN3UIcuO/yGuIqySNDBXlHteqjgwqD5NXmROnPqvaCRErrA== 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 BYAPR11MB3175.namprd11.prod.outlook.com (2603:10b6:a03:7c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5227.23; Fri, 13 May 2022 08:00:44 +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 08:00:44 +0000 From: Ovidiu Panait To: stable@vger.kernel.org Cc: Takashi Iwai , syzbot+6e5c88838328e99c7e1c@syzkaller.appspotmail.com, Ovidiu Panait Subject: [PATCH 4.19 5/5] ALSA: pcm: Fix potential AB/BA lock with buffer_mutex and mmap_lock Date: Fri, 13 May 2022 11:00:18 +0300 Message-Id: <20220513080018.1302825-5-ovidiu.panait@windriver.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220513080018.1302825-1-ovidiu.panait@windriver.com> References: <20220513080018.1302825-1-ovidiu.panait@windriver.com> X-ClientProxiedBy: VI1PR10CA0104.EURPRD10.PROD.OUTLOOK.COM (2603:10a6:803:28::33) 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: 32781517-1a47-471b-f652-08da34b6ae8c X-MS-TrafficTypeDiagnostic: BYAPR11MB3175: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: awUM89dW1bJqJaGCLzDj3Pf6I73Tt2aEXIgSCAWpydkNU5qr+SFlgriJhQo6CxD2Z2LjOUoC5AKDteEDGWJhIEbTXmenm/9rGgkpm30Xn0xWbEc1WotREFIj3HADeq6rDn0LDXxdniN3PHBHRCbvfNRpL0oHmQ6TWLmPsatBAipB3Txb0P+dzedTa5+YB39P6dcFWtCVx0s9VOpIfUpt06Bj7Z0lGT5ip9YWXZydvPDZ5oJXsRktJib6hE2/3Ga6ZkZ56ODt/huOdvb8Vj5ubHlbfp+KNq26HzBxcrA/4wcA4PWRmxKaiDrnrYMZCFQ/hvV1KqZBswJ4sBEo1Cx5U+HTPTCvXYNblXdOI6GkaexIQEFlZGQSJ4j9fiB+9p7nR/MU3w0i5s6W/4ACo9jxhQ89avCpixwsFwMzYaz6yixCT2xsm+KaHjE1ACzl5HttS1ej2+8IpL9P7RaMpddn2iiNwVeqCjaX6VowlZxFvsQEvuuUU2FnGJm4jlAJGvXcZBep9Qc+FvOEvtcRh8vYUAO+jfPozaSA92LFrUx1FrJtvwZfpzjLfthXFfl9MT9vhRhMOEDfQv2/3+m3NuGZFz2boeAKyQld13ZqOBSfWQ+8wrSRH8Bpa0+z55HhYtrH/P9DvYLbpt8IVT+k40OtFPrRZJWp9a7mX2h/qUubF8UJufc6LKCt+p07q6JS1YqcRiWELxpM4Nh+iC7wBn7DcLqoZ3JkvnJcN5qL6GRfCZBvo2JttNqAh3uCwIjT4Pl+zBoCHQ6l1k5Q8xkiWwQkA86aoAEkjAvJB2M2uZhQiTO3TWhhUikz5WKs8z8xejTBJW9p+XdyluhB2UUDF2tLpg== 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)(38350700002)(38100700002)(54906003)(2906002)(4326008)(83380400001)(8936002)(44832011)(5660300002)(66476007)(6916009)(66556008)(8676002)(316002)(36756003)(26005)(6512007)(6506007)(86362001)(2616005)(6486002)(966005)(508600001)(107886003)(186003)(1076003)(52116002)(6666004)(66946007); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Rj1vjSLBakC7GBKS3hRtvjiTfOq1oJVaIZeH2ZOm5rIwz3o/ng0kidVu76U0cbMuVNNeLRgFKlAMWuICClQ7uRacwCjY6lo13h3Kup6RZfHbmIUDwNy4V0zyi11vZ9HlrI5HrTHNHQfY9FNBNLabs8pZzNFRWtcMhYNjQr+7r+UanGT7txMLsxksahqSdxNdDPgBhuTMVntgWkBhfNXKyrg+BjWW9AQsXHXuSdi4hoyk9ksEwT1wkP3u5p9Oali9wsXWdSj1RjsO3xjtS4aLFbC+HrHLo3theVnS+mDtdkGKXtxxLSvMoG0HrF0+St3apaZ4yuB00lTCDujYs7U/WVs+Puj0/2RCp998k4qRBnWtMik46UHRA6E5InlZ+DHZKmoymnEpIlY2s51YTSGH4Ylvnt7O8MkRmxvg/WjJE6vp4ljxZxeTnbeRjNMKQvmISZXeNMCx4rpV/0kB2gN12gQpKOkY/VU0Kk07vmYyB+NsVg08LbdGzGsOGX8b0BO3XZGQuv5YiTHOra9OO+Uv8wiCJz+dZgK++pUyF5KuBHCv1nOe+AIQpk9gItx12DoAIdtqSFXhl5gT23zJ3X6MwZcmhrDvNS2iQxFVLvsn8oHSF2gZSEu6zC5r/WVcmo2MImKkIlKsi2rL1bPS4XYUNJokwtp7FYv5WOjUq0iT5B7YoKj1+c7qTVfaDI4MLUQ9U0djfwk4SSzHPlG6wXanx/opuYhUJrSu6d0GJPVlku6HWZ35ZkIO6dMmrBr4bAzMVEPsqImfVyisEALjMB7Bcg5KnRobV8QXQCRCmxfpdifEg2N2Di4jjbiS/enaRgPw6fJrdTh2LDcZiklwMAY6fIzf45tJqqca6udIhDCUWH2+g0LAuY587c4QU7LyM/bQtOMy3JCJtpSDmTza0eOtB1imGlEsm4J6p5durs8FxhULmHWCb/v/Hqz3A4id116rmKcxInPykyQB27psgku82CYhJqzdxjYpK69TC5nS7QZx/8a34ooqo1xo4zqxRiK2E1LNuZz+t1xZL3s0gk05q9htNSExBWxGcjR3oZM+yBQCtoNm7J5s/jaRsAcbxXV6yVVbpl35qd0RfLlEs5vwIMshPkVVLlGxv9ppm6bblymg6ZR4lW+QhDWEWW0WktdzzQ6sTvSMAyaXEm/inMspdMYu4Gimw22pB+JQNx1oLZZhaoxgma45jwyJGkr7IIUZIjb6uQHUr25gAzi1+X27T1U7HIa6VkxbOaOuH1fl3fBLUf+otP20rWNh9sP+iiZKZaTAowDIc2Wzwooett8SldkpX7eIduKbmw4GWrP8eBJSlmXsszx712vsqeuwQVD8rd2dZDNN7QjN2lNyuJB55+GJ16fSbioxM8Fsar/xeinQR0JEN8aWFO0l3HOMFU2PbSrBsX47XsSLjBHg+WiX/p+x6nHKOfOioF3h49T5Jeg/I02EHG8he06rKp603p6hS/NRVKzHKYUDBHdqjBGSEGDMsDUSAFUutTjyy2K/VX/TzzM00bi4bts6HbGCQPGeFfz94afHzxOJgGduvjg+ApF4HgqnYuW/tcnlQ9zCxL9kn+k3TXNJrsbRQVKQiUdNCUdjP5qIohjM1rk9ve8Ahj0y3g4Yqw9JQV1S5LWsj0g0yoer8N0UnuT7jUWcHvoL/XQ5PIfw+r/3Qs9hpB/b58cy86dCHk5XORG5F675AoGJJj6bUqsEr/Vw+cd/tHsnqokCfh+MF+AgfNy9dkqh8dYxYH/LfIXoAaCLken2yAU= X-OriginatorOrg: windriver.com X-MS-Exchange-CrossTenant-Network-Message-Id: 32781517-1a47-471b-f652-08da34b6ae8c X-MS-Exchange-CrossTenant-AuthSource: DM4PR11MB5327.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 May 2022 08:00:44.5230 (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: rvVkzvdQeWHpO7YFlM2MeaQeA2sHhfLLSdcTCdqdQqhu7XNOPZSnnfwnLF/HOW+dmJ99lOP4R2pmK1k9r9rjb86KJkJ//D11mqfQhdUf1DI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR11MB3175 X-Proofpoint-GUID: QnYHl454yRyVFsmUv9YpBYsXHSVCKVMv X-Proofpoint-ORIG-GUID: QnYHl454yRyVFsmUv9YpBYsXHSVCKVMv 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_03,2022-05-12_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 phishscore=0 mlxscore=0 malwarescore=0 clxscore=1011 bulkscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2205130034 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.19: 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 0871e16cd04b..cd7874535a4b 100644 --- a/include/sound/pcm.h +++ b/include/sound/pcm.h @@ -405,6 +405,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 a11365dc5349..8eed6244b832 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, 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 552c642ee53a..c376471cf760 100644 --- a/sound/core/pcm_lib.c +++ b/sound/core/pcm_lib.c @@ -1876,11 +1876,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) { @@ -2174,7 +2172,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) @@ -2224,10 +2221,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); @@ -2257,7 +2259,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 f2508f1f13a3..9862b60bfa06 100644 --- a/sound/core/pcm_native.c +++ b/sound/core/pcm_native.c @@ -666,6 +666,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 @@ -676,14 +694,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: @@ -788,7 +808,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; } @@ -821,7 +841,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: @@ -841,7 +863,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; } @@ -1208,12 +1230,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; }