From patchwork Tue Apr 16 18:44:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 10904017 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D92C81515 for ; Tue, 16 Apr 2019 20:02:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BC7C62228E for ; Tue, 16 Apr 2019 20:02:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B0E27289B6; Tue, 16 Apr 2019 20:02:36 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id CAAEA2228E for ; Tue, 16 Apr 2019 20:02:34 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id DF75B843; Tue, 16 Apr 2019 20:44:22 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz DF75B843 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1555440312; bh=gtwNBcodhOn5wbj0DJe0Ewm1gTBOTtfoY9im19xwf1I=; h=From:To:Date:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From; b=gr36O3quKcfmhwbK5upeucJEOS6ewilmDJFeaKhDkzX7tr5aoFlfpQu5yIq6zt2Fs ju0jG8ltWR/Bz2fBHsiqkXUhN8C0CT2qTYGNNZ/b1Q4XT8KF4WIYYCeIiq+zMYIduU S+QjtDpTU5PWm/0tckHwwY3Sm5DSlJRY/qNC7yTo= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 53744F896E3; Tue, 16 Apr 2019 20:44:22 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 16586F896E5; Tue, 16 Apr 2019 20:44:21 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id DCBF4F89616 for ; Tue, 16 Apr 2019 20:44:17 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz DCBF4F89616 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id A78F2AE7A for ; Tue, 16 Apr 2019 18:44:16 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Date: Tue, 16 Apr 2019 20:44:14 +0200 Message-Id: <20190416184415.27837-1-tiwai@suse.de> X-Mailer: git-send-email 2.16.4 Subject: [alsa-devel] [PATCH 1/2] ALSA: emu10k1: Drop superfluous id-uniquification behavior X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" X-Virus-Scanned: ClamAV using ClamSMTP The emu10k1 driver tries to create a unique id string by itself when it's copied from the card list, but it's rather superfluous, as the same thing will be done in ALSA core side at the card registration. Let's drop the code. This allows us removing snd_cards export. Signed-off-by: Takashi Iwai --- sound/pci/emu10k1/emu10k1_main.c | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/sound/pci/emu10k1/emu10k1_main.c b/sound/pci/emu10k1/emu10k1_main.c index 61f85ff91cd9..0419c75bdf5a 100644 --- a/sound/pci/emu10k1/emu10k1_main.c +++ b/sound/pci/emu10k1/emu10k1_main.c @@ -1882,22 +1882,8 @@ int snd_emu10k1_create(struct snd_card *card, c->name, pci->vendor, pci->device, emu->serial); - if (!*card->id && c->id) { - int i, n = 0; + if (!*card->id && c->id) strlcpy(card->id, c->id, sizeof(card->id)); - for (;;) { - for (i = 0; i < snd_ecards_limit; i++) { - if (snd_cards[i] && !strcmp(snd_cards[i]->id, card->id)) - break; - } - if (i >= snd_ecards_limit) - break; - n++; - if (n >= SNDRV_CARDS) - break; - snprintf(card->id, sizeof(card->id), "%s_%d", c->id, n); - } - } is_audigy = emu->audigy = c->emu10k2_chip; From patchwork Tue Apr 16 18:44:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Iwai X-Patchwork-Id: 10904015 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C32113B5 for ; Tue, 16 Apr 2019 20:02:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0B3E527FA8 for ; Tue, 16 Apr 2019 20:02:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F0D4D28A00; Tue, 16 Apr 2019 20:02:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D031527FA8 for ; Tue, 16 Apr 2019 20:02:34 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 040AA86C; Tue, 16 Apr 2019 20:45:18 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 040AA86C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1555440368; bh=ZaoqEji5EGhCRhntpOC/zPJy52axEiXrbXoaaZNYV78=; h=From:To:Date:In-Reply-To:References:Subject:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=VptJiqMZRCAdozz8KwNg1hgLHf5Vr2Zu+bjeocPwY3D19C3CEJ8A6HZUF8m8iIydG ofK6GtXVEhO83IhR328RUdnZaHINZN7QWxLN6ib83gfVjWF9fF6uvSs2mubN9IB1iD SbUc0GQlK9mkAPsT31MY464qHP+5CW07a5kuSMP8= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id 8868EF8970C; Tue, 16 Apr 2019 20:44:23 +0200 (CEST) X-Original-To: alsa-devel@alsa-project.org Delivered-To: alsa-devel@alsa-project.org Received: by alsa1.perex.cz (Postfix, from userid 50401) id 81F00F896E2; Tue, 16 Apr 2019 20:44:21 +0200 (CEST) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id E4FFAF896E2 for ; Tue, 16 Apr 2019 20:44:17 +0200 (CEST) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz E4FFAF896E2 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id BBE0AAF3B for ; Tue, 16 Apr 2019 18:44:16 +0000 (UTC) From: Takashi Iwai To: alsa-devel@alsa-project.org Date: Tue, 16 Apr 2019 20:44:15 +0200 Message-Id: <20190416184415.27837-2-tiwai@suse.de> X-Mailer: git-send-email 2.16.4 In-Reply-To: <20190416184415.27837-1-tiwai@suse.de> References: <20190416184415.27837-1-tiwai@suse.de> Subject: [alsa-devel] [PATCH 2/2] ALSA: core: Don't refer to snd_cards array directly X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" X-Virus-Scanned: ClamAV using ClamSMTP The snd_cards[] array holds the card pointers that have been currently registered, and it's exported for the external modules that may need to refer a card object. But accessing to this array can be racy against the driver probe or removal, as the card registration or free may happen concurrently. This patch gets rid of the direct access to snd_cards[] array and provides a helper function to give the card object from the index number with a refcount management. Then the caller can access to the given card object safely, and releases it via snd_card_unref(). While we're at it, add a proper comment to snd_card_unref() and make it an inlined function for type-safety, too. Signed-off-by: Takashi Iwai --- include/sound/core.h | 16 ++++++++++++++-- sound/core/init.c | 23 +++++++++++++++++++++-- sound/core/oss/mixer_oss.c | 16 ++++++++++++---- sound/core/sound.c | 5 ++++- sound/last.c | 10 +++++++--- 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/include/sound/core.h b/include/sound/core.h index e923c23e05dd..c90ebbc8d9c4 100644 --- a/include/sound/core.h +++ b/include/sound/core.h @@ -226,7 +226,6 @@ int copy_from_user_toio(volatile void __iomem *dst, const void __user *src, size /* init.c */ -extern struct snd_card *snd_cards[SNDRV_CARDS]; int snd_card_locked(int card); #if IS_ENABLED(CONFIG_SND_MIXER_OSS) #define SND_MIXER_OSS_NOTIFY_REGISTER 0 @@ -251,7 +250,20 @@ int snd_card_add_dev_attr(struct snd_card *card, int snd_component_add(struct snd_card *card, const char *component); int snd_card_file_add(struct snd_card *card, struct file *file); int snd_card_file_remove(struct snd_card *card, struct file *file); -#define snd_card_unref(card) put_device(&(card)->card_dev) + +struct snd_card *snd_card_ref(int card); + +/** + * snd_card_unref - Unreference the card object + * @card: the card object to unreference + * + * Call this function for the card object that was obtained via snd_card_ref() + * or snd_lookup_minor_data(). + */ +static inline void snd_card_unref(struct snd_card *card) +{ + put_device(&card->card_dev); +} #define snd_card_set_dev(card, devptr) ((card)->dev = (devptr)) diff --git a/sound/core/init.c b/sound/core/init.c index 079c12d64b0e..d64416f0a281 100644 --- a/sound/core/init.c +++ b/sound/core/init.c @@ -49,8 +49,7 @@ static const struct file_operations snd_shutdown_f_ops; /* locked for registering/using */ static DECLARE_BITMAP(snd_cards_lock, SNDRV_CARDS); -struct snd_card *snd_cards[SNDRV_CARDS]; -EXPORT_SYMBOL(snd_cards); +static struct snd_card *snd_cards[SNDRV_CARDS]; static DEFINE_MUTEX(snd_card_mutex); @@ -268,6 +267,26 @@ int snd_card_new(struct device *parent, int idx, const char *xid, } EXPORT_SYMBOL(snd_card_new); +/** + * snd_card_ref - Get the card object from the index + * @idx: the card index + * + * Returns a card object corresponding to the given index or NULL if not found. + * Release the object via snd_card_unref(). + */ +struct snd_card *snd_card_ref(int idx) +{ + struct snd_card *card; + + mutex_lock(&snd_card_mutex); + card = snd_cards[idx]; + if (card) + get_device(&card->card_dev); + mutex_unlock(&snd_card_mutex); + return card; +} +EXPORT_SYMBOL_GPL(snd_card_ref); + /* return non-zero if a card is already locked */ int snd_card_locked(int card) { diff --git a/sound/core/oss/mixer_oss.c b/sound/core/oss/mixer_oss.c index 64d904bee8bb..c8618678649c 100644 --- a/sound/core/oss/mixer_oss.c +++ b/sound/core/oss/mixer_oss.c @@ -1403,24 +1403,32 @@ static int snd_mixer_oss_notify_handler(struct snd_card *card, int cmd) static int __init alsa_mixer_oss_init(void) { + struct snd_card *card; int idx; snd_mixer_oss_notify_callback = snd_mixer_oss_notify_handler; for (idx = 0; idx < SNDRV_CARDS; idx++) { - if (snd_cards[idx]) - snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_REGISTER); + card = snd_card_ref(idx); + if (card) { + snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_REGISTER); + snd_card_unref(card); + } } return 0; } static void __exit alsa_mixer_oss_exit(void) { + struct snd_card *card; int idx; snd_mixer_oss_notify_callback = NULL; for (idx = 0; idx < SNDRV_CARDS; idx++) { - if (snd_cards[idx]) - snd_mixer_oss_notify_handler(snd_cards[idx], SND_MIXER_OSS_NOTIFY_FREE); + card = snd_card_ref(idx); + if (card) { + snd_mixer_oss_notify_handler(card, SND_MIXER_OSS_NOTIFY_FREE); + snd_card_unref(card); + } } } diff --git a/sound/core/sound.c b/sound/core/sound.c index b30f027eb0fe..a9ad4379523b 100644 --- a/sound/core/sound.c +++ b/sound/core/sound.c @@ -134,8 +134,11 @@ static struct snd_minor *autoload_device(unsigned int minor) if (dev == SNDRV_MINOR_CONTROL) { /* /dev/aloadC? */ int card = SNDRV_MINOR_CARD(minor); - if (snd_cards[card] == NULL) + struct snd_card *ref = snd_card_ref(card); + if (!ref) snd_request_card(card); + else + snd_card_unref(ref); } else if (dev == SNDRV_MINOR_GLOBAL) { /* /dev/aloadSEQ */ snd_request_other(minor); diff --git a/sound/last.c b/sound/last.c index 43f222825038..4f5a624ab438 100644 --- a/sound/last.c +++ b/sound/last.c @@ -24,14 +24,18 @@ static int __init alsa_sound_last_init(void) { + struct snd_card *card; int idx, ok = 0; printk(KERN_INFO "ALSA device list:\n"); - for (idx = 0; idx < SNDRV_CARDS; idx++) - if (snd_cards[idx] != NULL) { - printk(KERN_INFO " #%i: %s\n", idx, snd_cards[idx]->longname); + for (idx = 0; idx < SNDRV_CARDS; idx++) { + card = snd_card_ref(idx); + if (card) { + printk(KERN_INFO " #%i: %s\n", idx, card->longname); + snd_card_unref(card); ok++; } + } if (ok == 0) printk(KERN_INFO " No soundcards found.\n"); return 0;