mirror of
https://github.com/team-infusion-developers/android_kernel_samsung_msm8976.git
synced 2024-11-01 02:21:16 +00:00
BACKPORT: ALSA: timer: Handle disconnection more safely
[ Upstream commit 230323dac060123c340cf75997971145a42661ee ] Currently ALSA timer device doesn't take the disconnection into account very well; it merely unlinks the timer device at disconnection callback but does nothing else. Because of this, when an application accessing the timer device is disconnected, it may release the resource before actually closed. In most cases, it results in a warning message indicating a leftover timer instance like: ALSA: timer xxxx is busy? But basically this is an open race. This patch tries to address it. The strategy is like other ALSA devices: namely, - Manage card's refcount at each open/close - Wake up the pending tasks at disconnection - Check the shutdown flag appropriately at each possible call Note that this patch has one ugly hack to handle the wakeup of pending tasks. It'd be cleaner to introduce a new disconnect op to snd_timer_instance ops. But since it would lead to internal ABI breakage and it eventually increase my own work when backporting to stable kernels, I took a different path to implement locally in timer.c. A cleanup patch will follow at next for 4.5 kernel. Bug: 37240993 Change-Id: I05c7f0e7d28b63fc343091f800ceae9ec2afe4a4 Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=109431 Cc: <stable@vger.kernel.org> # v3.15+ Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Sasha Levin <sasha.levin@oracle.com> Signed-off-by: Siqi Lin <siqilin@google.com> (cherry picked from commit 230323dac060123c340cf75997971145a42661ee)
This commit is contained in:
parent
f514ea6147
commit
dada20bfa9
1 changed files with 48 additions and 0 deletions
|
@ -65,6 +65,7 @@ struct snd_timer_user {
|
||||||
int qtail;
|
int qtail;
|
||||||
int qused;
|
int qused;
|
||||||
int queue_size;
|
int queue_size;
|
||||||
|
bool disconnected;
|
||||||
struct snd_timer_read *queue;
|
struct snd_timer_read *queue;
|
||||||
struct snd_timer_tread *tqueue;
|
struct snd_timer_tread *tqueue;
|
||||||
spinlock_t qlock;
|
spinlock_t qlock;
|
||||||
|
@ -289,6 +290,9 @@ int snd_timer_open(struct snd_timer_instance **ti,
|
||||||
mutex_unlock(®ister_mutex);
|
mutex_unlock(®ister_mutex);
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
/* take a card refcount for safe disconnection */
|
||||||
|
if (timer->card)
|
||||||
|
get_device(timer->card->card_dev);
|
||||||
timeri->slave_class = tid->dev_sclass;
|
timeri->slave_class = tid->dev_sclass;
|
||||||
timeri->slave_id = slave_id;
|
timeri->slave_id = slave_id;
|
||||||
if (list_empty(&timer->open_list_head) && timer->hw.open)
|
if (list_empty(&timer->open_list_head) && timer->hw.open)
|
||||||
|
@ -358,6 +362,9 @@ int snd_timer_close(struct snd_timer_instance *timeri)
|
||||||
}
|
}
|
||||||
spin_unlock(&timer->lock);
|
spin_unlock(&timer->lock);
|
||||||
spin_unlock_irq(&slave_active_lock);
|
spin_unlock_irq(&slave_active_lock);
|
||||||
|
/* release a card refcount for safe disconnection */
|
||||||
|
if (timer->card)
|
||||||
|
put_device(timer->card->card_dev);
|
||||||
mutex_unlock(®ister_mutex);
|
mutex_unlock(®ister_mutex);
|
||||||
}
|
}
|
||||||
out:
|
out:
|
||||||
|
@ -478,6 +485,8 @@ int snd_timer_start(struct snd_timer_instance *timeri, unsigned int ticks)
|
||||||
timer = timeri->timer;
|
timer = timeri->timer;
|
||||||
if (timer == NULL)
|
if (timer == NULL)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
return -ENODEV;
|
||||||
spin_lock_irqsave(&timer->lock, flags);
|
spin_lock_irqsave(&timer->lock, flags);
|
||||||
if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
|
if (timeri->flags & (SNDRV_TIMER_IFLG_RUNNING |
|
||||||
SNDRV_TIMER_IFLG_START)) {
|
SNDRV_TIMER_IFLG_START)) {
|
||||||
|
@ -529,6 +538,10 @@ static int _snd_timer_stop(struct snd_timer_instance *timeri, int event)
|
||||||
}
|
}
|
||||||
list_del_init(&timeri->ack_list);
|
list_del_init(&timeri->ack_list);
|
||||||
list_del_init(&timeri->active_list);
|
list_del_init(&timeri->active_list);
|
||||||
|
if (timer->card && timer->card->shutdown) {
|
||||||
|
spin_unlock_irqrestore(&timer->lock, flags);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
|
if ((timeri->flags & SNDRV_TIMER_IFLG_RUNNING) &&
|
||||||
!(--timer->running)) {
|
!(--timer->running)) {
|
||||||
timer->hw.stop(timer);
|
timer->hw.stop(timer);
|
||||||
|
@ -589,6 +602,8 @@ int snd_timer_continue(struct snd_timer_instance *timeri)
|
||||||
timer = timeri->timer;
|
timer = timeri->timer;
|
||||||
if (! timer)
|
if (! timer)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
return -ENODEV;
|
||||||
spin_lock_irqsave(&timer->lock, flags);
|
spin_lock_irqsave(&timer->lock, flags);
|
||||||
if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
|
if (timeri->flags & SNDRV_TIMER_IFLG_RUNNING) {
|
||||||
result = -EBUSY;
|
result = -EBUSY;
|
||||||
|
@ -657,6 +672,9 @@ static void snd_timer_tasklet(unsigned long arg)
|
||||||
unsigned long resolution, ticks;
|
unsigned long resolution, ticks;
|
||||||
unsigned long flags;
|
unsigned long flags;
|
||||||
|
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
return;
|
||||||
|
|
||||||
spin_lock_irqsave(&timer->lock, flags);
|
spin_lock_irqsave(&timer->lock, flags);
|
||||||
/* now process all callbacks */
|
/* now process all callbacks */
|
||||||
while (!list_empty(&timer->sack_list_head)) {
|
while (!list_empty(&timer->sack_list_head)) {
|
||||||
|
@ -697,6 +715,9 @@ void snd_timer_interrupt(struct snd_timer * timer, unsigned long ticks_left)
|
||||||
if (timer == NULL)
|
if (timer == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
return;
|
||||||
|
|
||||||
spin_lock_irqsave(&timer->lock, flags);
|
spin_lock_irqsave(&timer->lock, flags);
|
||||||
|
|
||||||
/* remember the current resolution */
|
/* remember the current resolution */
|
||||||
|
@ -909,11 +930,28 @@ static int snd_timer_dev_register(struct snd_device *dev)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* just for reference in snd_timer_dev_disconnect() below */
|
||||||
|
static void snd_timer_user_ccallback(struct snd_timer_instance *timeri,
|
||||||
|
int event, struct timespec *tstamp,
|
||||||
|
unsigned long resolution);
|
||||||
|
|
||||||
static int snd_timer_dev_disconnect(struct snd_device *device)
|
static int snd_timer_dev_disconnect(struct snd_device *device)
|
||||||
{
|
{
|
||||||
struct snd_timer *timer = device->device_data;
|
struct snd_timer *timer = device->device_data;
|
||||||
|
struct snd_timer_instance *ti;
|
||||||
|
|
||||||
mutex_lock(®ister_mutex);
|
mutex_lock(®ister_mutex);
|
||||||
list_del_init(&timer->device_list);
|
list_del_init(&timer->device_list);
|
||||||
|
/* wake up pending sleepers */
|
||||||
|
list_for_each_entry(ti, &timer->open_list_head, open_list) {
|
||||||
|
/* FIXME: better to have a ti.disconnect() op */
|
||||||
|
if (ti->ccallback == snd_timer_user_ccallback) {
|
||||||
|
struct snd_timer_user *tu = ti->callback_data;
|
||||||
|
|
||||||
|
tu->disconnected = true;
|
||||||
|
wake_up(&tu->qchange_sleep);
|
||||||
|
}
|
||||||
|
}
|
||||||
mutex_unlock(®ister_mutex);
|
mutex_unlock(®ister_mutex);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -924,6 +962,8 @@ void snd_timer_notify(struct snd_timer *timer, int event, struct timespec *tstam
|
||||||
unsigned long resolution = 0;
|
unsigned long resolution = 0;
|
||||||
struct snd_timer_instance *ti, *ts;
|
struct snd_timer_instance *ti, *ts;
|
||||||
|
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
return;
|
||||||
if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
|
if (! (timer->hw.flags & SNDRV_TIMER_HW_SLAVE))
|
||||||
return;
|
return;
|
||||||
if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
|
if (snd_BUG_ON(event < SNDRV_TIMER_EVENT_MSTART ||
|
||||||
|
@ -1084,6 +1124,8 @@ static void snd_timer_proc_read(struct snd_info_entry *entry,
|
||||||
|
|
||||||
mutex_lock(®ister_mutex);
|
mutex_lock(®ister_mutex);
|
||||||
list_for_each_entry(timer, &snd_timer_list, device_list) {
|
list_for_each_entry(timer, &snd_timer_list, device_list) {
|
||||||
|
if (timer->card && timer->card->shutdown)
|
||||||
|
continue;
|
||||||
switch (timer->tmr_class) {
|
switch (timer->tmr_class) {
|
||||||
case SNDRV_TIMER_CLASS_GLOBAL:
|
case SNDRV_TIMER_CLASS_GLOBAL:
|
||||||
snd_iprintf(buffer, "G%i: ", timer->tmr_device);
|
snd_iprintf(buffer, "G%i: ", timer->tmr_device);
|
||||||
|
@ -1917,6 +1959,10 @@ static ssize_t snd_timer_user_read(struct file *file, char __user *buffer,
|
||||||
|
|
||||||
remove_wait_queue(&tu->qchange_sleep, &wait);
|
remove_wait_queue(&tu->qchange_sleep, &wait);
|
||||||
|
|
||||||
|
if (tu->disconnected) {
|
||||||
|
err = -ENODEV;
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (signal_pending(current)) {
|
if (signal_pending(current)) {
|
||||||
err = -ERESTARTSYS;
|
err = -ERESTARTSYS;
|
||||||
goto _error;
|
goto _error;
|
||||||
|
@ -1962,6 +2008,8 @@ static unsigned int snd_timer_user_poll(struct file *file, poll_table * wait)
|
||||||
mask = 0;
|
mask = 0;
|
||||||
if (tu->qused)
|
if (tu->qused)
|
||||||
mask |= POLLIN | POLLRDNORM;
|
mask |= POLLIN | POLLRDNORM;
|
||||||
|
if (tu->disconnected)
|
||||||
|
mask |= POLLERR;
|
||||||
|
|
||||||
return mask;
|
return mask;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue