Skip to content

Commit fc6e66f

Browse files
Darksonnojeda
authored andcommitted
rust: add abstraction for struct page
Adds a new struct called `Page` that wraps a pointer to `struct page`. This struct is assumed to hold ownership over the page, so that Rust code can allocate and manage pages directly. The page type has various methods for reading and writing into the page. These methods will temporarily map the page to allow the operation. All of these methods use a helper that takes an offset and length, performs bounds checks, and returns a pointer to the given offset in the page. This patch only adds support for pages of order zero, as that is all Rust Binder needs. However, it is written to make it easy to add support for higher-order pages in the future. To do that, you would add a const generic parameter to `Page` that specifies the order. Most of the methods do not need to be adjusted, as the logic for dealing with mapping multiple pages at once can be isolated to just the `with_pointer_into_page` method. Rust Binder needs to manage pages directly as that is how transactions are delivered: Each process has an mmap'd region for incoming transactions. When an incoming transaction arrives, the Binder driver will choose a region in the mmap, allocate and map the relevant pages manually, and copy the incoming transaction directly into the page. This architecture allows the driver to copy transactions directly from the address space of one process to another, without an intermediate copy to a kernel buffer. This code is based on Wedson's page abstractions from the old rust branch, but it has been modified by Alice by removing the incomplete support for higher-order pages, by introducing the `with_*` helpers to consolidate the bounds checking logic into a single place, and various other changes. Co-developed-by: Wedson Almeida Filho <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Trevor Gross <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Fixed typos and added a few intra-doc links. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent b33bf37 commit fc6e66f

File tree

5 files changed

+279
-0
lines changed

5 files changed

+279
-0
lines changed

rust/bindings/bindings_helper.h

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
/* `bindgen` gets confused at certain things. */
2222
const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
23+
const size_t RUST_CONST_HELPER_PAGE_SIZE = PAGE_SIZE;
2324
const gfp_t RUST_CONST_HELPER_GFP_ATOMIC = GFP_ATOMIC;
2425
const gfp_t RUST_CONST_HELPER_GFP_KERNEL = GFP_KERNEL;
2526
const gfp_t RUST_CONST_HELPER_GFP_KERNEL_ACCOUNT = GFP_KERNEL_ACCOUNT;

rust/helpers.c

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <linux/build_bug.h>
2626
#include <linux/err.h>
2727
#include <linux/errname.h>
28+
#include <linux/gfp.h>
29+
#include <linux/highmem.h>
2830
#include <linux/mutex.h>
2931
#include <linux/refcount.h>
3032
#include <linux/sched/signal.h>
@@ -94,6 +96,24 @@ int rust_helper_signal_pending(struct task_struct *t)
9496
}
9597
EXPORT_SYMBOL_GPL(rust_helper_signal_pending);
9698

99+
struct page *rust_helper_alloc_pages(gfp_t gfp_mask, unsigned int order)
100+
{
101+
return alloc_pages(gfp_mask, order);
102+
}
103+
EXPORT_SYMBOL_GPL(rust_helper_alloc_pages);
104+
105+
void *rust_helper_kmap_local_page(struct page *page)
106+
{
107+
return kmap_local_page(page);
108+
}
109+
EXPORT_SYMBOL_GPL(rust_helper_kmap_local_page);
110+
111+
void rust_helper_kunmap_local(const void *addr)
112+
{
113+
kunmap_local(addr);
114+
}
115+
EXPORT_SYMBOL_GPL(rust_helper_kunmap_local);
116+
97117
refcount_t rust_helper_REFCOUNT_INIT(int n)
98118
{
99119
return (refcount_t)REFCOUNT_INIT(n);

rust/kernel/alloc.rs

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ pub struct AllocError;
2020
#[derive(Clone, Copy)]
2121
pub struct Flags(u32);
2222

23+
impl Flags {
24+
/// Get the raw representation of this flag.
25+
pub(crate) fn as_raw(self) -> u32 {
26+
self.0
27+
}
28+
}
29+
2330
impl core::ops::BitOr for Flags {
2431
type Output = Self;
2532
fn bitor(self, rhs: Self) -> Self::Output {

rust/kernel/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub mod ioctl;
3535
pub mod kunit;
3636
#[cfg(CONFIG_NET)]
3737
pub mod net;
38+
pub mod page;
3839
pub mod prelude;
3940
pub mod print;
4041
mod static_assert;

rust/kernel/page.rs

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

0 commit comments

Comments
 (0)