Make sure .device_run is always called in non-atomic context
Subsystems (2)
ezequiel at collabora.com (Ezequiel Garcia)
v2 cover Make sure .device_run is always called in non-atomic context
This series goal is to avoid drivers from having ad-hoc code to call .device_run in non-atomic context. Currently, .device_run can be called via v4l2_m2m_job_finish(), potentially running in interrupt context.
This series will be useful for the upcoming Request API, where drivers typically require .device_run to be called in non-atomic context for v4l2_ctrl_request_setup() calls.
The solution is to add a per-device worker that is scheduled by v4l2_m2m_job_finish, which replaces drivers having a threaded interrupt or similar.
This change allows v4l2_m2m_job_finish() to be called in interrupt context, separating .device_run and v4l2_m2m_job_finish() contexts.
It's worth mentioning that v4l2_m2m_cancel_job() doesn't need to flush or cancel the new worker, because the job_spinlock synchronizes both and also because the core prevents simultaneous jobs. Either v4l2_m2m_cancel_job() will wait for the worker, or the worker will be unable to run a new job.
While working on this series, I found a bug recently introduced on commit "media: mem2mem: Remove excessive try_run call". The first patch fixes the bug.
In order to test the change, and make sure no regressions are introduced, a kselftest test is added to stress the mem2mem framework.
Patches are based on v4.18-rc4 plus:
34dbb848d5e47 "media: mem2mem: Remove excessive try_run call"
Ezequiel Garcia (4):
v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
v4l2-mem2mem: Avoid v4l2_m2m_prepare_buf from scheduling a job
v4l2-mem2mem: Avoid calling .device_run in v4l2_m2m_job_finish
selftests: media_tests: Add a memory-to-memory concurrent stress test
Sakari Ailus (1):
v4l2-mem2mem: Simplify exiting the function in __v4l2_m2m_try_schedule
drivers/media/v4l2-core/v4l2-mem2mem.c | 104 +++++-- tools/testing/selftests/media_tests/Makefile | 4 +- .../selftests/media_tests/m2m_job_test.c | 283 ++++++++++++++++++ 3 files changed, 362 insertions(+), 29 deletions(-) create mode 100644 tools/testing/selftests/media_tests/m2m_job_test.c
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 1/5 v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
Commit 34dbb848d5e4 ("media: mem2mem: Remove excessive try_run call") removed a redundant call to v4l2_m2m_try_run but instead introduced a bug. Consider the following case:
1) Context A schedules, queues and runs job A.
2) While the m2m device is running, context B schedules and queues job B. Job B cannot run, because it has to wait for job A.
3) Job A completes, calls v4l2_m2m_job_finish, and tries to queue a job for context A, but since the context is empty it won't do anything.
In this scenario, queued job B will never run. Fix this by calling v4l2_m2m_try_run from v4l2_m2m_try_schedule.
While here, add more documentation to these functions.
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-mem2mem.c +29 -3
@@ -209,15 +209,23 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) m2m_dev->curr_ctx->job_flags |=TRANS_RUNNING ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags); +dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx); m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); } -void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +/* +* __v4l2_m2m_try_queue() - queue a job +* @m2m_dev: m2m device +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. +* +* This function can run in interrupt context. +*/ +static void __v4l2_m2m_try_queue (struct v4l2_m2m_dev *m2m_dev,struct v4l2_m2m_ctx *m2m_ctx) { -struct v4l2_m2m_dev *m2m_dev;unsigned long flags_job, flags_out, flags_cap; - m2m_dev = m2m_ctx->m2m_dev;dprintk ("Trying to schedule a job for m2m_ctx: %p\n" , m2m_ctx); if (!m2m_ctx->out_q_ctx.q.streaming
@@ -275,7 +283,25 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +} + +/** +* v4l2_m2m_try_schedule() - schedule and possibly run a job for any context +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. If suitable, +* run the next queued job on the mem2mem device. +* +* This function shouldn't run in interrupt context. +* +* Note that v4l2_m2m_try_schedule() can schedule one job for this context, +* and then run another job for another context. +*/ +void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +{ +struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev; +__v4l2_m2m_try_queue (m2m_dev, m2m_ctx);v4l2_m2m_try_run (m2m_dev); }EXPORT_SYMBOL_GPL (v4l2_m2m_try_schedule);
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
hverkuil at xs4all.nl (Hans Verkuil)
PATCH v2 1/5 v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
Quoted 12 lines · Ezequiel Garcia
Commit
34dbb848d5e4("media: mem2mem: Remove excessivetry_runcall") removed a redundant call tov4l2_m2m_try_runbut instead introduced a bug. Consider the following case:1) Context A schedules, queues and runs job A.
2) While the m2m device is running, context B schedules and queues job B. Job B cannot run, because it has to wait for job A.
3) Job A completes, callsv4l2_m2m_job_finish, and tries to queue a job for context A, but since the context is empty it won't do anything.
Quoted 7 lines
In this scenario, queued job B will never run. Fix this by calling
v4l2_m2m_try_runfromv4l2_m2m_try_schedule.While here, add more documentation to these functions.
Fixes 34dbb848d5e4 ("media: mem2mem: Remove excessive try_run call")Signed-off-by Ezequiel Garcia <ezequiel at collabora.com>
So just to be clear: this first patch fixes a regression and can be applied separately from the other patches?
drivers/media/v4l2-core/v4l2-mem2mem.c static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) · line 212
Quoted 13 lines
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0@@ -209,15 +209,23 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) m2m_dev->curr_ctx->job_flags |=TRANS_RUNNING ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags); +dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx);
Is this intended? It feels out of place in this patch.
Quoted 35 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +19
-3m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); } -void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +/* +* __v4l2_m2m_try_queue() - queue a job +* @m2m_dev: m2m device +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. +* +* This function can run in interrupt context. +*/ +static void __v4l2_m2m_try_queue (struct v4l2_m2m_dev *m2m_dev,struct v4l2_m2m_ctx *m2m_ctx) { -struct v4l2_m2m_dev *m2m_dev;unsigned long flags_job, flags_out, flags_cap; - m2m_dev = m2m_ctx->m2m_dev;dprintk ("Trying to schedule a job for m2m_ctx: %p\n" , m2m_ctx); if (!m2m_ctx->out_q_ctx.q.streaming@@ -275,7 +283,25 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +} + +/** +* v4l2_m2m_try_schedule() - schedule and possibly run a job for any context +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. If suitable, +* run the next queued job on the mem2mem device. +*
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 304
Quoted 10 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +9
-0+* This function shouldn't run in interrupt context. +* +* Note that v4l2_m2m_try_schedule() can schedule one job for this context, +* and then run another job for another context. +*/ +void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +{ +struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev; +__v4l2_m2m_try_queue (m2m_dev, m2m_ctx);
Why introduce __v4l2_m2m_try_queue? Why not keep it in here?
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 307
Quoted 4 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0v4l2_m2m_try_run (m2m_dev); }EXPORT_SYMBOL_GPL (v4l2_m2m_try_schedule);
Regards,
Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
hverkuil at xs4all.nl (Hans Verkuil)
PATCH v2 1/5 v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
Quoted 84 lines · Hans Verkuil
Quoted 12 lines · Ezequiel Garcia
Commit
34dbb848d5e4("media: mem2mem: Remove excessivetry_runcall") removed a redundant call tov4l2_m2m_try_runbut instead introduced a bug. Consider the following case:1) Context A schedules, queues and runs job A.
2) While the m2m device is running, context B schedules and queues job B. Job B cannot run, because it has to wait for job A.
3) Job A completes, callsv4l2_m2m_job_finish, and tries to queue a job for context A, but since the context is empty it won't do anything.Quoted 7 lines
In this scenario, queued job B will never run. Fix this by calling
v4l2_m2m_try_runfromv4l2_m2m_try_schedule.While here, add more documentation to these functions.
Fixes 34dbb848d5e4 ("media: mem2mem: Remove excessive try_run call")Signed-off-by Ezequiel Garcia <ezequiel at collabora.com>So just to be clear: this first patch fixes a regression and can be applied separately from the other patches?
drivers/media/v4l2-core/v4l2-mem2mem.c static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) · line 212
Quoted 13 lines
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0@@ -209,15 +209,23 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) m2m_dev->curr_ctx->job_flags |=TRANS_RUNNING ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags); +dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx);Is this intended? It feels out of place in this patch.
Quoted 44 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +27
-3m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); } -void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +/* +* __v4l2_m2m_try_queue() - queue a job +* @m2m_dev: m2m device +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. +* +* This function can run in interrupt context. +*/ +static void __v4l2_m2m_try_queue (struct v4l2_m2m_dev *m2m_dev,struct v4l2_m2m_ctx *m2m_ctx) { -struct v4l2_m2m_dev *m2m_dev;unsigned long flags_job, flags_out, flags_cap; - m2m_dev = m2m_ctx->m2m_dev;dprintk ("Trying to schedule a job for m2m_ctx: %p\n" , m2m_ctx); if (!m2m_ctx->out_q_ctx.q.streaming@@ -275,7 +283,25 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +} + +/** +* v4l2_m2m_try_schedule() - schedule and possibly run a job for any context +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. If suitable, +* run the next queued job on the mem2mem device. +* +* This function shouldn't run in interrupt context. +* +* Note that v4l2_m2m_try_schedule() can schedule one job for this context, +* and then run another job for another context. +*/ +void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +{ +struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;
Quoted 5 lines
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 304
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0+__v4l2_m2m_try_queue (m2m_dev, m2m_ctx);Why introduce
__v4l2_m2m_try_queue? Why not keep it in here?Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0v4l2_m2m_try_run (m2m_dev);
I'm completely confused: v4l2_m2m_try_schedule() already calls v4l2_m2m_try_run()!
Either my brain has crashed due to the heatwave I'm suffering through, or there is something amiss with this patch.
Regards,
Hans
Quoted 8 lines
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 307
Quoted 3 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0}EXPORT_SYMBOL_GPL (v4l2_m2m_try_schedule);Regards,
Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 1/5 v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
Quoted 17 lines · Hans Verkuil
Quoted 16 lines · Hans Verkuil
Quoted 15 lines · Ezequiel Garcia
Commit
34dbb848d5e4("media: mem2mem: Remove excessivetry_runcall") removed a redundant call tov4l2_m2m_try_runbut instead introduced a bug. Consider the following case:1) Context A schedules, queues and runs job A.
2) While the m2m device is running, context B schedules and queues job B. Job B cannot run, because it has to wait for job A.
3) Job A completes, callsv4l2_m2m_job_finish, and tries to queue a job for context A, but since the context is empty it won't do anything.In this scenario, queued job B will never run. Fix this by calling
v4l2_m2m_try_runfromv4l2_m2m_try_schedule.
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
While here, add more documentation to these functions.
Fixes 34dbb848d5e4 ("media: mem2mem: Remove excessive try_run call")Signed-off-by Ezequiel Garcia <ezequiel at collabora.com>So just to be clear: this first patch fixes a regression and can be applied separately from the other patches?
That is correct, it's a regression and can be applied independently.
This patch has been tested independently of the rest of the series, using the test introduced in "selftests: media_tests: Add a memory-to-memory concurrent stress test".
Without this patch, the test fails.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0@@ -209,15 +209,23 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) m2m_dev->curr_ctx->job_flags |=TRANS_RUNNING ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags);
Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-mem2mem.c static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) · line 212
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0+dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx);Is this intended? It feels out of place in this patch.
It was intended :) Since the patch is adding some more documentation, it felt OK to add this debug message, as it also helps the testing.
But I can drop it if you think it's out of place.
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +27
-3m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); } -void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +/* +* __v4l2_m2m_try_queue() - queue a job +* @m2m_dev: m2m device +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. +* +* This function can run in interrupt context. +*/ +static void __v4l2_m2m_try_queue (struct v4l2_m2m_dev *m2m_dev,struct v4l2_m2m_ctx *m2m_ctx) { -struct v4l2_m2m_dev *m2m_dev;unsigned long flags_job, flags_out, flags_cap; - m2m_dev = m2m_ctx->m2m_dev;dprintk ("Trying to schedule a job for m2m_ctx: %p\n" , m2m_ctx); if (!m2m_ctx->out_q_ctx.q.streaming@@ -275,7 +283,25 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +} + +/** +* v4l2_m2m_try_schedule() - schedule and possibly run a job for any context +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. If suitable, +* run the next queued job on the mem2mem device. +* +* This function shouldn't run in interrupt context. +* +* Note that v4l2_m2m_try_schedule() can schedule one job for this context, +* and then run another job for another context. +*/ +void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +{ +struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;
Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 304
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0+__v4l2_m2m_try_queue (m2m_dev, m2m_ctx);Why introduce
__v4l2_m2m_try_queue? Why not keep it in here?
Well, specifically we need to make sure xxx_try_run is called even if no job was queued. After a lot of back and forth, I ended up thinking the queue and run operations were different and needed different functions.
If we were to keep the code in try_schedule, we would have to use some extra conditionals or gotos to make sure try_run is called even if no job was queued.
The important thing to keep in mind here is that these functions could be called on a given context, but then actually run a job for another context.
Quoted 8 lines
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 305
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0v4l2_m2m_try_run (m2m_dev);I'm completely confused:
v4l2_m2m_try_schedule()already callsv4l2_m2m_try_run()!Either my brain has crashed due to the heatwave I'm suffering through, or there is something amiss with this patch.
Right, v4l2_m2m_try_schedule calls v4l2_m2m_try_run *if* a job was queued. This might not always be the case.
Consider you schedule two jobs, both will be queued but only one will run. When will the second job run? Answer: never :)
Hope it's clear now!
Eze
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
hverkuil at xs4all.nl (Hans Verkuil)
PATCH v2 1/5 v4l2-mem2mem: Fix missing v4l2_m2m_try_run call
Quoted 128 lines · Ezequiel Garcia
Quoted 17 lines · Hans Verkuil
Quoted 16 lines · Hans Verkuil
Quoted 15 lines · Ezequiel Garcia
Commit
34dbb848d5e4("media: mem2mem: Remove excessivetry_runcall") removed a redundant call tov4l2_m2m_try_runbut instead introduced a bug. Consider the following case:1) Context A schedules, queues and runs job A.
2) While the m2m device is running, context B schedules and queues job B. Job B cannot run, because it has to wait for job A.
3) Job A completes, callsv4l2_m2m_job_finish, and tries to queue a job for context A, but since the context is empty it won't do anything.In this scenario, queued job B will never run. Fix this by calling
v4l2_m2m_try_runfromv4l2_m2m_try_schedule.Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
While here, add more documentation to these functions.
Fixes 34dbb848d5e4 ("media: mem2mem: Remove excessive try_run call")Signed-off-by Ezequiel Garcia <ezequiel at collabora.com>So just to be clear: this first patch fixes a regression and can be applied separately from the other patches?
That is correct, it's a regression and can be applied independently.
This patch has been tested independently of the rest of the series, using the test introduced in "selftests:
media_tests: Add a memory-to-memory concurrent stress test".Without this patch, the test fails.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 32 +++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-)drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0@@ -209,15 +209,23 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) m2m_dev->curr_ctx->job_flags |=TRANS_RUNNING ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags);Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-mem2mem.c static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) · line 212
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0+dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx);Is this intended? It feels out of place in this patch.
It was intended :) Since the patch is adding some more documentation, it felt OK to add this debug message, as it also helps the testing.
But I can drop it if you think it's out of place.
Quoted 44 lines
Quoted 44 lines
Quoted 44 lines
drivers/media/v4l2-core/v4l2-mem2mem.c +27
-3m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); } -void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +/* +* __v4l2_m2m_try_queue() - queue a job +* @m2m_dev: m2m device +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. +* +* This function can run in interrupt context. +*/ +static void __v4l2_m2m_try_queue (struct v4l2_m2m_dev *m2m_dev,struct v4l2_m2m_ctx *m2m_ctx) { -struct v4l2_m2m_dev *m2m_dev;unsigned long flags_job, flags_out, flags_cap; - m2m_dev = m2m_ctx->m2m_dev;dprintk ("Trying to schedule a job for m2m_ctx: %p\n" , m2m_ctx); if (!m2m_ctx->out_q_ctx.q.streaming@@ -275,7 +283,25 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +} + +/** +* v4l2_m2m_try_schedule() - schedule and possibly run a job for any context +* @m2m_ctx: m2m context +* +* Check if this context is ready to queue a job. If suitable, +* run the next queued job on the mem2mem device. +* +* This function shouldn't run in interrupt context. +* +* Note that v4l2_m2m_try_schedule() can schedule one job for this context, +* and then run another job for another context. +*/ +void v4l2_m2m_try_schedule (struct v4l2_m2m_ctx *m2m_ctx) +{ +struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;Quoted 4 lines
Quoted 4 lines
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 304
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +1
-0+__v4l2_m2m_try_queue (m2m_dev, m2m_ctx);Why introduce
__v4l2_m2m_try_queue? Why not keep it in here?Well, specifically we need to make sure
xxx_try_runis called even if no job was queued. After a lot of back and forth, I ended up thinking the queue and run operations were different and needed different functions.If we were to keep the code in
try_schedule, we would have to use some extra conditionals or gotos to make suretry_runis called even if no job was queued.The important thing to keep in mind here is that these functions could be called on a given context, but then actually run a job for another context.
Quoted 8 lines
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) · line 305
Quoted 1 line
drivers/media/v4l2-core/v4l2-mem2mem.c +0
-0v4l2_m2m_try_run (m2m_dev);I'm completely confused:
v4l2_m2m_try_schedule()already callsv4l2_m2m_try_run()!Either my brain has crashed due to the heatwave I'm suffering through, or there is something amiss with this patch.
Quoted 5 lines
Right,
v4l2_m2m_try_schedulecallsv4l2_m2m_try_run*if* a job was queued. This might not always be the case.Consider you schedule two jobs, both will be queued but only one will run. When will the second job run? Answer: never :)
Ah, now I get it. I hadn't seen that the 'return' statements in try_schedule would prevent the try_run call at the end.
Thanks for the patch, I'll make a pull request for this one.
Regards,
Hans
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 2/5 v4l2-mem2mem: Avoid v4l2_m2m_prepare_buf from scheduling a job
In reply to ezequiel at collabora.com (Ezequiel Garcia)There is no need for v4l2_m2m_prepare_buf to try to schedule a job, as it only prepares a buffer, but does not queue or changes the state of the queue.
Remove the call to v4l2_m2m_try_schedule from v4l2_m2m_prepare_buf.
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-mem2mem.c +1 -6
@@ -439,14 +439,9 @@ int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, struct v4l2_buffer *buf) {struct vb2_queue *vq; -int ret; vq =v4l2_m2m_get_vq (m2m_ctx, buf->type); - ret =vb2_prepare_buf (vq, buf); -if (!ret) -v4l2_m2m_try_schedule (m2m_ctx); - -return ret; +return vb2_prepare_buf (vq, buf); }EXPORT_SYMBOL_GPL (v4l2_m2m_prepare_buf);
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 3/5 v4l2-mem2mem: Simplify exiting the function in __v4l2_m2m_try_schedule
In reply to ezequiel at collabora.com (Ezequiel Garcia)From: Sakari Ailus <[email protected]>
The __v4l2_m2m_try_schedule function acquires and releases multiple spinlocks. Simplify unlocking the job lock by adding labels to unlock the lock and exit the function.
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 29 ++++++++++++-------------- 1 file changed, 13 insertions(+), 16 deletions(-)
drivers/media/v4l2-core/v4l2-mem2mem.c +13 -16
@@ -238,51 +238,48 @@ static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev *m2m_dev, struct v4l2_m2m_c/* If the context is aborted then don't schedule it */ if (m2m_ctx->job_flags &TRANS_ABORT ) { -spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job);dprintk ("Aborted context\n" ); -return ; + goto job_unlock; }if (m2m_ctx->job_flags &TRANS_QUEUED ) { -spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job);dprintk ("On job queue already\n" ); -return ; + goto job_unlock; }spin_lock_irqsave (&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);if (list_empty (&m2m_ctx->out_q_ctx.rdy_queue) && !m2m_ctx->out_q_ctx.buffered) { -spin_unlock_irqrestore (&m2m_ctx->out_q_ctx.rdy_spinlock, - flags_out); -spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job);dprintk ("No input buffers available\n" ); -return ; + goto out_unlock; }spin_lock_irqsave (&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);if (list_empty (&m2m_ctx->cap_q_ctx.rdy_queue) && !m2m_ctx->cap_q_ctx.buffered) { -spin_unlock_irqrestore (&m2m_ctx->cap_q_ctx.rdy_spinlock, - flags_cap); -spin_unlock_irqrestore (&m2m_ctx->out_q_ctx.rdy_spinlock, - flags_out); -spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job);dprintk ("No output buffers available\n" ); -return ; + goto cap_unlock; }spin_unlock_irqrestore (&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap);spin_unlock_irqrestore (&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out);if (m2m_dev->m2m_ops->job_ready && (!m2m_dev->m2m_ops->job_ready (m2m_ctx->priv))) { -spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job);dprintk ("Driver not ready\n" ); -return ; + goto job_unlock; }list_add_tail (&m2m_ctx->queue, &m2m_dev->job_queue); m2m_ctx->job_flags |=TRANS_QUEUED ;spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); +return ; + +cap_unlock: +spin_unlock_irqrestore (&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap); +out_unlock: +spin_unlock_irqrestore (&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out); +job_unlock: +spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags_job); }/**
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 4/5 v4l2-mem2mem: Avoid calling .device_run in v4l2_m2m_job_finish
In reply to ezequiel at collabora.com (Ezequiel Garcia)v4l2_m2m_job_finish() is typically called in interrupt context.
Some implementation of .device_run might sleep, and so it's desirable to avoid calling it directly from v4l2_m2m_job_finish(), thus avoiding .device_run from running in interrupt context.
Implement a deferred context that calls v4l2_m2m_try_run, and gets scheduled by v4l2_m2m_job_finish().
--- drivers/media/v4l2-core/v4l2-mem2mem.c | 36 +++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/v4l2-mem2mem.c +33 -3
@@ -56,6 +56,7 @@ module_param(debug, bool, 0644);* @curr_ctx: currently running instance * @job_queue: instances queued to run * @job_spinlock: protects job_queue +* @job_work: worker to run queued jobs. * @m2m_ops: driver callbacks */ struct v4l2_m2m_dev {
@@ -63,6 +64,7 @@ struct v4l2_m2m_dev {struct list_head job_queue;spinlock_t job_spinlock; +struct work_struct job_work;const struct v4l2_m2m_ops *m2m_ops; };
@@ -184,10 +186,11 @@ EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);/** * v4l2_m2m_try_run() - select next job to perform and run it if possible * @m2m_dev: per-device context +* @try_lock: indicates if the queue lock should be taken * * Get next transaction (if present) from the waiting jobs list and run it. */ -static void v4l2_m2m_try_run (struct v4l2_m2m_dev *m2m_dev) +static void v4l2_m2m_try_run (struct v4l2_m2m_dev *m2m_dev,bool try_lock) {unsigned long flags;
@@ -210,7 +213,20 @@ static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)spin_unlock_irqrestore (&m2m_dev->job_spinlock, flags);dprintk ("Running job on m2m_ctx: %p\n" , m2m_dev->curr_ctx); + +/* +* A m2m context lock is taken only after a m2m context +* is picked from the queue and marked as running. +* The lock is only needed if v4l2_m2m_try_run is called +* from the async worker. +*/ +if (try_lock && m2m_dev->curr_ctx->q_lock) +mutex_lock (m2m_dev->curr_ctx->q_lock); + m2m_dev->m2m_ops->device_run (m2m_dev->curr_ctx->priv); + +if (try_lock && m2m_dev->curr_ctx->q_lock) +mutex_unlock (m2m_dev->curr_ctx->q_lock); }/*
@@ -299,10 +315,22 @@ void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)struct v4l2_m2m_dev *m2m_dev = m2m_ctx->m2m_dev;__v4l2_m2m_try_queue (m2m_dev, m2m_ctx); -v4l2_m2m_try_run (m2m_dev); +v4l2_m2m_try_run (m2m_dev, false); }EXPORT_SYMBOL_GPL (v4l2_m2m_try_schedule); +/** +* v4l2_m2m_device_run_work() - run pending jobs for the context +* @work: Work structure used for scheduling the execution of this function. +*/ +static void v4l2_m2m_device_run_work (struct work_struct *work) +{ +struct v4l2_m2m_dev *m2m_dev = + container_of(work, struct v4l2_m2m_dev, job_work); + +v4l2_m2m_try_run (m2m_dev, true); +} +/** * v4l2_m2m_cancel_job() - cancel pending jobs for the context * @m2m_ctx: m2m context with jobs to be canceled
@@ -361,7 +389,8 @@ void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,/* This instance might have more buffers ready, but since we do not * allow more than one job on the job_queue per instance, each has * to be scheduled separately after the previous one finishes. */ -v4l2_m2m_try_schedule (m2m_ctx); +__v4l2_m2m_try_queue (m2m_dev, m2m_ctx); +schedule_work (&m2m_dev->job_work); }EXPORT_SYMBOL (v4l2_m2m_job_finish);
@@ -628,6 +657,7 @@ struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops) m2m_dev->m2m_ops = m2m_ops;INIT_LIST_HEAD (&m2m_dev->job_queue);spin_lock_init (&m2m_dev->job_spinlock); +INIT_WORK (&m2m_dev->job_work, v4l2_m2m_device_run_work);return m2m_dev; }
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
ezequiel at collabora.com (Ezequiel Garcia)
PATCH v2 5/5 selftests: media_tests: Add a memory-to-memory concurrent stress test
In reply to ezequiel at collabora.com (Ezequiel Garcia)Add a test for the memory-to-memory framework, to exercise the scheduling of concurrent jobs, using multiple contexts.
This test needs to be run using the vim2m virtual driver, and so needs no hardware.
--- tools/testing/selftests/media_tests/Makefile | 4 +- .../selftests/media_tests/m2m_job_test.c | 283 ++++++++++++++++++ 2 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/media_tests/m2m_job_test.c
tools/testing/selftests/media_tests/Makefile +3 -1
@@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 # CFLAGS += -I../ -I../../../../usr/include/ -TEST_GEN_PROGS := media_device_test media_device_open video_device_test +TEST_GEN_PROGS := media_device_test media_device_open video_device_test m2m_job_test include ../lib.mk + +LDLIBS += -lpthread
tools/testing/selftests/media_tests/m2m_job_test.c new file +283 -0
283 lines not on this page · raw message
Signature · 6 lines
2.18.0 -- To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html