Skip to contentSkip to conversation

media: vimc: streamer: fix memory leak in vimc subdevs if kthread_run fails

PATCH In review Dafna Hirschfeld 2 participants 4 messages Jan 13, 2020 Media (V4L/DVB)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: vimc: streamer: fix memory leak in vimc subdevs if kthread_run fails

In case kthread_run fails, the vimc subdevices should be notified that streaming stopped so they can release the memory for the streaming. Also, kthread should be set to NULL.

---
 drivers/media/platform/vimc/vimc-streamer.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/media/platform/vimc/vimc-streamer.c +7 -2
index 5c5d3c068398..47a7470e581e 100644
@@ -207,8 +207,13 @@ int vimc_streamer_s_stream(struct vimc_stream *stream,
 		stream->kthread = kthread_run(vimc_streamer_thread, stream,
 					      "vimc-streamer thread");
 
-		if (IS_ERR(stream->kthread))
-			return PTR_ERR(stream->kthread);
+		if (IS_ERR(stream->kthread)) {
+			ret = PTR_ERR(stream->kthread);
+			dev_err(ved->dev, "kthread_run failed with %d\n", ret);
+			vimc_streamer_pipeline_terminate(stream);
+			stream->kthread = NULL;
+			return ret;
+		}
 
 	} else {
 		if (!stream->kthread)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 media: vimc: streamer: if kthread_stop fails, ignore the error

Ignore errors returned from kthread_stop since the vimc subdevices should still be notified that streaming stopped so they can release the memory for the streaming, and also kthread should be set to NULL. kthread_stop can return -EINTR in case the thread did not yet ran. This can happen if userspace calls streamon and streamoff right after.

---
 drivers/media/platform/vimc/vimc-streamer.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/platform/vimc/vimc-streamer.c +1 -2
index cd6b55433c9e..5c5d3c068398 100644
@@ -216,8 +216,7 @@ int vimc_streamer_s_stream(struct vimc_stream *stream,
 
 		ret = kthread_stop(stream->kthread);
 		if (ret)
-			return ret;
-
+			dev_warn(ved->dev, "kthread_stop returned '%d'\n", ret);
 		stream->kthread = NULL;
 
 		vimc_streamer_pipeline_terminate(stream);
Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH] media: vimc: streamer: if kthread_stop fails, ignore the error

Quoted 18 lines · Dafna Hirschfeld

Ignore errors returned from kthread_stop since the vimc subdevices should still be notified that streaming stopped so they can release the memory for the streaming, and also kthread should be set to NULL. kthread_stop can return -EINTR in case the thread did not yet ran. This can happen if userspace calls streamon and streamoff right after.

---
 drivers/media/platform/vimc/vimc-streamer.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/platform/vimc/vimc-streamer.c +0 -0
index cd6b55433c9e..5c5d3c068398 100644

drivers/media/platform/vimc/vimc-streamer.c int vimc_streamer_s_stream(struct vimc_stream *stream, · line 217

Quoted 2 lines
drivers/media/platform/vimc/vimc-streamer.c +0 -0
@@ -216,8 +216,7 @@ int vimc_streamer_s_stream(struct vimc_stream *stream,
 
 		ret = kthread_stop(stream->kthread);

Could you add a comment here too? To make it clean in the code why we are ignoring this error?

drivers/media/platform/vimc/vimc-streamer.c int vimc_streamer_s_stream(struct vimc_stream *stream, · line 220

Quoted 3 lines
drivers/media/platform/vimc/vimc-streamer.c +0 -2
 		if (ret)
-			return ret;
-

No need to remove this line.

drivers/media/platform/vimc/vimc-streamer.c int vimc_streamer_s_stream(struct vimc_stream *stream, · line 222

Quoted 5 lines
drivers/media/platform/vimc/vimc-streamer.c +1 -0
+			dev_warn(ved->dev, "kthread_stop returned '%d'\n", ret);
 		stream->kthread = NULL;
 
 		vimc_streamer_pipeline_terminate(stream);

Thanks
Helen

Acked-by on v1 1/1
#3 lore raw
In reply to Dafna Hirschfeld
Quoted 6 lines · Dafna Hirschfeld

In case kthread_run fails, the vimc subdevices should be notified that streaming stopped so they can release the memory for the streaming. Also, kthread should be set to NULL.

Thanks
Helen

Quoted 25 lines
---
 drivers/media/platform/vimc/vimc-streamer.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/media/platform/vimc/vimc-streamer.c +7 -2
index 5c5d3c068398..47a7470e581e 100644
@@ -207,8 +207,13 @@ int vimc_streamer_s_stream(struct vimc_stream *stream,
 		stream->kthread = kthread_run(vimc_streamer_thread, stream,
 					      "vimc-streamer thread");
 
-		if (IS_ERR(stream->kthread))
-			return PTR_ERR(stream->kthread);
+		if (IS_ERR(stream->kthread)) {
+			ret = PTR_ERR(stream->kthread);
+			dev_err(ved->dev, "kthread_run failed with %d\n", ret);
+			vimc_streamer_pipeline_terminate(stream);
+			stream->kthread = NULL;
+			return ret;
+		}
 
 	} else {
 		if (!stream->kthread)