From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 420EFC5B543 for ; Wed, 4 Jun 2025 10:28:54 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E180310E092; Wed, 4 Jun 2025 10:28:53 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="qP9yIi1G"; dkim-atps=neutral Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5312A10E092; Wed, 4 Jun 2025 10:28:53 +0000 (UTC) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 31D86A5043F; Wed, 4 Jun 2025 10:28:52 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 401EFC4CEE7; Wed, 4 Jun 2025 10:28:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749032931; bh=JBl2S2HcUiHa7eTyy0mmRlKPLkijJl96CchvkFqMJNc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=qP9yIi1GWziSK+VEJTpm1vEyrBxdG0uppVJAd6frmyrfEEZs2s5ba3u9NsSupTlTw DztOcoXc8EjQM0TDdtKR1jrVryh2iiC/To4hlWR9VuGAIFk2WrfJYI/YEApkELqkSN /oms3NVAgEpR2Hnr9jYOi5HUvxcJiaEPktU+qL/quWemrVQKOeMZExJhiCLHrmpqoK mtpwUXM2kpVPamsZ+tdNlIM7qIBYb/VI9J4j/uk7jZyr9C6ujVg6oM5MT3ThPMqn/7 1yZ0kl9lccbciDhbRaTaw0sndq+0UyUJyHXf7mu7RTHxo32jxQv3ba2Poyru3o9bSt fMx2pRB0wAEbg== Date: Wed, 4 Jun 2025 12:28:44 +0200 From: Danilo Krummrich To: Alexandre Courbot Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , John Hubbard , Ben Skeggs , Joel Fernandes , Timur Tabi , Alistair Popple , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, nouveau@lists.freedesktop.org, dri-devel@lists.freedesktop.org Subject: Re: [PATCH v4 18/20] gpu: nova-core: add types for patching firmware binaries Message-ID: References: <20250521-nova-frts-v4-0-05dfd4f39479@nvidia.com> <20250521-nova-frts-v4-18-05dfd4f39479@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250521-nova-frts-v4-18-05dfd4f39479@nvidia.com> X-BeenThere: nouveau@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Nouveau development list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: nouveau-bounces@lists.freedesktop.org Sender: "Nouveau" On Wed, May 21, 2025 at 03:45:13PM +0900, Alexandre Courbot wrote: > +/// A [`DmaObject`] containing a specific microcode ready to be loaded into a falcon. > +/// > +/// This is module-local and meant for sub-modules to use internally. > +struct FirmwareDmaObject(DmaObject, PhantomData); > + > +/// Trait for signatures to be patched directly into a given firmware. > +/// > +/// This is module-local and meant for sub-modules to use internally. > +trait FirmwareSignature: AsRef<[u8]> {} > + > +#[expect(unused)] > +impl FirmwareDmaObject { > + /// Creates a new `UcodeDmaObject` containing `data`. > + fn new(dev: &device::Device, data: &[u8]) -> Result { > + DmaObject::from_data(dev, data).map(|dmaobj| Self(dmaobj, PhantomData)) > + } > + > + /// Patches the firmware at offset `sig_base_img` with `signature`. > + fn patch_signature>( > + &mut self, > + signature: &S, > + sig_base_img: usize, > + ) -> Result<()> { > + let signature_bytes = signature.as_ref(); > + if sig_base_img + signature_bytes.len() > self.0.size() { > + return Err(EINVAL); > + } > + > + // SAFETY: we are the only user of this object, so there cannot be any race. > + let dst = unsafe { self.0.start_ptr_mut().add(sig_base_img) }; > + > + // SAFETY: `signature` and `dst` are valid, properly aligned, and do not overlap. > + unsafe { > + core::ptr::copy_nonoverlapping(signature_bytes.as_ptr(), dst, signature_bytes.len()) > + }; > + > + Ok(()) > + } > +} If we can't patch them when the object is created, i.e. in FirmwareDmaObject::new(), I think we should take self by value in FirmwareDmaObject::patch_signature() and return a SignedFirmwareDmaObject (which can just be a transparent wrapper) instead in order to let the type system prove that we did not forget to call patch_signature().