Skip to content

Commit a2633dc

Browse files
teksturiUwe Kleine-König
authored andcommitted
rust: pwm: Fix potential memory leak on init error
When initializing a PWM chip using pwmchip_alloc(), the allocated device owns an initial reference that must be released on all error paths. If __pinned_init() were to fail, the allocated pwm_chip would currently leak because the error path returns without calling pwmchip_put(). Fixes: 7b3dce8 ("rust: pwm: Add Kconfig and basic data structures") Signed-off-by: Kari Argillander <kari.argillander@gmail.com> Acked-by: Michal Wilczynski <m.wilczynski@samsung.com> Link: https://patch.msgid.link/20260102-pwm-rust-v2-1-2702ce57d571@gmail.com Signed-off-by: Uwe Kleine-König <ukleinek@kernel.org>
1 parent b0dc6c6 commit a2633dc

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

rust/kernel/pwm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,11 @@ impl<T: PwmOps> Chip<T> {
607607
let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
608608

609609
// SAFETY: We construct the `T` object in-place in the allocated private memory.
610-
unsafe { data.__pinned_init(drvdata_ptr.cast())? };
610+
unsafe { data.__pinned_init(drvdata_ptr.cast()) }.inspect_err(|_| {
611+
// SAFETY: It is safe to call `pwmchip_put()` with a valid pointer obtained
612+
// from `pwmchip_alloc()`. We will not use pointer after this.
613+
unsafe { bindings::pwmchip_put(c_chip_ptr) }
614+
})?;
611615

612616
// SAFETY: `c_chip_ptr` points to a valid chip.
613617
unsafe {

0 commit comments

Comments
 (0)