From nobody Mon Feb 9 04:30:13 2026 Received: from m.foxido.dev (m.foxido.dev [81.177.217.87]) (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 03E262D063A; Wed, 7 Jan 2026 20:36:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.177.217.87 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767818199; cv=none; b=TOs/246nSmst9YZdPQDg5b6rgZI5EIGdIOA0T9o13guYuymTRYKZTqtnMHwfnqs/7K5FgRQD2EcVwG+m3elPDOfXUShgZkeyXPMFwnSI1I+W5fDan3ZiJKuoMZJo+Zj091TY9Vjq1ijbvZb8HRb4141vj9HwKyK+txULvgd3dRk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767818199; c=relaxed/simple; bh=i8W7YKX8S0kQo5L7YSzvXf0CToGzPt6PjLFHgHRkQ0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=swDhvMfeMODMg+iHAeMTT8LIX1GVfc8FcO1YtR8AIFBn/RJiKa5HXR5/w1kDwcXMEDXQuT+npP1uopCUXLm0jWovOM+fFCbxX708qX+tPFo8k/ktQnlw3VrfkNjikqVZ6tQDv+hE1mubBX+YqFnbQLtdD8RB+qIyR3SfKmwwqd8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev; spf=pass smtp.mailfrom=foxido.dev; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=JovUzSHT; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=ufY0KSjF; arc=none smtp.client-ip=81.177.217.87 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=foxido.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="JovUzSHT"; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="ufY0KSjF" DKIM-Signature: v=1; a=rsa-sha256; s=202508r; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=7WV1mwAtbFEAdoyjirC636K yJEP0FvI3MsKwDZMOxUQ=; b=JovUzSHTor4A6K9fchP3H84v3bsB+YfkMqIVyxyBESFSmQFOxc DayNl9MUj5c1v8XTEVxRFsUR+Irl/hEXTptUbmjWwP0yfpQ9Kv0C2IjowwYzruVAxwMsppF8ojL 3sR2no7iiAmaKUr1egEf1PPCaalR3/JMrbo5/ubLJDwAC7rbNx37huPnNyYgoUPPEn0ftfsgdGt lBafW+ZQc+SuTdqx7bnPvxsvgd2FYJ3gf4tDqnmalj+qtForz23xsPzKz7ax4wEGud0BKaBa1Ej TdnHP47ZoE5hyx6S5lpUtfNEoFtydZUgzpgRyHd3HUUXlPjbU9s+TfAZOaX+03uwJig==; DKIM-Signature: v=1; a=ed25519-sha256; s=202508e; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=7WV1mwAtbFEAdoyjirC636K yJEP0FvI3MsKwDZMOxUQ=; b=ufY0KSjF+r38GC7lncOoHtX0dBMIkCQ8/lrE7y2g1of3r67sSm A28mQFchmrCa1e/Frb53YidjCILt4vO4qYBQ==; From: Gladyshev Ilya To: "foxido @ foxido . dev-cc= Rafael J. Wysocki" Cc: Len Brown , Miguel Ojeda , Boqun Feng , Gary Guo , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Armin Wolf , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-acpi@vger.kernel.org, Gladyshev Ilya Subject: [PATCH 1/3] rust: enable const_intrinsic_copy feature Date: Wed, 7 Jan 2026 23:35:31 +0300 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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" Since Rust 1.83.0 the `const_intrinsic_copy` feature is stable. It allows usage of `ptr::copy` and `ptr::copy_nonoverlapping` in const context. Signed-off-by: Gladyshev Ilya --- rust/kernel/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index f812cf120042..a6eccdba50b5 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -36,6 +36,7 @@ #![feature(const_option)] #![feature(const_ptr_write)] #![feature(const_refs_to_cell)] +#![feature(const_intrinsic_copy)] // // Expected to become stable. #![feature(arbitrary_self_types)] --=20 2.52.0 From nobody Mon Feb 9 04:30:13 2026 Received: from sender6.mail.selcloud.ru (sender6.mail.selcloud.ru [5.8.75.169]) (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 8C9BF23C8A0; Wed, 7 Jan 2026 20:54:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.8.75.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819249; cv=none; b=MkjYSDQaQAyyYYsd3QjWvodgGdE1Mpx4ySTbvq/0aUkPNnrVM7PxfJdLDunW9jIvWGbEOKaVr6lE7LqAnt0/C7BNrpL+InWNzvhPNkwk1KwO66v9D5yw2MUBlrNabwNI8VYLxMs1dBA6eO2DMzmaJh0j+GvPwpT/ScUIF74IXgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819249; c=relaxed/simple; bh=+DCHUUSyyM1kANcD3QYs0ElAq8Y2r0xCGWcdKoADy1U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pfh5P+wh6VWW2bwLl27yCt85IwMPF8chbh4ntmT6SZA6TiGpCG0V7KCTBdxau1No6dCyLmde8Bf46oxUFbolPUaUxRMqVCIqOqIFSIGrpljaGls/1/V8yacFbM+10Hpb0zr+GVS2Qn+gXBpvvgNXIWyXpke8CkyEjXg71LURh2U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev; spf=pass smtp.mailfrom=mail.selcloud.ru; dkim=pass (1024-bit key) header.d=mail.selcloud.ru header.i=@mail.selcloud.ru header.b=rYkZVxEh; dkim=pass (1024-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=Rc+44m0p; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=dQtkPqkt; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=Fq/3aGKs; arc=none smtp.client-ip=5.8.75.169 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mail.selcloud.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mail.selcloud.ru header.i=@mail.selcloud.ru header.b="rYkZVxEh"; dkim=pass (1024-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="Rc+44m0p"; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="dQtkPqkt"; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="Fq/3aGKs" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.selcloud.ru; s=selcloud; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:List-id: List-Unsubscribe:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Help:List-Subscribe:List-Post:List-Owner:List-Archive; bh=EoPPcByryAWO+tKsOanY3nkf4rGppMtkx5HzY6lwzZ8=; t=1767819245; x=1767992045; b=rYkZVxEhRTp18W65V9T0YH15W0AzPPqIhgnvLMtibdMKU5pt9ntZmnP5OKgIjcFeS31jhkplCx W5g4Yj95edHBRh+CXa/BKnpS6Z+9qWEjH6JhK86czDtrs52rmXLhQ5n+atRA3UFB9+pS10JKleUC9 26EdskskmSVbPtHLdbrI=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=foxido.dev; s=selcloud; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:List-id:List-Unsubscribe:Sender:Reply-To: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Help:List-Subscribe: List-Post:List-Owner:List-Archive; bh=EoPPcByryAWO+tKsOanY3nkf4rGppMtkx5HzY6lwzZ8=; t=1767819245; x=1767992045; b=Rc+44m0p9zZgp7vItlgqZkHfNwHfDEIkg0XQpxAQ65gifJdaDmDMBTh0r1QHC3U9yhH9KxISqD wD0C3Q8SXqT3aLWZf4LyDSfWewbc8B17qkPIv6nGR6mbRr8dz6dhscX9JLKTi0LVCMR7eA7P+RZhw IhVwSjHf89BrXuJupAAw=; Precedence: bulk X-Issuen: 1539712 X-User: 335779833 X-Postmaster-Msgtype: 3849 Feedback-ID: 1539712:15965:3849:samotpravil X-From: foxido.dev X-from-id: 15965 X-MSG-TYPE: bulk List-Unsubscribe-Post: List-Unsubscribe=One-Click X-blist-id: 3849 X-Gungo: 20251225.193711 X-SMTPUID: mlgnr60 DKIM-Signature: v=1; a=rsa-sha256; s=202508r; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=EoPPcByryAWO+tKsOanY3nk f4rGppMtkx5HzY6lwzZ8=; b=dQtkPqktYNVkdp8Rb+VqoXQr/2NM/cjzk1+FfhskV7jwo4tkmG sgTz6EmcDYTeQnVf5aOUrGHbgVtJl9g/hDQTEj6NFYldAJOaep5n/z5IpZRy8EtAOfz6eYgtfIp 0a0XYMe9VxGv48aItAzpgJ/JXidABghXr6ysD4pMnHkHxJYnsvln21PWtbNsrWFourTqUvKSJvb G/UXAHo3bSwKeWb3hjyOK0YNnQZqMJnZBpucyig8u7yWk+s0siwKUSTicJcEkTQbE3+UcAglCSi pfncw8p5Q92DJiNfQVB3yiCrkgNfanmK+CMJhP70+YXOYAn4Ni/OKLscQabRGesXUwA==; DKIM-Signature: v=1; a=ed25519-sha256; s=202508e; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=EoPPcByryAWO+tKsOanY3nk f4rGppMtkx5HzY6lwzZ8=; b=Fq/3aGKsvGdB/erd0E0YJyzBhZcdkxbSOEqTOfv45c+eLNEvEO jRn8g6Yls80XnIVFs9jIQEFcHG7EvTuV69Bw==; From: Gladyshev Ilya To: "foxido @ foxido . dev-cc= Rafael J. Wysocki" Cc: Len Brown , Miguel Ojeda , Boqun Feng , Gary Guo , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Armin Wolf , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-acpi@vger.kernel.org, Gladyshev Ilya Subject: [PATCH 2/3] rust: implement wrapper for acpi_object Date: Wed, 7 Jan 2026 23:35:32 +0300 Message-ID: <05261e88ac8503e2af59d703389d94cc15f4450d.1767818037.git.foxido@foxido.dev> X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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" ACPI Object is represented via union on C-side. On Rust side, this union is transparently wrapped for each ACPI Type, with individual methods and Defer implementation to represented type (integer, string, buffer, etc). Signed-off-by: Gladyshev Ilya --- rust/kernel/acpi.rs | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/rust/kernel/acpi.rs b/rust/kernel/acpi.rs index 9b8efa623130..ac1a9f305f6c 100644 --- a/rust/kernel/acpi.rs +++ b/rust/kernel/acpi.rs @@ -2,6 +2,8 @@ =20 //! Advanced Configuration and Power Interface abstractions. =20 +use core::ops::Deref; + use crate::{ bindings, device_id::{RawDeviceId, RawDeviceIdIndex}, @@ -63,3 +65,92 @@ macro_rules! acpi_device_table { $crate::module_device_table!("acpi", $module_table_name, $table_na= me); }; } + +/// An ACPI object. +/// +/// This structure represents the Rust abstraction for a C [`struct acpi_o= bject`]. +/// You probably want to convert it into actual object type (e.g [`AcpiBuf= fer`]). +/// +/// # Example +/// ``` +/// # use kernel::prelude::*; +/// use kernel::acpi::{AcpiObject, AcpiBuffer}; +/// +/// fn read_first_acpi_byte(obj: &AcpiObject) -> Result { +/// let buf: &AcpiBuffer =3D obj.try_into()?; +/// +/// Ok(buf[0]) +/// } +/// ``` +/// +/// [`struct acpi_object`]: srctree/include/acpi/actypes.h +#[repr(transparent)] +pub struct AcpiObject(bindings::acpi_object); + +impl AcpiObject { + /// Returns object type id (see [`actypes.h`](srctree/include/acpi/act= ypes.h)). + pub fn type_id(&self) -> u32 { + // SAFETY: `type` field is valid in all union variants. + unsafe { self.0.type_ } + } +} + +/// Generate wrapper type for AcpiObject subtype. +/// +/// For given subtype implements +/// - `#[repr(transparent)]` type wrapper, +/// - `TryFrom<&AcpiObject> for &SubType` trait, +/// - unsafe from_unchecked() for 'trusted' conversion. +macro_rules! acpi_object_subtype { + ($subtype_name:ident <- ($acpi_type:ident, $field_name:ident, $union_t= ype:ty)) =3D> { + /// Wraps `acpi_object` subtype. + #[repr(transparent)] + pub struct $subtype_name($union_type); + + impl<'a> TryFrom<&'a AcpiObject> for &'a $subtype_name { + type Error =3D Error; + + fn try_from(value: &'a AcpiObject) -> core::result::Result { + if (value.type_id() !=3D $subtype_name::ACPI_TYPE) { + return Err(EINVAL); + } + + // SAFETY: Requested cast is valid because we validated ty= pe_id + Ok(unsafe { $subtype_name::from_unchecked(&value) }) + } + } + + impl $subtype_name { + /// Int value, representing this ACPI type (see [`acpitypes.h`= ]). + /// + /// [`acpitypes.h`]: srctree/include/linux/acpitypes.h + pub const ACPI_TYPE: u32 =3D bindings::$acpi_type; + + /// Converts opaque AcpiObject reference into exact ACPI type = reference. + /// + /// # Safety + /// + /// - Requested cast should be valid (value.type_id() is `Self= ::ACPI_TYPE`). + pub unsafe fn from_unchecked(value: &AcpiObject) -> &Self { + // SAFETY: + // - $field_name is currently active union's field due to = external safety contract, + // - Transmuting to `repr(transparent)` wrapper is safe. + unsafe { + ::core::mem::transmute::<&$union_type, &$subtype_name>= (&value.0.$field_name) + } + } + } + }; +} + +acpi_object_subtype!(AcpiBuffer + <- (ACPI_TYPE_BUFFER, buffer, bindings::acpi_object__bindgen_ty_3)); + +impl Deref for AcpiBuffer { + type Target =3D [u8]; + + fn deref(&self) -> &Self::Target { + // SAFETY: (pointer, length) indeed represents byte slice. + unsafe { ::core::slice::from_raw_parts(self.0.pointer, self.0.leng= th as usize) } + } +} --=20 2.52.0 From nobody Mon Feb 9 04:30:13 2026 Received: from sender5.mail.selcloud.ru (sender5.mail.selcloud.ru [5.8.75.168]) (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 8F26932ABFF; Wed, 7 Jan 2026 20:54:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=5.8.75.168 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819259; cv=none; b=VcGX3DhYTzHQ5ja/BrQU5hV5LsOCgHGEkWkAm7AxL6YlDP/4EAOo6P109rYGeYO9TD5gB2/j95rGIfIcz7JB3YS27FP/MVlgJ6AnYEjTfiBz9WklBpyQONPjtODfltVKQYLnwG8RabRI1LPy7NcUL/+VgWdJIAm9zCSKQ5VpdAo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767819259; c=relaxed/simple; bh=NBQ9TY3kF5fw/EoSTz/33J3iAwOL5DU0ElmmRDR60Tw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iWCxKMV1ILJD32UvVh33Fs7qrgfWuiBQ30+CWOmfqlq66SHIR3bm8DvhHmCwRamBzJ575wJiRmKL0tNw4pwL8krE6x+Lfn+myHZeq3QiK3qTxoA+5K27z9SlvqzKR0Az7hagjw82JrTCgdo1jJnY6LDjhJNNMvb8c6rsOfFqSOw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev; spf=pass smtp.mailfrom=mail.selcloud.ru; dkim=pass (1024-bit key) header.d=mail.selcloud.ru header.i=@mail.selcloud.ru header.b=UcfMCxJa; dkim=pass (1024-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=wFKkpf5l; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=odLQckj4; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b=nSe0+eEL; arc=none smtp.client-ip=5.8.75.168 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=foxido.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mail.selcloud.ru Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mail.selcloud.ru header.i=@mail.selcloud.ru header.b="UcfMCxJa"; dkim=pass (1024-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="wFKkpf5l"; dkim=pass (2048-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="odLQckj4"; dkim=permerror (0-bit key) header.d=foxido.dev header.i=@foxido.dev header.b="nSe0+eEL" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.selcloud.ru; s=selcloud; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:List-id: List-Unsubscribe:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Help:List-Subscribe:List-Post:List-Owner:List-Archive; bh=TjQ4wYNafNpSTegf9S2wuJejqmu04xdA+i2KKIYcbIs=; t=1767819255; x=1767992055; b=UcfMCxJaL4JQUwbeak0qOxZHBacDgNILyGGFQvmRhTTyZIFpco8xZvvWy3mdi2Df/XN45C/7KD q3Qut6krFBlIhFY6svu79CbInIsckftqngIdz3RK+Hm4CobCUM3kWRS5b8vS952i4LMYNJcLh7fR8 b4p6pOTyygGDCWkeLjTo=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=foxido.dev; s=selcloud; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:List-id:List-Unsubscribe:Sender:Reply-To: Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Help:List-Subscribe: List-Post:List-Owner:List-Archive; bh=TjQ4wYNafNpSTegf9S2wuJejqmu04xdA+i2KKIYcbIs=; t=1767819255; x=1767992055; b=wFKkpf5lPLBMfHtWQClle0XB8W36+LZ1+izC1x7gVSZrU2rEZB1zmt9TzFhNSl11QppFMiHbhj o34nEljSMOBOfqXS11RJaQ3GFKXprHKyA5GDNaVpUUuSV7X2qNr7vAE7bDPh20rzmr7Gf5HUkoZrB RutjRo3qauyvK1Jpl3Dg=; Precedence: bulk X-Issuen: 1539712 X-User: 335779833 X-Postmaster-Msgtype: 3849 Feedback-ID: 1539712:15965:3849:samotpravil X-From: foxido.dev X-from-id: 15965 X-MSG-TYPE: bulk List-Unsubscribe-Post: List-Unsubscribe=One-Click X-blist-id: 3849 X-Gungo: 20251225.193711 X-SMTPUID: mlgnr59 DKIM-Signature: v=1; a=rsa-sha256; s=202508r; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=TjQ4wYNafNpSTegf9S2wuJe jqmu04xdA+i2KKIYcbIs=; b=odLQckj4ln9aeeX97359WlXyUG28rAxphOIrhkudIp9G/dZU7/ mcrrkSwbG9FL/TdZ7hYbK1ZeVGsQywboY6MIaxJtROvmMDSqBo/2TDjZ8mHkGQU1FOqjwMgGK9E cpt76zL5cx7WpLTTbXR+ftm3Xni8yXuv4X+HnlFXgLVZx9zYg//krXeGXQf5PMoVH4BaTJaiDFD 9yuVtU1ArYl1E1ARRY4y8Uy71btKbqbHLMnNcw211YSLoXfn4j9EJzi2fnYdVSQyorPVL90dOXq Ii18qRyVDFzDI45GHeaR/FyMmqOISMVi62gBkiw/Bk11EwdIjmC13XsTB/sdRyU9Ofg==; DKIM-Signature: v=1; a=ed25519-sha256; s=202508e; d=foxido.dev; c=relaxed/relaxed; h=Message-ID:Date:Subject:To:From; t=1767818172; bh=TjQ4wYNafNpSTegf9S2wuJe jqmu04xdA+i2KKIYcbIs=; b=nSe0+eELZAvN+dBtuiuucjIjP5RaguJKHmetisl2CodO0a9EuG T3+r8fAmpWNjfqxrCkPGE++SYz1JGKfj2LBQ==; From: Gladyshev Ilya To: "foxido @ foxido . dev-cc= Rafael J. Wysocki" Cc: Len Brown , Miguel Ojeda , Boqun Feng , Gary Guo , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Tamir Duberstein , Armin Wolf , platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-acpi@vger.kernel.org, Gladyshev Ilya Subject: [PATCH 3/3] rust: add WMI abstractions Date: Wed, 7 Jan 2026 23:35:33 +0300 Message-ID: X-Mailer: git-send-email 2.52.0 In-Reply-To: References: 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" This introduces Rust abstraction for WMI subsystem via wmi::Driver trait and module_wmi_driver/wmi_device_table macros. Driver can be probed, notified on events and removed -- almost everything C-side can do. This abstraction will be used by subsequent redmi_wmi_rs reference driver. Signed-off-by: Gladyshev Ilya --- MAINTAINERS | 1 + rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 2 + rust/kernel/wmi.rs | 277 ++++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+) create mode 100644 rust/kernel/wmi.rs diff --git a/MAINTAINERS b/MAINTAINERS index 765ad2daa218..4909ae8be6e3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -404,6 +404,7 @@ F: Documentation/driver-api/wmi.rst F: Documentation/wmi/ F: drivers/platform/wmi/ F: include/uapi/linux/wmi.h +F: rust/kernel/wmi.rs =20 ACRN HYPERVISOR SERVICE MODULE M: Fei Li diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index a067038b4b42..f9671280c6b5 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -85,6 +85,7 @@ #include #include #include +#include #include #include =20 diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index a6eccdba50b5..29bc2b87a103 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -152,6 +152,8 @@ pub mod uaccess; #[cfg(CONFIG_USB =3D "y")] pub mod usb; +#[cfg(CONFIG_ACPI_WMI)] +pub mod wmi; pub mod workqueue; pub mod xarray; =20 diff --git a/rust/kernel/wmi.rs b/rust/kernel/wmi.rs new file mode 100644 index 000000000000..a15258365a2e --- /dev/null +++ b/rust/kernel/wmi.rs @@ -0,0 +1,277 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Abstractions for the WMI devices. +//! +//! C header: [`include/linux/wmi.h`](srctree/include/linux/wmi.h). + +use crate::{ + acpi::AcpiObject, + device, + device_id::{ + RawDeviceId, + RawDeviceIdIndex, // + }, + driver, + error::{ + from_result, + to_result, + VTABLE_DEFAULT_ERROR, // + }, + prelude::*, + types::Opaque, // +}; +use core::{marker::PhantomData, mem::MaybeUninit, ptr::NonNull}; +use macros::vtable; + +/// [`IdTable`](kernel::device_id::IdTable) type for WMI. +pub type IdTable =3D &'static dyn kernel::device_id::IdTable; + +/// The WMI driver trait. +#[vtable] +pub trait Driver: Send { + /// The type holding information about each one of the device ids supp= orted by the driver. + type IdInfo: 'static; + + /// The table of device ids supported by the driver. + const TABLE: IdTable; + + /// WMI driver probe. + /// + /// Called when a new WMI device is bound to this driver. + /// Implementers should attempt to initialize the driver here. + fn probe(dev: &Device, id_info: &Self::IdInfo) -> impl P= inInit; + + /// WMI device notify. + /// + /// Called when new WMI event received from bounded device. + fn notify(self: Pin<&Self>, _dev: &Device, _event: Optio= n<&AcpiObject>) { + build_error!(VTABLE_DEFAULT_ERROR); + } + + /// WMI driver remove. + fn unbind(self: Pin<&Self>, _dev: &Device) { + build_error!(VTABLE_DEFAULT_ERROR); + } +} + +/// A WMI device. +/// +/// This structure represents the Rust abstraction for a C [`struct wmi_de= vice`]. +/// The implementation abstracts the usage of a C [`struct wmi_device`] pa= ssed +/// in from the C side. +pub struct Device { + inner: Opaque, + _p: PhantomData, +} + +impl Device { + fn as_raw(&self) -> *mut bindings::wmi_device { + self.inner.get() + } +} + +/// An adapter for the registration of WMI drivers. +pub struct Adapter(T); + +// SAFETY: A call to `unregister` for a given instance of `RegType` is gua= ranteed to be valid if +// a preceding call to `register` has been successful. +unsafe impl driver::RegistrationOps for Adapter { + type RegType =3D bindings::wmi_driver; + + unsafe fn register( + wdrv: &Opaque, + name: &'static CStr, + module: &'static ThisModule, + ) -> Result { + macro_rules! map_callback { + ($flag:ident -> $callback:ident) =3D> { + if T::$flag { + Some(Self::$callback) + } else { + None + } + }; + } + + // SAFETY: It's safe to set the fields of `struct wmi_driver` on i= nitialization. + unsafe { + (*wdrv.get()).driver.name =3D name.as_char_ptr(); + (*wdrv.get()).driver.probe_type =3D bindings::probe_type_PROBE= _PREFER_ASYNCHRONOUS; + (*wdrv.get()).id_table =3D T::TABLE.as_ptr(); + (*wdrv.get()).probe =3D map_callback!(HAS_PROBE -> probe_callb= ack); + (*wdrv.get()).notify =3D map_callback!(HAS_NOTIFY -> notify_ca= llback); + (*wdrv.get()).remove =3D map_callback!(HAS_UNBIND -> remove_ca= llback); + (*wdrv.get()).shutdown =3D None; + (*wdrv.get()).no_singleton =3D true; + (*wdrv.get()).no_notify_data =3D true; + } + + // SAFETY: `wdrv` is guaranteed to be a valid `RegType`. + to_result(unsafe { bindings::__wmi_driver_register(wdrv.get(), mod= ule.0) }) + } + + unsafe fn unregister(wdrv: &Opaque) { + // SAFETY: `wdrv` is guaranteed to be a valid `RegType`. + unsafe { bindings::wmi_driver_unregister(wdrv.get()) }; + } +} + +impl Adapter { + extern "C" fn probe_callback( + wdev: *mut bindings::wmi_device, + id: *const c_void, + ) -> kernel::ffi::c_int { + // SAFETY: The WMI core only ever calls the probe callback with a = valid pointer to a + // `struct wmi_device`. + // + // INVARIANT: `wdev` is valid for the duration of `probe_callback(= )`. + let wdev =3D unsafe { &*wdev.cast::>(= ) }; + + let id =3D id as usize; + let info =3D T::TABLE.info(id); + + from_result(|| { + let data =3D T::probe(wdev, info); + + wdev.as_ref().set_drvdata(data)?; + Ok(0) + }) + } + + extern "C" fn notify_callback( + wdev: *mut bindings::wmi_device, + obj: *mut bindings::acpi_object, + ) { + // SAFETY: The WMI system only ever calls the notify callback with= a valid pointer to a + // `struct wmi_device`. + let wdev =3D unsafe { &*wdev.cast::>(= ) }; + // SAFETY: + // - AcpiObject is repr(transparent) wrapper around FFI object, so= it's safe to cast + // raw pointer to reference (in terms of alignment and etc), + // - Option<&ref> is guaranteed to have same layout as raw pointer= (with NULL representing + // None) by Rust's "nullable pointer optimization". + let obj: Option<&AcpiObject> =3D unsafe { core::mem::transmute(obj= as *const AcpiObject) }; + + // SAFETY: `notify_callback` is only ever called after a successfu= l call to + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `T`. + let this =3D unsafe { wdev.as_ref().drvdata_borrow::() }; + this.notify(wdev, obj); + } + + extern "C" fn remove_callback(wdev: *mut bindings::wmi_device) { + // SAFETY: The WMI system only ever calls the remove callback with= a valid pointer to a + // `struct wmi_device`. + let wdev =3D unsafe { &*wdev.cast::>(= ) }; + + // SAFETY: `remove_callback` is only ever called after a successfu= l call to + // `probe_callback`, hence it's guaranteed that `Device::set_drvda= ta()` has been called + // and stored a `T`. + let this =3D unsafe { wdev.as_ref().drvdata_borrow::() }; + this.unbind(wdev); + } +} + +impl AsRef> for Device { + fn as_ref(&self) -> &device::Device { + // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a p= ointer to a valid + // `struct platform_device`. + let dev =3D unsafe { &raw mut (*self.inner.get()).dev }; + + // SAFETY: `dev` points to a valid `struct device`. + unsafe { device::Device::from_raw(dev) } + } +} + +kernel::impl_device_context_deref!(unsafe { Device }); +kernel::impl_device_context_into_aref!(Device); + +// SAFETY: Instances of `Device` are always reference-counted. +unsafe impl crate::sync::aref::AlwaysRefCounted for Device { + fn inc_ref(&self) { + // SAFETY: The existence of a shared reference guarantees that the= refcount is non-zero. + unsafe { bindings::get_device(self.as_ref().as_raw()) }; + } + + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The safety requirements guarantee that the refcount is = non-zero. + unsafe { bindings::put_device(&raw mut (*obj.as_ref().as_raw()).de= v) } + } +} + +/// Abstraction for the WMI device ID structure, i.e. [`struct wmi_device_= id`]. +/// +/// [`struct wmi_device_id`]: https://docs.kernel.org/driver-api/basics.ht= ml#c.wmi_device_id +#[repr(transparent)] +pub struct DeviceId(bindings::wmi_device_id); + +impl DeviceId { + /// Constructs new DeviceId from GUID string. + pub const fn new(guid: &[u8; bindings::UUID_STRING_LEN as usize]) -> S= elf { + // SAFETY: FFI type is valid to be zero-initialized. + let mut inner: bindings::wmi_device_id =3D unsafe { MaybeUninit::z= eroed().assume_init() }; + + build_assert!(inner.guid_string.len() =3D=3D bindings::UUID_STRING= _LEN as usize + 1); + + // SAFETY: It's safe to copy UUID_STRING_LEN, because we validated= lengths. + // Also we leave last byte zeroed, so guid_string is valid C strin= g. + unsafe { + ::core::ptr::copy_nonoverlapping( + guid.as_ptr(), + &raw mut inner.guid_string[0], + bindings::UUID_STRING_LEN as usize, + ); + } + + Self(inner) + } +} + +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `wmi_device_i= d` and does not add +// additional invariants, so it's safe to transmute to `RawType`. +unsafe impl RawDeviceId for DeviceId { + type RawType =3D bindings::wmi_device_id; +} + +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `context` field. +unsafe impl RawDeviceIdIndex for DeviceId { + const DRIVER_DATA_OFFSET: usize =3D core::mem::offset_of!(bindings::wm= i_device_id, context); + + fn index(&self) -> usize { + self.0.context as usize + } +} + +/// Declares a kernel module that exposes a single WMI driver. +/// +/// # Examples +/// +/// ```ignore +/// module_wmi_driver! { +/// type: MyDriver, +/// name: "Module name", +/// author: ["Author name"], +/// description: "Description", +/// license: "GPL v2", +/// } +/// ``` +#[macro_export] +macro_rules! module_wmi_driver { + ($($f:tt)*) =3D> { + $crate::module_driver!(, $crate::wmi::Adapter, { $($f)* }); + } +} + +/// Create a WMI `IdTable` with its alias for modpost. +#[macro_export] +macro_rules! wmi_device_table { + ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $tabl= e_data: expr) =3D> { + const $table_name: $crate::device_id::IdArray< + $crate::wmi::DeviceId, + $id_info_type, + { $table_data.len() }, + > =3D $crate::device_id::IdArray::new($table_data); + + $crate::module_device_table!("wmi", $module_table_name, $table_nam= e); + }; +} --=20 2.52.0