Skip to content

Commit

Permalink
zoran: racy refcount handling in vm_ops ->open()/->close()
Browse files Browse the repository at this point in the history
worse, we lock ->resource_lock too late when we are destroying the
final clonal VMA; the check for lack of other mappings of the same
opened file can race with mmap().

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
  • Loading branch information
Al Viro committed May 31, 2013
1 parent 448293a commit 4ad1f70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion drivers/media/pci/zoran/zoran.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct zoran_fh;

struct zoran_mapping {
struct zoran_fh *fh;
int count;
atomic_t count;
};

struct zoran_buffer {
Expand Down
15 changes: 8 additions & 7 deletions drivers/media/pci/zoran/zoran_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2803,8 +2803,7 @@ static void
zoran_vm_open (struct vm_area_struct *vma)
{
struct zoran_mapping *map = vma->vm_private_data;

map->count++;
atomic_inc(&map->count);
}

static void
Expand All @@ -2815,7 +2814,7 @@ zoran_vm_close (struct vm_area_struct *vma)
struct zoran *zr = fh->zr;
int i;

if (--map->count > 0)
if (!atomic_dec_and_mutex_lock(&map->count, &zr->resource_lock))
return;

dprintk(3, KERN_INFO "%s: %s - munmap(%s)\n", ZR_DEVNAME(zr),
Expand All @@ -2828,14 +2827,16 @@ zoran_vm_close (struct vm_area_struct *vma)
kfree(map);

/* Any buffers still mapped? */
for (i = 0; i < fh->buffers.num_buffers; i++)
if (fh->buffers.buffer[i].map)
for (i = 0; i < fh->buffers.num_buffers; i++) {
if (fh->buffers.buffer[i].map) {
mutex_unlock(&zr->resource_lock);
return;
}
}

dprintk(3, KERN_INFO "%s: %s - free %s buffers\n", ZR_DEVNAME(zr),
__func__, mode_name(fh->map_mode));

mutex_lock(&zr->resource_lock);

if (fh->map_mode == ZORAN_MAP_MODE_RAW) {
if (fh->buffers.active != ZORAN_FREE) {
Expand Down Expand Up @@ -2939,7 +2940,7 @@ zoran_mmap (struct file *file,
goto mmap_unlock_and_return;
}
map->fh = fh;
map->count = 1;
atomic_set(&map->count, 1);

vma->vm_ops = &zoran_vm_ops;
vma->vm_flags |= VM_DONTEXPAND;
Expand Down

0 comments on commit 4ad1f70

Please sign in to comment.