From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) (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 47B414B0484 for ; Wed, 7 Oct 2026 14:30:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791383435; cv=none; b=H452uX1zKXUvXWtrEeauwazWXh9nsVwV9T6HtRh8zQuxlbN2o6lHiOL01D4AgYF0/vHuGBkmRsWdQfeYq1yK8rwDvYud5mKgxd/e1xVNVPParNrbW5Gz7BrirgwvfYaxQvQg6IrPZJxe4litIUYsQEtRspm9CfZXTdpGRnSNsdI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791383435; c=relaxed/simple; bh=VkcBvqhJgE5e7EfXX9M936ALkxN+JB7EdeqtMUM2+Wc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=oOVyXcUhwc8ZtqlnhSj2WElqBAgxOf6Q2O/QXQjD67wXsJnYEulhv/WDQi39tGEJs13+TYOpUEJPfgGMo/quwGT0Bu+3Y1ubBqaXelLqp8U+NgwAKS9yYYBrLRWzNaNnbwLrNpDC+kGvB+/MRTwic0WfdKUTg1OswexJm+LSD8Q= 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=BokSc/C6; arc=none smtp.client-ip=209.85.128.71 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="BokSc/C6" Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-49e6b5c5f44so63611995e9.2 for ; Wed, 07 Oct 2026 07:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791383427; x=1791988227; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=P4xl7iEeB8DBJtdIeM9BSRG6cgKYSvp/1se9UhiVhUY=; b=BokSc/C6F+cWgx9Wq6aC2qLhg5mmp7lcHuLacOp+WShnnnZu+7lWfD0M3/gKN+BQlL sJ8t63n262mQ3tOAaPvKpTjT+RAiMY3nYPpgv+XXBS6xiE4i3y7NrzW5N7xtd/QP2Zyz +aNlGnfB2mSiTprLSwyeBUMhhyVY3NVtohRjdrrUOlNcwvHQZZ3a4i/pfKaZqb3mqx5r Ud5RY4XIqGAQ8wJaN9xK8we/q1P9HV7xKZyM2aUhu22L6dhTX4xwl8LJFhUysyydM7ok 52BtL94Ld5lwi83rMYyGOwnOEpQTuT2HK3hBqEH+QOTwG3aO1weR/r7RnqjvJKA0pA1C 0FDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791383427; x=1791988227; h=content-type: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:content-type; bh=P4xl7iEeB8DBJtdIeM9BSRG6cgKYSvp/1se9UhiVhUY=; b=q6mX073iPTlOAVrmRItHXntjTatc7b7Uc55Q9XtU4k7W9l/FPB657ave7A+PSCypKL PKUXC33Z+wrMAXKJlSy4+j0EUoRmfZkttoO8paUassjosicD80L14cSVv/WGr8PXhCYZ zlIwU1acjdYkh9n9vqQgX7Whn8Yfptf91c5GFI2ohZQeh2BtuVERqOZa4ynmbDc3W+at UawZF1749SIxcNINdEO2Ns27Od0QGqLHPlxMwjN06GfY5zKn3ZNId8xMfjTtGbg/sW3e plZ2okKsRR5h1VY7waNjaWq2SiZHFeUCba7lv/NW7jElrV63Iv/LwJycXBSDC1gscw6X TV1g== X-Forwarded-Encrypted: i=1; AKwUvBxo/K7399ads5Zc61cEbjcw2TKKDesUVILJqx3ftEcBD1fOQuhgJan0edw4pR8kvc92poSy7D+hu8VLumA=@vger.kernel.org X-Gm-Message-State: AFuF++m1mFGejn/ViO51CpI0TDqoFbt2AAL8hVTodJPoJBACkS5xjle5 cYQunTqEFG7CyQBQzNDoxeSLIexl1EKFoJuRPmKbTntHBC5qnB45nQhZzTf76ZwOlTgSX5aC/86 EtjY+emrJy69fNMmqAw== X-Received: from wmpd9.prod.google.com ([2002:a05:600c:4c09:b0:49f:e58b:90d5]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4ed0:b0:4a1:7f41:b9dc with SMTP id 5b1f17b1804b1-4a18043b80amr37747435e9.23.1791383420616; Wed, 07 Oct 2026 07:30:20 -0700 (PDT) Date: Wed, 07 Oct 2026 14:30:02 +0000 In-Reply-To: <20261007-pr-ratelimited-v6-0-f14accd4ea3b@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20261007-pr-ratelimited-v6-0-f14accd4ea3b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4768; i=aliceryhl@google.com; h=from:subject:message-id; bh=VkcBvqhJgE5e7EfXX9M936ALkxN+JB7EdeqtMUM2+Wc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBqxld3dOp4Fo7xkyij20cYVpygnr7onqbGPUxio vldZdNIzGeJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCasZXdwAKCRAEWL7uWMY5 Rhq2EACNflkGDxeRaozl2z6PB2rF0Xfd+Pn+Hl8SBmFTaUT7zsJ2sYu4eW3rh/Sb/9c64JwTAfI nnkyWRSuPBWeWIpICnYx2zAIhpLJhvDH7FFGRLvx4gGwpmp4bQFuz5BfCCnNPVgijYL2AlC0L4E UlPzEShyPyWXChTr8WqSZ39ovo7wMlVvL3pPjJN02DNvU6sLVg6CbPohFXqG5RFtXpyx7qwO8IJ PZHJxjAIyOQFvTFw2332OHHwqHx08Qjda5m6u78S0uBocbBsgX0EQ4yUjE2FLRQ0jZ5n5dt0igS KN1rPb3UTYUfQjD9NDUN8NFMi8fUU2jUJq4yT0uMDBeoYOQB8pFssjKeEJqqD2BRCwKY9U/FpYj GerAsoPDGXeBUwoY7SzzZay/WciQFb8QP8Y1vrlFutFPqr9LjLqRHzK4up8dXb85scsVL7cAKSk ZtbOieoRH8xK9sCxwu78r9vMsABXMn6pa58gRYuGcamlvXO4+GDJlzjQuBjjHI8kRRCr55wrjbq jOWbpFNpPfaiD4t9CafThjlT8q540TQ7IOwUR3UeL8gkzL8+XbF3wHTjOCpuEkChQfnyAF+9gaO AmSoRjmnmjYksJqGRPNeQXxeeJrBFTxuYc+IYn4X/N8EpcihQn/+V0b3IcD/GoEr5RJrcCqOFoS gTapXfc66kk7RFA== X-Mailer: b4 0.14.3 Message-ID: <20261007-pr-ratelimited-v6-2-f14accd4ea3b@google.com> Subject: [PATCH v6 2/4] rust: sync: add const constructor for raw_spinlock_t From: Alice Ryhl To: Greg Kroah-Hartman , Carlos Llamas , Boqun Feng , Gary Guo , Tamir Duberstein , Alexandre Courbot Cc: "=?utf-8?q?Onur_=C3=96zkan?=" , Andreas Hindborg , Benno Lossin , "=?utf-8?q?Bj=C3=B6rn_Roy_Baron?=" , Daniel Almeida , Danilo Krummrich , Ingo Molnar , Lyude Paul , Miguel Ojeda , Peter Zijlstra , Trevor Gross , Waiman Long , Will Deacon , linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, Alice Ryhl Content-Type: text/plain; charset="utf-8" The abstractions for pr_*_ratelimited! need to construct a global `struct ratelimit_state`, which contains a `raw_spinlock_t` field. Thus, add a const constructor for the `raw_spinlock_t` type. The SPINLOCK_OWNER_INIT constant isn't mirrored via a const helper because bindgen generates a 'static mut' instead of a constant from the pointer constant., The __ARCH_SPIN_LOCK_UNLOCKED constant cannot be translated by bindgen because it's a define for a struct without type annotations, so it's explicitly declared in Rust. Reviewed-by: Boqun Feng Reviewed-by: Carlos Llamas Acked-by: Miguel Ojeda Signed-off-by: Alice Ryhl --- include/linux/spinlock_types_raw.h | 4 ++++ rust/bindings/lib.rs | 25 +++++++++++++++++++++++++ rust/kernel/sync/lock/spinlock.rs | 29 +++++++++++++++++++++++++++++ rust/kernel/sync/lockdep.rs | 22 ++++++++++++++++++++++ 4 files changed, 80 insertions(+) diff --git a/include/linux/spinlock_types_raw.h b/include/linux/spinlock_types_raw.h index e5644ab2161f..942c229c90bb 100644 --- a/include/linux/spinlock_types_raw.h +++ b/include/linux/spinlock_types_raw.h @@ -11,6 +11,10 @@ #include +/* + * Keep in sync with rust/kernel/sync/lock/spinlock.rs + */ + context_lock_struct(raw_spinlock) { arch_spinlock_t raw_lock; #ifdef CONFIG_DEBUG_SPINLOCK diff --git a/rust/bindings/lib.rs b/rust/bindings/lib.rs index 439ab88a5da1..076058e25ccf 100644 --- a/rust/bindings/lib.rs +++ b/rust/bindings/lib.rs @@ -83,3 +83,28 @@ mod bindings_helper { None } }; + +// Explicitly list architectures where this logic is checked correct. +#[cfg(any( + CONFIG_ARM, + CONFIG_ARM64, + CONFIG_LOONGARCH, + CONFIG_PPC, + CONFIG_RISCV, + CONFIG_S390, + CONFIG_UML_X86, + CONFIG_X86, +))] +pub const __ARCH_SPIN_LOCK_UNLOCKED: arch_spinlock_t = { + // SAFETY: The `arch_spinlock_t` type can be zeroed. + #[allow(unused_mut)] + let mut lock: arch_spinlock_t = unsafe { core::mem::zeroed() }; + + #[cfg(not(CONFIG_SMP))] + #[cfg(CONFIG_DEBUG_SPINLOCK)] + { + lock.slock = 1; + } + + lock +}; diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs index aafc80125f59..037fa63ebcac 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -463,3 +463,32 @@ fn spinlock_irq_condvar() -> Result { Ok(()) } } + +/// Helper for creating a raw unlocked `bindings::raw_spinlock_t`. +/// +/// For use in statics containing raw spinlocks. +#[doc(alias("__SPIN_LOCK_UNLOCKED", "DEFINE_SPINLOCK"))] +#[expect(dead_code)] +pub(crate) const fn raw_spin_lock_unlocked(name: &'static CStr) -> bindings::raw_spinlock_t { + // Silence unused variable warnings. + #[cfg(not(CONFIG_DEBUG_LOCK_ALLOC))] + let _ = name; + + bindings::raw_spinlock_t { + raw_lock: bindings::__ARCH_SPIN_LOCK_UNLOCKED, + + #[cfg(CONFIG_DEBUG_SPINLOCK)] + magic: bindings::SPINLOCK_MAGIC, + #[cfg(CONFIG_DEBUG_SPINLOCK)] + owner_cpu: u32::MAX, + #[cfg(CONFIG_DEBUG_SPINLOCK)] + owner: usize::MAX as *mut c_void, + + #[cfg(CONFIG_DEBUG_LOCK_ALLOC)] + dep_map: kernel::sync::lockdep::raw_lockdep_map( + name, + kernel::sync::lockdep::LD_WAIT_SPIN, + kernel::sync::lockdep::LD_WAIT_INV, + ), + } +} diff --git a/rust/kernel/sync/lockdep.rs b/rust/kernel/sync/lockdep.rs index 784821cc2a39..d9222be0fb29 100644 --- a/rust/kernel/sync/lockdep.rs +++ b/rust/kernel/sync/lockdep.rs @@ -137,3 +137,25 @@ macro_rules! optional_name { $crate::c_str!($name) }; } + +/// Not checked, catch all. +pub const LD_WAIT_INV: u8 = bindings::lockdep_wait_type_LD_WAIT_INV as u8; +/// Spin loops, `raw_spinlock_t` etc +pub const LD_WAIT_SPIN: u8 = bindings::lockdep_wait_type_LD_WAIT_SPIN as u8; + +/// Helper for declaring a raw `struct lockdep_map` for locks in statics. +/// +/// It's up to the caller to use the returned `struct lockdep_map` correctly. +#[cfg(CONFIG_DEBUG_LOCK_ALLOC)] +pub(crate) const fn raw_lockdep_map( + name: &'static CStr, + wait_type_inner: u8, + wait_type_outer: u8, +) -> bindings::lockdep_map { + // SAFETY: All zeros is valid for this type. + let mut map: bindings::lockdep_map = unsafe { core::mem::zeroed() }; + map.name = kernel::str::as_char_ptr_in_const_context(name); + map.wait_type_inner = wait_type_inner; + map.wait_type_outer = wait_type_outer; + map +} -- 2.56.0.rc1.315.gc6ed9934b7-goog