]> git.karo-electronics.de Git - linux-beck.git/blobdiff - drivers/staging/comedi/comedi_fops.c
Merge 3.15-rc2 into staging-next
[linux-beck.git] / drivers / staging / comedi / comedi_fops.c
index e283e6c7b52bb0ce172c3d4fcca8ec8e8b7f1546..70545e670dd0b8ccb3b8aeb54c7f85bc4711db76 100644 (file)
@@ -1925,14 +1925,21 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
        struct comedi_device *dev = file->private_data;
        struct comedi_subdevice *s;
        struct comedi_async *async;
-       struct comedi_buf_map *bm;
+       struct comedi_buf_map *bm = NULL;
        unsigned long start = vma->vm_start;
        unsigned long size;
        int n_pages;
        int i;
        int retval;
 
-       mutex_lock(&dev->mutex);
+       /*
+        * 'trylock' avoids circular dependency with current->mm->mmap_sem
+        * and down-reading &dev->attach_lock should normally succeed without
+        * contention unless the device is in the process of being attached
+        * or detached.
+        */
+       if (!down_read_trylock(&dev->attach_lock))
+               return -EAGAIN;
 
        if (!dev->attached) {
                dev_dbg(dev->class_dev, "no driver attached\n");
@@ -1972,7 +1979,9 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
        }
 
        n_pages = size >> PAGE_SHIFT;
-       bm = async->buf_map;
+
+       /* get reference to current buf map (if any) */
+       bm = comedi_buf_map_from_subdev_get(s);
        if (!bm || n_pages > bm->n_pages) {
                retval = -EINVAL;
                goto done;
@@ -1996,7 +2005,8 @@ static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
 
        retval = 0;
 done:
-       mutex_unlock(&dev->mutex);
+       up_read(&dev->attach_lock);
+       comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
        return retval;
 }