drm, i915: Fix memory leak in i915_gem_busy_ioctl().
Subsystems (2)
PATCH 1/1 drm, i915: Fix memory leak in i915_gem_busy_ioctl().
A call to i915_add_request() has been made in function i915_gem_busy_ioctl(). i915_add_request can fail, so in it's exit path previously allocated memory needs to be freed.
---
drivers/gpu/drm/i915/i915_gem.c +4 -2
@@ -3512,9 +3512,11 @@ i915_gem_busy_ioctl(struct drm_device *dev, void *data,* so emit a request to do so. */ request =kzalloc (sizeof (*request),GFP_KERNEL ); -if (request) +if (request) { ret =i915_add_request (obj->ring,NULL , request); -else +if (ret) +kfree (request); + }else ret = -ENOMEM ; }
Quoted 5 lines · Rakib Mullick
A call to
i915_add_request()has been made in functioni915_gem_busy_ioctl().i915_add_requestcan fail, so in it's exit path previously allocated memory needs to be freed.Signed-off-by Rakib Mullick <[email protected]>
Indeed, nice catch (albeit totally unlikely to be hit, because the error only happens when the gpu ceases to progress in the ring, so imo not stable material). Keith, please pick this up for fixes, thanks.
Signature · 3 lines
Daniel Vetter Mail: [email protected] Mobile: +41 (0)79 365 57 48
Quoted 3 lines · Daniel Vetter
Indeed, nice catch (albeit totally unlikely to be hit, because the error only happens when the gpu ceases to progress in the ring, so imo not stable material). Keith, please pick this up for fixes, thanks.
It's already there and queued for airlied :-)
Signature · 1 line
[email protected]
Quoted 8 lines · Keith Packard
Quoted 3 lines · Daniel Vetter
Indeed, nice catch (albeit totally unlikely to be hit, because the error only happens when the gpu ceases to progress in the ring, so imo not stable material). Keith, please pick this up for fixes, thanks.
It's already there and queued for airlied :-)
Thank you guys for reviewing and taking the patch.
Now, while I was looking at the uses of i915_add_request(), I found the following code :
ret = i915_gem_flush_ring(ring, 0,
I915_GEM_GPU_DOMAINS);
request = kzalloc(sizeof(*request), GFP_KERNEL);
if (ret || request == NULL ||
i915_add_request(ring, NULL, request))
kfree(request);Quoted 1 line
From above code, we might ended up by calling kfree(request) without
allocating request. Though, it's unlikely cause if request is NULL then BUG_ON will be hit in i915_add_request(). So, to unify the callee uses of i915_add_request(), I'm proposing the following patch. Please let me know what do you guys think. If you guys agree, I can sent a formal patch.
modi.linux-3.1.1/drivers/gpu/drm/i915/i915_gem.c +5 -5
@@ -1736,8 +1736,6 @@ i915_add_request(struct intel_ring_buffeint was_empty;int ret; -BUG_ON (request ==NULL ); - ret = ring->add_request (ring, &seqno);if (ret)return ret;
@@ -2002,9 +2000,11 @@ i915_gem_retire_work_handler(struct work ret =i915_gem_flush_ring (ring,0 ,I915_GEM_GPU_DOMAINS ); request =kzalloc (sizeof (*request),GFP_KERNEL ); -if (ret || request ==NULL || -i915_add_request (ring,NULL , request)) -kfree (request); +if (request) { + ret =i915_add_request (ring,NULL , request); +if (ret) +kfree (request); + } } idle &=list_empty (&ring->request_list);
Thanks,
Rakib
Quoted 21 lines · Rakib Mullick
Quoted 8 lines · Keith Packard
Quoted 3 lines · Daniel Vetter
Indeed, nice catch (albeit totally unlikely to be hit, because the error only happens when the gpu ceases to progress in the ring, so imo not stable material). Keith, please pick this up for fixes, thanks.
It's already there and queued for airlied :-)
Thank you guys for reviewing and taking the patch.
Now, while I was looking at the uses of
i915_add_request(), I found the following code :ret = i915_gem_flush_ring(ring, 0, I915_GEM_GPU_DOMAINS); request = kzalloc(sizeof(*request), GFP_KERNEL); if (ret || request == NULL || i915_add_request(ring, NULL, request)) kfree(request);
Quoted 6 lines
From above code, we might ended up by calling kfree(request) without allocating request. Though, it's unlikely cause if request is NULL then
BUG_ONwill be hit ini915_add_request(). So, to unify the callee uses ofi915_add_request(), I'm proposing the following patch. Please let me know what do you guys think. If you guys agree, I can sent a formal patch.
kfree(NULL) is permitted and taken advantage of here along with the short-circuiting behaviour of '||'.
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Quoted 29 lines · Chris Wilson
Quoted 21 lines · Rakib Mullick
Quoted 8 lines · Keith Packard
Quoted 3 lines · Daniel Vetter
Indeed, nice catch (albeit totally unlikely to be hit, because the error only happens when the gpu ceases to progress in the ring, so imo not stable material). Keith, please pick this up for fixes, thanks.
It's already there and queued for airlied :-)
Thank you guys for reviewing and taking the patch.
Now, while I was looking at the uses of
i915_add_request(), I found the following code :ret =
i915_gem_flush_ring(ring, 0, I915_GEM_GPU_DOMAINS);
request =kzalloc(sizeof(*request), GFP_KERNEL); if (ret || request == NULL ||i915_add_request(ring, NULL, request)) kfree(request);Quoted 6 lines
From above code, we might ended up by calling kfree(request) without allocating request. Though, it's unlikely cause if request is NULL then
BUG_ONwill be hit ini915_add_request(). So, to unify the callee uses ofi915_add_request(), I'm proposing the following patch. Please let me know what do you guys think. If you guys agree, I can sent a formal patch.
Quoted 2 lines
kfree(NULL) is permitted and taken advantage of here along with the short-circuiting behaviour of '||'.
Yes, no real problem with current code. I was just thinking from code cleanup's pov. Is BUG_ON really needed in i915_add_request() ?
thanks,
Rakib
Quoted 2 lines · Rakib Mullick
Yes, no real problem with current code. I was just thinking from code cleanup's pov. Is
BUG_ONreally needed ini915_add_request()?
No, just documentation as a reminder that the request should be preallocated, ideally so that we can fail gracefully without touching hardware and leaving it in an inconsistent state wrt to our bookkeeping. (This is more apparent in the overlay code which could hang the chip/driver if we hit a malloc error too late.)
The BUG_ON has certainly outlived its usefulness.
-Chris
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
Quoted 11 lines · Chris Wilson
Quoted 2 lines · Rakib Mullick
Yes, no real problem with current code. I was just thinking from code cleanup's pov. Is
BUG_ONreally needed ini915_add_request()?No, just documentation as a reminder that the request should be preallocated, ideally so that we can fail gracefully without touching hardware and leaving it in an inconsistent state wrt to our bookkeeping. (This is more apparent in the overlay code which could hang the chip/driver if we hit a malloc error too late.)
The
BUG_ONhas certainly outlived its usefulness.
Actually, I'm not seeing how BUG_ON could trigger (though, I've wrongly mentioned in previous thread, if request == NULL, BUG_ON could trigger), it's usefulness will never come into action. Other callers of i915_add_request also makes sure that, it gets called only if (request).
Although, kfree(NULL) is permitted, we shouldn't use it unnecessarily. Anyway, since the issue is not a big deal and no real bug, it could be droped.
Thanks,
Rakib