All of lore.kernel.org
 help / color / mirror / Atom feed
From: Danilo Krummrich <dakr@kernel.org>
To: dakr@kernel.org, aliceryhl@google.com,
	daniel.almeida@collabora.com, airlied@gmail.com, simona@ffwll.ch,
	lossin@kernel.org, gary@garyguo.net, ojeda@kernel.org,
	boqun@kernel.org, bjorn3_gh@protonmail.com,
	a.hindborg@kernel.org, tmgross@umich.edu, tamird@kernel.org,
	acourbot@nvidia.com, work@onurozkan.dev
Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH 4/4] drm/tyr: use ScopeGuard for firmware loading cleanup
Date: Thu, 19 Nov 2026 00:37:05 +0100	[thread overview]
Message-ID: <20261118233727.3147222-5-dakr@kernel.org> (raw)
In-Reply-To: <20261118233727.3147222-1-dakr@kernel.org>

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@kernel.org>
---
 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


      parent reply	other threads:[~2026-11-18 23:38 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18 23:37 [PATCH 0/4] drm/tyr: use pin-init self-referential support Danilo Krummrich
2026-11-18 23:37 ` [PATCH 1/4] drm/tyr: name resource lifetimes after their types Danilo Krummrich
2026-11-19  2:08   ` Gary Guo
2026-11-19 10:34     ` Danilo Krummrich
2026-11-19 11:28       ` Gary Guo
2026-11-19 12:28         ` Danilo Krummrich
2026-11-19 12:56           ` Gary Guo
2026-11-19 13:57             ` Danilo Krummrich
2026-11-19 15:44               ` Alice Ryhl
2026-11-19 16:38                 ` Danilo Krummrich
2026-11-18 23:37 ` [PATCH 2/4] rust: pin-init: internal: suppress multiple_bound_locations Danilo Krummrich
2026-11-19  2:23   ` Gary Guo
2026-11-18 23:37 ` [PATCH 3/4] drm/tyr: use pin-init self-referential support Danilo Krummrich
2026-11-18 23:47   ` sashiko-bot
2026-11-18 23:37 ` Danilo Krummrich [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261118233727.3147222-5-dakr@kernel.org \
    --to=dakr@kernel.org \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.