From nobody Thu Oct 9 02:15:30 2025 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) (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 1223D2AD16; Fri, 20 Jun 2025 21:05:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453560; cv=none; b=ftxoBu6+w6H73scgO+Y1gvgYvzdkhZUl7vz9EuK6QoJWQlNWI03IqSp8fEyDvnLxaZhN8D2NzwMYDnbpUgKbIcQxqy/PcTveiCzn1GmNfO+r9NLIGeTcGcXXJh2k0nIqW8SD+B9SvzjT1uqT8x4bYXfm0OPxBpAH91aoA1BXUsw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453560; c=relaxed/simple; bh=YCDOwanVdSgN1lzvKjfliGSLNHhYEYiFIKqo06ezlV8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XjHjgj4c6wkQekyAU7CrfWaUkNYa0igVp/kmb8nlOuv38+2HROKHsA3QmZa0OAjdC+/3Yq32cif4vlSShSS6cuxS7pwvagkZcA3W1DRgxbzo6W6U7IdXIVmuq7/auB5KzbZYVPN9VdCpG1EC8SEMJ2Hn0rsZEtSpOl1r0xGvhFA= 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=a+UZfyXq; arc=none smtp.client-ip=209.85.161.43 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="a+UZfyXq" Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-60f24b478e3so1230455eaf.3; Fri, 20 Jun 2025 14:05:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750453558; x=1751058358; 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=2BZga7WiPAFo8RpaVIzRl+mWtDd+QCARoDdoxd3nuOI=; b=a+UZfyXq/iA8T0J9FdcEIreZIH5GKomgtT3Nc+I7//evMo0Fv+Uj/gbaiW86HKjEpl eh+upibX9zZ77OryUVSFE5Xxuk6gLkQ7JoP+u8tlejadrR3ogbF/CdWQ67abgiONMg62 4Zkq53McC83INbfvoxFW0QYdf67ItVUoJ/JV5nU892ZqIREyZtRCDFnhBD+6a9aF0uRd M0/GLZKy/LBOnd39cYa2W3lAfHZLa5c6Lixth4k5f7i0J9ylJ5HAq9FoYNcMRKSpEw9S Xn1LEajXhymqyoGBAvAJocs5RmQRYrE39SAmlIMGrdN24Gmk8B5hdRXSQHWg8RudKWnm EH1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750453558; x=1751058358; 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=2BZga7WiPAFo8RpaVIzRl+mWtDd+QCARoDdoxd3nuOI=; b=TlUAoVz5thXKlx5RwianiuBZTulbLFt1vVVrsdF18+vnfKFjfN9rx42Xl8+E8++fS8 vZWog+FlFAgFc4jGhBAgOQH8epeopcFqKCOUMEiI1RBq91mgchoLgHikvXZRvtgR0Pm7 CrMQa7Fs3kQDwLKiZlpeGuTaLvFzPD0whM/gXgJt6ywwdsPr5q9FvLLNznnEuXbKj7lK V9y9wJaRyaCHqPU1jz/t9PkS45UGfCiw20nh9dKdH81psmKUuGfvJX3LIzS+9wU6ux8v Sns4gGUOeugvYaX+SSI4y4xHW1l5yV/e7v8JutRHVZniqdGrdlCTF9ktWQ13swhmnxnb 5rtg== X-Forwarded-Encrypted: i=1; AJvYcCUW89czu2AoC5VeFOrvfC3zLS7R8M0AOzaOo/VbeLkEjBr8VM1klJV2urJRtq4qwEFdV87vzcOYLWGQqcc=@vger.kernel.org, AJvYcCVoJbm8QNyWn8NgxF1rEv8pa9T8IC+bj2T6JT15lhKLynQqsDB464psWGakWcilP27JH+ZQ5GyvGQGoch4oNP8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3NH84K52gKsdBxYttI2r6XpMl1pi9Z3hqs/8THKrk92LRryLJ Ver5QrB5nFEJLN3bDftzq/hXJrZDjKXDtgLfeyNLdRUrN4HC/PpXxWEP0GQbciKd X-Gm-Gg: ASbGncvulJJK5sYhmV0+Ulta/dOM7qd/kVEnTrrnutgfUPzyvKEfegWyy8w4Cl+xlth 3VOZbhXR/O4vZbg0Lh//V3LxmKNJ1gBiCeYSR2Xa8jPNC7TfvESFfFL5hjgkxHH5/XqxOkTm9BV ylZ9tYCxz7ZdOXLWY4sdoDEbBxd8jAZoQ29BfzN8M/IAZr7GpOlg4MgUy+TymMTQRv4Z6R5ZlZa Fp9j+sGf2kGi9eqkOKuHSqTOFW1ErmfTU/ylqhSigEB8fWZiPVnh38PvziZbOPbq+R1aHye5aXj k963WMNO6fRwHbtS1X7yvLbCENL89xLKYN6N+7yT1e3xssG4mjGMOKsm9AsUfFuFR5NIDhOyUGh EDkSClpSQp8I50j5Y401Rf7ssK8MnRkzYRf5sLGk5Ndsp X-Google-Smtp-Source: AGHT+IHolrI5TAkf8jC+feL37CqzPdcJpQcoasli9F/48fwZZEtLMyT2jUooj5A1exswxBUYDPx/YQ== X-Received: by 2002:a05:6870:6e89:b0:29e:3c90:148b with SMTP id 586e51a60fabf-2eeee57780dmr2974028fac.26.1750453558140; Fri, 20 Jun 2025 14:05:58 -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 46e09a7af769-73a90cb7875sm460596a34.56.2025.06.20.14.05.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jun 2025 14:05:57 -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 v2 1/4] rust: jump label: add support for nested arguments Date: Fri, 20 Jun 2025 16:05:30 -0500 Message-ID: <20250620210533.400889-2-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250620210533.400889-1-andrewjballance@gmail.com> References: <20250620210533.400889-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..002cc3bd73da 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).+ ) =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)+); 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)= .+, 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).+ , $branch:expr) =3D> {'my_la= bel: { $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).+), branch =3D const $crate::jump_label::bool_to_int($branch), ); =20 --=20 2.49.0 From nobody Thu Oct 9 02:15:30 2025 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.43]) (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 A860823909F; Fri, 20 Jun 2025 21:06:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453565; cv=none; b=Emf4KleOpVTKsd+FX1iFDqiyCM6WnnctjLtsnLw18o6AZ3Vz9TrsY7PWi6fO2sFoYEoW/yzJ+i5vQiMMInF5/eBiRgbaK1UUn6NS2Htoq0aOxtwYQYzyYg2QjA7Vni/yoakj6Koi+aGorI+S22nsIJYtvsVFJO5BHmdSDyeiMek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453565; c=relaxed/simple; bh=rNlWtuR8c7x31Q2t55QMyJvnaO5HTQtXqtdR8NIKeOQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=txNGAKVz6bBLFLxedufBUPF35nzFaEAO7GvjsGIBxBH4n/rG37Dd1iqNB5bbleqreK+Az+KWBw8PjFKJYBzCGFxiLWdy/0FM+hBl3RzV347Izx4sU9c+tFqMyg4tRuCDJ94sUptUuDRnyHXLuSd7Kgjs75V+RVzisYxOZZSLj6I= 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=RTE0WhzL; arc=none smtp.client-ip=209.85.160.43 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="RTE0WhzL" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-2e541e0b974so1538734fac.1; Fri, 20 Jun 2025 14:06:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750453563; x=1751058363; 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=n3E60FYUUWfAZf8u+QDWDPe5xu8ebDGQOiJLbxOxa58=; b=RTE0WhzLwpULheUv0X3b83n1Cr5zXpm7q/cnV3cHnok99YqRyNdfINwCXhirUF214d PiRpsCOk9sO9okX3C8CX5ABrKaU/uvlGNK+C+1JZMtyaFySPfNfZ7Ur/lfNnbsQXPEMZ q03KD0vbA9hWrcCqGkvVwQ3deUHDGUKEh/hLB1NJzrtRxtZEhBdV8UO53kxcCPDTUWW8 lSTfsUtZWmYA/6JOKU9hrIIXQeK83DO3O06eDb0SmTf/km0daZ87PtJ+jQe1SFncKvhd bC3x+ByDJ96djF4QSjISudio7Oyzo+MsRtOeK02TWnJX/BQ1NW+v2IWjYzYbOuAcv2O9 Bsyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750453563; x=1751058363; 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=n3E60FYUUWfAZf8u+QDWDPe5xu8ebDGQOiJLbxOxa58=; b=N7mkpU5uzJyJq/H6csBeZ/yLgKawO0G/6gZYG8T2JGeMVvu+iSSlrL/QXxB+EIkUFp 1VrY+RCTb26/cSo85l5zDaPtoU9+5S3Gm72nAyUZrgwE5/5jZ6e0irwuNXOBbPQ7is2R wqdEyFh14bU3p/ZQiAYzv0JMcPwKKGBs8OTQQjAzgpv6D3W1lv5GcgUE5ZWnQCk1/kdD td0GNqXxNG81/ALJS+hshyZkjUb0GxEe+IVuW1/iSPMwpd0QcIuLEu6wbDoqT4dTF5zR ketut+8yrgbot0trL3kbryWb9hrU3QBlikluiUz/ZmuMQFqz9lCL9VIhcZq4nM1X0nRM 0vxw== X-Forwarded-Encrypted: i=1; AJvYcCWScSllRKnYo6/dxlKPMCzyWOCTxnBt2JkQL3tWrZcETkhHoo0APz4nyswqths7wcTMQ5CqyITVCciUqsg=@vger.kernel.org, AJvYcCWdZ40RP0EYAR9HSwIuOxvxDFYFd5IPOYL4lZGYu0wLTPlbvR+9wD5Vv5PmM6vatPmhK4AsLfHRVXFJEqNeuKc=@vger.kernel.org X-Gm-Message-State: AOJu0Ywm1vAhfS3fNj3SSrX49x79fLhj5e6rt04hfF1WLxkatFKQh8O3 Q1vpTqm9pDbvo4exUug6UJqqyasQ6Yklhq2AwuFneKMTNnyBiZxvvjmq X-Gm-Gg: ASbGncszrmqaFKroGLUv16qA12CJXlxRfd5MoGNYjaRO8pZDbBA6wQntrxOg9nmc4WR SgXl0obBqX0OLzsZpHsuqdZTzM8lfmxOxgPr9QcCbJC4LSu/SB+15UOqaO9fRnoun1jlRbxM6e2 OGyVuDezubyQUmAxzgBB+Lw1JUFbN0VU6vqDyJ+ybepqXVXDijSRNybppFODG9C6B+iPPP/I6Q9 xsPDXVUAEE0D1eevo/kG9C/OLBeW60vyMIjNPRhbGpZxBjR9V5UXnpkwoOKYWC+0xjV6pgdb845 XbAUoOfMue6aGKpNde7JpTSRLoTEGcjm3wVkTzIjOb5whAxt0UHtjBil36fnP1swB/syOBB/iwk fJHn1qqsfkdGYBi0DbESi1rkLwfRrJpC17Vm8f8w25Cme X-Google-Smtp-Source: AGHT+IF2mz/ImmHs4vMwxeTZnzIQirDaYEcwVTXFvUoKupjSurhgt5RxRvjzFbqT17Ii8gXc/U1o6g== X-Received: by 2002:a05:6870:8196:b0:2c1:4090:9263 with SMTP id 586e51a60fabf-2eeee8698ecmr3701545fac.35.1750453562689; Fri, 20 Jun 2025 14:06:02 -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 46e09a7af769-73a90cb7875sm460596a34.56.2025.06.20.14.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jun 2025 14:06:02 -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 v2 2/4] rust: jump label: add STATIC_KEY_INIT_FALSE Date: Fri, 20 Jun 2025 16:05:31 -0500 Message-ID: <20250620210533.400889-3-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250620210533.400889-1-andrewjballance@gmail.com> References: <20250620210533.400889-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 a const STATIC_KEY_INIT_FALSE which should be used to init a static_key_false. Signed-off-by: Andrew Ballance --- rust/kernel/jump_label.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs index 002cc3bd73da..e30db2d06c76 100644 --- a/rust/kernel/jump_label.rs +++ b/rust/kernel/jump_label.rs @@ -72,3 +72,8 @@ macro_rules! arch_static_branch { pub const fn bool_to_int(b: bool) -> i32 { b as i32 } + +/// Default value for a `static_key_false`. +// SAFETY: a unlikely static key is always zeroed +#[allow(dead_code)] +pub(crate) const STATIC_KEY_INIT_FALSE: bindings::static_key_false =3D uns= afe { core::mem::zeroed() }; --=20 2.49.0 From nobody Thu Oct 9 02:15:30 2025 Received: from mail-ot1-f52.google.com (mail-ot1-f52.google.com [209.85.210.52]) (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 F1C4D217660; Fri, 20 Jun 2025 21:06:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453570; cv=none; b=GG1VktpMzKmMab18p32yd4G6cF1jIgoHdj02wEqyylBTKboVRdk8o5wVm7EpZK4aNzRux8WwABqzaVei+Kcsgb4GIe4kWIyd+d5ME0BjXF8o6Ty37fToKm68F+goMnMt6WEBhcF2Lt++IPj42lfRR/cJNAB6odfhB1IdpOLUQho= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453570; c=relaxed/simple; bh=U1RQZ+CRKL2Uqmb9M58IDIIuCkIKGsciGNMHZvmmLA8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MpaX93fQZlDeLBTJK6bXUTXtOB2Nl6hUojzviCQ416mBve2BLsOOLCgKG6uBXfT0+DRtZfV48eM5HldXMKk99clzfR/HFxPtcepoKFzJhUdb5m8Vdm8w4IP0S5SM9S55dO51GQLzKDlR7YI8fd5eg2ZrWg40KkB+ZzBfJAf835E= 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=SBFQgXvR; arc=none smtp.client-ip=209.85.210.52 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="SBFQgXvR" Received: by mail-ot1-f52.google.com with SMTP id 46e09a7af769-72c27166ab3so1502539a34.1; Fri, 20 Jun 2025 14:06:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750453568; x=1751058368; 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=Wpnzqi+8ws3Ksj9eVEvWIJefWpQS1Ry3ErpuelRUI10=; b=SBFQgXvRenvQrpt+v+aQ4K5fNshfmxt4HFobZu7MFiDm7PHsJVRLdYrFzPFtmMuGvp l8s2Axn1he+nfRcJJ9mq9TtkjNG6P4t421B9tAL2f2rGx4ncCjFNFk0kEwBETgos6GJo hX+1c9fb5HjVZMTSqeNBkq0B8ZMjw6uadE/pGH/NRZrVKt1IN/fL/rKXyOoBFc+tTibg aqIDx0vp81EUGY4w5loZtogoo3rVU8pvgDjSYY/FbiXc2gg4vkps4GJ5Igsu4jSC9v6z EwEOO6UdMtne6dM7KfOY60K3GurtHUF1V6zxke4ggfFc61zkPLRm3rx5CcrlEo8yy1aY qwjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750453568; x=1751058368; 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=Wpnzqi+8ws3Ksj9eVEvWIJefWpQS1Ry3ErpuelRUI10=; b=rRk+bNrwX6RQbSrvnv54g3HhF+CGY6duuGkK/njLal8N0IVYDcsB+3+TX4cq3OixY5 JoH70fwa0QGX8HS+I9I8q88ixiYsXlVNq5KRh0BKW7XXK408TFimAnV5XTwH3u49CkdZ 81Q3eo/KsMRazIKxQoSzkY8BX8V464S2lHyq7EoIaDlOmSXwFBKs4zy7TLtAuA6qxys2 SpJjj0ssIQD27SHR9kZJpyoILrVNQdsu1D0sc9b5H6VBIn+UyrF/N61JNUu0laIgs9Nb 1vqzF3k8d+hZNZeMEOhLOz8mVBNiTzywfAe6eyJ6Nl+8YxDLRpXBuAzrl4QSsnGJjFfq NJRA== X-Forwarded-Encrypted: i=1; AJvYcCW5uvY4ME9g4o13Gx9mFGx5USLXV/Dw5fBPZdWjFso4sB/H3umCVnYKL8F7LYa3vfSTTn/EHFE1J6BtrBAxwjE=@vger.kernel.org, AJvYcCXA9Autzv7NjPNbgGtHe3dk0FpB+S2mROuGgnH4NqcV/2ujtE+SbeHfs9gmb7kC+ig5/f9SGjVvr5bZ7ho=@vger.kernel.org X-Gm-Message-State: AOJu0Yzm7fyd2jfDNmAAEoMxo2O5eqE7Ya0mtiFPHEUT6qGgrlwJvtrP ZxAL7sq0SjcWury/iWBvvQoNfebLg2q+X3G0JpX4+r9Jhoe5xrlqot13 X-Gm-Gg: ASbGncuCh4dnF83uEeGBPuaxN6uktculsHle7esJQBST8JUeGiPPldmB8h2cEVRLb7O pA69yJHYA3MWBrQhfzQ6POr/ZrQjILamPhYlfjU3T9LCnaTp/cKbctJirAnKw4SqV4iEV+12Mib fTHbj/kw0gSXKxMtcaejL58eEB8wuBTzWx3hUB/BaHwMUpo7DnjHzPWs2ksv2XoK7yRFXwpj8Og 9CgAcd7/knL/UIxK1WaR7z3OWTdpjfH6iAPO4aCGlilUfZd1HBtrJ9TnVP6wenRpus558VWpO8e dHWForxIF3+Vy2DLSPawBCRyUniosB+MGalQRnINZ6hcbBzFxnvT4zHb5f0kOAa1RikRsLQaeOK ucI0W9Ax8edGzYlIDCd/Nc/SOErXtdR1P4g== X-Google-Smtp-Source: AGHT+IEJ1smyAld8LZgMLy9CnbsWb4b2RQQkDlh9UGxWBabc9SWnsSxsvlr1DaOaDwMsDQjnt6Gdtw== X-Received: by 2002:a05:6830:926:b0:727:3f3e:53bb with SMTP id 46e09a7af769-73a91da16f6mr3560655a34.24.1750453567761; Fri, 20 Jun 2025 14:06:07 -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 46e09a7af769-73a90cb7875sm460596a34.56.2025.06.20.14.06.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jun 2025 14:06:07 -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 v2 3/4] rust: print: add support for dynamic debug to pr_debug! Date: Fri, 20 Jun 2025 16:05:32 -0500 Message-ID: <20250620210533.400889-4-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250620210533.400889-1-andrewjballance@gmail.com> References: <20250620210533.400889-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 | 171 +++++++++++++++++++++++++++++++- 2 files changed, 168 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..36573e56a176 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,154 @@ 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, + key: kernel::bindings::_ddebug__bindgen_ty_1 { + dd_key_false: kernel::jump_label::STATIC_KEY_I= NIT_FALSE, + }, + }, + } + } + + #[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 pointer 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 equivalent 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!(); + + #[used] + #[unsafe(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 + // use pointers to avoid undefined behavior + let ptr: *const _ddebug =3D &raw const DEBUG_INFO.inne= r; + (*ptr).flags() & 1 !=3D 0 + } + }; + + if should_print { + // SAFETY: `&raw mut DEBUG_INFO` is a valid pointer to a s= tatic mut _Ddebug + unsafe { + $crate::print::dynamic_debug::dynamic_pr_debug( + &raw mut DEBUG_INFO, + format_args!($($f)*) + ); + } + } + }}; + } +} --=20 2.49.0 From nobody Thu Oct 9 02:15:30 2025 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.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 DB38422E3FA; Fri, 20 Jun 2025 21:06:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453575; cv=none; b=WDLkYQPGLhRh8S1PXA9dGIDp1Q+QtndeP+1rce12M/I4QNl21i1XE1hb8QmvZVRtk4Ov2EyKzPQsTufY+Ke9cjU2BBW+sS/9HQV9Rp/H0LXSvNOKlBh7XLE+TyOJmoun07mOnptToaCqFQ0NKnsnfHjPpfVSzFNNXUrT0TlA2ew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750453575; c=relaxed/simple; bh=Qf9aWWDk1rgelGy/nw2y8YtBV+XFwSFwvNGpXSA9zpI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uh3Fv5cm6QW1o53sd53qV3VcJiBtLssZmB+JsNg6MwTytgIdO6amIp97IywWuj+Hp/LcTvwWLUns/Gg5eclbC48ePBfDIwnfo2jyl6ccaFrKy4o59ofyxciON9mIoH9wXuT2schOUuk9GowQBRT9zm349XodVyK8lGABOsAVZLQ= 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=Y7FZZqqP; arc=none smtp.client-ip=209.85.210.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="Y7FZZqqP" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-72bc3987a05so1314716a34.1; Fri, 20 Jun 2025 14:06:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750453573; x=1751058373; 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=S/HEUkl30GQM2b2jY07fS7ORbU/KCeaIh9AjjPxC8qo=; b=Y7FZZqqPu2/b3wPDEzr3LHbyQLOPKilO2rCa7R6ORssaHzAU5WZBhYd9bmaXZecrbw QFlmU9nOBKp+gLdriHEcOzoYQXivDxB4M33oPtgzlZzxWcGSDBQxJlgIO7EQkB08g7aM NjAtFOuMjxl02ZNDJ2vu/pHbkaa6/0W0/B+x5gWrtWpgvGnn0cbHr7tboojItHB4vhuo JY8qU6gkrlxyhQEJFhXd5FWqHQYj5wNtBm9zmoABJ0QOJ8Gj6Wng0yPojuq0MN1sAes8 /R12bUzZDm3WvgG+9+LoHEMr9x+8L2pMCCt39ofR6cwz5eWYOjvyTURc9yZfW7NY/6Gt tHIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750453573; x=1751058373; 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=S/HEUkl30GQM2b2jY07fS7ORbU/KCeaIh9AjjPxC8qo=; b=BZx3XyhDeWyuGYI+MSpddjsZzt2Fe5m6CbopWVMu5U3MMe142JCNKp6C1Zs75fEsrv We6FS/ni/vsOKa5sb4QL+XqQDYW5JFMu3mQiu9i9S8KMW5EawLP95pu4Iau1qih+43Zp 6mJxA0z/d7z/PhpY6Qrly//QMv71NKa84lhwqQD7vFxhAy/gA21NkhxI2XLjYE+0GIh9 HpkN+8kRMujOFMPXdO3XY938Qj4cJJCTTPTxKLzWg60uHkuaM7UYYsmUcELHwf0/AMGm JXUDDehDm1AlVsX+xAAAT/WUqDnx9rd/XUhlDXfDTiE8zLfhjZyJUhKek9OgdHFsnEId 06Kw== X-Forwarded-Encrypted: i=1; AJvYcCU1gB3CQ9FsuX4+Dx6uSt0spyVtvFe+qkTmEFGOPwh2pFysxiAEaOtKcpPMTdIDj/nVvXjNECFe1v7DCaA=@vger.kernel.org, AJvYcCUuyMNmViU8pN1GaQch021XqLeT5FZajgBfkKZSqx7DUM9Cf36BFKbayBgSFCYP1U3aPC7kV+2UB/cJTdldO7s=@vger.kernel.org X-Gm-Message-State: AOJu0YwGSBAmkX7pmAaKlM0MVB9pOGzH3Zqgoco7ZXP4WbQXgTRYzM8+ yDtPECfRKYvMlvr89/H0R9w/0ZSofGBfeeRsADFm0n37zq+B/IRYYcRw X-Gm-Gg: ASbGncuw93zYtpRRprSsxIdcTSNqITvYWY4sKlXME8P0LaujvYQ6dPtGR/Q8g9GY985 ADZThuT8GpfpOhAJIBsBRgLGNTnrpkg4zkHTJSEM/L0hDTpsrOS4lmFwMwEzhdRDW4aHUqmTPB9 48daReUqcv1mdL7QETswDIt3y3qXK8t5+FAMfPBx3cg5NFi6vYRP/+5jpvqAy8K8cLv0oSUIlTP hku7eK5i/scI+g+CKlwrkS2s/r4dyhtlN6I0kagjIoc3FwLjrv0pX2gNOyMwbqogzZ9prXZvBXB gTnW3xDE9AohsKqoMpusU2c6lJbdE5PTnVFY6xWGrHZDXXmUu5JDmVcqNb/+AIvHJwbhdLBdAUP TmbrTI/Mx63iG7vtvH6PLw/6VxltnTtwJYA== X-Google-Smtp-Source: AGHT+IHabmOuHzbM+hdIHSQO0XLdjw3L4ZxnmcTGpMjCK7p5/Y7slbedP3jLoLU7meuMmN2yz7sGpw== X-Received: by 2002:a05:6830:4d87:b0:73a:8a44:6181 with SMTP id 46e09a7af769-73a9314a2c8mr2840207a34.13.1750453572772; Fri, 20 Jun 2025 14:06:12 -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 46e09a7af769-73a90cb7875sm460596a34.56.2025.06.20.14.06.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jun 2025 14:06:12 -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 v2 4/4] rust: device: add support for dynamic debug to dev_dbg! Date: Fri, 20 Jun 2025 16:05:33 -0500 Message-ID: <20250620210533.400889-5-andrewjballance@gmail.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250620210533.400889-1-andrewjballance@gmail.com> References: <20250620210533.400889-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 | 105 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/rust/kernel/device.rs b/rust/kernel/device.rs index dea06b79ecb5..829b7ca7bb7b 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,103 @@ 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 pointer 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 equivalent 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!(); + + #[used] + #[unsafe(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 + // use pointers to avoid undefined behavior + let ptr: *const _ddebug =3D &raw const DEBUG_INFO.inne= r; + (*ptr).flags() & 1 !=3D 0 + } + }; + + if should_print { + // SAFETY: `&mut DEBUG_INFO` is a valid pointer to a stati= c mut _Ddebug + unsafe { + $crate::device::dynamic_debug::dynamic_dev_dbg( + &raw mut DEBUG_INFO, + $dev, + format_args!($($f)*) + ); + } + } + }}; + } } --=20 2.49.0