[media] vb2: fix 'UNBALANCED' warnings when calling vb2_thread_stop()
authorHans Verkuil <hverkuil@xs4all.nl>
Mon, 16 Feb 2015 10:49:07 +0000 (07:49 -0300)
committerSasha Levin <sasha.levin@oracle.com>
Fri, 24 Apr 2015 21:14:09 +0000 (17:14 -0400)
commitc6aa7146f15eb11c635dbbf48274db04a02241de
tree5a507217b603230341e25329f3e645748425999b
parent71871e8982c315c0fe3ba54c5b385f321135be7e
[media] vb2: fix 'UNBALANCED' warnings when calling vb2_thread_stop()

[ Upstream commit 0e661006370b7e7fb9ac9d94f9c3500a62cd559b ]

Stopping the vb2 thread (as used by several DVB devices) can result
in an 'UNBALANCED' warning such as this:

vb2: counters for queue ffff880407ee9828: UNBALANCED!
vb2:     setup: 1 start_streaming: 1 stop_streaming: 1
vb2:     wait_prepare: 249333 wait_finish: 249334

This is due to a race condition between stopping the thread and
calling vb2_internal_streamoff(). While I have not been able to deduce
the exact mechanism how this race condition can produce this warning,
I can see that the way the stream is stopped is likely to lead to a
race somewhere.

This patch simplifies how this is done by first ensuring that the
thread is completely stopped before cleaning up the vb2 queue. It
does that by setting threadio->stop to true, followed by a call to
vb2_queue_error() which will wake up the thread. The thread sees that
'stop' is true and it will exit.

The call to kthread_stop() waits until the thread has exited, and only
then is the queue cleaned up by calling __vb2_cleanup_fileio().

This is a much cleaner sequence and the warning has now disappeared.

Reported-by: Jurgen Kramer <gtmkramer@xs4all.nl>
Tested-by: Jurgen Kramer <gtmkramer@xs4all.nl>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Cc: <stable@vger.kernel.org> # for v3.18 and up
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
drivers/media/v4l2-core/videobuf2-core.c