ALSA: introduce simple refcount helper functions
cover ALSA: introduce simple refcount helper functions
Hi,
I noticed that we have open-code for managing the same refcount+sync patterns in various places, so here is a cleanup for them to use the standard helper functions.
No functional changes, but merely code refactoring.
Takashi
===
Takashi Iwai (5):
ALSA: Add simple refcount helper functions
ALSA: core: Use the new helper for the power refcount
ALSA: hda: Use the new helper for PCM instance refcount
ALSA: aloop: Use the new helper for stop-count refcount
ALSA: usb-audio: Use the new helper for shutdown refcount
include/sound/core.h | 31 +++++++++++++++++++++++++------ include/sound/hda_codec.h | 10 ++++++---- sound/core/init.c | 3 +-- sound/core/misc.c | 24 ++++++++++++++++++++++++ sound/drivers/aloop.c | 13 +++++-------- sound/hda/common/bind.c | 3 +-- sound/hda/common/codec.c | 14 +++----------- sound/usb/card.c | 14 +++++--------- sound/usb/usbaudio.h | 5 +++-- 9 files changed, 73 insertions(+), 44 deletions(-)
Signature · 1 line
2.54.0
PATCH 1/5 ALSA: Add simple refcount helper functions
There are many open-code to manage the same pattern for refcount + wakeup sync at closing. Let's provide the common helper functions to replace the open-code.
- The recount is kept in struct snd_refcount, where it's initialized by snd_refcount_init().
- The user can simply reference or unreference via snd_refcount_get() and snd_refcount_put() functions
- The user can wait for the all usages gone by snd_refcount_sync()
Note that here we use atomic_t instead of refcount_t since the current users allow reusing the refcount after sync again. The design of refcount_t prevents exactly this behavior, so it doesn't fit.
--- include/sound/core.h | 21 +++++++++++++++++++++ sound/core/misc.c | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
include/sound/core.h +21 -0
@@ -75,6 +75,27 @@ struct snd_device {#define snd_device (n) list_entry(n, struct snd_device, list) +/* +* A simple reference counter with a wait queue; +* typically used for usage counts, and you can synchronize at finishing +* via snd_refcount_sync(), which is woken up when the refcount reaches to +* zero again. +*/ +struct snd_refcount { +atomic_t count; +wait_queue_head_t waiter; +}; + +void snd_refcount_init (struct snd_refcount *ref); + +static inline void snd_refcount_get (struct snd_refcount *ref) +{ +atomic_inc (&ref->count); +} + +void snd_refcount_put (struct snd_refcount *ref); +void snd_refcount_sync (struct snd_refcount *ref); +/* main structure for soundcard */ struct snd_card {
sound/core/misc.c +24 -0
@@ -174,3 +174,27 @@ void snd_fasync_free(struct snd_fasync *fasync)kfree (fasync); }EXPORT_SYMBOL_GPL (snd_fasync_free); + +/* +* generic refcount helper +*/ + +void snd_refcount_init (struct snd_refcount *ref) +{ +atomic_set (&ref->count,0 ); +init_waitqueue_head (&ref->waiter); +} +EXPORT_SYMBOL_GPL (snd_refcount_init); + +void snd_refcount_put (struct snd_refcount *ref) +{ +if (atomic_dec_and_test (&ref->count)) +wake_up (&ref->waiter); +} +EXPORT_SYMBOL_GPL (snd_refcount_put); + +void snd_refcount_sync (struct snd_refcount *ref) +{ +wait_event (ref->waiter, !atomic_read (&ref->count)); +} +EXPORT_SYMBOL_GPL (snd_refcount_sync);
Signature · 1 line
2.54.0
PATCH 2/5 ALSA: core: Use the new helper for the power refcount
In reply to Takashi IwaiReplace the open code for managing the power refcount in the snd_card object with the new helper functions.
Only a code cleanup, no functional changes.
--- include/sound/core.h | 10 ++++------ sound/core/init.c | 3 +-- 2 files changed, 5 insertions(+), 8 deletions(-)
include/sound/core.h +4 -6
@@ -160,9 +160,8 @@ struct snd_card {#ifdef CONFIG_PM unsigned int power_state;/* power state */ -atomic_t power_ref;wait_queue_head_t power_sleep; -wait_queue_head_t power_ref_sleep; +struct snd_refcount power_ref;#endif #if IS_ENABLED (CONFIG_SND_MIXER_OSS )
@@ -197,7 +196,7 @@ static inline void snd_power_change_state(struct snd_card *card, unsigned int st*/ static inline void snd_power_ref (struct snd_card *card) { -atomic_inc (&card->power_ref); +snd_refcount_get (&card->power_ref); }/**
@@ -206,8 +205,7 @@ static inline void snd_power_ref(struct snd_card *card)*/ static inline void snd_power_unref (struct snd_card *card) { -if (atomic_dec_and_test (&card->power_ref)) -wake_up (&card->power_ref_sleep); +snd_refcount_put (&card->power_ref); }/**
@@ -219,7 +217,7 @@ static inline void snd_power_unref(struct snd_card *card)*/ static inline void snd_power_sync_ref (struct snd_card *card) { -wait_event (card->power_ref_sleep, !atomic_read (&card->power_ref)); +snd_refcount_sync (&card->power_ref); }/* init.c */
sound/core/init.c +1 -2
@@ -328,8 +328,7 @@ static int snd_card_init(struct snd_card *card, struct device *parent,mutex_init (&card->memory_mutex);#ifdef CONFIG_PM init_waitqueue_head (&card->power_sleep); -init_waitqueue_head (&card->power_ref_sleep); -atomic_set (&card->power_ref,0 ); +snd_refcount_init (&card->power_ref);#endif init_waitqueue_head (&card->remove_sleep); card->sync_irq =-1 ;
Signature · 1 line
2.54.0
PATCH 3/5 ALSA: hda: Use the new helper for PCM instance refcount
In reply to Takashi IwaiHD-audio core driver has some open-code for managing the refcount for PCM instances, and it can be replaced gracefully with the new helpers. Only a code cleanup, no functional changes.
--- include/sound/hda_codec.h | 10 ++++++---- sound/hda/common/bind.c | 3 +-- sound/hda/common/codec.c | 14 +++----------- 3 files changed, 10 insertions(+), 17 deletions(-)
include/sound/hda_codec.h +6 -4
@@ -188,8 +188,7 @@ struct hda_codec {/* PCM to create, set by hda_codec_ops.build_pcms callback */ struct list_head pcm_list_head; -refcount_t pcm_ref; -wait_queue_head_t remove_sleep; +struct snd_refcount pcm_ref;/* codec specific info */ void *spec;
@@ -439,9 +438,12 @@ void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec);static inline void snd_hda_codec_pcm_get (struct hda_pcm *pcm) { -refcount_inc (&pcm->codec->pcm_ref); +snd_refcount_get (&pcm->codec->pcm_ref); +} +static inline void snd_hda_codec_pcm_put (struct hda_pcm *pcm) +{ +snd_refcount_put (&pcm->codec->pcm_ref); } -void snd_hda_codec_pcm_put (struct hda_pcm *pcm); int snd_hda_codec_prepare(struct hda_codec *codec, struct hda_pcm_stream *hinfo,
sound/hda/common/bind.c +1 -2
@@ -162,8 +162,7 @@ static int hda_codec_driver_remove(struct device *dev)snd_hda_codec_disconnect_pcms (codec);snd_hda_jack_tbl_disconnect (codec); -if (!refcount_dec_and_test (&codec->pcm_ref)) -wait_event (codec->remove_sleep, !refcount_read (&codec->pcm_ref)); +snd_refcount_sync (&codec->pcm_ref);snd_power_sync_ref (codec->bus->card);if (driver->ops->remove)
sound/hda/common/codec.c +3 -11
@@ -689,13 +689,6 @@ get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)/* * PCM device */ -void snd_hda_codec_pcm_put (struct hda_pcm *pcm) -{ -if (refcount_dec_and_test (&pcm->codec->pcm_ref)) -wake_up (&pcm->codec->remove_sleep); -} -EXPORT_SYMBOL_GPL (snd_hda_codec_pcm_put); -struct hda_pcm *snd_hda_codec_pcm_new (struct hda_codec *codec,const char *fmt, ...) {
@@ -716,7 +709,7 @@ struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec, }list_add_tail (&pcm->list, &codec->pcm_list_head); -refcount_inc (&codec->pcm_ref); +snd_hda_codec_pcm_get (pcm);return pcm; }EXPORT_SYMBOL_GPL (snd_hda_codec_pcm_new);
@@ -787,7 +780,7 @@ void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)remove_conn_list (codec);snd_hdac_regmap_exit (&codec->core); codec->configured =0 ; -refcount_set (&codec->pcm_ref,1 );/* reset refcount */ +snd_refcount_init (&codec->pcm_ref);/* reset refcount */ }EXPORT_SYMBOL_GPL (snd_hda_codec_cleanup_for_unbind);
@@ -927,8 +920,7 @@ snd_hda_codec_device_init(struct hda_bus *bus, unsigned int codec_addr,INIT_LIST_HEAD (&codec->conn_list);INIT_LIST_HEAD (&codec->pcm_list_head);INIT_DELAYED_WORK (&codec->jackpoll_work, hda_jackpoll_work); -refcount_set (&codec->pcm_ref,1 ); -init_waitqueue_head (&codec->remove_sleep); +snd_refcount_init (&codec->pcm_ref);return codec; }
Signature · 1 line
2.54.0
PATCH 4/5 ALSA: aloop: Use the new helper for stop-count refcount
In reply to Takashi IwaiReplace the open-code for managing the stop-count refcount with the new helpers.
Only a code cleanup, no functional changes.
--- sound/drivers/aloop.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
sound/drivers/aloop.c +5 -8
@@ -100,8 +100,7 @@ struct loopback_cable {spinlock_t lock;struct loopback_pcm *streams[2 ];/* in-flight peer stops running outside cable->lock */ -atomic_t stop_count; -wait_queue_head_t stop_wait; +struct snd_refcount stop_count;struct snd_pcm_hardware hw;/* flags */ unsigned int valid;
@@ -371,7 +370,7 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)return -EIO ; else if (cruntime->state == SNDRV_PCM_STATE_RUNNING) {/* close must not free the peer runtime below */ -atomic_inc (&cable->stop_count); +snd_refcount_get (&cable->stop_count); stop_capture = true; } }
@@ -404,8 +403,7 @@ static int loopback_check_format(struct loopback_cable *cable, int stream)if (stop_capture) {snd_pcm_stop (dpcm_capt->substream,SNDRV_PCM_STATE_DRAINING ); -if (atomic_dec_and_test (&cable->stop_count)) -wake_up (&cable->stop_wait); +snd_refcount_put (&cable->stop_count); }return 0 ;
@@ -1070,7 +1068,7 @@ static void free_cable(struct snd_pcm_substream *substream) }/* Pair with the stop_count increment in loopback_check_format(). */ -wait_event (cable->stop_wait, !atomic_read (&cable->stop_count)); +snd_refcount_sync (&cable->stop_count);if (other_alive)return ;
@@ -1274,8 +1272,7 @@ static int loopback_open(struct snd_pcm_substream *substream) goto unlock; }spin_lock_init (&cable->lock); -atomic_set (&cable->stop_count,0 ); -init_waitqueue_head (&cable->stop_wait); +snd_refcount_init (&cable->stop_count); cable->hw = loopback_pcm_hardware;if (loopback->timer_source) cable->ops = &loopback_snd_timer_ops;
Signature · 1 line
2.54.0
PATCH 5/5 ALSA: usb-audio: Use the new helper for shutdown refcount
In reply to Takashi IwaiReplace the open-code for managing the shutdown refcount with the new helpers.
Only a code cleanup, no functional changes.
--- sound/usb/card.c | 14 +++++--------- sound/usb/usbaudio.h | 5 +++-- 2 files changed, 8 insertions(+), 11 deletions(-)
sound/usb/card.c +5 -9
@@ -769,7 +769,6 @@ static int snd_usb_audio_create(struct usb_interface *intf, chip = card->private_data;mutex_init (&chip->mutex); -init_waitqueue_head (&chip->shutdown_wait); chip->index = idx; chip->dev = dev; chip->card = card;
@@ -778,7 +777,7 @@ static int snd_usb_audio_create(struct usb_interface *intf, chip->autoclock = autoclock; chip->lowlatency = lowlatency;atomic_set (&chip->active,1 );/* avoid autopm during probing */ -atomic_set (&chip->usage_count,0 ); +snd_refcount_init (&chip->usage_count);atomic_set (&chip->shutdown,0 ); chip->usb_id = usb_id;
@@ -1107,8 +1106,7 @@ static bool __usb_audio_disconnect(struct usb_interface *intf,/* wait until all pending tasks done; * they are protected by snd_usb_lock_shutdown() */ -wait_event (chip->shutdown_wait, - !atomic_read (&chip->usage_count)); +snd_refcount_sync (&chip->usage_count);snd_card_disconnect (card);/* release the pcm resources */ list_for_each_entry (as, &chip->pcm_list, list) {
@@ -1166,7 +1164,7 @@ int snd_usb_lock_shutdown(struct snd_usb_audio *chip) {int err; -atomic_inc (&chip->usage_count); +snd_refcount_get (&chip->usage_count);if (atomic_read (&chip->shutdown)) { err = -EIO ; goto error;
@@ -1177,8 +1175,7 @@ int snd_usb_lock_shutdown(struct snd_usb_audio *chip)return 0 ; error: -if (atomic_dec_and_test (&chip->usage_count)) -wake_up (&chip->shutdown_wait); +snd_refcount_put (&chip->usage_count);return err; }EXPORT_SYMBOL_GPL (snd_usb_lock_shutdown);
@@ -1187,8 +1184,7 @@ EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown);void snd_usb_unlock_shutdown (struct snd_usb_audio *chip) {snd_usb_autosuspend (chip); -if (atomic_dec_and_test (&chip->usage_count)) -wake_up (&chip->shutdown_wait); +snd_refcount_put (&chip->usage_count); }EXPORT_SYMBOL_GPL (snd_usb_unlock_shutdown);
sound/usb/usbaudio.h +3 -2
@@ -7,6 +7,8 @@* Copyright (c) 2002 by Takashi Iwai <[email protected]> */ +#include <sound/core.h> +/* handling of USB vendor/product ID pairs as 32-bit numbers */ #define USB_ID (vendor, product) (((unsigned int)(vendor) << 16) | (product))#define USB_ID_VENDOR (id) ((id) >> 16)
@@ -41,8 +43,7 @@ struct snd_usb_audio {unsigned int system_suspend;atomic_t active;atomic_t shutdown; -atomic_t usage_count; -wait_queue_head_t shutdown_wait; +struct snd_refcount usage_count;unsigned int quirk_flags; unsigned int need_delayed_register:1; /* warn for delayed registration */int num_interfaces;
Signature · 1 line
2.54.0