Unnecessary BKL contention in video1394
A message that is not in the archive (sent off-list or not yet received): [email protected]
(added Cc: lkml to pull in some clues)
Daniel Drake wrote at linux1394-devel:
Quoted 14 lines
Hi,
I noticed that video1394 calls
lock_kernelin it'sfile_operations. I thought about converting these into a per-instance mutex or something, but in the end I couldn't find a reason why this locking is needed. (The more important I/O system is protected by separate spinlocks)The BKL is only contended when operations such as
ioctl()orrelease()are invoked. My knowledge lacks at this point, but I'm reasonably sure that some upper layer must ensure that these invokations are serialized, as opposed to leaving it up to the driver to handle nasty potential situations e.g.release()being called halfway through aread(). Can anyone clarify?
Quoted 2 lines
Thanks,
Daniel
I think you are right. Same with dv1394. Although we need to double-check whether something needs replacement protection then.
Quoted 63 lines
------------------------------------------------------------------------
Index:
linux/drivers/ieee1394/video1394.c
===================================================================drivers/ieee1394/video1394.c +0
-8@@ -1161,9 +1161,7 @@ static int __video1394_ioctl(struct filestatic long video1394_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {int err; -lock_kernel (); err =__video1394_ioctl (file, cmd, arg); -unlock_kernel ();return err; }@@ -1181,13 +1179,11 @@ static int video1394_mmap(struct file *fstruct file_ctx *ctx = (struct file_ctx *)file->private_data;int res = -EINVAL ; -lock_kernel ();if (ctx->current_ctx ==NULL ) {KERN_ERR , ctx->ohci->host->id,"Current iso context not set" ); }else res =dma_region_mmap (&ctx->current_ctx->dma, file, vma); -unlock_kernel ();return res; }@@ -1200,7 +1196,6 @@ static unsigned int video1394_poll(strucstruct dma_iso_ctx *d;int i; -lock_kernel (); ctx = file->private_data; d = ctx->current_ctx;if (d ==NULL ) {@@ -1221,7 +1216,6 @@ static unsigned int video1394_poll(struc }spin_unlock_irqrestore (&d->lock, flags); done: -unlock_kernel ();return mask; }@@ -1257,7 +1251,6 @@ static int video1394_release(struct inodstruct list_head *lh, *next;u64 mask; -lock_kernel ();list_for_each_safe (lh, next, &ctx->context_list) {struct dma_iso_ctx *d; d = list_entry(lh, struct dma_iso_ctx, link);@@ -1278,7 +1271,6 @@ static int video1394_release(struct inodkfree (ctx); file->private_data =NULL ; -unlock_kernel ();return 0 ; }
Signature · 3 lines
Stefan Richter -=====-=-==- =-=- =--=- http://arcgraph.de/sr/
Quoted 17 lines · Stefan Richter
(added Cc: lkml to pull in some clues)
Daniel Drake wrote at linux1394-devel:
Quoted 7 lines
Hi,
I noticed that video1394 calls
lock_kernelin it'sfile_operations. I thought about converting these into a per-instance mutex or something, but in the end I couldn't find a reason why this locking is needed. (The more important I/O system is protected by separate spinlocks)Quoted 6 lines
The BKL is only contended when operations such as
ioctl()orrelease()are invoked. My knowledge lacks at this point, but I'm reasonably sure that some upper layer must ensure that these invokations are serialized, as opposed to leaving it up to the driver to handle nasty potential situations e.g.release()being called halfway through aread(). Can anyone clarify?
Quoted 2 lines
I think you are right. Same with dv1394. Although we need to double-check whether something needs replacement protection then.
Adding Andi Kleen to CC, who added the BKL around __video1394_ioctl a long while back (when converting video1394 to compat_ioctl).
I don't feel that any replacement protection is needed, since the critical sections (where structures are used both in interrupts and in file_operations) are already protected by spinlocks.
Quoted 64 lines
Quoted 63 lines
------------------------------------------------------------------------
Index:
linux/drivers/ieee1394/video1394.c
===================================================================drivers/ieee1394/video1394.c +0
-8@@ -1161,9 +1161,7 @@ static int __video1394_ioctl(struct filestatic long video1394_ioctl (struct file *file,unsigned int cmd,unsigned long arg) {int err; -lock_kernel (); err =__video1394_ioctl (file, cmd, arg); -unlock_kernel ();return err; }@@ -1181,13 +1179,11 @@ static int video1394_mmap(struct file *fstruct file_ctx *ctx = (struct file_ctx *)file->private_data;int res = -EINVAL ; -lock_kernel ();if (ctx->current_ctx ==NULL ) {KERN_ERR , ctx->ohci->host->id,"Current iso context not set" ); }else res =dma_region_mmap (&ctx->current_ctx->dma, file, vma); -unlock_kernel ();return res; }@@ -1200,7 +1196,6 @@ static unsigned int video1394_poll(strucstruct dma_iso_ctx *d;int i; -lock_kernel (); ctx = file->private_data; d = ctx->current_ctx;if (d ==NULL ) {@@ -1221,7 +1216,6 @@ static unsigned int video1394_poll(struc }spin_unlock_irqrestore (&d->lock, flags); done: -unlock_kernel ();return mask; }@@ -1257,7 +1251,6 @@ static int video1394_release(struct inodstruct list_head *lh, *next;u64 mask; -lock_kernel ();list_for_each_safe (lh, next, &ctx->context_list) {struct dma_iso_ctx *d; d = list_entry(lh, struct dma_iso_ctx, link);@@ -1278,7 +1271,6 @@ static int video1394_release(struct inodkfree (ctx); file->private_data =NULL ; -unlock_kernel ();return 0 ; }
Quoted 7 lines
Adding Andi Kleen to CC, who added the BKL around
__video1394_ioctla long while back (when converting video1394 tocompat_ioctl).I don't feel that any replacement protection is needed, since the critical sections (where structures are used both in interrupts and in
file_operations) are already protected by spinlocks.
Fine by me. I just did it to preserve old semantics because I didn't want to audit the 1394 locking. But if you think it's not needed feel free to remove them.
-andi
Andi Kleen wrote:
[Daniel Drake wrote:]
Quoted 10 lines
Quoted 6 lines
Adding Andi Kleen to CC, who added the BKL around
__video1394_ioctla long while back (when converting video1394 tocompat_ioctl).I don't feel that any replacement protection is needed, since the critical sections (where structures are used both in interrupts and in
file_operations) are already protected by spinlocks.Fine by me. I just did it to preserve old semantics because I didn't want to audit the 1394 locking. But if you think it's not needed feel free to remove them.
Thanks for the info. Daniel, do you want to resend a signed-off patch? And __video1394_ioctl and its wrapper video1394_ioctl can certainly be merged then.
Signature · 3 lines
Stefan Richter -=====-=-==- =-=- =--== http://arcgraph.de/sr/
Quoted 3 lines · Stefan Richter
Thanks for the info. Daniel, do you want to resend a signed-off patch? And
__video1394_ioctland its wrappervideo1394_ioctlcan certainly be merged then.
Yep, I had already made that change locally. I will run it overnight on several cameras just to be sure, and will send a patch tomorrow.
I also did some more investigation and straightened out my knowledge offile_operations: release() can never be called while inside a read() or ioctl(): This would imply that separate threads are in use, and the driver's release() function is not called until *all* threads have closed the fd. In other words I'm now much more confident that this patch is not removing any necessary locking.
Daniel