From: Boqun Feng <boqun.feng@gmail.com>
To: Lyude Paul <lyude@redhat.com>
Cc: rust-for-linux@vger.kernel.org,
"Danilo Krummrich" <dakr@redhat.com>,
airlied@redhat.com, "Ingo Molnar" <mingo@redhat.com>,
"Will Deacon" <will@kernel.org>,
"Waiman Long" <longman@redhat.com>,
"Peter Zijlstra" <peterz@infradead.org>,
"Thomas Gleixner" <tglx@linutronix.de>,
linux-kernel@vger.kernel.org,
"Benno Lossin" <benno.lossin@proton.me>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Gary Guo" <gary@garyguo.net>, "Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@samsung.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"FUJITA Tomonori" <fujita.tomonori@gmail.com>,
"Valentin Obst" <kernel@valentinobst.de>
Subject: Re: [PATCH v6 1/3] rust: Introduce irq module
Date: Sun, 29 Sep 2024 16:45:23 -0700 [thread overview]
Message-ID: <ZvnmkwRjxRTDACZD@boqun-archlinux> (raw)
In-Reply-To: <20240916213025.477225-2-lyude@redhat.com>
On Mon, Sep 16, 2024 at 05:28:04PM -0400, Lyude Paul wrote:
> 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 <lyude@redhat.com>
> Reviewed-by: Benno Lossin <benno.lossin@proton.me>
> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com>
> Reviewed-by: Gary Guo <gary@garyguo.net>
>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Regards,
Boqun
> ---
>
> 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
>
> V6:
> * s/indicate/require/ in IrqDisabled docs
> * Reword comment above with_irqs_disabled in code example requested by
> Benno
> * Add paragraph to with_irqs_disabled docs requested by Benno
> * Apply the comments from Boqun's review for V4 that I missed
> * Document type invariants of `IrqDisabled`
>
> This patch depends on
> https://lore.kernel.org/rust-for-linux/ZuKNszXSw-LbgW1e@boqun-archlinux/
>
> ---
> rust/helpers/helpers.c | 1 +
> rust/helpers/irq.c | 22 ++++++++++
> rust/kernel/irq.rs | 96 ++++++++++++++++++++++++++++++++++++++++++
> rust/kernel/lib.rs | 1 +
> 4 files changed, 120 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 <linux/irqflags.h>
> +
> +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..ee3a4549aa389
> --- /dev/null
> +++ b/rust/kernel/irq.rs
> @@ -0,0 +1,96 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! Interrupt controls
> +//!
> +//! This module allows Rust code to control processor interrupts. [`with_irqs_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::*;
> +use crate::types::NotThreadSafe;
> +
> +/// A token that is only available in contexts where IRQs are disabled.
> +///
> +/// [`IrqDisabled`] is marker made available when interrupts are not active. Certain functions take
> +/// an [`IrqDisabled`] in order to require that they may only be run in IRQ-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_disabled`] for examples and
> +/// further information.
> +///
> +/// # Invariants
> +///
> +/// IRQs are disabled when an object of this type exists.
> +#[derive(Copy, Clone, Debug, Ord, Eq, PartialOrd, PartialEq, Hash)]
> +pub struct IrqDisabled<'a>(PhantomData<(&'a (), NotThreadSafe)>);
> +
> +impl IrqDisabled<'_> {
> + /// Create a new [`IrqDisabled`] token in an interrupt disabled context.
> + ///
> + /// This creates an [`IrqDisabled`] token, which can be passed to functions 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 or cost at runtime.
> + ///
> + /// # Panics
> + ///
> + /// If debug assertions are enabled, this function will panic if interrupts 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, and 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() });
> +
> + // INVARIANT: The safety requirements of this function ensure that IRQs are disabled.
> + Self(PhantomData)
> + }
> +}
> +
> +/// Run the closure `cb` with interrupts disabled on the local CPU.
> +///
> +/// This disables interrupts, creates an [`IrqDisabled`] token and passes it to `cb`. The previous
> +/// interrupt state will be restored once the closure completes. Note that interrupts must be
> +/// disabled for the entire 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/1115).
> +///
> +/// # Examples
> +///
> +/// Using [`with_irqs_disabled`] to call a function that can only be called 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. Actions that rely on this
> +/// // can be safely performed
> +/// }
> +///
> +/// // Disables interrupts, their previous state will be restored once the closure completes.
> +/// with_irqs_disabled(|irq| dont_interrupt_me(irq));
> +/// ```
> +#[inline]
> +pub fn with_irqs_disabled<T>(cb: impl for<'a> FnOnce(IrqDisabled<'a>) -> T) -> T {
> + // SAFETY: FFI call with no special requirements
> + let flags = unsafe { bindings::local_irq_save() };
> +
> + // SAFETY: We just disabled IRQs using `local_irq_save()`
> + let ret = cb(unsafe { IrqDisabled::new() });
> +
> + // Confirm that IRQs are still disabled now that the callback has finished
> + // 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;
> --
> 2.46.0
>
next prev parent reply other threads:[~2024-09-29 23:46 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-16 21:28 [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Lyude Paul
2024-09-16 21:28 ` [PATCH v6 1/3] rust: Introduce irq module Lyude Paul
2024-09-29 20:36 ` Trevor Gross
2024-09-29 23:45 ` Boqun Feng [this message]
2024-10-02 20:20 ` Thomas Gleixner
2024-10-04 8:58 ` Benno Lossin
2024-10-04 17:18 ` Lyude Paul
2024-10-17 18:51 ` Lyude Paul
2024-10-04 17:02 ` Lyude Paul
2024-10-10 21:00 ` Daniel Almeida
2024-09-16 21:28 ` [PATCH v6 2/3] rust: sync: Introduce lock::Backend::Context Lyude Paul
2024-09-29 20:40 ` Trevor Gross
2024-09-29 23:52 ` Boqun Feng
2024-09-16 21:28 ` [PATCH v6 3/3] rust: sync: Add SpinLockIrq Lyude Paul
2024-09-29 20:50 ` Trevor Gross
2024-09-29 23:59 ` Boqun Feng
2024-10-02 20:53 ` Thomas Gleixner
2024-10-03 12:51 ` Boqun Feng
2024-10-04 18:48 ` Lyude Paul
2024-10-05 18:19 ` Lyude Paul
2024-10-07 12:42 ` Boqun Feng
2024-10-07 18:13 ` Lyude Paul
2024-10-15 12:57 ` Andreas Hindborg
2024-10-15 20:17 ` Boqun Feng
2024-10-15 20:21 ` Boqun Feng
2024-10-16 20:57 ` Lyude Paul
2024-10-17 13:34 ` Andreas Hindborg
2024-10-07 12:01 ` Thomas Gleixner
2024-10-07 18:30 ` Lyude Paul
2024-10-08 15:21 ` Thomas Gleixner
2024-10-12 8:01 ` Boqun Feng
2024-10-10 16:39 ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Daniel Almeida
2024-10-12 5:29 ` Dirk Behme
2024-10-13 19:06 ` Thomas Gleixner
2024-10-13 21:43 ` Boqun Feng
2024-10-16 21:00 ` Thomas Gleixner
2024-10-16 21:31 ` Boqun Feng
2024-10-17 20:49 ` Lyude Paul
2024-10-17 22:27 ` Boqun Feng
2024-10-18 5:51 ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Boqun Feng
2024-10-18 5:51 ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-21 7:04 ` kernel test robot
2024-10-21 7:35 ` kernel test robot
2024-10-21 20:44 ` Lyude Paul
2024-10-24 16:18 ` Peter Zijlstra
2024-10-23 19:34 ` Thomas Gleixner
2024-10-23 19:51 ` Peter Zijlstra
2024-10-23 20:38 ` Thomas Gleixner
2024-10-24 10:05 ` Peter Zijlstra
2024-10-24 17:22 ` Thomas Gleixner
2024-10-24 21:57 ` Boqun Feng
2024-10-25 15:04 ` Thomas Gleixner
2024-10-25 18:28 ` Peter Zijlstra
2024-10-24 19:12 ` Lyude Paul
2025-07-24 20:36 ` w/r/t "irq & spin_lock: Add counted interrupt disabling/enabling": holes in pcpu_hot? Lyude Paul
2025-07-24 21:59 ` Thomas Gleixner
2024-10-24 5:05 ` [POC 1/6] irq & spin_lock: Add counted interrupt disabling/enabling Boqun Feng
2024-10-24 8:17 ` Thomas Gleixner
2024-10-24 16:20 ` Boqun Feng
2024-10-18 5:51 ` [POC 2/6] rust: Introduce interrupt module Boqun Feng
2024-10-31 20:45 ` Lyude Paul
2024-10-31 20:47 ` Lyude Paul
2024-10-18 5:51 ` [POC 3/6] rust: helper: Add spin_{un,}lock_irq_{enable,disable}() helpers Boqun Feng
2024-10-18 5:51 ` [POC 4/6] rust: sync: Add SpinLockIrq Boqun Feng
2024-10-18 19:23 ` Lyude Paul
2024-10-18 20:22 ` Boqun Feng
2024-10-18 5:51 ` [POC 5/6] rust: sync: Introduce lock::Backend::Context Boqun Feng
2024-10-31 20:54 ` Lyude Paul
2024-10-18 5:51 ` [POC 6/6] rust: sync: lock: Add `Backend::BackendInContext` Boqun Feng
2024-10-18 10:22 ` [POC 0/6] Allow SpinLockIrq to use a normal Guard interface Andreas Hindborg
2024-10-18 12:42 ` Boqun Feng
2024-10-18 11:16 ` Andreas Hindborg
2024-10-18 16:05 ` Dirk Behme
2024-10-31 20:56 ` Lyude Paul
2024-10-17 20:42 ` [PATCH v6 0/3] rust: Add irq abstraction, SpinLockIrq Lyude Paul
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZvnmkwRjxRTDACZD@boqun-archlinux \
--to=boqun.feng@gmail.com \
--cc=a.hindborg@samsung.com \
--cc=airlied@redhat.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=dakr@redhat.com \
--cc=daniel.almeida@collabora.com \
--cc=fujita.tomonori@gmail.com \
--cc=gary@garyguo.net \
--cc=kernel@valentinobst.de \
--cc=linux-kernel@vger.kernel.org \
--cc=longman@redhat.com \
--cc=lyude@redhat.com \
--cc=mingo@redhat.com \
--cc=ojeda@kernel.org \
--cc=peterz@infradead.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tglx@linutronix.de \
--cc=tmgross@umich.edu \
--cc=wedsonaf@gmail.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).