Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3497274rwa; Tue, 23 Aug 2022 05:52:10 -0700 (PDT) X-Google-Smtp-Source: AA6agR4lKs2paJHXYso6Fwh/RT1qxgFSwhZExoCH3/QCV4sM8uUDRcym+K7DXAOGkHEX8Ovp0cVi X-Received: by 2002:aa7:c554:0:b0:445:fa29:b11a with SMTP id s20-20020aa7c554000000b00445fa29b11amr3456351edr.168.1661259130406; Tue, 23 Aug 2022 05:52:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661259130; cv=none; d=google.com; s=arc-20160816; b=reH93bvrFpg0T+u4kcZlZI+CbQzYTauRHdK2XqFYKH4VaXTqMgKUOz7dkDyoJq2pwb unm7UhcRyJ55795sQsPRnEBgdwueFN4UAJxRjoTx0eN8oq2WmqTvep17qZsVx3J3ogu/ jELtMgtHKdTzW8UkTzNZbXXSPTIF0DR39ocaY/KUlrJe+oGkSzKfaCT5zNAyXUcqiyzp E1VZBEmUfiL/emMWEAkCRPZXvQH6DF0bJVOnpFgY2MHiAH69LthA1zgtXmIX1wGJOUA/ oBkc71kRAQSamSlISquDGQzTAMnS6l3p0Sr/iHwCq7reDQ0Mh6R4OWg/QFfUBY6DN+t8 YFuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pS/QJHC7NhqROkGf41jPeGPRpr46OMVXokSPmzrfw4w=; b=GB7KxDTrF5/a9MFb27UA4Isdx8U5KVObBdKwDWvFdBlKPHAq4gIB0LLNDJzVen0pPm nHcrVXGtfpJgXSXj5+p0sKK/dnsKF1uPyTzqpM2uUzONJAKVN3FEfkGmIyRJ38/XJsVh YM9hGISJ5QBHJHnZBO76YtLarFNcHon2BIeNMr4590sB5GlsU2+Jy5FOlq7mcgV6Tb95 JewQgneJfrWUAE7FTAqAQjW72277y/7sBzRLSNiMvxaQ7gc3MeeV6CmTQP8LL5EKRcAG JSjjLo/v8O8DgFeTX3al81rHorM9ol0hNcJ4nZwjCvUeJ0m/slYYrT0PyTxwVUx7GEG7 BYdA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BJ4KS3kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j11-20020a05640211cb00b00446c25c98cbsi2589175edw.347.2022.08.23.05.51.41; Tue, 23 Aug 2022 05:52:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BJ4KS3kn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348783AbiHWMAY (ORCPT + 99 others); Tue, 23 Aug 2022 08:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1359000AbiHWL6V (ORCPT ); Tue, 23 Aug 2022 07:58:21 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1ADD8B07EF; Tue, 23 Aug 2022 02:34:51 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id E9152B81C63; Tue, 23 Aug 2022 09:34:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2908BC433D6; Tue, 23 Aug 2022 09:34:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661247287; bh=exFm/ZMmf9i46JKGVkPhXIV2OSR9BEitNtRgrTQLhRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BJ4KS3knX4ociSV6+FPflMLnxBq1YUDF4VjQU/eSNzFN6yJ5eP1wigGglgtvNTLGB 58XQi+n5z0ltnHnMxYKFC1gbs0ZWzBG6cXFTz/ASNM/+vUUOIzhVogYmJGboJ0kuMF rAkd+pTbRZzgPe42+5e2e3heBuMpGl1hVVRmjiCY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Takashi Iwai , Sasha Levin Subject: [PATCH 5.4 378/389] ALSA: core: Add async signal helpers Date: Tue, 23 Aug 2022 10:27:36 +0200 Message-Id: <20220823080131.345297470@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080115.331990024@linuxfoundation.org> References: <20220823080115.331990024@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Takashi Iwai [ Upstream commit ef34a0ae7a2654bc9e58675e36898217fb2799d8 ] Currently the call of kill_fasync() from an interrupt handler might lead to potential spin deadlocks, as spotted by syzkaller. Unfortunately, it's not so trivial to fix this lock chain as it's involved with the tasklist_lock that is touched in allover places. As a temporary workaround, this patch provides the way to defer the async signal notification in a work. The new helper functions, snd_fasync_helper() and snd_kill_faync() are replacements for fasync_helper() and kill_fasync(), respectively. In addition, snd_fasync_free() needs to be called at the destructor of the relevant file object. Link: https://lore.kernel.org/r/20220728125945.29533-2-tiwai@suse.de Signed-off-by: Takashi Iwai Signed-off-by: Sasha Levin --- include/sound/core.h | 8 ++++ sound/core/misc.c | 94 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/include/sound/core.h b/include/sound/core.h index ee238f100f73..8a80121811d9 100644 --- a/include/sound/core.h +++ b/include/sound/core.h @@ -440,4 +440,12 @@ snd_pci_quirk_lookup_id(u16 vendor, u16 device, } #endif +/* async signal helpers */ +struct snd_fasync; + +int snd_fasync_helper(int fd, struct file *file, int on, + struct snd_fasync **fasyncp); +void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll); +void snd_fasync_free(struct snd_fasync *fasync); + #endif /* __SOUND_CORE_H */ diff --git a/sound/core/misc.c b/sound/core/misc.c index 3579dd7a161f..c3f3d94b5197 100644 --- a/sound/core/misc.c +++ b/sound/core/misc.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #ifdef CONFIG_SND_DEBUG @@ -145,3 +146,96 @@ snd_pci_quirk_lookup(struct pci_dev *pci, const struct snd_pci_quirk *list) } EXPORT_SYMBOL(snd_pci_quirk_lookup); #endif + +/* + * Deferred async signal helpers + * + * Below are a few helper functions to wrap the async signal handling + * in the deferred work. The main purpose is to avoid the messy deadlock + * around tasklist_lock and co at the kill_fasync() invocation. + * fasync_helper() and kill_fasync() are replaced with snd_fasync_helper() + * and snd_kill_fasync(), respectively. In addition, snd_fasync_free() has + * to be called at releasing the relevant file object. + */ +struct snd_fasync { + struct fasync_struct *fasync; + int signal; + int poll; + int on; + struct list_head list; +}; + +static DEFINE_SPINLOCK(snd_fasync_lock); +static LIST_HEAD(snd_fasync_list); + +static void snd_fasync_work_fn(struct work_struct *work) +{ + struct snd_fasync *fasync; + + spin_lock_irq(&snd_fasync_lock); + while (!list_empty(&snd_fasync_list)) { + fasync = list_first_entry(&snd_fasync_list, struct snd_fasync, list); + list_del_init(&fasync->list); + spin_unlock_irq(&snd_fasync_lock); + if (fasync->on) + kill_fasync(&fasync->fasync, fasync->signal, fasync->poll); + spin_lock_irq(&snd_fasync_lock); + } + spin_unlock_irq(&snd_fasync_lock); +} + +static DECLARE_WORK(snd_fasync_work, snd_fasync_work_fn); + +int snd_fasync_helper(int fd, struct file *file, int on, + struct snd_fasync **fasyncp) +{ + struct snd_fasync *fasync = NULL; + + if (on) { + fasync = kzalloc(sizeof(*fasync), GFP_KERNEL); + if (!fasync) + return -ENOMEM; + INIT_LIST_HEAD(&fasync->list); + } + + spin_lock_irq(&snd_fasync_lock); + if (*fasyncp) { + kfree(fasync); + fasync = *fasyncp; + } else { + if (!fasync) { + spin_unlock_irq(&snd_fasync_lock); + return 0; + } + *fasyncp = fasync; + } + fasync->on = on; + spin_unlock_irq(&snd_fasync_lock); + return fasync_helper(fd, file, on, &fasync->fasync); +} +EXPORT_SYMBOL_GPL(snd_fasync_helper); + +void snd_kill_fasync(struct snd_fasync *fasync, int signal, int poll) +{ + unsigned long flags; + + if (!fasync || !fasync->on) + return; + spin_lock_irqsave(&snd_fasync_lock, flags); + fasync->signal = signal; + fasync->poll = poll; + list_move(&fasync->list, &snd_fasync_list); + schedule_work(&snd_fasync_work); + spin_unlock_irqrestore(&snd_fasync_lock, flags); +} +EXPORT_SYMBOL_GPL(snd_kill_fasync); + +void snd_fasync_free(struct snd_fasync *fasync) +{ + if (!fasync) + return; + fasync->on = 0; + flush_work(&snd_fasync_work); + kfree(fasync); +} +EXPORT_SYMBOL_GPL(snd_fasync_free); -- 2.35.1