From nobody Thu Feb 12 21:45:24 2026 Received: from mail-lf1-f74.google.com (mail-lf1-f74.google.com [209.85.167.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 7F8DA1990AD for ; Thu, 6 Jun 2024 15:05:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686358; cv=none; b=kSaVqXVFD9dKNXPGaXzqfyYOSpGRmxOkZPaGh7P8Z2YSWxNCadVFrERC6azflamMdeBLQcbI+iso+OIj/DBO891sww9Zs4YPbKTGYFSaqfy/K0afjqSRmmeM4JEODepbspHo19OU/FfBQev+Xi0hjAjIYT8p6tTp1vusHv3YZ40= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686358; c=relaxed/simple; bh=VFFXazztmY9tR8xMGIPWOtLDs5Ye6yzjN07cIKwKBuk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lSlFjXTBK6/4nhB8HppJZacXutotkr/Xv2cOgl4LfSKQeJ/DMSUafNjPbobc4jgmBwfc7E0COg+0QYd2BeIb4jflbdAhu2Iu5FQpaaG+JUqZ3RBA4aBWT6RfYTeQUIsbdJ/pQX4a4AmWK6dBCYohwvklDcwBO3RO6NOHnhUXD/E= 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=OxRKjxuX; arc=none smtp.client-ip=209.85.167.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="OxRKjxuX" Received: by mail-lf1-f74.google.com with SMTP id 2adb3069b0e04-52b85bc3216so881378e87.2 for ; Thu, 06 Jun 2024 08:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717686355; x=1718291155; 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=HfXZTl4B3xvX6SOW1W7GHYUYqMo3gKS0xEQFx1y1p+8=; b=OxRKjxuXoH0O+4c3qpv+GxnAa7eXCuu+sfzZfM8MAObycZKs51zCHzjiT2dvDkHkJv nau1AYQqHOqHTo5dT6Msh/DuN2r2iGpOToeWyzi+n+o2qMcN0fGV0LZoOm07ouDfJmCD hGmtgoAXbQzPX9dv8IHkVhayIxOZS5NZiLi7TahStC7tf9EnrX2twh9LMBb3YB2Lcnwb nXcLrmEc+NQILml+UixTSvXINmoFoqOa2pww0lQ+dyi8LPg1luEZrJPzQb39h33xaPLR FzIKTwzDI5hkFrci/09WIPBEXarP+pF2fQw0c/LLMXmRxHbVCuvuCr/lSBeupqTeg8r5 D96g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717686355; x=1718291155; 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=HfXZTl4B3xvX6SOW1W7GHYUYqMo3gKS0xEQFx1y1p+8=; b=U+GOo4HZfFtqYPHhfbiMbpA2rkMNvf97/vDfNbRn5/kdBAf5JULi/ap+oetYzOz96o 6TffKP96qgrfaOyfeqaysV98zolmrEjz7qXEA35xNTaE1+4x52B3ZuoG6uVL3S3bxc0q ZF+B05u9NWfAu27osfMjsKGX26al8T6o6wafzqOwSwl6mnM/1nrebRjNb+2buuW3Y//A DsT809fUyYJzQAhYTsniJlBmmFOw8CRSxU36/rDMOLZtUGKWcMVL5JyndlZAQGTtQkQ7 hzbYFuw1c6yO+P45MNCDF1W5L2mCv2F2u0WlMqgd5gwMClNUra4GsgUyfv6pbzTE82xV t8LA== X-Forwarded-Encrypted: i=1; AJvYcCUuecK0z00iRn74b5D7cquUXc4yG5Y9Z9YYSvnfNLHIY/5q2C07ul6ddUyxlvyMmumRYLKXW2csXXOBGO66QtLftZbx1RVkY6Cn8lnR X-Gm-Message-State: AOJu0YxPKIHX3ybx/PGfGVAYs7RiYlUjQ2tbLDi28c8vaS2vzLw5wcHu nmWX30ro+U/oqzXVRcK3ISZE9HU1YowQpHOzsLHPR+Um9pkwyj+Sqvb7FkhNS1WFewiAyeLd+mE cMKbeOQvHcJainA== X-Google-Smtp-Source: AGHT+IEmhbHPuo49cWbozLCcAw8HPDoOUQPCCoJ5U1tZ9t3BRAJIEYeV4n48VgOfQlUamRkSC+ASE//k2dJAcvc= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a05:6512:228d:b0:522:e17:7a21 with SMTP id 2adb3069b0e04-52bab508fb8mr6516e87.11.1717686354408; Thu, 06 Jun 2024 08:05:54 -0700 (PDT) Date: Thu, 06 Jun 2024 15:05:24 +0000 In-Reply-To: <20240606-tracepoint-v1-0-6551627bf51b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240606-tracepoint-v1-0-6551627bf51b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3993; i=aliceryhl@google.com; h=from:subject:message-id; bh=VFFXazztmY9tR8xMGIPWOtLDs5Ye6yzjN07cIKwKBuk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmYdBKrNLgiljlOwRqSiSG7++Zael6mAiUfC69m MDnQpixko6JAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZmHQSgAKCRAEWL7uWMY5 RnFND/4kQp/lkvYAvaa5P9e1hZkXiQfV7GjO7L5daH8gg0x2AH9hOaDIsuJ9OKsgXf0IrEXeR+u WagKY23uHT1Vy8PHm1+Gjbv8QpVLtt+rURgrVYZqakdhx1gQ0bYFFNz1E7AHG4X788rWDT/pJi9 tr57IbW6Hf2eU5/lyP5InJfKnkSnJp3hSuMwsF2MgAh/PkTS4d3SuhPweqkt/eenc7n3n6JJdAH avROWCoC+vs//Nt0ZYcKcTPB1jRoxMaj3zue2HS2fSPmxmIS5aI/v3lvgJXBjOF+5EH1w/YoCE1 slYDA2A8bt5If2MzjcNcdaCaUEGPRJDnhORdZ4ysoaEwkjriaXURmXuxT9oO+bpoWjzaptU0pPo hgRz43xAHV0X2edmAQzf/HvfnYSumrcItOTWup9e5lzsg47KnZRZpVN0Tf/mqOLpuOwKIN/inGP Fhyoz5tLNaYYcEsWS21J5TbvpjrzW527UM2djAEmrkkNaICAyQ6KtAxiRTMzFTGNQ0aZM1TmwiP yKQ4/cBKWuzr2dWkVAjEw7pkrit2unrQfpJ4bf+CB7HIxslDOl3FVZ5iZiqPq+H1FB5mUYDOt45 gzQMzj9ncgyOP4eezGebUDOMQHrSGNpJAb4OQrqC6UtwOzneoscChotPyCsjsyYDX+GlThZ0796 UlPjVePCTZ/KMNw== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240606-tracepoint-v1-1-6551627bf51b@google.com> Subject: [PATCH 1/3] rust: add static_call support From: Alice Ryhl To: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: linux-trace-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add static_call support by mirroring how C does. When the platform does not support static calls (right now, that means that it is not x86), then the function pointer is loaded from a global and called. Otherwise, we generate a call to a trampoline function, and objtool is used to make these calls patchable at runtime. Signed-off-by: Alice Ryhl --- rust/kernel/lib.rs | 1 + rust/kernel/static_call.rs | 92 ++++++++++++++++++++++++++++++++++++++++++= ++++ 2 files changed, 93 insertions(+) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index fbd91a48ff8b..d534b1178955 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -38,6 +38,7 @@ pub mod prelude; pub mod print; mod static_assert; +pub mod static_call; #[doc(hidden)] pub mod std_vendor; pub mod str; diff --git a/rust/kernel/static_call.rs b/rust/kernel/static_call.rs new file mode 100644 index 000000000000..f7b8ba7bf1fb --- /dev/null +++ b/rust/kernel/static_call.rs @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for static calls. + +#[macro_export] +#[doc(hidden)] +macro_rules! ty_underscore_for { + ($arg:expr) =3D> { + _ + }; +} + +#[doc(hidden)] +#[repr(transparent)] +pub struct AddressableStaticCallKey { + _ptr: *const bindings::static_call_key, +} +unsafe impl Sync for AddressableStaticCallKey {} +impl AddressableStaticCallKey { + pub const fn new(ptr: *const bindings::static_call_key) -> Self { + Self { _ptr: ptr } + } +} + +#[cfg(CONFIG_HAVE_STATIC_CALL)] +#[doc(hidden)] +#[macro_export] +macro_rules! _static_call { + ($name:ident($($args:expr),* $(,)?)) =3D> {{ + // Symbol mangling will give this symbol a unique name. + #[cfg(CONFIG_HAVE_STATIC_CALL_INLINE)] + #[link_section =3D ".discard.addressable"] + #[used] + static __ADDRESSABLE: $crate::static_call::AddressableStaticCallKe= y =3D unsafe { + $crate::static_call::AddressableStaticCallKey::new(::core::ptr= ::addr_of!( + $crate::macros::paste! { $crate::bindings:: [<__SCK__ $nam= e >]; } + )) + }; + + let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_undersc= ore_for!($args)),*) -> _ =3D + $crate::macros::paste! { $crate::bindings:: [<__SCT__ $name >]= ; }; + (fn_ptr)($($args),*) + }}; +} + +#[cfg(not(CONFIG_HAVE_STATIC_CALL))] +#[doc(hidden)] +#[macro_export] +macro_rules! _static_call { + ($name:ident($($args:expr),* $(,)?)) =3D> {{ + let void_ptr_fn: *mut ::core::ffi::c_void =3D + $crate::macros::paste! { $crate::bindings:: [<__SCK__ $name >]= ; }.func; + + let fn_ptr: unsafe extern "C" fn($($crate::static_call::ty_undersc= ore_for!($args)),*) -> _ =3D + if true { + ::core::mem::transmute(void_ptr_fn) + } else { + // This is dead code, but it influences type inference on = `fn_ptr` so that we + // transmute the function pointer to the right type. + $crate::macros::paste! { $crate::bindings:: [<__SCT__ $nam= e >]; } + }; + + (fn_ptr)($($args),*) + }}; +} + +/// Statically call a global function. +/// +/// # Safety +/// +/// This macro will call the provided function. It is up to the caller to = uphold the safety +/// guarantees of the function. +/// +/// # Examples +/// +/// ```ignore +/// fn call_static() { +/// unsafe { +/// static_call! { your_static_call() }; +/// } +/// } +/// ``` +#[macro_export] +macro_rules! static_call { + // Forward to the real implementation. Separated like this so that we = don't have to duplicate + // the documentation. + ($($args:tt)*) =3D> { $crate::static_call::_static_call! { $($args)* }= }; +} + +pub use {_static_call, static_call, ty_underscore_for}; --=20 2.45.2.505.gda0bf45e8d-goog From nobody Thu Feb 12 21:45:24 2026 Received: from mail-lj1-f201.google.com (mail-lj1-f201.google.com [209.85.208.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 6980C19B5A2 for ; Thu, 6 Jun 2024 15:05:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686361; cv=none; b=O09qGAhuCveHBUyG5YNraISpYon/eRFBjE/Gl0i1GrR7cZrUYqRZ3IA2wKI/8Onf5cT1yE0frefZaoFjuPneE/CdH5slv3d/d14pH4XirQHNUnfrDmOpseCJ435cBPzUozJPOVNy9i54DBnd9ZKdtSt71FX7iUZQiJgiXGi5Chs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686361; c=relaxed/simple; bh=uHuK5cxCqNVkYxGJWDP/1f30u93vV/WBYZH9iUJYV3U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fJLbHMJzhE/16vLrV3eFzRh19hAV3R3H8DQmI6nnmgbUjRluHcJcFI37OCNeFCtlqRIoETc7mhkI/LN9HeeL7pm7dcOnYF02ZN/MX66oEM4Gdl+P5XSpMJN7XLA5l8EGubqoHjeb2RiRMNz3JrCxZXDNzxPaDi1cFFTl4BMfScE= 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=tONNOKmR; arc=none smtp.client-ip=209.85.208.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="tONNOKmR" Received: by mail-lj1-f201.google.com with SMTP id 38308e7fff4ca-2ea93c06e3cso9373881fa.2 for ; Thu, 06 Jun 2024 08:05:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717686358; x=1718291158; 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=s9QSHkAsTwIK08A93jjRpyw8x8o2l5o1Rlv7tR4OejU=; b=tONNOKmRnShWw1pN3jV8h5u7NS1xBpvlPM1qFtK+n8OlBbLxtchQejcg85QjO3BJYw 0KFdzKOvIgaxw249tccwh4mt7HwX0FjZqpKa55J0YbYSvuphgxkozM3pi5m4ZhHA+Hxc wf2+VCYfST+G+HZbySr+NjGFsbEHU01d654k3Shn+MLBqhGq+V/v3BFYkTAbm1dj8t1t gU1sr6Z+XaE9l+7ST9XyNcklS/kGRkv4V94gaxLZ5LqxYwNHdG1cjE0xts01Wq7Db9ie RjxszmU1BXtqxBJWemT46HWsKUlJYMwpD5UyLwBBjrp9Z3Eoh+2KTqHXzraNWLfwyEqY BTIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717686358; x=1718291158; 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=s9QSHkAsTwIK08A93jjRpyw8x8o2l5o1Rlv7tR4OejU=; b=H8uhNCgxu5Rgo/jZUx26egayWD3XluYfK26EEISCslM1/vi7fZJqUEeGdl4xZcCC5N m+ZfFJU6WV9vf+HvD0qMcFPOmgUb0Yeg0U4NRZCwmvkfglFKqyTia8o6HN4+jwFCnQhq Ilk+xofVtd6WHZ2X1S42MsiHeb4sWB3T1bjZb4T7PBtpT8dYvUj3Hiq3U7Trgq+R5GaY P6EMFQpmnhZQzfdzobahd7GNbwQF/Zmsjjzto6NSWk6fHuIJZdxKlLrrR4LBShjzAU3j anK9fWsVx/61n6Ea5gmCbFG477diwNl3gPe3gp9F7XJ4IOjNOhS/7qPVZ9oEdRa6q6Ef Qdog== X-Forwarded-Encrypted: i=1; AJvYcCVFImm4gQGttzfjyU4N2KWjHd88tY3DTeXQzjjqN8U9M3MqFxzMl09TydI3dkqQcbmgUzzwBU3WO/spECqFfeR48HkxYRR6mhOgnOJM X-Gm-Message-State: AOJu0Yxj7oBVaJ7UlGcA0ZiDYhNt7rD9lxZn14kuoFS5ObY6Ird9MdFG SRn4pAFVHgQy8ZCPHLRhi4/Ii5+QuyEUl15YmK5lOsm5/cd0o3DISq9xQyVg+USYRprokpB6/1E QYhvJF0vGiSemXA== X-Google-Smtp-Source: AGHT+IEkXpPuontDmJFUZKaqvGblSitXXKIgIsSGBnTpCuf/+wnRYsTU/LOByO7Ao309s/06RJ1U2FmGmlwULhM= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:a2e:8082:0:b0:2ea:82c7:33cf with SMTP id 38308e7fff4ca-2eac7a97074mr62471fa.10.1717686357499; Thu, 06 Jun 2024 08:05:57 -0700 (PDT) Date: Thu, 06 Jun 2024 15:05:25 +0000 In-Reply-To: <20240606-tracepoint-v1-0-6551627bf51b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240606-tracepoint-v1-0-6551627bf51b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4222; i=aliceryhl@google.com; h=from:subject:message-id; bh=uHuK5cxCqNVkYxGJWDP/1f30u93vV/WBYZH9iUJYV3U=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmYdBKoUoIL/G+5ecJleyzv9Uh17RVRQHXMZ0gd s0xAlyPxmSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZmHQSgAKCRAEWL7uWMY5 RhvJD/9oQToFBgs8kw4XgbLrWL0zjwaOWrckWwvD8cwzPfnmq/2ghW6DoVOXs8w8J4rxycXTOjV fCXOenZ0frK62IFBwcQxpcK43worsfjN8LGzgkmFy675pdJZQOGI/HOKc7Z4yvVSp/1uWMB6tSy 2pUwJbqFzh7oKCAnHDl72zt0NmoAo6CUlPSntj7nzvhZH94zijFtT7DqjSm91WHbT+OgMRRJPTw WQqzLHjkP+u7Weg03ftuFndJGB0y0LSySMsFJ67qHYyD9R8i8jnRn44srHPu5Mt5osUX7XC3B9W WrSINyhKQOEpJ+nBmLTLJWyQo2Zlu1yFh6ODt3mT56RNXjziApotIyXnkcDpW7bGWFhw+5OtS0b Xy1JsusGh+oWIDWRFRa6JdzHQ0IPgUwWEj7zoPT6XGQFpc7Yxi4mMwBb+OEU2mbAKJ77oTSJkiG d+rGxeS0fjVx82l8Ta22js8ZQx8NmLja+wC/Q33Po2WYZoYNV5NItlLMp2wxZewMe8PdcaHpUoY 8pvQncs8ZCntY/nkhLt9BnEiUye4/5kr7HOplUPZtCd8+zFGYlGcyDw7jNf0MBXnb4LbZEwfs3A aCHDZC+ds2RxbxhUksw3YnCInW6cFgHYfBz85OgBwKaSFuWvwyylOkYFMNabqtLaIEVNgdyW8Ga 4drFLhl+VtNBx7A== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240606-tracepoint-v1-2-6551627bf51b@google.com> Subject: [PATCH 2/3] rust: add static_key_false From: Alice Ryhl To: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: linux-trace-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add just enough support for static key so that we can use it from tracepoints. Tracepoints rely on `static_key_false` even though it is deprecated, so we add the same functionality to Rust. Signed-off-by: Alice Ryhl --- rust/kernel/lib.rs | 1 + rust/kernel/static_key.rs | 87 +++++++++++++++++++++++++++++++++++++++++++= ++++ scripts/Makefile.build | 2 +- 3 files changed, 89 insertions(+), 1 deletion(-) diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index d534b1178955..22e1fedd0774 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -39,6 +39,7 @@ pub mod print; mod static_assert; pub mod static_call; +pub mod static_key; #[doc(hidden)] pub mod std_vendor; pub mod str; diff --git a/rust/kernel/static_key.rs b/rust/kernel/static_key.rs new file mode 100644 index 000000000000..6c3dbe14c98a --- /dev/null +++ b/rust/kernel/static_key.rs @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for static keys. + +use crate::bindings::*; + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "x86_64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + 1: .byte 0x0f,0x1f,0x44,0x00,0x00 + + .pushsection __jump_table, "aw" + .balign 8 + .long 1b - . + .long {0} - . + .quad {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(target_arch =3D "aarch64")] +macro_rules! _static_key_false { + ($key:path, $keytyp:ty, $field:ident) =3D> {'my_label: { + core::arch::asm!( + r#" + 1: nop + + .pushsection __jump_table, "aw" + .align 3 + .long 1b - ., {0} - . + .quad {1} + {2} - . + .popsection + "#, + label { + break 'my_label true; + }, + sym $key, + const ::core::mem::offset_of!($keytyp, $field), + ); + + break 'my_label false; + }}; +} + +/// Branch based on a static key. +/// +/// Takes three arguments: +/// +/// * `key` - the path to the static variable containing the `static_key`. +/// * `keytyp` - the type of `key`. +/// * `field` - the name of the field of `key` that contains the `static_k= ey`. +#[macro_export] +macro_rules! static_key_false { + // Forward to the real implementation. Separated like this so that we = don't have to duplicate + // the documentation. + ($key:path, $keytyp:ty, $field:ident) =3D> {{ + // Assert that `$key` has type `$keytyp` and that `$key.$field` ha= s type `static_key`. + // + // SAFETY: We know that `$key` is a static because otherwise the i= nline assembly will not + // compile. The raw pointers created in this block are in-bounds o= f `$key`. + static _TY_ASSERT: () =3D unsafe { + let key: *const $keytyp =3D ::core::ptr::addr_of!($key); + let _: *const $crate::bindings::static_key =3D ::core::ptr::ad= dr_of!((*key).$field); + }; + + $crate::static_key::_static_key_false! { $key, $keytyp, $field } + }}; +} + +pub use {_static_key_false, static_key_false}; diff --git a/scripts/Makefile.build b/scripts/Makefile.build index efacca63c897..60197c1c063f 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -263,7 +263,7 @@ $(obj)/%.lst: $(obj)/%.c FORCE # Compile Rust sources (.rs) # ------------------------------------------------------------------------= --- =20 -rust_allowed_features :=3D new_uninit +rust_allowed_features :=3D asm_const,asm_goto,new_uninit =20 # `--out-dir` is required to avoid temporaries being created by `rustc` in= the # current working directory, which may be not accessible in the out-of-tree --=20 2.45.2.505.gda0bf45e8d-goog From nobody Thu Feb 12 21:45:24 2026 Received: from mail-lf1-f73.google.com (mail-lf1-f73.google.com [209.85.167.73]) (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 F23BB19CD10 for ; Thu, 6 Jun 2024 15:06:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686364; cv=none; b=XL5pwIeYT1xnATB0WOowLLDXSyyEs0gMEoRmLi91ji0+o1MXWcIOvVu3zsaMeOtSqlbsC534MOipFAi+bxxygpWpydfG0bc5WHryGQQXPhxWCXZTbid9J7uV1WdLJBO8R3HkwT+8YBzSEAp6AgKpoM8pAEMSMxuPATiL4OmyeC0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717686364; c=relaxed/simple; bh=xxnCkS/98mK/H9Sz+c63sGlDPQKDxAYAQZLVvfkC5Lc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=s2n6+Jh8OA+5myWB0rWuVIu5jou25uLw4dJz1VvJPl1CYQRBp3xHSz13CLLMyzaozvqJHixUjcLSNH80uisdfvy28AEqXSeq6mP9dqShJdyAm8Ov65f32UgkPMWG/qSvioUbKO+ACRTA08MnSKBOwJ1f0TzywITySlkq1UQXvZY= 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=nD1Mkbwp; arc=none smtp.client-ip=209.85.167.73 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="nD1Mkbwp" Received: by mail-lf1-f73.google.com with SMTP id 2adb3069b0e04-52b84359026so899217e87.0 for ; Thu, 06 Jun 2024 08:06:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717686360; x=1718291160; 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=WtYQY0bIVsBsFhO8mlkrQxri1pzuLKr+k7DZn7Q0yvI=; b=nD1MkbwphI/G60UdeNnNsl/dGxA+/mGRee3KdoMRifhE7ZsV9PiFnZCISxOsLia3DE Fa0SG+vr6jZNS0fp6+9j5GKGuUSV4z54a1G/pGs9mCshNqhthHfwgj4W2VKVsXANzwo5 xwWjj7Wxn1SVsPwUbfsnyvMVHGKyw1kaapxDtwM7J1/+fYEyPIpzkuzOHJAQUuqTf2Dd A0E9VnsY7TVSW98Rit+gHvx0TDF4ZvrG6/4gXzdftNc8Mdp7uaaQP1/WakDTushXdXsQ K/oGg3OVfZtxNSvSn1oWV0HjPc5iOF1wuZ2uM58cZOyrZ/+aLlVPXYKlq/KA9/hcJz97 4Y7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717686360; x=1718291160; 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=WtYQY0bIVsBsFhO8mlkrQxri1pzuLKr+k7DZn7Q0yvI=; b=vASeZ81xY8LNA14NhHecUTdjXXDywctZgtvoTa8zErWoMFwXDzREgpC40hs6Hdf+LR qTO8SwZKIi+S4taWo0gKnhMYGUubjxgO43EuFm+FpdGQAZyolPQwvsMsLCZk0ko5lcH8 /LMuME2gnaQTKEQu+ZX9giKyTXZgb/W3aINL4K9xi2RDtt6ccFIrELeAGAq7XyFAqCDN OeJ+Z5jADKMuMdJfP8r20cMFVuZjvrTKR5ifoGAKwg8WaZUGnK0NNp4sMvBBxFZnoCS5 8mze8Gdrhe4/KD2OzjIF+3skfnd8GCGeZ9g0NrI2ogFNXKAKQkeFsDowOyZ/8QRp3BJk 8Qag== X-Forwarded-Encrypted: i=1; AJvYcCXa/cKLR6KTlTDExv/tc8ssBf7i/oNse+dpnTE52mV7BVZ/Yej3ihY8eMuPP5E5ToDtr7pVNkci0JXboM3JqnMKmo2/PkS9RvvOly1c X-Gm-Message-State: AOJu0YyWmjKBBFwQYmvn0cAsqEQcl+9tclQNCI8kytXU/mX0TKevBnUY vx0z+14XOc/JsNNKJgFgTWkhWi6tl0BJysZuCR18vut8OXwYz0TTrBoGbA1oSScM2Q1hGLTI/Oe vPx4YWfOawM76Pw== X-Google-Smtp-Source: AGHT+IE9ZAzKt5UqrFi1ej+90Nb7hUWgketJ9SlKdTM+irVsqHe00KvF/rNPKaJv0vNPmhAXSajuowviVyVSB/0= X-Received: from aliceryhl2.c.googlers.com ([fda3:e722:ac3:cc00:68:949d:c0a8:572]) (user=aliceryhl job=sendgmr) by 2002:ac2:4f09:0:b0:52b:9d7f:8517 with SMTP id 2adb3069b0e04-52bab263d38mr7361e87.0.1717686360110; Thu, 06 Jun 2024 08:06:00 -0700 (PDT) Date: Thu, 06 Jun 2024 15:05:26 +0000 In-Reply-To: <20240606-tracepoint-v1-0-6551627bf51b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240606-tracepoint-v1-0-6551627bf51b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=6676; i=aliceryhl@google.com; h=from:subject:message-id; bh=xxnCkS/98mK/H9Sz+c63sGlDPQKDxAYAQZLVvfkC5Lc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBmYdBL2CIIJl2CpYfykmTKCky6PbMRvEedW7O7S MZG3XbAIxuJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZmHQSwAKCRAEWL7uWMY5 RhtMD/49T1do6ynz2d0b63kf8ow7WWy0FfOSHhGjzIGqd5oYTDDT1T/mtiNYwioMn/WFLHxIBq7 /O7XVNSQHtiv0FnEuV91hRRPE3Ni6G9WHPXttZ/F9l+yA1HcGbAxGe3GV6RjrSZboh0qrudTFZu b6797/0YeBO7ZBwrjoyS3Be3BZR0Aa4PhHt3ZnMIx+K9PJ7dRdd24mLC2e6Qkga5Ph2O2osbRJG YCR6aGk1VvrlQiFWeMZHEBpNtgiI0L29GOZZbnu9YUORwXNPcUacHdztBboFPma6rQ9AiYrspV5 oeCr6LGBqUiRiZ73SAY3umK+AUlDCcHH9hNvf5G7CMRcYJtHly8r92KeMpv//ZTx2AnpfnE3V3K fgn8AIbfoHzlMy48j6LU/o4KoVYeyNbC7He042Ep/C0NsAc5Fxls8MqqcpkWhHOHbRFn/aY0yGX B4REuSZOMHNRvRO46QCgJBYj+nkxeFinxKlTJJhxiewYwwbAa37s0uL4L77eLts4W2H+XOe4Owe iMYKrYPnjc3p5TXGExNU33X4fr6XX2nJW5cEilQddtDIVPlyG7r35w2x5fk/omzbaN28enIVmOW iTXEens9LAYAueMMU7/WuJ/UPanksI2+atmsCOfRoH55E8NVhposB/c7dUYk0N2ltu4VeLgnU7e 4Sof8vQag3PuaJA== X-Mailer: b4 0.13-dev-26615 Message-ID: <20240606-tracepoint-v1-3-6551627bf51b@google.com> Subject: [PATCH 3/3] rust: add tracepoint support From: Alice Ryhl To: Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Ard Biesheuvel , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Benno Lossin , Andreas Hindborg Cc: linux-trace-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Make it possible to have Rust code call into tracepoints defined by C code. It is still required that the tracepoint is declared in a C header, and that this header is included in the input to bindgen. Signed-off-by: Alice Ryhl --- rust/bindings/bindings_helper.h | 1 + rust/bindings/lib.rs | 15 +++++++ rust/helpers.c | 24 +++++++++++ rust/kernel/lib.rs | 1 + rust/kernel/tracepoint.rs | 92 +++++++++++++++++++++++++++++++++++++= ++++ 5 files changed, 133 insertions(+) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index ddb5644d4fd9..d442f9ccfc2c 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include =20 diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 40ddaee50d8b..48856761d682 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -48,3 +48,18 @@ mod bindings_helper { } =20 pub use bindings_raw::*; + +/// Rust version of the C macro `rcu_dereference_raw`. +/// +/// The rust helper only works with void pointers, but this wrapper method= makes it work with any +/// pointer type using pointer casts. +/// +/// # Safety +/// +/// This method has the same safety requirements as the C macro of the sam= e name. +#[inline(always)] +pub unsafe fn rcu_dereference_raw(p: *const *mut T) -> *mut T { + // SAFETY: This helper calls into the C macro, so the caller promises = to uphold the safety + // requirements. + unsafe { __rcu_dereference_raw(p as *mut *mut _) as *mut T } +} diff --git a/rust/helpers.c b/rust/helpers.c index 2c37a0f5d7a8..0560cc2a512a 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -165,6 +165,30 @@ rust_helper_krealloc(const void *objp, size_t new_size= , gfp_t flags) } EXPORT_SYMBOL_GPL(rust_helper_krealloc); =20 +void rust_helper_preempt_enable_notrace(void) +{ + preempt_enable_notrace(); +} +EXPORT_SYMBOL_GPL(rust_helper_preempt_enable_notrace); + +void rust_helper_preempt_disable_notrace(void) +{ + preempt_disable_notrace(); +} +EXPORT_SYMBOL_GPL(rust_helper_preempt_disable_notrace); + +bool rust_helper_current_cpu_online(void) +{ + return cpu_online(raw_smp_processor_id()); +} +EXPORT_SYMBOL_GPL(rust_helper_current_cpu_online); + +void *rust_helper___rcu_dereference_raw(void **p) +{ + return rcu_dereference_raw(p); +} +EXPORT_SYMBOL_GPL(rust_helper___rcu_dereference_raw); + /* * `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can * use it in contexts where Rust expects a `usize` like slice (array) indi= ces. diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 22e1fedd0774..3f3b280bb437 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -46,6 +46,7 @@ pub mod sync; pub mod task; pub mod time; +pub mod tracepoint; pub mod types; pub mod workqueue; =20 diff --git a/rust/kernel/tracepoint.rs b/rust/kernel/tracepoint.rs new file mode 100644 index 000000000000..d628ae71fc58 --- /dev/null +++ b/rust/kernel/tracepoint.rs @@ -0,0 +1,92 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Logic for tracepoints. + +/// Declare the Rust entry point for a tracepoint. +#[macro_export] +macro_rules! declare_trace { + ($($(#[$attr:meta])* $pub:vis fn $name:ident($($argname:ident : $argty= p:ty),* $(,)?);)*) =3D> {$( + $( #[$attr] )* + #[inline(always)] + $pub unsafe fn $name($($argname : $argtyp),*) { + #[cfg(CONFIG_TRACEPOINTS)] + { + use $crate::bindings::*; + + // SAFETY: This macro only compiles if $name is a real tra= cepoint, and if it is a + // real tracepoint, then it is okay to query the static ke= y. + let should_trace =3D unsafe { + $crate::macros::paste! { + $crate::static_key::static_key_false!( + [< __tracepoint_ $name >], + $crate::bindings::tracepoint, + key + ) + } + }; + + if should_trace { + $crate::tracepoint::do_trace!($name($($argname : $argt= yp),*), cond); + } + } + + #[cfg(not(CONFIG_TRACEPOINTS))] + { + // If tracepoints are disabled, insert a trivial use of ea= ch argument + // to avoid unused argument warnings. + $( let _unused =3D $argname; )* + } + } + )*} +} + +#[doc(hidden)] +#[macro_export] +macro_rules! do_trace { + ($name:ident($($argname:ident : $argtyp:ty),* $(,)?), $cond:expr) =3D>= {{ + if !$crate::bindings::current_cpu_online() { + return; + } + + // SAFETY: This call is balanced with the call below. + unsafe { $crate::bindings::preempt_disable_notrace() }; + + // SAFETY: This calls the tracepoint with the provided arguments. = The caller of the Rust + // wrapper guarantees that this is okay. + #[cfg(CONFIG_HAVE_STATIC_CALL)] + unsafe { + let it_func_ptr: *mut $crate::bindings::tracepoint_func =3D + $crate::bindings::rcu_dereference_raw( + ::core::ptr::addr_of!( + $crate::macros::concat_idents!(__tracepoint_, $nam= e).funcs + ) + ); + + if !it_func_ptr.is_null() { + let __data =3D (*it_func_ptr).data; + $crate::macros::paste! { + $crate::static_call::static_call! { + [< tp_func_ $name >] (__data, $($argname),*) + }; + } + } + } + + // SAFETY: This calls the tracepoint with the provided arguments. = The caller of the Rust + // wrapper guarantees that this is okay. + #[cfg(not(CONFIG_HAVE_STATIC_CALL))] + unsafe { + $crate::macros::concat_idents!(__traceiter_, $name)( + ::core::ptr::null_mut(), + $($argname),* + ); + } + + // SAFETY: This call is balanced with the call above. + unsafe { $crate::bindings::preempt_enable_notrace() }; + }} +} + +pub use {declare_trace, do_trace}; --=20 2.45.2.505.gda0bf45e8d-goog