mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ALSA: pcm: free struct snd_pcm only after the last open file is closed
@ 2026-12-02  2:32 Hanxiang Xu
  0 siblings, 0 replies; only message in thread
From: Hanxiang Xu @ 2026-12-02  2:32 UTC (permalink / raw)
  To: Takashi Iwai, Jaroslav Kysela; +Cc: linux-sound, linux-kernel

When a sub-driver removes its pcm from a live card with
snd_device_free(), snd_pcm_dev_disconnect() does not wait for open
files and snd_pcm_free() immediately kfrees every substream and
struct snd_pcm itself.  Open files keep pcm_file->substream and
substream->pcm pointing at the freed memory, so an in-flight ioctl
dereferences it and the delayed close() runs snd_pcm_release() on it,
ending in snd_pcm_release_substream() -> substream->ops->close.

Give struct snd_pcm a kref: the card device entry holds the initial
reference (dropped by snd_pcm_free()), snd_pcm_attach_substream()
takes one per open and the tail of snd_pcm_release() (and the open
failure path) drops it.  The whole free body moves behind the kref
release, so a concurrent snd_device_free() can no longer free a pcm
that still has open files.

Cc: <stable@vger.kernel.org>
Signed-off-by: Hanxiang Xu <xuhx@hust.edu.cn>
---
 include/sound/pcm.h | 1 +
 sound/core/pcm.c | 17 +++++++++++++----
 sound/core/pcm_native.c | 2 ++
 3 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/include/sound/pcm.h b/include/sound/pcm.h
index 1cf511f8d880ea53e9309721ea7efaa625304b22..65cae3349d374bce0cf2d5269796c879753023af 100644
@@ -541,6 +541,7 @@
 	char id[64];
 	char name[80];
 	struct snd_pcm_str streams[2];
+	struct kref ref; /* freed once the device entry and all files are gone */
 	struct mutex open_mutex;
 	wait_queue_head_t open_wait;
 	void *private_data;
diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index 6c8274414c226543e8044c348e90388e038beb05..c59c5e2fa34fa949e579021d2ffcf5ecb51be3f6 100644
@@ -723,6 +723,7 @@
 	pcm->card = card;
 	pcm->device = device;
 	pcm->internal = internal;
+	kref_init(&pcm->ref);
 	mutex_init(&pcm->open_mutex);
 	init_waitqueue_head(&pcm->open_wait);
 	INIT_LIST_HEAD(&pcm->list);
@@ -857,10 +858,10 @@ static void snd_pcm_free_stream(struct snd_pcm_str * pstr)
 #define pcm_call_notify(pcm, call) do {} while (0)
 #endif
 
-static int snd_pcm_free(struct snd_pcm *pcm)
-{
-	if (!pcm)
-		return 0;
+static void snd_pcm_free_up(struct kref *ref)
+{
+	struct snd_pcm *pcm = container_of(ref, struct snd_pcm, ref);
+
 	if (!pcm->internal) {
 		scoped_guard(mutex, &register_mutex)
 			pcm_call_notify(pcm, n_unregister);
@@ -871,6 +872,13 @@ static int snd_pcm_free(struct snd_pcm *pcm)
 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_PLAYBACK]);
 	snd_pcm_free_stream(&pcm->streams[SNDRV_PCM_STREAM_CAPTURE]);
 	kfree(pcm);
+}
+
+static int snd_pcm_free(struct snd_pcm *pcm)
+{
+	if (!pcm)
+		return 0;
+	kref_put(&pcm->ref, snd_pcm_free_up);
 	return 0;
 }
 
@@ -981,6 +989,7 @@ static int snd_pcm_dev_free(struct snd_device *device)
 	substream->f_flags = file->f_flags;
 	substream->pid = get_pid(task_pid(current));
 	pstr->substream_opened++;
+	kref_get(&pcm->ref);
 	*rsubstream = substream;
 #ifdef CONFIG_SND_PCM_XRUN_DEBUG
 	substream->xrun_counter = 0;
diff --git a/sound/core/pcm_native.c b/sound/core/pcm_native.c
index 897975e43a9bd634fb11e2a086378a4142a90f64..4fef3020328fb706a4e2328ea4ad6b7df4e0e7f0 100644
@@ -3037,6 +3037,7 @@
 
       __error:
 	module_put(pcm->card->module);
+	kref_put(&pcm->ref, snd_pcm_free_up);
       __error2:
       	snd_card_file_remove(pcm->card, file);
       __error1:
@@ -3065,6 +3066,7 @@ static int snd_pcm_release(struct inode *inode, struct file *file)
 	wake_up(&pcm->open_wait);
 	module_put(pcm->card->module);
 	snd_card_file_remove(pcm->card, file);
+	kref_put(&pcm->ref, snd_pcm_free_up);
 	return 0;
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-12-02  3:07 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  2:32 [PATCH] ALSA: pcm: free struct snd_pcm only after the last open file is closed Hanxiang Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®