From: Hanxiang Xu <xuhx@hust.edu.cn>
To: Takashi Iwai <tiwai@suse.com>, Jaroslav Kysela <perex@perex.cz>
Cc: linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] ALSA: pcm: free struct snd_pcm only after the last open file is closed
Date: Tue, 02 Dec 2026 10:32:00 +0800 [thread overview]
Message-ID: <20261202103200.3-1-xuhx@hust.edu.cn> (raw)
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, ®ister_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
reply other threads:[~2026-12-02 3:07 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261202103200.3-1-xuhx@hust.edu.cn \
--to=xuhx@hust.edu.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
--cc=perex@perex.cz \
--cc=tiwai@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®