From nobody Sat Oct 11 00:28:59 2025 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (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 1119615C0; Wed, 11 Jun 2025 20:30:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673824; cv=none; b=jWIMLZ95DmAp8cqZgW6MMq/GL7o/1lQ0mYan50ZKhwnKGk89kd9NZ7eQNNMwN5VZ6sadpRnfPPHDft73oaJCW2U8UVljNcoe61AdhQto5MjlIDnCr97IPoR6hP3c4tExiiPxx3+CyXuk4StK2BblqXVhtuvwuqNIMiWbLdDIDqU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673824; c=relaxed/simple; bh=jmv1s8okU7fGVIgSZoVVYiYUYXdg54/SEyU1yXSHBmQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MfxsdYClv3k9ihJYCyCt6q5JqPl6nbzI1FwlliQR3XqM49oNUsMLjBp/wxg/MyOBSYbMuM2qL2ZwR6f8YDzjHlpyvlWGd4F/6eYTdAhGOdL8JmsI0TmiRXTNQQRcsMZdM7uiILtENUKnFD/zDNbWgtzKTCfmgIBjJho0h6LvFAs= 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=kkL+aFQy; arc=none smtp.client-ip=209.85.161.44 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="kkL+aFQy" Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-610d86b3da9so115605eaf.0; Wed, 11 Jun 2025 13:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749673822; x=1750278622; 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=sFWeRrlFpOMM4iQ4a6IP8kikC8hDTgeRtbqFoG+aocs=; b=kkL+aFQyYw/Zt3YkzP+Yi3yaelfqwT6CNVH329DqW5yRHPbHJ9wVvzf1pZYxX95e/V qVkG8putPsuWCKt4GwOAhIKRhbJlnue618Gq3kXIVg8YUwojQt5/yHO0DBv/4/OGfjPS T39UPpcSj/aXy9RL/OIaJd6qsL6gbkHtLTWIB6Oqjn1LEohahhxfubQfGw2n5NUYjXFU rvTWHanUeICtLPKqjvvAF6aY8uYEMJVWU34UVUsYz0N6PozvecULvd3UpwigmLW00IT7 RD/vhHdeYndlWsZFdjW4TtFcAmiFn2ev8G3aFlbyTcdDBq/RSDXlVYWmXvgD80xOH3M5 37/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749673822; x=1750278622; 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=sFWeRrlFpOMM4iQ4a6IP8kikC8hDTgeRtbqFoG+aocs=; b=dv0b9oAzRdJ/InAnwbbLw0MBJeVH7v7aLBGY4l/43yqzgdmh2dN8XYDes79shUkSP8 8a/83kvhPa4Bf8kncSSOV5Twnp4DEVxh1UCSqF9L5g5Gc7Gp3WZnjZMHRXwEYpMAFHTA al+8J90rjeftcis0p2hwv1qj02c2uSybUGNgCbtyZ5RMk1lVwyJSkUbD+Qwk4lPcTMRO /Y4hVixs2MVztvgF+3lh5xMwiRPdDIZZwRfM0s3xbfhdv2Cio4ahnp+zJaQpcw6vMc7Y 9dAftRG5Ej3mDr6vt3ybWTOehLtbm3CLbR1W32MC4IfXkDLVJR6Itn4qQxLm4Rb38aYK GoWQ== X-Forwarded-Encrypted: i=1; AJvYcCVaci1xZka//qbhWIa61wg/a2E2v1TwpTLMMZUguQR4r5PxSsOUq4s5or3sukuS2WwuYDXFnW9jECZ46znHhws=@vger.kernel.org, AJvYcCVyiW/RDm1S43WoXJgpnD37tJXONXcBO9J5xU1LM9ZGznII8iF+lLU9GOKxJuiFjaKy0Q4jNYbvQ6rnUXg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx3zqYrxKFygAm8sknZLEAQB21ME2Cu43mz/wIRIrpepuISpsls ExPsXW2iPWxaKXzAcX9qmwO4O10Elxtst0kvVRK02YyusM0lkghUL5OD X-Gm-Gg: ASbGncspU8c2HNfoyrkNCRiMch1t/jEiWYw3upoz5h1zx8Sl7mbshwaxinSVMwMZQqM A4r4c66dEUC3U1yF8f0hy2pE+bhRNDb2MNRpt7Se7p5pW5PcdYjwy4HX05POqj0okuqvwh5SqVR NHy4gFlbWr3ijc1i+mI3wNQUbib1LrpBy1FqI0CjkWMKpWlpVE8hVgSpyaDrmRH1v0c6fWGTcn4 wqVfk2jddidYnbZGcaRIGLSnoVxq3ejGa+E9RJEBIy0kIk+6oz0xO/o3uPmEhcFopeRErxS2t3D 8bE6AT2RJbZtz/MVOTRoZ9GTPP3+v2jxp8H4SUVInUm5N/1z4n2QJuKlET0d3gkuJS/R9XO1H1j zDGaw/fSpM1F+DXH1MbbSwOxPPPPVyx2hIqZaZ7vr3MbG X-Google-Smtp-Source: AGHT+IHfExfMOVgwkoL3DIkAd/QDD7c9tb8K0wezR2X4uTYHBas9Vi7SeG9b3yryvsowf3ZTsm7+eA== X-Received: by 2002:a05:6820:216:b0:610:fbf2:bd7d with SMTP id 006d021491bc7-610fbf2c05emr769570eaf.6.1749673822021; Wed, 11 Jun 2025 13:30:22 -0700 (PDT) Received: from my-computer.lan (c-73-76-29-249.hsd1.tx.comcast.net. [73.76.29.249]) by smtp.googlemail.com with ESMTPSA id 006d021491bc7-610fd44cc85sm19129eaf.16.2025.06.11.13.30.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jun 2025 13:30:21 -0700 (PDT) From: Andrew Ballance To: jbaron@akamai.com, jim.cromie@gmail.com, daniel.almeida@collabora.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, rostedt@goodmis.org, andrewjballance@gmail.com Cc: viresh.kumar@linaro.org, lina+kernel@asahilina.net, tamird@gmail.com, jubalh@iodoru.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] rust: static jump: add support for nested arguments Date: Wed, 11 Jun 2025 15:29:50 -0500 Message-ID: <20250611202952.1670168-2-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611202952.1670168-1-andrewjballance@gmail.com> References: <20250611202952.1670168-1-andrewjballance@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" allows for nested arguments to be used with the static_branch macro. e.g. `outer.inner.key` can now be accessed by the macro Signed-off-by: Andrew Ballance --- rust/kernel/jump_label.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs index 4e974c768dbd..4ea3cbb340ff 100644 --- a/rust/kernel/jump_label.rs +++ b/rust/kernel/jump_label.rs @@ -19,9 +19,9 @@ /// The macro must be used with a real static key defined by C. #[macro_export] macro_rules! static_branch_unlikely { - ($key:path, $keytyp:ty, $field:ident) =3D> {{ + ($key:path, $keytyp:ty, $field:ident $(.$field_cont:ident)*) =3D> {{ let _key: *const $keytyp =3D ::core::ptr::addr_of!($key); - let _key: *const $crate::bindings::static_key_false =3D ::core::pt= r::addr_of!((*_key).$field); + let _key: *const $crate::bindings::static_key_false =3D ::core::pt= r::addr_of!((*_key).$field$(.$field_cont)*); let _key: *const $crate::bindings::static_key =3D _key.cast(); =20 #[cfg(not(CONFIG_JUMP_LABEL))] @@ -30,7 +30,7 @@ macro_rules! static_branch_unlikely { } =20 #[cfg(CONFIG_JUMP_LABEL)] - $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, f= alse } + $crate::jump_label::arch_static_branch! { $key, $keytyp, $field$(.= $field_cont)*, false } }}; } pub use static_branch_unlikely; @@ -46,14 +46,14 @@ macro_rules! static_branch_unlikely { #[doc(hidden)] #[cfg(CONFIG_JUMP_LABEL)] macro_rules! arch_static_branch { - ($key:path, $keytyp:ty, $field:ident, $branch:expr) =3D> {'my_label: { + ($key:path, $keytyp:ty, $field:ident $(.$field_cont:ident)*, $branch:e= xpr) =3D> {'my_label: { $crate::asm!( include!(concat!(env!("OBJTREE"), "/rust/kernel/generated_arch= _static_branch_asm.rs")); l_yes =3D label { break 'my_label true; }, symb =3D sym $key, - off =3D const ::core::mem::offset_of!($keytyp, $field), + off =3D const ::core::mem::offset_of!($keytyp, $field$(.$field= _cont)*), branch =3D const $crate::jump_label::bool_to_int($branch), ); =20 --=20 2.49.0 From nobody Sat Oct 11 00:28:59 2025 Received: from mail-oi1-f178.google.com (mail-oi1-f178.google.com [209.85.167.178]) (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 1D24823C8A4; Wed, 11 Jun 2025 20:30:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673829; cv=none; b=UTDAyuZEoLBW4qHI+VxzVeQF9EebtAuliB/fNzsLJw0Lj06VPS/zD2znWIVp5ij3EhKiQdIA8nd5KCAJ4OELBdbeZSwAt1W1nSNnbGzhswscPA0baHwG2Fx6A/41aYFa+qTUG2N4uu0u0sqgG810YmmGXf7seaiNXtZYHBLUzac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673829; c=relaxed/simple; bh=HlxWIgcO2YAUwRogGvZWE3e36d37xhYkW6Z6frtFmMg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uOQGc9pQXIrb1hSEm+Jtz7ydOgq9fKflm2DBHVtkLjL9+pz4aNLjGGVKU31b+D5D8PUWfJPsmIU4Hz+naHvpXKmWUEOPuebCZFHnK6uEh1TR9buQef6nlYFEmPCiU62yNwlNu6TaURbJ041ByY/ZUnvV9mttfCTQqg4ZogZ++WE= 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=H/z7UYpw; arc=none smtp.client-ip=209.85.167.178 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="H/z7UYpw" Received: by mail-oi1-f178.google.com with SMTP id 5614622812f47-407a3c10d03so153924b6e.2; Wed, 11 Jun 2025 13:30:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749673827; x=1750278627; 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=7F1aFIM16QoXbhR1Ql3MIkyQxYsSme3Sb2YHK9N7YB0=; b=H/z7UYpwQ2/cjaNchLfdxR7oSHtuPzGvenIvQHoA33ZCrCWHfb/OIhHSA/oXXenRjU S9iQu7B5k0G4V8wNFcSYbMf6cSilfc9F/zQjBoziqEG1dwHwaba/drQmoB3HI7Frem0h lC83/xW6qkESU7PLCrdsuWHnwnygl6S3m6RDUJAraFfZ0IGQSo4T+b5RQkOH/HMhLe/L uCuRRig+tVKji4sGU7WGd4Sqf12vm5LJDcK43R64O2+UOEonbbjs0Fnynr+iLoUlRl6V cBYe+OR9vmi2C86+JrmEq0WfyGflj9+SdAu8MUgIz6x6//gc+W1SQF2GioFMWLCiRnvW Cn1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749673827; x=1750278627; 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=7F1aFIM16QoXbhR1Ql3MIkyQxYsSme3Sb2YHK9N7YB0=; b=LskkBS0LuAwoM6kUAqbbcyiuWdvBeoBzPKv92AkJBaZnNE7iCytO3ZMixqvukauZWP 24kfh3ZOSeF9D5+jKU+FMBu/63pbq8jBfb+sAmJTgjurJISA21bLXoWnWtma1mNjwJz9 /k3WcyK4sA9zw2ZN4PP8ZNw6HjJ7WPTnQtXldVtIe1ul8ZeyPztAvxaNFz0HkPZGc8he 5ToFCG8KFJIIaSBxatVsEw8vglCmRSGgYLPXSqDrOsbjnIqiNDr+JXD2YSJSYoxyiJGZ SoMExcgGhd2nSvgtAQ6w5m/ojRwTbIlinVNRLQBSXvdUkIRyQwC5BrsEdbyxGhzgUJKh V2HQ== X-Forwarded-Encrypted: i=1; AJvYcCUQFAlFwYEDCEC3uV2O5VBOmvpuXMcfEZY6R46sUFvjMt0wSjS70qbqwXmAXSMI1ORNhBh8lreeI05vYVTupGg=@vger.kernel.org, AJvYcCV4KdQ9Di4sreuBmYWgnoMz3tMlw1BHLFdw5tsl/1iuD2EkDn1oHetEq5cBh+1lk3EQMVR0suffOLtVCao=@vger.kernel.org X-Gm-Message-State: AOJu0YzKU6xDkNzuh5t60OD2viAtAtuZ0IS3xvRtrv2PneJRFQIGqgqQ 3JbsWJqJ77rZK5roeEt5jf8XX9Df8iZGxt5DzDGexqvTiCaP42OUkF4K X-Gm-Gg: ASbGncu/P4qlpvFFHEuTVzeQtxy3dtY78lbmkMmrrVZfScuI4+gnzTSGmduYiHgxHdt sFU/i8qlzKuvX7MKWWF0bwA4uNsONKE1fZx+Q+ea5HNDPB/DjT/gCo14E2pBAQ5Ofg4Rzu2JfJv BXtztX57dsVub2pTMCisXXMUBOahn0oZG9Abp3VRY2KSTexyaJ7S4zmHY9D4NzLPBhdu2K8oiUr nelPN/KSjKCWOoKz21YLWn/lkJDSx8QjxQkQGRd+8YEpWH2xEnUB6XMeKyyFSvnJViTVKRKjrAt J7P447md4cT3wH9H/N9sUdZxg95JkIO4yc0S3Xw2pm5EeftzblSrt5Ctwtaf9qDIqEAk7v+nrEC 0QTjhI64oF8oDyx1fKKD+LsfHRGTyBcL1dw== X-Google-Smtp-Source: AGHT+IH84ZfXGvPYL8b9G78h//kg+YTPpdxAkU3C8niVW1ioQJ2WQGT1MXzuNHxit0+xTYeRY/hy3Q== X-Received: by 2002:a05:6808:1889:b0:401:e963:e973 with SMTP id 5614622812f47-40a66b25a16mr304173b6e.30.1749673827036; Wed, 11 Jun 2025 13:30:27 -0700 (PDT) Received: from my-computer.lan (c-73-76-29-249.hsd1.tx.comcast.net. [73.76.29.249]) by smtp.googlemail.com with ESMTPSA id 006d021491bc7-610fd44cc85sm19129eaf.16.2025.06.11.13.30.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jun 2025 13:30:26 -0700 (PDT) From: Andrew Ballance To: jbaron@akamai.com, jim.cromie@gmail.com, daniel.almeida@collabora.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, rostedt@goodmis.org, andrewjballance@gmail.com Cc: viresh.kumar@linaro.org, lina+kernel@asahilina.net, tamird@gmail.com, jubalh@iodoru.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/3] rust: device add support for dynamic debug to pr_debug! Date: Wed, 11 Jun 2025 15:29:51 -0500 Message-ID: <20250611202952.1670168-3-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611202952.1670168-1-andrewjballance@gmail.com> References: <20250611202952.1670168-1-andrewjballance@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" adds support for dynamic debug for the pr_debug macro. Signed-off-by: Andrew Ballance --- rust/bindings/bindings_helper.h | 1 + rust/kernel/print.rs | 167 +++++++++++++++++++++++++++++++- 2 files changed, 164 insertions(+), 4 deletions(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index bc494745f67b..e05e9ce5d887 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -46,6 +46,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/kernel/print.rs b/rust/kernel/print.rs index 9783d960a97a..4f0d79804d23 100644 --- a/rust/kernel/print.rs +++ b/rust/kernel/print.rs @@ -371,13 +371,15 @@ macro_rules! pr_info ( /// /// Use this level for debug messages. /// -/// Equivalent to the kernel's [`pr_debug`] macro, except that it doesn't = support dynamic debug -/// yet. +/// Equivalent to the kernel's [`pr_debug`] macro. +/// +/// This has support for [`dynamic debug`]. /// /// Mimics the interface of [`std::print!`]. See [`core::fmt`] and /// [`std::format!`] for information about the formatting syntax. /// /// [`pr_debug`]: https://docs.kernel.org/core-api/printk-basics.html#c.pr= _debug +/// [`dynamic debug`]: https://docs.kernel.org/admin-guide/dynamic-debug-h= owto.html /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html /// @@ -390,8 +392,18 @@ macro_rules! pr_info ( #[doc(alias =3D "print")] macro_rules! pr_debug ( ($($arg:tt)*) =3D> ( - if cfg!(debug_assertions) { - $crate::print_macro!($crate::print::format_strings::DEBUG, fal= se, $($arg)*) + #[cfg(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG))] + { + if cfg!(debug_assertions) { + $crate::dynamic_pr_debug_unlikely!($($arg)*); + } + } + + #[cfg(not(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG)))] + { + if cfg!(debug_assertions) { + $crate::print_macro!($crate::print::format_strings::DEBUG,= false, $($arg)*) + } } ) ); @@ -423,3 +435,150 @@ macro_rules! pr_cont ( $crate::print_macro!($crate::print::format_strings::CONT, true, $(= $arg)*) ) ); + +/// all of the code that is used for dynamic debug for pr_debug! +/// this is public but hidden. This code should only be called +/// by the `pr_debug!` or `dev_dbg!` macros. +#[cfg(CONFIG_DYNAMIC_DEBUG_CORE)] +#[doc(hidden)] +pub mod dynamic_debug { + + pub use bindings::_ddebug; + + use crate::c_str; + use core::fmt; + use kernel::str::CStr; + + /// a wrapper around the C `struct _ddebug`. + /// this is public but hidden. + /// + /// # Invariants + /// - this is always static mut. + /// - this is always located in the "__dyndbg" section. + /// - this has the same layout as `_ddebug`. + #[repr(transparent)] + pub struct _Ddebug { + pub inner: bindings::_ddebug, + } + + impl _Ddebug { + pub const fn new_unlikely( + modname: &'static CStr, + function: &'static CStr, + filename: &'static CStr, + format: &'static CStr, + line_num: u32, + ) -> Self { + // rust does not have support for c like bit fields. so + // do some bit fiddling to set the line, class and flags varib= les + let class: u32 =3D bindings::_DPRINTK_CLASS_DFLT << 18; + let flags: u32 =3D bindings::_DPRINTK_FLAGS_NONE << 24; + let bit_fields: u32 =3D line_num | class | flags; + + let arr: [u8; 4] =3D bit_fields.to_ne_bytes(); + let bits =3D bindings::__BindgenBitfieldUnit::new(arr); + + #[cfg(CONFIG_JUMP_LABEL)] + { + Self { + inner: bindings::_ddebug { + modname: modname.as_char_ptr(), + function: function.as_char_ptr(), + filename: filename.as_char_ptr(), + format: format.as_char_ptr(), + _bitfield_align_1: [], + _bitfield_1: bits, + // SAFETY: STATIC_KEY_INIT_FALSE is initialized as= zero + key: unsafe { core::mem::zeroed() }, + }, + } + } + + #[cfg(not(CONFIG_JUMP_LABEL))] + { + Self { + inner: bindings::_ddebug { + modname: modname.as_char_ptr(), + function: function.as_char_ptr(), + filename: filename.as_char_ptr(), + format: format.as_char_ptr(), + _bitfield_align_1: [], + _bitfield_1: bits, + __bindgen_padding_0: 0, + }, + } + } + } + } + + /// a wrapper function around the c function `__dynamic_pr_debug`. + /// # Safety + /// - descriptor must be a valid reference to a `static mut` _Ddebug + pub unsafe fn dynamic_pr_debug(descriptor: &mut _Ddebug, args: fmt::Ar= guments<'_>) { + // SAFETY: + // - "%pA" is null terminated and is the format for rust printing + // - descriptor.inner is a valid _ddebug + unsafe { + bindings::__dynamic_pr_debug( + &raw mut descriptor.inner, + c_str!("%pA").as_char_ptr(), + (&raw const args).cast::(), + ); + } + } + + /// macro for dynamic debug equilant to the C `pr_debug` macro + #[doc(hidden)] + #[macro_export] + macro_rules! dynamic_pr_debug_unlikely { + ($($f:tt)*) =3D> {{ + use $crate::c_str; + use $crate::str::CStr; + use $crate::print::dynamic_debug::{_ddebug, _Ddebug}; + + const MOD_NAME: &CStr =3D c_str!(module_path!()); + // right now rust does not have a function! macro. so, hard co= de this to be + // the name of the macro that is printing + // TODO: + // replace this once either a function! macro exists + // or core::any::type_name becomes const + const FN_NAME: &CStr =3D c_str!("pr_debug!"); + const FILE_NAME: &CStr =3D c_str!(file!()); + const MESSAGE: &CStr =3D c_str!(stringify!($($f)*)); + const LINE: u32 =3D line!(); + + #[link_section =3D "__dyndbg"] + static mut DEBUG_INFO: _Ddebug =3D + _Ddebug::new_unlikely(MOD_NAME, FN_NAME, FILE_NAME, MESSAG= E, LINE); + + // SAFETY: + // - this is reading from a `static mut` variable + // - key.dd_key_false is a valid static key + let should_print: bool =3D unsafe { + #[cfg(CONFIG_JUMP_LABEL)] + { + $crate::jump_label::static_branch_unlikely!( + DEBUG_INFO, + _Ddebug, + inner.key.dd_key_false + ) + } + #[cfg(not(CONFIG_JUMP_LABEL))] + { + // gets the _DPRINTK_FLAGS_PRINT bit + DEBUG_INFO.inner.flags() & 1 !=3D 0 + } + }; + + if should_print { + // SAFETY: `&mut DEBUG_INFO` is a valid reference to a sta= tic mut _Ddebug + unsafe { + $crate::print::dynamic_debug::dynamic_pr_debug( + &mut DEBUG_INFO, + format_args!($($f)*) + ); + } + } + }}; + } +} --=20 2.49.0 From nobody Sat Oct 11 00:28:59 2025 Received: from mail-oi1-f169.google.com (mail-oi1-f169.google.com [209.85.167.169]) (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 422082BEC2E; Wed, 11 Jun 2025 20:30:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673834; cv=none; b=lbn2ueyGFe/SCLIkkNtECx6lG3BpSuDBnrcMBUlMt3tDyqL4nVCXGO0aYaweQpEzkSObCDHyQemtklC8F0HCHlUWdbGkDFcNfJV2B4vd4VzChGcOmLKrJI4LZC/0KW6GCuiU5EZ6ijs26f9DyaCfmIUY0aUFtP7Oc43rdJdv8Oo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749673834; c=relaxed/simple; bh=Vrqvff5jDFcGNcQZuPYGZ5RQ4KL8yEpJ9KW8TrWMNYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ix6wTkr1atCrQ8Gt9UzSoXpsZC1G84KRUL24MT7a20F0vR9YWs0zsGsalSfKkQy3VVa3Uz/i3XFFrKVubGTDHQUeqDKuzI4T2sljKZUZHZGbmAywyGpA/CPI/lcbQMJvDuwxMSjiRKC8SncxPez067njziVca//YvFyoPxCZMUY= 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=KRelzSvT; arc=none smtp.client-ip=209.85.167.169 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="KRelzSvT" Received: by mail-oi1-f169.google.com with SMTP id 5614622812f47-4034118aeb7so69986b6e.0; Wed, 11 Jun 2025 13:30:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749673832; x=1750278632; 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=q4l0RofuIVC7rDkXTNk6jKf/KgoomCHHGke/JzkR01w=; b=KRelzSvTTGdQbwzLlHJVzz/mTbuGCHdFlQVnNp1M0seF7ckUqG4wgloggnpjHyXNm8 +TdHKNgxXw40fRTjVgiOh3dJ4RJp67ev7YBsE2tfr8JGIbPFxhKzg/jJEjtBa+v6wxpf /79hEBJ9acWbjVb2121t+sZUrDQo7RfhYQOFAFOGxRirzRw2DcGXatUhVn1HIDCnYoK5 bxG3gm6Wd9DvSH/ev5HAVr+swhBc9MCo1Fx92u63k+nUqxGjLOAdb7s0xTuSXhfBENDy c6NdOzeBkwQ1oEZJgbxOuJ6eKDK5DfHIlRwD96h8bjgh53U21wHmy/RCiN2qwdEx2MT/ UJvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749673832; x=1750278632; 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=q4l0RofuIVC7rDkXTNk6jKf/KgoomCHHGke/JzkR01w=; b=b+MqHOV3T07Eu8rqQUnOe83kZz9Rzz7xgjRz6fFEFuMfh2TffRKJpBDXcQ3iBkF5Vz HJlEgBbbb7XkWBcbd176mjzuSx63Sc6EGkHoPLJqoqrL8N53/Ip04EOhAK25W6/5/cHp d/xrx8oX8ubCRZMBQjsfllyOGH48ZrhBVu0ZSAv38Jg2Aqtmec5VqgopkUKLfldDta4q 1dF3I/TbgyGd7OyDc8Iyrh9AhUizIsc4oq3LIYVvDv/1GY6SA/wqa96cCmr20Byyh49u 3U/sB6G32me/fhgwRNsybbXU5PRn/VX9SFLH/W+0MSbFXs3FMC67zF3xWbypo80b/w2t LF8w== X-Forwarded-Encrypted: i=1; AJvYcCXPLHksn+ltgAlhZhjwOeYnuGFz8YkXo3r5PwrS9rDLMOQ1WxGFT9mtOfrzGvJ1PF/xlwVm7ej6PcprnJE=@vger.kernel.org, AJvYcCXda4CoyAr2hBJOh7gdIXZamz5dTUM4c6G0WWzMa60orO8Q28dmjj+DGmtCFDEFuE35AVhw8JagHiZQnCXejEM=@vger.kernel.org X-Gm-Message-State: AOJu0Yw3b4SGfoavTelsTl/QIWeZoApgBlMfqFsZ3Zs4IAVfIZdRCCiW DeNhNpOerbQSa9ouMVaXHCOa7ccgu/x5YvaitiTMqWD5YESvUbX5fSWDZ9moKN/+ X-Gm-Gg: ASbGncshIs1wx4J/hOkyLOJ6/XUU/nicJQo0EkmNUHlG8r6qgFPXpGGGUD/3M0Kutsd 8wJudY035fw/1OS7eTcduD9OUjH0I576Hfu/1qoRBFzqyzeZdvM/mb+urxDp+z+uyZlEQF+azj+ /Hc/bnsGWE1ovgjT0Owc3aVOqYPPKU389r5Eo4UM6nsfp29nFQRRcgUl7/FdX+qBn9nSp2UzTv9 HT3nrUQxzOMJ+H6xAY5Dquh4V5Paur/NoqMWRnp8hg0qECpEOPlbyRLiPo9OSmDz6Wsx9wTRWCs Tlr0/qYig2Kb5rcdaE0oIRk49oouWiRpTfqVlHssRja5dsnIEUrKjd89C6TFqy+eI3BJFyYxoVf bNR0iGFFj18uOpQVLDWEY3W97/N036VuCfw== X-Google-Smtp-Source: AGHT+IEK+C4nJhi3JBx0pttmVmGHK803hC/mTGSHJ63I9XWQoLP7w/KRp5+Njqf5dI6BRGWjo/FNhA== X-Received: by 2002:a05:6808:318e:b0:408:fef8:9ca3 with SMTP id 5614622812f47-40a660ef02cmr616591b6e.8.1749673831724; Wed, 11 Jun 2025 13:30:31 -0700 (PDT) Received: from my-computer.lan (c-73-76-29-249.hsd1.tx.comcast.net. [73.76.29.249]) by smtp.googlemail.com with ESMTPSA id 006d021491bc7-610fd44cc85sm19129eaf.16.2025.06.11.13.30.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Jun 2025 13:30:31 -0700 (PDT) From: Andrew Ballance To: jbaron@akamai.com, jim.cromie@gmail.com, daniel.almeida@collabora.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, lossin@kernel.org, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu, dakr@kernel.org, gregkh@linuxfoundation.org, rafael@kernel.org, rostedt@goodmis.org, andrewjballance@gmail.com Cc: viresh.kumar@linaro.org, lina+kernel@asahilina.net, tamird@gmail.com, jubalh@iodoru.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/3] rust: device add support for dynamic debug to dev_dbg! Date: Wed, 11 Jun 2025 15:29:52 -0500 Message-ID: <20250611202952.1670168-4-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250611202952.1670168-1-andrewjballance@gmail.com> References: <20250611202952.1670168-1-andrewjballance@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" adds support for dynamic debug for the dev_dbg macro. Signed-off-by: Andrew Ballance --- rust/kernel/device.rs | 102 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index dea06b79ecb5..f59fed944876 100644 --- a/rust/kernel/device.rs +++ b/rust/kernel/device.rs @@ -536,11 +536,14 @@ macro_rules! dev_info { /// /// This level should be used for debug messages. /// -/// Equivalent to the kernel's `dev_dbg` macro, except that it doesn't sup= port dynamic debug yet. +/// Equivalent to the kernel's `dev_dbg` macro. +/// +/// This has support for [`dynamic debug`]. /// /// Mimics the interface of [`std::print!`]. More information about the sy= ntax is available from /// [`core::fmt`] and [`std::format!`]. /// +/// [`dynamic debug`]: https://docs.kernel.org/admin-guide/dynamic-debug-h= owto.html /// [`std::print!`]: https://doc.rust-lang.org/std/macro.print.html /// [`std::format!`]: https://doc.rust-lang.org/std/macro.format.html /// @@ -555,5 +558,100 @@ macro_rules! dev_info { /// ``` #[macro_export] macro_rules! dev_dbg { - ($($f:tt)*) =3D> { $crate::dev_printk!(pr_dbg, $($f)*); } + ($($f:tt)*) =3D> { + #[cfg(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG))] + { $crate::dynamic_dev_dbg_unlikely!($($f)*); } + + #[cfg(not(any(DYNAMIC_DEBUG_MODULE, CONFIG_DYNAMIC_DEBUG)))] + { $crate::dev_printk!(pr_dbg, $($f)*); } + } +} + +/// this contains all of the code that is used for dynamic debug for dev_d= bg! +/// this is public but hidden. This code should only be called +/// by the `pr_debug!` or `dev_dbg!` macros. +#[cfg(CONFIG_DYNAMIC_DEBUG_CORE)] +#[doc(hidden)] +pub mod dynamic_debug { + + use super::Device; + use kernel::c_str; + use kernel::print::dynamic_debug::_Ddebug; + + /// a wrapper function around the c function `__dynamic_dev_dbg`. + /// # Safety + /// - descriptor must be a valid reference to a `static mut` _Ddebug + pub unsafe fn dynamic_dev_dbg( + descriptor: &mut _Ddebug, + dev: &Device, + args: core::fmt::Arguments<'_>, + ) { + // SAFETY: + // - "%pA" is null terminated and is the format for rust printing + // - dev.as_raw() is a valid pointer to struct device + unsafe { + bindings::__dynamic_dev_dbg( + &raw mut descriptor.inner, + dev.as_raw(), + c_str!("%pA").as_char_ptr(), + (&raw const args).cast::(), + ) + }; + } + + /// macro for dynamic debug equilant to the C `dev_dbg` macro + #[doc(hidden)] + #[macro_export] + macro_rules! dynamic_dev_dbg_unlikely { + ($dev:expr, $($f:tt)*) =3D> {{ + use $crate::c_str; + use $crate::str::CStr; + use $crate::print::dynamic_debug::{_ddebug, _Ddebug}; + + const MOD_NAME: &CStr =3D c_str!(module_path!()); + // right now rust does not have a function! macro so hard code= this to be + // the name of the macro that is printing + // TODO: + // replace this once either a function! macro exists + // or core::any::type_name becomes const + const FN_NAME: &CStr =3D c_str!("dev_dbg!"); + const FILE_NAME: &CStr =3D c_str!(file!()); + const MESSAGE: &CStr =3D c_str!(stringify!($($f)*)); + const LINE: u32 =3D line!(); + + #[link_section =3D "__dyndbg"] + static mut DEBUG_INFO: _Ddebug =3D + _Ddebug::new_unlikely(MOD_NAME, FN_NAME, FILE_NAME, MESSAG= E, LINE); + + // SAFETY: + // - this is reading from a `static mut` variable + // - key.dd_key_false is a valid static key + let should_print: bool =3D unsafe { + #[cfg(CONFIG_JUMP_LABEL)] + { + ::kernel::jump_label::static_branch_unlikely!( + DEBUG_INFO, + _Ddebug, + inner.key.dd_key_false + ) + } + #[cfg(not(CONFIG_JUMP_LABEL))] + { + // gets the _DPRINTK_FLAGS_PRINT bit + DEBUG_INFO.inner.flags() & 1 !=3D 0 + } + }; + + if should_print { + // SAFETY: `&mut DEBUG_INFO` is a valid reference to a sta= tic mut _Ddebug + unsafe { + $crate::device::dynamic_debug::dynamic_dev_dbg( + &mut DEBUG_INFO, + $dev, + format_args!($($f)*) + ); + } + } + }}; + } } --=20 2.49.0