New virtual device snd_disconnected
cover New virtual device snd_disconnected
Hi
Motivation for this came from oopses consistently happening when my usb-soundcard disconnected.
Takashi and me discussed various fixes.
This patch series tries to implement the result of our discussion.
The patch is split into two parts:
1) snd_disconnected_file, virtual device
2) wire up snd_disconnected_file, remove obsolete things
Karsten
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
PATCH 1/2 New virtual device snd_disconnected
snd_disconnected_file, virtual device
On response to "usb disconnect" an usb-soundcard manipulates that snd_card's file's f_ops to only allow release
by calling this virtual device's
void snd_disconnect_file(struct file *file).
After release is actually called, the virtual device instance is freed.
core/disconnected.c renamed from core/_disconnected.c +147 -0
@@ -0,0 +1,147 @@ +/* virtual device +hides a real device's f_ops, +exept for release + +* Copyright (c) by Karsten Wiese <[email protected]> +* +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +*/ + + +#include <linux/fs.h> +#include <linux/poll.h> +#include <linux/module.h> + +struct snd_disconnected_file { +struct file *file; +const struct file_operations *f_op; +struct snd_disconnected_file *next; +}; + +static struct snd_disconnected_file *disconnecting_files; +static struct file_operations snd_disconnect_f_ops; +static DEFINE_MUTEX (mutex); + +void snd_disconnect_file (struct file *file) +{ +struct snd_disconnected_file *df, **_dfs; + df =kmalloc (sizeof (struct snd_disconnected_file ),GFP_ATOMIC ); +if (df ==NULL ) +panic ("Atomic allocation failed for snd_disconnected_file!" ); + + df->file = file; + df->f_op = file->f_op; + df->next =NULL ; + +mutex_lock (&mutex); + _dfs = &disconnecting_files; +while (*_dfs !=NULL ) + _dfs = &(*_dfs)->next; + *_dfs = df; +mutex_unlock (&mutex); + +fops_get (&snd_disconnect_f_ops); + file->f_op = &snd_disconnect_f_ops; +printk (KERN_INFO "%s\n" , __FUNCTION__); +} + + +static loff_t snd_disconnect_llseek (struct file *file,loff_t offset,int orig) +{ +return -ENODEV ; +} + +static ssize_t snd_disconnect_read (struct file *file,char __user *buf, +size_t count,loff_t *offset) +{ +return -ENODEV ; +} + +static ssize_t snd_disconnect_write (struct file *file,const char __user *buf, +size_t count,loff_t *offset) +{ +return -ENODEV ; +} + +static int snd_disconnect_release (struct inode *inode,struct file *file) +{ +struct snd_disconnected_file *df, **_dfs, **__dfs; +int err =0 ; + __dfs = _dfs = &disconnecting_files; + +mutex_lock (&mutex); +while ((df = *_dfs)) +if (df->file == file) { + *__dfs = df->next; +break ; + }else { + __dfs = _dfs; + _dfs = &df->next; + } +mutex_unlock (&mutex); + +if (df) { + err = df->f_op->release (inode, file); +fops_put (df->f_op); +kfree (df); +printk (KERN_INFO "%s\n" , __FUNCTION__); +return err; + } + +panic ("%s(%p, %p) failed!" , __FUNCTION__, inode, file); +} + +static unsigned int snd_disconnect_poll (struct file * file,poll_table * wait) +{ +return POLLERR |POLLNVAL ; +} + +static long snd_disconnect_ioctl (struct file *file, +unsigned int cmd,unsigned long arg) +{ +return -ENODEV ; +} + +static int snd_disconnect_mmap (struct file *file,struct vm_area_struct *vma) +{ +return -ENODEV ; +} + +static int snd_disconnect_fasync (int fd,struct file *file,int on) +{ +return -ENODEV ; +} + +/* + +*/ + +static struct file_operations snd_disconnect_f_ops = +{ + .owner =THIS_MODULE , + .llseek = snd_disconnect_llseek, + .read = snd_disconnect_read, + .write = snd_disconnect_write, + .release = snd_disconnect_release, + .poll = snd_disconnect_poll, + .unlocked_ioctl = snd_disconnect_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = snd_disconnect_ioctl, +#endif + .mmap = snd_disconnect_mmap, + .fasync = snd_disconnect_fasync +};
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Re: [PATCH 0/2] New virtual device snd_disconnected rc3
In reply to Karsten WieseHi
Motivation for this came from oopses consistently happening when my usb-soundcard disconnected.
Takashi and me discussed various fixes and this patch series tries to implement the result of our discussion.
Changes in rc3: kmalloc with GFP_KERNEL, use linux list
The patch is split into two parts:
1) snd_disconnected_file, virtual device
2) wire up snd_disconnected_file, remove obsolete things
Karsten
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
PATCH 1/2 New virtual device snd_disconnected rc3
snd_disconnected_file, virtual device
On response to "usb disconnect" an usb-soundcard manipulates that snd_card's file's f_ops to only allow release
by calling this virtual device's
void snd_disconnect_file(struct file *file).
After release is actually called, the virtual device instance is freed.
core/disconnected.c renamed from core/_disconnected.c +141 -0
@@ -0,0 +1,141 @@ +/* virtual device +hides a real device's f_ops, +except for release + +* Copyright (c) by Karsten Wiese <[email protected]> +* +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +* +*/ + + +#include <linux/fs.h> +#include <linux/list.h> +#include <linux/poll.h> +#include <linux/module.h> + +static DEFINE_MUTEX (mutex); +static LIST_HEAD (disconnected_files); + +struct snd_disconnected_file { +struct file *file; +const struct file_operations *f_op; +struct list_head list; +}; + +static struct file_operations snd_disconnect_f_ops; + +void snd_disconnect_file (struct file *file) +{ +struct snd_disconnected_file *df; + df =kmalloc (sizeof (struct snd_disconnected_file ),GFP_KERNEL ); +if (df ==NULL ) +panic ("Atomic allocation failed for snd_disconnected_file!" ); + + df->file = file; + df->f_op = file->f_op; + +mutex_lock (&mutex); +list_add (&df->list, &disconnected_files); +mutex_unlock (&mutex); + +fops_get (&snd_disconnect_f_ops); + file->f_op = &snd_disconnect_f_ops; +printk (KERN_INFO "%s rc3\n" , __FUNCTION__); +} + + +static loff_t snd_disconnect_llseek (struct file *file,loff_t offset,int orig) +{ +return -ENODEV ; +} + +static ssize_t snd_disconnect_read (struct file *file,char __user *buf, +size_t count,loff_t *offset) +{ +return -ENODEV ; +} + +static ssize_t snd_disconnect_write (struct file *file,const char __user *buf, +size_t count,loff_t *offset) +{ +return -ENODEV ; +} + +static int snd_disconnect_release (struct inode *inode,struct file *file) +{ +struct snd_disconnected_file *df =NULL ; +struct list_head *entry; + +mutex_lock (&mutex); +list_for_each (entry, &disconnected_files) { +struct snd_disconnected_file *_df; + _df = list_entry(entry, struct snd_disconnected_file, list); +if (_df->file == file) { +list_del (entry); + df = _df; +break ; + } + } +mutex_unlock (&mutex); + +if (likely (df !=NULL )) { +int err = df->f_op->release (inode, file); +fops_put (df->f_op); +kfree (df); +printk (KERN_INFO "%s rc3\n" , __FUNCTION__); +return err; + } + +panic ("%s(%p, %p) failed!" , __FUNCTION__, inode, file); +} + +static unsigned int snd_disconnect_poll (struct file * file,poll_table * wait) +{ +return POLLERR |POLLNVAL ; +} + +static long snd_disconnect_ioctl (struct file *file, +unsigned int cmd,unsigned long arg) +{ +return -ENODEV ; +} + +static int snd_disconnect_mmap (struct file *file,struct vm_area_struct *vma) +{ +return -ENODEV ; +} + +static int snd_disconnect_fasync (int fd,struct file *file,int on) +{ +return -ENODEV ; +} + +static struct file_operations snd_disconnect_f_ops = +{ + .owner =THIS_MODULE , + .llseek = snd_disconnect_llseek, + .read = snd_disconnect_read, + .write = snd_disconnect_write, + .release = snd_disconnect_release, + .poll = snd_disconnect_poll, + .unlocked_ioctl = snd_disconnect_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = snd_disconnect_ioctl, +#endif + .mmap = snd_disconnect_mmap, + .fasync = snd_disconnect_fasync +};
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Re: [PATCH 1/2] New virtual device snd_disconnected rc3
At Thu, 5 Oct 2006 15:54:47 +0200,
Quoted 10 lines · Karsten Wiese
snd_disconnected_file, virtual deviceOn response to "usb disconnect" an usb-soundcard manipulates that snd_card's file's
f_opsto only allow release
by calling this virtual device's
voidsnd_disconnect_file(struct file *file).
After release is actually called, the virtual device instance is freed.Signed-off-by Karsten Wiese <[email protected]>
Thanks, the code looks better.
core/disconnected.c line 46
Quoted 6 lines
core/disconnected.c renamed from core/_disconnected.c +6
-0+void snd_disconnect_file (struct file *file) +{ +struct snd_disconnected_file *df; + df =kmalloc (sizeof (struct snd_disconnected_file ),GFP_KERNEL ); +if (df ==NULL ) +panic ("Atomic allocation failed for snd_disconnected_file!" );
The message doesn't match...
Some people prefer "if (!df)" although I myself don't mind at all.
core/disconnected.c line 57
Quoted 11 lines
core/disconnected.c renamed from core/_disconnected.c +11
-0+ + df->file = file; + df->f_op = file->f_op; + +mutex_lock (&mutex); +list_add (&df->list, &disconnected_files); +mutex_unlock (&mutex); + +fops_get (&snd_disconnect_f_ops); + file->f_op = &snd_disconnect_f_ops; +printk (KERN_INFO "%s rc3\n" , __FUNCTION__);
This will be removed in the final version, right?
Quoted 1 line
+}
My only concern is whether the release of the old f_op can be called at the same time during snd_disconnect_file(). If this can happen, it's racy and results in a memory leak.
Could you check whether this scenario could be true? For example, insert ssleep(10) after kmalloc so that you have a chance to close the app manually during disconnect is called. Mmaybe better to add a debug printk in the original release f_ops, too.
Takashi
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Re: [PATCH 1/2] New virtual device snd_disconnected rc3
Am Donnerstag, 5. Oktober 2006 16:13 schrieb Takashi Iwai:
Quoted 15 lines
At Thu, 5 Oct 2006 15:54:47 +0200,
Quoted 10 lines · Karsten Wiese
snd_disconnected_file, virtual deviceOn response to "usb disconnect" an usb-soundcard manipulates that snd_card's file's
f_opsto only allow release
by calling this virtual device's
voidsnd_disconnect_file(struct file *file).
After release is actually called, the virtual device instance is freed.Signed-off-by Karsten Wiese <[email protected]>Thanks, the code looks better.
Quoted 9 lines
core/disconnected.c line 46
Quoted 6 lines
core/disconnected.c renamed from core/_disconnected.c +6
-0+void snd_disconnect_file (struct file *file) +{ +struct snd_disconnected_file *df; + df =kmalloc (sizeof (struct snd_disconnected_file ),GFP_KERNEL ); +if (df ==NULL ) +panic ("Atomic allocation failed for snd_disconnected_file!" );The message doesn't match...
Some people prefer "if (!df)" although I myself don't mind at all.
Me neither. I remember Al Viro preferring the "== NULL".
Quoted 14 lines
core/disconnected.c line 57
Quoted 12 lines
core/disconnected.c renamed from core/_disconnected.c +11
-0+ + df->file = file; + df->f_op = file->f_op; + +mutex_lock (&mutex); +list_add (&df->list, &disconnected_files); +mutex_unlock (&mutex); + +fops_get (&snd_disconnect_f_ops); + file->f_op = &snd_disconnect_f_ops; +printk (KERN_INFO "%s rc3\n" , __FUNCTION__);This will be removed in the final version, right?
right.
Quoted 6 lines
Quoted 2 lines
+}
My only concern is whether the release of the old
f_opcan be called at the same time duringsnd_disconnect_file(). If this can happen, it's racy and results in a memory leak.
Uiuiui, good point!
rc4 will merge with init.c.
Karsten
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
PATCH 1/1 New virtual device snd_disconnected rc4
Hi
Motivation for this came from oopses consistently happening when my usb-soundcard disconnected.
Takashi and me discussed various fixes and this patch implements the result of our discussion.
Changes in rc4: struct snd_disconnected_file merged into
struct snd_monitor_file.
Omit a separate kmalloc/free for disconnect at the cost of sizeof(3 pointers) more in each struct snd_monitor_file.
Move the cleanup of the virtual device to snd_card_remove_file() so it wont be missed in case the release call is already underway, when disconnect starts.
use DEFINE_SPINLOCK(shutdown_mutex); instead of DEFINE_MUTEX(mutex); 'cause code paths are small under that lock.
Merged with init.c.
If agreed upon, this should go to all stable.
Karsten
--------------------------------------------------------------------------
Handle file operations during snd_card disconnects using static file->f_op
Alsa used to kmalloc one file->f_op per file per disconnecting snd_card. This led to oopses sometimes when file->f_op was freed before __fput() finished.
Patch adds a virtual device for disconnect: VDD.
VDD consists of:
LIST_HEAD(shutdown_files)
protected by DEFINE_SPINLOCK(shutdown_mutex)
static struct file_operations snd_shutdown_f_ops
and functions assigned to it
Additions to struct snd_monitor_file
to specify if instance is hidden by VDD or not.
A VDD's instance is
created in snd_card_disconnect() under the card->files_lock. cleaned up in snd_card_file_remove() under the card->files_lock.
include/core.h +2 -2
@@ -89,10 +89,10 @@struct snd_monitor_file {struct file *file;struct snd_monitor_file *next; +const struct file_operations *disconnected_f_op; +struct list_head shutdown_list; }; -struct snd_shutdown_f_ops ;/* define it later in init.c */ -/* main structure for soundcard */ struct snd_card {
core/init.c +55 -40
151 lines not on this page · raw message
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
PATCH 1/1 New virtual device snd_disconnected rc5
New virtual device snd_disconnected rc4
Hi
Motivation for this came from oopses consistently happening when my usb-soundcard disconnected.
Takashi and me discussed various fixes and this patch implements the result of our discussion.
Changes in rc3: kmalloc with GFP_KERNEL, use linux list
Changes in rc4: struct snd_disconnected_file merged into
struct snd_monitor_file.
Omit a separate kmalloc/free for disconnect at the cost of sizeof(3 pointers) more in each struct snd_monitor_file.
Move the cleanup of the virtual device to snd_card_remove_file() so it wont be missed in case the release call is already underway, when disconnect starts.
use DEFINE_SPINLOCK(shutdown_mutex); instead of DEFINE_MUTEX(mutex); 'cause code paths are small under that lock.
Merged with init.c.
Changes in rc5: move fops_put(mfile->disconnected_f_op);
out of the spin_lock.
If agreed upon, this should go to all stable.
Karsten
--------------------------------------------------------------------------
Handle file operations during snd_card disconnects using static file->f_op
Alsa used to kmalloc one file->f_op per file per disconnecting snd_card. This led to oopses sometimes when file->f_op was freed before __fput() finished.
Patch adds a virtual device for disconnect: VDD.
VDD consists of:
LIST_HEAD(shutdown_files)
protected by DEFINE_SPINLOCK(shutdown_mutex)
static struct file_operations snd_shutdown_f_ops
and functions assigned to it
Additions to struct snd_monitor_file
to specify if instance is hidden by VDD or not.
A VDD's instance is
created in snd_card_disconnect() under the card->files_lock. cleaned up in snd_card_file_remove() under the card->files_lock.
include/core.h +2 -2
@@ -89,10 +89,10 @@struct snd_monitor_file {struct file *file;struct snd_monitor_file *next; +const struct file_operations *disconnected_f_op; +struct list_head shutdown_list; }; -struct snd_shutdown_f_ops ;/* define it later in init.c */ -/* main structure for soundcard */ struct snd_card {
core/init.c +55 -40
151 lines not on this page · raw message
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
Re: [PATCH] New virtual device snd_disconnected rc5
At Fri, 6 Oct 2006 14:15:50 +0200,
core/init.c line 36
Quoted 12 lines · Karsten Wiese
core/init.c +1
-4@@ -33,10 +33,10 @@#include <sound/control.h> #include <sound/info.h> -struct snd_shutdown_f_ops { -struct file_operations f_ops; -struct snd_shutdown_f_ops *next; -}; +static DEFINE_SPINLOCK (shutdown_mutex);
Let's rename from mutex to lock if it's spinlock :)
Quoted 7 lines
core/init.c +3
-0+static LIST_HEAD (shutdown_files); + +static struct file_operations snd_shutdown_f_ops;static unsigned int snd_cards_lock;/* locked for registering/using */ struct snd_card *snd_cards[SNDRV_CARDS ];
core/init.c line 207
Quoted 10 lines
core/init.c +7
-0@@ -198,6 +198,28 @@return -ENODEV ; } +static int snd_disconnect_release (struct inode *inode,struct file *file) +{ +struct snd_monitor_file *df =NULL ; +struct list_head *entry; + +spin_lock (&shutdown_mutex); +list_for_each (entry, &shutdown_files) {
You can use list_for_each_entry() to remove one more variable.
Otherwise the code looks OK. Could you fix the above, then I'll merge to the ALSA tree.
Thanks,
Takashi
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
PATCH 1/1 New virtual device snd_disconnected rc6
Hi
Motivation for this came from oopses consistently happening when my usb-soundcard disconnected running 2.6.18-rt4. Takashi and me discussed various fixes and this patch implements the result of our discussion.
Changes in rc3: kmalloc with GFP_KERNEL, use linux list
Changes in rc4: struct snd_disconnected_file merged into
struct snd_monitor_file.
Omit a separate kmalloc/free for disconnect at the cost of sizeof(3 pointers) more in each struct snd_monitor_file.
Move the cleanup of the virtual device to snd_card_remove_file() so it wont be missed in case the release call is already underway, when disconnect starts.
use DEFINE_SPINLOCK(shutdown_mutex); instead of DEFINE_MUTEX(mutex); 'cause code paths are small under that lock.
Merged with init.c.
Changes in rc5: move fops_put(mfile->disconnected_f_op);
out of the spin_lock.
Changes in rc6: rename shutdow_mutex to *_lock
use list_for_each_entry()
If agreed upon, this should go to all stable.
Karsten
--------------------------------------------------------------------------
Handle file operations during snd_card disconnects using static file->f_op
Alsa used to kmalloc one file->f_op per file per disconnecting snd_card. This led to oopses sometimes when file->f_op was freed before __fput() finished.
Patch adds a virtual device for disconnect: VDD.
VDD consists of:
LIST_HEAD(shutdown_files)
protected by DEFINE_SPINLOCK(shutdown_lock)
static struct file_operations snd_shutdown_f_ops
and functions assigned to it
Additions to struct snd_monitor_file
to specify if instance is hidden by VDD or not.
A VDD's instance is
created in snd_card_disconnect() under the card->files_lock. cleaned up in snd_card_file_remove() under the card->files_lock.
include/core.h +2 -2
@@ -89,10 +89,10 @@struct snd_monitor_file {struct file *file;struct snd_monitor_file *next; +const struct file_operations *disconnected_f_op; +struct list_head shutdown_list; }; -struct snd_shutdown_f_ops ;/* define it later in init.c */ -/* main structure for soundcard */ struct snd_card {
core/init.c +52 -40
148 lines not on this page · raw message
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV