From patchwork Tue Oct 1 13:29:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13817981 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (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 196E61C2DAB for ; Tue, 1 Oct 2024 13:30:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789435; cv=none; b=ctGRMTdn0hrZXibIFXzmIQXUTGHAjOShBjf3E+YM/qj6wTAqTjQuGK3ttq9oiKnLnFMCv5BMgjx6IloWin0Jx2ogav/qB+bWbHmlW0XJ1nJXFp1gxYNSWd/4ZsiRllfGQi4UH+KVXcJ0Hu8NYFGFVL4sP8fjGZ3fQI27bckXZfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789435; c=relaxed/simple; bh=AvPTYbgUDavV8mVnKHm16jwbLov+IgYYrRjsKEF8/Ko=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sOrPJPMlYnxd7DrDsOwJZXmqhvqoujcnuu+qm/LUlZdc9mmnWw2b4fak7on6xkwVLddFW8HqmtGaGKSs6Ise0a17eTM/zx4axc0MvpCXK9QJeudFlyLabWSWJp2X77NGLEjr4N97XrrDpKCX2odS8zwxAUCDXhBD0jsSuswmxuw= 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=Rw/PKBQq; arc=none smtp.client-ip=209.85.221.74 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="Rw/PKBQq" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-37ce6262705so895598f8f.1 for ; Tue, 01 Oct 2024 06:30:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727789432; x=1728394232; 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=6NcH/txgm4SoZvRl8jrqK6AfP/r/sFmOakhN5fEYqYs=; b=Rw/PKBQqi72GbqSGoeqoXtUZaOsVnb7VZt2wFCjDJpQPn1f/Yjjptqd2FpGNmHt51b zukzhB6RH0hKHgixjjctCCPW9IKuJSCg4zkm4knyfzLvcBHpA97tLE7jxlNa6Ysjnl59 P+Y/7TkFWprEyemaCEYeYLtH6XJu8/cvqHcGItLGolrRC1SH3IXYq07ApSVBxY2aZOJA LAELeBu+yqDxnX/jeto+303SdMIurhcZ4QavA5ZPqwiBtsnLKKVkd6Lpwjj/An1DOaIo SvyxB+x1XGzJSrLtBZLiBO7LwQeziqndiSG/+TBPf43OVJynWDBwoekBEadkB0jhH/ZJ Mq8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727789432; x=1728394232; 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=6NcH/txgm4SoZvRl8jrqK6AfP/r/sFmOakhN5fEYqYs=; b=MGET6VDDPbhZq1eXzmZFBTCPuhoqhnSGKUx5YMdG1qDg+M5fl0mYiDfAQvHeymEVX0 EHwONn+8IA1kgqAngILgA964N8LJgbzidSwq1T+htUAgFIahkYpU9knoV7a82lbnXBPM Svz8Mr+95vRIDs6LhNUfXoo9yFtaa9c4upxkOXTKBapacSCPXECOOQy5zYQ89VWVXmyL W8ON5bB5fsAgEh1RXRzW9/t5gFkqLP4nPXlc+n8e6ftZWyoaS4q6wPF3POiziM4n2CuC VyrR0+SyLQ2L6X+JU6DeC6FDiNnW4qDDJewr88m6yZ9KL+tZ7y9FA8mlyxMXef+DZU9s 51Tg== X-Gm-Message-State: AOJu0YyDtNl2BTVqwtuCMpD6yfTWOgxkvwo03ZIgE8un8b2q6OyllfWa R1Ek5qdZznUwOLsZCXzw5tpvr7ueOJiyDC1qpDbsQxTnAmYOx2CIKuGb23aosxz/ZsYCff9N6ps IHCDANwcNu21vkQ== X-Google-Smtp-Source: AGHT+IHEHR04HLA0/xTKtqy65DSu4rhuHv96KunVk64O/ZW/4J8kBgg9MneRCFsC6zfzdrsaOIeGwwArA+9MwtA= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:6000:e48:b0:37c:d2ed:c51a with SMTP id ffacd0b85a97d-37cd5a9ee86mr8842f8f.5.1727789432081; Tue, 01 Oct 2024 06:30:32 -0700 (PDT) Date: Tue, 01 Oct 2024 13:29:58 +0000 In-Reply-To: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3861; i=aliceryhl@google.com; h=from:subject:message-id; bh=AvPTYbgUDavV8mVnKHm16jwbLov+IgYYrRjsKEF8/Ko=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBm+/lwNcJAULBaBeL+Dr8YX6BXARPW8IdrfPDru oHV4BLfmr6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZvv5cAAKCRAEWL7uWMY5 RrXoD/46qHxmQQefR6J6dxs+nMxE/PeCwUzN+iBASNF6GRFzW+qtSy6GbpoUkBk6lPtMPXIZ21+ 7vYAkFVr51pxI/ktD9Q/9G8vTic38vSwXmAw135cEzOw4quHQWf1CcMO8iaNvtmYwwX23i4p90K 7b++yxkkUjapjpbwOMqLk33VOnYXWilNEXsK2owifqR8LngjI5Ybp5rmPx/JbOJaPnog5WGamGz 8vcavGHdPBhs6IxJdmWGacvrmJWdrBe5a5mHtnKnPoAcwtrkGjh1K1xeimz2ARL5bdIUaNTFQeU g33eq/B1bfg9vw3Tn/JLEheM10xngvB1fCfiAFRbRISYHdwywqjZSpz4/RrFVCfMWajI+c0aQ2X Nv1IdKug4kbpyOdrRJs4ZX4KQTVfK3c/ERGMvUmb1BLYpmyyWV+pgrLZ3g5oxw8tEqL41msxdRn rvgFmIp86sYIGFicKbcfzS7pD7DlCREeL00e4mGdetCl2lTUHy31MDOkBY13fi5Yx+G9W9IHdBW OsRW4Tt7wHJlIO8mhtb/JxNTSXdLw5OlZSjC4Vvil4u101yVHeMAtrjGcr99GphInredXfMWI6V WCfcFx7E6cIEpP3GMqvqRWlpH4etvET/oIetiWYjQgwYiKCZl6i0zVMvIZnp49cR9RiKj+ft9Vq XJjgUh3pWF492BQ== X-Mailer: b4 0.13.0 Message-ID: <20241001-tracepoint-v9-1-1ad3b7d78acb@google.com> Subject: [PATCH v9 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 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. Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl Signed-off-by: Josh Poimboeuf --- rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/jump_label.c | 15 +++++++++++++++ rust/kernel/jump_label.rs | 29 +++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 5 files changed, 47 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index ae82e9c941af..e0846e7e93e6 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/helpers.c b/rust/helpers/helpers.c index 30f40149f3a9..17e1b60d178f 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -12,6 +12,7 @@ #include "build_assert.c" #include "build_bug.c" #include "err.c" +#include "jump_label.c" #include "kunit.c" #include "mutex.c" #include "page.c" diff --git a/rust/helpers/jump_label.c b/rust/helpers/jump_label.c new file mode 100644 index 000000000000..0e9ed15903f6 --- /dev/null +++ b/rust/helpers/jump_label.c @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* + * Copyright (C) 2024 Google LLC. + */ + +#include + +#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 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 22a3bfa5a9e9..6dfafa69a84e 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; pub mod list; From patchwork Tue Oct 1 13:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13817982 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 680771C4622 for ; Tue, 1 Oct 2024 13:30:36 +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=1727789439; cv=none; b=KPad6w4yEd3A6/PRJKhJ7DJSMqYyLzsZVGSLZcYOY7W2ptoKVBvnJtRZaxpClAh1sv9jj967c0Lgu3j1+zcagTtVkZcnA7Yp/kIV+6KbQu+E4pefqDHq63sxTUfp32vQR4SESoEn6FSOWNfbRVkDinRchHjNlKi44jrwHvdY5qM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789439; c=relaxed/simple; bh=QAR3+Uf8S8g4ti0ixSbviLuiwMUo5f4YoL+bKjncXao=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WZPNU3L4pfyv/4yXUDZDAaxQ4UuddNiG/gCRdeZLTcTP/2pNL+CmxjqX7RWMnh85TxFU7jQPIP618YCRfJqu0VZtRjEESFmZ0inAz7IU5wu4eCBv3uO9TeqE3q3FLYvHqJ4SIpoB7SBfpNx9EUtwQQL4FTe3bg8uWkPW8hXtrCc= 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=iqOPgDmD; 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="iqOPgDmD" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e28b624bfcso21873597b3.2 for ; Tue, 01 Oct 2024 06:30:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727789435; x=1728394235; 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=vHT7RCQWh9VJMF8oIXuBP7+2LpYWya7O6BZRCiI/HOc=; b=iqOPgDmDozx4UCSBbSRSKuahjVjNUKuTA3S0oUBSAPUjw3ttXO4Kpc7MGx0p8HEb6Y Puf28Cn5PKe90esTQ8MH7PdP3c337wlO78QqdtdeTkDQHuw4C8SjG4MHrgBr+Vxxyl8U KasTwMCiyKfBD4MugmLbJCWA/ir/SRMFd0iS44D8qi46s12imVe5tRm8BRokYaJhU9cw nd/G2kstMD8jmo9anl5+2bmDcmlBTwwInT86BAdbVDE6sROlxGEeksCOnazuwQ1eV3G6 1x6V5r1B5XfC5gDdbhQc4ernWT+SGbbLYBDB9uWdpMCUU46g17QVw6GXIBrStxkHLwUp lzhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727789435; x=1728394235; 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=vHT7RCQWh9VJMF8oIXuBP7+2LpYWya7O6BZRCiI/HOc=; b=egl7HZMI+HnWXA9X+uaArp+XEUeHQV7WCi6QyA4ZcXSC+6Ae3MX80Ph6x+PPhgBVa7 kuNCpk3IOmBa+tayRGCeIAOMt3D4SXDVIs7qtg6Xb5gsrwL7c33/R3CFCwiE7c9a7rwa 9G2FbWPUa9dhVi2ezXvT02fnJjXb91tKyNtSBUSm47Y+fTYxaiuojCZcAmildJi3hI99 ypjCXDFi0MQNNNju6BDAMP+Xt04i7CYUKhqHuZcLcFXRj8bfZcSozmm0fvrw5I0YE3n3 TIXJxG+Bqjol1wTYGdJGs6cuLF9Ov4JlcHnaWg+fP2Cy1PLUD4O9iERw9IsNzNDGuLlp VHVA== X-Gm-Message-State: AOJu0YyjYIbHC83l6An1viVNNiWLj2tTnXxzs/dOV1BUjBI4ia49nPav F6wPvBVqnvwxCQK9omluD2gmqX9+suMR1/2Eayqfd/6QZRutuibETpFn/CXFjN5xx6j+K8vGXFW Q+veM+bXUy8IVOA== X-Google-Smtp-Source: AGHT+IHkTUHVo8ojrSDPAhDaM/l29E/UDPZXNOXZHUGDN5vnnYr5okPT46k0myzDL6E14UX9F+O4B3eF5tiFWOE= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a81:e349:0:b0:61c:89a4:dd5f with SMTP id 00721157ae682-6e2473994camr609407b3.0.1727789435216; Tue, 01 Oct 2024 06:30:35 -0700 (PDT) Date: Tue, 01 Oct 2024 13:29:59 +0000 In-Reply-To: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6914; i=aliceryhl@google.com; h=from:subject:message-id; bh=QAR3+Uf8S8g4ti0ixSbviLuiwMUo5f4YoL+bKjncXao=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBm+/lwLLpJIPmK9F6nIFaq3qeHRS41Xthj8IwUL +dYfwevd5iJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZvv5cAAKCRAEWL7uWMY5 RnHND/0e5/swnUVhsY9o7C6VXyx98GKYvW6h/sOOvEpHwRdKaw7MsuphOCFBlp3GridSl2CdREw kp/PCwtrSdI6P4EfmhKrH1U+QG6MJA12aikTzwudkUJxgWL2jyadrWeeUpRuafUBS4oshCCbVAR 56k1blE8NbJ4OJIttcUHPn2QkleaQdVFQNlZtzLRbtGdXUPMQvARAJENhTDyFqjFrSFQ5Nw/1md ZbD6ky8sXA1sGw0ZKaMOlxcaB7UM7oKYmxP0rg3JEtuMyVPRGTrxkJqWZUZhL9bCCQSRfWvxwuK PC2/p7aRxTIBBqTRYRKOIE1FhTqDXe+cbzeY5awE4vH+8ybFrIqkJ4HAyc7o1+SoqDqnOEU72tE OG6obmjo9io0JpBu/CFoyxUimdW1TVmPWanZY2koIXdKJczPx1y+xn6d8MKqcbdfKSE8IXdtpG6 igaZwliGMBU9wxPeRJ2NAlqCcH/oIaODqDVLCz+IM0jbkAuTnLtqpjyR+2R3EgtaC7cjVnpZDJl c6pK13s+xQNGHg3LmwKHj+oEvRGGG/G/iR7aMe9BcvzrXgeKqlAWuHZh+f9QEVRa/78GBgcOAP8 7Fg1csdkSMxBpFtgmhkmd1m61D9+nWPaZyBtreeMY/NNAtHZT0vYpal4VB7VBNDsxdQnKiAYd7M DCPx0zC4o9ThG/g== X-Mailer: b4 0.13.0 Message-ID: <20241001-tracepoint-v9-2-1ad3b7d78acb@google.com> Subject: [PATCH v9 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 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 Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl --- 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 93a9f3070b48..316cb046653d 100644 --- a/include/linux/tracepoint.h +++ b/include/linux/tracepoint.h @@ -257,6 +257,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 @@ -272,6 +284,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)) \ @@ -356,7 +375,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 e0846e7e93e6..752572e638a6 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 6dfafa69a84e..77610e19df96 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -54,6 +54,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 Tue Oct 1 13:30:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13817983 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 4ED8B2BD0E for ; Tue, 1 Oct 2024 13:30:39 +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=1727789440; cv=none; b=mXVskQUtEFI8pF8uktz41k+7fHeNk68Q/U/86Of5ZN2sbjU0PwBRvJBo1jxKRGNVgiOoisU+nhFNTWqBwl9aQ5lpFOEUb0btsvMV4YiyUfkBwVCT+RHxXxCqvpTOaGaMBFVZ+mZC105G/E/31GQofx8HY8fwGOkYDShm3d9YJIs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789440; c=relaxed/simple; bh=tevkgj09OalAKZgQaXHqxVyJ12UyDG/afaB9IjNpmbE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NKf/7kpRjQmvueDI86G3s40i6Mcq4boFQ3SrsNzg74kT3zvRiNtiA1tjrya5et+MqdbBBllh2h5Kgwbz22gscDL6kkkHE7ssBAcFgbnef+FMTF+iBPqPSEu5gCMVKNFW9JqHahECm/RzhsaaeUruklp4QKKTg4+aW827EpwAL+M= 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=FmNZem5J; 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="FmNZem5J" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e2590f5bc1so48293547b3.2 for ; Tue, 01 Oct 2024 06:30:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727789438; x=1728394238; 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=uK9Ua6p4XY0W7oFZP1bhbr5r14FIpD3VJc8sLYS+srQ=; b=FmNZem5JxAatJFQPXTTC0CSVdqtnm6TFGh66U5fHhoMKw04qCFXUyCHdQEBq0Lgk2V 8NwqvVA/S5CkhY0EAEo7ozaRC5qr/zQso+un0ZvMi4l7SoW9SjlvObb50Iyq3Jk7cCaB s4aJBhEL+nqYgLGnKP5IKioZ+GOW1JyXJDVHM4eE5iWWk4ylKn5Nph1XP2hjnqe97v5F 6Y+5RrzvaXO44XYxoICDHu92ZUk48aCHzVFz9xcMMQqzhpolwsSQrzLCVInHnGIEdJBl GQnKrmKZmHRSmarHsbi6JA/mxjZGf+OeFwRZxGG7zIsk68eRB2UeSI0Pb4TBg7l2zzH3 tn6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727789438; x=1728394238; 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=uK9Ua6p4XY0W7oFZP1bhbr5r14FIpD3VJc8sLYS+srQ=; b=R2F0Nvlk5oA+Y7MECwWtdN3DHVGRpEfWZpT4EdxwNEVNsL6vf8Uum2xwD3wKFjqx8f cZXKqephmd4FNfBTTRhkM2ocERcmZzzO/fiQhHg8L9Mf4HS3l5SUgq9IwT+4565hT/pV usVsSCaxMcj377O17x+n1ujWabsEaQRa9lf7/qKs6xQXFRENV26tniTM+8B2SkAEM9Tf BaPs0f4zj5N5j4eZ6GN4stVCQNoHYsBY+kWQdOppCe6nbVxBy8uh2QzzztcQfkDHs04M jsNCYP9bUGWpbX43Ev8MreQr00omAuIBqPhhEQRZ6OOKtSiXvH5hW6vrkEkz52GLRXmu OSQA== X-Gm-Message-State: AOJu0YwHS1mkOmaeFuTAg4wF5x10WjsnsduHQTvUeHM6vfKVoTDTVJd6 ue4X1RNmP8caufXHBEepNsgS2BgKiDfaScddnU7T+rAqQAIwsmOZ0nWchqWQ1yjVC3bmYrHYTsZ FxPq+YP6xMwqgMg== X-Google-Smtp-Source: AGHT+IErH97ZXlD4tIoKA3thsCdHJH2G2pNUVlIr0AE5dq7niJnu55wTopZT9tfYYM/zS3Eb67ZCaVjEjZLfD4o= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:904:0:b0:e1a:97f7:3715 with SMTP id 3f1490d57ef6-e2604b6eea6mr13252276.6.1727789438182; Tue, 01 Oct 2024 06:30:38 -0700 (PDT) Date: Tue, 01 Oct 2024 13:30:00 +0000 In-Reply-To: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4317; i=aliceryhl@google.com; h=from:subject:message-id; bh=tevkgj09OalAKZgQaXHqxVyJ12UyDG/afaB9IjNpmbE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBm+/lxOJBJv7omOMsbp/Xd4tFgRoa50P5L5jXEg q/9H5708lmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZvv5cQAKCRAEWL7uWMY5 RqSlEACEKv5QTIw+nAQSvSSW6Kpws6OZ6oAyOuZA+duoWU53yOedIOmcS616kIBLvLOJqQ5VR/3 t4iFWYZkPFMl1gYEfQk9J6jbd20WdIip/QV0Zj/iqarYpZKWZZdfZ7xuhRBngrHVjpQyp+G//Cu 2flzn3x0yol5SxeMSw/zPDCImqoSDJxf5qO0ug0WcHdWK72vOf0U9UkUeTbB5/CbWT6JWP8xmH5 aSMWpJqml1bePjZEtR9hRE01LRD6DAYU55A6cwnsSVizAdgHrlIdcCUtNevexZPbfngWgSN5iIB lpclIa0KNxZO6XCIph0o/bWhuRxd8Y8z01nFoKbQFccYvcB5heYSMtcr1OOG32qkexg0I5cCmwd d9et3EwVRnUGnejQJcanzeN8ta+L/Vy/Ev8lyzRv23CKT94QfUac+Lkm+oOZHISIa6jAIfV+Dz2 Mf8xnV1UzrsouZ30D13Cs2rPafyQ7nIybwbxL5+zKuVzpYMw6zcJ+u7JIUVSt1krIjmIH3dKNZZ sSAQSapkweJqcjTOxOM4hVN4lc8A8pnWqQxUamYrkBQGa5tIlGwbhuLhM87svgn8rqwLfcLp7mQ 6itQZyogV0FJpRi7cRTONJ/G2htBlFNSSE00b9KRUppypTgwTZUn+yCGkTrq1/PhW0om84OGrWz a3fyXEthwW96hSg== X-Mailer: b4 0.13.0 Message-ID: <20241001-tracepoint-v9-3-1ad3b7d78acb@google.com> Subject: [PATCH v9 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 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. Reviewed-by: Boqun Feng Signed-off-by: Alice Ryhl --- 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 c27f3190737f..5995a8982130 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20219,6 +20219,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 752572e638a6..b072c197ce9e 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 Tue Oct 1 13:30:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13817984 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 B180A1C688F for ; Tue, 1 Oct 2024 13:30:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789444; cv=none; b=LGkg09XBu5pFVDulFc1rn3W4jN9BxsCc5rqdQbmXL5HRagVrBY2ooaYhBDzzLkMqVHwB9pDv3k9SvoaryEA8+jL5xTxCiQ3cdI8HbfQzIGVm9YQKnv2+VT1ZAf2GVs6nracnndcmF0s0HFyKpd95O1jxn6G66dftJuVfXhiO05M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789444; c=relaxed/simple; bh=sLUHlSyC2EqYvVOyetWqSigN1sJyjMhs3a8R4LFtn9I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kqu/nQyC/vt7cF1BjOqf4NrX+DPMmwLkaxyCw7C/izRe3Y93WOdspy+/oWgsL5SmL305mgLto0EIJZYxF+pTJFRsijFS+OryyypUu6tqmBhCKhxj/d41CGv6kbuqfXSoNR0NlTOI0d20qvI3EeRZhau227GQ6F7yxlFTDkonzZ0= 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=N2BUul3O; arc=none smtp.client-ip=209.85.219.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="N2BUul3O" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e0353b731b8so7958895276.2 for ; Tue, 01 Oct 2024 06:30:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727789442; x=1728394242; 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=olXUlrdi/nz0nOHZ/AvYtKp2YQWJmsFP4oPaErFpY5E=; b=N2BUul3Obh9uuXnDUQsPYAM3AH7TgKBwFZbhI2njHiXKMoBATZjBym+A052YXhrJZw 0oJZSI6bqEqF3pYZQ3kE6KNE27d+W64jHqPp4WCuPzkdhRTN4PXR5IwG1s3ERqhf6IMg E+tL4XTlXL3jmNCUk8whU9I2AC8xRqBY8EWoyNWyE6GtnKrbTZmsmVVDSEzKdhRIv0ER iWAF5ZCNTA5DpKVZMIwSXBiDzBHELbGIACJVL2ECZjbzk9TkwlMJfk03K8vvujh88PrM rCfpCugLV1iygodEwtTN/EHcJta7n/0I9bsQufqjuiaeXuxVOrdgRpGTLuWSneD24lQB N74w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727789442; x=1728394242; 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=olXUlrdi/nz0nOHZ/AvYtKp2YQWJmsFP4oPaErFpY5E=; b=kC83n0BPaqFZbP+UjzPLq6dcJAtEcHKic/reIGcJpqeuHUeWT9d/Z3D/Mid4PMUvkD GphVuRXQngu8cxCzKwTLZYWh0Q8x50gf8cAxkIJfSYwP2CoEWXbC4Yhh27Qrno38e+WO VrcympBnlhBAo7hX4mb2mdYYufe0ysQm6wr+5Jq5w7DirmJgxbhIpAljCewJlD7nPyeR /aPPNM0p43ulthdVrNIJ5ocPpKz7TAoiY2c+aWWVsBjoVsTmuJlnm5aZ630DcSjXgY2b uZEr82N/+88YzOplLq+pwca4bBqp4HyHv+cIWIkRZR7UvFmGkZk9U9K0EOV2pNhcOy4v HuSw== X-Gm-Message-State: AOJu0YzPwvg/9gvXrFePomFm7nVOre8Dc+jIHdQHwfeCRKOAAIVj9ddl mnGgcL2I5ZGVHn1Yt2IiLxDJB+CpIH6m23fAO/DJCGoZYZz6vMDHxFwMqre8WxqZImudhklmtmx d7F7d+2WzKbosDQ== X-Google-Smtp-Source: AGHT+IGXMSmR9tUOXy3jDwtrnaVHFTjyw2Rte+yvJh1ufczYgFKCKfYOH4n1ufOUBJAb23e/2z0m5y/fQ6YZfIY= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:ce4f:0:b0:e22:5bdf:39c1 with SMTP id 3f1490d57ef6-e2604c883d4mr11214276.10.1727789441208; Tue, 01 Oct 2024 06:30:41 -0700 (PDT) Date: Tue, 01 Oct 2024 13:30:01 +0000 In-Reply-To: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241001-tracepoint-v9-0-1ad3b7d78acb@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=sLUHlSyC2EqYvVOyetWqSigN1sJyjMhs3a8R4LFtn9I=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBm+/lyFHY2O6VQre0mj/jV5ALg+wxdcErPZc9Ns wtLhtPfTGSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZvv5cgAKCRAEWL7uWMY5 RoYbEAC5if5x80NiwTtpU9LeC4+oAi1q5MHgnTGm68dIhGpYCOoeG1ea1oiYjhlLQ0h5aeMOWDq GL4oyLEBn4vIKh3GxvtqCxY6fPwPuwvLJxJMmmVjS6jKmnXT7exM6pnaxl4UGlCpcG6mT+FHJMy l+Mu9CRNp/MEYFFazkQuAzRjifpW5vTH/09vvghWKJsiOTJhCZQDru/mCL6WrpBSFIDvrWy4z4W ubUt1jPZhK1x4teTN6Ud/lVUWAggku2oHPZGzsoFIy/HouPG8gclU+QLzkMPYNNQHaW3pOa0Z1O J193KVWf5SrBPxEFZ7d4FYFf1wzDgkHwaNhD+Ii+Pz+Jxb5w90Wh0xHS/OGeKecwYFY8fEfnMug 7whg7BEInzPsDgeQ7YlYYsagCiJWZeL15JjdaRU5C0Ia/xpfpEYanMwRbvj7EPJxcEbmJ2IMnYX IRxQBPv3HM0wYyELTuvMbr1pXyUl/iwwT2CT16kpRxJrx26uhxxU7rkyvh5vgvnpYGxJHXxBz7O JcjxhGXJdN+6X8kmOSnGoNeWpaKxseawbxKhbRC0W1E5YHvMDy91yStaghZxgFCa+AykTYPRQhw fsy97qMvgB4Lkx1GfsLrjFA5QX7Z8ZAV/AQZHLt0CyNngyFZ2J0DBStkA5Fxw7FsLbdzSEAcdz1 03ykKfQkxk4YDtw== X-Mailer: b4 0.13.0 Message-ID: <20241001-tracepoint-v9-4-1ad3b7d78acb@google.com> Subject: [PATCH v9 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 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 Acked-by: Catalin Marinas --- 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 Tue Oct 1 13:30:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13817985 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 9957C1C4606 for ; Tue, 1 Oct 2024 13:30:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789447; cv=none; b=apjDB7iS0kjZqusoXrhDRkcc2XILDcEmN+zkqJQzs3y6+K3NDkSHod6jChsokquB3moYPSeTXXUw+7ILLC7w7LXl1M6cbZpjQpHR9EzUgaToIksvzKHnbF+fMaGK9RYcBT2tULLAoU8OJvnwG58Z1LehVNkHwwUdRTiaOHovVbs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727789447; c=relaxed/simple; bh=B20XPkbS2BldO1VNXORVIshLvelvNfCPEDgTJEytqJk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T+ZtPeB4Ln9ws4sm+88qrV+uh1m1CzGpb8xqkWjxFc/LbITQ8WZlMm/ugFVsYE/dif82QlaVh58MdtjAXAGboQpqmvXE/ysXaL3G9yy/kuHijsCP3lsKlqXNtqN/naFt9NUb9jv870IBcZHyrlvC6sYrjeqOeVFber7USYjzQXY= 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=mWd7FH7J; arc=none smtp.client-ip=209.85.219.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="mWd7FH7J" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e25cef43f5aso6533486276.3 for ; Tue, 01 Oct 2024 06:30:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727789444; x=1728394244; 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=fkhFbvRkdz/oOfeYJrLg9+G9M/Uj0+RwqIl1/SZKHXU=; b=mWd7FH7JGFa3Gmit9k9qrqYpmzypSjOq0B+iR7etHOEcNwmL5SelAIN/ztqNMygNgZ Nn7y7zdPjvXK4C735rKcFGm77HLdXzIs/Fxm5WgOuCjC5PgVwieqScpVHTjPglwFvlPM NA7IyS239cAimrCfXV83l+gS8FpPaghu8FDREHolZtKf5IV/wIiqJcQs7Z7GCoLPTF8J aCCX1upy1R57pTlvFqzQcfTgvE28Ap62LFKON2kqLdD6olPYq2USSNRSh3ZjiqqaeM8w 1V1TOUjED7lkPH7v0Va+eT29GBij7ey0UaaULBfKUakonbv0CIGMoNOp/QR+Qhp80Rsu rBjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727789444; x=1728394244; 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=fkhFbvRkdz/oOfeYJrLg9+G9M/Uj0+RwqIl1/SZKHXU=; b=VoYAHtXD4KeoFG14gxZ+wAtbIQ9n1Yt+yvk4bOB7mqQ2w4q1U3Y80A+Bm2QPUqBln6 0ftkcq8PhiITJ1q42TwszTh/S9aISq5blIs32eI56Z8/7sakWgoE9DwkjK/YHyejdEaP +CJL81sf0sYnmY0xjd7xvw+2SF/rMjnUyrcK95xCbindQaOdzaNGfsKRFryH96QZZ70e 4COPbU3xPMiINFeYNrpFPDgqZGgemstQFc+8qmaN1XG+tjb9oMQxbqlyk2Bf29KFlMrQ Q1XcliNyzNm+nbb9XhNHIXa59AjL4w3HEzcayT69usDI/35oEysLNc+fZ7QY0v4WFc/a U5Mg== X-Gm-Message-State: AOJu0YxSCpTmQhvxuyffLhHWeRa6M8DI+WPuTERaM3hwadXkywzeDyjQ 1eR0KuVRkHCiq3/0OjJd/ARWpDbmc882QSIXQh2VhJk6MLpXhlietc3JeeTSjawZKhBj3hvkGU3 LyWGFRQuBT1zvKw== X-Google-Smtp-Source: AGHT+IHx8WYLlRQ85MZlmXhOzStV8hphkC3W/SA4f78hZMUVqK+AieUKfwBlCdSzSifKonxye2KCvb5tQYoxE/A= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a25:8304:0:b0:e1a:a29d:33f6 with SMTP id 3f1490d57ef6-e2604b2bbd3mr134063276.3.1727789444352; Tue, 01 Oct 2024 06:30:44 -0700 (PDT) Date: Tue, 01 Oct 2024 13:30:02 +0000 In-Reply-To: <20241001-tracepoint-v9-0-1ad3b7d78acb@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241001-tracepoint-v9-0-1ad3b7d78acb@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=B20XPkbS2BldO1VNXORVIshLvelvNfCPEDgTJEytqJk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBm+/lyTWXV0qt+2jySV7mW4g92htG/ndvQ8HqpA dk6PB1Bf0yJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZvv5cgAKCRAEWL7uWMY5 RkGpEACtKcH0XvxL+nqPbUO1qDZYBp7bMUnck3VQWyZKLA989Zc7bnXqY74U0cmqeyfj/XoMJ5Z lnQ5jogYpeKqs3goYSkOH+M4p4ej9Ie95XbXHIMRYWpPojVgq4djGHfYMtRJrD3hjvEO6RlaQMc AeL+nbrCeI1NUFAMheXguFI4z5YgMNFrkdJ3LujbqXfax5LKBopJpR9sIf+2zmz5cC56fRIAyop p2Br8NEWvvZd37qk9WAK8Bb8SQTqhS5MslcMU6ATBjPTChXJVV9Zw0Yu9EVkparyOh+lILISo6l nj3W1hFE4wA765Q6mxAlnKubwjV5oWtEyPu5Ju6wnL0gXlHvr+c7DmEA0wnBLV37vuy0Vp/6Kpt hF041c9wo0ehSoZObI95FnGdJsjNLcm3CdNagGhlWLDqT/juXtplimiVk4Yru1uEtsAKIncI0Kh 2vBc9thKTWi4uiKDUgoR80YNDNo/khAorOcLybKN7XOXe6XWuZvSsb/Q9vJwO3Rz6PJjFK1/szN VWquccbjzSPl3/zsb1gR1lEcyEUuJg9/oIAi7qCT9l7ArCrWTOwyeReOGled4NdKqDou3bXF3fL 1CoH3B2NyClyJ50UDrVa4jHNW/AwmSHJp8dqwKeIqLeYIpyIxb72ihi3ukO1m3EOe7JWuBRc626 8ofGkmyjOxUAWFQ== X-Mailer: b4 0.13.0 Message-ID: <20241001-tracepoint-v9-5-1ad3b7d78acb@google.com> Subject: [PATCH v9 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 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 b5e0a73b78f3..09ea07cc4001 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 @@ -421,7 +423,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 77610e19df96..448af0880785 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -148,3 +148,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 8f423a1faf50..03ee558fcd4d 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -248,12 +248,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 \ @@ -303,6 +304,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) # ---------------------------------------------------------------------------