kernel/
page.rs

1// SPDX-License-Identifier: GPL-2.0
2
3//! Kernel page allocation and management.
4
5use crate::{
6    alloc::{AllocError, Flags},
7    bindings,
8    error::code::*,
9    error::Result,
10    uaccess::UserSliceReader,
11};
12use core::ptr::{self, NonNull};
13
14/// A bitwise shift for the page size.
15pub const PAGE_SHIFT: usize = bindings::PAGE_SHIFT as usize;
16
17/// The number of bytes in a page.
18pub const PAGE_SIZE: usize = bindings::PAGE_SIZE;
19
20/// A bitmask that gives the page containing a given address.
21pub const PAGE_MASK: usize = !(PAGE_SIZE - 1);
22
23/// Round up the given number to the next multiple of [`PAGE_SIZE`].
24///
25/// It is incorrect to pass an address where the next multiple of [`PAGE_SIZE`] doesn't fit in a
26/// [`usize`].
27pub const fn page_align(addr: usize) -> usize {
28    // Parentheses around `PAGE_SIZE - 1` to avoid triggering overflow sanitizers in the wrong
29    // cases.
30    (addr + (PAGE_SIZE - 1)) & PAGE_MASK
31}
32
33/// A pointer to a page that owns the page allocation.
34///
35/// # Invariants
36///
37/// The pointer is valid, and has ownership over the page.
38pub struct Page {
39    page: NonNull<bindings::page>,
40}
41
42// SAFETY: Pages have no logic that relies on them staying on a given thread, so moving them across
43// threads is safe.
44unsafe impl Send for Page {}
45
46// SAFETY: Pages have no logic that relies on them not being accessed concurrently, so accessing
47// them concurrently is safe.
48unsafe impl Sync for Page {}
49
50impl Page {
51    /// Allocates a new page.
52    ///
53    /// # Examples
54    ///
55    /// Allocate memory for a page.
56    ///
57    /// ```
58    /// use kernel::page::Page;
59    ///
60    /// let page = Page::alloc_page(GFP_KERNEL)?;
61    /// # Ok::<(), kernel::alloc::AllocError>(())
62    /// ```
63    ///
64    /// Allocate memory for a page and zero its contents.
65    ///
66    /// ```
67    /// use kernel::page::Page;
68    ///
69    /// let page = Page::alloc_page(GFP_KERNEL | __GFP_ZERO)?;
70    /// # Ok::<(), kernel::alloc::AllocError>(())
71    /// ```
72    pub fn alloc_page(flags: Flags) -> Result<Self, AllocError> {
73        // SAFETY: Depending on the value of `gfp_flags`, this call may sleep. Other than that, it
74        // is always safe to call this method.
75        let page = unsafe { bindings::alloc_pages(flags.as_raw(), 0) };
76        let page = NonNull::new(page).ok_or(AllocError)?;
77        // INVARIANT: We just successfully allocated a page, so we now have ownership of the newly
78        // allocated page. We transfer that ownership to the new `Page` object.
79        Ok(Self { page })
80    }
81
82    /// Returns a raw pointer to the page.
83    pub fn as_ptr(&self) -> *mut bindings::page {
84        self.page.as_ptr()
85    }
86
87    /// Runs a piece of code with this page mapped to an address.
88    ///
89    /// The page is unmapped when this call returns.
90    ///
91    /// # Using the raw pointer
92    ///
93    /// It is up to the caller to use the provided raw pointer correctly. The pointer is valid for
94    /// `PAGE_SIZE` bytes and for the duration in which the closure is called. The pointer might
95    /// only be mapped on the current thread, and when that is the case, dereferencing it on other
96    /// threads is UB. Other than that, the usual rules for dereferencing a raw pointer apply: don't
97    /// cause data races, the memory may be uninitialized, and so on.
98    ///
99    /// If multiple threads map the same page at the same time, then they may reference with
100    /// different addresses. However, even if the addresses are different, the underlying memory is
101    /// still the same for these purposes (e.g., it's still a data race if they both write to the
102    /// same underlying byte at the same time).
103    fn with_page_mapped<T>(&self, f: impl FnOnce(*mut u8) -> T) -> T {
104        // SAFETY: `page` is valid due to the type invariants on `Page`.
105        let mapped_addr = unsafe { bindings::kmap_local_page(self.as_ptr()) };
106
107        let res = f(mapped_addr.cast());
108
109        // This unmaps the page mapped above.
110        //
111        // SAFETY: Since this API takes the user code as a closure, it can only be used in a manner
112        // where the pages are unmapped in reverse order. This is as required by `kunmap_local`.
113        //
114        // In other words, if this call to `kunmap_local` happens when a different page should be
115        // unmapped first, then there must necessarily be a call to `kmap_local_page` other than the
116        // call just above in `with_page_mapped` that made that possible. In this case, it is the
117        // unsafe block that wraps that other call that is incorrect.
118        unsafe { bindings::kunmap_local(mapped_addr) };
119
120        res
121    }
122
123    /// Runs a piece of code with a raw pointer to a slice of this page, with bounds checking.
124    ///
125    /// If `f` is called, then it will be called with a pointer that points at `off` bytes into the
126    /// page, and the pointer will be valid for at least `len` bytes. The pointer is only valid on
127    /// this task, as this method uses a local mapping.
128    ///
129    /// If `off` and `len` refers to a region outside of this page, then this method returns
130    /// [`EINVAL`] and does not call `f`.
131    ///
132    /// # Using the raw pointer
133    ///
134    /// It is up to the caller to use the provided raw pointer correctly. The pointer is valid for
135    /// `len` bytes and for the duration in which the closure is called. The pointer might only be
136    /// mapped on the current thread, and when that is the case, dereferencing it on other threads
137    /// is UB. Other than that, the usual rules for dereferencing a raw pointer apply: don't cause
138    /// data races, the memory may be uninitialized, and so on.
139    ///
140    /// If multiple threads map the same page at the same time, then they may reference with
141    /// different addresses. However, even if the addresses are different, the underlying memory is
142    /// still the same for these purposes (e.g., it's still a data race if they both write to the
143    /// same underlying byte at the same time).
144    fn with_pointer_into_page<T>(
145        &self,
146        off: usize,
147        len: usize,
148        f: impl FnOnce(*mut u8) -> Result<T>,
149    ) -> Result<T> {
150        let bounds_ok = off <= PAGE_SIZE && len <= PAGE_SIZE && (off + len) <= PAGE_SIZE;
151
152        if bounds_ok {
153            self.with_page_mapped(move |page_addr| {
154                // SAFETY: The `off` integer is at most `PAGE_SIZE`, so this pointer offset will
155                // result in a pointer that is in bounds or one off the end of the page.
156                f(unsafe { page_addr.add(off) })
157            })
158        } else {
159            Err(EINVAL)
160        }
161    }
162
163    /// Maps the page and reads from it into the given buffer.
164    ///
165    /// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes
166    /// outside of the page, then this call returns [`EINVAL`].
167    ///
168    /// # Safety
169    ///
170    /// * Callers must ensure that `dst` is valid for writing `len` bytes.
171    /// * Callers must ensure that this call does not race with a write to the same page that
172    ///   overlaps with this read.
173    pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result {
174        self.with_pointer_into_page(offset, len, move |src| {
175            // SAFETY: If `with_pointer_into_page` calls into this closure, then
176            // it has performed a bounds check and guarantees that `src` is
177            // valid for `len` bytes.
178            //
179            // There caller guarantees that there is no data race.
180            unsafe { ptr::copy_nonoverlapping(src, dst, len) };
181            Ok(())
182        })
183    }
184
185    /// Maps the page and writes into it from the given buffer.
186    ///
187    /// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes
188    /// outside of the page, then this call returns [`EINVAL`].
189    ///
190    /// # Safety
191    ///
192    /// * Callers must ensure that `src` is valid for reading `len` bytes.
193    /// * Callers must ensure that this call does not race with a read or write to the same page
194    ///   that overlaps with this write.
195    pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Result {
196        self.with_pointer_into_page(offset, len, move |dst| {
197            // SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a
198            // bounds check and guarantees that `dst` is valid for `len` bytes.
199            //
200            // There caller guarantees that there is no data race.
201            unsafe { ptr::copy_nonoverlapping(src, dst, len) };
202            Ok(())
203        })
204    }
205
206    /// Maps the page and zeroes the given slice.
207    ///
208    /// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes
209    /// outside of the page, then this call returns [`EINVAL`].
210    ///
211    /// # Safety
212    ///
213    /// Callers must ensure that this call does not race with a read or write to the same page that
214    /// overlaps with this write.
215    pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result {
216        self.with_pointer_into_page(offset, len, move |dst| {
217            // SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a
218            // bounds check and guarantees that `dst` is valid for `len` bytes.
219            //
220            // There caller guarantees that there is no data race.
221            unsafe { ptr::write_bytes(dst, 0u8, len) };
222            Ok(())
223        })
224    }
225
226    /// Copies data from userspace into this page.
227    ///
228    /// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes
229    /// outside of the page, then this call returns [`EINVAL`].
230    ///
231    /// Like the other `UserSliceReader` methods, data races are allowed on the userspace address.
232    /// However, they are not allowed on the page you are copying into.
233    ///
234    /// # Safety
235    ///
236    /// Callers must ensure that this call does not race with a read or write to the same page that
237    /// overlaps with this write.
238    pub unsafe fn copy_from_user_slice_raw(
239        &self,
240        reader: &mut UserSliceReader,
241        offset: usize,
242        len: usize,
243    ) -> Result {
244        self.with_pointer_into_page(offset, len, move |dst| {
245            // SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a
246            // bounds check and guarantees that `dst` is valid for `len` bytes. Furthermore, we have
247            // exclusive access to the slice since the caller guarantees that there are no races.
248            reader.read_raw(unsafe { core::slice::from_raw_parts_mut(dst.cast(), len) })
249        })
250    }
251}
252
253impl Drop for Page {
254    fn drop(&mut self) {
255        // SAFETY: By the type invariants, we have ownership of the page and can free it.
256        unsafe { bindings::__free_pages(self.page.as_ptr(), 0) };
257    }
258}