From patchwork Thu Aug 22 12:04:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13773279 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 0FE8DC52D7C for ; Thu, 22 Aug 2024 12:05:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=iHiOuWi1LCrCLlIWs39Rsb1iRox5iY606vEU71zx5po=; b=0kIZioRDrpgWANNXNg7K+wdd/w 5UsC0fREzbTZKl1/lFHuKySFIbsnnkorsMbo/toVlFOw69tDOesCAbjCke38EU9vD1N022CLFSRAz LCtodJHQL2sfP/N2Unwncjmt3bTYY0grGFF8EpSbqbolK+N0sXYJZ3SIJVtmZNXIMrVDwUAa5C83J jRQRZ9LqXUg/7jEiPaHj2QoLlYCd3ByJcbJl/We7Qb18sUGBNDpIDiD/c/yBvhQKm+/Ea6rIK9D0R ZlvZB9BZnivVIwY9AG2IfeQ1ckZBdJqt0z3Su3gQXyr8Hug2tAN0N15GM3GL5qPkNUg3CNbIbXJJ7 IRRvEW8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yq-0000000ChL2-0vZY; Thu, 22 Aug 2024 12:05:04 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yg-0000000ChCY-2x2t for linux-riscv@lists.infradead.org; Thu, 22 Aug 2024 12:04:56 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-371881b6de3so339119f8f.2 for ; Thu, 22 Aug 2024 05:04:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724328292; x=1724933092; darn=lists.infradead.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=m06R8rBHhIV+fNfH7++C5Q7rVR6HteIR9+x3IVgvVo0=; b=HHx4f3KB/+P/Tb/lWIZXs81y7gAtncjJWO28ZiQ3QntpWXDIqKDoKNARk3k41GkNDp XEhlmlneAA5GlDm4SUNzMdPAUyH4NSQfWmtnNy/suf7od037jCIVDZYPSUtlbM/LD9Ur P3g0M4irVPJKJZj+RqFNDz9cVusuI+GBWH7FMoVDh4GDAkNMfb2aSSzvKmOdd+TVbA4M nMLMeIRVX9wMVBD8yBHn3VPl30vgT0Y0Zpf7qzqhEpxjbAzyUtVz9zh7SOIAVMpg+stP dE1Dboco5RCssBIjgoq4Q9CO7X+FUEspdPq9n+IDSix1lnN7jVnu8td2Ff5biBCDo94/ slWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724328293; x=1724933093; 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=m06R8rBHhIV+fNfH7++C5Q7rVR6HteIR9+x3IVgvVo0=; b=Ti+AAcWZNWShtvWZ8Qtk8zz6ODssclSnSb+NezaXfXS0M3qs2CyTmfvMq7veowZq/B DBIJm0qWFcc+OLJt//yRqCqWsAOtLYgmArdSJU8YDOFzJ4xDmY4MHSS/5SuL8qGeS0wQ pDK++Cje+I9HHiCyvT7TmOvNNmc9OpD94N32rqFOjbzPU7pLjrW7HVpWdYgCAXboy0Eh 3zULzktdyR10Ez4pi8bAKNrh+hLeUNrIqnTRkoYwGT1Xtwq45XCFr6Foxia1xsDNF31a UTPZRYpN1twuNsi+r/91+4g7ia8HsEoNZIVrbBMBo+t0lol4FhwVWu5vZM6dM7JyoP1f OSUA== X-Forwarded-Encrypted: i=1; AJvYcCUOyVbbfoJV/7zKNQaIUq/JAcAcSw94AUYf3RAY2JdKxHHd7rdepy+eiCCbzIAuHRL8bz6cm2V4w96r5Q==@lists.infradead.org X-Gm-Message-State: AOJu0YyUR8jIRq+X6pi4oB/JWXtarkfVH++Apt7qlp+aCqgFVOBKKHyd bm0flJ3UYkD8oquGVW+PuR3bOaV5W4hTbfWSw8gYsb0mGZnd5w3NDWVbqBdgVkGqbB6etktfGke pdq78igr1uCOXZA== X-Google-Smtp-Source: AGHT+IERBhVv08A2uZv+MpnM9lKKuFpvF/ldKWmNQbSwx22l+/nHv3lZyxTumbSPIvwOOIsyXNIWmCzz19pCed0= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:6000:2ad:b0:371:9373:5078 with SMTP id ffacd0b85a97d-372fd59721cmr11543f8f.3.1724328292363; Thu, 22 Aug 2024 05:04:52 -0700 (PDT) Date: Thu, 22 Aug 2024 12:04:13 +0000 In-Reply-To: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> Mime-Version: 1.0 References: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3678; i=aliceryhl@google.com; h=from:subject:message-id; bh=x4Fj8n509ULmZ45N1WACwPWEQuAc2oLxAGwmKjHodr0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmxylcN8mcEXadzOJVoentM5FyVm0c7KXiYJoT5 nryZ6UFTe2JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZscpXAAKCRAEWL7uWMY5 Rh5pEACltjdooXrOi5jfHOH3q4t2NrSAWmz0Wzo7scorn20GnCAwxRr3y/CF07ko6cGGlugqo7w fw59+gzaz0SDqS70pEVZt5qbsa21Mbm1GrJKocf2mXPLC9onKsU9xOyLlPXlxi+o89Ln3Lo729R IKu+oYEF5yDZC0nRxFTEZsX0YJ/bNKZwEMFsYem9WSrjI4ifDw7HDPifTLoRh1HRJmBRciDe9+f V7Tm/sST0aeMNPsFLh/eCQsQvL4Bf916kOL0jkbZGKSoSS4gJPTDf2569fmJ0PU/Z7/62qsKJaw paEkLJnj/MQLxgslDQ8lazmAMMDoQYH0CqrkBlIzksOwB0ftEuQI3hrV7ELD/5AnUCrup2fWscm jtounuZ8L6EpPLOCSAdPObnCCNgNTpIO3CcN6wt+GW8Flc7OsncgAq2qa8JDNUesF4jHVm9v9u4 hX2UZM5ou9VSVpxvlf2oOYTmZPnNyvrL9DZu9LjXnEwwg+qXpgQKvEvDY6ufRj+kC8SedA+UB3F ulk9WotpkJkTv4PxvDq1h9XI4djWh4uXsdrRizqa+QZAMv/1vjm8bMkaFkElesH5gA5akc2m1NP sNi8Jr2dh7/sqpvIvYrOucLMAcK/bzi7/M17+DSgo1+PyKsqSqlwBJiDqkeh3uvgJJRRh7MaE3U UHQDUWgEeb5N4nQ== X-Mailer: b4 0.13.0 Message-ID: <20240822-tracepoint-v8-1-f0c5899e6fd3@google.com> Subject: [PATCH v8 1/5] rust: add generic 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, Arnd Bergmann , linux-arch@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Uros Bizjak , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mark Rutland , Ryan Roberts , Fuad Tabba , linux-arm-kernel@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Alexandre Ghiti , Conor Dooley , Samuel Holland , linux-riscv@lists.infradead.org, Huacai Chen , WANG Xuerui , Bibo Mao , Tiezhu Yang , Andrew Morton , Tianrui Zhao , loongarch@lists.linux.dev, Alice Ryhl X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_050454_908568_C4A4CD4F X-CRM114-Status: GOOD ( 20.23 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. This patch only provides a generic implementation without code patching (matching the one used when CONFIG_JUMP_LABEL is disabled). Later patches add support for inline asm implementations that use runtime patching. When CONFIG_JUMP_LABEL is unset, `static_key_count` is a static inline function, so a Rust helper is defined for `static_key_count` in this case. If Rust is compiled with LTO, this call should get inlined. The helper can be eliminated once we have the necessary inline asm to make atomic operations from Rust. Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 9 +++++++++ rust/kernel/jump_label.rs | 29 +++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 4 files changed, 40 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index b940a5777330..8fd092e1b809 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 92d3c03ae1bd..5a9bf5209cd8 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -133,6 +134,14 @@ bool rust_helper_refcount_dec_and_test(refcount_t *r) } EXPORT_SYMBOL_GPL(rust_helper_refcount_dec_and_test); +#ifndef CONFIG_JUMP_LABEL +int rust_helper_static_key_count(struct static_key *key) +{ + return static_key_count(key); +} +EXPORT_SYMBOL_GPL(rust_helper_static_key_count); +#endif + __force void *rust_helper_ERR_PTR(long err) { return ERR_PTR(err); diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs new file mode 100644 index 000000000000..011e1fc1d19a --- /dev/null +++ b/rust/kernel/jump_label.rs @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for static keys. +//! +//! C header: [`include/linux/jump_label.h`](srctree/include/linux/jump_label.h). + +/// 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`. +/// +/// # Safety +/// +/// The macro must be used with a real static key defined by C. +#[macro_export] +macro_rules! static_key_false { + ($key:path, $keytyp:ty, $field:ident) => {{ + let _key: *const $keytyp = ::core::ptr::addr_of!($key); + let _key: *const $crate::bindings::static_key = ::core::ptr::addr_of!((*_key).$field); + + $crate::bindings::static_key_count(_key.cast_mut()) > 0 + }}; +} +pub use static_key_false; diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 274bdc1b0a82..91af9f75d121 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -36,6 +36,7 @@ pub mod firmware; pub mod init; pub mod ioctl; +pub mod jump_label; #[cfg(CONFIG_KUNIT)] pub mod kunit; #[cfg(CONFIG_NET)] From patchwork Thu Aug 22 12:04:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13773280 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 04D4FC3DA4A for ; Thu, 22 Aug 2024 12:05:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=WcnDqxL2kzX3BfgvoC8qhLTAYPEYdXgTZBJuwjovtgA=; b=qsj8xEZrcQN4JuDJ0b3Ll8lvP4 Ha1rRn9RB28mckNdp5s0Vy9kaWUfoYiu7AL1pYMtfYvlzryiCZlklefcOziC8I5Wq8yjGIS2nknis dISFj/u9J+vG3D0tgzozpqC0o8/EIHgRPmwVAxgDk98vVkazLmRkl3VuhgM13v2v7FQKlSHS0wnSL RLJ/1PYi5utlMAB1RC+NgwEuRep1GDrkyfrlkBs0CJNmFYaaczsT7EJR9IZ9kcJoO2Oogde0EFlye EmeOz5xxnhjTZ2ro2Tixz09Zm0aPimz3njRc8dPH6RiDlhLsAwSuA1C1aB5qTkyeCTWr0kgUOy2/0 qtU/s0hA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yt-0000000ChNu-39Ty; Thu, 22 Aug 2024 12:05:07 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yi-0000000ChDq-2LZa for linux-riscv@lists.infradead.org; Thu, 22 Aug 2024 12:04:58 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-6c2599195c5so11435607b3.0 for ; Thu, 22 Aug 2024 05:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724328295; x=1724933095; darn=lists.infradead.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=f8cDHUaReumSMCOwyu04+mqYJDxHlzEMGiEzz4YHS0I=; b=GihwG5NcsA9GfmwS/IaT2j7WaYkIwMxZJXJVVcglcCJzRmJ9AWHBMXlRGfa+zLpohB sbU83SkdBG2VESKuQv6C+hygwNzt/9Aw9a21S80exuGbyJaXQMXY/CF3xFSddln0IM9r T9JETH2b4OVE7Zk/EjlNj9pLUos+oZOjP4GE3Eru+VIIJ52513j/8uOXN76W0XmDyQgh /Ugwm6BXWsgxAj/i20OdN6cz4/bg+9/8EoVbbwn+PpZ7VMeE2Qy8DmMP59zKRhGgfthy DAjdYxO6kCFEgleFtfM4MYcxzjdIUJQhXepM/UaoszD4cdy96tMfvF8kYKOXnTe9sYjw hoiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724328295; x=1724933095; 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=f8cDHUaReumSMCOwyu04+mqYJDxHlzEMGiEzz4YHS0I=; b=QkIh2MbkvkNvYD5yQOzoHwyxzWBXNMAivZUqZypiMRWsEQF/+UohHucEzJmjxi1W3g f8zcNuTs7TR96cYHa2dbhwTChzRTsikivubmuoWwaCsb34s6D00XHaDc79jRFTGefJ93 tVgFTXORjYR2EJuwHR0FL8ILXp05XTduCIdTndxP1aaon7er1KX2A1y/T25NbSxO0xA1 djZwK9iuthazhfU1HlvRndEaKVXzDziJv6DTXe3GqYymOfMS5YU2hj1mTSJxuyD7KT8S HdI0x/6NovjuTfUzGTG7YiHsuuZwwOje6EKCEGsBS53Vxqbz4jufEmC4VfyY9Dbm5Odb 1Ubw== X-Forwarded-Encrypted: i=1; AJvYcCVZYdxSyAw0coksahukmEgsNFLgnsxm/Nvew8Fa+DO3Az3sWLYUp2Zw5wTzBFgS9JjmAxR7XRnN+s3WPg==@lists.infradead.org X-Gm-Message-State: AOJu0YzLtSP71LZuqfxp7dCbnXH7Yk5jc2/xTLRk55t+Xnc2pqWTz7B0 pgAY4FNaFLhuPg/xsSF6gG8WiWLZUDtpY2LCayso88qwWQTJH8ZCZWbsQ1UIpgt95TKRg8qnJ/y DicW0FWe38ghWCg== X-Google-Smtp-Source: AGHT+IFS8l23pd6XKyyBpjZiibiQJuE/dGIDebXAym+Zq17SC6CU8fu8OUtqV4IA2VQql6cypuUkw5hRlPx/fCk= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:3683:b0:62f:1f63:ae4f with SMTP id 00721157ae682-6c09ba0765fmr3529837b3.1.1724328294934; Thu, 22 Aug 2024 05:04:54 -0700 (PDT) Date: Thu, 22 Aug 2024 12:04:14 +0000 In-Reply-To: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> Mime-Version: 1.0 References: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6866; i=aliceryhl@google.com; h=from:subject:message-id; bh=mbr+t2kdtAR0gaGGgahPyE7BcbfXEyne0mHE/YfAoMM=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmxyldgT9/w/ZnIezX+YKTsIh4Z5lhApHS8iZgy GDuXKIjQROJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZscpXQAKCRAEWL7uWMY5 Ri7YD/0ZsdMONo37tyeCBN+jKz/68J+eujSawLXMswiPYdTohvjITOoKtaLFazXZVIPOFfxDqrO MnNH9Nu53HzI5zwZy5mFohAdI2DyHlD3epSshGT9WBTbAhkBhQtppAStZiGcKhgg4pPuAVs7YB3 b7NfaOR0ZAw+dx6u0ogwlrFB9HeVcrm8R0G5kFpwjGJnCgzWWh/VnTK4j+EINoBxBr82tHq3FSq DZq92HK3Q5sDkph8Z43wEhXzYUznw0mFas3+1Wj3aTgxlBYTvzAdCQdfpQN5+Hom37p8BNfJM7D OLN9kPzMmDEUV4asCHvO748lZ4FUAFQTYTsLFLz4G3kEJWxhmjd1ratMl1+YShBy1p5pI/K5qy9 NO/C1gtukYu+zNURzZA5ssWdj+TLWVFEvfuKS7KK46XkbFn0o0PBn7yJ16z7kfLRRah+y/nNHkP kwg/SvSiYZ4a2dKhWmcpGKzOs4BGYHTEluxQaxUT2EINxWOAO/u1HqWxyvK+C4KDVUGJdUVu0Er 0fBQQXZZnBWp63IQDfkQxt/UXIhGxZpb/+kEVlout+lLTVKx3uXKonl/YvbVyFPe2gLdAyX41Yb V9KNcoRaP8OKgE3RDT61a4JM3IFQA5/x7xNShkujV0gaMl2g3SLuTih4eBQyBEEsU76DrPZF7w1 3EbN95Wt07MWnKQ== X-Mailer: b4 0.13.0 Message-ID: <20240822-tracepoint-v8-2-f0c5899e6fd3@google.com> Subject: [PATCH v8 2/5] 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, Arnd Bergmann , linux-arch@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Uros Bizjak , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mark Rutland , Ryan Roberts , Fuad Tabba , linux-arm-kernel@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Alexandre Ghiti , Conor Dooley , Samuel Holland , linux-riscv@lists.infradead.org, Huacai Chen , WANG Xuerui , Bibo Mao , Tiezhu Yang , Andrew Morton , Tianrui Zhao , loongarch@lists.linux.dev, Alice Ryhl , Carlos Llamas X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_050456_699757_FAFF72DC X-CRM114-Status: GOOD ( 27.66 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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. Instead of calling __DO_TRACE directly, the exported rust_do_trace_ function calls an inline helper function. This is because the `cond` argument does not exist at the callsite of DEFINE_RUST_DO_TRACE. __DECLARE_TRACE always emits an inline static and an extern declaration that is only used when CREATE_RUST_TRACE_POINTS is set. These should not end up in the final binary so it is not a problem that they sometimes are emitted without a user. Reviewed-by: Carlos Llamas Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng --- include/linux/tracepoint.h | 22 +++++++++++++++++- include/trace/define_trace.h | 12 ++++++++++ rust/bindings/bindings_helper.h | 1 + rust/kernel/lib.rs | 1 + rust/kernel/tracepoint.rs | 49 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/include/linux/tracepoint.h b/include/linux/tracepoint.h index 6be396bb4297..5042ca588e41 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -237,6 +237,18 @@ 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(name, proto, args) \ + notrace void rust_do_trace_##name(proto) \ + { \ + __rust_do_trace_##name(args); \ + } + /* * Make sure the alignment of the structure in the __tracepoints section will * not add unwanted padding between the beginning of the section and the @@ -252,6 +264,13 @@ 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 __rust_do_trace_##name(proto) \ + { \ + __DO_TRACE(name, \ + TP_ARGS(args), \ + TP_CONDITION(cond), 0); \ + } \ static inline void trace_##name(proto) \ { \ if (static_key_false(&__tracepoint_##name.key)) \ @@ -336,7 +355,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..8159294c2041 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(name, PARAMS(proto), PARAMS(args)) +#endif + #undef TRACE_INCLUDE #undef __TRACE_INCLUDE @@ -129,6 +136,11 @@ # undef UNDEF_TRACE_INCLUDE_PATH #endif +#ifdef CREATE_RUST_TRACE_POINTS +# undef DEFINE_RUST_DO_TRACE +# define DEFINE_RUST_DO_TRACE(name, proto, args) +#endif + /* We may be processing more files */ #define CREATE_TRACE_POINTS diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 8fd092e1b809..fc6f94729789 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 91af9f75d121..d00a44b000b6 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -51,6 +51,7 @@ pub mod sync; pub mod task; pub mod time; +pub mod tracepoint; pub mod types; pub mod uaccess; pub mod workqueue; diff --git a/rust/kernel/tracepoint.rs b/rust/kernel/tracepoint.rs new file mode 100644 index 000000000000..cf2d9ad15912 --- /dev/null +++ b/rust/kernel/tracepoint.rs @@ -0,0 +1,49 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for tracepoints. + +/// Declare the Rust entry point for a tracepoint. +/// +/// This macro generates an unsafe function that calls into C, and its safety requirements will be +/// whatever the relevant C code requires. To document these safety requirements, you may add +/// doc-comments when invoking the macro. +#[macro_export] +macro_rules! declare_trace { + ($($(#[$attr:meta])* $pub:vis unsafe fn $name:ident($($argname:ident : $argtyp:ty),* $(,)?);)*) => {$( + $( #[$attr] )* + #[inline(always)] + $pub unsafe fn $name($($argname : $argtyp),*) { + #[cfg(CONFIG_TRACEPOINTS)] + { + // SAFETY: It's always okay to query the static key for a tracepoint. + let should_trace = unsafe { + $crate::macros::paste! { + $crate::jump_label::static_key_false!( + $crate::bindings::[< __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 { $crate::bindings::[< 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; From patchwork Thu Aug 22 12:04:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13773281 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 19352C3DA4A for ; Thu, 22 Aug 2024 12:05:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=ghclI3fHXITnMCImyDIU1aDDMP71kKHki5NVww3XDXU=; b=e2mBIjXoZsy8Ohnl/rejDJ64qC RqfJ4Mf1iO1JSYedtSu3lfUIbSDqxnEgZp2VGdsOFG+vbPtKMn91qI+JloRmyRqEclm8VjlL96dqy 4ZwiZXLJfu2A7edyN02SfY9NVHkuNtVv8u7C5OsASsIatG6KKlUSS3ZnHWa3T1RJ8z6xH4AU/UsHZ LgOwi+gMhnea7cxpGL4hJU1FM7fnTRCjFx+DxHS+oew/ani9bNL7PYpBPF+ELck9PjU1LwMA/WU1C OT8hHIPcymaWJrnlSwrQO0mVLKBfrzW0wIERojqvzVUGTRLBawt5dwWWmNOUm1ZsHM4HT/17Xcazc xx5y2f3g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Z0-0000000ChSD-1W6u; Thu, 22 Aug 2024 12:05:14 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yl-0000000ChG5-0FA9 for linux-riscv@lists.infradead.org; Thu, 22 Aug 2024 12:05:03 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-e0353b731b8so1223644276.2 for ; Thu, 22 Aug 2024 05:04:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724328298; x=1724933098; darn=lists.infradead.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=8WXJYVeFW+jGG7DCrz93gCg6JPKvPQ2Wf4cfjKgCwaM=; b=DpDOSouPmqG2MxIEXrw6V8qN30B3czTtInvVPBgdYzVW4ezh3bU+gJPEreGjWL1gwB tIC/mMEnlvteAACIuJenJ2qbCYyecQKExXDkc2H6adkq3yrQZrn4GJazBpcgOxc5JqY2 IdTKq3gYTCfQ2ge/3X5RkcNA/YvS4mGowiolqC8g/KaENOvbpRSt1zU/F+SAmhxqRDqP U/HH4yCdASkLcH/FhtzK4/6n/DQgL31nsa836KqiaodEbgM15RVoU5tKy3ymAw4R6bax sAtXcYWA9C5ji0s2ICFFJlxnUJbFcIRKKFWfWdyoQWS6lnu2oIR9SR8TmPe3mnHvNtJg i9yQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724328298; x=1724933098; 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=8WXJYVeFW+jGG7DCrz93gCg6JPKvPQ2Wf4cfjKgCwaM=; b=KtxheNDGTgaUXpD9o714dmxf6h+tOmW8Yj7J2dUwHhozKkd8IRWJeFImux/MKP1MiZ ApeVEWpKGS4sh8GboJpnIu/Fs18v29H/AK4CQD+112iSWZBLCjtVHNi/is77Ds1gpzTw twavp3AP8Ho0DzVqH7+a/eymyH/c+8DAFl9IQh5bWWMzI8tZt3mm/ZMF8UiAdUUGeoTt fxEZiIhduO7AJRfZEwH9vsLfOsjDPmVaXllcLqvxhImEphE3g6U5e8oqK1K+t3M9hQB7 5uHGUDRiPuzv1avxLKxQqKV9mAyhwT046ZutjZFDxm/9IraHY15hw+oow8xqKFmQvKb6 aNyg== X-Forwarded-Encrypted: i=1; AJvYcCWyIHWctifUYiHFJF+1Xd1MaSat2N90GGqKSpJQkWxFP8MA3BpngRpb3VzvV5TfPN0OMCDUxucpy9GQgQ==@lists.infradead.org X-Gm-Message-State: AOJu0YxtMA70++aMJX4tfdxJ5rTJqYSjAEFLaYpMkEotaiwp1dMJjDIt FWWnB52ITfZYeeQMSSQpE0Hrk/q+AsJaf7Ru4Ma09OTzd/VWuSK/W57E6Wkm1qopntFO9KmZJmN 43m7Qhz2ZotbyNQ== X-Google-Smtp-Source: AGHT+IHOwQ7F4WwpIwCYP14vLZEMvblPZGwmqHarUMkdlvedNsRunUgeqjgc8IGJA2wVHg86ug/U3YKoS2Z6kS8= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:7483:0:b0:e11:44fb:af26 with SMTP id 3f1490d57ef6-e166540e1eamr8764276.2.1724328297474; Thu, 22 Aug 2024 05:04:57 -0700 (PDT) Date: Thu, 22 Aug 2024 12:04:15 +0000 In-Reply-To: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> Mime-Version: 1.0 References: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4269; i=aliceryhl@google.com; h=from:subject:message-id; bh=FjpJXotW1KGtmig+hIm0KQivxPyDztMFhKa3lXhpNM0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmxyldDFylo0W2FYUlH9zPLgV0aNf5JfdXWxC+k EV6BXx+qxOJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZscpXQAKCRAEWL7uWMY5 RmwSD/9lb6aazG/rJFEouodeNF6AgBtP/doqXucmQsYP5lsbpIKM6wD/RrdAfk8KvahqhtiKNZo M80qvgsWx6kR60U8/3k8BpYVdbSLtWq8iy7kDwbqPsW7/lbh6lSSN7po7WTvUGkLxdy55aBL7Sn pxvZeuKi80CsU5j1xBKwqA64hnBiiXrCmIbUYWMsfWCV1QC7IB3G4FGcWnkuPdSj/Sec7odVug3 UGVoS0jRtXoInadAPmAvJimKkQGrM9XN/1U3Y//9PfoOiMapFuQ1CRn8NPVS3wLdpTesPhUn9u7 eexLr2wbBy043Lp3azw2EeBu0AAzJ5FYxBnV7XKV7V+5VISDYLTQky2gSjsC+RTTVAW0ZxL4ExJ 7tcK/uSNtex2S3P2p73VyR46otDTS3YeIjA7K6J8Cw537m+mpFL257yU0gdDeWj4RBhhIEH6JzO Y+Fi1fjq/r5jug7WVznXBOO0L9iN+HL/NGCTv/tRN5iexYDGVO0utnbT5kTx9B3ePtHUT0FmpGN DxAq0+0gRNwsVdxm4jY7ARuozjUBGvHfrKfTKGfwBzWN+t8wjR5lnxyPtKMfDczcG0A9Ii5xWdu wOQZQ3zt/bMwGS3BgCox0QdlxbK6m1+NaZgBJECLHrZEBYyYJo/0uQgI8Kd415KJtvBEaMpr5xh HbPkWR325Lp7a5w== X-Mailer: b4 0.13.0 Message-ID: <20240822-tracepoint-v8-3-f0c5899e6fd3@google.com> Subject: [PATCH v8 3/5] rust: samples: add tracepoint to Rust sample 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, Arnd Bergmann , linux-arch@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Uros Bizjak , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mark Rutland , Ryan Roberts , Fuad Tabba , linux-arm-kernel@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Alexandre Ghiti , Conor Dooley , Samuel Holland , linux-riscv@lists.infradead.org, Huacai Chen , WANG Xuerui , Bibo Mao , Tiezhu Yang , Andrew Morton , Tianrui Zhao , loongarch@lists.linux.dev, Alice Ryhl X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_050459_372024_2A3729CB X-CRM114-Status: GOOD ( 18.74 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This updates the Rust printing sample to invoke a tracepoint. This ensures that we have a user in-tree from the get-go even though the patch is being merged before its real user. Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng --- MAINTAINERS | 1 + include/trace/events/rust_sample.h | 31 +++++++++++++++++++++++++++++++ rust/bindings/bindings_helper.h | 1 + samples/rust/Makefile | 3 ++- samples/rust/rust_print.rs | 18 ++++++++++++++++++ samples/rust/rust_print_events.c | 8 ++++++++ 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/MAINTAINERS b/MAINTAINERS index f328373463b0..1acf5bfddfc4 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -19922,6 +19922,7 @@ C: zulip://rust-for-linux.zulipchat.com P: https://rust-for-linux.com/contributing T: git https://github.com/Rust-for-Linux/linux.git rust-next F: Documentation/rust/ +F: include/trace/events/rust_sample.h F: rust/ F: samples/rust/ F: scripts/*rust* diff --git a/include/trace/events/rust_sample.h b/include/trace/events/rust_sample.h new file mode 100644 index 000000000000..dbc80ca2e465 --- /dev/null +++ b/include/trace/events/rust_sample.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Tracepoints for `samples/rust/rust_print.rs`. + * + * Copyright (C) 2024 Google, Inc. + */ + +#undef TRACE_SYSTEM +#define TRACE_SYSTEM rust_sample + +#if !defined(_RUST_SAMPLE_TRACE_H) || defined(TRACE_HEADER_MULTI_READ) +#define _RUST_SAMPLE_TRACE_H + +#include + +TRACE_EVENT(rust_sample_loaded, + TP_PROTO(int magic_number), + TP_ARGS(magic_number), + TP_STRUCT__entry( + __field(int, magic_number) + ), + TP_fast_assign( + __entry->magic_number = magic_number; + ), + TP_printk("magic=%d", __entry->magic_number) +); + +#endif /* _RUST_SAMPLE_TRACE_H */ + +/* This part must be outside protection */ +#include diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index fc6f94729789..fe97256afe65 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -23,6 +23,7 @@ #include #include #include +#include /* `bindgen` gets confused at certain things. */ const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN; diff --git a/samples/rust/Makefile b/samples/rust/Makefile index 03086dabbea4..f29280ec4820 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 +ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o -obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o +obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print_events.o subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs diff --git a/samples/rust/rust_print.rs b/samples/rust/rust_print.rs index 6eabb0d79ea3..6d14b08cac1c 100644 --- a/samples/rust/rust_print.rs +++ b/samples/rust/rust_print.rs @@ -69,6 +69,8 @@ fn init(_module: &'static ThisModule) -> Result { arc_print()?; + trace::trace_rust_sample_loaded(42); + Ok(RustPrint) } } @@ -78,3 +80,19 @@ fn drop(&mut self) { pr_info!("Rust printing macros sample (exit)\n"); } } + +mod trace { + use core::ffi::c_int; + + kernel::declare_trace! { + /// # Safety + /// + /// Always safe to call. + unsafe fn rust_sample_loaded(magic: c_int); + } + + pub(crate) fn trace_rust_sample_loaded(magic: i32) { + // SAFETY: Always safe to call. + unsafe { rust_sample_loaded(magic as c_int) } + } +} diff --git a/samples/rust/rust_print_events.c b/samples/rust/rust_print_events.c new file mode 100644 index 000000000000..a9169ff0edf1 --- /dev/null +++ b/samples/rust/rust_print_events.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2024 Google LLC + */ + +#define CREATE_TRACE_POINTS +#define CREATE_RUST_TRACE_POINTS +#include From patchwork Thu Aug 22 12:04:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13773282 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DCEEFC3DA4A for ; Thu, 22 Aug 2024 12:05:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=wxjxFHCXKRsZUF7piqLhJRqqd2+5a0hn64MPqXZaytY=; b=1MQlw6BMEylJ27sG0axwmshYsO uBkMSeA46DIKGPBOCKPJzJCNMwaN8KZVF6DCgQBcNJjZ9SbAoZcXTgwDvtP3CL8Yp/ehQJkeV6YsA fvf0SBItKey6nZq0Xdyy1q37pn7oRlhw6b+c9ie/WaVialfTHkvmsGd2IrTZ4jwfU9Hy/BU/qk0x+ aygc4YZGwXNkDTolU/ahlsbF9JIHziSva5gzpe5d0MVCi6WWHk5I3nYR/qK5xrvVcUjz8I8yOlp9p MFiIafYKj4atVi3Z+U1rUX3HUhCOtI5JSzg0VJ01iUMUKXHE3psM+ZKsJUH+Gj+WtJ5KVaat4ytm0 3tPcJjNw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Z6-0000000ChWd-0WZN; Thu, 22 Aug 2024 12:05:20 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yo-0000000ChIp-3Tql for linux-riscv@lists.infradead.org; Thu, 22 Aug 2024 12:05:08 +0000 Received: by mail-wr1-x449.google.com with SMTP id ffacd0b85a97d-3718e1d1847so398845f8f.1 for ; Thu, 22 Aug 2024 05:05:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724328300; x=1724933100; darn=lists.infradead.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=Q375QRvUD5Wva8hc3MN+sMayJuBnYHhQhae9RWdaskQ=; b=4YMIlbpPFY0p6fj1aHwdszHdIrPCMj13fKivpqclb9njL+fei4eBkWefpAbalD5+OS Cq80MeGUB2W/cZCWvOfZkQoQzMYdXuCaK5gzVOWXNL7IXFfyEX8e4+IOvTRPIlFFRElr PwZAaEV0FlJ4jBfwihI8Je7UtBvTrV1M91x6H10gumH+EhJ6mJZ0FkOx+YICqBYhQFrk 99Vn1GoOO82VoCzWvmu3MZris3CuSCCdjvJPztjKCZS7dreEYjui9HTixvHwBT+74QLb ILTH/bjQZQhzreDcx2dsLURPd1RmVhDRhixVPqO8otySpN6E+FA/U0KLzXwMwy0DP2dH yFig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724328300; x=1724933100; 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=Q375QRvUD5Wva8hc3MN+sMayJuBnYHhQhae9RWdaskQ=; b=oefsj59Yxo7peOpApeOZuILu/Li1Y05m0RAlkp87yF41aBQhB69ZsL1M70O5T7kJNI 2hY9TuezQHsBWIf+m0BF5n5yrQCcFB9U4UOMEQvjgalbaZofGttllsBIgxMFJUNpu0b3 Ae2nKu/tHQbtu/kuTxpx+c7+vpwSM6j2BRpAXyIbO2XnCeN1FUVDQ1e6MSl7R6yVwiH7 0dtKJgmEdoA/g+NvLLXXNUJn/dh0hyjRvRVdB9Zlci2gC6nVcskuqfwTbZ9S13pQPpBl OTmsviaRu53E0FKlXzYmIspl5tExU1hkAuagE9I4MVPZDAQwduSrWzPQry4u+OeBIb2E jhCA== X-Forwarded-Encrypted: i=1; AJvYcCWrA/4RK9U1jLfT3Ru0/pBvdE/NStxzyFZoqZnOyCqt3jNsXlerkalqHwjocY5TfNMFF8WvZ2ogdLaP3g==@lists.infradead.org X-Gm-Message-State: AOJu0YwCFbAmuefsy3edORdU3r7tlLmjKu/Mipgg5/POQvXjZLdETXJt E5sf/aqz56lcHN0bJ6cCL8VIBixukJMvMRXe13inkRob87dKJ2e2fHy7yuO/4kS9w0b4yB0dnre 7cD5Xpzz9xLFWOA== X-Google-Smtp-Source: AGHT+IGw3yfAzaYcyD6d0zmHCHRXiA2QSLiNwuvY02gaan1SLSwcPyY0PwxfH/wKsRMOPzJoAId5eBQhi7+XpwQ= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a5d:5392:0:b0:368:371b:3ae with SMTP id ffacd0b85a97d-372fd5902c7mr8464f8f.6.1724328300071; Thu, 22 Aug 2024 05:05:00 -0700 (PDT) Date: Thu, 22 Aug 2024 12:04:16 +0000 In-Reply-To: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> Mime-Version: 1.0 References: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=9372; i=aliceryhl@google.com; h=from:subject:message-id; bh=NCijnrFVATFS0Jb3tHGcAM6xeWG4d78Efm1z0mpVoWk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmxylehr+vvDlXsGVX+QAyOJmDHQJ8dueLfCPb0 1aADq7Xg3SJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZscpXgAKCRAEWL7uWMY5 RvEiD/9pK5VDC1VlIuqdn/1xgy7FKaKXzMAuZ9DX+dqwtjqh8kmF6asI9lg7b+zWAu71c/J/CJC RSaScqbPYPJ+HvpKfEqipatv8FovDMOM7YowcspUQUAT4CcPlQQ3vTdZfdUVD8/c/hfsWgFz6WI OAB231FNblq1h7IKhnXwNpbb23O9QwyDg7EaYpN3jYLa/oCFvn3O4gedIPo+d6vylLIeVej0+7b OROXwoSwxvIsMFL+e8v01YwJbYe/uwRS/kkf2LWxDwCv6leShBLhXCY/kDSd0Nxjv8hbdiyB0ef D26TdMF1EAvU73JkPQWcsvXpkbN9+SPqAV9X4yfhFur4M7AK8m+gCFr6ecVj4Yb9MMmgt6abyZu 1Rub67vPI8oa3OrU7+lR0xlqX9s0PDM70OkEFAN7q5V4lC+TnQyhmKwu/8HGr39I5KdIyEbPSCC v1+4BItg3uGOFFoGZidk6zVDoQQP+e/wIwXK9u0PYMpdfNQ88o75b8J/tK1sytl9045RqV1WZKx mWy6QuM7Fdj5xdBZ7uO6rQDeySO8zBPHGhvrR/qq6LzdwKEi3zt8D31LO2OJhOWCKfbn/BbdDQp ruagtYvfR/vABAefkqf9zlG1Lr2e2Xohy2qpe8HBXL5YociYOJ5HIffB6gWsGenNKX3SwQZG0Q2 bwo4NPnXyRTaPIg== X-Mailer: b4 0.13.0 Message-ID: <20240822-tracepoint-v8-4-f0c5899e6fd3@google.com> Subject: [PATCH v8 4/5] jump_label: adjust inline asm to be consistent 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, Arnd Bergmann , linux-arch@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Uros Bizjak , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mark Rutland , Ryan Roberts , Fuad Tabba , linux-arm-kernel@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Alexandre Ghiti , Conor Dooley , Samuel Holland , linux-riscv@lists.infradead.org, Huacai Chen , WANG Xuerui , Bibo Mao , Tiezhu Yang , Andrew Morton , Tianrui Zhao , loongarch@lists.linux.dev, Alice Ryhl X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_050503_178954_0C31DA3A X-CRM114-Status: GOOD ( 14.62 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org To avoid duplication of inline asm between C and Rust, we need to import the inline asm from the relevant `jump_label.h` header into Rust. To make that easier, this patch updates the header files to expose the inline asm via a new ARCH_STATIC_BRANCH_ASM macro. The header files are all updated to define a ARCH_STATIC_BRANCH_ASM that takes the same arguments in a consistent order so that Rust can use the same logic for every architecture. Suggested-by: Peter Zijlstra (Intel) Acked-by: Peter Zijlstra (Intel) Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Signed-off-by: Alice Ryhl --- arch/arm/include/asm/jump_label.h | 14 +++++---- arch/arm64/include/asm/jump_label.h | 20 ++++++++----- arch/loongarch/include/asm/jump_label.h | 16 +++++++---- arch/riscv/include/asm/jump_label.h | 50 ++++++++++++++++++--------------- arch/x86/include/asm/jump_label.h | 38 ++++++++++--------------- 5 files changed, 75 insertions(+), 63 deletions(-) diff --git a/arch/arm/include/asm/jump_label.h b/arch/arm/include/asm/jump_label.h index e4eb54f6cd9f..a35aba7f548c 100644 --- a/arch/arm/include/asm/jump_label.h +++ b/arch/arm/include/asm/jump_label.h @@ -9,13 +9,17 @@ #define JUMP_LABEL_NOP_SIZE 4 +/* This macro is also expanded on the Rust side. */ +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + "1:\n\t" \ + WASM(nop) "\n\t" \ + ".pushsection __jump_table, \"aw\"\n\t" \ + ".word 1b, " label ", " key "\n\t" \ + ".popsection\n\t" \ + static __always_inline bool arch_static_branch(struct static_key *key, bool branch) { - asm goto("1:\n\t" - WASM(nop) "\n\t" - ".pushsection __jump_table, \"aw\"\n\t" - ".word 1b, %l[l_yes], %c0\n\t" - ".popsection\n\t" + asm goto(ARCH_STATIC_BRANCH_ASM("%c0", "%l[l_yes]") : : "i" (&((char *)key)[branch]) : : l_yes); return false; diff --git a/arch/arm64/include/asm/jump_label.h b/arch/arm64/include/asm/jump_label.h index a0a5bbae7229..424ed421cd97 100644 --- a/arch/arm64/include/asm/jump_label.h +++ b/arch/arm64/include/asm/jump_label.h @@ -19,10 +19,14 @@ #define JUMP_TABLE_ENTRY(key, label) \ ".pushsection __jump_table, \"aw\"\n\t" \ ".align 3\n\t" \ - ".long 1b - ., %l["#label"] - .\n\t" \ - ".quad %c0 - .\n\t" \ - ".popsection\n\t" \ - : : "i"(key) : : label + ".long 1b - ., " label " - .\n\t" \ + ".quad " key " - .\n\t" \ + ".popsection\n\t" + +/* This macro is also expanded on the Rust side. */ +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + "1: nop\n\t" \ + JUMP_TABLE_ENTRY(key, label) static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch) @@ -30,8 +34,8 @@ static __always_inline bool arch_static_branch(struct static_key * const key, char *k = &((char *)key)[branch]; asm goto( - "1: nop \n\t" - JUMP_TABLE_ENTRY(k, l_yes) + ARCH_STATIC_BRANCH_ASM("%c0", "%l[l_yes]") + : : "i"(k) : : l_yes ); return false; @@ -43,9 +47,11 @@ static __always_inline bool arch_static_branch_jump(struct static_key * const ke const bool branch) { char *k = &((char *)key)[branch]; + asm goto( "1: b %l[l_yes] \n\t" - JUMP_TABLE_ENTRY(k, l_yes) + JUMP_TABLE_ENTRY("%c0", "%l[l_yes]") + : : "i"(k) : : l_yes ); return false; l_yes: diff --git a/arch/loongarch/include/asm/jump_label.h b/arch/loongarch/include/asm/jump_label.h index 29acfe3de3fa..8a924bd69d19 100644 --- a/arch/loongarch/include/asm/jump_label.h +++ b/arch/loongarch/include/asm/jump_label.h @@ -13,18 +13,22 @@ #define JUMP_LABEL_NOP_SIZE 4 -#define JUMP_TABLE_ENTRY \ +/* This macro is also expanded on the Rust side. */ +#define JUMP_TABLE_ENTRY(key, label) \ ".pushsection __jump_table, \"aw\" \n\t" \ ".align 3 \n\t" \ - ".long 1b - ., %l[l_yes] - . \n\t" \ - ".quad %0 - . \n\t" \ + ".long 1b - ., " label " - . \n\t" \ + ".quad " key " - . \n\t" \ ".popsection \n\t" +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + "1: nop \n\t" \ + JUMP_TABLE_ENTRY(key, label) + static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch) { asm goto( - "1: nop \n\t" - JUMP_TABLE_ENTRY + ARCH_STATIC_BRANCH_ASM("%0", "%l[l_yes]") : : "i"(&((char *)key)[branch]) : : l_yes); return false; @@ -37,7 +41,7 @@ static __always_inline bool arch_static_branch_jump(struct static_key * const ke { asm goto( "1: b %l[l_yes] \n\t" - JUMP_TABLE_ENTRY + JUMP_TABLE_ENTRY("%0", "%l[l_yes]") : : "i"(&((char *)key)[branch]) : : l_yes); return false; diff --git a/arch/riscv/include/asm/jump_label.h b/arch/riscv/include/asm/jump_label.h index 1c768d02bd0c..87a71cc6d146 100644 --- a/arch/riscv/include/asm/jump_label.h +++ b/arch/riscv/include/asm/jump_label.h @@ -16,21 +16,28 @@ #define JUMP_LABEL_NOP_SIZE 4 +#define JUMP_TABLE_ENTRY(key, label) \ + ".pushsection __jump_table, \"aw\" \n\t" \ + ".align " RISCV_LGPTR " \n\t" \ + ".long 1b - ., " label " - . \n\t" \ + "" RISCV_PTR " " key " - . \n\t" \ + ".popsection \n\t" + +/* This macro is also expanded on the Rust side. */ +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + " .align 2 \n\t" \ + " .option push \n\t" \ + " .option norelax \n\t" \ + " .option norvc \n\t" \ + "1: nop \n\t" \ + " .option pop \n\t" \ + JUMP_TABLE_ENTRY(key, label) + static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch) { asm goto( - " .align 2 \n\t" - " .option push \n\t" - " .option norelax \n\t" - " .option norvc \n\t" - "1: nop \n\t" - " .option pop \n\t" - " .pushsection __jump_table, \"aw\" \n\t" - " .align " RISCV_LGPTR " \n\t" - " .long 1b - ., %l[label] - . \n\t" - " " RISCV_PTR " %0 - . \n\t" - " .popsection \n\t" + ARCH_STATIC_BRANCH_ASM("%0", "%l[label]") : : "i"(&((char *)key)[branch]) : : label); return false; @@ -38,21 +45,20 @@ static __always_inline bool arch_static_branch(struct static_key * const key, return true; } +#define ARCH_STATIC_BRANCH_JUMP_ASM(key, label) \ + " .align 2 \n\t" \ + " .option push \n\t" \ + " .option norelax \n\t" \ + " .option norvc \n\t" \ + "1: j " label " \n\t" \ + " .option pop \n\t" \ + JUMP_TABLE_ENTRY(key, label) + static __always_inline bool arch_static_branch_jump(struct static_key * const key, const bool branch) { asm goto( - " .align 2 \n\t" - " .option push \n\t" - " .option norelax \n\t" - " .option norvc \n\t" - "1: j %l[label] \n\t" - " .option pop \n\t" - " .pushsection __jump_table, \"aw\" \n\t" - " .align " RISCV_LGPTR " \n\t" - " .long 1b - ., %l[label] - . \n\t" - " " RISCV_PTR " %0 - . \n\t" - " .popsection \n\t" + ARCH_STATIC_BRANCH_JUMP_ASM("%0", "%l[label]") : : "i"(&((char *)key)[branch]) : : label); return false; diff --git a/arch/x86/include/asm/jump_label.h b/arch/x86/include/asm/jump_label.h index cbbef32517f0..fb79fa1cf70a 100644 --- a/arch/x86/include/asm/jump_label.h +++ b/arch/x86/include/asm/jump_label.h @@ -12,49 +12,41 @@ #include #include -#define JUMP_TABLE_ENTRY \ +#define JUMP_TABLE_ENTRY(key, label) \ ".pushsection __jump_table, \"aw\" \n\t" \ _ASM_ALIGN "\n\t" \ ".long 1b - . \n\t" \ - ".long %l[l_yes] - . \n\t" \ - _ASM_PTR "%c0 + %c1 - .\n\t" \ + ".long " label " - . \n\t" \ + _ASM_PTR " " key " - . \n\t" \ ".popsection \n\t" +/* This macro is also expanded on the Rust side. */ #ifdef CONFIG_HAVE_JUMP_LABEL_HACK - -static __always_inline bool arch_static_branch(struct static_key *key, bool branch) -{ - asm goto("1:" - "jmp %l[l_yes] # objtool NOPs this \n\t" - JUMP_TABLE_ENTRY - : : "i" (key), "i" (2 | branch) : : l_yes); - - return false; -l_yes: - return true; -} - +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + "1: jmp " label " # objtool NOPs this \n\t" \ + JUMP_TABLE_ENTRY(key, label) #else /* !CONFIG_HAVE_JUMP_LABEL_HACK */ +#define ARCH_STATIC_BRANCH_ASM(key, label) \ + "1: .byte " __stringify(BYTES_NOP5) "\n\t" \ + JUMP_TABLE_ENTRY(key, label) +#endif /* CONFIG_HAVE_JUMP_LABEL_HACK */ static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch) { - asm goto("1:" - ".byte " __stringify(BYTES_NOP5) "\n\t" - JUMP_TABLE_ENTRY - : : "i" (key), "i" (branch) : : l_yes); + int hack_bit = IS_ENABLED(CONFIG_HAVE_JUMP_LABEL_HACK) ? 2 : 0; + asm goto(ARCH_STATIC_BRANCH_ASM("%c0 + %c1", "%l[l_yes]") + : : "i" (key), "i" (hack_bit | branch) : : l_yes); return false; l_yes: return true; } -#endif /* CONFIG_HAVE_JUMP_LABEL_HACK */ - static __always_inline bool arch_static_branch_jump(struct static_key * const key, const bool branch) { asm goto("1:" "jmp %l[l_yes]\n\t" - JUMP_TABLE_ENTRY + JUMP_TABLE_ENTRY("%c0 + %c1", "%l[l_yes]") : : "i" (key), "i" (branch) : : l_yes); return false; From patchwork Thu Aug 22 12:04:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13773283 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E8880C52D7C for ; Thu, 22 Aug 2024 12:05:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=k9QeVRE06UlLzdvgNPk47G98p/UKYTNmCDVENlHD/us=; b=o6ApljHSjPiSYqgL6jJqxdG7Y5 Vne2uKrQiafRBI8d6YCAdZgpjrtIHu2ccIyRLm/hMF7qohnI/0hoB8MyzvfMFRf0AaCBov++PDpPL eP/TiPGQ65h038o3HZ/RwMnYacTleEgZDBQo28UIDakrMB5IFq7c7Bz4ZVrCob/K23TARsPZXZGUH MGm4X0TRi3+MXSjKTk81oM+EW/dQfIvYGlpD7b7juiEBgnUZJh0hxQWp9YR+dWfti4+Rmpf87F9g0 GjAumaPloxDaCuKtFNnS9VC2u3PfO/olmj41WLDhksAwuvAdsfS8RefPWxhbORr22Tfl+tXejJpRn ZMQ8LT4A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Z7-0000000ChY0-3W6M; Thu, 22 Aug 2024 12:05:21 +0000 Received: from mail-yw1-f201.google.com ([209.85.128.201]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sh6Yr-0000000ChKx-3VMj for linux-riscv@lists.infradead.org; Thu, 22 Aug 2024 12:05:12 +0000 Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6643016423fso15392977b3.3 for ; Thu, 22 Aug 2024 05:05:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724328303; x=1724933103; darn=lists.infradead.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=uxEAzZMzJr3y8oSKmglTMtntRHzNRdKVl0l9n/9vlWE=; b=dA9GGcofqAW8JdzJjaicp9W4L7zrhKbQ03tWddj8SLKC+zQpf+9AzgVDoaO6zRKbJX f8uFfMIPO8sfnolRdGQz3TSXdGrZhlVbmik/vwtCtfamaMko/D3J8BZMgttsx2qZXdbO xEsA+e/OcKBQWX1SxiilNKhnJWLbURSnPoe30GQSAQK8beUdStS9ltH/aasudPQ0a5z4 bg8VTGuQsT3ZekQ88KZYbzITJKFrfnBAI46jGxxlCIMVWy1AbHuQVF0blYuCLMDBzDnq N9CDDDKYZ8rn1qNuUmfOWBKTz+vTSJHp40BIFMeQ79r2t35fkA25vPtLDR7AJ17aQCTR IVmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724328303; x=1724933103; 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=uxEAzZMzJr3y8oSKmglTMtntRHzNRdKVl0l9n/9vlWE=; b=s7PZLqS3wxRkBmQuTq+FO0Zw8PBNOQ/J8NzrM2+RFv53m4R/ZZU/HIarlTNhpAXsSR 3LNTuAzxex6CCw/qzTbKBOzZ7480LykLuw5zDfOeSAcbffObvX8Lh2DWDxSFPG5n3YsV unZF+vA/QW2jMBoocI6ys39swi9WXkkcZAcUPV2du9Lv01ZzI5ctUS0hJiuO0PqMthPT +jCWfnxaq+oBcDQr2eorsXvMbOiXGyJYHwrk/c+9NZVnfBbeH45e/3S2WKfqN8BF4Yt/ WN58Xq47CMRCzn1IbThzeuXYWoxxC9CRzZGd2a5xA5vhNipaxwaVOc8YO8WpGriiZd9F S2Qg== X-Forwarded-Encrypted: i=1; AJvYcCX8WbM/3Ni4bAThQlzZUL2BDTojMNKMFyGYROikw5sVHuKE9A/KUBKS6OT6WtSEFVGef1xGVZ/ZgMXZ5A==@lists.infradead.org X-Gm-Message-State: AOJu0YyvE1GXkdIhi/RY1PEjI+hfPBqtuIP+0lpe5PlPMOfufT+q7JxP OF07mTyQtvaju2zPdbq5OifkTOpQV/AJq0lh7qVZ33fL3K6SNq9ql6/qwpuZAa4dqqIoB4c9QKH 6zf7h7U+dP4t0Lw== X-Google-Smtp-Source: AGHT+IGiVlbAew1+icwXyROm+LUwKLbHv3EgJ+ELhtPSIkQV+jPl3FGLr+njU0tLgnmjraKSgCDY+G6v6Qi4DsI= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:4813:b0:6be:9d4a:f097 with SMTP id 00721157ae682-6c0a0236c74mr298177b3.7.1724328302925; Thu, 22 Aug 2024 05:05:02 -0700 (PDT) Date: Thu, 22 Aug 2024 12:04:17 +0000 In-Reply-To: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> Mime-Version: 1.0 References: <20240822-tracepoint-v8-0-f0c5899e6fd3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=8506; i=aliceryhl@google.com; h=from:subject:message-id; bh=od1Acum7ZP3SHNuZV1Frd7Px5i5rAfWdJgysjyMhaoo=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmxyleWMqafxpffJQSjGJQbTSeoPMOs5rqYzOKA h7koJQoGQSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZscpXgAKCRAEWL7uWMY5 RshoD/9FmNAsAln8K8mfbgINydaroarjZhGmYp77z+tBgBU+vnZq3W+3YIcpqwRKHl8Gi3mFOVb OW5B9NMhyyhupwWgDELW5cgPZPUAT00yM0kK+sIVmpV9cxS3lbtT+5thF+ONcWTby43Cywgc6DX Dv2xq3ZdUBFAiUcVz1wy8EQHE8llFacx/qswJVcnRAjlJQKb26/jKadb1BpBYh0SA9yBFxzyHso NZHN/bTSJJ7VR8QjiTT1Y59WzjhkjR3/B3JcgHs8TAnOGP/kdDuldwkkhveUQBkwLNI0zpthfQs mstX0wkLGbi0K7xXR75OHisSb98lx6kM48LbFLpjM3+ZL+tGcqNtOOTubwbHza6j4GBuzDjots7 i+gHzc80Q9z/Sal6mV/oQ8tgOOTBDldQXlaLbxLTG1oFfgRKSXRoYaFowmf0myQZiXSa+nWDfJs E/JYPRErb801FL/Jfu+42W89+vTpAHYEC8UW1uiVKXgONBT1Z3BMYWxInklTnTjGC6AWB38SmMV Surujt2TeSla20M8LkJfsq8mjId+E8sGO3/Fij807tf/blvZ5Kdh1kDpGRyHqELlLutojpxRaja geMR+csmMbMbDT3SggRsWo0zAG2mCRSXS1PXSJJoKpyvfmATpigqjksoCY3eK0alhr4mQL5zNrk Gn0aY4b7TsoYfLw== X-Mailer: b4 0.13.0 Message-ID: <20240822-tracepoint-v8-5-f0c5899e6fd3@google.com> Subject: [PATCH v8 5/5] rust: add arch_static_branch 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, Arnd Bergmann , linux-arch@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Sean Christopherson , Uros Bizjak , Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , Mark Rutland , Ryan Roberts , Fuad Tabba , linux-arm-kernel@lists.infradead.org, Paul Walmsley , Palmer Dabbelt , Albert Ou , Anup Patel , Andrew Jones , Alexandre Ghiti , Conor Dooley , Samuel Holland , linux-riscv@lists.infradead.org, Huacai Chen , WANG Xuerui , Bibo Mao , Tiezhu Yang , Andrew Morton , Tianrui Zhao , loongarch@lists.linux.dev, Alice Ryhl X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240822_050506_070525_C688991E X-CRM114-Status: GOOD ( 25.03 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org To allow the Rust implementation of static_key_false to use runtime code patching instead of the generic implementation, pull in the relevant inline assembly from the jump_label.h header by running the C preprocessor on a .rs.S file. Build rules are added for .rs.S files. Since the relevant inline asm has been adjusted to export the inline asm via the ARCH_STATIC_BRANCH_ASM macro in a consistent way, the Rust side does not need architecture specific code to pull in the asm. It is not possible to use the existing C implementation of arch_static_branch via a Rust helper because it passes the argument `key` to inline assembly as an 'i' parameter. 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. Suggested-by: Peter Zijlstra (Intel) Co-developed-by: Miguel Ojeda Signed-off-by: Miguel Ojeda Signed-off-by: Alice Ryhl --- rust/Makefile | 5 ++- rust/kernel/.gitignore | 3 ++ rust/kernel/arch_static_branch_asm.rs.S | 7 ++++ rust/kernel/jump_label.rs | 64 ++++++++++++++++++++++++++++++++- rust/kernel/lib.rs | 35 ++++++++++++++++++ scripts/Makefile.build | 9 ++++- 6 files changed, 120 insertions(+), 3 deletions(-) diff --git a/rust/Makefile b/rust/Makefile index 043d8737b430..27da24d90b0c 100644 --- a/rust/Makefile +++ b/rust/Makefile @@ -36,6 +36,8 @@ always-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.c obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated.o obj-$(CONFIG_RUST_KERNEL_DOCTESTS) += doctests_kernel_generated_kunit.o +always-$(subst y,$(CONFIG_RUST),$(CONFIG_JUMP_LABEL)) += kernel/arch_static_branch_asm.rs + # Avoids running `$(RUSTC)` for the sysroot when it may not be available. ifdef CONFIG_RUST @@ -409,7 +411,8 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \ $(obj)/kernel.o: private rustc_target_flags = --extern alloc \ --extern build_error --extern macros --extern bindings --extern uapi $(obj)/kernel.o: $(src)/kernel/lib.rs $(obj)/alloc.o $(obj)/build_error.o \ - $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o FORCE + $(obj)/libmacros.so $(obj)/bindings.o $(obj)/uapi.o \ + $(obj)/kernel/arch_static_branch_asm.rs FORCE +$(call if_changed_rule,rustc_library) endif # CONFIG_RUST diff --git a/rust/kernel/.gitignore b/rust/kernel/.gitignore new file mode 100644 index 000000000000..d082731007c6 --- /dev/null +++ b/rust/kernel/.gitignore @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0 + +/arch_static_branch_asm.rs diff --git a/rust/kernel/arch_static_branch_asm.rs.S b/rust/kernel/arch_static_branch_asm.rs.S new file mode 100644 index 000000000000..9e373d4f7567 --- /dev/null +++ b/rust/kernel/arch_static_branch_asm.rs.S @@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +// Cut here. + +::kernel::concat_literals!(ARCH_STATIC_BRANCH_ASM("{symb} + {off} + {branch}", "{l_yes}")) diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs index 011e1fc1d19a..ccfd20589c21 100644 --- a/rust/kernel/jump_label.rs +++ b/rust/kernel/jump_label.rs @@ -23,7 +23,69 @@ macro_rules! static_key_false { let _key: *const $keytyp = ::core::ptr::addr_of!($key); let _key: *const $crate::bindings::static_key = ::core::ptr::addr_of!((*_key).$field); - $crate::bindings::static_key_count(_key.cast_mut()) > 0 + #[cfg(not(CONFIG_JUMP_LABEL))] + { + $crate::bindings::static_key_count(_key.cast_mut()) > 0 + } + + #[cfg(CONFIG_JUMP_LABEL)] + $crate::jump_label::arch_static_branch! { $key, $keytyp, $field, false } }}; } pub use static_key_false; + +/// Assert that the assembly block evaluates to a string literal. +#[cfg(CONFIG_JUMP_LABEL)] +const _: &str = include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs")); + +#[macro_export] +#[doc(hidden)] +#[cfg(CONFIG_JUMP_LABEL)] +#[cfg(not(CONFIG_HAVE_JUMP_LABEL_HACK))] +macro_rules! arch_static_branch { + ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: { + $crate::asm!( + include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs")); + l_yes = label { + break 'my_label true; + }, + symb = sym $key, + off = const ::core::mem::offset_of!($keytyp, $field), + branch = const $crate::jump_label::bool_to_int($branch), + ); + + break 'my_label false; + }}; +} + +#[macro_export] +#[doc(hidden)] +#[cfg(CONFIG_JUMP_LABEL)] +#[cfg(CONFIG_HAVE_JUMP_LABEL_HACK)] +macro_rules! arch_static_branch { + ($key:path, $keytyp:ty, $field:ident, $branch:expr) => {'my_label: { + $crate::asm!( + include!(concat!(env!("OBJTREE"), "/rust/kernel/arch_static_branch_asm.rs")); + l_yes = label { + break 'my_label true; + }, + symb = sym $key, + off = const ::core::mem::offset_of!($keytyp, $field), + branch = const 2 | $crate::jump_label::bool_to_int($branch), + ); + + break 'my_label false; + }}; +} + +#[cfg(CONFIG_JUMP_LABEL)] +pub use arch_static_branch; + +/// A helper used by inline assembly to pass a boolean to as a `const` parameter. +/// +/// Using this function instead of a cast lets you assert that the input is a boolean, and not some +/// other type that can also be cast to an integer. +#[doc(hidden)] +pub const fn bool_to_int(b: bool) -> i32 { + b as i32 +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index d00a44b000b6..c912124b5e6b 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -145,3 +145,38 @@ macro_rules! container_of { ptr.sub(offset) as *const $type }} } + +/// Helper for `.rs.S` files. +#[doc(hidden)] +#[macro_export] +macro_rules! concat_literals { + ($( $asm:literal )* ) => { + ::core::concat!($($asm),*) + }; +} + +/// Wrapper around `asm!` configured for use in the kernel. +/// +/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` +/// syntax. +// For x86, `asm!` uses intel syntax by default, but we want to use at&t syntax in the kernel. +#[cfg(target_arch = "x86_64")] +#[macro_export] +macro_rules! asm { + ($($asm:expr),* ; $($rest:tt)*) => { + ::core::arch::asm!( $($asm)*, options(att_syntax), $($rest)* ) + }; +} + +/// Wrapper around `asm!` configured for use in the kernel. +/// +/// Uses a semicolon to avoid parsing ambiguities, even though this does not match native `asm!` +/// syntax. +// For non-x86 arches we just pass through to `asm!`. +#[cfg(not(target_arch = "x86_64"))] +#[macro_export] +macro_rules! asm { + ($($asm:expr),* ; $($rest:tt)*) => { + ::core::arch::asm!( $($asm)*, $($rest)* ) + }; +} diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 72b1232b1f7d..79dde37621e4 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -263,12 +263,13 @@ $(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 # modules case. rust_common_cmd = \ + OBJTREE=$(abspath $(objtree)) \ RUST_MODFILE=$(modfile) $(RUSTC_OR_CLIPPY) $(rust_flags) \ -Zallow-features=$(rust_allowed_features) \ -Zcrate-attr=no_std \ @@ -318,6 +319,12 @@ quiet_cmd_rustc_ll_rs = $(RUSTC_OR_CLIPPY_QUIET) $(quiet_modtag) $@ $(obj)/%.ll: $(obj)/%.rs FORCE +$(call if_changed_dep,rustc_ll_rs) +quiet_cmd_rustc_rs_rs_S = RSCPP $(quiet_modtag) $@ + cmd_rustc_rs_rs_S = $(CPP) $(c_flags) -xc -C -P $< | sed '1,/^\/\/ Cut here.$$/d' >$@ + +$(obj)/%.rs: $(obj)/%.rs.S FORCE + +$(call if_changed_dep,rustc_rs_rs_S) + # Compile assembler sources (.S) # ---------------------------------------------------------------------------