From nobody Wed Oct 8 05:47:00 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 0B36E13C3F2; Tue, 1 Jul 2025 14:14:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379270; cv=none; b=qYA3O9Vah3IRsNRbRXCYp7Xyjuyvy11c9vW94Dx0aXkqHLIhfHuQ7J7hDy+EkRM15sA2NLR9kqAEQHUNIk5JAYiNzX+QKjmZzkYkr1mbAxsPdukWvZhBUoY/m9oUqsyRCdg9+j8acLmkYvw2UGz53Nq5TQsaryBSZCYx03pKi74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379270; c=relaxed/simple; bh=DnWJ47zmIM3CNpSMLuoBSV5JDMZLwDBp7AxfPXYX8eU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I8QXTRN0WNo+6aeXIwJNZKARjTdoWDsQsIb1Kodd10I5Y6Sm6DOBZmsPp8/00+BHdcz9muJ0qA0V47CwUAoAiUeeL4nlDZGec99r5LIS0j7+ICA9pvzDHgbbYg9Om7PLPCQ9HX0kiR0FPxh3hBaOuX9ePkmCuBh5nKekLdojN20= 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=PnVBHUi8; arc=none smtp.client-ip=209.85.214.177 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="PnVBHUi8" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-23636167b30so30037875ad.1; Tue, 01 Jul 2025 07:14:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751379268; x=1751984068; 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=qOCKpsUZDZEoNUG++n7K5XV+fKbpAGpkvU6WO6gZ3IM=; b=PnVBHUi8gbxmTqKK6Vi6rtitWo7vvZgAbj11jrx1OgFVAOlQln2xoxjdQZt5Jkviik ITtIoWsp3txL2MXO7gKUQnBxLCUJboKOwRXaY9Lsf1p00Hbotr8iFvTEyeBHj7sGP50w qwwVVAu7HYtkP5vYQHrSqZ8d1zEt7xuatr7S2Zfo9JpLr9dtT7euSkYVHoqVZ7mz85jS sKJpsFHqF6G20oJ+pnB2SN3bHgs8otJk8PbowEPhOD6qfxc7vKcMRlqKTFz1pDUBulNr N+Hq7LUvdArzE515eIWvdTCndVIdLEPquRcV2KSXMs5k1F5Ns24KEQim6Yz42SEnOI5e Wlfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751379268; x=1751984068; 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=qOCKpsUZDZEoNUG++n7K5XV+fKbpAGpkvU6WO6gZ3IM=; b=vHd1VuIsu8dknmX1vv4jOh60XRW3h/Zx+EtzMzw+pdpImlNbEpmzBRLRnMNiaE7xe5 SMO+NmjAieQ9bhoVfi2D+KXSie3iX6nOatwCSG4cp+aY+z9WafnA9ObjI0PyP5gSv6CW BSQgPzCIie2B/hlhMM1cKL/Iv10MT09vlxU3QRSedt2L5rhMe6xTxQDk7vbBHgE2N8+L /vnMLF2WYEE9g4v9NAMkfcdj0yU1RM+FrtFQJXCIR3kOjj/1/8Z2fEJCYSLrOjV64huX 1WRPMkxOPQe4x4wLP7ADhHAZEhdepJXkoyfslBw6kbTDtJwbkBMUszIRk5AsrxuO1R9i 75nA== X-Forwarded-Encrypted: i=1; AJvYcCVALlddOvC/WbYyN/Az1vo+XcAQdsOIFlC+ZTsW3OlzjtauAYbWSpzQbAoO5s5hPdyUkeeFylRLhHOZX7jsZ38=@vger.kernel.org, AJvYcCVFMEYZLVv74VssdG09fqessGyh2pc4OJc468JdRUgdcv5xfSIppvBCECwJUSoBYUJ6My21WuRevlqy@vger.kernel.org, AJvYcCVPWXlnhYUbMOI7GPHlJWAEmIKfTTZqm+kSOEas2XMninzr5XCbsoaiJ4kbJ/EtB1LiJN1Ps/o3wdd0@vger.kernel.org, AJvYcCXF+uFak/1zGAPTCQuuwwkp2s8d5DtP7zorq4+3bk6ga7W/aNYJvpSzdt+WNK6PM5MEdMm10YzVPL0cuuft@vger.kernel.org, AJvYcCXd7fyNSIHv4i19Sm71SP2f29hDaGULVguzvOUIQsDOfyGOw7PDRk50seOUPRvwmsCSVGoRUUmv@vger.kernel.org X-Gm-Message-State: AOJu0Ywm0w6TVk4z92eP/BBEHQL6x0zDIv145lOeyB+go2HIFL5KPLvW KZRNi4BEaeD7R+NAWzGTXHTQOE/ASTXHWLObRPwHpBAGFnsbgJbT+IuG X-Gm-Gg: ASbGnctVhorvphNNlPo875sDJIqOrkVZKi98ijh0kxG+yl7Rnm8jv95ks9Ky4D/xFef U/pmVyWc1BERV1JthC+w5u7qA8Ndr/LDMrW9nL73YZm5ukV08juYDQ+jWfiABOxoMZk6mMofFiE kWLThG2sIGFJiqaE2yjwkDUCGnrsV22uknOeCPfhuTzraGcDkFTcuRR70BhODGzLY0QMRpRoSA5 QMOvvJxg/k6oPg4qtNxGsZrqgGkTJOxKvWxsKgmLVSMG9hUj5kvmdaGV7vpK6b/LHxV5P92T8qb c/7arAEOWeUodHspbrhFmNNi0C4+gJoqIjOm0DsXqcgRWhZ8HakInwnnBSAc5SuEAvf3qHHQVkv JBriw2lcGl6ps+Gj9Oas2kXqlBZF+eoXtwIM= X-Google-Smtp-Source: AGHT+IFMCaqKWcoT+iQosvdy9MhDIaWNRFN+aa8mzb6/3XLacvz2stzxPzWfOox4CKyORbY+yZ8pcw== X-Received: by 2002:a17:903:2445:b0:237:f76f:ce21 with SMTP id d9443c01a7336-23ac3deb3a4mr212940675ad.20.1751379267840; Tue, 01 Jul 2025 07:14:27 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23acb3b8324sm107240885ad.178.2025.07.01.07.14.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jul 2025 07:14:27 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com Cc: a.hindborg@kernel.org, aliceryhl@google.com, bhelgaas@google.com, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, david.m.ertman@intel.com, devicetree@vger.kernel.org, gary@garyguo.net, ira.weiny@intel.com, kwilczynski@kernel.org, leon@kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, lossin@kernel.org, netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, tmgross@umich.edu Subject: [PATCH v2 1/3] rust: device_id: split out index support into a separate trait Date: Tue, 1 Jul 2025 23:12:50 +0900 Message-ID: <20250701141252.600113-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250701141252.600113-1-fujita.tomonori@gmail.com> References: <20250701141252.600113-1-fujita.tomonori@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" Introduce a new trait `RawDeviceIdIndex`, which extends `RawDeviceId` to provide support for device ID types that include an index or context field (e.g., `driver_data`). This separates the concerns of layout compatibility and index-based data embedding, and allows `RawDeviceId` to be implemented for types that do not contain a `driver_data` field. Several such structures are defined in include/linux/mod_devicetable.h. Refactor `IdArray::new()` into a generic `build()` function, which takes an optional offset. Based on the presence of `RawDeviceIdIndex`, index writing is conditionally enabled. A new `new_without_index()` constructor is also provided for use cases where no index should be written. This refactoring is a preparation for enabling the PHY abstractions to use device_id trait. Signed-off-by: FUJITA Tomonori --- rust/kernel/auxiliary.rs | 7 +++- rust/kernel/device_id.rs | 80 +++++++++++++++++++++++++++++++--------- rust/kernel/of.rs | 11 +++++- rust/kernel/pci.rs | 7 +++- 4 files changed, 81 insertions(+), 24 deletions(-) diff --git a/rust/kernel/auxiliary.rs b/rust/kernel/auxiliary.rs index d2cfe1eeefb6..655ed61247d0 100644 --- a/rust/kernel/auxiliary.rs +++ b/rust/kernel/auxiliary.rs @@ -6,7 +6,7 @@ =20 use crate::{ bindings, container_of, device, - device_id::RawDeviceId, + device_id::{RawDeviceId, RawDeviceIdIndex}, driver, error::{to_result, Result}, prelude::*, @@ -143,10 +143,13 @@ pub const fn new(modname: &'static CStr, name: &'stat= ic CStr) -> Self { // SAFETY: // * `DeviceId` is a `#[repr(transparent)`] wrapper of `auxiliary_device_i= d` and does not add // additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::auxiliary_device_id; +} =20 +// SAFETY: +// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::auxiliary_device_id, driver_data); =20 diff --git a/rust/kernel/device_id.rs b/rust/kernel/device_id.rs index 3dc72ca8cfc2..3bcab2310087 100644 --- a/rust/kernel/device_id.rs +++ b/rust/kernel/device_id.rs @@ -24,22 +24,36 @@ /// Ideally, this should be achieved using a const function that does = conversion instead of /// transmute; however, const trait functions relies on `const_trait_i= mpl` unstable feature, /// which is broken/gone in Rust 1.73. -/// -/// - `DRIVER_DATA_OFFSET` is the offset of context/data field of the de= vice ID (usually named -/// `driver_data`) of the device ID, the field is suitable sized to wr= ite a `usize` value. -/// -/// Similar to the previous requirement, the data should ideally be ad= ded during `Self` to -/// `RawType` conversion, but there's currently no way to do it when u= sing traits in const. pub unsafe trait RawDeviceId { /// The raw type that holds the device id. /// /// Id tables created from [`Self`] are going to hold this type in its= zero-terminated array. type RawType: Copy; +} =20 - /// The offset to the context/data field. +/// Extension trait for [`RawDeviceId`] for devices that embed an index or= context value. +/// +/// This is typically used when the device ID struct includes a field like= `driver_data` +/// that is used to store a pointer-sized value (e.g., an index or context= pointer). +/// +/// # Safety +/// +/// Implementers must ensure that: +/// - `DRIVER_DATA_OFFSET` is the correct offset (in bytes) to the conte= xt/data field (e.g., the +/// `driver_data` field) within the raw device ID structure. This fiel= d must be correctly sized +/// to hold a `usize`. +/// +/// Ideally, the data should ideally be added during `Self` to `RawTyp= e` conversion, +/// but there's currently no way to do it when using traits in const. +/// +/// - The `index` method must return the value stored at the location sp= ecified +/// by `DRIVER_DATA_OFFSET`, assuming `self` is layout-compatible with= `RawType`. +pub unsafe trait RawDeviceIdIndex: RawDeviceId { + /// The offset (in bytes) to the context/data field in the raw device = ID. const DRIVER_DATA_OFFSET: usize; =20 - /// The index stored at `DRIVER_DATA_OFFSET` of the implementor of the= [`RawDeviceId`] trait. + /// The index stored at `DRIVER_DATA_OFFSET` of the implementor of the= [`RawDeviceIdIndex`] + /// trait. fn index(&self) -> usize; } =20 @@ -68,7 +82,14 @@ impl IdArray= { /// Creates a new instance of the array. /// /// The contents are derived from the given identifiers and context in= formation. - pub const fn new(ids: [(T, U); N]) -> Self { + /// + /// # Safety + /// + /// If `offset` is `Some(offset)`, then: + /// - `offset` must be the correct offset (in bytes) to the context/da= ta field + /// (e.g., the `driver_data` field) within the raw device ID structu= re. + /// - The field at `offset` must be correctly sized to hold a `usize`. + const unsafe fn build(ids: [(T, U); N], offset: Option) -> Self= { let mut raw_ids =3D [const { MaybeUninit::::uninit() }= ; N]; let mut infos =3D [const { MaybeUninit::uninit() }; N]; =20 @@ -77,14 +98,17 @@ impl IdArray { // SAFETY: by the safety requirement of `RawDeviceId`, we're g= uaranteed that `T` is // layout-wise compatible with `RawType`. raw_ids[i] =3D unsafe { core::mem::transmute_copy(&ids[i].0) }; - // SAFETY: by the safety requirement of `RawDeviceId`, this wo= uld be effectively - // `raw_ids[i].driver_data =3D i;`. - unsafe { - raw_ids[i] - .as_mut_ptr() - .byte_add(T::DRIVER_DATA_OFFSET) - .cast::() - .write(i); + + if let Some(offset) =3D offset { + // SAFETY: by the safety requirement of this function, thi= s would be effectively + // `raw_ids[i].driver_data =3D i;`. + unsafe { + raw_ids[i] + .as_mut_ptr() + .byte_add(offset) + .cast::() + .write(i); + } } =20 // SAFETY: this is effectively a move: `infos[i] =3D ids[i].1`= . We make a copy here but @@ -92,7 +116,6 @@ impl IdArray= { infos[i] =3D MaybeUninit::new(unsafe { core::ptr::read(&ids[i]= .1) }); i +=3D 1; } - core::mem::forget(ids); =20 Self { @@ -109,12 +132,33 @@ impl IdArray { } } =20 + /// Creates a new instance of the array without writing index values. + /// + /// The contents are derived from the given identifiers and context in= formation. + pub const fn new_without_index(ids: [(T, U); N]) -> Self { + // SAFETY: Calling `Self::build` with `offset =3D None` is always = safe, + // because no raw memory writes are performed in this case. + unsafe { Self::build(ids, None) } + } + /// Reference to the contained [`RawIdArray`]. pub const fn raw_ids(&self) -> &RawIdArray { &self.raw_ids } } =20 +impl IdArray { + /// Creates a new instance of the array. + /// + /// The contents are derived from the given identifiers and context in= formation. + pub const fn new(ids: [(T, U); N]) -> Self { + // SAFETY: by the safety requirement of `RawDeviceIdIndex`, + // `T::DRIVER_DATA_OFFSET` is guaranteed to be the correct offset = (in bytes) to + // a field within `T::RawType`. + unsafe { Self::build(ids, Some(T::DRIVER_DATA_OFFSET)) } + } +} + /// A device id table. /// /// This trait is only implemented by `IdArray`. diff --git a/rust/kernel/of.rs b/rust/kernel/of.rs index 40d1bd13682c..c690bceb0d73 100644 --- a/rust/kernel/of.rs +++ b/rust/kernel/of.rs @@ -2,7 +2,11 @@ =20 //! Device Tree / Open Firmware abstractions. =20 -use crate::{bindings, device_id::RawDeviceId, prelude::*}; +use crate::{ + bindings, + device_id::{RawDeviceId, RawDeviceIdIndex}, + prelude::*, +}; =20 /// IdTable type for OF drivers. pub type IdTable =3D &'static dyn kernel::device_id::IdTable; @@ -15,10 +19,13 @@ // SAFETY: // * `DeviceId` is a `#[repr(transparent)` wrapper of `struct of_device_id= ` and does not add // additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `data` field. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::of_device_id; +} =20 +// SAFETY: +// * `DRIVER_DATA_OFFSET` is the offset to the `data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::of= _device_id, data); =20 fn index(&self) -> usize { diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 6b94fd7a3ce9..96d6275c7afb 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -7,7 +7,7 @@ use crate::{ alloc::flags::*, bindings, container_of, device, - device_id::RawDeviceId, + device_id::{RawDeviceId, RawDeviceIdIndex}, devres::Devres, driver, error::{to_result, Result}, @@ -164,10 +164,13 @@ pub const fn from_class(class: u32, class_mask: u32) = -> Self { // SAFETY: // * `DeviceId` is a `#[repr(transparent)` wrapper of `pci_device_id` and = does not add // additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. unsafe impl RawDeviceId for DeviceId { type RawType =3D bindings::pci_device_id; +} =20 +// SAFETY: +// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId { const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::pc= i_device_id, driver_data); =20 fn index(&self) -> usize { --=20 2.43.0 From nobody Wed Oct 8 05:47:00 2025 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (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 D622B277C9B; Tue, 1 Jul 2025 14:14:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379275; cv=none; b=UP/he82bmQmuaGjvfMiiInuY8Cw1JZxKQtAz5+gfm6Xj7EQWnnPhcf16rHylbBs5UkxORF7u1AjFzrcXSVOqX2SP1lygth3W/UEEEqLf105zbAIQ+fm29t3k3RbDozzmMbqF15D08Pi0rtvRae+6bfu9lOxtuIIszKJvx3SXByQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379275; c=relaxed/simple; bh=Rb82lXwhfTwYPKu8Q8XF7KI5JIB8d/RCndqm2TyQhxc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OEGhFp7RuoRglcd/vzzpapEmXRfbtl2J21TiFZJvbeGg1AuJIK/fUnm6Eau9hiMegJuAUJ99D42FTJSJg5Dziu8f2W6fX0NUv/eKe7mKi7WbzY4ekt7dqAdCNIyC4mKmMLMLcry+fCEXDfcz5DIEFTT9W4unakoaWE3BQ62twwI= 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=INEQSCf4; arc=none smtp.client-ip=209.85.214.171 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="INEQSCf4" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-23602481460so58293035ad.0; Tue, 01 Jul 2025 07:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751379273; x=1751984073; 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=jN4iAGbjA8Ktlzd5eLTpLdmfO5ssDEZ/FUSkzLKEQtA=; b=INEQSCf4nRCzPqv7pAW5eYcDdcL1+lWcjzcw/yS7WLBGKAyIpiFeS6dHlQsvT9ot88 e+IFX0Fre0dzjOH6nd8oFn/EHFggv4gC6yplwwuqMEdEHgoxLPtFmd6ytAmxnGxl31F+ bQa2qjiqbYldOc0c7Oj7LZjWbgU5hPMZ8KRSNJ5l7ueZ0/rlINyr9yLGPTsoeZZ6ko1O j1IS9QFtNcPyhaLB+r7QfxgSHyvhIBIWSVDXfXvz4nfnN9RVd6MOnepf/JNpR2YeM+yx goeh6H40V8fY6BQ3N9A11d5RZ5Y2Tw0roMyS2p4ErxVkrW+sy+3d1pEqzbMYzn4OYbpJ Fzyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751379273; x=1751984073; 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=jN4iAGbjA8Ktlzd5eLTpLdmfO5ssDEZ/FUSkzLKEQtA=; b=pNilFqpPe2gp9aobeUBAwwV0aPl3ABKFu6KWmqBKiUFvNaGhaJajb6m04fM6vWLovv igcR6VJK5Y8ULFxeK8jVgAvt58Xe9un6HjP+djT65BKNASpR0Wa7+bI+4P13soJ8zr1c jMAPOkKMBPcCfg1bemylSYzMlz6pK965lCBY1HmDAFwaKZdULKxvxo5m7esTUrld0eIZ KN4GxTh71I9UTA7M2x+VbT51ElPBkh1rPTjZowQCfk5UqpddS+ghy4ZubzVR3az8T8UJ VwR75Wx+2NGquvMJk4UP7JobBWN73l5EClf4fv1LIkrsv23I/6UU4XJ7Tvq+809RNmHl Pb6Q== X-Forwarded-Encrypted: i=1; AJvYcCUfCRjnjAv8yADLtHG6U126w9JoL5Lkm5CQCQVqtC4l4rwSsp7hQIFBq0HaLsed7bqPHAsp0KG4+X0J@vger.kernel.org, AJvYcCUp4E63oFpcTXPUGwNVtnKVMyXahsZp+U7W46+JZLU2sZKtk4ki19yFTC2goG76gERKTX+pqxSAWBVA@vger.kernel.org, AJvYcCWDOSFZejw3DW6Zlkr3tVJ44PLhrubSqDFKv+RPwg+7EcLtMnkn6ZlySDmQNleDwzqx6H6OH7oiuKsjVC//@vger.kernel.org, AJvYcCWNbklLq7iEhs+ShCLoZzZmPYhWbM/jzHMBOCzHOXzlKmJ7wIgZsIq4UsxIAI2JxJ+NbB3Q42M3@vger.kernel.org, AJvYcCXnAqXr2eWk4h7kGg3wHopPiiYIwpADlj88ZHghTJnbSFg99HnR6cWJHot3upFcl3DJEjhog8721+yiQ4y50Y0=@vger.kernel.org X-Gm-Message-State: AOJu0YwfROhiCCWw8ipqPXSf9Bw0gDmzM7Cre1qZw7LHpuYsbgDhVepw 6XA/wdi5ryEjA444iXWhxUBxJXqTDCp0mRf6HjCPxeFw+jZb6xbxmlgJ X-Gm-Gg: ASbGnctTCJ/zLflS5THygmanYzHNdzdOFi5890b1Kie+qlXX5VTa7jxfPWf3fUosGEo hFw7yml1INrvpiPMuU2pHm48VQrXNkjUq9A78lQeyrfUuxG3P8BwVWb7YAvayxHvJDzDNa16jZA 9a9yGRNv3mryRhgDAbanTKWquNLz2N8Q/WKUFj5YwHS7q8jRa3Rj+yqdLOnutG2DPPW07UXMcTf z/5CbRzZPvpolYtWqzBVnQaEu2UyWXdDTync4O8pswEHGCx15fgd+2QAn83m2DgQp3HCixCWgC5 5OAzZpp/LsefC4nVA5mUlCPpeZlYXUuOBRluDZGot+si0PdeUPCUzqvwQ239D2BklwMYNZjmx9r yP/+U1iC6XyWkuUlBtX/gPLT000JfxnUGddk= X-Google-Smtp-Source: AGHT+IGyxcHOqw/Ds4ck8Rm15wPSrkIYKHYGR3uJAD9psSqRiYCXcGDXjJRSBaDoY/cnoS5FY9D23w== X-Received: by 2002:a17:902:f792:b0:236:6f7b:bf33 with SMTP id d9443c01a7336-23ac45e3681mr309972215ad.24.1751379273052; Tue, 01 Jul 2025 07:14:33 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23acb3b8324sm107240885ad.178.2025.07.01.07.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jul 2025 07:14:32 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com Cc: a.hindborg@kernel.org, aliceryhl@google.com, bhelgaas@google.com, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, david.m.ertman@intel.com, devicetree@vger.kernel.org, gary@garyguo.net, ira.weiny@intel.com, kwilczynski@kernel.org, leon@kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, lossin@kernel.org, netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, tmgross@umich.edu Subject: [PATCH v2 2/3] rust: net::phy represent DeviceId as transparent wrapper over mdio_device_id Date: Tue, 1 Jul 2025 23:12:51 +0900 Message-ID: <20250701141252.600113-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250701141252.600113-1-fujita.tomonori@gmail.com> References: <20250701141252.600113-1-fujita.tomonori@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" Refactor the DeviceId struct to be a #[repr(transparent)] wrapper around the C struct bindings::mdio_device_id. This refactoring is a preparation for enabling the PHY abstractions to use device_id trait. Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 53 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 65ac4d59ad77..940972ffadae 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -507,7 +507,7 @@ pub const fn create_phy_driver() -> DriverVT= able { DriverVTable(Opaque::new(bindings::phy_driver { name: T::NAME.as_char_ptr().cast_mut(), flags: T::FLAGS, - phy_id: T::PHY_DEVICE_ID.id, + phy_id: T::PHY_DEVICE_ID.id(), phy_id_mask: T::PHY_DEVICE_ID.mask_as_int(), soft_reset: if T::HAS_SOFT_RESET { Some(Adapter::::soft_reset_callback) @@ -691,42 +691,41 @@ fn drop(&mut self) { /// /// Represents the kernel's `struct mdio_device_id`. This is used to find = an appropriate /// PHY driver. -pub struct DeviceId { - id: u32, - mask: DeviceMask, -} +#[repr(transparent)] +#[derive(Clone, Copy)] +pub struct DeviceId(bindings::mdio_device_id); =20 impl DeviceId { /// Creates a new instance with the exact match mask. pub const fn new_with_exact_mask(id: u32) -> Self { - DeviceId { - id, - mask: DeviceMask::Exact, - } + Self(bindings::mdio_device_id { + phy_id: id, + phy_id_mask: DeviceMask::Exact.as_int(), + }) } =20 /// Creates a new instance with the model match mask. pub const fn new_with_model_mask(id: u32) -> Self { - DeviceId { - id, - mask: DeviceMask::Model, - } + Self(bindings::mdio_device_id { + phy_id: id, + phy_id_mask: DeviceMask::Model.as_int(), + }) } =20 /// Creates a new instance with the vendor match mask. pub const fn new_with_vendor_mask(id: u32) -> Self { - DeviceId { - id, - mask: DeviceMask::Vendor, - } + Self(bindings::mdio_device_id { + phy_id: id, + phy_id_mask: DeviceMask::Vendor.as_int(), + }) } =20 /// Creates a new instance with a custom match mask. pub const fn new_with_custom_mask(id: u32, mask: u32) -> Self { - DeviceId { - id, - mask: DeviceMask::Custom(mask), - } + Self(bindings::mdio_device_id { + phy_id: id, + phy_id_mask: DeviceMask::Custom(mask).as_int(), + }) } =20 /// Creates a new instance from [`Driver`]. @@ -734,18 +733,20 @@ pub const fn new_with_driver() -> Self { T::PHY_DEVICE_ID } =20 + /// Get a `phy_id` as u32. + pub const fn id(&self) -> u32 { + self.0.phy_id + } + /// Get a `mask` as u32. pub const fn mask_as_int(&self) -> u32 { - self.mask.as_int() + self.0.phy_id_mask } =20 // macro use only #[doc(hidden)] pub const fn mdio_device_id(&self) -> bindings::mdio_device_id { - bindings::mdio_device_id { - phy_id: self.id, - phy_id_mask: self.mask.as_int(), - } + self.0 } } =20 --=20 2.43.0 From nobody Wed Oct 8 05:47:00 2025 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 07299279DB1; Tue, 1 Jul 2025 14:14:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379280; cv=none; b=bQW4BVg9tUp0tKlpe3pqT4khcRKxqJIEnO0ggRd3+tmPjdwoVBxoCZmWEc8zBur+A+iSGYbF9CLw1m//KmMPwFUmMtfpBZDrcM4eUjUI/KEICkIX07FBkrtNfaJ0IjJGr6aQ2edIGKQTtzLJSbqN7vvP6bQhlF68FN2qEksgNyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751379280; c=relaxed/simple; bh=wY3pJ9nFlqmEeyPfW98+n/Y28S8S70pNb3omes9fzIQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UMUBjdHcdlS7uw7IoneCn8X00LvDbNEKrzrCH2SgYBMJzdwGbSXvji3pIHA4kXoI3rSOWUqRlmMjmAC+BvIMPZc2vOBtCVRsIfRKLJeIMchwJHqaP0XXaBI1/91kL0oY8NEAVd2y7gsjQNDXYgldCbhXe7zwQmgOKMblHL+qmjE= 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=lbX99E/m; arc=none smtp.client-ip=209.85.214.182 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="lbX99E/m" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-235ea292956so29694695ad.1; Tue, 01 Jul 2025 07:14:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1751379278; x=1751984078; 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=MhF5CcEqPzfan8Qj+FWBWIOdIwHq+OpoPrnKrGKGhts=; b=lbX99E/mEnl40ISUYBTgKXRRs+lpjk4qZwn13KtDSSrMCiwybyZIX/YV4PtbQMPgSF 2S8OhC2X2aE3HIVWrosltix0ytK9M1tciSccZbQzLm0pUp2Ne+8z/C2kdp5azOk4CLDN kMjGvPILacPhZK8OAY+NRLG3xMDhL43DLR26IMR2nSCZDrmWA4tO+LbcrKyN9PvXCHhI Ke+LG8uKMyomx5tuwslbFP3wiY61zHDvzL1ibxOUXAXuVD8TycBnS0b/RES7ZIeN5MzK MFe54PjDuGZ4Wfbwz4FsvX2oe1c/grUmnJEouEEdTBeo2a9P6Kadp1rmaIkgPZvLu+FK FVBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751379278; x=1751984078; 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=MhF5CcEqPzfan8Qj+FWBWIOdIwHq+OpoPrnKrGKGhts=; b=CNIfwQdcrrteFtS7Pt9XvH+zU0UVB1WZHCxuDa9ktZrIeB4P3tqkILWVK6W+OxgHz9 eTPiyTYo2VawW6vB1gQERrADxSDQJ4SdoJFypKxf4CtVNBn6GurQkNrKmr36EOtvySBG nAPPQjxrgrHCXtogsTHYKCGSRnP7nfi6+BOQ7WulQe+VgLjihu8xLPOPBZiE4Jm7tQzB VmU5nJLCuMuCihP9NQoFN+Gu++of8qXg0WYmsJPSoP6Azlzvy6GBiTMfgbOxuLbgelKH hZwJaL63MX/MJijCff39XpGHFneQk3g10igHL2lIGFeaYwhi/eyJcfNzZdeXhQpF+boe EjKw== X-Forwarded-Encrypted: i=1; AJvYcCVH9JDFVYszkVwUpSZOffGTDP+NeBth1Or0oLKjUZYVpwCnoGWlowfSWgk+w4pnKhkJxlmbpm+sDE6M@vger.kernel.org, AJvYcCVJPx26M3QQT0J/Xw4qTe9mDdr3M1gStOjaU8UaeO48Q7hL5qpipPzemSzG+yfvw+V4s6nJ5sUon9QS@vger.kernel.org, AJvYcCWMosOxbzdcYrTXpFvfj8d6+R58oh7Hcg3yYGXg4RGhB7xrFu4Mnja0wFwehbCrxPKdyoVTMd6L@vger.kernel.org, AJvYcCXDzES5nBq/Huy4p8zGbE5CSUXLICTB81UaxbPd91BJBrNxddlBKLgMLiyN2nAt9ndDO/qgxsbfL8ONEZYRJiU=@vger.kernel.org, AJvYcCXUOXPMdbLF6BPIDP1rmmjmRSzjtWmUovoMYuX+/GPDMMd+NAFImn3ZtsoygU/JqUoYxTMxy5p66UVqmGy+@vger.kernel.org X-Gm-Message-State: AOJu0YzR0I+zcibeStVyZb3zKev0/8Ln1ZbErVIiyB/dM13XgE/tI6YA jmNtHvodyq/ktf9ZbdQKz3dz5zYofShrGMBSfK5s1dEob0a6cIAaraQj X-Gm-Gg: ASbGnctXAdy8F01zTFbDTAuPf9TIOougqXAbA3r88OHWogJq1+pJ0RaK5uNWgDpJG4i fIaVge48tgf7V7W8LySwrFmQSsqmStY0DYE/kJN5Ld3K/+n4gkxPa+VeqOYdlcI2OiLntk7cIw+ d8qQuaCnhwX2LjnvMcOEPVlTYFJItGzDobcJAO2KNnuXo+jWsyRuY9nGCENNtCulwgSNF/1IPF+ 78/ChA12wrXhWJWjbABfpQdfLUfFY+CbOIbF9PvBh0dnXy7TNNpjPYIXKIxB0tXwzk8Z3+r+yO9 xvhrHZwldVHmy1Crb9NRQFo89miUFvsUBjsV36AXwzuSXMsd8y75UncDQyDOvcbHO4J0AEcpTE5 m/FASrSRUh2wj3y0UDjXi2D9p3XdQTWKikvo= X-Google-Smtp-Source: AGHT+IHs/sgaXxj0cg5nqN7WtEqfWV4a21o8vt1rWTuNUjMzgURiIciaiuN3uj8jT0zhbiEB8voasA== X-Received: by 2002:a17:902:e5cb:b0:235:779:edfd with SMTP id d9443c01a7336-23ac465ff41mr242885165ad.39.1751379278049; Tue, 01 Jul 2025 07:14:38 -0700 (PDT) Received: from bee.. (p5332007-ipxg23901hodogaya.kanagawa.ocn.ne.jp. [180.34.120.7]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-23acb3b8324sm107240885ad.178.2025.07.01.07.14.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Jul 2025 07:14:37 -0700 (PDT) From: FUJITA Tomonori To: alex.gaynor@gmail.com, dakr@kernel.org, gregkh@linuxfoundation.org, ojeda@kernel.org, rafael@kernel.org, robh@kernel.org, saravanak@google.com Cc: a.hindborg@kernel.org, aliceryhl@google.com, bhelgaas@google.com, bjorn3_gh@protonmail.com, boqun.feng@gmail.com, david.m.ertman@intel.com, devicetree@vger.kernel.org, gary@garyguo.net, ira.weiny@intel.com, kwilczynski@kernel.org, leon@kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, lossin@kernel.org, netdev@vger.kernel.org, rust-for-linux@vger.kernel.org, tmgross@umich.edu Subject: [PATCH v2 3/3] rust: net::phy Change module_phy_driver macro to use module_device_table macro Date: Tue, 1 Jul 2025 23:12:52 +0900 Message-ID: <20250701141252.600113-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250701141252.600113-1-fujita.tomonori@gmail.com> References: <20250701141252.600113-1-fujita.tomonori@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" Change module_phy_driver macro to build device tables which are exported to userspace by using module_device_table macro. Signed-off-by: FUJITA Tomonori --- rust/kernel/net/phy.rs | 51 ++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/rust/kernel/net/phy.rs b/rust/kernel/net/phy.rs index 940972ffadae..8bb362aefe27 100644 --- a/rust/kernel/net/phy.rs +++ b/rust/kernel/net/phy.rs @@ -6,7 +6,7 @@ //! //! C headers: [`include/linux/phy.h`](srctree/include/linux/phy.h). =20 -use crate::{error::*, prelude::*, types::Opaque}; +use crate::{device_id::RawDeviceId, error::*, prelude::*, types::Opaque}; use core::{marker::PhantomData, ptr::addr_of_mut}; =20 pub mod reg; @@ -750,6 +750,12 @@ pub const fn mdio_device_id(&self) -> bindings::mdio_d= evice_id { } } =20 +// SAFETY: [`DeviceId`] is a `#[repr(transparent)` wrapper of `struct mdio= _device_id` +// and does not add additional invariants, so it's safe to transmute to `R= awType`. +unsafe impl RawDeviceId for DeviceId { + type RawType =3D bindings::mdio_device_id; +} + enum DeviceMask { Exact, Model, @@ -850,19 +856,18 @@ const fn as_int(&self) -> u32 { /// } /// }; /// -/// const _DEVICE_TABLE: [::kernel::bindings::mdio_device_id; 2] =3D [ -/// ::kernel::bindings::mdio_device_id { -/// phy_id: 0x00000001, -/// phy_id_mask: 0xffffffff, -/// }, -/// ::kernel::bindings::mdio_device_id { -/// phy_id: 0, -/// phy_id_mask: 0, -/// }, -/// ]; -/// #[cfg(MODULE)] -/// #[no_mangle] -/// static __mod_device_table__mdio__phydev: [::kernel::bindings::mdio_dev= ice_id; 2] =3D _DEVICE_TABLE; +/// const N: usize =3D 1; +/// +/// const TABLE: ::kernel::device_id::IdArray<::kernel::net::phy::DeviceId= , (), N> =3D +/// ::kernel::device_id::IdArray::new_without_index([ +/// ::kernel::net::phy::DeviceId( +/// ::kernel::bindings::mdio_device_id { +/// phy_id: 0x00000001, +/// phy_id_mask: 0xffffffff, +/// }), +/// ]); +/// +/// ::kernel::module_device_table!("mdio", phydev, TABLE); /// ``` #[macro_export] macro_rules! module_phy_driver { @@ -873,20 +878,12 @@ macro_rules! module_phy_driver { }; =20 (@device_table [$($dev:expr),+]) =3D> { - // SAFETY: C will not read off the end of this constant since the = last element is zero. - const _DEVICE_TABLE: [$crate::bindings::mdio_device_id; - $crate::module_phy_driver!(@count_devices $($dev),+) + 1] =3D [ - $($dev.mdio_device_id()),+, - $crate::bindings::mdio_device_id { - phy_id: 0, - phy_id_mask: 0 - } - ]; + const N: usize =3D $crate::module_phy_driver!(@count_devices $($de= v),+); + + const TABLE: $crate::device_id::IdArray<$crate::net::phy::DeviceId= , (), N> =3D + $crate::device_id::IdArray::new_without_index([ $(($dev,())),+= , ]); =20 - #[cfg(MODULE)] - #[no_mangle] - static __mod_device_table__mdio__phydev: [$crate::bindings::mdio_d= evice_id; - $crate::module_phy_driver!(@count_devices $($dev),+) + 1] =3D = _DEVICE_TABLE; + $crate::module_device_table!("mdio", phydev, TABLE); }; =20 (drivers: [$($driver:ident),+ $(,)?], device_table: [$($dev:expr),+ $(= ,)?], $($f:tt)*) =3D> { --=20 2.43.0