From: Igor Belousov A reference counted `Page` can be shared through `ARef`s, but its data can then only be accessed with byte-wise atomic operations, and there is no way to look up a page lock-free the way brd_lookup_page() does. Add a `Guarded` page kind for a reference counted page used as plain memory, such as a page holding the contents of a RAM disk: - `Page::alloc_guarded()` allocates a zeroed `Page`; - `Page::lock_data()` takes a bit spinlock in the page's `private` field and returns a guard that releases it. While held, the guard gives access to the data of the page as a slice, so a page that is shared through `ARef`s can be read and written with plain memory accesses, such as the bio segment copies. This gives a page a lock of its own at no memory cost; - `Page::try_get_raw()` takes a reference from a possibly stale pointer with `get_page_unless_zero()`, for lock-free lookups like brd_lookup_page(). This is sound because a `struct page` is never freed, even once its memory is. The caller must confirm that the page is still the one it looked up before it uses the reference. A `Page` only dereferences to `Page`, not to `Page`: the safe operations that access the data of a page otherwise, such as `with_page_as_sysmem_local()`, or that hand the page to someone who could, such as `vm_insert_page()`, cannot be applied to it. So the lock cannot be bypassed, and `lock_data()` is safe. Add Rust helpers for `get_page_unless_zero()`, `bit_spin_lock()` and `bit_spin_unlock()`. Signed-off-by: Igor Belousov Signed-off-by: Vitaly Wool Co-Authored-By: Claude Opus 5.5 --- rust/helpers/bit_spinlock.c | 13 ++ rust/helpers/helpers.c | 1 + rust/helpers/page.c | 5 + rust/kernel/page.rs | 235 ++++++++++++++++++++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 rust/helpers/bit_spinlock.c diff --git a/rust/helpers/bit_spinlock.c b/rust/helpers/bit_spinlock.c new file mode 100644 index 000000000000..5c59632bd171 --- /dev/null +++ b/rust/helpers/bit_spinlock.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +__rust_helper void rust_helper_bit_spin_lock(int bitnum, unsigned long *addr) +{ + bit_spin_lock(bitnum, addr); +} + +__rust_helper void rust_helper_bit_spin_unlock(int bitnum, unsigned long *addr) +{ + bit_spin_unlock(bitnum, addr); +} diff --git a/rust/helpers/helpers.c b/rust/helpers/helpers.c index ab8d38b43117..0dc7a587f43c 100644 --- a/rust/helpers/helpers.c +++ b/rust/helpers/helpers.c @@ -44,6 +44,7 @@ #include "barrier.c" #include "binder.c" #include "bitmap.c" +#include "bit_spinlock.c" #include "bitops.c" #include "blk.c" #include "bug.c" diff --git a/rust/helpers/page.c b/rust/helpers/page.c index 313a3a3b30b7..071de61112af 100644 --- a/rust/helpers/page.c +++ b/rust/helpers/page.c @@ -47,3 +47,8 @@ __rust_helper int rust_helper_page_to_nid(const struct page *page) return page_to_nid(page); } #endif + +__rust_helper bool rust_helper_get_page_unless_zero(struct page *page) +{ + return get_page_unless_zero(page); +} diff --git a/rust/kernel/page.rs b/rust/kernel/page.rs index 969dd6752841..50a965917190 100644 --- a/rust/kernel/page.rs +++ b/rust/kernel/page.rs @@ -16,6 +16,10 @@ code::*, Result, // }, + ffi::{ + c_int, + c_ulong, // + }, io::{ Io, IoCopyable, @@ -29,6 +33,7 @@ AlwaysRefCounted, // }, types::{ + NotThreadSafe, Opaque, Ownable, Owned, // @@ -178,6 +183,7 @@ impl PageKind for RefCounted {} /// - `Page` is a page for which nothing is known beyond its validity. /// - `Page` is a page whose data is initialized and not referenced by Rust references. /// - `Page`, or just `Page`, is a page that is managed by the page reference count. +/// - `Page` is a reference counted page whose data is only accessed under a lock. /// - `Page` is a uniquely owned page that references cannot be taken to. /// - `Page` is a uniquely owned page whose data follows standard Rust aliasing rules. /// @@ -709,6 +715,235 @@ pub fn with_page_as_sysmem_local( } } +/// A [`PageKind`] for a reference counted page whose data is only accessed under a lock. +/// +/// Like a [`Page`], a [`Page`] is managed by the page reference count and +/// shared through [`ARef`]s. Its data, however, can only be reached through [`Page::lock_data`], +/// which takes a bit spinlock in the `private` field of the page. Since that lock serializes all +/// accesses to the data, the data can be accessed through plain Rust references while the lock is +/// held, see [`PageDataGuard::with_slice`] and [`PageDataGuard::with_mut_slice`]. +/// +/// A [`Page`] only dereferences to a [`Page`]. Thus, the operations that access the +/// data of a page in other ways, or that hand the page to someone who could, such as mapping it to +/// user space, are not available for it. This is what makes [`Page::lock_data`] safe: the lock +/// cannot be bypassed. +/// +/// A [`Page`] suits a page used as plain memory that is shared between threads, such as +/// a page holding the contents of a RAM disk. +/// +/// # Invariants +/// +/// For a [`Page`]: +/// +/// - The page was allocated by [`Page::alloc_guarded`]. Its allocation is managed by the page +/// reference count, and the reference count is non-zero. +/// - The data of the page is initialized. +/// - The `private` field of the page is only accessed as the bit spinlock of +/// [`Page::lock_data`], and the data of the page is only accessed while holding that lock. +pub struct Guarded; + +impl private::Sealed for Guarded {} +impl PageKind for Guarded {} + +impl Deref for Page { + type Target = Page; + + #[inline] + fn deref(&self) -> &Self::Target { + // SAFETY: `Page` is transparent over `struct page` for all page kinds, so the pointer cast + // is valid. `Base` has no invariants in addition to the ones of `Page`, which hold for + // `self`. + unsafe { &*ptr::from_ref(self).cast() } + } +} + +/// The bit of `page->private` that [`Page::lock_data`] uses as a bit spinlock. +const GUARDED_LOCK_BIT: c_int = 0; + +impl Page { + /// Allocates a new guarded page. + /// + /// The page is always zeroed. That is, `__GFP_ZERO` is added to `flags`. + /// + /// # Examples + /// + /// ``` + /// use kernel::page::Page; + /// + /// let page = Page::alloc_guarded(GFP_KERNEL)?; + /// + /// let data = [0xdeu8, 0xad, 0xbe, 0xef]; + /// let mut buf = [0u8; 4]; + /// { + /// let mut guard = page.lock_data(); + /// guard.with_mut_slice(|s| s[..4].copy_from_slice(&data)); + /// guard.with_slice(|s| buf.copy_from_slice(&s[..4])); + /// } + /// assert_eq!(buf, data); + /// # Ok::<(), kernel::alloc::AllocError>(()) + /// ``` + #[inline] + pub fn alloc_guarded(flags: Flags) -> Result, AllocError> { + let flags = flags | __GFP_ZERO; + + // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it + // is always safe to call this method. + let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) }; + let page = NonNull::new(page).ok_or(AllocError)?; + // INVARIANT: The page was just allocated with `alloc_pages()`, so it is managed by the + // page reference count, and we hold a reference. We allocated with `__GFP_ZERO`, so the + // data of the page is initialized. The page allocator clears `private` when it hands out + // a page, so the lock starts out released, and the page is newly allocated, so nothing + // else accesses its `private` field or its data. + // SAFETY: We just successfully allocated a page, so we own the reference count returned + // by `alloc_pages`, and we transfer that reference to the new `ARef`. Since `Page` is + // transparent, we can cast the pointer directly. + Ok(unsafe { ARef::from_raw(page.cast()) }) + } + + /// Attempts to take a reference to a page from a raw, possibly stale pointer. + /// + /// Returns a new reference on success, or `None` if the reference count of the page had + /// already dropped to zero, i.e. if its last owner released it. + /// + /// This allows a lock-free lookup, such as `brd_lookup_page()`, to turn a page pointer it + /// loaded into a reference without knowing whether the page is still alive. A caller that + /// gets a reference checks that the pointer is still published where it was loaded from, since + /// the page may have been freed and reallocated in the meantime. + /// + /// # Safety + /// + /// - `ptr` must have been obtained from a [`Page`], for instance with + /// [`ARef::into_raw`]. Unlike most kernel objects, a `struct page` is never freed: only the + /// memory it describes is, once its reference count drops to zero. So it is safe to operate + /// on the reference count through a pointer that was once validly obtained, even if the page + /// has been released since. + /// - The page may have been freed and reallocated to another user since, who may manage it by + /// other means than the page reference count, and who may use its `private` field and its + /// data in any way. Thus, the caller must not use the returned reference other than to drop + /// it until it has confirmed that the page is still the [`Page`] it looked up. + #[inline] + pub unsafe fn try_get_raw(ptr: NonNull) -> Option> { + // SAFETY: By the safety requirements of this function, `ptr` points to a `struct page`. + if unsafe { bindings::get_page_unless_zero(ptr.as_ptr().cast()) } { + // INVARIANT: The reference count was non-zero, so the page is managed by it, and we + // just took a reference. The caller confirms that the page is still a + // `Page` before it uses it otherwise than by dropping it. + // SAFETY: We just took a reference to the page, which we transfer to the `ARef`. + Some(unsafe { ARef::from_raw(ptr) }) + } else { + None + } + } + + /// Returns a pointer to the `private` field of this page. + #[inline] + fn private_ptr(&self) -> *mut c_ulong { + // SAFETY: `self.as_ptr()` points to a valid `struct page` by the type invariant. Taking + // the address of a field neither reads nor writes it. + unsafe { &raw mut (*self.as_ptr()).__bindgen_anon_1.__bindgen_anon_1.private } + } + + /// Locks the data of this page. + /// + /// Spins until the bit spinlock in the `private` field of the page is released, then takes + /// it. The lock is released when the returned guard is dropped. As with any spinlock, + /// preemption is disabled while it is held. Bit spinlocks are not tracked by lockdep. + /// + /// While the lock is held, the guard gives access to the data of the page as a slice, see + /// [`PageDataGuard::with_slice`] and [`PageDataGuard::with_mut_slice`]. + #[inline] + pub fn lock_data(&self) -> PageDataGuard<'_> { + // SAFETY: `private_ptr()` returns a valid pointer, and by the type invariant, the field is + // only accessed by this lock. + unsafe { bindings::bit_spin_lock(GUARDED_LOCK_BIT, self.private_ptr()) }; + // INVARIANT: The lock was just acquired. + PageDataGuard { + page: self, + _not_send: NotThreadSafe, + } + } +} + +/// The lock on the data of a [`Page`]. +/// +/// Created by [`Page::lock_data`]. The lock is released when the guard is dropped. While it is +/// held, the guard gives access to the data of the page as a slice. +/// +/// # Invariants +/// +/// The guard holds the lock of [`Page::lock_data`] on `page`. +#[must_use = "the lock is released immediately when the guard is unused"] +pub struct PageDataGuard<'a> { + page: &'a Page, + _not_send: NotThreadSafe, +} + +impl PageDataGuard<'_> { + /// Runs `f` with the data of the locked page as a slice of [`PAGE_SIZE`] bytes. + #[inline] + pub fn with_slice(&self, f: impl FnOnce(&[u8]) -> T) -> T { + self.page.with_page_mapped(|ptr| { + // SAFETY: + // - `with_page_mapped` maps the page at `ptr` for `PAGE_SIZE` bytes for the duration + // of this closure, and the slice does not escape it. + // - By the invariant of `Guarded`, the data of the page is initialized. + // - By the invariant of `Guarded` and the type invariant, nobody else accesses the + // data of the page while we hold the lock, and it is only written through + // `with_mut_slice`, which takes `&mut self` and thus cannot run while this shared + // borrow of `self` exists. + f(unsafe { core::slice::from_raw_parts(ptr, PAGE_SIZE) }) + }) + } + + /// Runs `f` with the data of the locked page as a mutable slice of [`PAGE_SIZE`] bytes. + #[inline] + pub fn with_mut_slice(&mut self, f: impl FnOnce(&mut [u8]) -> T) -> T { + self.page.with_page_mapped(|ptr| { + // SAFETY: + // - `with_page_mapped` maps the page at `ptr` for `PAGE_SIZE` bytes for the duration + // of this closure, and the slice does not escape it. + // - By the invariant of `Guarded`, the data of the page is initialized, and safe code + // can only write initialized bytes through the slice. + // - By the invariant of `Guarded` and the type invariant, nobody else accesses the + // data of the page while we hold the lock, and `&mut self` makes this the only + // access through the guard. + f(unsafe { core::slice::from_raw_parts_mut(ptr, PAGE_SIZE) }) + }) + } +} + +impl Drop for PageDataGuard<'_> { + #[inline] + fn drop(&mut self) { + // SAFETY: By the type invariant, we hold the lock. + unsafe { bindings::bit_spin_unlock(GUARDED_LOCK_BIT, self.page.private_ptr()) }; + } +} + +// SAFETY: By the type invariant, a `Page` is managed by the page reference count. +// Incrementing it via `get_page()` keeps the page alive at least until a matching `put_page()`, +// which frees the page when the count reaches zero. +unsafe impl aref::RefCounted for Page { + #[inline] + fn inc_ref(&self) { + // SAFETY: By the type invariant, `self.as_ptr()` points to a valid `struct page` with a + // non-zero reference count. + unsafe { bindings::get_page(self.as_ptr()) }; + } + + #[inline] + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: By the function safety requirements, the caller relinquishes a reference count + // on the page. Since `Page` is transparent, we can cast the pointer directly. + unsafe { bindings::put_page(obj.cast().as_ptr()) }; + } +} + +// SAFETY: A `&Page` proves that the reference count is non-zero, so it is okay to obtain +// an `ARef>` from it. +unsafe impl AlwaysRefCounted for Page {} + // SAFETY: By the type invariant, a `Page` is managed by the page reference count. // Incrementing it via `get_page()` keeps the page alive at least until a matching `put_page()`, // which frees the page when the count reaches zero. -- 2.39.5