From patchwork Mon Jun 10 14:01:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13692099 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.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 D3C248563F for ; Mon, 10 Jun 2024 14:01:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028111; cv=none; b=Fz2xheMimhoQs8KuVx8XeAC6yFuGumgwhndSfDD4d6YAeEPVHhtDWDkOUrhoAUR+6d+RFCb5X81iunF8BD8zMcY+UqtyXIkMRiKhSCuBPt9DVvv+cQjtaVU2osqAsCckYGJ8odthpOEV9quUfZCsJZJM5zn7jVfWkwcNIZAO10A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028111; c=relaxed/simple; bh=9vYGSS2BmEd2xDDj2+LOFjdqN6wry32Wd02DytPyAGM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MkphSg01Noy+j7ygBwm4u8YlisXdRq9B7FutPBEpOBtYsJFtjweZfEhx5ekoD6c9AocIr/QKwjwVgKyUN0iS8RVGAre59U0iRtFxm8vSZYu+yzeywkHeeoj6qcX9pN87L2TAxChrIFMrzdtIyiS074XK9lCEhQlb0XcinWUpsmw= 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=a+6kbwzk; arc=none smtp.client-ip=209.85.208.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="a+6kbwzk" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2eaafba0579so24332491fa.1 for ; Mon, 10 Jun 2024 07:01:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718028108; x=1718632908; 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=gqi0Ch+WHXCymy1FXRv5YloHXZpb5Bjpuk0IhhxS8JI=; b=a+6kbwzkQjAl+c3pZcGg+sgjnG6PZ15AUHbBgphw8LXwaMNAr8f8yyYUAk7KtUdeab +nKZfMPyehXmWmVn8xvO76tm+k3zXWNWAsg3Q+1/MVEBVht5wd+A2I0WCZn+MHuoyot/ RdivqAO+LRBhiU6wNUXEmag5XjUD1T7t+LlulmR+TtkJ2+wClMb4H+uYI2bvNTBkwdJ+ aHnpdtZvwcuoRSldSZcJ5gCMSnwfwRnA13Ltoazu2HBb0kkyQd5MlrwxDDelaQAWvdPZ iFVVnM6dhD81TUwRIa2dX4dQeLeHJouHdkDSF7Ir/e1eETtLM9OfjQyiRqT984y0+ohg G6Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718028108; x=1718632908; 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=gqi0Ch+WHXCymy1FXRv5YloHXZpb5Bjpuk0IhhxS8JI=; b=FvwBuG6aiq4OeXZmk98wxDYqgbX3Rw4fRcnJR1o9CedT/GjK6xwO3ZjTuHVDanqJms ytY7tQc4ZFSYQ5ktaIJJuw2+wy5ZH9v3XJpp5r3y+v3Bi5qGR6oMUrIeYS/HkbtgGQdf liaxZmU1mOroTXX7LIRXKbOJgi0ZD4tld51a5sFJZ2XSlJ5iHXM5hNeIZg/yRm0f1uPA 15eDwIellyMP70Ebi1WZ8EJLH2F5TqyPvyCtZMwzPO38CwiQYglQfvM+uQdG1vRypHD/ of/DHioWhytdz8r7VCVZatDXpEI3ABDAgo+XWnp2EOq8G9t1Ql3JIP3Suf+lQQHX+hZr Oetw== X-Gm-Message-State: AOJu0Ywf0QFaJnV2+dldflB+rP0mip9qlxth7SxNZZvGw7lKrVFVw/Lx /OgVqsV1hbpS1p+jWUmPsDCMb6J2IvNd24yQ5DryHLbfwfOLZ+14gl6xxVO206ztnihulRiv6cX s19ylO1qC4BMzjQ== X-Google-Smtp-Source: AGHT+IHmESnzCQ1ZJ5vYbTFugTYk4eNBgny8iUT+z4SkkUunHjrOJQIWIMsPkpYFThPLRJmh2nIwyqCWIX2G2Ok= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:2c0d:b0:52c:8eff:ecf6 with SMTP id 2adb3069b0e04-52c8effef73mr1599e87.5.1718028108013; Mon, 10 Jun 2024 07:01:48 -0700 (PDT) Date: Mon, 10 Jun 2024 14:01:04 +0000 In-Reply-To: <20240610-tracepoint-v2-0-faebad81b355@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240610-tracepoint-v2-0-faebad81b355@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4510; i=aliceryhl@google.com; h=from:subject:message-id; bh=9vYGSS2BmEd2xDDj2+LOFjdqN6wry32Wd02DytPyAGM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmZwdEPykOiap15c/fLpyNtrxN5Tig8vcli+THt 9+Kjoo82q2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZmcHRAAKCRAEWL7uWMY5 RlZUEACvE9nXD8ucFeS4gyrBRW8aiQYMALTJ1gxduY1c5sqM8XqcB25c0eZu5m4HDkU9MMyAtfX BPiuO8DHV2wO93rAEKZsGjPU/ocSKwt/LD1YmSEp3xfklG1nT6q9DOzn74sfaRH/+ILuhU6Tg6s BJ31tjZqbOAo2R0n+jMvA5tNFhf4j98gye+pEYcVI+aEzYyVampkt0KzHI3wEYB53cKM6D0QEr3 Nq3xp6IwHcX1bQ05dXpT9MXn48BEmb9yHoNEIjiKahN9o1vqbXD4fWOyLDPDkbaZYx0Dp/JJOW9 gz1K+7w12sKb+yj3Q0A5jLnWiEU/VmerzhYJvoqR5mEpSBP/WGvpob2+d1qJOhAvpJMKg1jvteX p4YlFkMfoPP6FSnfoUPVaOxgfXuseC9QXqLt90fw+Z8A7CeA+CLDgyGLrt9SFj8UXcRSPoA8wi+ g4/jdnVsTekzD4hHWr24ydThH/JdyyyiP5LgQy4ceSTrPbCkPabKJ6cezKzCJRhAXTCCCjIUpP9 5wIyWYSFIRw8xm1+LAwOIt5EAKDunrE8YI6vmqE3fSAsDpd5atJAeINpoW/eKfcw0TSm1+KLquB JOab7zCkOrc+KSH2ge1ekNNNGb2nXLvTHitXnCTkMf7jr6Sk2e18J/lf3Q9XCSwHQXvIamfGBm/ LuGEn3Xmspv2gnA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240610-tracepoint-v2-1-faebad81b355@google.com> Subject: [PATCH v2 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 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 --- rust/kernel/lib.rs | 1 + rust/kernel/static_key.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/Makefile.build | 2 +- 3 files changed, 89 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..6c3dbe14c98a --- /dev/null +++ b/rust/kernel/static_key.rs @@ -0,0 +1,87 @@ +// 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 = "x86_64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) => {'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 = "aarch64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) => {'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; + }}; +} + +/// 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_key`. +#[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) => {{ + // Assert that `$key` has type `$keytyp` and that `$key.$field` has type `static_key`. + // + // SAFETY: We know that `$key` is a static because otherwise the inline assembly will not + // compile. The raw pointers created in this block are in-bounds of `$key`. + static _TY_ASSERT: () = unsafe { + let key: *const $keytyp = ::core::ptr::addr_of!($key); + let _: *const $crate::bindings::static_key = ::core::ptr::addr_of!((*key).$field); + }; + + $crate::static_key::_static_key_false! { $key, $keytyp, $field } + }}; +} + +pub use {_static_key_false, 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) # --------------------------------------------------------------------------- -rust_allowed_features := new_uninit +rust_allowed_features := asm_const,asm_goto,new_uninit # `--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 From patchwork Mon Jun 10 14:01:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13692100 Received: from mail-lj1-f202.google.com (mail-lj1-f202.google.com [209.85.208.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 0166B13DB92 for ; Mon, 10 Jun 2024 14:01:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028114; cv=none; b=pWecI3QUEepUK+3aejCnDkHPQ00j4xxXwKJVI8vHjOqTyIC+Zc4PQI+9Tv6d4rMvavWh3VKgAy4D/RZKluFGRx4mKEAALUSxLEj3JAD9qTHMOlnpD9VTbAiZsA0Qyvk6xV0ayQCt+vocWSV4i+rlUxhPtZZvp6x0mLaxSiN3pzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718028114; c=relaxed/simple; bh=Cbr/oyqLs30l5kHzPHIKBFw+7ijjcP/soSV8d5bF/6c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s5+/uzieR9pZDLgd4ISGqgfa14nq/NJo7VrEEN0u3pKrTF9NNqoD+GGq0Hw6gUsqhVcWZdKmGyyDRGqtzEAm+YrFP3luZIHc37mIkwCACV8rX5wffaQ1I44Ph2Yu2ilW25370GLlqPvro2aryzSR7B+Wh/OWryFaNgNYUFlt/BA= 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=umrWojCO; arc=none smtp.client-ip=209.85.208.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="umrWojCO" Received: by mail-lj1-f202.google.com with SMTP id 38308e7fff4ca-2eae76312d8so24379421fa.2 for ; Mon, 10 Jun 2024 07:01:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1718028111; x=1718632911; 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=KHgyDe9OFhKfiKpcPG5odSWdTyuUd8K6Z6ATNBTmhDw=; b=umrWojCOZLvj1CmIiRlfeXgyBFWZaZkYQGx5Fl5U25WeqiqXsq6U2+S4xmUTAq6QDN 2zYwfaBl836JpAHcEe7+ahhnRKqCM9QddBM30bUkZ2zZRge96Pv+gfklhePA6eGs8XFS fuHlcDhu68iLkqhFGQjbfZ2li4oYK5XGgMckVBQ/FWE+T4KNKSfP40C7nhMTWMIzg2lp 6ew5bTWRmIeaFz46b6dGlpf/KirENE0eyW2AKEFEboGT1AzUzB6N4ActZqHBjZUYLjTl mVNoRNaFTRD0L9RElLOQGRPdZrNfz7rwzOWnW8InjQY6nbexoQOBVt436X4nIdiQSunx hZ7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718028111; x=1718632911; 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=KHgyDe9OFhKfiKpcPG5odSWdTyuUd8K6Z6ATNBTmhDw=; b=oyEkYPeCylCQcJnrP99q6uLOgFPt+nJsxvjFPyqxiSetxd+Wj+GATLFRIct1/SU95m PUiUBCbRPXirFpxpCx7/RGGt4mKmWQcYaTdldw+jKUvuJLLUCmJudV7IL7O7WVyBOOfr 2yMiZC3HKvp3E0tzTNbNR8ch+BijAOpIW9oXmI8z8S7r/HT81qT8FcLfTJs79WCJbF00 3v/QXE+cqs61R199Ddd6wi7ubzeOmVl1pzniu43Xn8S/36Fx84Xq5oa0yiYq6VyJ7ab/ xO4+W0us30T3RRFH+JdBn4oBUTSfNMMwDiASNHP4VsFcS7amV4Hccy8IkIZdrKVLN1ks gguA== X-Gm-Message-State: AOJu0Yxxl+8MZaiiE1xML4q+dCrSs3ERDuDXLGgWXP6u6t65u7Rlpv5j sOPCszeZNIycjMAwUV0xFQqNxi4pn8J9+vIGze4pStNmd5cLP5Wq6MCVoiiWwSYa1XfgbiZLGnc 1B2xWNvk2+6i/QA== X-Google-Smtp-Source: AGHT+IEccQhl85iyAkHm79GW1ZTFlTgN5K/F0xRloYf0k9KFN8QZT/uSMpgILMSEjfT1xaJgdafVpHDBL+WygIg= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:2c19:0:b0:2eb:ec37:ad85 with SMTP id 38308e7fff4ca-2ebec37b072mr18821fa.9.1718028110654; Mon, 10 Jun 2024 07:01:50 -0700 (PDT) Date: Mon, 10 Jun 2024 14:01:05 +0000 In-Reply-To: <20240610-tracepoint-v2-0-faebad81b355@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240610-tracepoint-v2-0-faebad81b355@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=Cbr/oyqLs30l5kHzPHIKBFw+7ijjcP/soSV8d5bF/6c=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmZwdFyoZES6wUnB8a6HfkMMvV3cAI2deGLmDYw YjVBlpZV9SJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZmcHRQAKCRAEWL7uWMY5 RgKLD/99q9BaDPXlZi93F4HrhWiyx3jSWWiL3FlVntMFmRjh30wfOBLJwSneVkTvLDr/UONVh6p LgRKlhQfXVYFHK3JFfYbEXSonuGFWkaRFPF0A+i3XuYUosnS+LxNcHv0md16126nUtrhtEyMgr4 1B+m6thCprxdnJU8KofRtFvz2cRAP9Z2KPVorFBL7libs3XRcJfO9HHaYYcqT0hSm/MzLvq9OBt AzNIa1ZZD8AvNn+vnKjZr6wvpJmJ5ljiXTUskTc/qQ4eyho0izkoHRMMhqanxI3G8LMNoXmGUB3 3iP2ZECiFB5tlDP0yKmP6Ee42WcFqrTcVP3y8coiQv9efgKDc9j75g/0F+VXWGSXwpiEA+z5L3h WsovfcZE48Wo6Nk0HxaplalVElXl941R/ZDxDrpyru9Gxisty496fmuy2Era2M3EYq9iVeM67zE RsEJBkAlP08pvJ27sEtl8PwTOtsZI7Jrq1jsWxICXSw62nG1KAAuIMoRAOGg7hQb6Pjmis9ZOns VMtVqXExCcdnki/ribs68aTwAfl6RG4YLLKb6ddtlH9NTvNYVBPZmrJhqFWn8PGZOsQOuU3WrTv NeZYWE3flvevie9DeqTfo1zMKwDvi7Mdtrh8IBC6uskew3ZG3LRkYqckoR9zN8WfVcrCT8+HAv2 7lhMJU9ONZeRPxQ== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240610-tracepoint-v2-2-faebad81b355@google.com> Subject: [PATCH v2 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 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 +/* + * Declare an exported function that Rust code can call to trigger this + * tracepoint. This function does not include the static branch; that is done + * 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 will * not add unwanted padding between the beginning of the section and the @@ -253,6 +267,7 @@ static inline struct tracepoint *tracepoint_ptr_deref(tracepoint_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(tracepoint_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)) #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)) +/* 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 diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.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 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; 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 : $argtyp:ty),* $(,)?);)*) => {$( + $( #[$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 = 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 to call this tracepoint. + unsafe { [< rust_do_trace_ $name >]($($argname),*) }; + } + } + } + + #[cfg(not(CONFIG_TRACEPOINTS))] + { + // If tracepoints are disabled, insert a trivial use of each argument + // to avoid unused argument warnings. + $( let _unused = $argname; )* + } + } + )*} +} + +pub use declare_trace;