From patchwork Thu Dec 12 16:33:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13905616 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7BC312288FD; Thu, 12 Dec 2024 16:35:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734021329; cv=none; b=b+BwRNWuc0aO0tsrT5QqNIXfuSwPM4j0KeNC3LORrZ9D5igpVfZKv0+u7MrjRVU+XE6Zv1oCd3a6+wjw0BALVYtSIjg/wHmHCTeS7XspHYsEvkrsWBjVe8K5i2GMlJMxHaK9+ZluD5/0X8CwmmwnsTKl2wBnYaJDZOqiBXyalOc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734021329; c=relaxed/simple; bh=qGSHWpF8AwiVqHzGCBSZ8iAgZfzAVJgvQwZpKGoYuto=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uf7mAaZElg0wmShQKD4w9eqf/EUVGX0Y3kcq8hvF20IH6ivGmiC1uVPSphWiyBaEyTD9k4PKL0dgmOGYXQpQr/Zi5W8hZbs5PM0whGi0cgJ1ZYubDHHsLw/QM3rmlNMLA+TVaRudDIlAV3oEXOen5TNuxXPfdFYBqMCZmnZ4YjE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ci9rbztr; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ci9rbztr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8E02AC4CEDD; Thu, 12 Dec 2024 16:35:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734021329; bh=qGSHWpF8AwiVqHzGCBSZ8iAgZfzAVJgvQwZpKGoYuto=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ci9rbztr7nUJrJ5NBN8lg4lS0o1vpf/lHcfYqM1IC5c0lcucRgBvwvFTO6NGdPxC8 2PIX3a6e3U29LK+eHxjuEyL3K7fUwoKtmrqV6pVb5drHDcJnqgCA+f6RvdTlPm71/k tGEPMB7I/YTHXoWys0pC3x1RSKu5W8PUGFcLyJRvQWwmose4TPwQjtS8NCSIfr97XB MiKKLaIU+HSa3gZoH/ZqLvPN/HFSmEo+I6MPKt0aeDk1JdvHHnPEHvrtFNyGSR8aZd DiG3ixnEMI78JZBoq3caeijATXLBWHYH5NKsUU7SOWeQtmFGdqwjQtdFWEfV2Kpy1p QmmO39q+lST6A== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, bhelgaas@google.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, tmgross@umich.edu, a.hindborg@samsung.com, aliceryhl@google.com, airlied@gmail.com, fujita.tomonori@gmail.com, lina@asahilina.net, pstanner@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, robh@kernel.org, daniel.almeida@collabora.com, saravanak@google.com, dirk.behme@de.bosch.com, j@jannau.net, fabien.parent@linaro.org, chrisi.schrefl@gmail.com, paulmck@kernel.org Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, devicetree@vger.kernel.org, rcu@vger.kernel.org, Danilo Krummrich Subject: [PATCH v6 05/16] rust: types: add `Opaque::pin_init` Date: Thu, 12 Dec 2024 17:33:36 +0100 Message-ID: <20241212163357.35934-6-dakr@kernel.org> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241212163357.35934-1-dakr@kernel.org> References: <20241212163357.35934-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: rcu@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Analogous to `Opaque::new` add `Opaque::pin_init`, which instead of a value `T` takes a `PinInit` and returns a `PinInit>`. Suggested-by: Alice Ryhl Signed-off-by: Danilo Krummrich --- rust/kernel/types.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ec6457bb3084..3aea6af9a0bc 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -281,6 +281,17 @@ pub const fn uninit() -> Self { } } + /// Create an opaque pin-initializer from the given pin-initializer. + pub fn pin_init(slot: impl PinInit) -> impl PinInit { + Self::ffi_init(|ptr: *mut T| { + // SAFETY: + // - `ptr` is a valid pointer to uninitialized memory, + // - `slot` is not accessed on error; the call is infallible, + // - `slot` is pinned in memory. + let _ = unsafe { init::PinInit::::__pinned_init(slot, ptr) }; + }) + } + /// Creates a pin-initializer from the given initializer closure. /// /// The returned initializer calls the given closure with the pointer to the inner `T` of this