Skip to content

Commit 4ea3b2e

Browse files
Eliot Courtneygregkh
authored andcommitted
rust: drm: gem: clean up GEM state in init failure case
commit 2e42a17 upstream. Currently, if `drm_gem_object_init` fails, the object is freed without any cleanup. Perform the cleanup in that case. Cc: stable@vger.kernel.org Fixes: c284d3e ("rust: drm: gem: Add GEM object abstraction") Signed-off-by: Eliot Courtney <ecourtney@nvidia.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Onur Özkan <work@onurozkan.dev> Link: https://patch.msgid.link/20260423-fix-gem-1-v1-1-e12e35f7bba9@nvidia.com [ Move safety comment closer to unsafe block to avoid a clippy warning. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 32bd343 commit 4ea3b2e

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

rust/kernel/drm/gem/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,17 @@ impl<T: DriverObject> Object<T> {
232232
// SAFETY: `obj.as_raw()` is guaranteed to be valid by the initialization above.
233233
unsafe { (*obj.as_raw()).funcs = &Self::OBJECT_FUNCS };
234234

235-
// SAFETY: The arguments are all valid per the type invariants.
236-
to_result(unsafe { bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size) })?;
235+
if let Err(err) =
236+
// SAFETY: The arguments are all valid per the type invariants.
237+
to_result(unsafe {
238+
bindings::drm_gem_object_init(dev.as_raw(), obj.obj.get(), size)
239+
})
240+
{
241+
// SAFETY: `drm_gem_object_init()` initializes the private GEM object state before
242+
// failing, so `drm_gem_private_object_fini()` is the matching cleanup.
243+
unsafe { bindings::drm_gem_private_object_fini(obj.obj.get()) };
244+
return Err(err);
245+
}
237246

238247
// SAFETY: We never move out of `Self`.
239248
let ptr = KBox::into_raw(unsafe { Pin::into_inner_unchecked(obj) });

0 commit comments

Comments
 (0)