|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +//! Groups of contiguous pages, folios. |
| 4 | +//! |
| 5 | +//! C headers: [`include/linux/mm.h`](../../include/linux/mm.h) |
| 6 | +
|
| 7 | +use crate::error::{code::*, Result}; |
| 8 | +use crate::types::{AlwaysRefCounted, Opaque, ScopeGuard}; |
| 9 | +use core::{cmp::min, ptr}; |
| 10 | + |
| 11 | +/// Wraps the kernel's `struct folio`. |
| 12 | +/// |
| 13 | +/// # Invariants |
| 14 | +/// |
| 15 | +/// Instances of this type are always ref-counted, that is, a call to `folio_get` ensures that the |
| 16 | +/// allocation remains valid at least until the matching call to `folio_put`. |
| 17 | +#[repr(transparent)] |
| 18 | +pub struct Folio(Opaque<bindings::folio>); |
| 19 | + |
| 20 | +// SAFETY: The type invariants guarantee that `Folio` is always ref-counted. |
| 21 | +unsafe impl AlwaysRefCounted for Folio { |
| 22 | + fn inc_ref(&self) { |
| 23 | + // SAFETY: The existence of a shared reference means that the refcount is nonzero. |
| 24 | + unsafe { bindings::folio_get(self.0.get()) }; |
| 25 | + } |
| 26 | + |
| 27 | + unsafe fn dec_ref(obj: ptr::NonNull<Self>) { |
| 28 | + // SAFETY: The safety requirements guarantee that the refcount is nonzero. |
| 29 | + unsafe { bindings::folio_put(obj.cast().as_ptr()) } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl Folio { |
| 34 | + /// Returns the byte position of this folio in its file. |
| 35 | + pub fn pos(&self) -> i64 { |
| 36 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 37 | + unsafe { bindings::folio_pos(self.0.get()) } |
| 38 | + } |
| 39 | + |
| 40 | + /// Returns the byte size of this folio. |
| 41 | + pub fn size(&self) -> usize { |
| 42 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 43 | + unsafe { bindings::folio_size(self.0.get()) } |
| 44 | + } |
| 45 | + |
| 46 | + /// Flushes the data cache for the pages that make up the folio. |
| 47 | + pub fn flush_dcache(&self) { |
| 48 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 49 | + unsafe { bindings::flush_dcache_folio(self.0.get()) } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// A locked [`Folio`]. |
| 54 | +pub struct LockedFolio<'a>(&'a Folio); |
| 55 | + |
| 56 | +impl LockedFolio<'_> { |
| 57 | + /// Creates a new locked folio from a raw pointer. |
| 58 | + /// |
| 59 | + /// # Safety |
| 60 | + /// |
| 61 | + /// Callers must ensure that the folio is valid and locked. Additionally, that the |
| 62 | + /// responsibility of unlocking is transferred to the new instance of [`LockedFolio`]. Lastly, |
| 63 | + /// that the returned [`LockedFolio`] doesn't outlive the refcount that keeps it alive. |
| 64 | + #[allow(dead_code)] |
| 65 | + pub(crate) unsafe fn from_raw(folio: *const bindings::folio) -> Self { |
| 66 | + let ptr = folio.cast(); |
| 67 | + // SAFETY: The safety requirements ensure that `folio` (from which `ptr` is derived) is |
| 68 | + // valid and will remain valid while the `LockedFolio` instance lives. |
| 69 | + Self(unsafe { &*ptr }) |
| 70 | + } |
| 71 | + |
| 72 | + /// Marks the folio as being up to date. |
| 73 | + pub fn mark_uptodate(&mut self) { |
| 74 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 75 | + unsafe { bindings::folio_mark_uptodate(self.0 .0.get()) } |
| 76 | + } |
| 77 | + |
| 78 | + /// Sets the error flag on the folio. |
| 79 | + pub fn set_error(&mut self) { |
| 80 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 81 | + unsafe { bindings::folio_set_error(self.0 .0.get()) } |
| 82 | + } |
| 83 | + |
| 84 | + fn for_each_page( |
| 85 | + &mut self, |
| 86 | + offset: usize, |
| 87 | + len: usize, |
| 88 | + mut cb: impl FnMut(&mut [u8]) -> Result, |
| 89 | + ) -> Result { |
| 90 | + let mut remaining = len; |
| 91 | + let mut next_offset = offset; |
| 92 | + |
| 93 | + // Check that we don't overflow the folio. |
| 94 | + let end = offset.checked_add(len).ok_or(EDOM)?; |
| 95 | + if end > self.size() { |
| 96 | + return Err(EINVAL); |
| 97 | + } |
| 98 | + |
| 99 | + while remaining > 0 { |
| 100 | + let page_offset = next_offset & (bindings::PAGE_SIZE - 1); |
| 101 | + let usable = min(remaining, bindings::PAGE_SIZE - page_offset); |
| 102 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount; |
| 103 | + // `next_offset` is also guaranteed be lesss than the folio size. |
| 104 | + let ptr = unsafe { bindings::kmap_local_folio(self.0 .0.get(), next_offset) }; |
| 105 | + |
| 106 | + // SAFETY: `ptr` was just returned by the `kmap_local_folio` above. |
| 107 | + let _guard = ScopeGuard::new(|| unsafe { bindings::kunmap_local(ptr) }); |
| 108 | + |
| 109 | + // SAFETY: `kmap_local_folio` maps whole page so we know it's mapped for at least |
| 110 | + // `usable` bytes. |
| 111 | + let s = unsafe { core::slice::from_raw_parts_mut(ptr.cast::<u8>(), usable) }; |
| 112 | + cb(s)?; |
| 113 | + |
| 114 | + next_offset += usable; |
| 115 | + remaining -= usable; |
| 116 | + } |
| 117 | + |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + |
| 121 | + /// Writes the given slice into the folio. |
| 122 | + pub fn write(&mut self, offset: usize, data: &[u8]) -> Result { |
| 123 | + let mut remaining = data; |
| 124 | + |
| 125 | + self.for_each_page(offset, data.len(), |s| { |
| 126 | + s.copy_from_slice(&remaining[..s.len()]); |
| 127 | + remaining = &remaining[s.len()..]; |
| 128 | + Ok(()) |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + /// Writes zeroes into the folio. |
| 133 | + pub fn zero_out(&mut self, offset: usize, len: usize) -> Result { |
| 134 | + self.for_each_page(offset, len, |s| { |
| 135 | + s.fill(0); |
| 136 | + Ok(()) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl core::ops::Deref for LockedFolio<'_> { |
| 142 | + type Target = Folio; |
| 143 | + fn deref(&self) -> &Self::Target { |
| 144 | + self.0 |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl Drop for LockedFolio<'_> { |
| 149 | + fn drop(&mut self) { |
| 150 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 151 | + unsafe { bindings::folio_unlock(self.0 .0.get()) } |
| 152 | + } |
| 153 | +} |
0 commit comments