Skip to content

Commit

Permalink
Merge pull request torvalds#217 from wedsonaf/exports
Browse files Browse the repository at this point in the history
Expose `signal_pending` and `cond_resched` to drivers.
  • Loading branch information
ojeda authored Apr 23, 2021
2 parents 3b8575d + f51b971 commit 75cb572
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
6 changes: 6 additions & 0 deletions rust/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 2 additions & 4 deletions rust/kernel/sync/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 18 additions & 1 deletion rust/kernel/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! }
//! ```
use crate::{bindings, CStr};
use crate::{bindings, c_types, CStr};
use core::pin::Pin;

mod arc;
Expand All @@ -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.
Expand Down Expand Up @@ -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 }
}

0 comments on commit 75cb572

Please sign in to comment.