rust: page: improve access methods for racy memory regions
Subsystems (8)
Andreas Hindborg
reviewercover rust: page: improve access methods for racy memory regions
Page has a number of methods for manipulating the memory of the page. These use core::ptr::{copy_nonoverlapping,write_bytes}, which are not valid for use when one end of the operation may be subject to concurrent reads or writes.
With recent patches that allow pages to be exposed as SysMem regions, we can use the SysMem infrastructure to do the operations as byte-wise atomic operations, thus making the operations well defined.
This series builds on the atomic memcpy series [1], the Ownable series
[2] and the Page refactor series [3] to prepare for handling the pages of a Bio in a well defined way. As part of this work:
Remove the core::ptr memory operations from page.
Add a safe method to get a SysMem view of a page and use that when copying into pages from UserSliceReader. Remove code paths in UserSliceReader that would make a &mut [MaybeUninit<u8>] to page contents.
As SysMem is Send and Sync but pages are mapped with kmap_local, add a SysMemLocal variant that is neither Send nor Sync and use this for the views of page data.
Finally, remove Page::copy_from_user_slice_raw as it is no longer used.
The three dependency series do not compose without conflicts, so I have provided my b4 tree with all of them applied [4].
[1] https://lore.kernel.org/all/[email protected]
[2] https://lore.kernel.org/all/[email protected]
[3] https://lore.kernel.org/all/[email protected]
[4] https://github.com/metaspace/linux/tree/page-shared
---
Andreas Hindborg (11):
rust: page: tighten safety requirements of SysMem copy methods
rust: sync: atomic: add memset
rust: io: add SysMem::fill_zero
rust: page: use byte-wise atomic operations in raw accessors
rust: uaccess: take a raw pointer in UserSliceReader::read_raw
rust: uaccess: add UserSliceReader::read_sysmem
rust: page: add Shared page kind
rust: io: add SysMemLocal
rust: page: add Page::with_page_as_sysmem_local
rust_binder: access page data through SysMemLocal
rust: page: remove Page::copy_from_user_slice_raw
drivers/android/binder/page_range.rs | 35 ++-- rust/kernel/io.rs | 151 +++++++++++++-- rust/kernel/page.rs | 355 +++++++++++++++++++++++++---------- rust/kernel/sync/atomic.rs | 25 +++ rust/kernel/uaccess.rs | 79 ++++++-- 5 files changed, 496 insertions(+), 149 deletions(-)
---
base-commit: fd9230005e85472b81c1c7c16afe93dd878996b1
change-id: 20261119-page-shared-9b748d499e3e
prerequisite-change-id: 20260130-page-volatile-io-05ff595507d3:v6
prerequisite-patch-id: 112f7ee8c101c766ba33128a08c053ff7dd1bd6f
prerequisite-patch-id: 4b725c8c4d3313096ea722ac2866bd5e36227b88
prerequisite-patch-id: bf25326b450044d86f2cc7392f67d17d674afe8a
prerequisite-patch-id: acbb5d7f331c45b8da915ea32bef91447ac0b234
prerequisite-change-id: 20250305-unique-ref-29fcd675f9e9:v22
prerequisite-patch-id: 4e3c7a1a2077aaa0ece2d557fa73486355903ae5
prerequisite-patch-id: d1073c5dcd6287fbcba7848a5d182108930230c1
prerequisite-patch-id: 3244def11bbdf2eabaa6e94e15ce06540b2f5814
prerequisite-patch-id: cef14c5bef5ba519ceaf0a0e7c9e93ef974db7b3
prerequisite-patch-id: 56410da35ed8dbe7b210adae0985af705cc0b5b0
prerequisite-change-id: 20261112-page-ownable-2e97941a83b2:v22
prerequisite-patch-id: 4e3c7a1a2077aaa0ece2d557fa73486355903ae5
prerequisite-patch-id: d1073c5dcd6287fbcba7848a5d182108930230c1
prerequisite-patch-id: 3244def11bbdf2eabaa6e94e15ce06540b2f5814
prerequisite-patch-id: cef14c5bef5ba519ceaf0a0e7c9e93ef974db7b3
prerequisite-patch-id: 56410da35ed8dbe7b210adae0985af705cc0b5b0
prerequisite-patch-id: 7454d72e9d18045c4b45c3be614c51288c7c437a
prerequisite-patch-id: edcc308e1ba9596e7939859cb50230ca56f0b265
prerequisite-patch-id: 90fcb80e26232818fea9472fa83871b6577bbddd
prerequisite-patch-id: 10e8ae82a9750cd76b42754393d3e39b9e849474
prerequisite-patch-id: 881055f06785a8df0ec9f8107065428900ca78d9
prerequisite-patch-id: e0759f2b41824d562a594c9e2452bf41ba514cbd
Best regards,
Signature · 1 line
Andreas Hindborg <[email protected]>
Andreas Hindborg
reviewerPATCH 1/11 rust: page: tighten safety requirements of `SysMem` copy methods
Page::read_sysmem and Page::write_sysmem are available for all page kinds, so they can be called on a Page<Exclusive>, whose data can be referenced as a slice. Add the requirements that the raw accessors of Page have for this case: no mutable reference to the data that is read may exist, no reference to the data that is written may exist, and data written to a Page<Exclusive> must be initialized.
--- rust/kernel/page.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
rust/kernel/page.rs +10 -3
@@ -374,6 +374,8 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result/// Callers must ensure that: /// /// - This call does not race with a write to the source page that overlaps with this read. +/// - No mutable reference to the data of the page that overlaps with this read exists during +/// this call. /// - The memory region of `dst` does not overlap with the memory of the page. /// /// # Examples
@@ -391,7 +393,8 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result/// // way while `dst` is alive. /// let dst = unsafe { SysMem::new(&raw mut buf as *mut [u8]) }; /// -/// // SAFETY: `page` is not written to during this call, and `buf` is not part of the page. +/// // SAFETY: `page` is not written to during this call, no reference to the data of `page` +/// // exists, and `buf` is not part of the page. /// unsafe { page.read_sysmem(dst, 0)? }; /// assert_eq!(buf, [0u8; 16]); /// # Ok::<(), Error>(())
@@ -453,6 +456,10 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res/// /// - This call does not race with a read or write to the destination page that overlaps with /// this write. +/// - No reference to the data of the page that overlaps with this write exists during this +/// call. Such references can be obtained from a [`Page<Exclusive>`], see +/// [`Page::as_slice`]. +/// - If the page is a [`Page<Exclusive>`], the bytes of `src` are initialized. /// - The memory region of `src` does not overlap with the memory of the page. /// /// # Examples
@@ -476,8 +483,8 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res/// ) /// }; /// -/// // SAFETY: `page` is not accessed in any other way during these calls, and neither `data` -/// // nor `buf` is part of the page. +/// // SAFETY: `page` is not accessed in any other way during these calls, no reference to the +/// // data of `page` exists, and neither `data` nor `buf` is part of the page. /// unsafe { /// page.write_sysmem(src, 0)?; /// page.read_sysmem(dst, 0)?;
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 2/11 rust: sync: atomic: add memset
In reply to Andreas HindborgAdd atomic::memset(), the byte-wise atomic counterpart of core::ptr::write_bytes(). Just as atomic::memcpy() is implemented in terms of the C memcpy() function, atomic::memset() is implemented in terms of the C memset() function.
This is used to zero the data of a page that is subject to concurrent accesses.
--- rust/kernel/sync/atomic.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+)
rust/kernel/sync/atomic.rs +25 -0
@@ -884,3 +884,28 @@ pub unsafe fn memcpy(src: *const u8, dst: *mut u8, len: usize) { ) }; } + +/// Set `len` bytes at `dst` to `value` using byte-wise atomic operations. +/// +/// This is the concurrent-safe counterpart of `core::ptr::write_bytes()`. Because of the atomicity +/// at byte level, when racing with another concurrent atomic access, or with an external access +/// (from DMA or userspace), the behavior of this function is defined: the memory is written at (at +/// least) byte granularity. +/// +/// Implementation note: this is currently implemented by the kernel's `memset()`, which is +/// implemented in a way such that byte-wise atomic memory store instructions are used. +/// +/// # Safety +/// +/// Callers must ensure that `dst` is valid for atomic writes for `len` bytes for the duration of +/// the call. +#[inline] +pub unsafe fn memset (dst: *mut u8 , value:u8 , len:usize ) { +// SAFETY: By the safety requirements of this function, the following operation will not: +// - Trap. +// - Invalidate any reference invariants. +// - Race with any operation by the Rust AM, as `bindings::memset` is a byte-wise atomic +// operation and all operations by the Rust AM to the involved memory area use byte-wise +// atomic semantics. +unsafe { bindings::memset (dst.cast ::<kernel::ffi::c_void >(), value.into (), len) }; +}
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 3/11 rust: io: add `SysMem::fill_zero`
In reply to Andreas HindborgAdd a method for zeroing the memory of a SysMem byte view. The method uses atomic::memset(), so the memory can be subject to concurrent accesses.
This is used to zero the data of a page that is mapped to user space or shared with a device.
--- rust/kernel/io.rs | 11 +++++++++++ 1 file changed, 11 insertions(+)
rust/kernel/io.rs +11 -0
@@ -1586,6 +1586,17 @@ unsafe impl<T: ?Sized + Sync> Send for SysMem<'_, T> {}// SAFETY: `SysMem<'_, T>` is conceptually `&T`. unsafe impl <T : ?Sized +Sync >Sync for SysMem <'_,T > {} +impl SysMem <'_, [u8 ]> { +/// Writes zeroes to all bytes of the view using byte-wise atomic operations. +#[inline] +pub fn fill_zero (self) { +// SAFETY: Per type invariant, `self.ptr` is a valid kernel accessible memory region of +// `self.ptr.len()` bytes, and no plain access races with this write, so it is valid for +// atomic writes. +unsafe { atomic::memset (self.ptr.cast (),0 , self.ptr.len ()) }; + } +} +impl <'a,T : ?Sized >SysMem <'a,T > {/// Create a `SysMem` from a raw pointer. ///
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 4/11 rust: page: use byte-wise atomic operations in raw accessors
In reply to Andreas HindborgThe raw accessors of Page are used to access pages that may be mapped to user space or shared with devices. A plain access to such a page is a data race when the other party writes to the page.
Add the private method Page::with_page_as_sysmem(), which maps a range of a page and passes it to a closure as a SysMem view. Access the data of the page through this view in Page::read_raw(), Page::write_raw() and Page::fill_zero_raw(), so that all accesses are byte-wise atomic. Zero the data with SysMem::fill_zero().
Narrow the safety requirements of these methods, and of Page::read_sysmem() and Page::write_sysmem(), accordingly: a call must not race with a plain access to the same data, but it is allowed to race with other non-plain accesses.
Page::copy_from_user_slice_raw() still writes to the page with plain accesses. State that in the safety requirements.
--- rust/kernel/page.rs | 153 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 107 insertions(+), 46 deletions(-)
rust/kernel/page.rs +107 -46
@@ -334,28 +334,74 @@ fn with_pointer_into_page<T>( } } +/// Runs a piece of code with a range of this page mapped as a [`SysMem`] view. +/// +/// If `f` is called, then it is called with a view of the `len` bytes of the page that start +/// at `offset`. Accesses through the view are volatile or byte-wise atomic, so the view is +/// suitable for a page whose data is subject to concurrent writes by hardware or user space. +/// The view is valid for the duration of the call to `f`. +/// +/// If `offset` and `len` refer to a region outside of this page, then this method returns +/// [`EINVAL`] and does not call `f`. +/// +/// # Safety +/// +/// * Callers must ensure that no plain access to the mapped range races with an access +/// performed through the view, see [`SysMem`]. +/// * Callers must ensure that no mutable reference to the data of the page that overlaps with +/// the mapped range exists during this call. If `f` writes through the view, no reference to +/// that data may exist during this call. Such references can be obtained from a +/// [`Page<Exclusive>`], see [`Page::as_slice`]. +/// * If the page is a [`Page<Exclusive>`], `f` must only write initialized bytes through the +/// view. +/// * Callers must ensure that the view is only used on the current task, as this method may +/// use a local mapping. +unsafe fn with_page_as_sysmem <T >( + &self, + offset:usize , + len:usize , + f:impl FnOnce (SysMem <'_, [u8 ]>) ->Result <T >, + ) ->Result <T > { + self.with_pointer_into_page (offset, len,move |ptr| { +// SAFETY: +// - If `with_pointer_into_page` calls into this closure, then it has performed a +// bounds check and guarantees that `ptr` is valid for `len` bytes for the duration +// of the closure. A byte slice has an alignment of 1. +// - By the safety requirements of this function, no plain access to the range races +// with an access through the view. +f (unsafe {SysMem ::new (ptr::slice_from_raw_parts_mut (ptr, len)) }) + }) + } +/// Maps the page and reads from it into the given buffer. /// /// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes /// outside of the page, then this call returns [`EINVAL`]. /// +/// The copy is byte-wise atomic, so it is well-defined even if the data of the page is +/// concurrently written by hardware, by user space, or by other kernel code using atomic or +/// volatile accesses. In that case, the data that is read may be arbitrary. +/// /// # Safety /// -/// * Callers must ensure that `dst` is valid for writing `len` bytes. -/// * Callers must ensure that this call does not race with a write to the same page that -/// overlaps with this read. +/// * Callers must ensure that `dst` is valid for writing `len` bytes, and that this memory +/// does not overlap with the data of the page. +/// * Callers must ensure that this call does not race with a plain write to the same page that +/// overlaps with this read, see [`SysMem`]. /// * Callers must ensure that no mutable reference to the data of the page that overlaps with /// this read exists during this call. pub unsafe fn read_raw (&self, dst: *mut u8 , offset:usize , len:usize ) ->Result { - self.with_pointer_into_page (offset, len,move |src| { -// SAFETY: If `with_pointer_into_page` calls into this closure, then -// it has performed a bounds check and guarantees that `src` is -// valid for `len` bytes. -// -// There caller guarantees that there is no data race. -unsafe { ptr::copy_nonoverlapping (src, dst, len) }; +let copy = |src:SysMem <'_, [u8 ]>| { +// SAFETY: By the safety requirements of this function, `dst` is valid for writing +// `len` bytes, which is equal to `src.size()`, and it does not overlap with `src`. +unsafe {SysMemBackend ::copy_from_io (src, dst) };Ok (()) - }) + }; + +// SAFETY: By the safety requirements of this function, no plain write races with the read +// through the view, and no mutable reference to the data that is read exists. `copy` does +// not write through the view, and it only uses the view on the current task. +unsafe { self.with_page_as_sysmem (offset, len, copy) } }/// Maps the page and reads from it into the given system memory region using byte-wise atomic
@@ -366,14 +412,14 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result/// /// The writes to `dst` are byte-wise atomic, so they are well-defined even if `dst` is /// concurrently accessed by hardware, by user space, or by other kernel code using atomic or -/// volatile accesses. The reads from the page are plain accesses, so the page must not be -/// written to concurrently. +/// volatile accesses. The reads from the page are byte-wise atomic as well. /// /// # Safety /// /// Callers must ensure that: /// -/// - This call does not race with a write to the source page that overlaps with this read. +/// - This call does not race with a plain write to the source page that overlaps with this +/// read, see [`SysMem`]. /// - No mutable reference to the data of the page that overlaps with this read exists during /// this call. /// - The memory region of `dst` does not overlap with the memory of the page.
@@ -393,8 +439,8 @@ pub unsafe fn read_raw(&self, dst: *mut u8, offset: usize, len: usize) -> Result/// // way while `dst` is alive. /// let dst = unsafe { SysMem::new(&raw mut buf as *mut [u8]) }; /// -/// // SAFETY: `page` is not written to during this call, no reference to the data of `page` -/// // exists, and `buf` is not part of the page. +/// // SAFETY: No plain write to `page` happens during this call, no reference to the data of +/// // `page` exists, and `buf` is not part of the page. /// unsafe { page.read_sysmem(dst, 0)? }; /// assert_eq!(buf, [0u8; 16]); /// # Ok::<(), Error>(())
@@ -405,8 +451,8 @@ pub unsafe fn read_sysmem(&self, dst: SysMem<'_, [u8]>, offset: usize) -> Result// - If `with_pointer_into_page` calls into this closure, then it has performed a // bounds check and guarantees that `src` is valid for `dst.len()` bytes, which is // equal to `dst.size()`. -// - By the safety requirements of this function, the page is not written to during -// this call, so `src` is valid for volatile reads. +// - By the safety requirements of this function, no plain write to the page races +// with this call, so `src` is valid for volatile reads. // - By the safety requirements of this function, `src` does not overlap with `dst`. unsafe {SysMemBackend ::copy_to_io (dst, src) };Ok (())
@@ -418,25 +464,34 @@ pub unsafe fn read_sysmem(&self, dst: SysMem<'_, [u8]>, offset: usize) -> Result/// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes /// outside of the page, then this call returns [`EINVAL`]. /// +/// The copy is byte-wise atomic, so it is well-defined even if the data of the page is +/// concurrently accessed by hardware, by user space, or by other kernel code using atomic or +/// volatile accesses. +/// /// # Safety /// -/// * Callers must ensure that `src` is valid for reading `len` bytes. -/// * Callers must ensure that this call does not race with a read or write to the same page -/// that overlaps with this write. +/// * Callers must ensure that `src` is valid for reading `len` bytes, and that this memory +/// does not overlap with the data of the page. +/// * Callers must ensure that this call does not race with a plain read or write to the same +/// page that overlaps with this write, see [`SysMem`]. /// * Callers must ensure that no reference to the data of the page that overlaps with this /// write exists during this call. Such references can be obtained from a /// [`Page<Exclusive>`], see [`Page::as_slice`]. /// * If the page is a [`Page<Exclusive>`], callers must ensure that the `len` bytes at `src` /// are initialized. pub unsafe fn write_raw (&self, src: *const u8 , offset:usize , len:usize ) ->Result { - self.with_pointer_into_page (offset, len,move |dst| { -// SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a -// bounds check and guarantees that `dst` is valid for `len` bytes. -// -// There caller guarantees that there is no data race. -unsafe { ptr::copy_nonoverlapping (src, dst, len) }; +let copy = |dst:SysMem <'_, [u8 ]>| { +// SAFETY: By the safety requirements of this function, `src` is valid for reading +// `len` bytes, which is equal to `dst.size()`, and it does not overlap with `dst`. +unsafe {SysMemBackend ::copy_to_io (dst, src) };Ok (()) - }) + }; + +// SAFETY: By the safety requirements of this function, no plain access races with the +// write through the view, no reference to the data that is written exists, and the bytes +// written are initialized if the page requires it. `copy` only uses the view on the +// current task. +unsafe { self.with_page_as_sysmem (offset, len, copy) } }/// Maps the page and writes into it from the given system memory region using byte-wise atomic
@@ -447,15 +502,14 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res/// /// The reads from `src` are byte-wise atomic, so they are well-defined even if `src` is /// concurrently accessed by hardware, by user space, or by other kernel code using atomic or -/// volatile accesses. The writes to the page are plain accesses, so the page must not be read -/// from or written to concurrently. +/// volatile accesses. The writes to the page are byte-wise atomic as well. /// /// # Safety /// /// Callers must ensure that: /// -/// - This call does not race with a read or write to the destination page that overlaps with -/// this write. +/// - This call does not race with a plain read or write to the destination page that overlaps +/// with this write, see [`SysMem`]. /// - No reference to the data of the page that overlaps with this write exists during this /// call. Such references can be obtained from a [`Page<Exclusive>`], see /// [`Page::as_slice`].
@@ -483,8 +537,9 @@ pub unsafe fn write_raw(&self, src: *const u8, offset: usize, len: usize) -> Res/// ) /// }; /// -/// // SAFETY: `page` is not accessed in any other way during these calls, no reference to the -/// // data of `page` exists, and neither `data` nor `buf` is part of the page. +/// // SAFETY: No plain access to `page` happens during these calls, no reference to the data +/// // of `page` exists, `data` is initialized, and neither `data` nor `buf` is part of the +/// // page. /// unsafe { /// page.write_sysmem(src, 0)?; /// page.read_sysmem(dst, 0)?;
@@ -498,8 +553,8 @@ pub unsafe fn write_sysmem(&self, src: SysMem<'_, [u8]>, offset: usize) -> Resul// - If `with_pointer_into_page` calls into this closure, then it has performed a // bounds check and guarantees that `dst` is valid for `src.len()` bytes, which is // equal to `src.size()`. -// - By the safety requirements of this function, the page is not read from or written -// to during this call, so `dst` is valid for volatile writes. +// - By the safety requirements of this function, no plain access to the page races +// with this call, so `dst` is valid for volatile writes. // - By the safety requirements of this function, `dst` does not overlap with `src`. unsafe {SysMemBackend ::copy_from_io (src, dst) };Ok (())
@@ -511,22 +566,27 @@ pub unsafe fn write_sysmem(&self, src: SysMem<'_, [u8]>, offset: usize) -> Resul/// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes /// outside of the page, then this call returns [`EINVAL`]. /// +/// The writes are byte-wise atomic, so they are well-defined even if the data of the page is +/// concurrently accessed by hardware, by user space, or by other kernel code using atomic or +/// volatile accesses. +/// /// # Safety /// -/// * Callers must ensure that this call does not race with a read or write to the same page -/// that overlaps with this write. +/// * Callers must ensure that this call does not race with a plain read or write to the same +/// page that overlaps with this write, see [`SysMem`]. /// * Callers must ensure that no reference to the data of the page that overlaps with this /// write exists during this call. Such references can be obtained from a /// [`Page<Exclusive>`], see [`Page::as_slice`]. pub unsafe fn fill_zero_raw (&self, offset:usize , len:usize ) ->Result { - self.with_pointer_into_page (offset, len,move |dst| { -// SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a -// bounds check and guarantees that `dst` is valid for `len` bytes. -// -// There caller guarantees that there is no data race. -unsafe { ptr::write_bytes (dst,0u8 , len) }; +let fill = |dst:SysMem <'_, [u8 ]>| { + dst.fill_zero ();Ok (()) - }) + }; + +// SAFETY: By the safety requirements of this function, no plain access races with the +// write through the view, and no reference to the data that is written exists. The bytes +// written are initialized. `fill` only uses the view on the current task. +unsafe { self.with_page_as_sysmem (offset, len, fill) } }/// Copies data from userspace into this page.
@@ -540,7 +600,8 @@ pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result {/// # Safety /// /// * Callers must ensure that this call does not race with a read or write to the same page -/// that overlaps with this write. +/// that overlaps with this write. This includes byte-wise atomic accesses, as this method +/// writes to the page with plain accesses. /// * Callers must ensure that no reference to the data of the page that overlaps with this /// write exists during this call. Such references can be obtained from a /// [`Page<Exclusive>`], see [`Page::as_slice`].
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 5/11 rust: uaccess: take a raw pointer in `UserSliceReader::read_raw`
In reply to Andreas HindborgUserSliceReader::read_raw() takes the destination as a mutable slice. Thus, it cannot be used to copy into memory that is subject to concurrent accesses, such as a page that is mapped to user space, since a mutable reference to such memory must not exist.
Change UserSliceReader::read_raw() to take the destination as a raw pointer and a length, and make it unsafe. The copy is done by copy_from_user(), which uses byte-wise atomic store instructions for the kernel memory, so the destination only has to be valid for atomic writes.
Update the callers. UserSliceReader::read_slice() remains as the safe method for copying into a slice.
Page::copy_from_user_slice_raw() no longer creates a mutable slice of the data of the page, and thus no longer writes to the page with plain accesses. Remove the remark about this from its safety requirements.
--- rust/kernel/page.rs | 9 ++++----- rust/kernel/uaccess.rs | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 20 deletions(-)
rust/kernel/page.rs +4 -5
@@ -600,8 +600,7 @@ pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result {/// # Safety /// /// * Callers must ensure that this call does not race with a read or write to the same page -/// that overlaps with this write. This includes byte-wise atomic accesses, as this method -/// writes to the page with plain accesses. +/// that overlaps with this write. /// * Callers must ensure that no reference to the data of the page that overlaps with this /// write exists during this call. Such references can be obtained from a /// [`Page<Exclusive>`], see [`Page::as_slice`].
@@ -613,9 +612,9 @@ pub unsafe fn copy_from_user_slice_raw( ) -> Result { self.with_pointer_into_page(offset, len, move |dst| {// SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a -// bounds check and guarantees that `dst` is valid for `len` bytes. Furthermore, we have -// exclusive access to the slice since the caller guarantees that there are no races. - reader.read_raw (unsafe { core::slice::from_raw_parts_mut (dst.cast (), len) }) +// bounds check and guarantees that `dst` is valid for `len` bytes. The caller +// guarantees that there are no races, so `dst` is valid for atomic writes. +unsafe { reader.read_raw (dst, len) } }) } }
rust/kernel/uaccess.rs +32 -15
74 lines not on this page · raw message
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 6/11 rust: uaccess: add `UserSliceReader::read_sysmem`
In reply to Andreas HindborgAdd a safe method for copying data from user space to a system memory region that is represented by a SysMem view. The copy is byte-wise atomic, so the destination can be subject to concurrent accesses, such as the data of a page that is mapped to user space.
The method builds on UserSliceReader::read_raw().
--- rust/kernel/uaccess.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
rust/kernel/uaccess.rs +32 -0
@@ -11,6 +11,7 @@ error::Result, ffi::{c_char, c_void}, fs::file, + io::SysMem, prelude::*, ptr::KnownSize, transmute::{AsBytes, FromBytes},
@@ -292,6 +293,37 @@ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {Ok (()) } +/// Reads raw data from the user slice into the system memory region `out`. +/// +/// Reads `out.len()` bytes. The writes to `out` are byte-wise atomic, so they are well-defined +/// even if the memory of `out` is concurrently accessed by hardware, by user space, or by +/// other kernel code using atomic or volatile accesses. +/// +/// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of +/// bounds of this [`UserSliceReader`]. This call may modify the memory of `out` even if it +/// returns an error. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// io::SysMem, +/// uaccess::UserSliceReader, // +/// }; +/// +/// fn fill_from_user(reader: &mut UserSliceReader, mem: SysMem<'_, [u8]>) -> Result { +/// reader.read_sysmem(mem) +/// } +/// ``` +pub fn read_sysmem (&mut self, out:SysMem <'_, [u8 ]>) ->Result { +let out = out.as_ptr (); + +// SAFETY: By the type invariant of `SysMem`, the memory of `out` is valid for writing +// `out.len()` bytes, and no plain access races with this write, so it is valid for atomic +// writes. +unsafe { self.read_raw (out.cast (), out.len ()) } + } +/// Reads raw data from the user slice into a kernel buffer. /// /// Fails with [`EFAULT`] if the read happens on a bad address, or if the read goes out of
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 7/11 rust: page: add `Shared` page kind
In reply to Andreas HindborgA Page<Exclusive> hands out Rust references to the data of the page, and it dereferences to a Page<Base>. Thus, no safe method on Page<Base> can access the data of a page, since the access could alias a slice obtained from a Page<Exclusive>.
Add a page kind Shared for a page whose data is initialized and is not referenced by Rust references. This allows safe accessors that use byte-wise atomic operations to be added for this kind.
Let Page<RefCounted> and Page<Frozen> dereference to Page<Shared>, and let Page<Shared> dereference to Page<Base>. Page<Exclusive> keeps dereferencing to Page<Base>, so it does not reach the methods of Page<Shared>.
Force __GFP_ZERO in Page::alloc_page() and Page::alloc_frozen(), so that the data of the allocated pages is initialized.
Require the source bytes of Page::write_raw() and Page::write_sysmem() to be initialized for all pages that are referenced as another kind than Base.
Add a TODO in binder, which writes objects that are not bounded by IntoBytes to its pages.
--- drivers/android/binder/page_range.rs | 6 +- rust/kernel/page.rs | 109 ++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 37 deletions(-)
drivers/android/binder/page_range.rs +4 -2
@@ -588,8 +588,10 @@ pub(crate) unsafe fn write<T: ?Sized>(&self, offset: usize, obj: &T) -> Result {let obj_ptr = ptr::from_ref (obj).cast ::<u8 >().add (obj_offset);// SAFETY: We have a reference to the object, so the pointer is valid. The pages // of this range are `Page<RefCounted>` pages, and this driver never creates a -// reference to their data, so no reference to the data that is written exists. As -// the page is not a `Page<Exclusive>`, the object does not have to be initialized. +// reference to their data, so no reference to the data that is written exists. +// TODO: `write_raw()` requires the bytes of the object to be initialized, as the +// data of a `Page<RefCounted>` is initialized. `T` is not bounded by `IntoBytes`, +// so this is not ensured for a `T` with padding. page.write_raw (obj_ptr, offset, to_copy)?; obj_offset += to_copy;Ok (())
rust/kernel/page.rs +74 -35
236 lines not on this page · raw message
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 8/11 rust: io: add `SysMemLocal`
In reply to Andreas HindborgA SysMem view is Send and Sync. Thus, it cannot safely represent memory that is only accessible to the current task, such as a page that is mapped with a mapping that is local to the current task.
Make SysMem and SysMemBackend generic over a new marker trait SysMemScope, with the scopes AnyTask and CurrentTask. AnyTask is the default, so existing users are not affected. Only a SysMem of the scope AnyTask is Send and Sync. All other functionality is shared between the scopes.
Add the alias SysMemLocal for a SysMem of the scope CurrentTask, together with the constructor SysMemLocal::new_local() and a conversion from a SysMem of the default scope.
Make SysMem::fill_zero() and UserSliceReader::read_sysmem() available for a byte view of any scope.
--- rust/kernel/io.rs | 142 ++++++++++++++++++++++++++++++++++++++++++------- rust/kernel/uaccess.rs | 4 +- 2 files changed, 125 insertions(+), 21 deletions(-)
rust/kernel/io.rs +123 -19
@@ -1436,11 +1436,41 @@ pub fn relaxed(self) -> RelaxedMmio<'a, T> {#[cfg( CONFIG_64BIT )] impl_mmio_io_capable! (RelaxedMmioBackend ,u64 , readq_relaxed, writeq_relaxed); +mod private { +pub trait Sealed {} +} + +/// Marker trait for the scopes of a [`SysMem`]. +/// +/// The scope of a [`SysMem`] describes which tasks the view can be used on. See [`AnyTask`] and +/// [`CurrentTask`]. +pub trait SysMemScope : private::Sealed {} + +/// The default [`SysMemScope`]: the view can be used on any task. +/// +/// A [`SysMem`] of this scope is [`Send`] and [`Sync`] when its target is [`Sync`]. +pub enum AnyTask {} + +impl private::Sealed for AnyTask {} +impl SysMemScope for AnyTask {} + +/// A [`SysMemScope`] for a view that can only be used on the task that created it. +/// +/// This is the scope of a view whose memory is only accessible to the current task, such as a +/// view that is backed by a mapping that is local to the current task. A [`SysMem`] of this scope, +/// that is, a [`SysMemLocal`], is neither [`Send`] nor [`Sync`]. +pub enum CurrentTask {} + +impl private::Sealed for CurrentTask {} +impl SysMemScope for CurrentTask {} +/// I/O Backend for system memory. -pub struct SysMemBackend ; +/// +/// The backend is generic over the [`SysMemScope`] of its views. +pub struct SysMemBackend <S :SysMemScope =AnyTask >(PhantomData <S >); -impl IoBackend for SysMemBackend { -type View <'a,T : ?Sized +KnownSize > =SysMem <'a,T >; +impl <S :SysMemScope >IoBackend for SysMemBackend <S > { +type View <'a,T : ?Sized +KnownSize > =SysMem <'a,T ,S >;#[inline] fn as_ptr <'a,T : ?Sized +KnownSize >(view:Self ::View <'a,T >) -> *mut T {
@@ -1466,9 +1496,9 @@ unsafe fn project_view<'a, T: ?Sized + KnownSize, U: ?Sized + KnownSize>(/// `write_volatile`. macro_rules! impl_sysmem_io_capable { ($ty:ty) => { -impl IoCapable <$ty>for SysMemBackend { +impl <S :SysMemScope >IoCapable <$ty>for SysMemBackend <S > {#[inline] -fn io_read (view:SysMem <'_, $ty>) -> $ty { +fn io_read (view:SysMem <'_, $ty,S >) -> $ty {// SAFETY: // - Per type invariant, `ptr` is valid and aligned. // - Using read_volatile() here so that race with hardware is well-defined.
@@ -1480,7 +1510,7 @@ fn io_read(view: SysMem<'_, $ty>) -> $ty { }#[inline] -fn io_write (view:SysMem <'_, $ty>, value: $ty) { +fn io_write (view:SysMem <'_, $ty,S >, value: $ty) {// SAFETY: // - Per type invariant, `ptr` is valid and aligned. // - Using write_volatile() here so that race with hardware is well-defined.
@@ -1499,7 +1529,7 @@ fn io_write(view: SysMem<'_, $ty>, value: $ty) {#[cfg( CONFIG_64BIT )] impl_sysmem_io_capable! (u64 ); -impl IoCopyable for SysMemBackend { +impl <S :SysMemScope >IoCopyable for SysMemBackend <S > {#[inline] unsafe fn copy_from_io (view:Self ::View <'_, [u8 ]>, buffer: *mut u8 ) {// SAFETY:
@@ -1562,31 +1592,103 @@ fn copy_write<T: IntoBytes>(view: Self::View<'_, T>, value: T) {/// Accesses performed by hardware or by user space are not plain accesses in this sense. Racing /// with them is well-defined, but the data observed may be arbitrary. /// +/// A `SysMem` is generic over a [`SysMemScope`], which describes the tasks that the view can be +/// used on. A view of the default scope [`AnyTask`] can be sent to and shared with other tasks. A +/// view of the scope [`CurrentTask`], a [`SysMemLocal`], cannot leave the task that created it. +/// Apart from that, the two are identical. +/// /// # Invariants /// /// - `self.ptr.addr() .. self.ptr.addr() + KnownSize::size(self.ptr)` is valid and aligned kernel -/// accessible memory region for the lifetime `'a`. +/// accessible memory region for the lifetime `'a`. For a view of the scope [`CurrentTask`], +/// this holds on the task that created the view. /// - No plain access to that memory region races with an access performed through this view. -pub struct SysMem <'a,T : ?Sized > { +pub struct SysMem <'a,T : ?Sized ,S :SysMemScope =AnyTask > { ptr: *mut T , - phantom:PhantomData <&'a ()>, + phantom:PhantomData <(&'a (),S )>, } -impl <T : ?Sized >Copy for SysMem <'_,T > {} -impl <T : ?Sized >Clone for SysMem <'_,T > { +/// A view of a system memory region that can only be used on the task that created it. +/// +/// This is a [`SysMem`] that is neither [`Send`] nor [`Sync`]. It is used for memory that is only +/// accessible to the current task, such as a page that is mapped with a mapping that is local to +/// the current task. +/// +/// A [`SysMem`] of the default scope converts to a `SysMemLocal` with [`From`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::io::{ +/// Io, +/// SysMem, +/// SysMemLocal, // +/// }; +/// +/// let mut data = [0u8; 4]; +/// +/// // SAFETY: `data` is valid for the lifetime of `mem`, and it is not accessed in any other way +/// // while `mem` is alive. +/// let mem = unsafe { SysMem::new(&raw mut data as *mut [u8]) }; +/// let local = SysMemLocal::from(mem); +/// +/// local.copy_from_slice(&[1, 2, 3, 4]); +/// local.fill_zero(); +/// +/// let mut buf = [0xffu8; 4]; +/// local.copy_to_slice(&mut buf); +/// assert_eq!(buf, [0u8; 4]); +/// ``` +pub type SysMemLocal <'a,T > =SysMem <'a,T ,CurrentTask >; + +impl <T : ?Sized ,S :SysMemScope >Copy for SysMem <'_,T ,S > {} +impl <T : ?Sized ,S :SysMemScope >Clone for SysMem <'_,T ,S > {#[inline] fn clone (&self) ->Self { *self } } -// SAFETY: `SysMem<'_, T>` is conceptually `&T`. -unsafe impl <T : ?Sized +Sync >Send for SysMem <'_,T > {} +// SAFETY: `SysMem<'_, T>` is conceptually `&T`. A view of the scope `AnyTask` can be used on any +// task. +unsafe impl <T : ?Sized +Sync >Send for SysMem <'_,T ,AnyTask > {} -// SAFETY: `SysMem<'_, T>` is conceptually `&T`. -unsafe impl <T : ?Sized +Sync >Sync for SysMem <'_,T > {} +// SAFETY: `SysMem<'_, T>` is conceptually `&T`. A view of the scope `AnyTask` can be used on any +// task. +unsafe impl <T : ?Sized +Sync >Sync for SysMem <'_,T ,AnyTask > {} -impl SysMem <'_, [u8 ]> { +impl <'a,T : ?Sized >From <SysMem <'a,T >>for SysMemLocal <'a,T > { +#[inline] +fn from (value:SysMem <'a,T >) ->Self { +// INVARIANT: By the type invariant of `value`, the memory region is valid on all tasks, +// and no plain access races with an access through a view of it. +Self { + ptr: value.ptr, + phantom:PhantomData , + } + } +} + +impl <'a,T : ?Sized >SysMemLocal <'a,T > { +/// Create a `SysMemLocal` from a raw pointer. +/// +/// # Safety +/// +/// - `ptr.addr() .. ptr.addr() + KnownSize::size(ptr)` must be valid and aligned kernel +/// accessible memory region on the current task for the lifetime `'a`. +/// - No plain access (see [`SysMem`]) to that memory region may race with an access performed +/// through the returned view. +#[inline] +pub unsafe fn new_local (ptr: *mut T ) ->Self { +// INVARIANT: Per safety requirement. +Self { + ptr, + phantom:PhantomData , + } + } +} + +impl <S :SysMemScope >SysMem <'_, [u8 ],S > {/// Writes zeroes to all bytes of the view using byte-wise atomic operations. #[inline] pub fn fill_zero (self) {
@@ -1614,7 +1716,9 @@ pub unsafe fn new(ptr: *mut T) -> Self { phantom: PhantomData, } } +} +impl <T : ?Sized ,S :SysMemScope >SysMem <'_,T ,S > {/// Obtain the raw pointer to the memory. #[inline] pub fn as_ptr (self) -> *mut T {
@@ -1622,8 +1726,8 @@ pub fn as_ptr(self) -> *mut T { } } -impl <'a,T : ?Sized +KnownSize >IoBase <'a>for SysMem <'a,T > { -type Backend =SysMemBackend ; +impl <'a,T : ?Sized +KnownSize ,S :SysMemScope >IoBase <'a>for SysMem <'a,T ,S > { +type Backend =SysMemBackend <S >;type Target =T ;#[inline]
rust/kernel/uaccess.rs +2 -2
@@ -11,7 +11,7 @@ error::Result, ffi::{c_char, c_void}, fs::file, - io::SysMem, + io::{SysMem, SysMemScope}, prelude::*, ptr::KnownSize, transmute::{AsBytes, FromBytes},
@@ -315,7 +315,7 @@ pub unsafe fn read_raw(&mut self, out: *mut u8, len: usize) -> Result {/// reader.read_sysmem(mem) /// } /// ``` -pub fn read_sysmem (&mut self, out:SysMem <'_, [u8 ]>) ->Result { +pub fn read_sysmem <S :SysMemScope >(&mut self, out:SysMem <'_, [u8 ],S >) ->Result {let out = out.as_ptr ();// SAFETY: By the type invariant of `SysMem`, the memory of `out` is valid for writing
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 9/11 rust: page: add `Page::with_page_as_sysmem_local`
In reply to Andreas HindborgThe data of a Page<Shared> is initialized and is not referenced by Rust references, so it can be accessed safely with byte-wise atomic operations. The existing accessors of Page are available for all page kinds and are thus unsafe: a write can alias a slice obtained from a Page<Exclusive>.
Add the safe method Page::with_page_as_sysmem_local() for Page<Shared>. It maps a range of the page and passes it to a closure as a SysMemLocal view. The view is not Send, since the mapping that backs it can be local to the current task.
A Page<Exclusive> does not dereference to a Page<Shared>, so the method is not available for it.
--- rust/kernel/page.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 4 deletions(-)
rust/kernel/page.rs +85 -4
@@ -20,7 +20,8 @@ Io, IoCopyable, SysMem, - SysMemBackend, // + SysMemBackend, + SysMemLocal, // }, sync::aref::{ self,
@@ -132,6 +133,8 @@ impl PageKind for Base {}/// /// The data of a [`Page<Shared>`] can be accessed concurrently by other parties, such as user /// space, devices or other kernel code. Thus, no Rust reference to the data of the page exists. +/// The data is accessed with volatile or byte-wise atomic operations through a [`SysMemLocal`], +/// see [`Page::with_page_as_sysmem_local`]. /// /// A [`Page<RefCounted>`] and a [`Page<Frozen>`] dereference to a [`Page<Shared>`]. A page that /// is managed by other means can be referenced as a [`Page<Shared>`] with [`Page::from_raw`].
@@ -374,13 +377,14 @@ fn with_pointer_into_page<T>(/// Runs a piece of code with a range of this page mapped as a [`SysMem`] view. /// /// If `f` is called, then it is called with a view of the `len` bytes of the page that start -/// at `offset`. Accesses through the view are volatile or byte-wise atomic, so the view is -/// suitable for a page whose data is subject to concurrent writes by hardware or user space. -/// The view is valid for the duration of the call to `f`. +/// at `offset`. The view is valid for the duration of the call to `f`. /// /// If `offset` and `len` refer to a region outside of this page, then this method returns /// [`EINVAL`] and does not call `f`. /// +/// This is the building block for the accessors of [`Page`]. Use +/// [`Page::with_page_as_sysmem_local`] for safe access to the data of a [`Page<Shared>`]. +/// /// # Safety /// /// * Callers must ensure that no plain access to the mapped range races with an access
@@ -658,6 +662,83 @@ pub unsafe fn copy_from_user_slice_raw( } } +impl Page <Shared > { +/// Runs a piece of code with a range of this page mapped as a [`SysMemLocal`] view. +/// +/// If `f` is called, then it is called with a view of the `len` bytes of the page that start +/// at `offset`. Accesses through the view are volatile or byte-wise atomic, so the view is +/// suitable for a page whose data is subject to concurrent accesses by hardware, by user space +/// or by other kernel code. When an access races with a write, the data observed may be +/// arbitrary. The view is valid for the duration of the call to `f`. +/// +/// The view is not [`Send`], as it can be backed by a mapping that is local to the current +/// task. +/// +/// If `offset` and `len` refer to a region outside of this page, then this method returns +/// [`EINVAL`] and does not call `f`. +/// +/// This method is not available for a [`Page<Exclusive>`], as writing through the view would +/// violate the aliasing rules for the slices returned by [`Page::as_slice`] and +/// [`Page::as_mut_slice`]. +/// +/// # Examples +/// +/// ``` +/// use kernel::{ +/// io::Io, +/// page::Page, // +/// }; +/// +/// let page = Page::alloc_page(GFP_KERNEL)?; +/// let mut buf = [0xffu8; 4]; +/// +/// page.with_page_as_sysmem_local(8, 4, |view| { +/// view.copy_to_slice(&mut buf); +/// assert_eq!(buf, [0u8; 4]); +/// +/// view.copy_from_slice(&[1, 2, 3, 4]); +/// view.copy_to_slice(&mut buf); +/// assert_eq!(buf, [1, 2, 3, 4]); +/// +/// view.fill_zero(); +/// view.copy_to_slice(&mut buf); +/// assert_eq!(buf, [0u8; 4]); +/// Ok(()) +/// })?; +/// # Ok::<(), Error>(()) +/// ``` +/// +/// Copy data from user space to a page: +/// +/// ``` +/// use kernel::{ +/// page::Page, +/// uaccess::UserSliceReader, // +/// }; +/// +/// fn copy_to_page(page: &Page, reader: &mut UserSliceReader, len: usize) -> Result { +/// page.with_page_as_sysmem_local(0, len, |view| reader.read_sysmem(view)) +/// } +/// ``` +pub fn with_page_as_sysmem_local <T >( + &self, + offset:usize , + len:usize , + f:impl FnOnce (SysMemLocal <'_, [u8 ]>) ->Result <T >, + ) ->Result <T > { +let f =move |mem:SysMem <'_, [u8 ]>|f (SysMemLocal ::from (mem)); + +// SAFETY: +// - By the type invariant of `Page<Shared>`, no plain access to the data of the page races +// with another access, and no Rust reference to the data of the page exists. +// - Safe code can only write initialized bytes through a `SysMemLocal`. +// - `f` converts the view to a `SysMemLocal` before it is used. A `SysMemLocal` is not +// `Send` and it cannot outlive the call to `f`, so the view is only used on the current +// task. +unsafe { self.with_page_as_sysmem (offset, len, f) } + } +} +// SAFETY: By the type invariant, a `Page<RefCounted>` 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.
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 10/11 rust_binder: access page data through `SysMemLocal`
In reply to Andreas HindborgThe pages of a binder page range are Page<RefCounted> pages, which dereference to Page<Shared>. Thus, their data can be accessed through the safe Page::with_page_as_sysmem_local().
Use the view in ShrinkablePageRange::copy_from_user_slice() and ShrinkablePageRange::fill_zero(). This removes the reliance on the safety requirements of Page::copy_from_user_slice_raw() and Page::fill_zero_raw() in these functions.
ShrinkablePageRange::read() and ShrinkablePageRange::write() copy to and from raw pointers, so they keep using the raw accessors.
--- drivers/android/binder/page_range.rs | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-)
drivers/android/binder/page_range.rs +14 -15
@@ -538,14 +538,12 @@ pub(crate) unsafe fn copy_from_user_slice( offset: usize, size: usize, ) -> Result { - // SAFETY: `self.iterate` has the same safety requirements as `copy_from_user_slice`. The - // pages of this range are `Page<RefCounted>` pages, and this driver never creates a - // reference to their data, so no reference to the data that is written exists. - unsafe { - self.iterate (offset, size, |page, offset, to_copy| { - page.copy_from_user_slice_raw (reader, offset, to_copy) - }) - } + let copy = |page: &Page, offset: usize, to_copy: usize| { + page.with_page_as_sysmem_local (offset, to_copy, |view| reader.read_sysmem (view)) + }; + +// SAFETY: `self.iterate` has the same safety requirements as `copy_from_user_slice`. +unsafe { self.iterate (offset, size, copy) } }/// Copy from this page range into kernel space.
@@ -605,14 +603,15 @@ pub(crate) unsafe fn write<T: ?Sized>(&self, offset: usize, obj: &T) -> Result {/// /// All pages touched by this operation must be in use for the duration of this call. pub (crate )unsafe fn fill_zero (&self, offset:usize , size:usize ) ->Result { -// SAFETY: `self.iterate` has the same safety requirements as `fill_zero`. The pages of -// this range are `Page<RefCounted>` pages, and this driver never creates a reference to -// their data, so no reference to the data that is written exists. -unsafe { - self.iterate (offset, size, |page, offset, len| { - page.fill_zero_raw (offset, len) +let fill = |page: &Page , offset:usize , len:usize | { + page.with_page_as_sysmem_local (offset, len, |view| { + view.fill_zero (); +Ok (()) }) - } + }; + +// SAFETY: `self.iterate` has the same safety requirements as `fill_zero`. +unsafe { self.iterate (offset, size, fill) } } }
Signature · 1 line
2.54.0
Andreas Hindborg
reviewerPATCH 11/11 rust: page: remove `Page::copy_from_user_slice_raw`
In reply to Andreas HindborgThe last user of Page::copy_from_user_slice_raw() now copies from user space through a SysMemLocal view, see UserSliceReader::read_sysmem(). Remove the unused method.
--- rust/kernel/page.rs | 32 +------------------------------- 1 file changed, 1 insertion(+), 31 deletions(-)
rust/kernel/page.rs +1 -31
@@ -32,8 +32,7 @@ Opaque, Ownable, Owned, // - }, - uaccess::UserSliceReader, // + }, // };use core::{ marker::PhantomData ,
@@ -631,35 +630,6 @@ pub unsafe fn fill_zero_raw(&self, offset: usize, len: usize) -> Result {// written are initialized. `fill` only uses the view on the current task. unsafe { self.with_page_as_sysmem (offset, len, fill) } } - -/// Copies data from userspace into this page. -/// -/// This method will perform bounds checks on the page offset. If `offset .. offset+len` goes -/// outside of the page, then this call returns [`EINVAL`]. -/// -/// Like the other `UserSliceReader` methods, data races are allowed on the userspace address. -/// However, they are not allowed on the page you are copying into. -/// -/// # Safety -/// -/// * Callers must ensure that this call does not race with a read or write to the same page -/// that overlaps with this write. -/// * Callers must ensure that no reference to the data of the page that overlaps with this -/// write exists during this call. Such references can be obtained from a -/// [`Page<Exclusive>`], see [`Page::as_slice`]. -pub unsafe fn copy_from_user_slice_raw ( - &self, - reader: &mut UserSliceReader , - offset:usize , - len:usize , - ) ->Result { - self.with_pointer_into_page (offset, len,move |dst| { -// SAFETY: If `with_pointer_into_page` calls into this closure, then it has performed a -// bounds check and guarantees that `dst` is valid for `len` bytes. The caller -// guarantees that there are no races, so `dst` is valid for atomic writes. -unsafe { reader.read_raw (dst, len) } - }) - } }impl Page <Shared > {
Signature · 1 line
2.54.0