From patchwork Sun Jun 25 16:22:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: YE Chengfeng X-Patchwork-Id: 696577 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 alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F1593EB64DC for ; Sun, 25 Jun 2023 16:24:32 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 2296E839; Sun, 25 Jun 2023 18:23:40 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 2296E839 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1687710270; bh=mGZTnvMo6Po+M8u/HPP2Kmqw5w4BvrHq5tLOfQt0o4I=; h=From:To:CC:Subject:Date:List-Id:List-Archive:List-Help:List-Owner: List-Post:List-Subscribe:List-Unsubscribe:From; b=QLDOcNybU31jXULniUTrAPhmjxTDr48hbeaePV49jwDjHMLTsfkDly2/kXKymAofW +aLPyR1rL0VMEKcGIiabuyQ5+WZ8zOO1/mdc0G5s/BE9hVsekCKKxlg0OG2lH+GKPF fWLEdb4p8WeKXrNz1KdtKCXmVfxG1qP6ZBWk8uVY= Received: by alsa1.perex.cz (Postfix, from userid 50401) id AC42FF80534; Sun, 25 Jun 2023 18:23:14 +0200 (CEST) Received: from mailman-core.alsa-project.org (mailman-core.alsa-project.org [10.254.200.10]) by alsa1.perex.cz (Postfix) with ESMTP id 19D1CF80246; Sun, 25 Jun 2023 18:23:14 +0200 (CEST) Received: by alsa1.perex.cz (Postfix, from userid 50401) id E5525F80246; Sun, 25 Jun 2023 18:23:08 +0200 (CEST) Received: from JPN01-OS0-obe.outbound.protection.outlook.com (mail-os0jpn01on20703.outbound.protection.outlook.com [IPv6:2a01:111:f403:700c::703]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id CAFD1F80124 for ; Sun, 25 Jun 2023 18:23:03 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz CAFD1F80124 Authentication-Results: alsa1.perex.cz; dkim=pass (1024-bit key, unprotected) header.d=connect.ust.hk header.i=@connect.ust.hk header.a=rsa-sha256 header.s=selector2 header.b=JOFaKIVG ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bAL7/HMhFLGHp8YTE5rkVwwrSPPpyaCFBvo0vH7odywq6xrEEuJIrj0QjgarHhSgRiG9r20mCJdbosFikbUxElwci8m/o+e7u2ScrMbBwQ8TN3TRU4EiEk9CGG6HC3XHF8qgekpDB4DGtELwi7h6GJSP1+VYJLXmsUcgkPdq+C3zXBM7IM0ndQSY1LOjAiYrAfnmZXCT974qKmcRuugmnH0TLiDqaSR4lZo3vwhznAq//S4h47pZQIHtZpLbHVUVmWS8h8LRsvmYp3DJcYLbKWKG0SbFfLSD2I52EnvktW2n5JilhaBXow5UtiTwzqkApvSdzbJgUFJBZmNM/vPhuQ== 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=mGZTnvMo6Po+M8u/HPP2Kmqw5w4BvrHq5tLOfQt0o4I=; b=NksagqynyDp/FsgjFf57KFL0mcfJmI/d5dIGJEzZZG8ULqYb8T5bXimVkqgiHoPPa/D816xUEC1d/4HQ7wzAG5tX0wBY4GoTkoRCRiGHv19DQKoUB991hMe6Sdii/EspfCzlCWbyWXDImrliucWAvZT8ND62TLWCqBQY8IWMyl8At0Bb3TWOgrm87NtylfKu06cfyJtD5aBXonf3C3bl+Ssg1A83/NkAIDi7acnAic360OD92NK17lxURmeXRVXeKMYzeONyijXcJLdqHEMuIJv7zayJGTvDyYA5Emq+e0sW0Kty8p+L9ApNMrdPq58OCERaOdGkn4lJNHyx5h+/OQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=connect.ust.hk; dmarc=pass action=none header.from=connect.ust.hk; dkim=pass header.d=connect.ust.hk; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=connect.ust.hk; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mGZTnvMo6Po+M8u/HPP2Kmqw5w4BvrHq5tLOfQt0o4I=; b=JOFaKIVGMKFMQc02P0R5l9E/dALB99Pl6fofbgTAn+6/Kgjm/6kl41bqd1pwsrwHedEjVqGgk2Ukr0GWPMWAQc0WqwAyIHpLx9Dc5ZCSuSQdOSAFb1a4UiqBlKwuYppNFoyvteMVQKLpZvGuCjsmeFhQ2LHBHj3u3W4JTXaDpPY= Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:b7::8) by TYWP286MB2731.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:24e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6521.23; Sun, 25 Jun 2023 16:22:57 +0000 Received: from TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8]) by TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM ([fe80::b429:5aa3:12f1:6b8%4]) with mapi id 15.20.6521.026; Sun, 25 Jun 2023 16:22:57 +0000 From: YE Chengfeng To: "perex@perex.cz" , "tiwai@suse.com" , "pteerapong@chromium.org" CC: "alsa-devel@alsa-project.org" , "linux-kernel@vger.kernel.org" Subject: [PATCH] ALSA: aloop: Fix &cable->lock deadlock issues Thread-Topic: [PATCH] ALSA: aloop: Fix &cable->lock deadlock issues Thread-Index: AQHZp4CrxJ87x2u0IEK4c5YXsaGunQ== Date: Sun, 25 Jun 2023 16:22:56 +0000 Message-ID: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=connect.ust.hk; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: TYCP286MB1188:EE_|TYWP286MB2731:EE_ x-ms-office365-filtering-correlation-id: 578704a0-cdd4-4c2f-184a-08db75986f86 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: RGkTURwOkLPlH7waGtLhCaHczWEXLkhvU5WL++OoXcXIB3jECx5unPdKorv+o536o9zUOTL5tF5BXsqH2fMXKbpPzDVuTOlol+mWb+2oz/psIIg5xedKCt6wtHoGErgiXfwE+4bVk54GyrhfCui1wzA1VGe6BxHAG7Szj/HmP8md2z/PrOwwz6H6yvb+D3IIjpjUVTOcpZZiuLjC6C5yWUUI8S7pAp5v2BLqzz9dbolgb154VNrSHrHRT5JrmRwKcrzzHkwdSnEFpWgqmX9tUbEzo4ecnYWzGT4VXcNEXlqRnIJlISj58jE8E+gOnaqBFVYT6q7c/Dpc8vQalpQdZzewJMEnN91IeRnaWOKikz99vTAKp8PTFLiBq56+fJYklRkitlibpGubdgxwyO9K5W92lZN+yOD31HR9mjWZIEUKTf38YzgJJ+SPp82QEwQVbFpz0UYAiCx39vaifXPaCPlYIfPmWdPAV7bsqvnw/zT/8l7AmUgus3yOMxrB3s9WsQ0wBWNgo4C03OjDrgXL7Y6KeO6VY+syDBEEKPrxudkXlLgyrGv+h9j0RnP2HhxDD58odPj6QCm7mN9+oBh6+zx/mtxm4kSTonH+gxFEdXBFB1t1dgUqkO/g5nZySVxl x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM; PTR:; CAT:NONE; SFS:(13230028)(4636009)(346002)(39860400002)(396003)(136003)(376002)(366004)(451199021)(33656002)(52536014)(5660300002)(41300700001)(316002)(86362001)(66476007)(66446008)(8936002)(8676002)(55016003)(38070700005)(64756008)(66556008)(4326008)(38100700002)(122000001)(76116006)(91956017)(786003)(66946007)(26005)(6506007)(2906002)(9686003)(186003)(7696005)(71200400001)(478600001)(83380400001)(110136005)(54906003); DIR:OUT; SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?eucgb2312_cn?b?NHV0a3NUempBRlkrQ0ZGcWov?= =?eucgb2312_cn?b?N05jTnN4QU0zUFBPMGhMZmxYUkhJMHk0YTRGc2YxUHJ5MXlDN1VhQ1oxMTdNR0ho?= =?eucgb2312_cn?b?NXEwaXpOQXNGT2pzSXdBS0ZDeXI3RVlnUTVKSkhNdU0yNmp6NEdrTG5ERWs4dDlD?= =?eucgb2312_cn?b?L1h6RFlKU09NZVI4bTc5ckZjTHMvNTJKclRvV21YbWR1M2REMzBvOTAySDlhMjZR?= =?eucgb2312_cn?b?TW9XY0JobDRFTkRTS2pFWGFDZ2l4M1NVYUROejBLK1I5UjNUaXBWbEpQaXowK3JY?= =?eucgb2312_cn?b?enMzWmdLQ0VMdVBxbEVpSzFtblFUMjhhQ3NzOFFadSs4cTRQVmxPR0RJVTBSNXZH?= =?eucgb2312_cn?b?MDJqT0lxMHF1NEJKZmFsL0w4dDhnZmJpV0VRYmN3N2NDMW5YMk44RjVQUTlmRUVm?= =?eucgb2312_cn?b?Q0FPOHVaV2tBQU9jUnV3T0lHY2cvMzd0V2NvSUh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.0 X-OriginatorOrg: connect.ust.hk X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: TYCP286MB1188.JPNP286.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 578704a0-cdd4-4c2f-184a-08db75986f86 X-MS-Exchange-CrossTenant-originalarrivaltime: 25 Jun 2023 16:22:56.9251 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 6c1d4152-39d0-44ca-88d9-b8d6ddca0708 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: nZmEpbbeMO+DfX/X516MGUbERggJTdG7bMB0GjbNrgjw1ofFZbCI5U4gEI13DL3elRZJAfDGy48kBW6CR8xqGA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYWP286MB2731 Message-ID-Hash: 45XQIH6MZJPT5IGHNYJHPB3OH5JTHTQZ X-Message-ID-Hash: 45XQIH6MZJPT5IGHNYJHPB3OH5JTHTQZ X-MailFrom: cyeaa@connect.ust.hk X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-alsa-devel.alsa-project.org-0; header-match-alsa-devel.alsa-project.org-1; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: The timer loopback_jiffies_timer_function is executed under bottom-half softirq context and require a spinlock, thus other process context code requiring the same lock (i.e., loopback_trigger, loopback_pointer) can deadlock with the timer if it is preempted while holding the lock. Deadlock scenario: loopback_trigger -> spin_lock(&cable->lock); -> loopback_jiffies_timer_function -> spin_lock_irqsave(&dpcm->cable->lock, flags); Fix the potential deadlock by using spin_lock_irqsave. Signed-off-by: Chengfeng Ye --- sound/drivers/aloop.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index a38e602b4fc6..8ee93f8581b4 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -379,6 +379,7 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; struct loopback_cable *cable = dpcm->cable; + unsigned long flags; int err = 0, stream = 1 << substream->stream; switch (cmd) { @@ -389,39 +390,39 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd) dpcm->last_jiffies = jiffies; dpcm->pcm_rate_shift = 0; dpcm->last_drift = 0; - spin_lock(&cable->lock); + spin_lock_irqsave(&cable->lock, flags); cable->running |= stream; cable->pause &= ~stream; err = cable->ops->start(dpcm); - spin_unlock(&cable->lock); + spin_unlock_irqrestore(&cable->lock, flags); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) loopback_active_notify(dpcm); break; case SNDRV_PCM_TRIGGER_STOP: - spin_lock(&cable->lock); + spin_lock_irqsave(&cable->lock, flags); cable->running &= ~stream; cable->pause &= ~stream; err = cable->ops->stop(dpcm); - spin_unlock(&cable->lock); + spin_unlock_irqrestore(&cable->lock, flags); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) loopback_active_notify(dpcm); break; case SNDRV_PCM_TRIGGER_PAUSE_PUSH: case SNDRV_PCM_TRIGGER_SUSPEND: - spin_lock(&cable->lock); + spin_lock_irqsave(&cable->lock, flags); cable->pause |= stream; err = cable->ops->stop(dpcm); - spin_unlock(&cable->lock); + spin_unlock_irqrestore(&cable->lock, flags); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) loopback_active_notify(dpcm); break; case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: case SNDRV_PCM_TRIGGER_RESUME: - spin_lock(&cable->lock); + spin_lock_irqsave(&cable->lock, flags); dpcm->last_jiffies = jiffies; cable->pause &= ~stream; err = cable->ops->start(dpcm); - spin_unlock(&cable->lock); + spin_unlock_irqrestore(&cable->lock, flags); if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) loopback_active_notify(dpcm); break; @@ -865,12 +866,13 @@ static snd_pcm_uframes_t loopback_pointer(struct snd_pcm_substream *substream) struct snd_pcm_runtime *runtime = substream->runtime; struct loopback_pcm *dpcm = runtime->private_data; snd_pcm_uframes_t pos; + unsigned long flags; - spin_lock(&dpcm->cable->lock); + spin_lock_irqsave(&dpcm->cable->lock, flags); if (dpcm->cable->ops->pos_update) dpcm->cable->ops->pos_update(dpcm->cable); pos = dpcm->buf_pos; - spin_unlock(&dpcm->cable->lock); + spin_unlock_irqrestore(&dpcm->cable->lock, flags); return bytes_to_frames(runtime, pos); }