summaryrefslogtreecommitdiff
path: root/rust
diff options
context:
space:
mode:
authorKari Argillander <kari.argillander@gmail.com>2026-01-02 09:51:41 +0200
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2026-02-26 15:01:01 -0800
commitbaa8b7097d9cc68ff85819cf683972a58c2ce32b (patch)
treedadd7c50a1183cf07b8c0295f25da5caab9094f6 /rust
parenta8de694aa714ce78a621068b8f5746a3a0877865 (diff)
rust: pwm: Fix potential memory leak on init error
[ Upstream commit a2633dc243c35754a0c2270131d8a199c987c9bf ] 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: 7b3dce814a15 ("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> Signed-off-by: Sasha Levin <sashal@kernel.org>
Diffstat (limited to 'rust')
-rw-r--r--rust/kernel/pwm.rs6
1 files changed, 5 insertions, 1 deletions
diff --git a/rust/kernel/pwm.rs b/rust/kernel/pwm.rs
index cb00f8a8765c..2ba9cfd02bfd 100644
--- a/rust/kernel/pwm.rs
+++ b/rust/kernel/pwm.rs
@@ -601,7 +601,11 @@ impl<T: PwmOps> Chip<T> {
let drvdata_ptr = unsafe { bindings::pwmchip_get_drvdata(c_chip_ptr) };
// SAFETY: We construct the `T` object in-place in the allocated private memory.
- unsafe { data.__pinned_init(drvdata_ptr.cast())? };
+ unsafe { data.__pinned_init(drvdata_ptr.cast()) }.inspect_err(|_| {
+ // SAFETY: It is safe to call `pwmchip_put()` with a valid pointer obtained
+ // from `pwmchip_alloc()`. We will not use pointer after this.
+ unsafe { bindings::pwmchip_put(c_chip_ptr) }
+ })?;
// SAFETY: `c_chip_ptr` points to a valid chip.
unsafe {