Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp2789860ioo; Sun, 29 May 2022 03:53:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwAY3JYsqyIiCpciohL521pzE6K2kc1l+FvS/qw1bsFzdEkrDT/VMSG1ef7dKed67aO6BgW X-Received: by 2002:a17:902:f641:b0:15f:21f8:92a1 with SMTP id m1-20020a170902f64100b0015f21f892a1mr53377343plg.56.1653821632524; Sun, 29 May 2022 03:53:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653821632; cv=none; d=google.com; s=arc-20160816; b=oonyzS/vU/CKABugsX/5dXpBt9SPFgHBzj3ENcNlbjb/8ZA89nUZyD7wefSVeGKneZ tJiGs8VVyxQYR/NMrYp+qsJvfmLbUBVsRt6Arh93uwdtWH7lwFoyRLlRb6zSNQKkH/2O gjJLu6IHpvoLZxoBlzvjen2H/jG24giKW1FmlcMHoKf5xMi06krNgSgVwxZZ0QYl/P3C fh3jaStILxc2W5na6h3wJJOipxTvWf7IWENaNchtjT39Fn01QX6gbxn71T012ekVoRo9 u633PZB5Po121NV6S8M9Ey4BACnavORAu7PcQ1obwNvQGYwjUT+YhkJPcmYQ38bU4lU/ WBEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature:dkim-signature; bh=8Rtujg9sB4Pj/rXWhXsT0WvzqNzEXC5UHMpDPH16RR8=; b=hAIiPa09loa0MUEtzH5Vt6nhG5KJaFnWNH8l9+TyDqcB82/VE9b84V9kaY7HUP3CyC Ltsr6H3z9IcXTe015Dt/B26E9zsTXcqrUIRafEKlnDqY8+4S253muLlp6Mmgig+wGCjO AUrsiFSasVhkpjqWr8gWgejeGojcGvhe6FaET28qVIbPvf6lwaEpZ3RNR3bR/8ej7Km7 vDiWn1EGMFLRj7KHGma1y3LxNS8PFCEEoaQLWdevjfzVm2oRha9j52nK1kjdSLQ2iHFa O7aqmYDLpx4Jf3Tw+k8WGIfW0qpSOmA/0PtCmIOQAVPlegSA0NJKqDak0zdGZKtiGDN3 MYEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=Uf6KFYhj; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k10-20020a65434a000000b003faf1d213dcsi12710626pgq.687.2022.05.29.03.53.41; Sun, 29 May 2022 03:53:52 -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=@suse.de header.s=susede2_rsa header.b=Uf6KFYhj; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; 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=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229798AbiE2Jf4 (ORCPT + 99 others); Sun, 29 May 2022 05:35:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48394 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229622AbiE2Jfz (ORCPT ); Sun, 29 May 2022 05:35:55 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 123EE522E5 for ; Sun, 29 May 2022 02:35:54 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 079DE1F8C8; Sun, 29 May 2022 09:35:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1653816952; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8Rtujg9sB4Pj/rXWhXsT0WvzqNzEXC5UHMpDPH16RR8=; b=Uf6KFYhjpNC13uxQMka7vtllz02yDMYsrnmgrOrhcOF72oWPEx2vhNrbuyNsFyDVRfTFlT pEkZM6BoyZ8rhnRPLUZlcUq4qcHNGExr8lQai0XQ9FSv2J9QYpu52ZzPU1I1ful5VPFXUq ywcjBIqLz0QR4t6RFBW2QDssofSsX/I= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1653816952; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=8Rtujg9sB4Pj/rXWhXsT0WvzqNzEXC5UHMpDPH16RR8=; b=1Jeo79TBT8g7wWOXnTBqYm2p+8E/cKpbiRi6+HIp0zg5tb6WxOVwA/JW6toV7BzwWcO9tE QJEeVK6zLFjk3fBg== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id ADD92139EC; Sun, 29 May 2022 09:35:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id v/TWJXc+k2KwfwAAMHmgww (envelope-from ); Sun, 29 May 2022 09:35:51 +0000 Date: Sun, 29 May 2022 11:35:50 +0200 Message-ID: <8735gsznnt.wl-tiwai@suse.de> From: Takashi Iwai To: Raghu Bankapur Cc: Jaroslav Kysela , Takashi Iwai , Liam Girdwood , Mark Brown , alsa-devel@alsa-project.org, linux-kernel@vger.kernel.org, Krishna Jha Subject: Re: [PATCH V0 1/1] asoc: msm: use hashtable to check kcontrol In-Reply-To: References: User-Agent: Wanderlust/2.15.9 (Almost Unreal) Emacs/27.2 Mule/6.0 MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 On Sun, 29 May 2022 10:50:09 +0200, Raghu Bankapur wrote: > > use hashtabe instead of linear list to check kcontrol before > adding them for improving early audio KPI. > > Change-Id: I7134816736e08e338c0f22a8ae283a0520aa847a > Signed-off-by: Raghu Bankapur Aha, interesting, a faster lookup is indeed measurable and needed. One point with your patch is whether it works when a control element gets removed dynamically. It's often the case with the user-space kctls. Also, multiple ctl elements may have the same name string but with different index or device number. Comparing only the string isn't enough. (And I wonder how about the hash key collision.) FWIW, I posted an RFC patch faster lookup with Xarray some time ago: https://lore.kernel.org/all/20211028130027.18764-1-tiwai@suse.de/T/ thanks, Takashi > --- > include/sound/control.h | 4 ++ > include/sound/core.h | 12 +++++- > sound/core/control.c | 92 +++++++++++++++++++++++++++++++++-------- > sound/core/init.c | 3 ++ > sound/soc/Kconfig | 9 ++++ > 5 files changed, 101 insertions(+), 19 deletions(-) > > diff --git a/include/sound/control.h b/include/sound/control.h > index 985c51a8fb74..1b85d36c2066 100644 > --- a/include/sound/control.h > +++ b/include/sound/control.h > @@ -70,6 +70,10 @@ struct snd_kcontrol_volatile { > struct snd_kcontrol { > struct list_head list; /* list of controls */ > struct snd_ctl_elem_id id; > +#ifdef CONFIG_SND_CTL_HASHTABLE > + struct hlist_node hnode; > + unsigned int knametoint; /* kctl name to uint, hash key value */ > +#endif > unsigned int count; /* count of same elements */ > snd_kcontrol_info_t *info; > snd_kcontrol_get_t *get; > diff --git a/include/sound/core.h b/include/sound/core.h > index b7e9b58d3c78..dd6714fc43ff 100644 > --- a/include/sound/core.h > +++ b/include/sound/core.h > @@ -14,7 +14,9 @@ > #include /* pm_message_t */ > #include > #include > - > +#ifdef CONFIG_SND_CTL_HASHTABLE > +#include > +#endif > /* number of supported soundcards */ > #ifdef CONFIG_SND_DYNAMIC_MINORS > #define SNDRV_CARDS CONFIG_SND_MAX_CARDS > @@ -24,6 +26,10 @@ > > #define CONFIG_SND_MAJOR 116 /* standard configuration */ > > +#ifdef CONFIG_SND_CTL_HASHTABLE > +#define SND_CTL_HASH_TABLE_BITS 14 /* buckets numbers: 1 << 14 */ > +#endif > + > /* forward declarations */ > struct pci_dev; > struct module; > @@ -103,7 +109,9 @@ struct snd_card { > size_t user_ctl_alloc_size; // current memory allocation by user controls. > struct list_head controls; /* all controls for this card */ > struct list_head ctl_files; /* active control files */ > - > +#ifdef CONFIG_SND_CTL_HASHTABLE > + DECLARE_HASHTABLE(ctl_htable, SND_CTL_HASH_TABLE_BITS); > +#endif > struct snd_info_entry *proc_root; /* root for soundcard specific files */ > struct proc_dir_entry *proc_root_link; /* number link to real id */ > > diff --git a/sound/core/control.c b/sound/core/control.c > index a25c0d64d104..914d05647497 100644 > --- a/sound/core/control.c > +++ b/sound/core/control.c > @@ -368,6 +368,47 @@ enum snd_ctl_add_mode { > CTL_ADD_EXCLUSIVE, CTL_REPLACE, CTL_ADD_ON_REPLACE, > }; > > +#ifdef CONFIG_SND_CTL_HASHTABLE > +char snd_ctl_string[50] = { '\0' }; > + > +/* Used to convert the string into int value -- BKDRHash */ > +unsigned int snd_ctl_strtoint(const char *s) > +{ > + unsigned int res = 0; > + > + while (*s) > + res = (res << 5) - res + (*s++); > + > + return (res & 0x7FFFFFFF); > +} > + > +/** > + * snd_ctl_hash_check - Check the duplicate enrty on snd hashtable > + * @card: the card instance > + * @nametoint: kctl name to uint > + * > + * Finds the control instance with the given nametoint from the card. > + * > + * Return: The pointer of the instance if found, or %NULL if not. > + * > + */ > +static struct snd_kcontrol *snd_ctl_hash_check(struct snd_card *card, > + unsigned int nametoint) > +{ > + struct snd_kcontrol *kctl = NULL; > + > + if (snd_BUG_ON(!card)) > + return NULL; > + > + hash_for_each_possible(card->ctl_htable, kctl, hnode, nametoint) { > + if (kctl->knametoint != nametoint) > + continue; > + return kctl; > + } > + return NULL; > +} > +#endif > + > /* add/replace a new kcontrol object; call with card->controls_rwsem locked */ > static int __snd_ctl_add_replace(struct snd_card *card, > struct snd_kcontrol *kcontrol, > @@ -382,24 +423,38 @@ static int __snd_ctl_add_replace(struct snd_card *card, > if (id.index > UINT_MAX - kcontrol->count) > return -EINVAL; > > - old = snd_ctl_find_id(card, &id); > - if (!old) { > - if (mode == CTL_REPLACE) > - return -EINVAL; > - } else { > - if (mode == CTL_ADD_EXCLUSIVE) { > - dev_err(card->dev, > - "control %i:%i:%i:%s:%i is already present\n", > - id.iface, id.device, id.subdevice, id.name, > - id.index); > - return -EBUSY; > - } > +#ifdef CONFIG_SND_CTL_HASHTABLE > + snprintf(snd_ctl_string, strlen(kcontrol->id.name) + 6, "%s%d%d%d", > + kcontrol->id.name, kcontrol->id.iface, kcontrol->id.device, > + kcontrol->id.subdevice); > > - err = snd_ctl_remove(card, old); > - if (err < 0) > - return err; > - } > + kcontrol->knametoint = snd_ctl_strtoint(snd_ctl_string); > + if (kcontrol->knametoint < 0) > + return -EINVAL; > + > + old = snd_ctl_hash_check(card, kcontrol->knametoint); > + if (old) { > +#endif > + old = snd_ctl_find_id(card, &id); > + if (!old) { > + if (mode == CTL_REPLACE) > + return -EINVAL; > + } else { > + if (mode == CTL_ADD_EXCLUSIVE) { > + dev_err(card->dev, > + "control %i:%i:%i:%s:%i is already present\n", > + id.iface, id.device, id.subdevice, id.name, > + id.index); > + return -EBUSY; > + } > > + err = snd_ctl_remove(card, old); > + if (err < 0) > + return err; > + } > +#ifdef CONFIG_SND_CTL_HASHTABLE > + } > +#endif > if (snd_ctl_find_hole(card, kcontrol->count) < 0) > return -ENOMEM; > > @@ -410,7 +465,10 @@ static int __snd_ctl_add_replace(struct snd_card *card, > > for (idx = 0; idx < kcontrol->count; idx++) > snd_ctl_notify_one(card, SNDRV_CTL_EVENT_MASK_ADD, kcontrol, idx); > - > + > +#ifdef CONFIG_SND_CTL_HASHTABLE > + hash_add(card->ctl_htable, &kcontrol->hnode, kcontrol->knametoint); > +#endif > return 0; > } > > diff --git a/sound/core/init.c b/sound/core/init.c > index 31ba7024e3ad..fda38b2137ee 100644 > --- a/sound/core/init.c > +++ b/sound/core/init.c > @@ -284,6 +284,9 @@ static int snd_card_init(struct snd_card *card, struct device *parent, > INIT_LIST_HEAD(&card->ctl_files); > spin_lock_init(&card->files_lock); > INIT_LIST_HEAD(&card->files_list); > +#ifdef CONFIG_SND_CTL_HASHTABLE > + hash_init(card->ctl_htable); > +#endif > mutex_init(&card->memory_mutex); > #ifdef CONFIG_PM > init_waitqueue_head(&card->power_sleep); > diff --git a/sound/soc/Kconfig b/sound/soc/Kconfig > index 5dcf77af07af..0eb18f8ee6fd 100644 > --- a/sound/soc/Kconfig > +++ b/sound/soc/Kconfig > @@ -58,6 +58,15 @@ config SND_SOC_TOPOLOGY_KUNIT_TEST > config SND_SOC_ACPI > tristate > > +config SND_CTL_HASHTABLE > + bool "Add SND CTL hashtable" > + help > + This enables hash table in sound card for kcontrols. The traditional way is > + traversing the linked list of controls and compare each exsiting control with > + the new kcontrol to find out whether there are duplicate kcontrols, which will > + consumes much time during bootup. Enable this will use hash table instead of > + linked list to check new kcontrol and reduce much time for sound card registration. > + > # All the supported SoCs > source "sound/soc/adi/Kconfig" > source "sound/soc/amd/Kconfig" > -- > 2.17.1 >