Commit 6b060ffe authored by Frank Zago's avatar Frank Zago Committed by Mauro Carvalho Chehab

V4L/DVB (9086): gspca: Use a kref to avoid potentialy blocking forever in disconnect.

Signed-off-by: default avatarFrank Zago <frank@zago.net>
Signed-off-by: default avatarJean-Francois Moine <moinejf@free.fr>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab@redhat.com>
parent 6c49da7f
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <linux/string.h> #include <linux/string.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/io.h> #include <linux/io.h>
#include <linux/kref.h>
#include <asm/page.h> #include <asm/page.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/jiffies.h> #include <linux/jiffies.h>
...@@ -834,6 +835,16 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, ...@@ -834,6 +835,16 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
return ret; return ret;
} }
static void gspca_delete(struct kref *kref)
{
struct gspca_dev *gspca_dev = container_of(kref, struct gspca_dev, kref);
PDEBUG(D_STREAM, "device deleted");
kfree(gspca_dev->usb_buf);
kfree(gspca_dev);
}
static int dev_open(struct inode *inode, struct file *file) static int dev_open(struct inode *inode, struct file *file)
{ {
struct gspca_dev *gspca_dev; struct gspca_dev *gspca_dev;
...@@ -853,6 +864,10 @@ static int dev_open(struct inode *inode, struct file *file) ...@@ -853,6 +864,10 @@ static int dev_open(struct inode *inode, struct file *file)
goto out; goto out;
} }
gspca_dev->users++; gspca_dev->users++;
/* one more user */
kref_get(&gspca_dev->kref);
file->private_data = gspca_dev; file->private_data = gspca_dev;
#ifdef GSPCA_DEBUG #ifdef GSPCA_DEBUG
/* activate the v4l2 debug */ /* activate the v4l2 debug */
...@@ -895,7 +910,11 @@ static int dev_close(struct inode *inode, struct file *file) ...@@ -895,7 +910,11 @@ static int dev_close(struct inode *inode, struct file *file)
} }
file->private_data = NULL; file->private_data = NULL;
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
PDEBUG(D_STREAM, "close done"); PDEBUG(D_STREAM, "close done");
kref_put(&gspca_dev->kref, gspca_delete);
return 0; return 0;
} }
...@@ -1809,6 +1828,7 @@ int gspca_dev_probe(struct usb_interface *intf, ...@@ -1809,6 +1828,7 @@ int gspca_dev_probe(struct usb_interface *intf,
err("couldn't kzalloc gspca struct"); err("couldn't kzalloc gspca struct");
return -EIO; return -EIO;
} }
kref_init(&gspca_dev->kref);
gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL); gspca_dev->usb_buf = kmalloc(USB_BUF_SZ, GFP_KERNEL);
if (!gspca_dev->usb_buf) { if (!gspca_dev->usb_buf) {
err("out of memory"); err("out of memory");
...@@ -1858,8 +1878,7 @@ int gspca_dev_probe(struct usb_interface *intf, ...@@ -1858,8 +1878,7 @@ int gspca_dev_probe(struct usb_interface *intf,
PDEBUG(D_PROBE, "probe ok"); PDEBUG(D_PROBE, "probe ok");
return 0; return 0;
out: out:
kfree(gspca_dev->usb_buf); kref_put(&gspca_dev->kref, gspca_delete);
kfree(gspca_dev);
return ret; return ret;
} }
EXPORT_SYMBOL(gspca_dev_probe); EXPORT_SYMBOL(gspca_dev_probe);
...@@ -1874,8 +1893,8 @@ void gspca_disconnect(struct usb_interface *intf) ...@@ -1874,8 +1893,8 @@ void gspca_disconnect(struct usb_interface *intf)
{ {
struct gspca_dev *gspca_dev = usb_get_intfdata(intf); struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
if (!gspca_dev) usb_set_intfdata(intf, NULL);
return;
gspca_dev->present = 0; gspca_dev->present = 0;
mutex_lock(&gspca_dev->queue_lock); mutex_lock(&gspca_dev->queue_lock);
mutex_lock(&gspca_dev->usb_lock); mutex_lock(&gspca_dev->usb_lock);
...@@ -1883,16 +1902,12 @@ void gspca_disconnect(struct usb_interface *intf) ...@@ -1883,16 +1902,12 @@ void gspca_disconnect(struct usb_interface *intf)
destroy_urbs(gspca_dev); destroy_urbs(gspca_dev);
mutex_unlock(&gspca_dev->usb_lock); mutex_unlock(&gspca_dev->usb_lock);
mutex_unlock(&gspca_dev->queue_lock); mutex_unlock(&gspca_dev->queue_lock);
while (gspca_dev->users != 0) { /* wait until fully closed */
atomic_inc(&gspca_dev->nevent);
wake_up_interruptible(&gspca_dev->wq); /* wake processes */
schedule();
}
/* We don't want people trying to open up the device */ /* We don't want people trying to open up the device */
video_unregister_device(&gspca_dev->vdev); video_unregister_device(&gspca_dev->vdev);
/* Free the memory */
kfree(gspca_dev->usb_buf); kref_put(&gspca_dev->kref, gspca_delete);
kfree(gspca_dev);
PDEBUG(D_PROBE, "disconnect complete"); PDEBUG(D_PROBE, "disconnect complete");
} }
EXPORT_SYMBOL(gspca_disconnect); EXPORT_SYMBOL(gspca_disconnect);
......
...@@ -121,6 +121,7 @@ struct gspca_dev { ...@@ -121,6 +121,7 @@ struct gspca_dev {
struct video_device vdev; /* !! must be the first item */ struct video_device vdev; /* !! must be the first item */
struct file_operations fops; struct file_operations fops;
struct usb_device *dev; struct usb_device *dev;
struct kref kref;
struct file *capt_file; /* file doing video capture */ struct file *capt_file; /* file doing video capture */
struct cam cam; /* device information */ struct cam cam; /* device information */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment