diff options
author | Hans Verkuil <hans.verkuil@cisco.com> | 2015-01-19 04:16:18 -0500 |
---|---|---|
committer | Mauro Carvalho Chehab <mchehab@osg.samsung.com> | 2015-01-21 18:07:26 -0500 |
commit | 6cf11ee6300f38b7cfc43af9b7be2afaa5e05869 (patch) | |
tree | d6df278332bd72f37c6d6f38586d1fc2a7b57cb5 /drivers/media/v4l2-core | |
parent | 42d74e4fe6508308abc1baac95ba36ad0cc5143e (diff) |
[media] vb2: fix vb2_thread_stop race conditions
The locking scheme inside the vb2 thread is unsafe when stopping the
thread. In particular kthread_stop was called *after* internal data
structures were cleaned up instead of doing that before. In addition,
internal vb2 functions were called after threadio->stop was set to
true and vb2_internal_streamoff was called. This is also not allowed.
All this led to a variety of race conditions and kernel warnings and/or
oopses.
Fixed by moving the kthread_stop call up before the cleanup takes
place, and by checking threadio->stop before calling internal vb2
queuing operations.
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Cc: <stable@vger.kernel.org> # for v3.16 and up
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Diffstat (limited to 'drivers/media/v4l2-core')
-rw-r--r-- | drivers/media/v4l2-core/videobuf2-core.c | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c index d09a8916e940..bc08a829bc13 100644 --- a/drivers/media/v4l2-core/videobuf2-core.c +++ b/drivers/media/v4l2-core/videobuf2-core.c | |||
@@ -3146,27 +3146,26 @@ static int vb2_thread(void *data) | |||
3146 | prequeue--; | 3146 | prequeue--; |
3147 | } else { | 3147 | } else { |
3148 | call_void_qop(q, wait_finish, q); | 3148 | call_void_qop(q, wait_finish, q); |
3149 | ret = vb2_internal_dqbuf(q, &fileio->b, 0); | 3149 | if (!threadio->stop) |
3150 | ret = vb2_internal_dqbuf(q, &fileio->b, 0); | ||
3150 | call_void_qop(q, wait_prepare, q); | 3151 | call_void_qop(q, wait_prepare, q); |
3151 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); | 3152 | dprintk(5, "file io: vb2_dqbuf result: %d\n", ret); |
3152 | } | 3153 | } |
3153 | if (threadio->stop) | 3154 | if (ret || threadio->stop) |
3154 | break; | ||
3155 | if (ret) | ||
3156 | break; | 3155 | break; |
3157 | try_to_freeze(); | 3156 | try_to_freeze(); |
3158 | 3157 | ||
3159 | vb = q->bufs[fileio->b.index]; | 3158 | vb = q->bufs[fileio->b.index]; |
3160 | if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR)) | 3159 | if (!(fileio->b.flags & V4L2_BUF_FLAG_ERROR)) |
3161 | ret = threadio->fnc(vb, threadio->priv); | 3160 | if (threadio->fnc(vb, threadio->priv)) |
3162 | if (ret) | 3161 | break; |
3163 | break; | ||
3164 | call_void_qop(q, wait_finish, q); | 3162 | call_void_qop(q, wait_finish, q); |
3165 | if (set_timestamp) | 3163 | if (set_timestamp) |
3166 | v4l2_get_timestamp(&fileio->b.timestamp); | 3164 | v4l2_get_timestamp(&fileio->b.timestamp); |
3167 | ret = vb2_internal_qbuf(q, &fileio->b); | 3165 | if (!threadio->stop) |
3166 | ret = vb2_internal_qbuf(q, &fileio->b); | ||
3168 | call_void_qop(q, wait_prepare, q); | 3167 | call_void_qop(q, wait_prepare, q); |
3169 | if (ret) | 3168 | if (ret || threadio->stop) |
3170 | break; | 3169 | break; |
3171 | } | 3170 | } |
3172 | 3171 | ||
@@ -3235,11 +3234,11 @@ int vb2_thread_stop(struct vb2_queue *q) | |||
3235 | threadio->stop = true; | 3234 | threadio->stop = true; |
3236 | vb2_internal_streamoff(q, q->type); | 3235 | vb2_internal_streamoff(q, q->type); |
3237 | call_void_qop(q, wait_prepare, q); | 3236 | call_void_qop(q, wait_prepare, q); |
3237 | err = kthread_stop(threadio->thread); | ||
3238 | q->fileio = NULL; | 3238 | q->fileio = NULL; |
3239 | fileio->req.count = 0; | 3239 | fileio->req.count = 0; |
3240 | vb2_reqbufs(q, &fileio->req); | 3240 | vb2_reqbufs(q, &fileio->req); |
3241 | kfree(fileio); | 3241 | kfree(fileio); |
3242 | err = kthread_stop(threadio->thread); | ||
3243 | threadio->thread = NULL; | 3242 | threadio->thread = NULL; |
3244 | kfree(threadio); | 3243 | kfree(threadio); |
3245 | q->fileio = NULL; | 3244 | q->fileio = NULL; |