From patchwork Tue Oct 15 13:14:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13836381 X-Patchwork-Delegate: rostedt@goodmis.org 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 3205B1D5143 for ; Tue, 15 Oct 2024 13:15:27 +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=1728998128; cv=none; b=BtuU0oXSO6EjppKqeRTIp863/eolKUWgqLKpAEGCoIU7w0EDuGfmYkd1hBrDCdcOh6VjUEaNFQNjgwgxBHz1v/RhLEuUl+/mSv0sV5H9BNh1j0Gkx8SOJdKuJu4LFSzMzyaUbo1iycr7IYH4WesGJXTaNbtSJC965aODoh9OnFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728998128; c=relaxed/simple; bh=WmlS9ruybdrhRlaID/2lHfheMmCjKvPhIBuh/AhhcO0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YywPkFsjhN866qfk8gQbGfdnUR3Bzf4t8ygNicZL5JfY8raG+vYK3TAxJFhTRpIFAEGbLlXZInfziANqUng2Y1+H7mTrRghZAMMrJcCRRz91J3Sq4Q/aX+J/t70NyftZB4vR28r2ca0pE52KonfPHBNpNQIfJ3HQrLaKEkixisI= 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=qr9JgJov; 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="qr9JgJov" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e347b1e29dso49938527b3.0 for ; Tue, 15 Oct 2024 06:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728998126; x=1729602926; 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=u88YC+HCUxUWsmDhhJzv+FDTM9B6f4PIf50qF6/YsME=; b=qr9JgJovbt1qFSaT6XroxB568QVPs8OQJU3EiaBQBrxapYgUQQuWT3WeRW2oAkjcSw JYx0FwsrnNdR5SO/nrWj8T8fZk/sCsCZK4HA2bDkCry8ykdQtYlowwDEzxzsVZFQcqvh BKqU/zVhAlPEJ0hwaOtNf4W6/IP4sxL+sJyoF6FRa+XDdUtaJP6N4I/504xskbrUuHFi Sr1O9rEK4gIzSng7ILEa/yst/l+3J04o/fEWGxD/yJ42RgNnPZ4rItIEf63O++M5OCnW 06xYvj7AcooOFjw/C5SZtsCoZzMmtXQIQhXQJnS+8lcT0JNcWz4q/MnQ+WfNcy76TsXj P6zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728998126; x=1729602926; 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=u88YC+HCUxUWsmDhhJzv+FDTM9B6f4PIf50qF6/YsME=; b=kXccnGZ4nMx/EdxoIyjAxB/bsTKnz22jIx13LWXCLY1pd8eZQ6n7GraieYmmzeGPhS LEQkAax9m/LKlYDWGhKjMSoUPXRA6clSoOI404JruDXqXvqrUEMdpDUfTiJLkIGkHqV8 FM0oBZAV9fSmkc+e3UUwC2XYCAi8IwXksihayeOutJFI4jSMwbJU1/K5oNuKg12m1ZY0 9uruFOOmQxW/MXLXsRzvJ9qKDTItaY+83CPyeEeS0xJkzuBK0QcKlkO6EPsVVm8lRJtk MPLZWgIt2qzt/z/HyfmVmeOnaTe4ndcxUu5+Y3+9iW9Q0sOSOcsQbzOYgN5yRUG37g8t vlVg== X-Gm-Message-State: AOJu0Yz7zfAvx5ULToeIG6hIxJcHBnmyMutgh6XHD8UL7icPtmYN3m2O g0f9VmWHyrJrUxpCmqYRH82GLeoJYxpZjVAxUMJYZLJJBlTIyMWdXdwXmyXdAemiRp3rWerEfQa M/KpsiQQz6pUh1w== X-Google-Smtp-Source: AGHT+IE4zub7wJu93X96Jlwuq2gB7BqQ9VZQMzbObcPdfJYRyYWGnR7sC3n9RszpByxiydfMX++uZvml6S+7iH0= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:35bd]) (user=aliceryhl job=sendgmr) by 2002:a05:690c:4447:b0:6e3:189a:ad66 with SMTP id 00721157ae682-6e3d41c59camr45117b3.5.1728998126076; Tue, 15 Oct 2024 06:15:26 -0700 (PDT) Date: Tue, 15 Oct 2024 13:14:55 +0000 In-Reply-To: <20241015-tracepoint-v11-0-cceb65820089@google.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241015-tracepoint-v11-0-cceb65820089@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3948; i=aliceryhl@google.com; h=from:subject:message-id; bh=WmlS9ruybdrhRlaID/2lHfheMmCjKvPhIBuh/AhhcO0=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnDmrmE/iEkUSJVfKvPvKYIgmZrgzDGpoY3SBMI +DDHm4J4paJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZw5q5gAKCRAEWL7uWMY5 Ru07D/90RnN4RcIrVzabl6QI81OsSJoCo6zq7NOIuJ+OciuqqlV90O//XrOJs+KsGYD+LZYM86w qEvM3RrHzXGhhMH4gcgfyg+kElp+aETc7eOLVfkWKqrRY3B2zCbjXrpwL7hHkyk0zPChq26bRtj BCCrpflbMr6ik0H++KIv8chFwz7XCwhnKmg4kaxAa+f1gHFcreTKCeIDC5mntcjL7iqpFJKPKpo JhPRylwhU4wEypAteXU6LGBEX7qQu1GoOS4YE8jA5sM9JPv4e83oUW0Q8BPjjLNzp2gB2+mgB5z F/Mw6u5XJmGDv87TVRfMD9Upr7U4G37rIYzTLYfwmRiD8IjjR0STuik1BWICYqlyWoi7a16iKZa jKtwKNI2hVGut0P4LjnQyqwVOqDRoFavLMuVyvPOhc6K0xpJl5ZRYMPIoPNuqSDKD0ObE7gQzKf Ly67ib70h3KKZrctRwAIcJn3u7Os6i15dEUCs5hXQeOZQOEjUrcswKFaSKLHy7Tj07jVauXV9kG lrujRTXo1UTGQaNnzAWcEk4PdbwgbbISpoX4ryPEgSe377NXOPIxEGed35rpdLwxAZ7BiQNa+36 O6HABmLzc8XIvLWJoiIXrF3lvjIrrh+Xt/VgOkz0y+tKcFeeSDGLmS96O483iooq9sV3TqcTOcC ev/Y1GyjFnqcc1Q== X-Mailer: b4 0.13.0 Message-ID: <20241015-tracepoint-v11-1-cceb65820089@google.com> Subject: [PATCH v11 1/5] rust: add static_branch_unlikely for 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_branch_unlikely` with a `struct static_key_false`, 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 Reviewed-by: Gary Guo Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/helpers/helpers.c | 1 + rust/helpers/jump_label.c | 14 ++++++++++++++ rust/kernel/jump_label.rs | 30 ++++++++++++++++++++++++++++++ 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..fc1f1e0df08e --- /dev/null +++ b/rust/helpers/jump_label.c @@ -0,0 +1,14 @@ +// 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); +} +#endif diff --git a/rust/kernel/jump_label.rs b/rust/kernel/jump_label.rs new file mode 100644 index 000000000000..4b7655b2a022 --- /dev/null +++ b/rust/kernel/jump_label.rs @@ -0,0 +1,30 @@ +// 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_branch_unlikely { + ($key:path, $keytyp:ty, $field:ident) => {{ + let _key: *const $keytyp = ::core::ptr::addr_of!($key); + let _key: *const $crate::bindings::static_key_false = ::core::ptr::addr_of!((*_key).$field); + let _key: *const $crate::bindings::static_key = _key.cast(); + + $crate::bindings::static_key_count(_key.cast_mut()) > 0 + }}; +} +pub use static_branch_unlikely; diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index b5f4b3ce6b48..708ff817ccc3 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;