|
| 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::{ARef, 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(pub(crate) 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 | + /// Tries to allocate a new folio. |
| 35 | + /// |
| 36 | + /// On success, returns a folio made up of 2^order pages. |
| 37 | + pub fn try_new(order: u32) -> Result<UniqueFolio> { |
| 38 | + if order > bindings::MAX_ORDER { |
| 39 | + return Err(EDOM); |
| 40 | + } |
| 41 | + |
| 42 | + // SAFETY: We checked that `order` is within the max allowed value. |
| 43 | + let f = ptr::NonNull::new(unsafe { bindings::folio_alloc(bindings::GFP_KERNEL, order) }) |
| 44 | + .ok_or(ENOMEM)?; |
| 45 | + |
| 46 | + // SAFETY: The folio returned by `folio_alloc` is referenced. The ownership of the |
| 47 | + // reference is transferred to the `ARef` instance. |
| 48 | + Ok(UniqueFolio(unsafe { ARef::from_raw(f.cast()) })) |
| 49 | + } |
| 50 | + |
| 51 | + /// Returns the byte position of this folio in its file. |
| 52 | + pub fn pos(&self) -> i64 { |
| 53 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 54 | + unsafe { bindings::folio_pos(self.0.get()) } |
| 55 | + } |
| 56 | + |
| 57 | + /// Returns the byte size of this folio. |
| 58 | + pub fn size(&self) -> usize { |
| 59 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 60 | + unsafe { bindings::folio_size(self.0.get()) } |
| 61 | + } |
| 62 | + |
| 63 | + /// Flushes the data cache for the pages that make up the folio. |
| 64 | + pub fn flush_dcache(&self) { |
| 65 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 66 | + unsafe { bindings::flush_dcache_folio(self.0.get()) } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/// A [`Folio`] that has a single reference to it. |
| 71 | +pub struct UniqueFolio(pub(crate) ARef<Folio>); |
| 72 | + |
| 73 | +impl UniqueFolio { |
| 74 | + /// Maps the contents of a folio page into a slice. |
| 75 | + pub fn map_page(&self, page_index: usize) -> Result<MapGuard<'_>> { |
| 76 | + if page_index >= self.0.size() / bindings::PAGE_SIZE { |
| 77 | + return Err(EDOM); |
| 78 | + } |
| 79 | + |
| 80 | + // SAFETY: We just checked that the index is within bounds of the folio. |
| 81 | + let page = unsafe { bindings::folio_page(self.0 .0.get(), page_index) }; |
| 82 | + |
| 83 | + // SAFETY: `page` is valid because it was returned by `folio_page` above. |
| 84 | + let ptr = unsafe { bindings::kmap(page) }; |
| 85 | + |
| 86 | + // SAFETY: We just mapped `ptr`, so it's valid for read. |
| 87 | + let data = unsafe { core::slice::from_raw_parts(ptr.cast::<u8>(), bindings::PAGE_SIZE) }; |
| 88 | + |
| 89 | + Ok(MapGuard { data, page }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// A mapped [`UniqueFolio`]. |
| 94 | +pub struct MapGuard<'a> { |
| 95 | + data: &'a [u8], |
| 96 | + page: *mut bindings::page, |
| 97 | +} |
| 98 | + |
| 99 | +impl core::ops::Deref for MapGuard<'_> { |
| 100 | + type Target = [u8]; |
| 101 | + |
| 102 | + fn deref(&self) -> &Self::Target { |
| 103 | + self.data |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl Drop for MapGuard<'_> { |
| 108 | + fn drop(&mut self) { |
| 109 | + // SAFETY: A `MapGuard` instance is only created when `kmap` succeeds, so it's ok to unmap |
| 110 | + // it when the guard is dropped. |
| 111 | + unsafe { bindings::kunmap(self.page) }; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// A locked [`Folio`]. |
| 116 | +pub struct LockedFolio<'a>(&'a Folio); |
| 117 | + |
| 118 | +impl LockedFolio<'_> { |
| 119 | + /// Creates a new locked folio from a raw pointer. |
| 120 | + /// |
| 121 | + /// # Safety |
| 122 | + /// |
| 123 | + /// Callers must ensure that the folio is valid and locked. Additionally, that the |
| 124 | + /// responsibility of unlocking is transferred to the new instance of [`LockedFolio`]. Lastly, |
| 125 | + /// that the returned [`LockedFolio`] doesn't outlive the refcount that keeps it alive. |
| 126 | + #[allow(dead_code)] |
| 127 | + pub(crate) unsafe fn from_raw(folio: *const bindings::folio) -> Self { |
| 128 | + let ptr = folio.cast(); |
| 129 | + // SAFETY: The safety requirements ensure that `folio` (from which `ptr` is derived) is |
| 130 | + // valid and will remain valid while the `LockedFolio` instance lives. |
| 131 | + Self(unsafe { &*ptr }) |
| 132 | + } |
| 133 | + |
| 134 | + /// Marks the folio as being up to date. |
| 135 | + pub fn mark_uptodate(&mut self) { |
| 136 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 137 | + unsafe { bindings::folio_mark_uptodate(self.0 .0.get()) } |
| 138 | + } |
| 139 | + |
| 140 | + /// Sets the error flag on the folio. |
| 141 | + pub fn set_error(&mut self) { |
| 142 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 143 | + unsafe { bindings::folio_set_error(self.0 .0.get()) } |
| 144 | + } |
| 145 | + |
| 146 | + fn for_each_page( |
| 147 | + &mut self, |
| 148 | + offset: usize, |
| 149 | + len: usize, |
| 150 | + mut cb: impl FnMut(&mut [u8]) -> Result, |
| 151 | + ) -> Result { |
| 152 | + let mut remaining = len; |
| 153 | + let mut next_offset = offset; |
| 154 | + |
| 155 | + // Check that we don't overflow the folio. |
| 156 | + let end = offset.checked_add(len).ok_or(EDOM)?; |
| 157 | + if end > self.size() { |
| 158 | + return Err(EINVAL); |
| 159 | + } |
| 160 | + |
| 161 | + while remaining > 0 { |
| 162 | + let page_offset = next_offset & (bindings::PAGE_SIZE - 1); |
| 163 | + let usable = min(remaining, bindings::PAGE_SIZE - page_offset); |
| 164 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount; |
| 165 | + // `next_offset` is also guaranteed be lesss than the folio size. |
| 166 | + let ptr = unsafe { bindings::kmap_local_folio(self.0 .0.get(), next_offset) }; |
| 167 | + |
| 168 | + // SAFETY: `ptr` was just returned by the `kmap_local_folio` above. |
| 169 | + let _guard = ScopeGuard::new(|| unsafe { bindings::kunmap_local(ptr) }); |
| 170 | + |
| 171 | + // SAFETY: `kmap_local_folio` maps whole page so we know it's mapped for at least |
| 172 | + // `usable` bytes. |
| 173 | + let s = unsafe { core::slice::from_raw_parts_mut(ptr.cast::<u8>(), usable) }; |
| 174 | + cb(s)?; |
| 175 | + |
| 176 | + next_offset += usable; |
| 177 | + remaining -= usable; |
| 178 | + } |
| 179 | + |
| 180 | + Ok(()) |
| 181 | + } |
| 182 | + |
| 183 | + /// Writes the given slice into the folio. |
| 184 | + pub fn write(&mut self, offset: usize, data: &[u8]) -> Result { |
| 185 | + let mut remaining = data; |
| 186 | + |
| 187 | + self.for_each_page(offset, data.len(), |s| { |
| 188 | + s.copy_from_slice(&remaining[..s.len()]); |
| 189 | + remaining = &remaining[s.len()..]; |
| 190 | + Ok(()) |
| 191 | + }) |
| 192 | + } |
| 193 | + |
| 194 | + /// Writes zeroes into the folio. |
| 195 | + pub fn zero_out(&mut self, offset: usize, len: usize) -> Result { |
| 196 | + self.for_each_page(offset, len, |s| { |
| 197 | + s.fill(0); |
| 198 | + Ok(()) |
| 199 | + }) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +impl core::ops::Deref for LockedFolio<'_> { |
| 204 | + type Target = Folio; |
| 205 | + fn deref(&self) -> &Self::Target { |
| 206 | + self.0 |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +impl Drop for LockedFolio<'_> { |
| 211 | + fn drop(&mut self) { |
| 212 | + // SAFETY: The folio is valid because the shared reference implies a non-zero refcount. |
| 213 | + unsafe { bindings::folio_unlock(self.0 .0.get()) } |
| 214 | + } |
| 215 | +} |
0 commit comments