ALSA: control: Locking optimization and cleanups
Subsystems (4)
cover ALSA: control: Locking optimization and cleanups
Hi,
this is a series of patches to reduce the locking at control elements look-up and cleanups of APIs. It was inspired from the discussion thread about the mutex inversions of Cirrus codec driver, but this version goes further, dropping the locking in normal use cases.
The snd_ctl_find_*_unlocked() API functions are completely dropped, as there is no real use any longer.
The patch touches / removes ASoC code to drop and replace *_locked() API calls.
Takashi
===
Takashi Iwai (3):
ALSA: control: Rename ctl_files_rwlock to controls_rwlock
ALSA: control: Optimize locking for look-up
ASoC: Drop snd_soc_card_get_kcontrol_locked()
include/sound/control.h | 25 ------ include/sound/core.h | 2 +- include/sound/soc-card.h | 2 - sound/core/control.c | 151 ++++++++++++++---------------------- sound/core/control_compat.c | 2 +- sound/core/control_led.c | 2 +- sound/core/init.c | 2 +- sound/core/oss/mixer_oss.c | 10 +-- sound/soc/codecs/cs35l45.c | 2 +- sound/soc/fsl/fsl_xcvr.c | 2 +- sound/soc/soc-card-test.c | 57 -------------- sound/soc/soc-card.c | 10 --- 12 files changed, 70 insertions(+), 197 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/3 ALSA: control: Rename ctl_files_rwlock to controls_rwlock
We'll re-use the existing rwlock for the protection of control list lookup, too, and now rename it to a more generic name.
This is a preliminary change, only the rename of the struct field here, no functional changes.
--- include/sound/core.h | 2 +- sound/core/control.c | 10 +++++----- sound/core/init.c | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-)
include/sound/core.h +1 -1
@@ -99,7 +99,7 @@ struct snd_card {struct device *ctl_dev;/* control device */ unsigned int last_numid;/* last used numeric ID */ struct rw_semaphore controls_rwsem;/* controls lock (list and values) */ -rwlock_t ctl_files_rwlock;/* ctl_files list lock */ +rwlock_t controls_rwlock;/* lock for ctl_files list */ int controls_count;/* count of all controls */ size_t user_ctl_alloc_size;// current memory allocation by user controls. struct list_head controls;/* all controls for this card */
sound/core/control.c +5 -5
@@ -79,7 +79,7 @@ static int snd_ctl_open(struct inode *inode, struct file *file) ctl->preferred_subdevice[i] =-1 ; ctl->pid =get_pid (task_pid (current)); file->private_data = ctl; - scoped_guard(write_lock_irqsave, &card->ctl_files_rwlock) + scoped_guard(write_lock_irqsave, &card->controls_rwlock)list_add_tail (&ctl->list, &card->ctl_files);snd_card_unref (card);return 0 ;
@@ -117,7 +117,7 @@ static int snd_ctl_release(struct inode *inode, struct file *file) file->private_data =NULL ; card = ctl->card; - scoped_guard(write_lock_irqsave, &card->ctl_files_rwlock) + scoped_guard(write_lock_irqsave, &card->controls_rwlock)list_del (&ctl->list); scoped_guard(rwsem_write, &card->controls_rwsem) {
@@ -157,7 +157,7 @@ void snd_ctl_notify(struct snd_card *card, unsigned int mask,if (card->shutdown)return ; -guard (read_lock_irqsave)(&card->ctl_files_rwlock); +guard (read_lock_irqsave)(&card->controls_rwlock);#if IS_ENABLED (CONFIG_SND_MIXER_OSS ) card->mixer_oss_change_count++;#endif
@@ -2191,7 +2191,7 @@ int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type)struct snd_ctl_file *kctl;int subdevice =-1 ; -guard (read_lock_irqsave)(&card->ctl_files_rwlock); +guard (read_lock_irqsave)(&card->controls_rwlock);list_for_each_entry (kctl, &card->ctl_files, list) {if (kctl->pid ==task_pid (current)) { subdevice = kctl->preferred_subdevice[type];
@@ -2341,7 +2341,7 @@ static int snd_ctl_dev_disconnect(struct snd_device *device)struct snd_card *card = device->device_data;struct snd_ctl_file *ctl; - scoped_guard(read_lock_irqsave, &card->ctl_files_rwlock) { + scoped_guard(read_lock_irqsave, &card->controls_rwlock) {list_for_each_entry (ctl, &card->ctl_files, list) {wake_up (&ctl->change_sleep);snd_kill_fasync (ctl->fasync,SIGIO ,POLL_ERR );
sound/core/init.c +1 -1
@@ -315,7 +315,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent, card->module = module;INIT_LIST_HEAD (&card->devices);init_rwsem (&card->controls_rwsem); -rwlock_init (&card->ctl_files_rwlock); +rwlock_init (&card->controls_rwlock);INIT_LIST_HEAD (&card->controls);INIT_LIST_HEAD (&card->ctl_files);#ifdef CONFIG_SND_CTL_FAST_LOOKUP
Signature · 1 line
2.43.0
PATCH 2/3 ALSA: control: Optimize locking for look-up
In reply to Takashi IwaiFor a fast look-up of a control element via either numid or name matching (enabled via CONFIG_SND_CTL_FAST_LOOKUP), a locking isn't needed at all thanks to Xarray. OTOH, the locking is still needed for a slow linked-list traversal, and that's rather a rare case.
In this patch, we reduce the use of locking at snd_ctl_find_*() API functions, and switch from controls_rwsem to controls_rwlock for avoiding unnecessary lock inversions. This also resulted in a nice cleanup, as *_unlocked() version of snd_ctl_find_*() APIs can be dropped.
snd_ctl_find_id_mixer_unlocked() is still left just as an alias of snd_ctl_find_id_mixer(), since soc-card.c has a wrapper and there are several users. Once after converting there, we can remove it later.
--- include/sound/control.h | 22 +----- include/sound/core.h | 2 +- sound/core/control.c | 141 ++++++++++++++---------------------- sound/core/control_compat.c | 2 +- sound/core/control_led.c | 2 +- sound/core/oss/mixer_oss.c | 10 +-- 6 files changed, 64 insertions(+), 115 deletions(-)
include/sound/control.h +2 -20
@@ -140,9 +140,7 @@ int snd_ctl_remove_id(struct snd_card * card, struct snd_ctl_elem_id *id);int snd_ctl_rename_id (struct snd_card * card,struct snd_ctl_elem_id *src_id,struct snd_ctl_elem_id *dst_id);void snd_ctl_rename (struct snd_card *card,struct snd_kcontrol *kctl,const char *name);int snd_ctl_activate_id (struct snd_card *card,struct snd_ctl_elem_id *id,int active); -struct snd_kcontrol *snd_ctl_find_numid_locked (struct snd_card *card,unsigned int numid);struct snd_kcontrol *snd_ctl_find_numid (struct snd_card *card,unsigned int numid); -struct snd_kcontrol *snd_ctl_find_id_locked (struct snd_card *card,const struct snd_ctl_elem_id *id);struct snd_kcontrol *snd_ctl_find_id (struct snd_card *card,const struct snd_ctl_elem_id *id);/**
@@ -167,27 +165,11 @@ snd_ctl_find_id_mixer(struct snd_card *card, const char *name)return snd_ctl_find_id (card, &id); } -/** -* snd_ctl_find_id_mixer_locked - find the control instance with the given name string -* @card: the card instance -* @name: the name string -* -* Finds the control instance with the given name and -* @SNDRV_CTL_ELEM_IFACE_MIXER. Other fields are set to zero. -* -* This is merely a wrapper to snd_ctl_find_id_locked(). -* The caller must down card->controls_rwsem before calling this function. -* -* Return: The pointer of the instance if found, or %NULL if not. -*/ +/* to be removed */ static inline struct snd_kcontrol *snd_ctl_find_id_mixer_locked (struct snd_card *card,const char *name) { -struct snd_ctl_elem_id id = {}; - - id.iface =SNDRV_CTL_ELEM_IFACE_MIXER ; -strscpy (id.name, name,sizeof (id.name)); -return snd_ctl_find_id_locked (card, &id); +return snd_ctl_find_id_mixer (card, name); }int snd_ctl_create (struct snd_card *card);
include/sound/core.h +1 -1
@@ -99,7 +99,7 @@ struct snd_card {struct device *ctl_dev;/* control device */ unsigned int last_numid;/* last used numeric ID */ struct rw_semaphore controls_rwsem;/* controls lock (list and values) */ -rwlock_t controls_rwlock;/* lock for ctl_files list */ +rwlock_t controls_rwlock;/* lock for lookup and ctl_files list */ int controls_count;/* count of all controls */ size_t user_ctl_alloc_size;// current memory allocation by user controls. struct list_head controls;/* all controls for this card */
sound/core/control.c +54 -87
258 lines not on this page · raw message
sound/core/control_compat.c +1 -1
@@ -168,7 +168,7 @@ static int get_ctl_type(struct snd_card *card, struct snd_ctl_elem_id *id,int err;guard (rwsem_read)(&card->controls_rwsem); - kctl =snd_ctl_find_id_locked (card, id); + kctl =snd_ctl_find_id (card, id);if (!kctl)return -ENOENT ; info =kzalloc (sizeof (*info),GFP_KERNEL );
sound/core/control_led.c +1 -1
@@ -254,7 +254,7 @@ static int snd_ctl_led_set_id(int card_number, struct snd_ctl_elem_id *id,if (!card)return -ENXIO ;guard (rwsem_write)(&card->controls_rwsem); - kctl =snd_ctl_find_id_locked (card, id); + kctl =snd_ctl_find_id (card, id);if (!kctl)return -ENOENT ; ioff =snd_ctl_get_ioff (kctl, id);
sound/core/oss/mixer_oss.c +5 -5
@@ -510,7 +510,7 @@ static struct snd_kcontrol *snd_mixer_oss_test_id(struct snd_mixer_oss *mixer, c id.iface =SNDRV_CTL_ELEM_IFACE_MIXER ;strscpy (id.name, name,sizeof (id.name)); id.index = index; -return snd_ctl_find_id_locked (card, &id); +return snd_ctl_find_id (card, &id); } static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,
@@ -526,7 +526,7 @@ static void snd_mixer_oss_get_volume1_vol(struct snd_mixer_oss_file *fmixer,if (numid ==ID_UNKNOWN )return ;guard (rwsem_read)(&card->controls_rwsem); - kctl =snd_ctl_find_numid_locked (card, numid); + kctl =snd_ctl_find_numid (card, numid);if (!kctl)return ; uinfo =kzalloc (sizeof (*uinfo),GFP_KERNEL );
@@ -559,7 +559,7 @@ static void snd_mixer_oss_get_volume1_sw(struct snd_mixer_oss_file *fmixer,if (numid ==ID_UNKNOWN )return ;guard (rwsem_read)(&card->controls_rwsem); - kctl =snd_ctl_find_numid_locked (card, numid); + kctl =snd_ctl_find_numid (card, numid);if (!kctl)return ; uinfo =kzalloc (sizeof (*uinfo),GFP_KERNEL );
@@ -619,7 +619,7 @@ static void snd_mixer_oss_put_volume1_vol(struct snd_mixer_oss_file *fmixer,if (numid ==ID_UNKNOWN )return ;guard (rwsem_read)(&card->controls_rwsem); - kctl =snd_ctl_find_numid_locked (card, numid); + kctl =snd_ctl_find_numid (card, numid);if (!kctl)return ; uinfo =kzalloc (sizeof (*uinfo),GFP_KERNEL );
@@ -656,7 +656,7 @@ static void snd_mixer_oss_put_volume1_sw(struct snd_mixer_oss_file *fmixer,if (numid ==ID_UNKNOWN )return ;guard (rwsem_read)(&card->controls_rwsem); - kctl =snd_ctl_find_numid_locked (card, numid); + kctl =snd_ctl_find_numid (card, numid);if (!kctl)return ; uinfo =kzalloc (sizeof (*uinfo),GFP_KERNEL );
Signature · 1 line
2.43.0
PATCH 3/3 ASoC: Drop snd_soc_card_get_kcontrol_locked()
In reply to Takashi IwaiThe recent cleanup in ALSA control core made no difference between snd_ctl_find_id_mixer() and snd_ctl_find_id_mixer_locked(), and the latter is to be dropped. The only user of the left API was ASoC, and that's snd_soc_card_get_kcontrol_locked().
This patch drops snd_soc_card_get_kcontrol_locked() and rewrites its users to call snd_soc_card_get_kcontrol() instead. The test of the API became superfluous, hence dropped as well.
As all callers of snd_ctl_find_id_mixer_locked() are gone, snd_ctl_find_id_mixer_locked() is finally dropped, too.
--- include/sound/control.h | 7 ----- include/sound/soc-card.h | 2 -- sound/soc/codecs/cs35l45.c | 2 +- sound/soc/fsl/fsl_xcvr.c | 2 +- sound/soc/soc-card-test.c | 57 -------------------------------------- sound/soc/soc-card.c | 10 ------- 6 files changed, 2 insertions(+), 78 deletions(-)
include/sound/control.h +0 -7
@@ -165,13 +165,6 @@ snd_ctl_find_id_mixer(struct snd_card *card, const char *name)return snd_ctl_find_id (card, &id); } -/* to be removed */ -static inline struct snd_kcontrol * -snd_ctl_find_id_mixer_locked (struct snd_card *card,const char *name) -{ -return snd_ctl_find_id_mixer (card, name); -} -int snd_ctl_create (struct snd_card *card);int snd_ctl_register_ioctl (snd_kctl_ioctl_func_t fcn);
include/sound/soc-card.h +0 -2
@@ -30,8 +30,6 @@ static inline void snd_soc_card_mutex_unlock(struct snd_soc_card *card)struct snd_kcontrol *snd_soc_card_get_kcontrol (struct snd_soc_card *soc_card,const char *name); -struct snd_kcontrol *snd_soc_card_get_kcontrol_locked (struct snd_soc_card *soc_card, -const char *name);int snd_soc_card_jack_new (struct snd_soc_card *card,const char *id,int type,struct snd_soc_jack *jack); int snd_soc_card_jack_new_pins(struct snd_soc_card *card, const char *id,
sound/soc/codecs/cs35l45.c +1 -1
@@ -184,7 +184,7 @@ static int cs35l45_activate_ctl(struct snd_soc_component *component,else snprintf (name,SNDRV_CTL_ELEM_ID_NAME_MAXLEN ,"%s" , ctl_name); - kcontrol =snd_soc_card_get_kcontrol_locked (component->card, name); + kcontrol =snd_soc_card_get_kcontrol (component->card, name);if (!kcontrol) {dev_err (component->dev,"Can't find kcontrol %s\n" , name);return -EINVAL ;
sound/soc/fsl/fsl_xcvr.c +1 -1
@@ -186,7 +186,7 @@ static int fsl_xcvr_activate_ctl(struct snd_soc_dai *dai, const char *name,lockdep_assert_held (&card->snd_card->controls_rwsem); - kctl =snd_soc_card_get_kcontrol_locked (card, name); + kctl =snd_soc_card_get_kcontrol (card, name);if (kctl ==NULL )return -ENOENT ;
sound/soc/soc-card-test.c +0 -57
69 lines not on this page · raw message
sound/soc/soc-card.c +0 -10
@@ -29,16 +29,6 @@ static inline int _soc_card_ret(struct snd_soc_card *card,return ret; } -struct snd_kcontrol *snd_soc_card_get_kcontrol_locked (struct snd_soc_card *soc_card, -const char *name) -{ -if (unlikely (!name)) -return NULL ; - -return snd_ctl_find_id_mixer_locked (soc_card->snd_card, name); -} -EXPORT_SYMBOL_GPL (snd_soc_card_get_kcontrol_locked); -struct snd_kcontrol *snd_soc_card_get_kcontrol (struct snd_soc_card *soc_card,const char *name) {
Signature · 1 line
2.43.0
Re: [PATCH 3/3] ASoC: Drop snd_soc_card_get_kcontrol_locked()
Quoted 4 lines · Takashi Iwai
The recent cleanup in ALSA control core made no difference between
snd_ctl_find_id_mixer()andsnd_ctl_find_id_mixer_locked(), and the latter is to be dropped. The only user of the left API was ASoC, and that'ssnd_soc_card_get_kcontrol_locked().