ALSA: dummy: Improvements of timer backend calls
cover ALSA: dummy: Improvements of timer backend calls
Here is a small patch series for ALSA dummy driver to improve the handling of systimer and hrtimer callbacks. This is mainly a cleanup and a hardening for the cases where we miss the synchronizations after stopping the PCM.
Takashi
===
Takashi Iwai (3):
ALSA: dummy: Use the standard PCM sync_stop callback for hrtimer
ALSA: dummy: Implement sync_stop for systimer, too
ALSA: dummy: Properly shutdown the systimer at freeing
sound/drivers/dummy.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/3 ALSA: dummy: Use the standard PCM sync_stop callback for hrtimer
The hrtimer backend code in ALSA dummy driver calls explicitly the synchronization of hrtimer cancel from prepare and free callbacks, and this is exactly what the standard PCM sync_stop callback serves for.
Replace the open-code with the standard PCM sync_stop callback.
--- sound/drivers/dummy.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
sound/drivers/dummy.c +16 -3
@@ -100,6 +100,7 @@ struct dummy_timer_ops {int (*prepare)(struct snd_pcm_substream *);int (*start)(struct snd_pcm_substream *);int (*stop)(struct snd_pcm_substream *); +int (*sync_stop)(struct snd_pcm_substream *);snd_pcm_uframes_t (*pointer)(struct snd_pcm_substream *); };
@@ -407,9 +408,12 @@ static int dummy_hrtimer_stop(struct snd_pcm_substream *substream)return 0 ; } -static inline void dummy_hrtimer_sync (struct dummy_hrtimer_pcm *dpcm) +static int dummy_hrtimer_sync_stop (struct snd_pcm_substream *substream) { +struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; +hrtimer_cancel (&dpcm->timer); +return 0 ; } static snd_pcm_uframes_t
@@ -435,7 +439,6 @@ static int dummy_hrtimer_prepare(struct snd_pcm_substream *substream)long sec;unsigned long nsecs; -dummy_hrtimer_sync (dpcm); period = runtime->period_size; rate = runtime->rate; sec = period / rate;
@@ -463,7 +466,7 @@ static int dummy_hrtimer_create(struct snd_pcm_substream *substream)static void dummy_hrtimer_free (struct snd_pcm_substream *substream) {struct dummy_hrtimer_pcm *dpcm = substream->runtime->private_data; -dummy_hrtimer_sync (dpcm); +kfree (dpcm); }
@@ -473,6 +476,7 @@ static const struct dummy_timer_ops dummy_hrtimer_ops = { .prepare = dummy_hrtimer_prepare, .start = dummy_hrtimer_start, .stop = dummy_hrtimer_stop, + .sync_stop = dummy_hrtimer_sync_stop, .pointer = dummy_hrtimer_pointer, };
@@ -495,6 +499,13 @@ static int dummy_pcm_trigger(struct snd_pcm_substream *substream, int cmd)return -EINVAL ; } +static int dummy_pcm_sync_stop (struct snd_pcm_substream *substream) +{ +if (get_dummy_ops (substream)->sync_stop) +return get_dummy_ops (substream)->sync_stop (substream); +return 0 ; +} +static int dummy_pcm_prepare (struct snd_pcm_substream *substream) {return get_dummy_ops (substream)->prepare (substream);
@@ -646,6 +657,7 @@ static const struct snd_pcm_ops dummy_pcm_ops = { .hw_params = dummy_pcm_hw_params, .prepare = dummy_pcm_prepare, .trigger = dummy_pcm_trigger, + .sync_stop = dummy_pcm_sync_stop, .pointer = dummy_pcm_pointer, };
@@ -655,6 +667,7 @@ static const struct snd_pcm_ops dummy_pcm_ops_no_buf = { .hw_params = dummy_pcm_hw_params, .prepare = dummy_pcm_prepare, .trigger = dummy_pcm_trigger, + .sync_stop = dummy_pcm_sync_stop, .pointer = dummy_pcm_pointer, .copy = dummy_pcm_copy, .fill_silence = dummy_pcm_silence,
Signature · 1 line
2.55.0
PATCH 2/3 ALSA: dummy: Implement sync_stop for systimer, too
In reply to Takashi IwaiThe systimer backend invokes timer_delete() at stopping the PCM, but it misses its synchronization, which might lead to concurrent changes or releases at PCM prepare or free. Use the sync_stop callback to assure the synchronization of timer deletion.
--- sound/drivers/dummy.c | 9 +++++++++ 1 file changed, 9 insertions(+)
sound/drivers/dummy.c +9 -0
@@ -286,6 +286,14 @@ static int dummy_systimer_stop(struct snd_pcm_substream *substream)return 0 ; } +static int dummy_systimer_sync_stop (struct snd_pcm_substream *substream) +{ +struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; + +timer_delete_sync (&dpcm->timer); +return 0 ; +} +static int dummy_systimer_prepare (struct snd_pcm_substream *substream) {struct snd_pcm_runtime *runtime = substream->runtime;
@@ -351,6 +359,7 @@ static const struct dummy_timer_ops dummy_systimer_ops = { .prepare = dummy_systimer_prepare, .start = dummy_systimer_start, .stop = dummy_systimer_stop, + .sync_stop = dummy_systimer_sync_stop, .pointer = dummy_systimer_pointer, };
Signature · 1 line
2.55.0
PATCH 3/3 ALSA: dummy: Properly shutdown the systimer at freeing
In reply to Takashi IwaiAdd timer_shutdown_sync() call at the free callback for systimer backend, in order to make sure that we can release the resources.
This is only for hardening, and there shouldn't be any actual issue that requires this change for now.
--- sound/drivers/dummy.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
sound/drivers/dummy.c +4 -1
@@ -350,7 +350,10 @@ static int dummy_systimer_create(struct snd_pcm_substream *substream)static void dummy_systimer_free (struct snd_pcm_substream *substream) { -kfree (substream->runtime->private_data); +struct dummy_systimer_pcm *dpcm = substream->runtime->private_data; + +timer_shutdown_sync (&dpcm->timer); +kfree (dpcm); }static const struct dummy_timer_ops dummy_systimer_ops = {
Signature · 1 line
2.55.0