From nobody Sat Feb 7 17:45:37 2026 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 CACBF171675 for ; Fri, 21 Jun 2024 10:35:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966137; cv=none; b=BAPb02QdVv4BHFnro73IkSOO9HccNBJF1jIfzwHNW8AFFBd2GFNoZKiSc7UP/r2vcH3EWdWsNrXPlxNlCEyKSuuOGWyaKbFRoy9wnh8WQLsgeuldMecr/80GuyF7vIeFkouz14jMtyERkz6pUyCgU0xrAiODjJb5Vma3FqVzMeY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966137; c=relaxed/simple; bh=6wWzVlFR1VHADGH+6BePWydBpBp6gryq9IdChcDcKU8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F0vBG5dTtsG7uEBbcA8r7zTaIQnR1pGR6GzPXxwwtP8JeQVR7iMoJVrYU5k3VXYjpW+zhzMP62ArudCFzsLCjOBZvYvvDCEqGGOtgtl7/4l16gvB9zQkyGvQiTcIMMpFH1EAg8XCaBv7d4+66SROL4V+QLzce0DYHihQHr0JeNw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=WOqjIGx9; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WOqjIGx9" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-627f43bec13so34418657b3.0 for ; Fri, 21 Jun 2024 03:35:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718966135; x=1719570935; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=CrOCucYoXI5A0zFoBN/LEOcGTzj1eDJ5JNGNyCICG4w=; b=WOqjIGx9AgPHEbgJiOX2VDwLi0vtz64YFROeDTh3nu3M84ApJKRD8giUmtjIbh2pDD zR94ccUdKpwK+iWxrilp3edZm4XwZtDTDo77Xp0Y7662TfQJimRq5iBVWEjB0egFvNSl VcziKGD/JOzz+LJcT29ZZWhZ98MncJEjTCfx6xH/OKjVTxIpLpH8PcUfy8A/e573xtEk ap974m1E/R85oqToANRP5GGDXxpIKvTM49xRjaCiyATE6idBOS8c7v2EAD3xgLhKehJ0 Yp9Rv9IN5yCfprL0q2vr6Xn0FWpz6IPz5NmonykArckO/2Edfp4FgxVfY1LN102EaUkw tCgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718966135; x=1719570935; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=CrOCucYoXI5A0zFoBN/LEOcGTzj1eDJ5JNGNyCICG4w=; b=jvQq1o4orKczcO0fNalMTAaNDoLSXlP1jKXjOrn63i5HDUE+O5sGoh4WWq7pUxENZk Beu7GA3gV0U3vaDWjNE9weedTFnf/ksrx8k8hAHv+5hdlg7PeZWk7KjUhL2gFd87oG5y 7RLKNrUOoz4VPBQCMPiHrKOXECUSjhPhfbUxvJcPKAmFWtKq7tu3/tQVIxz4MvZEr0Xd 1KRFVbDqlN77qbl2569gbngjj77MBwp7Q9Z3Hz1sKl9psUatq8iXtTfoD2fikTYUfZI6 QQyvcQbPkU7TPYy8oufUIKANX2+9gK1/UzOf5/KAAsTbLK2vwBBlCIO41ForN/YBBfsb W2cQ== X-Forwarded-Encrypted: i=1; AJvYcCVDkUTq2Xt0MwxlCt9Bh2Pvwt4L5tNbJ1U9BZf8NBF2BnMvSg6zZFSsp37+dHCAJQchdgEb3Eq1/0TNws8YG7/s/EO08ALA0Y4v8Ump X-Gm-Message-State: AOJu0YyePmHAkALEaE29F60xWx0gCBJflihrQ1kTVjKNGYhtGZzcWFnU gCcUplPq8FJJ0qRtzhfoxc4T0M4srJjeYuDwl29O3Fi3/V9o9kiTBFoWaH8wt+PJYSaD4uI/eru qJAq+UA9JHEKB9A== X-Google-Smtp-Source: AGHT+IGVPYWO/IU1ZjZdU5tBe8plUiU3EKikmKQ+F6kZ+2U8VVqNPl/NGi/EZ0pSAI0i9VOJqPEAwp8eflEuhWE= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6902:1003:b0:e02:bd4f:def2 with SMTP id 3f1490d57ef6-e02be20b2ccmr1838087276.11.1718966134957; Fri, 21 Jun 2024 03:35:34 -0700 (PDT) Date: Fri, 21 Jun 2024 10:35:26 +0000 In-Reply-To: <20240621-tracepoint-v3-0-9e44eeea2b85@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240621-tracepoint-v3-0-9e44eeea2b85@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5918; i=aliceryhl@google.com; h=from:subject:message-id; bh=6wWzVlFR1VHADGH+6BePWydBpBp6gryq9IdChcDcKU8=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmdVdwWHR38Id2Y1qI7zKbIntmDpdG2XqgVF2X9 Pf7bNMKSfaJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZnVXcAAKCRAEWL7uWMY5 Rm0kEAC0vvj6AE4i4zy0e5goiDGQ1miDq/5MGdB+jxfbD9hho/GGNUobR2VV0zVUu9wxLRmoA/4 NKx7emh8PfAA5wI3i0i3SN29Z3bIEl5/TnoAO8j848tBnzNJDMAxTaSwgEntSuk1eZC2VJTFobd j4P1EGP06EhipWKPROT0ioujoB8oHmo47fC/NLSYdd8s8f3ToWYHGyaMdZbu54kaqwTcUQpWOR7 j1m7sS9o4glt/wpz/7lvmCOlcs22IIDYR8VzP8eH0MgbIYWpnkY27pFGfDoms3Jf4D/gzK5q1kW J5R5maMNT1VX9uGbg6z6lR/CbYgMjhElPospfQmamRqUJN3TkfZSGExaswS0syMtfLHKvoC2xBr pEzEcqvcjArWvSapahCk5dTdtopIg9hEEimEsPI3+x1nOsv1AOVzkJL/94964U+4sWTOMmzPxMH 9JLUkoLxl2tTx3uPlZ9VNSFNMIDC98FgIlJOFywZKn0UODwVpL4ZoKwkrPA7kTecKAy5Bz8qn2h bzMpljBeRNhRDLHdEknLlpsyLy0MuLFBXtpMWns5FkGpb2oMeaTaEcrb0Vb0Sewv9uhYSV/sPpr HEvRKk6j17ZcDCq1n/IXwxYeNKxptYFJej0Zw0Xoruoi/oIcuk5XW+0rLdOLP+JvocuA2GBrH/7 zNeFgSDFbaEWQiQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240621-tracepoint-v3-1-9e44eeea2b85@google.com> Subject: [PATCH v3 1/2] rust: add static_key_false From: Alice Ryhl To: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: linux-trace-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add just enough support for static key so that we can use it from tracepoints. Tracepoints rely on `static_key_false` even though it is deprecated, so we add the same functionality to Rust. It is not possible to use the existing C implementation of arch_static_branch because it passes the argument `key` to inline assembly as an 'i' parameter, so any attempt to add a C helper for this function will fail to compile because the value of `key` must be known at compile-time. Signed-off-by: Alice Ryhl Acked-by: Boqun Feng --- rust/kernel/lib.rs | 1 + rust/kernel/static_key.rs | 143 ++++++++++++++++++++++++++++++++++++++++++= ++++ scripts/Makefile.build | 2 +- 3 files changed, 145 insertions(+), 1 deletion(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index fbd91a48ff8b..a0be9544996d 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -38,6 +38,7 @@ pub mod prelude; pub mod print; mod static_assert; +pub mod static_key; #[doc(hidden)] pub mod std_vendor; pub mod str; diff --git a/rust/kernel/static_key.rs b/rust/kernel/static_key.rs new file mode 100644 index 000000000000..9c844fe3e3a3 --- /dev/null +++ b/rust/kernel/static_key.rs @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for static keys. + +use crate::bindings::*; + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "x86_64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + 1: .byte 0x0f,0x1f,0x44,0x00,0x00 + + .pushsection __jump_table, "aw" + .balign 8 + .long 1b - . + .long {0} - . + .quad {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "aarch64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + 1: nop + + .pushsection __jump_table, "aw" + .align 3 + .long 1b - ., {0} - . + .quad {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "loongarch64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + 1: nop + + .pushsection __jump_table, "aw" + .align 3 + .long 1b - ., {0} - . + .quad {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "riscv64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + .align 2 + .option push + .option norelax + .option norvc + 1: nop + .option pop + .pushsection __jump_table, "aw" + .align 3 + .long 1b - ., {0} - . + .dword {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +/// Branch based on a static key. +/// +/// Takes three arguments: +/// +/// * `key` - the path to the static variable containing the `static_key`. +/// * `keytyp` - the type of `key`. +/// * `field` - the name of the field of `key` that contains the `static_k= ey`. +#[macro_export] +macro_rules! static_key_false { + // Forward to the real implementation. Separated like this so that we = don't have to duplicate + // the documentation. + ($key:path, $keytyp:ty, $field:ident) =3D> {{ + // Assert that `$key` has type `$keytyp` and that `$key.$field` ha= s type `static_key`. + // + // SAFETY: We know that `$key` is a static because otherwise the i= nline assembly will not + // compile. The raw pointers created in this block are in-bounds o= f `$key`. + static _TY_ASSERT: () =3D unsafe { + let key: *const $keytyp =3D ::core::ptr::addr_of!($key); + let _: *const $crate::bindings::static_key =3D ::core::ptr::ad= dr_of!((*key).$field); + }; + + $crate::_static_key_false! { $key, $keytyp, $field } + }}; +} + +pub use static_key_false; diff --git a/scripts/Makefile.build b/scripts/Makefile.build index efacca63c897..60197c1c063f 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -263,7 +263,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE # Compile Rust sources (.rs) # ------------------------------------------------------------------------= --- =20 -rust_allowed_features :=3D new_uninit +rust_allowed_features :=3D asm_const,asm_goto,new_uninit =20 # `--out-dir` is required to avoid temporaries being created by `rustc` in= the # current working directory, which may be not accessible in the out-of-tree --=20 2.45.2.741.gdbec12cfda-goog From nobody Sat Feb 7 17:45:37 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 A4C8617276E for ; Fri, 21 Jun 2024 10:35:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966141; cv=none; b=DW5c39EwdgpdLrfMkMDL488xWfuhdq9iLg3IZ+lfP8E3x0mTF/DxChRLrPEm4Z0/EmqAeExy4uKaS75YUbkoKSozEm59I9RbMyW7GJ/9Dcu0hvtYLEU2FPO8d90TTxn24JkUPDSkVq1LgXyv0QaiVmAA8knpaXJ7zKez4aCZD68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718966141; c=relaxed/simple; bh=IyJYxPljI7iFl5u4Sih+kt1htjoHEPPM9Rn03F/wALY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GfX5PllMdW+V4AoIQ0+jARmRF5xpZWFeY+51hODQycnuVa8zbJ1FD09G11ppyL5BUzrEgEIRIfpXWJgl+Nf0FEU5byFt7dttz8TVSH0B010xYn9JicDubE4KWjfZSZg5+J4WIAa3aPA44scN3vZLEovS2bQ/UfYifsUJ4FBZ6Bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=T6LNg8sY; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="T6LNg8sY" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-62f8a1b2969so34447087b3.3 for ; Fri, 21 Jun 2024 03:35:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718966138; x=1719570938; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GPfiWV/WIDacLf3rDcewrodB4jCLD8RHBJ9128vNrBo=; b=T6LNg8sYXT0B6ZfmVsDAslYELbynxDq2/npLkMLRUmAbrEt6oSYMVW/gzir4seetSg MIi7+RKgaR+xCtyAN3U1B8B34et0Qcj+/D/piq+8ZsrVVmU3yZmYlD3p4TWtIUYxd9Zx +bJs3oTBUGwla9LWTbPKRrzqfEDqrGrso5txnDDEnJQw5NkVdJjNnOEvxFQGUTMl1kp5 YvfBDzZXyJD/sWT2a1gbi3cTaJasI1/3+2PFokqH4WujndANvHaw6UcXNsOm4V0ks8dB H2bMwLO72lgM09yuq0s5jQaJZ5R/gRokHyXv2ULNgDg9VKNNTrQ2aYZXOAxOqIev2NQI 117A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718966138; x=1719570938; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GPfiWV/WIDacLf3rDcewrodB4jCLD8RHBJ9128vNrBo=; b=MvGZsMR/NLYuHAc9ZFNIg+3h/Klxtv/Rrb589wpJAecwJ1cveGHdNAmO4Kv1/JehdA fDX+D2TEN5p2VAZZpgGtU6TZs+epWv+PYR6wcbkvX3/PtLp0tgvzrvP7LtIn0K4WTUZJ WA5pCccpSppbXA/OkpaRtd7v11dDCFGCkAPK5A/CfLHoGvKZj1usCxFJqSu9tVbM89wS gDywPx4kTT71XFwDjTqutOPBYlO/+yZq5CP/Ztq3TJxmb0ScBwQUIco0LoPksVY2douL n5gIG2ei4BkViESmWr7by0Jd634aArfBzo5Cu8CxG96fFh7n0r9SjelgbfIqi3trgp0m 17yA== X-Forwarded-Encrypted: i=1; AJvYcCWXPcQj3cjt1k/j+iEr6h1jXHmiDgsqLwOBMK9uHN2ArPhIcGRJzuJ7eICdsHIbUtdR4xV4jmQ4VrEIeoh8fXfRiyPQ5LS5CYttlZiv X-Gm-Message-State: AOJu0YxtXizucuMIcZA56i0X6ezrAbRFoleHeZG3Ck2VUHSfCGNa6Yu+ aSgNO+IVoyAy3GWv86mqSQsTq03rjAFisgYAl/E5ouaSF1u8z+ORqaVRGzEGMZVlwEVu5LTN2pp asS4/VnuKYYL0xQ== X-Google-Smtp-Source: AGHT+IEIDTsMkz3eJ+kNm6sUuA+LJREhP07E+V4i0dphDRkHWr5SOOvLH7WwR2zXCO6wCAOIFvMK2RsUDDwSB9c= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:338a:b0:61b:ebab:ce9b with SMTP id 00721157ae682-63a8e1d8effmr12105287b3.3.1718966137658; Fri, 21 Jun 2024 03:35:37 -0700 (PDT) Date: Fri, 21 Jun 2024 10:35:27 +0000 In-Reply-To: <20240621-tracepoint-v3-0-9e44eeea2b85@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240621-tracepoint-v3-0-9e44eeea2b85@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5679; i=aliceryhl@google.com; h=from:subject:message-id; bh=IyJYxPljI7iFl5u4Sih+kt1htjoHEPPM9Rn03F/wALY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmdVdwr7eAzVR1TABESJh6WnzNzHO+UsGRmHWB7 zZQ2rdVQmOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZnVXcAAKCRAEWL7uWMY5 RktPEACMeNTuMAZDjkYKloCPhZmi/TrriD3pmJ316f5tfvrNF25GNmXjTU+QxgELUvMydx9PjHa t099GbcRYhqhUB8MGL5mR54izLApuwlSax7utmXsZBSWwzFgODS6RB1E5CmmVaVa/43CIeWl/9F AAZLhJFvj/s1OYYL1o2e1C1ie6ynkT9l+Pz/Pxx+zj9eDrlkjNILOtIYgDVgPnXjm2fAwATTXdo 5x12jjPdngbCzcDzdqtBypT4onSGG5U0d3R4PCuDmk+7KOiVr9gnp5bDCgo4rQ0IPdD/5iYLTrx CECGf1VGPx2ixM8dLNbKZ/U1lm9pc7rtQXwjHIKNCJo+X9lJBWf7oCa7sRdt9sGzHsE/odgyMEq q/zDj6peeHzYeGTBOZfCDfH8vp2kypI45V9Gx3agh1mc5g1df8bvOM/YDYt5+AW4ThZ3ycew9bf 6X81xruz0jYrfBQtnxu3z3lnAAjRJ0Frf3Yc0ysqZeIuvY0fPhoeAJoTUwwRqR8gxYftkw0fCP4 f5860Ud/7QQ4OZZRLJmTMG70HNWpqm4gOjCYT4jYlF+91EGJBJ08bbUlBz7uKAkq/+85UdT68tS AcoT9PdzOJ9OPTmJxOWypTYt3rU7EJR+1a+BxCD4oEJCC5DV61M2meTCfg9uv7CyFyNdzgE+A/m GGK2ylrDOUL+AjA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240621-tracepoint-v3-2-9e44eeea2b85@google.com> Subject: [PATCH v3 2/2] rust: add tracepoint support From: Alice Ryhl To: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: linux-trace-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Make it possible to have Rust code call into tracepoints defined by C code. It is still required that the tracepoint is declared in a C header, and that this header is included in the input to bindgen. Signed-off-by: Alice Ryhl --- include/linux/tracepoint.h | 18 +++++++++++++++- include/trace/define_trace.h | 7 ++++++ rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 1 + rust/kernel/tracepoint.rs | 47 +++++++++++++++++++++++++++++++++++++= ++++ 5 files changed, 73 insertions(+), 1 deletion(-) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 689b6d71590e..d82af4d77c9f 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -238,6 +238,20 @@ static inline struct tracepoint *tracepoint_ptr_deref(= tracepoint_ptr_t *p) #define __DECLARE_TRACE_RCU(name, proto, args, cond) #endif =20 +/* + * Declare an exported function that Rust code can call to trigger this + * tracepoint. This function does not include the static branch; that is d= one + * in Rust to avoid a function call when the tracepoint is disabled. + */ +#define DEFINE_RUST_DO_TRACE(name, proto, args) +#define DEFINE_RUST_DO_TRACE_REAL(name, proto, args) \ + notrace void rust_do_trace_##name(proto) \ + { \ + __DO_TRACE(name, \ + TP_ARGS(args), \ + cpu_online(raw_smp_processor_id()), 0); \ + } + /* * Make sure the alignment of the structure in the __tracepoints section w= ill * not add unwanted padding between the beginning of the section and the @@ -253,6 +267,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(t= racepoint_ptr_t *p) extern int __traceiter_##name(data_proto); \ DECLARE_STATIC_CALL(tp_func_##name, __traceiter_##name); \ extern struct tracepoint __tracepoint_##name; \ + extern void rust_do_trace_##name(proto); \ static inline void trace_##name(proto) \ { \ if (static_key_false(&__tracepoint_##name.key)) \ @@ -337,7 +352,8 @@ static inline struct tracepoint *tracepoint_ptr_deref(t= racepoint_ptr_t *p) void __probestub_##_name(void *__data, proto) \ { \ } \ - DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name); + DEFINE_STATIC_CALL(tp_func_##_name, __traceiter_##_name); \ + DEFINE_RUST_DO_TRACE(_name, TP_PROTO(proto), TP_ARGS(args)) =20 #define DEFINE_TRACE(name, proto, args) \ DEFINE_TRACE_FN(name, NULL, NULL, PARAMS(proto), PARAMS(args)); diff --git a/include/trace/define_trace.h b/include/trace/define_trace.h index 00723935dcc7..b47cc036acba 100644 --- a/include/trace/define_trace.h +++ b/include/trace/define_trace.h @@ -72,6 +72,13 @@ #define DECLARE_TRACE(name, proto, args) \ DEFINE_TRACE(name, PARAMS(proto), PARAMS(args)) =20 +/* If requested, create helpers for calling these tracepoints from Rust. */ +#ifdef CREATE_RUST_TRACE_POINTS +#undef DEFINE_RUST_DO_TRACE +#define DEFINE_RUST_DO_TRACE(name, proto, args) \ + DEFINE_RUST_DO_TRACE_REAL(name, PARAMS(proto), PARAMS(args)) +#endif + #undef TRACE_INCLUDE #undef __TRACE_INCLUDE =20 diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index ddb5644d4fd9..d442f9ccfc2c 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include =20 diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index a0be9544996d..96f8f11c51f2 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -45,6 +45,7 @@ pub mod sync; pub mod task; pub mod time; +pub mod tracepoint; pub mod types; pub mod workqueue; =20 diff --git a/rust/kernel/tracepoint.rs b/rust/kernel/tracepoint.rs new file mode 100644 index 000000000000..1005f09e0330 --- /dev/null +++ b/rust/kernel/tracepoint.rs @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for tracepoints. + +/// Declare the Rust entry point for a tracepoint. +#[macro_export] +macro_rules! declare_trace { + ($($(#[$attr:meta])* $pub:vis fn $name:ident($($argname:ident : $argty= p:ty),* $(,)?);)*) =3D> {$( + $( #[$attr] )* + #[inline(always)] + $pub unsafe fn $name($($argname : $argtyp),*) { + #[cfg(CONFIG_TRACEPOINTS)] + { + use $crate::bindings::*; + + // SAFETY: It's always okay to query the static key for a = tracepoint. + let should_trace =3D unsafe { + $crate::macros::paste! { + $crate::static_key::static_key_false!( + [< __tracepoint_ $name >], + $crate::bindings::tracepoint, + key + ) + } + }; + + if should_trace { + $crate::macros::paste! { + // SAFETY: The caller guarantees that it is okay t= o call this tracepoint. + unsafe { [< rust_do_trace_ $name >]($($argname),*)= }; + } + } + } + + #[cfg(not(CONFIG_TRACEPOINTS))] + { + // If tracepoints are disabled, insert a trivial use of ea= ch argument + // to avoid unused argument warnings. + $( let _unused =3D $argname; )* + } + } + )*} +} + +pub use declare_trace; --=20 2.45.2.741.gdbec12cfda-goog