[PATCH 4/4] drm/tyr: use ScopeGuard for firmware loading cleanup
From: Danilo Krummrich
Date: Wed Nov 18 2026 - 18:38:28 EST
Use ScopeGuard to call vm.kill() when firmware section loading fails.
Dismiss it on success, replacing the nested closure and explicit error
check.
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
---
drivers/gpu/drm/tyr/fw.rs | 59 ++++++++++++++++++---------------------
1 file changed, 27 insertions(+), 32 deletions(-)
diff --git a/drivers/gpu/drm/tyr/fw.rs b/drivers/gpu/drm/tyr/fw.rs
index bd377875778b..755112959ee2 100644
--- a/drivers/gpu/drm/tyr/fw.rs
+++ b/drivers/gpu/drm/tyr/fw.rs
@@ -29,7 +29,8 @@
num::Bounded,
prelude::*,
str::CString,
- time, //
+ time,
+ types::ScopeGuard, //
};
use crate::{
@@ -228,46 +229,40 @@ pub(crate) fn new<'a, 'res>(
sections: {
let vm = vm.get_ref();
vm.activate()?;
+ let guard = ScopeGuard::new(|| vm.kill());
- let result: Result<_> = (|| {
- let (fw, parsed_sections) = Self::load(dev, ddev, gpu_info)?;
- let mut sections = KVec::new();
- for parsed in parsed_sections {
- let size =
- u64::from(parsed.va.end.checked_sub(parsed.va.start).ok_or(EINVAL)?);
+ let (fw, parsed_sections) = Self::load(dev, ddev, gpu_info)?;
+ let mut sections = KVec::new();
+ for parsed in parsed_sections {
+ let size =
+ u64::from(parsed.va.end.checked_sub(parsed.va.start).ok_or(EINVAL)?);
- let va = u64::from(parsed.va.start);
+ let va = u64::from(parsed.va.start);
- let mut mem = KernelBo::new(
- ddev,
- vm,
- size,
- KernelBoVaAlloc::Explicit(va),
- parsed.vm_map_flags,
- )?;
+ let mut mem = KernelBo::new(
+ ddev,
+ vm,
+ size,
+ KernelBoVaAlloc::Explicit(va),
+ parsed.vm_map_flags,
+ )?;
- let section_start = parsed.data_range.start as usize;
- let section_end = parsed.data_range.end as usize;
- let mut data = KVec::new();
+ let section_start = parsed.data_range.start as usize;
+ let section_end = parsed.data_range.end as usize;
+ let mut data = KVec::new();
- // Ensure that the firmware slice is not out of bounds.
- let fw_data = fw.data();
- let bytes = fw_data.get(section_start..section_end).ok_or(EINVAL)?;
- data.extend_from_slice(bytes, GFP_KERNEL)?;
+ // Ensure that the firmware slice is not out of bounds.
+ let fw_data = fw.data();
+ let bytes = fw_data.get(section_start..section_end).ok_or(EINVAL)?;
+ data.extend_from_slice(bytes, GFP_KERNEL)?;
- Self::init_section_mem(dev, &mut mem, &data)?;
+ Self::init_section_mem(dev, &mut mem, &data)?;
- sections.push(Section { data, mem }, GFP_KERNEL)?;
- }
-
- Ok(sections)
- })();
-
- if result.is_err() {
- vm.kill();
+ sections.push(Section { data, mem }, GFP_KERNEL)?;
}
- result?
+ guard.dismiss();
+ sections
},
})
}
--
2.56.1