From nobody Thu Oct 9 02:54:19 2025 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 515252701C8; Sat, 21 Jun 2025 19:51:40 +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=1750535501; cv=none; b=vEIhOqR4MBpz5Aol7Yp8LaVHsSK+sRjKK1Fj9JiBo8W4WXok/FMLkkctk7KRrZ3Yj3wLyfQ6gjq69iuVxeuE3t3rjId6h4hoCEH2zj3JjzBFs9lB9uIlrca3wOpPRiClEEg4UU31Ok8OPr9xiX2WWyvEjwtscAolaD9QhDQQvD0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750535501; c=relaxed/simple; bh=64LA9BPdINZxDweXi3XrPvDZBhIiC67sjGS0F3HdOPg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qbEg1qQaM3BwOu53FHbqekIZElx5fuJ1Sw40LTbR/FKccdOadyASym86tRYqh0iHoCcbSkga0DqhpHbUqb+rEQs/9kQ9/EKuUKKatgBVCIwp/7DNw/yEmb84T7D5H1hQy2i6NkUAQHO5x2B4KUYBgLUnh/fSE6fRvRyE2S0bIdY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hPIx4PHB; 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="hPIx4PHB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0374FC4CEF0; Sat, 21 Jun 2025 19:51:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1750535500; bh=64LA9BPdINZxDweXi3XrPvDZBhIiC67sjGS0F3HdOPg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hPIx4PHBQ9WQfZht+Ufuepc4qS5Roc/26MRLN02aPZzGbw9qYrodi3BjLb0StRPzC NJlj77yt+o9HTMKe2buOjP+U1Wb5ABZFKlND4Cw19YlOjjWfK89jjRy+joD78uzsCt DyeQkN5ol//HC7ZY0lGkOwA7cGG35e8/q38RNQJHOTA46Rclm7crVRvKvFmfMnW58m 25lS9K9dxiHVzIG24n4EnSRb/hY4JFm67eif6rRTuJvfgwt9Npcoc8isoBlNri952h ddimMd8GSOGONMB2AkAHvxbkOOcmiqEVdQtr8mfS5BCD+3ImtUrlPEzeRHsYo3AKBJ 1pmPkPBy10fEQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, david.m.ertman@intel.com, ira.weiny@intel.com, leon@kernel.org, kwilczynski@kernel.org, bhelgaas@google.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Danilo Krummrich Subject: [PATCH 3/8] rust: platform: use generic device drvdata accessors Date: Sat, 21 Jun 2025 21:43:29 +0200 Message-ID: <20250621195118.124245-4-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250621195118.124245-1-dakr@kernel.org> References: <20250621195118.124245-1-dakr@kernel.org> 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" Take advantage of the generic drvdata accessors of the generic Device type. While at it, use from_result() instead of match. Signed-off-by: Danilo Krummrich --- rust/helpers/platform.c | 10 ---------- rust/kernel/platform.rs | 36 +++++++++++++++++------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/rust/helpers/platform.c b/rust/helpers/platform.c index 82171233d12f..1ce89c1a36f7 100644 --- a/rust/helpers/platform.c +++ b/rust/helpers/platform.c @@ -2,16 +2,6 @@ =20 #include =20 -void *rust_helper_platform_get_drvdata(const struct platform_device *pdev) -{ - return platform_get_drvdata(pdev); -} - -void rust_helper_platform_set_drvdata(struct platform_device *pdev, void *= data) -{ - platform_set_drvdata(pdev, data); -} - bool rust_helper_dev_is_platform(const struct device *dev) { return dev_is_platform(dev); diff --git a/rust/kernel/platform.rs b/rust/kernel/platform.rs index 5b21fa517e55..dc0c36d70963 100644 --- a/rust/kernel/platform.rs +++ b/rust/kernel/platform.rs @@ -6,11 +6,11 @@ =20 use crate::{ bindings, container_of, device, driver, - error::{to_result, Result}, + error::{from_result, to_result, Result}, of, prelude::*, str::CStr, - types::{ForeignOwnable, Opaque}, + types::Opaque, ThisModule, }; =20 @@ -61,30 +61,28 @@ extern "C" fn probe_callback(pdev: *mut bindings::platf= orm_device) -> kernel::ff // `struct platform_device`. // // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. - let pdev =3D unsafe { &*pdev.cast::>() }; - + let pdev =3D unsafe { &*pdev.cast::>() }; let info =3D ::id_info(pdev.as_ref()); - match T::probe(pdev, info) { - Ok(data) =3D> { - // Let the `struct platform_device` own a reference of the= driver's private data. - // SAFETY: By the type invariant `pdev.as_raw` returns a v= alid pointer to a - // `struct platform_device`. - unsafe { bindings::platform_set_drvdata(pdev.as_raw(), dat= a.into_foreign() as _) }; - } - Err(err) =3D> return Error::to_errno(err), - } =20 - 0 + from_result(|| { + let data =3D T::probe(pdev, info)?; + + pdev.as_ref().set_drvdata(data); + Ok(0) + }) } =20 extern "C" fn remove_callback(pdev: *mut bindings::platform_device) { - // SAFETY: `pdev` is a valid pointer to a `struct platform_device`. - let ptr =3D unsafe { bindings::platform_get_drvdata(pdev) }.cast(); + // SAFETY: The platform bus only ever calls the remove callback wi= th a valid pointer to a + // `struct platform_device`. + // + // INVARIANT: `pdev` is valid for the duration of `probe_callback(= )`. + let pdev =3D unsafe { &*pdev.cast::>() }; =20 // SAFETY: `remove_callback` is only ever called after a successfu= l call to - // `probe_callback`, hence it's guaranteed that `ptr` points to a = valid and initialized - // `KBox` pointer created through `KBox::into_foreign`. - let _ =3D unsafe { KBox::::from_foreign(ptr) }; + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `Pin>`. + let _ =3D unsafe { pdev.as_ref().drvdata_obtain::>>() = }; } } =20 --=20 2.49.0