From nobody Tue Nov 26 03:36:39 2024 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5EAF91CDFC3; Tue, 22 Oct 2024 22:49:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729637362; cv=none; b=tIB2OuIr0kTLrh7m7LfEnKxzc4OKerkIN7AIiwJwAZ4rUD2tAPJjO4RTb3BTUELyOB/xGOusIqOK7MIX6zv+0H21ZL8wNc8u2n9hSAORVxBsWWyqJ33aFIWgJo6izOT2uL6AmId3X+isklQVPN39I/UoY9vWwhWB2IBJ+HTsdR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729637362; c=relaxed/simple; bh=uW9diALEP9ZtiLTXAb1PZ+oZzD1vayfhCvZE6VvGSGc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ltg7dsNwhFcsfl4zslL4KewdNq7RQATT/vzvv5yUGOh19O8FZqcVhEKoRWilnMdHiB9X6dOEeMyaxonmckqs7c5SfeJ4j38xn5jzamli9HkLwvTNDcLmxMYJmxKwmv6G6jagoYGvFL/atC+zC6Lp+nrbnrf05BhxtXvyPpnZYF4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gk5US8YZ; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gk5US8YZ" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-539f76a6f0dso5499405e87.1; Tue, 22 Oct 2024 15:49:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729637358; x=1730242158; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q/j3y+C65g8JL4SR/Lkh7VbOU4M9pFoYcH6/NF8qhIk=; b=gk5US8YZjUUXbV5FV1CBz+DpqwstZS9YU8OEZVPsg0ptkvjjHMC33TehZUB76nzxkm /Z29+nDWzM22s+U2QJ7iHoCiTQpowBoK51d7wbnEph8336hhjU9Y7CR9SrYaO5D2GH6Z 2OuIdy31zLPB78GlhFKOZr2v/Z6tOcq1MSp9BoxDLVxwWqLeOR9/LhijJIU41RN/9YHW g+OlYTycrwYqnIAfMEDJXC7B1xxRAkveh2/AzlHn4RuFDDg9Dpf5qSBscY0hySGEWAI4 M9YhetLvGUu/DxsaJZdinHzP3blmF995gsQJOAIIjjPaG6xyWHXymOQTw7GOZrsNQ0GK q5rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729637358; x=1730242158; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q/j3y+C65g8JL4SR/Lkh7VbOU4M9pFoYcH6/NF8qhIk=; b=Hu8Ua7HOTiw5UhlludtVFHMxzDzCLWK5gnkNudAPilMJkAmYdR4RTgyMbaTm2qjlyo QoBxx3HNNSJPu53Ah4foGpKDD6vdTzKjM2Ks/GmUMH3g9qtqWpnUBBrnUPEP6y3dx6IY nE70W+lxyjBxZStvknrvFOmOoaLPneQive2RKuhl1FT2bYcNegOJ3BwMYlxR0vWUV3Xc OT1cYy0oKFsJ28H1BTBwVuln39rHVcke7NZceIB4pACUjAF5sNgLqnL0nGSyCoJtiTox X5mS6eoqXfe3YF/pKOGcII9A1IZq0/vu7J3OxQS8ae2haiFhq71R1LQRsNilO5OwcO0W K27w== X-Forwarded-Encrypted: i=1; AJvYcCWmImNYGGAbMcfT/x+/46Baxzygm5w9aV/vYIq6W1chl4BCJ+ikav/ntzRpdbKlQoe+EtQiYTIqNSU6+ec=@vger.kernel.org X-Gm-Message-State: AOJu0YzhKerCEPKHa6vyQ9rBLDF91+iQh6WWWciadp6mNwbO++zjczR2 Da1tGTaTH+UuITHl6s9Cvbh/M5vRMzy+nlxe3zLezAtBjIv/epRKnh/yhQ== X-Google-Smtp-Source: AGHT+IGJNc5G/N9oAN/iMJnW3Q1hdzKMq4lY+yTSxrxoH6XLwRn2fVgS/hlgwcDG1ZslsQYakLw6dA== X-Received: by 2002:a05:6512:281e:b0:539:89f7:3187 with SMTP id 2adb3069b0e04-53b1a36c709mr169463e87.47.1729637358314; Tue, 22 Oct 2024 15:49:18 -0700 (PDT) Received: from abj-NUC9VXQNX.. (87-94-132-183.rev.dnainternet.fi. [87.94.132.183]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-53a223e595csm894881e87.14.2024.10.22.15.49.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Oct 2024 15:49:16 -0700 (PDT) From: Abdiel Janulgue To: rust-for-linux@vger.kernel.org, aliceryhl@google.com Cc: dakr@redhat.com, linux-kernel@vger.kernel.org, airlied@redhat.com, miguel.ojeda.sandonis@gmail.com, boqun.feng@gmail.com, Abdiel Janulgue Subject: [PATCH v2 5/5] rust: firmware: implement `Ownable` for Firmware Date: Wed, 23 Oct 2024 01:44:49 +0300 Message-ID: <20241022224832.1505432-6-abdiel.janulgue@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241022224832.1505432-1-abdiel.janulgue@gmail.com> References: <20241022224832.1505432-1-abdiel.janulgue@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" For consistency, wrap the firmware as an `Owned` smart pointer in the constructor. Cc: Danilo Krummrich Suggested-by: Boqun Feng Signed-off-by: Abdiel Janulgue --- rust/kernel/firmware.rs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/rust/kernel/firmware.rs b/rust/kernel/firmware.rs index dee5b4b18aec..6da834b37455 100644 --- a/rust/kernel/firmware.rs +++ b/rust/kernel/firmware.rs @@ -4,8 +4,8 @@ //! //! C header: [`include/linux/firmware.h`](srctree/include/linux/firmware.= h) =20 -use crate::{bindings, device::Device, error::Error, error::Result, str::CS= tr}; -use core::ptr::NonNull; +use crate::{bindings, device::Device, error::Error, error::Result, str::CS= tr, + types::{Opaque, Owned, Ownable}}; =20 /// # Invariants /// @@ -52,10 +52,11 @@ fn request_nowarn() -> Self { /// # Ok(()) /// # } /// ``` -pub struct Firmware(NonNull); + #[repr(transparent)] +pub struct Firmware(Opaque); =20 impl Firmware { - fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result= { + fn request_internal(name: &CStr, dev: &Device, func: FwFunc) -> Result= > { let mut fw: *mut bindings::firmware =3D core::ptr::null_mut(); let pfw: *mut *mut bindings::firmware =3D &mut fw; =20 @@ -65,25 +66,26 @@ fn request_internal(name: &CStr, dev: &Device, func: Fw= Func) -> Result { if ret !=3D 0 { return Err(Error::from_errno(ret)); } - + // CAST: Self` is a `repr(transparent)` wrapper around `bindings::= firmware`. + let ptr =3D fw.cast::(); // SAFETY: `func` not bailing out with a non-zero error code, guar= antees that `fw` is a // valid pointer to `bindings::firmware`. - Ok(Firmware(unsafe { NonNull::new_unchecked(fw) })) + Ok(unsafe { Owned::to_owned(ptr) }) } =20 /// Send a firmware request and wait for it. See also `bindings::reque= st_firmware`. - pub fn request(name: &CStr, dev: &Device) -> Result { + pub fn request(name: &CStr, dev: &Device) -> Result> { Self::request_internal(name, dev, FwFunc::request()) } =20 /// Send a request for an optional firmware module. See also /// `bindings::firmware_request_nowarn`. - pub fn request_nowarn(name: &CStr, dev: &Device) -> Result { + pub fn request_nowarn(name: &CStr, dev: &Device) -> Result= > { Self::request_internal(name, dev, FwFunc::request_nowarn()) } =20 fn as_raw(&self) -> *mut bindings::firmware { - self.0.as_ptr() + self.0.get() } =20 /// Returns the size of the requested firmware in bytes. @@ -101,10 +103,13 @@ pub fn data(&self) -> &[u8] { } } =20 -impl Drop for Firmware { - fn drop(&mut self) { - // SAFETY: `self.as_raw()` is valid by the type invariant. - unsafe { bindings::release_firmware(self.as_raw()) }; +unsafe impl Ownable for Firmware { + unsafe fn ptr_drop(ptr: *mut Self) { + // SAFETY: + // - By the type invariants, we have ownership of the ptr and can = free it. + // - Per function safety, this is called in Owned::drop(), so `ptr= ` is a + // unique pointer to object, it's safe to release the firmware. + unsafe { bindings::release_firmware(ptr.cast()) }; } } =20 --=20 2.43.0