lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240725222822.1784931-2-lyude@redhat.com>
Date: Thu, 25 Jul 2024 18:27:50 -0400
From: Lyude Paul <lyude@...hat.com>
To: rust-for-linux@...r.kernel.org
Cc: Danilo Krummrich <dakr@...hat.com>,
	airlied@...hat.com,
	Ingo Molnar <mingo@...hat.com>,
	Will Deacon <will@...nel.org>,
	Waiman Long <longman@...hat.com>,
	Peter Zijlstra <peterz@...radead.org>,
	Miguel Ojeda <ojeda@...nel.org>,
	Alex Gaynor <alex.gaynor@...il.com>,
	Wedson Almeida Filho <wedsonaf@...il.com>,
	Boqun Feng <boqun.feng@...il.com>,
	Gary Guo <gary@...yguo.net>,
	Björn Roy Baron <bjorn3_gh@...tonmail.com>,
	Benno Lossin <benno.lossin@...ton.me>,
	Andreas Hindborg <a.hindborg@...sung.com>,
	Alice Ryhl <aliceryhl@...gle.com>,
	Martin Rodriguez Reboredo <yakoyoku@...il.com>,
	FUJITA Tomonori <fujita.tomonori@...il.com>,
	Aakash Sen Sharma <aakashsensharma@...il.com>,
	Valentin Obst <kernel@...entinobst.de>,
	linux-kernel@...r.kernel.org (open list)
Subject: [PATCH 1/3] rust: Introduce irq module

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@...hat.com>
---
 rust/helpers.c     | 14 +++++++++
 rust/kernel/irq.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++
 rust/kernel/lib.rs |  1 +
 3 files changed, 89 insertions(+)
 create mode 100644 rust/kernel/irq.rs

diff --git a/rust/helpers.c b/rust/helpers.c
index 87ed0a5b60990..12ac32de820b5 100644
--- a/rust/helpers.c
+++ b/rust/helpers.c
@@ -69,6 +69,20 @@ void rust_helper_spin_unlock(spinlock_t *lock)
 }
 EXPORT_SYMBOL_GPL(rust_helper_spin_unlock);
 
+unsigned long rust_helper_local_irq_save(void) {
+	unsigned long flags;
+
+	local_irq_save(flags);
+
+	return flags;
+}
+EXPORT_SYMBOL_GPL(rust_helper_local_irq_save);
+
+void rust_helper_local_irq_restore(unsigned long flags) {
+	local_irq_restore(flags);
+}
+EXPORT_SYMBOL_GPL(rust_helper_local_irq_restore);
+
 void rust_helper_init_wait(struct wait_queue_entry *wq_entry)
 {
 	init_wait(wq_entry);
diff --git a/rust/kernel/irq.rs b/rust/kernel/irq.rs
new file mode 100644
index 0000000000000..8a540bd6123f7
--- /dev/null
+++ b/rust/kernel/irq.rs
@@ -0,0 +1,74 @@
+// 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 that interrupts already be disabled.
+
+use bindings;
+use core::marker::*;
+
+/// A guarantee that IRQs are disabled on this CPU
+///
+/// An [`IrqDisabled`] represents a guarantee that interrupts will remain disabled on the current CPU
+/// until the lifetime of the object ends. However, it does not disable or enable interrupts on its
+/// own - see [`with_irqs_disabled()`] for that.
+///
+/// This object has no cost at runtime (TODO: …except if whatever kernel compile-time option that
+/// would assert IRQs are enabled or not is enabled - in which case we should actually verify that
+/// they're enabled).
+///
+/// # Examples
+///
+/// If you want to ensure that a function may only be invoked within contexts where interrupts are
+/// disabled, you can do so by requiring that a reference to this type be passed. You can also
+/// create this type using unsafe code in order to indicate that it's known that interrupts are
+/// already disabled on this CPU
+///
+/// ```
+/// use kernel::irq::{IrqDisabled, disable_irqs};
+///
+/// // Requiring interrupts be disabled to call a function
+/// fn dont_interrupt_me(_irq: &IrqDisabled<'_>) { }
+///
+/// // Disabling interrupts. They'll be re-enabled once this closure completes.
+/// disable_irqs(|irq| dont_interrupt_me(&irq));
+/// ```
+pub struct IrqDisabled<'a>(PhantomData<&'a ()>);
+
+impl<'a> IrqDisabled<'a> {
+    /// Create a new [`IrqDisabled`] without disabling interrupts
+    ///
+    /// If debug assertions are enabled, this function will check that interrupts are disabled.
+    /// Otherwise, it has no cost at runtime.
+    ///
+    /// # 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 {
+        Self(PhantomData)
+    }
+}
+
+/// Run the closure `cb` with interrupts disabled on the local CPU.
+///
+/// Interrupts will be re-enabled once the closure returns. If interrupts were already disabled on
+/// this CPU, this is a no-op.
+#[inline]
+pub fn with_irqs_disabled<T, F>(cb: F) -> T
+where
+    F: FnOnce(IrqDisabled<'_>) -> T,
+{
+    // SAFETY: FFI call with no special requirements
+    let flags = unsafe { bindings::local_irq_save() };
+
+    let ret = cb(IrqDisabled(PhantomData));
+
+    // 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 e6b7d3a80bbce..37835ccd51087 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;
 #[cfg(CONFIG_NET)]
-- 
2.45.2


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ