Skip to contentSkip to conversation

drm/v3d: Fix memory leak in v3d_submit_cl_ioctl

PATCH In review Navid Emamdoost 3 participants 7 messages Oct 21, 2019 – Oct 23, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/v3d: Fix memory leak in v3d_submit_cl_ioctl

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks. One is when allocation for bin fails, and the other is when bin initialization fails. If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Fixes a783a09ee76d ("drm/v3d: Refactor job management.")
---
 drivers/gpu/drm/v3d/v3d_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +4 -1
index 5d80507b539b..19c092d75266 100644
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
 	if (args->bcl_start != args->bcl_end) {
 		bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
-		if (!bin)
+		if (!bin) {
+			v3d_job_put(&render->base);
 			return -ENOMEM;
+		}
 
 		ret = v3d_job_init(v3d, file_priv, &bin->base,
 				   v3d_job_free, args->in_sync_bcl);
 		if (ret) {
 			v3d_job_put(&render->base);
+			kfree(bin);
 			return ret;
 		}
 
Signature · 1 line
2.17.1
#1 lore raw
Quoted 1 line

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks.

Please avoid another typo also in this change description.

Quoted 3 lines

… If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Will an “imperative mood” be more appropriate for such wordings?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=7d194c2100ad2a6dded545887d02754948ca5241#n151

…

Quoted 2 lines

+++ b/drivers/gpu/drm/v3d/v3d_gem.c

(quoted diff) +0 -0
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,

…

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 569

Quoted 3 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
 		if (ret) {
 			v3d_job_put(&render->base);
+			kfree(bin);

…

Can it be helpful to move the added function call before the other in this if branch (if you prefer to avoid the addition of a jump target here)?

Regards,
Markus

#2 lore raw
Quoted 11 lines

…

Quoted 2 lines

+++ b/drivers/gpu/drm/v3d/v3d_gem.c

(quoted diff) +0 -0
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,

…

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 569

Quoted 3 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
 		if (ret) {
 			v3d_job_put(&render->base);
+			kfree(bin);

…

Can it be helpful to move the added function call before the other in this if branch (if you prefer to avoid the addition of a jump target here)?

I got into the mood to take another look at these implementation details. I suggest then to look at the commit 0d352a3a8a1f26168d09f7073e61bb4b328e3bb9 ("drm/v3d: don't leak bin job if v3d_job_init fails." from 2019-09-18) once more.

With which software versions did you perform your source code analysis?

Regards,
Markus

#3 lore raw
In reply to Navid Emamdoost
Quoted 17 lines · Navid Emamdoost

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks. One is when allocation for bin fails, and the other is when bin initialization fails. If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Fixes a783a09ee76d ("drm/v3d: Refactor job management.")
---
 drivers/gpu/drm/v3d/v3d_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
index 5d80507b539b..19c092d75266 100644

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 561

Quoted 6 lines
drivers/gpu/drm/v3d/v3d_gem.c +2 -1
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
 	if (args->bcl_start != args->bcl_end) {
 		bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
-		if (!bin)
+		if (!bin) {
+			v3d_job_put(&render->base);

The job isn't initialized yet, this doesn't work.

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 568

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
 			return -ENOMEM;
+		}
 
 		ret = v3d_job_init(v3d, file_priv, &bin->base,
 				   v3d_job_free, args->in_sync_bcl);
 		if (ret) {
 			v3d_job_put(&render->base);

v3d_job_put will call kfree, if you chase the callchain long enough (in v3d_job_free). So no bug here, this would lead to a double kfree and crash.
-Daniel

Quoted 7 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
+			kfree(bin);
 			return ret;
 		}
 
Signature · 1 line
2.17.1
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
#4 lore raw
Quoted 20 lines · Daniel Vetter
Quoted 18 lines · Navid Emamdoost

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks. One is when allocation for bin fails, and the other is when bin initialization fails. If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Fixes a783a09ee76d ("drm/v3d: Refactor job management.")
---
 drivers/gpu/drm/v3d/v3d_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
index 5d80507b539b..19c092d75266 100644
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
Quoted 7 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 561

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_gem.c +2 -1
      if (args->bcl_start != args->bcl_end) {
              bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
-             if (!bin)
+             if (!bin) {
+                     v3d_job_put(&render->base);

The job isn't initialized yet, this doesn't work.

Do you mean we have to release render via kfree() here?

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
                      return -ENOMEM;
+             }
 
Quoted 8 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 568

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
              ret = v3d_job_init(v3d, file_priv, &bin->base,
                                 v3d_job_free, args->in_sync_bcl);
              if (ret) {
                      v3d_job_put(&render->base);

v3d_job_put will call kfree, if you chase the callchain long enough (in v3d_job_free). So no bug here, this would lead to a double kfree and crash.

Yes, v3d_job_put() takes care of render,

Quoted 3 lines

-Daniel

Quoted 1 line
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
+                     kfree(bin);

but how about leaking bin?

Quoted 11 lines
Quoted 6 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -1
                      return ret;
              }
 
--

2.17.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 1 line
Navid.
Reviewed-by
#5 lore raw
Quoted 42 lines · Navid Emamdoost
Quoted 20 lines · Daniel Vetter
Quoted 18 lines · Navid Emamdoost

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks. One is when allocation for bin fails, and the other is when bin initialization fails. If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Fixes a783a09ee76d ("drm/v3d: Refactor job management.")
---
 drivers/gpu/drm/v3d/v3d_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
index 5d80507b539b..19c092d75266 100644
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
Quoted 7 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 561

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_gem.c +2 -1
      if (args->bcl_start != args->bcl_end) {
              bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
-             if (!bin)
+             if (!bin) {
+                     v3d_job_put(&render->base);

The job isn't initialized yet, this doesn't work.

Do you mean we have to release render via kfree() here?

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
                      return -ENOMEM;
+             }
 
Quoted 8 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 568

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
              ret = v3d_job_init(v3d, file_priv, &bin->base,
                                 v3d_job_free, args->in_sync_bcl);
              if (ret) {
                      v3d_job_put(&render->base);

v3d_job_put will call kfree, if you chase the callchain long enough (in v3d_job_free). So no bug here, this would lead to a double kfree and crash.

Quoted 6 lines

Yes, v3d_job_put() takes care of render,

Quoted 3 lines

-Daniel

Quoted 1 line
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
+                     kfree(bin);

but how about leaking bin?

Sorry, I totally missed that this is bin, no render. Patch looks correct to me.

Quoted 17 lines
Quoted 12 lines
Quoted 7 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -1
                      return ret;
              }
 
--

2.17.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 1 line
Navid.
Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#6 lore raw
Quoted 47 lines · Daniel Vetter
Quoted 46 lines · Navid Emamdoost
Quoted 20 lines · Daniel Vetter
Quoted 18 lines · Navid Emamdoost

In the impelementation of v3d_submit_cl_ioctl() there are two memory leaks. One is when allocation for bin fails, and the other is when bin initialization fails. If kcalloc fails to allocate memory for bin then render->base should be put. Also, if v3d_job_init() fails to initialize bin->base then allocated memory for bin should be released.

Fixes a783a09ee76d ("drm/v3d: Refactor job management.")
---
 drivers/gpu/drm/v3d/v3d_gem.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
index 5d80507b539b..19c092d75266 100644
@@ -557,13 +557,16 @@ v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
 
Quoted 7 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 561

Quoted 5 lines
drivers/gpu/drm/v3d/v3d_gem.c +2 -1
      if (args->bcl_start != args->bcl_end) {
              bin = kcalloc(1, sizeof(*bin), GFP_KERNEL);
-             if (!bin)
+             if (!bin) {
+                     v3d_job_put(&render->base);

The job isn't initialized yet, this doesn't work.

Do you mean we have to release render via kfree() here?

Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
                      return -ENOMEM;
+             }
 
Quoted 8 lines

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 568

Quoted 4 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -0
              ret = v3d_job_init(v3d, file_priv, &bin->base,
                                 v3d_job_free, args->in_sync_bcl);
              if (ret) {
                      v3d_job_put(&render->base);

v3d_job_put will call kfree, if you chase the callchain long enough (in v3d_job_free). So no bug here, this would lead to a double kfree and crash.

Yes, v3d_job_put() takes care of render,

Quoted 2 lines

-Daniel

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

drivers/gpu/drm/v3d/v3d_gem.c v3d_submit_cl_ioctl(struct drm_device *dev, void *data, · line 569

Quoted 1 line
drivers/gpu/drm/v3d/v3d_gem.c +1 -0
+                     kfree(bin);

but how about leaking bin?

Sorry, I totally missed that this is bin, no render. Patch looks correct to me.

Double-checked with Eric and applied to drm-misc-fixes. -Daniel

Quoted 23 lines
Quoted 18 lines
Quoted 13 lines
Quoted 8 lines
drivers/gpu/drm/v3d/v3d_gem.c +0 -1
                      return ret;
              }
 
--

2.17.1

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

Signature · 1 line
Navid.
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch