[PATCH] ALSA: pcm: Optimize cpu_latency_qos_*() calls

From: Takashi Iwai

Date: Fri Oct 02 2026 - 11:04:55 EST


We have a few cpu_latency_qos_*() calls in PCM callbacks, but they
seem suboptimal in a few aspects:
- We call them at each time when the hw_params changed, but explicitly
do remove and (re-)add; this could be
cpu_latency_qos_update_request(), instead
- The setup of cpu_latency_qos_*() doesn't have to be in hw_params at
all; it's not the place where the actual parameters are set up, but
rather better at the prepare callback.
- The hw_free calls cpu_latency_qos_remove_request() unconditionally,
which may be poking a WARNING when it wasn't queued.

For addressing the issues above, this patch cleans up and reorganize
the cpu_latency_qos_*() calls in the PCM core by a couple helper
functions. Now, they are set up at prepare callback, and cleared at
hw_free and release callbacks via the new helpers.

Signed-off-by: Takashi Iwai <tiwai@xxxxxxx>
---
sound/core/pcm_native.c | 35 +++++++++++++++++++++++++----------
1 file changed, 25 insertions(+), 10 deletions(-)

diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 5abe16a03b34..6efaebc7f8b4 100644
--- a/sound/core/pcm_native.c
+++ b/sound/core/pcm_native.c
@@ -778,11 +778,32 @@ EXPORT_SYMBOL_GPL(snd_pcm_runtime_buffer_set_silence);
#define is_oss_stream(substream) false
#endif

+static void pcm_qos_req_clear(struct snd_pcm_substream *substream)
+{
+ if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
+ cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
+}
+
+static void pcm_qos_req_update(struct snd_pcm_substream *substream, int usecs)
+{
+ if (usecs < 0) {
+ pcm_qos_req_clear(substream);
+ return;
+ }
+
+ if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
+ cpu_latency_qos_update_request(&substream->latency_pm_qos_req,
+ usecs);
+ else
+ cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
+ usecs);
+}
+
static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_pcm_runtime *runtime;
- int err, usecs;
+ int err;
unsigned int bits;
snd_pcm_uframes_t frames;

@@ -889,12 +910,6 @@ static int snd_pcm_hw_params(struct snd_pcm_substream *substream,
snd_pcm_timer_resolution_change(substream);
snd_pcm_set_state(substream, SNDRV_PCM_STATE_SETUP);

- if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
- cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
- usecs = period_to_usecs(runtime);
- if (usecs >= 0)
- cpu_latency_qos_add_request(&substream->latency_pm_qos_req,
- usecs);
err = 0;
_error:
if (err) {
@@ -971,7 +986,7 @@ static int snd_pcm_hw_free(struct snd_pcm_substream *substream)
goto unlock;
result = do_hw_free(substream);
snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
- cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
+ pcm_qos_req_clear(substream);
unlock:
snd_pcm_buffer_access_unlock(runtime);
return result;
@@ -2005,6 +2020,7 @@ static int snd_pcm_do_prepare(struct snd_pcm_substream *substream,
err = substream->ops->prepare(substream);
if (err < 0)
return err;
+ pcm_qos_req_update(substream, period_to_usecs(substream->runtime));
return snd_pcm_do_reset(substream, state);
}

@@ -2795,8 +2811,7 @@ void snd_pcm_release_substream(struct snd_pcm_substream *substream)
substream->ops->close(substream);
substream->hw_opened = 0;
}
- if (cpu_latency_qos_request_active(&substream->latency_pm_qos_req))
- cpu_latency_qos_remove_request(&substream->latency_pm_qos_req);
+ pcm_qos_req_clear(substream);
if (substream->pcm_release) {
substream->pcm_release(substream);
substream->pcm_release = NULL;
--
2.55.0