Skip to content

Commit 73a1106

Browse files
Hans Verkuilmchehab
authored andcommitted
media: v4l2-core: push taking ioctl mutex down to ioctl handler
The ioctl serialization mutex (vdev->lock or q->lock for vb2 queues) was taken at the highest level in v4l2-dev.c. This prevents more fine-grained locking since at that level we cannot examine the ioctl arguments, we can only do that after video_usercopy is called. So push the locking down to __video_do_ioctl() and subdev_do_ioctl_lock(). This also allows us to make a few functions in v4l2-ioctl.c static and video_usercopy() is no longer exported. The locking scheme is not changed by this patch, just pushed down. Signed-off-by: Hans Verkuil <hansverk@cisco.com> Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
1 parent eba09b5 commit 73a1106

5 files changed

Lines changed: 33 additions & 31 deletions

File tree

‎drivers/media/v4l2-core/v4l2-dev.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,8 @@ static long v4l2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
360360
int ret = -ENODEV;
361361

362362
if (vdev->fops->unlocked_ioctl) {
363-
struct mutex *lock = v4l2_ioctl_get_lock(vdev, cmd);
364-
365-
if (lock && mutex_lock_interruptible(lock))
366-
return -ERESTARTSYS;
367363
if (video_is_registered(vdev))
368364
ret = vdev->fops->unlocked_ioctl(filp, cmd, arg);
369-
if (lock)
370-
mutex_unlock(lock);
371365
} else
372366
ret = -ENOTTY;
373367

‎drivers/media/v4l2-core/v4l2-ioctl.c‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,14 +2655,15 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
26552655
};
26562656
#define V4L2_IOCTLS ARRAY_SIZE(v4l2_ioctls)
26572657

2658-
bool v4l2_is_known_ioctl(unsigned int cmd)
2658+
static bool v4l2_is_known_ioctl(unsigned int cmd)
26592659
{
26602660
if (_IOC_NR(cmd) >= V4L2_IOCTLS)
26612661
return false;
26622662
return v4l2_ioctls[_IOC_NR(cmd)].ioctl == cmd;
26632663
}
26642664

2665-
struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned cmd)
2665+
static struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev,
2666+
unsigned int cmd)
26662667
{
26672668
if (_IOC_NR(cmd) >= V4L2_IOCTLS)
26682669
return vdev->lock;
@@ -2713,6 +2714,7 @@ static long __video_do_ioctl(struct file *file,
27132714
unsigned int cmd, void *arg)
27142715
{
27152716
struct video_device *vfd = video_devdata(file);
2717+
struct mutex *lock; /* ioctl serialization mutex */
27162718
const struct v4l2_ioctl_ops *ops = vfd->ioctl_ops;
27172719
bool write_only = false;
27182720
struct v4l2_ioctl_info default_info;
@@ -2731,6 +2733,16 @@ static long __video_do_ioctl(struct file *file,
27312733
if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags))
27322734
vfh = file->private_data;
27332735

2736+
lock = v4l2_ioctl_get_lock(vfd, cmd);
2737+
2738+
if (lock && mutex_lock_interruptible(lock))
2739+
return -ERESTARTSYS;
2740+
2741+
if (!video_is_registered(vfd)) {
2742+
ret = -ENODEV;
2743+
goto unlock;
2744+
}
2745+
27342746
if (v4l2_is_known_ioctl(cmd)) {
27352747
info = &v4l2_ioctls[_IOC_NR(cmd)];
27362748

@@ -2780,6 +2792,9 @@ static long __video_do_ioctl(struct file *file,
27802792
}
27812793
}
27822794

2795+
unlock:
2796+
if (lock)
2797+
mutex_unlock(lock);
27832798
return ret;
27842799
}
27852800

@@ -2969,7 +2984,6 @@ video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
29692984
kvfree(mbuf);
29702985
return err;
29712986
}
2972-
EXPORT_SYMBOL(video_usercopy);
29732987

29742988
long video_ioctl2(struct file *file,
29752989
unsigned int cmd, unsigned long arg)

‎drivers/media/v4l2-core/v4l2-subdev.c‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,10 +502,25 @@ static long subdev_do_ioctl(struct file *file, unsigned int cmd, void *arg)
502502
return 0;
503503
}
504504

505+
static long subdev_do_ioctl_lock(struct file *file, unsigned int cmd, void *arg)
506+
{
507+
struct video_device *vdev = video_devdata(file);
508+
struct mutex *lock = vdev->lock;
509+
long ret = -ENODEV;
510+
511+
if (lock && mutex_lock_interruptible(lock))
512+
return -ERESTARTSYS;
513+
if (video_is_registered(vdev))
514+
ret = subdev_do_ioctl(file, cmd, arg);
515+
if (lock)
516+
mutex_unlock(lock);
517+
return ret;
518+
}
519+
505520
static long subdev_ioctl(struct file *file, unsigned int cmd,
506521
unsigned long arg)
507522
{
508-
return video_usercopy(file, cmd, arg, subdev_do_ioctl);
523+
return video_usercopy(file, cmd, arg, subdev_do_ioctl_lock);
509524
}
510525

511526
#ifdef CONFIG_COMPAT

‎include/media/v4l2-dev.h‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,6 @@ void video_device_release(struct video_device *vdev);
435435
*/
436436
void video_device_release_empty(struct video_device *vdev);
437437

438-
/**
439-
* v4l2_is_known_ioctl - Checks if a given cmd is a known V4L ioctl
440-
*
441-
* @cmd: ioctl command
442-
*
443-
* returns true if cmd is a known V4L2 ioctl
444-
*/
445-
bool v4l2_is_known_ioctl(unsigned int cmd);
446-
447438
/**
448439
* v4l2_disable_ioctl- mark that a given command isn't implemented.
449440
* shouldn't use core locking

‎include/media/v4l2-ioctl.h‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -658,18 +658,6 @@ void v4l_printk_ioctl(const char *prefix, unsigned int cmd);
658658

659659
struct video_device;
660660

661-
662-
/**
663-
* v4l2_ioctl_get_lock - get the mutex (if any) that it is need to lock for
664-
* a given command.
665-
*
666-
* @vdev: Pointer to struct &video_device.
667-
* @cmd: Ioctl name.
668-
*
669-
* .. note:: Internal use only. Should not be used outside V4L2 core.
670-
*/
671-
struct mutex *v4l2_ioctl_get_lock(struct video_device *vdev, unsigned int cmd);
672-
673661
/* names for fancy debug output */
674662
extern const char *v4l2_field_names[];
675663
extern const char *v4l2_type_names[];

0 commit comments

Comments
 (0)