Skip to content

Commit 6a34367

Browse files
committed
ALSA: timer: Fix possible race at assigning a timer instance
When a new timer instance is created and assigned to the active link in snd_timer_open(), the caller still doesn't (can't) set its callback and callback data. In both the user-timer and the sequencer-timer code, they do manually set up the callbacks after calling snd_timer_open(). This has a potential risk of race when the timer instance is added to the already running timer target, as the callback might get triggered during setting up the callback itself. This patch tries to address it by changing the API usage slightly: - An empty timer instance is created at first via the new function snd_timer_instance_new(). This object isn't linked to the timer list yet. - The caller sets up the callbacks and others stuff for the new timer instance. - The caller invokes snd_timer_open() with this instance, so that it's linked to the target timer. For closing, do similarly: - Call snd_timer_close(). This unlinks the timer instance from the timer list. - Free the timer instance via snd_timer_instance_free() after that. Signed-off-by: Takashi Iwai <tiwai@suse.de> Link: https://lore.kernel.org/r/20191107192008.32331-4-tiwai@suse.de Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 33bbb8a commit 6a34367

3 files changed

Lines changed: 63 additions & 56 deletions

File tree

‎include/sound/timer.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ int snd_timer_global_new(char *id, int device, struct snd_timer **rtimer);
118118
int snd_timer_global_free(struct snd_timer *timer);
119119
int snd_timer_global_register(struct snd_timer *timer);
120120

121-
int snd_timer_open(struct snd_timer_instance **ti, char *owner, struct snd_timer_id *tid, unsigned int slave_id);
121+
struct snd_timer_instance *snd_timer_instance_new(const char *owner);
122+
void snd_timer_instance_free(struct snd_timer_instance *timeri);
123+
int snd_timer_open(struct snd_timer_instance *timeri, struct snd_timer_id *tid, unsigned int slave_id);
122124
void snd_timer_close(struct snd_timer_instance *timeri);
123125
unsigned long snd_timer_resolution(struct snd_timer_instance *timeri);
124126
int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks);

‎sound/core/seq/seq_timer.c‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,13 @@ int snd_seq_timer_open(struct snd_seq_queue *q)
272272
return -EINVAL;
273273
if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
274274
tmr->alsa_id.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
275-
err = snd_timer_open(&t, str, &tmr->alsa_id, q->queue);
275+
t = snd_timer_instance_new(str);
276+
if (!t)
277+
return -ENOMEM;
278+
t->callback = snd_seq_timer_interrupt;
279+
t->callback_data = q;
280+
t->flags |= SNDRV_TIMER_IFLG_AUTO;
281+
err = snd_timer_open(t, &tmr->alsa_id, q->queue);
276282
if (err < 0 && tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_SLAVE) {
277283
if (tmr->alsa_id.dev_class != SNDRV_TIMER_CLASS_GLOBAL ||
278284
tmr->alsa_id.device != SNDRV_TIMER_GLOBAL_SYSTEM) {
@@ -282,16 +288,14 @@ int snd_seq_timer_open(struct snd_seq_queue *q)
282288
tid.dev_sclass = SNDRV_TIMER_SCLASS_SEQUENCER;
283289
tid.card = -1;
284290
tid.device = SNDRV_TIMER_GLOBAL_SYSTEM;
285-
err = snd_timer_open(&t, str, &tid, q->queue);
291+
err = snd_timer_open(t, &tid, q->queue);
286292
}
287293
}
288294
if (err < 0) {
289295
pr_err("ALSA: seq fatal error: cannot create timer (%i)\n", err);
296+
snd_timer_instance_free(t);
290297
return err;
291298
}
292-
t->callback = snd_seq_timer_interrupt;
293-
t->callback_data = q;
294-
t->flags |= SNDRV_TIMER_IFLG_AUTO;
295299
spin_lock_irq(&tmr->lock);
296300
tmr->timeri = t;
297301
spin_unlock_irq(&tmr->lock);
@@ -310,8 +314,10 @@ int snd_seq_timer_close(struct snd_seq_queue *q)
310314
t = tmr->timeri;
311315
tmr->timeri = NULL;
312316
spin_unlock_irq(&tmr->lock);
313-
if (t)
317+
if (t) {
314318
snd_timer_close(t);
319+
snd_timer_instance_free(t);
320+
}
315321
return 0;
316322
}
317323

‎sound/core/timer.c‎

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ static void snd_timer_reschedule(struct snd_timer * timer, unsigned long ticks_l
8888

8989
/*
9090
* create a timer instance with the given owner string.
91-
* when timer is not NULL, increments the module counter
9291
*/
93-
static struct snd_timer_instance *snd_timer_instance_new(char *owner,
94-
struct snd_timer *timer)
92+
struct snd_timer_instance *snd_timer_instance_new(const char *owner)
9593
{
9694
struct snd_timer_instance *timeri;
95+
9796
timeri = kzalloc(sizeof(*timeri), GFP_KERNEL);
9897
if (timeri == NULL)
9998
return NULL;
@@ -108,15 +107,20 @@ static struct snd_timer_instance *snd_timer_instance_new(char *owner,
108107
INIT_LIST_HEAD(&timeri->slave_list_head);
109108
INIT_LIST_HEAD(&timeri->slave_active_head);
110109

111-
timeri->timer = timer;
112-
if (timer && !try_module_get(timer->module)) {
110+
return timeri;
111+
}
112+
EXPORT_SYMBOL(snd_timer_instance_new);
113+
114+
void snd_timer_instance_free(struct snd_timer_instance *timeri)
115+
{
116+
if (timeri) {
117+
if (timeri->private_free)
118+
timeri->private_free(timeri);
113119
kfree(timeri->owner);
114120
kfree(timeri);
115-
return NULL;
116121
}
117-
118-
return timeri;
119122
}
123+
EXPORT_SYMBOL(snd_timer_instance_free);
120124

121125
/*
122126
* find a timer instance from the given timer id
@@ -236,12 +240,11 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
236240
* open a timer instance
237241
* when opening a master, the slave id must be here given.
238242
*/
239-
int snd_timer_open(struct snd_timer_instance **ti,
240-
char *owner, struct snd_timer_id *tid,
243+
int snd_timer_open(struct snd_timer_instance *timeri,
244+
struct snd_timer_id *tid,
241245
unsigned int slave_id)
242246
{
243247
struct snd_timer *timer;
244-
struct snd_timer_instance *timeri = NULL;
245248
struct device *card_dev_to_put = NULL;
246249
int err;
247250

@@ -259,22 +262,14 @@ int snd_timer_open(struct snd_timer_instance **ti,
259262
err = -EBUSY;
260263
goto unlock;
261264
}
262-
timeri = snd_timer_instance_new(owner, NULL);
263-
if (!timeri) {
264-
err = -ENOMEM;
265-
goto unlock;
266-
}
267265
timeri->slave_class = tid->dev_sclass;
268266
timeri->slave_id = tid->device;
269267
timeri->flags |= SNDRV_TIMER_IFLG_SLAVE;
270268
list_add_tail(&timeri->open_list, &snd_timer_slave_list);
271269
num_slaves++;
272270
err = snd_timer_check_slave(timeri);
273-
if (err < 0) {
274-
snd_timer_close_locked(timeri, &card_dev_to_put);
275-
timeri = NULL;
276-
}
277-
goto unlock;
271+
if (err < 0)
272+
goto close;
278273
}
279274

280275
/* open a master instance */
@@ -304,45 +299,40 @@ int snd_timer_open(struct snd_timer_instance **ti,
304299
err = -EBUSY;
305300
goto unlock;
306301
}
307-
timeri = snd_timer_instance_new(owner, timer);
308-
if (!timeri) {
309-
err = -ENOMEM;
302+
if (!try_module_get(timer->module)) {
303+
err = -EBUSY;
310304
goto unlock;
311305
}
312306
/* take a card refcount for safe disconnection */
313-
if (timer->card)
307+
if (timer->card) {
314308
get_device(&timer->card->card_dev);
315-
timeri->slave_class = tid->dev_sclass;
316-
timeri->slave_id = slave_id;
309+
card_dev_to_put = &timer->card->card_dev;
310+
}
317311

318312
if (list_empty(&timer->open_list_head) && timer->hw.open) {
319313
err = timer->hw.open(timer);
320314
if (err) {
321-
kfree(timeri->owner);
322-
kfree(timeri);
323-
timeri = NULL;
324-
325-
if (timer->card)
326-
card_dev_to_put = &timer->card->card_dev;
327315
module_put(timer->module);
328316
goto unlock;
329317
}
330318
}
331319

320+
timeri->timer = timer;
321+
timeri->slave_class = tid->dev_sclass;
322+
timeri->slave_id = slave_id;
323+
332324
list_add_tail(&timeri->open_list, &timer->open_list_head);
333325
timer->num_instances++;
334326
err = snd_timer_check_master(timeri);
335-
if (err < 0) {
327+
close:
328+
if (err < 0)
336329
snd_timer_close_locked(timeri, &card_dev_to_put);
337-
timeri = NULL;
338-
}
339330

340331
unlock:
341332
mutex_unlock(&register_mutex);
342333
/* put_device() is called after unlock for avoiding deadlock */
343-
if (card_dev_to_put)
334+
if (err < 0 && card_dev_to_put)
344335
put_device(card_dev_to_put);
345-
*ti = timeri;
346336
return err;
347337
}
348338
EXPORT_SYMBOL(snd_timer_open);
@@ -363,9 +353,11 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
363353
spin_unlock_irq(&timer->lock);
364354
}
365355

366-
list_del(&timeri->open_list);
367-
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
368-
num_slaves--;
356+
if (!list_empty(&timeri->open_list)) {
357+
list_del_init(&timeri->open_list);
358+
if (timeri->flags & SNDRV_TIMER_IFLG_SLAVE)
359+
num_slaves--;
360+
}
369361

370362
/* force to stop the timer */
371363
snd_timer_stop(timeri);
@@ -384,6 +376,7 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
384376
/* remove slave links */
385377
spin_lock_irq(&slave_active_lock);
386378
spin_lock(&timer->lock);
379+
timeri->timer = NULL;
387380
list_for_each_entry_safe(slave, tmp, &timeri->slave_list_head,
388381
open_list) {
389382
list_move_tail(&slave->open_list, &snd_timer_slave_list);
@@ -401,11 +394,6 @@ static void snd_timer_close_locked(struct snd_timer_instance *timeri,
401394
timer = NULL;
402395
}
403396

404-
if (timeri->private_free)
405-
timeri->private_free(timeri);
406-
kfree(timeri->owner);
407-
kfree(timeri);
408-
409397
if (timer) {
410398
if (list_empty(&timer->open_list_head) && timer->hw.close)
411399
timer->hw.close(timer);
@@ -1480,8 +1468,10 @@ static int snd_timer_user_release(struct inode *inode, struct file *file)
14801468
tu = file->private_data;
14811469
file->private_data = NULL;
14821470
mutex_lock(&tu->ioctl_lock);
1483-
if (tu->timeri)
1471+
if (tu->timeri) {
14841472
snd_timer_close(tu->timeri);
1473+
snd_timer_instance_free(tu->timeri);
1474+
}
14851475
mutex_unlock(&tu->ioctl_lock);
14861476
kfree(tu->queue);
14871477
kfree(tu->tqueue);
@@ -1722,6 +1712,7 @@ static int snd_timer_user_tselect(struct file *file,
17221712
tu = file->private_data;
17231713
if (tu->timeri) {
17241714
snd_timer_close(tu->timeri);
1715+
snd_timer_instance_free(tu->timeri);
17251716
tu->timeri = NULL;
17261717
}
17271718
if (copy_from_user(&tselect, _tselect, sizeof(tselect))) {
@@ -1731,9 +1722,11 @@ static int snd_timer_user_tselect(struct file *file,
17311722
sprintf(str, "application %i", current->pid);
17321723
if (tselect.id.dev_class != SNDRV_TIMER_CLASS_SLAVE)
17331724
tselect.id.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
1734-
err = snd_timer_open(&tu->timeri, str, &tselect.id, current->pid);
1735-
if (err < 0)
1725+
tu->timeri = snd_timer_instance_new(str);
1726+
if (!tu->timeri) {
1727+
err = -ENOMEM;
17361728
goto __err;
1729+
}
17371730

17381731
tu->timeri->flags |= SNDRV_TIMER_IFLG_FAST;
17391732
tu->timeri->callback = tu->tread
@@ -1742,6 +1735,12 @@ static int snd_timer_user_tselect(struct file *file,
17421735
tu->timeri->callback_data = (void *)tu;
17431736
tu->timeri->disconnect = snd_timer_user_disconnect;
17441737

1738+
err = snd_timer_open(tu->timeri, &tselect.id, current->pid);
1739+
if (err < 0) {
1740+
snd_timer_instance_free(tu->timeri);
1741+
tu->timeri = NULL;
1742+
}
1743+
17451744
__err:
17461745
return err;
17471746
}

0 commit comments

Comments
 (0)