From: Wedson Almeida Filho Date: Fri, 23 Apr 2021 11:55:38 +0000 (+0100) Subject: rust: sync: expose `signal_pending` and `cond_resched` to drivers X-Git-Url: https://www.infradead.org/git/?a=commitdiff_plain;h=a097e1c92c74f56ef803929c4d2ead6ce6a6f6ff;p=users%2Fjedix%2Flinux-maple.git rust: sync: expose `signal_pending` and `cond_resched` to drivers Signed-off-by: Wedson Almeida Filho [normalized title] Signed-off-by: Miguel Ojeda --- diff --git a/rust/helpers.c b/rust/helpers.c index 133fe9119990f..aa1de0eb0cbe3 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -81,6 +81,12 @@ void rust_helper_kunmap(struct page *page) } EXPORT_SYMBOL_GPL(rust_helper_kunmap); +int rust_helper_cond_resched(void) +{ + return cond_resched(); +} +EXPORT_SYMBOL_GPL(rust_helper_cond_resched); + #if !defined(CONFIG_ARM) // See https://github.com/rust-lang/rust-bindgen/issues/1671 static_assert(__builtin_types_compatible_p(size_t, uintptr_t), diff --git a/rust/kernel/sync/condvar.rs b/rust/kernel/sync/condvar.rs index 6d57fb1daea4e..be6012794777c 100644 --- a/rust/kernel/sync/condvar.rs +++ b/rust/kernel/sync/condvar.rs @@ -6,12 +6,11 @@ //! variable. use super::{Guard, Lock, NeedsLockClass}; -use crate::{bindings, c_types, CStr}; +use crate::{bindings, CStr}; use core::{cell::UnsafeCell, marker::PhantomPinned, mem::MaybeUninit, pin::Pin}; extern "C" { fn rust_helper_init_wait(wq: *mut bindings::wait_queue_entry); - fn rust_helper_signal_pending() -> c_types::c_int; } /// Safely initialises a [`CondVar`] with the given name, generating a new lock class. @@ -92,8 +91,7 @@ impl CondVar { // SAFETY: Both `wait` and `wait_list` point to valid memory. unsafe { bindings::finish_wait(self.wait_list.get(), wait.as_mut_ptr()) }; - // SAFETY: No arguments, just checks `current` for pending signals. - unsafe { rust_helper_signal_pending() != 0 } + super::signal_pending() } /// Calls the kernel function to notify the appropriate number of threads with the given flags. diff --git a/rust/kernel/sync/mod.rs b/rust/kernel/sync/mod.rs index 25f5109429a8e..0a8cec021e5c5 100644 --- a/rust/kernel/sync/mod.rs +++ b/rust/kernel/sync/mod.rs @@ -17,7 +17,7 @@ //! } //! ``` -use crate::{bindings, CStr}; +use crate::{bindings, c_types, CStr}; use core::pin::Pin; mod arc; @@ -34,6 +34,11 @@ pub use locked_by::LockedBy; pub use mutex::Mutex; pub use spinlock::SpinLock; +extern "C" { + fn rust_helper_signal_pending() -> c_types::c_int; + fn rust_helper_cond_resched() -> c_types::c_int; +} + /// Safely initialises an object that has an `init` function that takes a name and a lock class as /// arguments, examples of these are [`Mutex`] and [`SpinLock`]. Each of them also provides a more /// specialised name that uses this macro. @@ -66,3 +71,15 @@ pub trait NeedsLockClass { /// `key` must point to a valid memory location as it will be used by the kernel. unsafe fn init(self: Pin<&Self>, name: CStr<'static>, key: *mut bindings::lock_class_key); } + +/// Determines if a signal is pending on the current process. +pub fn signal_pending() -> bool { + // SAFETY: No arguments, just checks `current` for pending signals. + unsafe { rust_helper_signal_pending() != 0 } +} + +/// Reschedules the caller's task if needed. +pub fn cond_resched() -> bool { + // SAFETY: No arguments, reschedules `current` if needed. + unsafe { rust_helper_cond_resched() != 0 } +}