From nobody Sat Nov 30 02:57:00 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 09B831C1731 for ; Thu, 12 Sep 2024 19:06:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167968; cv=none; b=umdsZioGlxaLnbdmP//nFC+vCMgh//xnkJmbvQO3rxNjmNq4FI3TtrYfjSLl79CVZvHj3MDWO92knvN/sHYiebvOdc3Qnda+qq0i5L1KHonVxqhBDtXY1GS6gNMBazVpfkIPc6QASrB0fyFzDLxfq4+RcEHUEL/h4SudrmGoXrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167968; c=relaxed/simple; bh=aUCb3E4fwDe14SGb551CJJxNiEUdiFN6FsgRj8WtT9I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nDa70SdPP8tGfzcOumwVxCi9EyBOvX1fRfPDgrXivBDYdwRHt72GJudaP2uQHwEXMo6Act/NYym2nOXGi0Bz1C2cYDo5QI0ZGM0odPKsdBAzen6X4IOAI70mm80REBVIL09z6zdyQyhPNdqJqhRr7KsTGRsI7shsDJQ2hy3SlXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=DmFGdaVw; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="DmFGdaVw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726167966; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ENrAML7KbA1Q5b573pfU+EdAs+FhPf7qf7Vmr13bKQg=; b=DmFGdaVwjq/P1acWOlrVwcl9HS4XT7CQe3GKHeDrOjxU/X6PK/fO8bV531rCOV6DSQ4vB0 DijNCuiqcLZQP9DX40QXa3cJ74bcAQ+JIcjdMiJ2EnGuSiG5hY0KfUJIO1GtLRUKW24IB6 x4cPKPgWe5xSwKwXZQfOV6z6ny/gtAg= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-647-6HUIZljXObGcDf_py-gYtg-1; Thu, 12 Sep 2024 15:06:01 -0400 X-MC-Unique: 6HUIZljXObGcDf_py-gYtg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 17C12195608C; Thu, 12 Sep 2024 19:05:59 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.9.159]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6C86A19560B5; Thu, 12 Sep 2024 19:05:53 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , airlied@redhat.com, Ingo Molnar , Will Deacon , Waiman Long , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , FUJITA Tomonori , Valentin Obst Subject: [PATCH v5 1/3] rust: Introduce irq module Date: Thu, 12 Sep 2024 15:04:16 -0400 Message-ID: <20240912190540.53221-2-lyude@redhat.com> In-Reply-To: <20240912190540.53221-1-lyude@redhat.com> References: <20240912190540.53221-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" This introduces a module for dealing with interrupt-disabled contexts, including the ability to enable and disable interrupts (with_irqs_disabled()) - along with the ability to annotate functions as expecting that IRQs are already disabled on the local CPU. Signed-off-by: Lyude Paul Reviewed-by: Benno Lossin Reviewed-by: Daniel Almeida Reviewed-by: Gary Guo --- V2: * Actually make it so that we check whether or not we have interrupts disabled with debug assertions * Fix issues in the documentation (added suggestions, missing periods, made sure that all rustdoc examples compile properly) * Pass IrqDisabled by value, not reference * Ensure that IrqDisabled is !Send and !Sync using PhantomData<(&'a (), *mut ())> * Add all of the suggested derives from Benno Lossin V3: * Use `impl` for FnOnce bounds in with_irqs_disabled() * Use higher-ranked trait bounds for the lifetime of with_irqs_disabled() * Wording changes in the documentation for the module itself V4: * Use the actual unsafe constructor for IrqDisabled in with_irqs_disabled() * Fix comment style in with_irqs_disabled example * Check before calling local_irq_restore() in with_irqs_disabled that interrupts are still disabled. It would have been nice to do this from a Drop implementation like I hoped, but I realized rust doesn't allow that for types that implement Copy. * Document that interrupts can't be re-enabled within the `cb` provided to `with_irqs_disabled`, and link to the github issue I just filed about this that describes the solution for this. V5: * Rebase against rust-next for the helpers split * Fix typo (enabled -> disabled) - Dirk Signed-off-by: Lyude Paul --- rust/helpers/helpers.c | 1 + rust/helpers/irq.c | 22 +++++++++++ rust/kernel/irq.rs | 90 ++++++++++++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 1 + 4 files changed, 114 insertions(+) create mode 100644 rust/helpers/irq.c create mode 100644 rust/kernel/irq.rs diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index 30f40149f3a96..0bb48df454bd8 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 "irq.c" #include "kunit.c" #include "mutex.c" #include "page.c" diff --git a/rust/helpers/irq.c b/rust/helpers/irq.c new file mode 100644 index 0000000000000..ec1e8d700a220 --- /dev/null +++ b/rust/helpers/irq.c @@ -0,0 +1,22 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +unsigned long rust_helper_local_irq_save(void) +{ + unsigned long flags; + + local_irq_save(flags); + + return flags; +} + +void rust_helper_local_irq_restore(unsigned long flags) +{ + local_irq_restore(flags); +} + +bool rust_helper_irqs_disabled(void) +{ + return irqs_disabled(); +} diff --git a/rust/kernel/irq.rs b/rust/kernel/irq.rs new file mode 100644 index 0000000000000..0673087161f08 --- /dev/null +++ b/rust/kernel/irq.rs @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Interrupt controls +//! +//! This module allows Rust code to control processor interrupts. [`with_i= rqs_disabled()`] may be +//! used for nested disables of interrupts, whereas [`IrqDisabled`] can be= used for annotating code +//! that requires interrupts to be disabled. + +use bindings; +use core::marker::*; + +/// A token that is only available in contexts where IRQs are disabled. +/// +/// [`IrqDisabled`] is marker made available when interrupts are not activ= e. Certain functions take +/// an [`IrqDisabled`] in order to indicate that they may only be run in I= RQ-free contexts. +/// +/// This is a marker type; it has no size, and is simply used as a compile= -time guarantee that +/// interrupts are disabled where required. +/// +/// This token can be created by [`with_irqs_disabled`]. See [`with_irqs_d= isabled`] for examples and +/// further information. +#[derive(Copy, Clone, Debug, Ord, Eq, PartialOrd, PartialEq, Hash)] +pub struct IrqDisabled<'a>(PhantomData<(&'a (), *mut ())>); + +impl IrqDisabled<'_> { + /// Create a new [`IrqDisabled`] without disabling interrupts. + /// + /// This creates an [`IrqDisabled`] token, which can be passed to func= tions that must be run + /// without interrupts. If debug assertions are enabled, this function= will assert that + /// interrupts are disabled upon creation. Otherwise, it has no size o= r cost at runtime. + /// + /// # Panics + /// + /// If debug assertions are enabled, this function will panic if inter= rupts are not disabled + /// upon creation. + /// + /// # Safety + /// + /// This function must only be called in contexts where it is already = known that interrupts have + /// been disabled for the current CPU, as the user is making a promise= that they will remain + /// disabled at least until this [`IrqDisabled`] is dropped. + pub unsafe fn new() -> Self { + // SAFETY: FFI call with no special requirements + debug_assert!(unsafe { bindings::irqs_disabled() }); + + Self(PhantomData) + } +} + +/// Run the closure `cb` with interrupts disabled on the local CPU. +/// +/// This creates an [`IrqDisabled`] token, which can be passed to function= s that must be run +/// without interrupts. Note that interrupts must be disabled for the enti= re duration of `cb`, they +/// cannot be re-enabled. In the future, this may be expanded on +/// [as documented here](https://github.com/Rust-for-Linux/linux/issues/11= 15). +/// +/// # Examples +/// +/// Using [`with_irqs_disabled`] to call a function that can only be calle= d with interrupts +/// disabled: +/// +/// ``` +/// use kernel::irq::{IrqDisabled, with_irqs_disabled}; +/// +/// // Requiring interrupts be disabled to call a function +/// fn dont_interrupt_me(_irq: IrqDisabled<'_>) { +/// // When this token is available, IRQs are known to be disabled. Ac= tions that rely on this +/// // can be safely performed +/// } +/// +/// // Disabling interrupts. They'll be re-enabled once this closure compl= etes. +/// with_irqs_disabled(|irq| dont_interrupt_me(irq)); +/// ``` +#[inline] +pub fn with_irqs_disabled(cb: impl for<'a> FnOnce(IrqDisabled<'a>) -> T= ) -> T { + // SAFETY: FFI call with no special requirements + let flags =3D unsafe { bindings::local_irq_save() }; + + // SAFETY: We just disabled IRQs using `local_irq_save()` + let ret =3D cb(unsafe { IrqDisabled::new() }); + + // Confirm that IRQs are still disabled now that the callback has fini= shed + // SAFETY: FFI call with no special requirements + debug_assert!(unsafe { bindings::irqs_disabled() }); + + // SAFETY: `flags` comes from our previous call to local_irq_save + unsafe { bindings::local_irq_restore(flags) }; + + ret +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index f10b06a78b9d5..df10c58e95c19 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 irq; #[cfg(CONFIG_KUNIT)] pub mod kunit; pub mod list; --=20 2.46.0 From nobody Sat Nov 30 02:57:00 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA96F1C2327 for ; Thu, 12 Sep 2024 19:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167972; cv=none; b=aojZC3i8QdhjlF39OLmrRiRx5kRaz/qeCxTA8s51Ak4+frYBNN37Bz3GEBrqQJUXTHFVkxaFIlRtozicrAKnEKMLJI3KPN1ib6jhEGnb+gqYGleQC+5kwGg5kILAgz+4ChgYfaCMXa7vzI9dOiKfYGEN8V66wlyRFqeBTCyDOBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167972; c=relaxed/simple; bh=/rdYGX7UjvkB2gk5fsyYiBzOkcUaojbBXtAJ5ZK88U8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JaT8wcAjlktOZGUIT5dzO6z49/sOyR1Tq6JKHiVQxam8eTcHe+1Cv/HUgSmOdtf+UUD86LYEmGBzIkyJERBRVD0gp6tJqUaCVt/vk2HoWsKLtFhDn3IwjZKs+Yl18biqh1ZCGR098VW98zapWawnQk4R/LgBQiCyv1peuGN36hU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zgp8AbKy; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zgp8AbKy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726167969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WKQklmomFwAhOEFxjHGrCGOmakddEyukxyf0Xx3I0gQ=; b=Zgp8AbKyEsRK0Ydv1GUGc8mydkQOUrh8HOFar5Aedb7snjFzMIP3axqFJ6nqlvICrFCt9I xfbr+rhoFrWMTPI/uys71CqyLwecYZdr9fC+gezRJKVJRRnF4UwU2ifRArEpNdg5OZTxC1 8jFwFv+8jXEl/u5Z2TBsfyI1FZJ0c2s= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-IQ4apj-_MBKDrbtMdN8_uw-1; Thu, 12 Sep 2024 15:06:08 -0400 X-MC-Unique: IQ4apj-_MBKDrbtMdN8_uw-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E369F1944AB1; Thu, 12 Sep 2024 19:06:05 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.9.159]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4B24C19560B5; Thu, 12 Sep 2024 19:06:00 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , airlied@redhat.com, Ingo Molnar , Will Deacon , Waiman Long , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Martin Rodriguez Reboredo , Valentin Obst Subject: [PATCH v5 2/3] rust: sync: Introduce lock::Backend::Context Date: Thu, 12 Sep 2024 15:04:17 -0400 Message-ID: <20240912190540.53221-3-lyude@redhat.com> In-Reply-To: <20240912190540.53221-1-lyude@redhat.com> References: <20240912190540.53221-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Now that we've introduced an `IrqDisabled` token for marking contexts in which IRQs are disabled, we need a way to be able to pass it to locks that require that IRQs are disabled. In order to continue using the `lock::Backend` type instead of inventing our own thing, we accomplish this by adding the associated Context type, along with a `lock_with()` function that can accept a Context when acquiring a lock. To allow current users of context-less locks to keep using the normal `lock()` method, we take an example from Wedson Almeida Filho's work and add a `where T<'a>: Default` bound to `lock()` so that it can only be called on lock types where the context is simply a placeholder value, then re-implement it through the new `lock_with()` function. Signed-off-by: Lyude Paul Reviewed-by: Benno Lossin Reviewed-by: Daniel Almeida Reviewed-by: Gary Guo --- V3: * Use explicit lifetimes in lock_with() to ensure self and _context have the same lifetime (Benno) * Use () for locks that don't need a Context instead of PhantomData (Benno) V4: * Fix typo (Dirk) Signed-off-by: Lyude Paul --- rust/kernel/sync/lock.rs | 17 +++++++++++++++-- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 1 + 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index f6c34ca4d819f..7429bda852173 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -38,6 +38,9 @@ pub unsafe trait Backend { /// [`unlock`]: Backend::unlock type GuardState; =20 + /// The context which must be provided to acquire the lock. + type Context<'a>; + /// Initialises the lock. /// /// # Safety @@ -120,14 +123,24 @@ pub fn new(t: T, name: &'static CStr, key: &'static L= ockClassKey) -> impl PinIni } =20 impl Lock { - /// Acquires the lock and gives the caller access to the data protecte= d by it. - pub fn lock(&self) -> Guard<'_, T, B> { + /// Acquires the lock with the given context and gives the caller acce= ss to the data protected + /// by it. + pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, = T, B> { // SAFETY: The constructor of the type calls `init`, so the existe= nce of the object proves // that `init` was called. let state =3D unsafe { B::lock(self.state.get()) }; // SAFETY: The lock was just acquired. unsafe { Guard::new(self, state) } } + + /// Acquires the lock and gives the caller access to the data protecte= d by it. + #[inline] + pub fn lock<'a>(&'a self) -> Guard<'a, T, B> + where + B::Context<'a>: Default, + { + self.lock_with(B::Context::default()) + } } =20 /// A lock guard. diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 30632070ee670..7c2c239944931 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -93,6 +93,7 @@ macro_rules! new_mutex { unsafe impl super::Backend for MutexBackend { type State =3D bindings::mutex; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index ea5c5bc1ce12e..97d85a5576615 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -92,6 +92,7 @@ macro_rules! new_spinlock { unsafe impl super::Backend for SpinLockBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, --=20 2.46.0 From nobody Sat Nov 30 02:57:00 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 75C0D1C2434 for ; Thu, 12 Sep 2024 19:06:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167979; cv=none; b=mJwApLRZU2sqoQnXw8qrHdC9u7NxvBe5xjbG2O0Q7qHjG1nTtT6E7JsdW1Zm5lDe+WYW7jFBtUr3KNSOkt7FbHhCOXPvYDTMunHUJAfpcTHqwEu4s4MYAtVsqAlu17fbqdjvUh66Cyc+SkFk7IO4B1mGLsh91Nh0xdz1Ye2GUYo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167979; c=relaxed/simple; bh=NCiAWhLXHRt4Co7rpuhMDCPctoF3Xn4/Hp4FhL+4OxI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RlynAjFHYP8Uh3+3DNrOHxNfAgpLJq3EyLu706Xh5B0RY2FiRXT2T/YkRHLk9AoHP9tDt1c2QdbJowRYoH7fnjvNBru6qqZHpezYTYeIt0w4qTc/gj8k5TuLn7Mysh7LGJXBcU838k7+PF376hfQ0afLYbezpVh08LAOhMZRW18= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Fo1oENNq; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Fo1oENNq" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726167976; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+OGhrfJ98BvRkmlDl+pbOcc2LBNeMy80VxbOVlvu+v4=; b=Fo1oENNqM/vAFhPq8320BH0BxDX7cQKGDxz6GOhhV/47A9ggF+4ZQvaPbVQb+f3rJvxK02 b0aeyQbq5jAPUjTIGvRlVJx9rsKau5KEBJA4U0ZKyCYwFJUwifss91q4QO5YkRe25mYPqt DncjvheTgGvqQBk0NhNOCsvRXTeiyo4= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-541--kTCIirfN6KJurtP0za4nQ-1; Thu, 12 Sep 2024 15:06:13 -0400 X-MC-Unique: -kTCIirfN6KJurtP0za4nQ-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id F38531955D4C; Thu, 12 Sep 2024 19:06:10 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.9.159]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0320819560B5; Thu, 12 Sep 2024 19:06:06 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , airlied@redhat.com, Ingo Molnar , Will Deacon , Waiman Long , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Martin Rodriguez Reboredo , Valentin Obst Subject: [PATCH v5 3/3] rust: sync: Add SpinLockIrq Date: Thu, 12 Sep 2024 15:04:18 -0400 Message-ID: <20240912190540.53221-4-lyude@redhat.com> In-Reply-To: <20240912190540.53221-1-lyude@redhat.com> References: <20240912190540.53221-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" A variant of SpinLock that is expected to be used in noirq contexts, and thus requires that the user provide an kernel::irq::IrqDisabled to prove they are in such a context upon lock acquisition. This is the rust equivalent of spin_lock_irqsave()/spin_lock_irqrestore(). Signed-off-by: Lyude Paul Reviewed-by: Benno Lossin Reviewed-by: Daniel Almeida Reviewed-by: Gary Guo --- V2: * s/IrqSpinLock/SpinLockIrq/ * Implement `lock::Backend` now that we have `Context` * Add missing periods * Make sure rustdoc examples compile correctly * Add documentation suggestions Signed-off-by: Lyude Paul --- rust/kernel/sync.rs | 2 +- rust/kernel/sync/lock/spinlock.rs | 104 ++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync.rs b/rust/kernel/sync.rs index 0ab20975a3b5d..b028ee325f2a6 100644 --- a/rust/kernel/sync.rs +++ b/rust/kernel/sync.rs @@ -15,7 +15,7 @@ pub use arc::{Arc, ArcBorrow, UniqueArc}; pub use condvar::{new_condvar, CondVar, CondVarTimeoutResult}; pub use lock::mutex::{new_mutex, Mutex}; -pub use lock::spinlock::{new_spinlock, SpinLock}; +pub use lock::spinlock::{new_spinlock, new_spinlock_irq, SpinLock, SpinLoc= kIrq}; pub use locked_by::LockedBy; =20 /// Represents a lockdep class. It's a wrapper around C's `lock_class_key`. diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index 97d85a5576615..47c71d779062a 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -3,6 +3,7 @@ //! A kernel spinlock. //! //! This module allows Rust code to use the kernel's `spinlock_t`. +use kernel::irq::*; =20 /// Creates a [`SpinLock`] initialiser with the given name and a newly-cre= ated lock class. /// @@ -116,3 +117,106 @@ unsafe fn unlock(ptr: *mut Self::State, _guard_state:= &Self::GuardState) { unsafe { bindings::spin_unlock(ptr) } } } + +/// Creates a [`SpinLockIrq`] initialiser with the given name and a newly-= created lock class. +/// +/// It uses the name if one is given, otherwise it generates one based on = the file name and line +/// number. +#[macro_export] +macro_rules! new_spinlock_irq { + ($inner:expr $(, $name:literal)? $(,)?) =3D> { + $crate::sync::SpinLockIrq::new( + $inner, $crate::optional_name!($($name)?), $crate::static_lock= _class!()) + }; +} +pub use new_spinlock_irq; + +/// A spinlock that may be acquired when interrupts are disabled. +/// +/// A version of [`SpinLock`] that can only be used in contexts where inte= rrupts for the local CPU +/// are disabled. It requires that the user acquiring the lock provide pro= of that interrupts are +/// disabled through [`IrqDisabled`]. +/// +/// For more info, see [`SpinLock`]. +/// +/// # Examples +/// +/// The following example shows how to declare, allocate initialise and ac= cess a struct (`Example`) +/// that contains an inner struct (`Inner`) that is protected by a spinloc= k. +/// +/// ``` +/// use kernel::{ +/// sync::{new_spinlock_irq, SpinLockIrq}, +/// irq::{with_irqs_disabled, IrqDisabled} +/// }; +/// +/// struct Inner { +/// a: u32, +/// b: u32, +/// } +/// +/// #[pin_data] +/// struct Example { +/// c: u32, +/// #[pin] +/// d: SpinLockIrq, +/// } +/// +/// impl Example { +/// fn new() -> impl PinInit { +/// pin_init!(Self { +/// c: 10, +/// d <- new_spinlock_irq!(Inner { a: 20, b: 30 }), +/// }) +/// } +/// } +/// +/// // Accessing an `Example` from a function that can only be called in n= o-irq contexts +/// fn noirq_work(e: &Example, irq: IrqDisabled<'_>) { +/// assert_eq!(e.c, 10); +/// assert_eq!(e.d.lock_with(irq).a, 20); +/// } +/// +/// // Allocate a boxed `Example` +/// let e =3D Box::pin_init(Example::new(), GFP_KERNEL)?; +/// +/// // Accessing an `Example` from a context where IRQs may not be disable= d already. +/// let b =3D with_irqs_disabled(|irq| { +/// noirq_work(&e, irq); +/// e.d.lock_with(irq).b +/// }); +/// assert_eq!(b, 30); +/// # Ok::<(), Error>(()) +/// ``` +pub type SpinLockIrq =3D super::Lock; + +/// A kernel `spinlock_t` lock backend that is acquired in no-irq contexts. +pub struct SpinLockIrqBackend; + +unsafe impl super::Backend for SpinLockIrqBackend { + type State =3D bindings::spinlock_t; + type GuardState =3D (); + type Context<'a> =3D IrqDisabled<'a>; + + unsafe fn init( + ptr: *mut Self::State, + name: *const core::ffi::c_char, + key: *mut bindings::lock_class_key, + ) { + // SAFETY: The safety requirements ensure that `ptr` is valid for = writes, and `name` and + // `key` are valid for read indefinitely. + unsafe { bindings::__spin_lock_init(ptr, name, key) } + } + + unsafe fn lock(ptr: *mut Self::State) -> Self::GuardState { + // SAFETY: The safety requirements of this function ensure that `p= tr` points to valid + // memory, and that it has been initialised before. + unsafe { bindings::spin_lock(ptr) } + } + + unsafe fn unlock(ptr: *mut Self::State, _guard_state: &Self::GuardStat= e) { + // SAFETY: The safety requirements of this function ensure that `p= tr` is valid and that the + // caller is the owner of the spinlock. + unsafe { bindings::spin_unlock(ptr) } + } +} --=20 2.46.0