Skip to contentSkip to conversation

uvcvideo: BKL removal

GIT PULL Laurent Pinchart 4 participants 19 messages Dec 14, 2010 – Dec 23, 2010 1 message not in the archive Media (V4L/DVB)

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Quoted 28 lines · On Monday 29 November 2010 11:15:10 Laurent Pinchart wrote:

Hi Mauro,

The following changes since commit
c796e203229c8c08250f9d372ae4e10c466b1787:

[media] kconfig: add an option to determine a menu's visibility
(2010-11-22 10:37:56 -0200)

are available in the git repository at:
git://linuxtv.org/pinchartl/uvcvideo.git uvcvideo-stable

They complete the BKL removal from the uvcvideo driver. Feedback received from Hans during review has been integrated.

Laurent Pinchart (5):
uvcvideo: Lock controls mutex when querying menus
uvcvideo: Move mutex lock/unlock inside uvc_free_buffers
uvcvideo: Move mmap() handler to uvc_queue.c
uvcvideo: Lock stream mutex when accessing format-related information
uvcvideo: Convert to unlocked_ioctl

 drivers/media/video/uvc/uvc_ctrl.c  |   48 +++++++++-
 drivers/media/video/uvc/uvc_queue.c |  133 +++++++++++++++++++++-----
 drivers/media/video/uvc/uvc_v4l2.c  |  185
+++++++++++----------------------- drivers/media/video/uvc/uvc_video.c |  
 3 -
 drivers/media/video/uvc/uvcvideo.h  |   10 ++-
 5 files changed, 222 insertions(+), 157 deletions(-)
Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

Laurent,

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

- with saa7134 and qv4l2 (and after a fix for input capabilities): saa7134 and/or qv4l2 doesn't seem to work fine if video format is changed to a 60HZ format (NTSC or PAL/M). It keeps trying to use 576 lines, but the driver only works with 480 lines for those formats. So, if qv4l2 tries to capture with STD/M, it fails, except if the number of lines is manually fixed by the user.

- at least with the saa7134 board I used for test, video capture fails on some conditions. This is not related to BKL patches. I suspect it may be some initialization failure with the tuner (tda8275/tda8290), but I didn't have time to dig into it, nor to test with a simpler saa7134 device. The device I used was an Avermedia m135.

Quoted 34 lines · On Monday 29 November 2010 11:15:10 Laurent Pinchart wrote:
Quoted 31 lines

Hi Mauro,

The following changes since commit
c796e203229c8c08250f9d372ae4e10c466b1787:

[media] kconfig: add an option to determine a menu's visibility
(2010-11-22 10:37:56 -0200)

are available in the git repository at:
git://linuxtv.org/pinchartl/uvcvideo.git uvcvideo-stable

They complete the BKL removal from the uvcvideo driver. Feedback received from Hans during review has been integrated.

Laurent Pinchart (5):
uvcvideo: Lock controls mutex when querying menus
uvcvideo: Move mutex lock/unlock inside uvc_free_buffers
uvcvideo: Move mmap() handler to uvc_queue.c
uvcvideo: Lock stream mutex when accessing format-related information
uvcvideo: Convert to unlocked_ioctl

 drivers/media/video/uvc/uvc_ctrl.c  |   48 +++++++++-
 drivers/media/video/uvc/uvc_queue.c |  133 +++++++++++++++++++++-----
 drivers/media/video/uvc/uvc_v4l2.c  |  185
+++++++++++----------------------- drivers/media/video/uvc/uvc_video.c |  
 3 -
 drivers/media/video/uvc/uvcvideo.h  |   10 ++-
 5 files changed, 222 insertions(+), 157 deletions(-)
#3 lore raw

Hi Mauro,

Quoted 6 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

Thank you.

Quoted 4 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

Quoted 8 lines

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

Quoted 12 lines

- with saa7134 and qv4l2 (and after a fix for input capabilities): saa7134 and/or qv4l2 doesn't seem to work fine if video format is changed to a 60HZ format (NTSC or PAL/M). It keeps trying to use 576 lines, but the driver only works with 480 lines for those formats. So, if qv4l2 tries to capture with STD/M, it fails, except if the number of lines is manually fixed by the user.

- at least with the saa7134 board I used for test, video capture fails on some conditions. This is not related to BKL patches. I suspect it may be some initialization failure with the tuner (tda8275/tda8290), but I didn't have time to dig into it, nor to test with a simpler saa7134 device. The device I used was an Avermedia m135.

Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw
Quoted 25 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:

Hi Mauro,

Quoted 6 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

Thank you.

Quoted 4 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

Quoted 8 lines

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 3 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

The reason a lot of drivers don't do this is partially because for many TV capture drivers it is highly unlikely that the buffer size will change after calling REQBUFS (there are basically only two formats: 720x480 or 720x576 and users will normally never change between the two). However, this is much more likely to happen for webcams and embedded systems supporting HDTV.

The other reason is probably because driver developers simple do not realize they need to lock the resources on REQBUFS. I'm sure many existing drivers will fail miserably if you change the format after calling REQBUFS (particularly with mmap streaming mode).

Regards,

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#5 lore raw

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 26 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 25 lines

Hi Mauro,

Quoted 6 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

Thank you.

Quoted 12 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 8 lines
Quoted 3 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF. Anyway, even replacing REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

One consequence on the way uvcvideo is currently doing it is that, if you use qv4l2, it is impossible to change the video size, as it returns -EBUSY, if you ask it to select a different format (even without streaming):

	$ ./qv4l2
	Set Capture Format: Device or resource busy
Quoted 5 lines

The reason a lot of drivers don't do this is partially because for many TV capture drivers it is highly unlikely that the buffer size will change after calling REQBUFS (there are basically only two formats: 720x480 or 720x576 and users will normally never change between the two). However, this is much more likely to happen for webcams and embedded systems supporting HDTV.

What applications do, when they need to change the formats, is to call REQBUFS again.

Quoted 4 lines

The other reason is probably because driver developers simple do not realize they need to lock the resources on REQBUFS. I'm sure many existing drivers will fail miserably if you change the format after calling REQBUFS (particularly with mmap streaming mode).

I didn't make any test, but I don't think they'll fail (at least, on the drivers that use videobuf), as streaming format will be stored at the videobuf handling (at buffer_prepare callback).

So, if you change the format, the change will be applied only at the next call to REQBUFS.

Cheers,
Mauro

#6 lore raw
Quoted 31 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 30 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 28 lines

Hi Mauro,

Quoted 6 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

Thank you.

Quoted 4 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

Quoted 8 lines

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

Quoted 7 lines
Quoted 4 lines

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

Quoted 23 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

One consequence on the way uvcvideo is currently doing it is that, if you use qv4l2, it is impossible to change the video size, as it returns -EBUSY, if you ask it to select a different format (even without streaming):

	$ ./qv4l2
	Set Capture Format: Device or resource busy
Quoted 5 lines

The reason a lot of drivers don't do this is partially because for many TV capture drivers it is highly unlikely that the buffer size will change after calling REQBUFS (there are basically only two formats: 720x480 or 720x576 and users will normally never change between the two). However, this is much more likely to happen for webcams and embedded systems supporting HDTV.

What applications do, when they need to change the formats, is to call REQBUFS again.

Quoted 4 lines

The other reason is probably because driver developers simple do not realize they need to lock the resources on REQBUFS. I'm sure many existing drivers will fail miserably if you change the format after calling REQBUFS (particularly with mmap streaming mode).

Quoted 6 lines

I didn't make any test, but I don't think they'll fail (at least, on the drivers that use videobuf), as streaming format will be stored at the videobuf handling (at buffer_prepare callback).

So, if you change the format, the change will be applied only at the next call to REQBUFS.

This behavior isn't according to the spec. G/S_FMT relate to the current format, not to some future format. Most non-videobuf drivers will not support this behavior.

It should be simple, really:

STREAMOFF
REQBUFS(0)

That's all that should be needed to stop streaming and return all buffers to the app. This is what uvc should also support (and I actually thought it did).

Attempts to change formats while buffers have been requested should be blocked with EBUSY. It's all perfectly reasonable. Well, perhaps next year we might succeed in having all drivers behave consistently...

Regards,

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#7 lore raw

Hi Hans,

Quoted 31 lines · On Monday 20 December 2010 13:28:06 Hans Verkuil wrote:
Quoted 30 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 28 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 17 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:
Quoted 9 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

Quoted 6 lines

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 4 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

Quoted 11 lines
Quoted 2 lines

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

Quoted 4 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

That's not correct. Buffers are released when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

Quoted 34 lines
Quoted 23 lines

One consequence on the way uvcvideo is currently doing it is that, if you use qv4l2, it is impossible to change the video size, as it returns -EBUSY, if you ask it to

select a different format (even without streaming):

	$ ./qv4l2
	Set Capture Format: Device or resource busy
Quoted 6 lines

The reason a lot of drivers don't do this is partially because for many TV capture drivers it is highly unlikely that the buffer size will change after calling REQBUFS (there are basically only two formats:
720x480 or 720x576 and users will normally never change between the two). However, this is much more likely to happen for webcams and embedded systems supporting HDTV.

What applications do, when they need to change the formats, is to call REQBUFS again.

Quoted 4 lines

The other reason is probably because driver developers simple do not realize they need to lock the resources on REQBUFS. I'm sure many existing drivers will fail miserably if you change the format after calling REQBUFS (particularly with mmap streaming mode).

Quoted 6 lines

I didn't make any test, but I don't think they'll fail (at least, on the drivers that use videobuf), as streaming format will be stored at the videobuf handling (at buffer_prepare callback).

So, if you change the format, the change will be applied only at the next call to REQBUFS.

This behavior isn't according to the spec. G/S_FMT relate to the current format, not to some future format. Most non-videobuf drivers will not support this behavior.

Quoted 8 lines

It should be simple, really:

STREAMOFF
REQBUFS(0)

That's all that should be needed to stop streaming and return all buffers to the app. This is what uvc should also support (and I actually thought it did).

That's what uvcvideo does.

Quoted 3 lines

Attempts to change formats while buffers have been requested should be blocked with EBUSY. It's all perfectly reasonable. Well, perhaps next year we might succeed in having all drivers behave consistently...

Signature · 3 lines
Regards,

Laurent Pinchart
#8 lore raw
Quoted 41 lines · On Monday, December 20, 2010 13:35:28 Laurent Pinchart wrote:

Hi Hans,

Quoted 38 lines · On Monday 20 December 2010 13:28:06 Hans Verkuil wrote:
Quoted 24 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 23 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 21 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:
Quoted 9 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

Quoted 6 lines

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

Quoted 8 lines
Quoted 5 lines

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

That's not correct. Buffers are released when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

Why? After REQBUFS(0) any filehandle ownership should be dropped.

Regards,

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#9 lore raw

Hi Hans,

Quoted 46 lines · On Monday 20 December 2010 13:45:45 Hans Verkuil wrote:
Quoted 45 lines · On Monday, December 20, 2010 13:35:28 Laurent Pinchart wrote:
Quoted 43 lines · On Monday 20 December 2010 13:28:06 Hans Verkuil wrote:
Quoted 29 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 22 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 16 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:
Quoted 8 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

Quoted 6 lines

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 4 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

Quoted 5 lines

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

Quoted 3 lines

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

Quoted 4 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

Quoted 6 lines
Quoted 4 lines

That's not correct. Buffers are released when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

Why? After REQBUFS(0) any filehandle ownership should be dropped.

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Signature · 3 lines
Regards,

Laurent Pinchart
#10 lore raw
Quoted 54 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

Hi Hans,

Quoted 51 lines · On Monday 20 December 2010 13:45:45 Hans Verkuil wrote:
Quoted 49 lines · On Monday, December 20, 2010 13:35:28 Laurent Pinchart wrote:
Quoted 30 lines · On Monday 20 December 2010 13:28:06 Hans Verkuil wrote:
Quoted 29 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 27 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 16 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:
Quoted 8 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

Quoted 6 lines

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 4 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

Quoted 13 lines
Quoted 3 lines

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

Quoted 4 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

That's not correct. Buffers are released when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

Quoted 6 lines
Quoted 1 line

Why? After REQBUFS(0) any filehandle ownership should be dropped.

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

That's why S_PRIORITY was invented.

One of the nice properties of V4L2 is the ability to allow multiple processes to access the same device at the same time and even make modifications where possible.

To prevent unwanted modifications the priority scheme was created. Unfortunately, too many drivers do not support it. It really should be core functionality. I did work on it earlier in the year, but never finished it, although it shouldn't be hard to do.

Regards,

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#11 lore raw

Hi Hans,

Quoted 8 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ? And the "unset" priority ? Are other applications allowed to change controls when an application has the record priority ?

In general I find the priority ioctls underspecified, that's why I haven't implemented them yet.

On a side note, I've just tested the latest uvcvideo driver, and I've successfully captured video using a second application after calling REQBUFS(0) in a first application.

Signature · 3 lines
Regards,

Laurent Pinchart
#12 lore raw
Quoted 10 lines · On Thursday, December 23, 2010 10:02:33 Laurent Pinchart wrote:

Hi Hans,

Quoted 7 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Quoted 4 lines
Quoted 1 line

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ?

As the documentation mentions, it can be used for background processes monitoring VBI (e.g. teletext) transmissions. I'm not aware of any such applications, though.

PRIORITY_DEFAULT and PRIORITY_RECORD are the only two relevant prios in practice.

Quoted 1 line

And the "unset" priority ?

Internal prio only. I think it's the value when no file handle is open.

Quoted 3 lines

Are
other applications allowed to change controls when an application has the record priority ?

No. Only read-only ioctls can be executed.

Quoted 2 lines

In general I find the priority ioctls underspecified, that's why I haven't implemented them yet.

Use the prio support functions in v4l2-common. They are easy to use and do the job.

Regards,

Hans

Quoted 5 lines

On a side note, I've just tested the latest uvcvideo driver, and I've successfully captured video using a second application after calling REQBUFS(0) in a first application.

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#13 lore raw

Hi Hans,

Quoted 18 lines · On Thursday 23 December 2010 10:20:17 Hans Verkuil wrote:
Quoted 8 lines · On Thursday, December 23, 2010 10:02:33 Laurent Pinchart wrote:
Quoted 7 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Quoted 4 lines
Quoted 1 line

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ?

As the documentation mentions, it can be used for background processes monitoring VBI (e.g. teletext) transmissions. I'm not aware of any such applications, though.

Quoted 6 lines

PRIORITY_DEFAULT and PRIORITY_RECORD are the only two relevant prios in practice.

Quoted 1 line

And the "unset" priority ?

Internal prio only. I think it's the value when no file handle is open.

Aren't priorities associated with file handles ?

Quoted 4 lines
Quoted 2 lines

Are other applications allowed to change controls when an application has the record priority ?

No. Only read-only ioctls can be executed.

Then we got an issue here. I want an application to be able to acquire exclusive streaming rights on the device (so that there won't be race conditions when changing the resolution), but still allow other applications to change controls.

Quoted 5 lines
Quoted 2 lines

In general I find the priority ioctls underspecified, that's why I haven't implemented them yet.

Use the prio support functions in v4l2-common. They are easy to use and do the job.

Signature · 3 lines
Regards,

Laurent Pinchart
#14 lore raw
Quoted 21 lines · On Thursday, December 23, 2010 10:27:25 Laurent Pinchart wrote:

Hi Hans,

Quoted 18 lines · On Thursday 23 December 2010 10:20:17 Hans Verkuil wrote:
Quoted 8 lines · On Thursday, December 23, 2010 10:02:33 Laurent Pinchart wrote:
Quoted 7 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Quoted 4 lines
Quoted 1 line

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ?

As the documentation mentions, it can be used for background processes monitoring VBI (e.g. teletext) transmissions. I'm not aware of any such applications, though.

Quoted 8 lines
Quoted 6 lines

PRIORITY_DEFAULT and PRIORITY_RECORD are the only two relevant prios in practice.

Quoted 1 line

And the "unset" priority ?

Internal prio only. I think it's the value when no file handle is open.

Aren't priorities associated with file handles ?

Yes, but there is also a global prio.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

Are other applications allowed to change controls when an application has the record priority ?

No. Only read-only ioctls can be executed.

Then we got an issue here. I want an application to be able to acquire exclusive streaming rights on the device (so that there won't be race conditions when changing the resolution), but still allow other applications to change controls.

Why? The whole idea of prios is that once you are PRIO_RECORD no one else can mess with settings. Allowing other apps access to controls will make it possible to e.g. change the contrast to some crappy value. Not acceptable.

If the only thing you want to use PRIO_RECORD for in apps is to prevent this 'race condition' (I don't really see this as a real race, to be honest), then the app can raise the prio to RECORD just before STREAMOFF, change the resolution, start streaming again and lower it to the default prio.

Regards,

Hans

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

In general I find the priority ioctls underspecified, that's why I haven't implemented them yet.

Use the prio support functions in v4l2-common. They are easy to use and do the job.

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#15 lore raw
In reply to Laurent Pinchart
Quoted 10 lines · Laurent Pinchart

Hi Hans,

Quoted 7 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Quoted 4 lines
Quoted 1 line

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ?

http://linuxtv.org/downloads/v4l-dvb-apis/app-pri.html

"Another objective is to permit low priority applications working in background, which can be preempted by user controlled applications and automatically regain control of the device at a later time."

<contrived examples>
A use case would be for a daemon that does background channel scanning or VBI data collection when the user isn't doing something else with the video device.

For a camera, maybe it would be an application that does device health monitoring, some sort of continuous calibration, or motion detection when the device isn't in use by the user for viewing. </contrived examples>

Quoted 1 line

And the "unset" priority ?

When checking for the maximum priority in use, it indicates that no nodes have any priorities. See v4l2_prio_max(). For a driver that supports priorities, it means no device nodes are opened, since any device node being open would get a priority of V4L2_PRIORITY_INTERACTIVE/_DEFAULT by default in v4l2_prio_open().

V4L2_PRIORITY_UNSET is also used to indicate to the v4l2_prio_*() functions that a struct v4l2_prio_state hasn't been initialized (i.e. has just been kzalloc()'ed).

Quoted 3 lines

Are
other applications allowed to change controls when an application has the record priority ?

According to the spec, no:

"V4L2_PRIORITY_RECORD 3 Highest priority. Only one file descriptor can have this priority, it blocks any other fd from changing device properties."

Once an automated, scheduled-recording process is recording, one really wouldn't want another process changing them. I personally would not like unpredictable volume level variations on my TV show recordings.

In cx18-controls.c one can find an implementation example for the old control framework:

        int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
        {       
                struct cx18_open_id *id = fh;   
                struct cx18 *cx = id->cx;       
                int ret;
                struct v4l2_control ctrl;       
        
                ret = v4l2_prio_check(&cx->prio, id->prio);
                if (ret)
                        return ret;
    [...]

However, the new control framework in v4l2-ctrl.c seems to be missing any v4l2_prio_check() calls. The ioctl() handling in v4l2-ioctl.c doesn't have any either.

Quoted 2 lines

In general I find the priority ioctls underspecified, that's why I haven't implemented them yet.

Yes, it appears some of the details should be gleaned from the existing driver implementations before they are/were converted to the new control framework.

Regards,
Andy

#16 lore raw
Quoted 74 lines · On Thursday, December 23, 2010 14:04:38 Andy Walls wrote:
Quoted 10 lines · Laurent Pinchart

Hi Hans,

Quoted 7 lines · On Monday 20 December 2010 14:09:40 Hans Verkuil wrote:
Quoted 5 lines · On Monday, December 20, 2010 13:48:51 Laurent Pinchart wrote:

What if the application wants to change the resolution during capture ? It will have to stop capture, call REQBUFS(0), change the format, request buffers and restart capture. If filehandle ownership is dropped after REQBUFS(0) that will open the door to a race condition.

Quoted 4 lines
Quoted 1 line

That's why S_PRIORITY was invented.

Right, I should implement that. I think the documentation isn't clear though. What is the background priority for exactly ?

http://linuxtv.org/downloads/v4l-dvb-apis/app-pri.html

"Another objective is to permit low priority applications working in background, which can be preempted by user controlled applications and automatically regain control of the device at a later time."

<contrived examples>
A use case would be for a daemon that does background channel scanning or VBI data collection when the user isn't doing something else with the video device.

For a camera, maybe it would be an application that does device health monitoring, some sort of continuous calibration, or motion detection when the device isn't in use by the user for viewing. </contrived examples>

Quoted 1 line

And the "unset" priority ?

When checking for the maximum priority in use, it indicates that no nodes have any priorities. See v4l2_prio_max(). For a driver that supports priorities, it means no device nodes are opened, since any device node being open would get a priority of V4L2_PRIORITY_INTERACTIVE/_DEFAULT by default in v4l2_prio_open().

V4L2_PRIORITY_UNSET is also used to indicate to the v4l2_prio_*() functions that a struct v4l2_prio_state hasn't been initialized (i.e. has just been kzalloc()'ed).

Quoted 3 lines

Are
other applications allowed to change controls when an application has the record priority ?

According to the spec, no:

"V4L2_PRIORITY_RECORD 3 Highest priority. Only one file descriptor can have this priority, it blocks any other fd from changing device properties."

Once an automated, scheduled-recording process is recording, one really wouldn't want another process changing them. I personally would not like unpredictable volume level variations on my TV show recordings.

In cx18-controls.c one can find an implementation example for the old control framework:

        int cx18_s_ext_ctrls(struct file *file, void *fh, struct v4l2_ext_controls *c)
        {       
                struct cx18_open_id *id = fh;   
                struct cx18 *cx = id->cx;       
                int ret;
                struct v4l2_control ctrl;       
        
                ret = v4l2_prio_check(&cx->prio, id->prio);
                if (ret)
                        return ret;
    [...]
Quoted 3 lines

However, the new control framework in v4l2-ctrl.c seems to be missing any v4l2_prio_check() calls. The ioctl() handling in v4l2-ioctl.c doesn't have any either.

Oops, I'd forgotten about that. The priority support really should be moved into the v4l2 core framework where it belongs. I'll see if I can spend some time on that after Christmas.

Regards,

Hans

Signature · 1 line
Hans Verkuil - video4linux developer - sponsored by Cisco
#17 lore raw
In reply to Hans Verkuil

Em 20-12-2010 10:28, Hans Verkuil escreveu:

Quoted 37 lines · On Monday, December 20, 2010 13:10:32 Mauro Carvalho Chehab wrote:
Quoted 36 lines

Em 18-12-2010 08:45, Hans Verkuil escreveu:

Quoted 34 lines · On Saturday, December 18, 2010 01:54:41 Laurent Pinchart wrote:
Quoted 25 lines

Hi Mauro,

Quoted 6 lines · On Friday 17 December 2010 18:09:39 Mauro Carvalho Chehab wrote:

Em 14-12-2010 08:55, Laurent Pinchart escreveu:

Quoted 3 lines

Hi Mauro,

Please don't forget this pull request for 2.6.37.

Pull request for upstream sent today.

Thank you.

Quoted 4 lines

I didn't find any regressions at the BKL removal patches, but I noticed a few issues with qv4l2, not all related to uvcvideo. The remaining of this email is an attempt to document them for later fixes.

Quoted 8 lines

They don't seem to be regressions caused by BKL removal, but the better would be to fix them later.

- with uvcvideo and two video apps, if qv4l2 is started first, the second application doesn't start/capture. I suspect that REQBUFS (used by qv4l2 to probe mmap/userptr capabilities) create some resource locking at uvcvideo. The proper way is to lock the resources only if the driver is streaming, as other drivers and videobuf do.

Quoted 3 lines

I don't agree with that. The uvcvideo driver has one buffer queue per device, so if an application requests buffers on one file handle it will lock other applications out. If the driver didn't it would be subject to race conditions.

I agree with Laurent. Once an application calls REQBUFS with non-zero count, then it should lock the resources needed for streaming. The reason behind that is that REQBUFS also locks the current selected format in place, since the format determines the amount of memory needed for the buffers.

Quoted 6 lines
Quoted 2 lines

qv4l2 calls REQBUFS(1), then REQBUFS(0). Well, this is currently wrong, as most drivers will only release buffers at VIDIOC_STREAMOFF.

qv4l2 first calls STREAMOFF, then REQBUFS(1), then REQBUFS(0). In the hope that one of these will actually free any buffers. It's random at the moment when drivers release buffers, one of the reasons for using vb2.

This won't free buffers. REQBUFS(0) is not honored. so, what you're doing is really STREAMOFF/REQBUFS(1).

It would work fine if you change the order to:
REQBUFS(1)
REQBUFS(0)
STREAMOFF

Quoted 39 lines
Quoted 23 lines

Anyway, even replacing
REQBUFS(0) with VIDIOC_STREAMOFF at qv4l2 won't help with uvcvideo. It seems that, once buffers are requested at uvcvideo, they will release only at close().

One consequence on the way uvcvideo is currently doing it is that, if you use qv4l2, it is impossible to change the video size, as it returns -EBUSY, if you ask it to select a different format (even without streaming):

	$ ./qv4l2
	Set Capture Format: Device or resource busy
Quoted 5 lines

The reason a lot of drivers don't do this is partially because for many TV capture drivers it is highly unlikely that the buffer size will change after calling REQBUFS (there are basically only two formats: 720x480 or 720x576 and users will normally never change between the two). However, this is much more likely to happen for webcams and embedded systems supporting HDTV.

What applications do, when they need to change the formats, is to call REQBUFS again.

Quoted 4 lines

The other reason is probably because driver developers simple do not realize they need to lock the resources on REQBUFS. I'm sure many existing drivers will fail miserably if you change the format after calling REQBUFS (particularly with mmap streaming mode).

Quoted 6 lines

I didn't make any test, but I don't think they'll fail (at least, on the drivers that use videobuf), as streaming format will be stored at the videobuf handling (at buffer_prepare callback).

So, if you change the format, the change will be applied only at the next call to REQBUFS.

This behavior isn't according to the spec. G/S_FMT relate to the current format, not to some future format. Most non-videobuf drivers will not support this behavior.

It should be simple, really:

STREAMOFF
REQBUFS(0)

Quoted 6 lines

That's all that should be needed to stop streaming and return all buffers to the app. This is what uvc should also support (and I actually thought it did).

Attempts to change formats while buffers have been requested should be blocked with EBUSY. It's all perfectly reasonable. Well, perhaps next year we might succeed in having all drivers behave consistently...

You didn't understand me: uvcvideo is returning -EBUSY to format changes with buffers freed.

If I understood well, during probe time, qv4l2 calls REQBUFS(1)/REQBUFS(0) for both userptr and mmap, in order to detect what the device supports. This takes some locking schema inside uvcvideo, but, as uvcvideo is only releasing such lock at close(), it is impossible to change the video format. I even added an extra STREAMOFF at qv4l2 code, but this didn't solve the issue.

Well, maybe better than explaining it, you should try to change the video image size or format with qv4l2 and an uvc camera ;)

Cheers,
Mauro

#18 lore raw

Hi Mauro,

Quoted 3 lines · On Tuesday 21 December 2010 12:10:18 Mauro Carvalho Chehab wrote:

You didn't understand me: uvcvideo is returning -EBUSY to format changes with buffers freed.

As explained in my answer to Hans, that's on purpose.

The uvcvideo driver releases buffers when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

The reason for that is to avoid race conditions when an application wants to change the resolution during capture. As the application has to stop capture, call REQBUFS(0), change the format, request buffers and restart capture, it prevents another application from racing it after REQBUFS(0).

As Hans correctly pointed out, this should be implemented using the priority ioctls. I will fix that.

Signature · 3 lines
Regards,

Laurent Pinchart
#19 lore raw
Quoted 8 lines · On Thursday 23 December 2010 09:34:27 Laurent Pinchart wrote:

Hi Mauro,

Quoted 2 lines · On Tuesday 21 December 2010 12:10:18 Mauro Carvalho Chehab wrote:

You didn't understand me: uvcvideo is returning -EBUSY to format changes with buffers freed.

As explained in my answer to Hans, that's on purpose.

Quoted 8 lines

The uvcvideo driver releases buffers when calling REQBUFS(0). However, the file handle is still marked as owning the device for streaming purpose, so other applications can't change the format or request buffers.

The reason for that is to avoid race conditions when an application wants to change the resolution during capture. As the application has to stop capture, call REQBUFS(0), change the format, request buffers and restart capture, it prevents another application from racing it after REQBUFS(0).

And it's actually not correct. I've just tested the latest uvcvideo version, and I can start capture in a second application after calling REQBUFS(0) in the first one.

Quoted 2 lines

As Hans correctly pointed out, this should be implemented using the priority ioctls. I will fix that.

Signature · 3 lines
Regards,

Laurent Pinchart