* [PATCH 2/3] rust: dma: generalize BinaryWriter impl for Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
@ 2026-03-25 0:39 ` Danilo Krummrich
2026-03-25 0:39 ` [PATCH 3/3] gpu: nova-core: use sized array for GSP log buffers Danilo Krummrich
` (5 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2026-03-25 0:39 UTC (permalink / raw)
To: acourbot, aliceryhl, abdiel.janulgue, daniel.almeida,
robin.murphy, a.hindborg, ojeda, boqun, gary, bjorn3_gh, lossin,
tmgross, ttabi
Cc: nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux,
Danilo Krummrich
Generalize the BinaryWriter implementation from Coherent<[u8]> to
Coherent<T> where T: KnownSize + AsBytes + ?Sized. The implementation
only uses size() and write_dma(), neither of which depends on the
inner type being a byte slice.
This allows any Coherent allocation with an AsBytes inner type to be
exposed as a debugfs binary file.
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
rust/kernel/dma.rs | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/rust/kernel/dma.rs b/rust/kernel/dma.rs
index 3eef7c2396bb..7bc97f9f83fd 100644
--- a/rust/kernel/dma.rs
+++ b/rust/kernel/dma.rs
@@ -885,7 +885,7 @@ unsafe impl<T: KnownSize + Send + ?Sized> Send for Coherent<T> {}
// The safe methods only return metadata or raw pointers whose use requires `unsafe`.
unsafe impl<T: KnownSize + ?Sized + AsBytes + FromBytes + Sync> Sync for Coherent<T> {}
-impl debugfs::BinaryWriter for Coherent<[u8]> {
+impl<T: KnownSize + AsBytes + ?Sized> debugfs::BinaryWriter for Coherent<T> {
fn write_to_slice(
&self,
writer: &mut UserSliceWriter,
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH 3/3] gpu: nova-core: use sized array for GSP log buffers
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
2026-03-25 0:39 ` [PATCH 2/3] rust: dma: generalize BinaryWriter impl for Coherent<T> Danilo Krummrich
@ 2026-03-25 0:39 ` Danilo Krummrich
2026-03-25 11:44 ` [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Alice Ryhl
` (4 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Danilo Krummrich @ 2026-03-25 0:39 UTC (permalink / raw)
To: acourbot, aliceryhl, abdiel.janulgue, daniel.almeida,
robin.murphy, a.hindborg, ojeda, boqun, gary, bjorn3_gh, lossin,
tmgross, ttabi
Cc: nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux,
Danilo Krummrich
Switch LogBuffer from Coherent<[u8]> (unsized) to
Coherent<[u8; LOG_BUFFER_SIZE]> (sized). The buffer size is a
compile-time constant (RM_LOG_BUFFER_NUM_PAGES * GSP_PAGE_SIZE), so a
fixed-size array is more precise and avoids the need for the runtime
length parameter of zeroed_slice().
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
---
drivers/gpu/nova-core/gsp.rs | 16 ++++++----------
1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/drivers/gpu/nova-core/gsp.rs b/drivers/gpu/nova-core/gsp.rs
index 04e3976127cc..ba5b7f990031 100644
--- a/drivers/gpu/nova-core/gsp.rs
+++ b/drivers/gpu/nova-core/gsp.rs
@@ -42,6 +42,7 @@
/// Number of GSP pages to use in a RM log buffer.
const RM_LOG_BUFFER_NUM_PAGES: usize = 0x10;
+const LOG_BUFFER_SIZE: usize = RM_LOG_BUFFER_NUM_PAGES * GSP_PAGE_SIZE;
/// Array of page table entries, as understood by the GSP bootloader.
#[repr(C)]
@@ -77,24 +78,19 @@ fn entry(start: DmaAddress, index: usize) -> Result<u64> {
/// then pp points to index into the buffer where the next logging entry will
/// be written. Therefore, the logging data is valid if:
/// 1 <= pp < sizeof(buffer)/sizeof(u64)
-struct LogBuffer(Coherent<[u8]>);
+struct LogBuffer(Coherent<[u8; LOG_BUFFER_SIZE]>);
impl LogBuffer {
/// Creates a new `LogBuffer` mapped on `dev`.
fn new(dev: &device::Device<device::Bound>) -> Result<Self> {
- const NUM_PAGES: usize = RM_LOG_BUFFER_NUM_PAGES;
-
- let obj = Self(Coherent::<u8>::zeroed_slice(
- dev,
- NUM_PAGES * GSP_PAGE_SIZE,
- GFP_KERNEL,
- )?);
+ let obj = Self(Coherent::zeroed(dev, GFP_KERNEL)?);
let start_addr = obj.0.dma_handle();
// SAFETY: `obj` has just been created and we are its sole user.
- let pte_region =
- unsafe { &mut obj.0.as_mut()[size_of::<u64>()..][..NUM_PAGES * size_of::<u64>()] };
+ let pte_region = unsafe {
+ &mut obj.0.as_mut()[size_of::<u64>()..][..RM_LOG_BUFFER_NUM_PAGES * size_of::<u64>()]
+ };
// Write values one by one to avoid an on-stack instance of `PteArray`.
for (i, chunk) in pte_region.chunks_exact_mut(size_of::<u64>()).enumerate() {
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
2026-03-25 0:39 ` [PATCH 2/3] rust: dma: generalize BinaryWriter impl for Coherent<T> Danilo Krummrich
2026-03-25 0:39 ` [PATCH 3/3] gpu: nova-core: use sized array for GSP log buffers Danilo Krummrich
@ 2026-03-25 11:44 ` Alice Ryhl
2026-03-25 13:01 ` Gary Guo
` (3 subsequent siblings)
6 siblings, 0 replies; 10+ messages in thread
From: Alice Ryhl @ 2026-03-25 11:44 UTC (permalink / raw)
To: Danilo Krummrich
Cc: acourbot, abdiel.janulgue, daniel.almeida, robin.murphy,
a.hindborg, ojeda, boqun, gary, bjorn3_gh, lossin, tmgross, ttabi,
nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux
On Wed, Mar 25, 2026 at 01:39:15AM +0100, Danilo Krummrich wrote:
> Generalize write_dma() from &Coherent<[u8]> to &Coherent<T> where
> T: KnownSize + AsBytes + ?Sized. The function body only uses as_ptr()
> and size(), which work for any such T, so there is no reason to
> restrict it to byte slices.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
` (2 preceding siblings ...)
2026-03-25 11:44 ` [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Alice Ryhl
@ 2026-03-25 13:01 ` Gary Guo
2026-03-25 13:46 ` Danilo Krummrich
2026-03-26 0:39 ` Alexandre Courbot
` (2 subsequent siblings)
6 siblings, 1 reply; 10+ messages in thread
From: Gary Guo @ 2026-03-25 13:01 UTC (permalink / raw)
To: Danilo Krummrich, acourbot, aliceryhl, abdiel.janulgue,
daniel.almeida, robin.murphy, a.hindborg, ojeda, boqun, gary,
bjorn3_gh, lossin, tmgross, ttabi
Cc: nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux
On Wed Mar 25, 2026 at 12:39 AM GMT, Danilo Krummrich wrote:
> Generalize write_dma() from &Coherent<[u8]> to &Coherent<T> where
> T: KnownSize + AsBytes + ?Sized. The function body only uses as_ptr()
> and size(), which work for any such T, so there is no reason to
> restrict it to byte slices.
I think there is a reason, which is that it weakens the strong typing that we
have. I think it'll be better to have this be
fn write_dma<T>(&mut self, view: View<'_, Coherent<T>, [u8]>, offset: usize) -> Result
once we have I/O view and expect people to create cast views into bytes
explicitly.
Although, given that we already have APIs like
fn write<T: AsBytes>(&mut self, value: &T)
I'm okay with putting this in until we have a replacement solution.
The fact that our current uaccess mechanism is untyped itself is something that
I dislike (it feels a step back that C can represent `arbitary_type __user*` and
we just have a `void __user*`). It's on my list of things to fix to make a
generic `UserPtr<T>`.
Best,
Gary
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
> ---
> rust/kernel/uaccess.rs | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> index e26ef90ba8ad..6c9c1cce3c63 100644
> --- a/rust/kernel/uaccess.rs
> +++ b/rust/kernel/uaccess.rs
> @@ -12,6 +12,7 @@
> ffi::{c_char, c_void},
> fs::file,
> prelude::*,
> + ptr::KnownSize,
> transmute::{AsBytes, FromBytes},
> };
> use core::mem::{size_of, MaybeUninit};
> @@ -524,7 +525,12 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result {
> /// writer.write_dma(alloc, 0, 256)
> /// }
> /// ```
> - pub fn write_dma(&mut self, alloc: &Coherent<[u8]>, offset: usize, count: usize) -> Result {
> + pub fn write_dma<T: KnownSize + AsBytes + ?Sized>(
> + &mut self,
> + alloc: &Coherent<T>,
> + offset: usize,
> + count: usize,
> + ) -> Result {
> let len = alloc.size();
> if offset.checked_add(count).ok_or(EOVERFLOW)? > len {
> return Err(ERANGE);
>
> base-commit: dff8302ca1d0e773c90dbeeb05e759f995c95482
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 13:01 ` Gary Guo
@ 2026-03-25 13:46 ` Danilo Krummrich
2026-03-25 14:55 ` Gary Guo
0 siblings, 1 reply; 10+ messages in thread
From: Danilo Krummrich @ 2026-03-25 13:46 UTC (permalink / raw)
To: Gary Guo
Cc: acourbot, aliceryhl, abdiel.janulgue, daniel.almeida,
robin.murphy, a.hindborg, ojeda, boqun, bjorn3_gh, lossin,
tmgross, ttabi, nouveau, dri-devel, linux-kernel, driver-core,
rust-for-linux
On Wed Mar 25, 2026 at 2:01 PM CET, Gary Guo wrote:
> I think there is a reason, which is that it weakens the strong typing that we
> have. I think it'll be better to have this be
>
> fn write_dma<T>(&mut self, view: View<'_, Coherent<T>, [u8]>, offset: usize) -> Result
>
> once we have I/O view and expect people to create cast views into bytes
> explicitly.
>
There is technically no difference between the above and the below, except that
for the above the user has to create the View first, i.e. it needs additional
code. What's the advantage?
>> + pub fn write_dma<T: KnownSize + AsBytes + ?Sized>(
>> + &mut self,
>> + alloc: &Coherent<T>,
>> + offset: usize,
>> + count: usize,
>> + ) -> Result {
<snip>
> Although, given that we already have APIs like
>
> fn write<T: AsBytes>(&mut self, value: &T)
>
> I'm okay with putting this in until we have a replacement solution.
Sounds good.
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 13:46 ` Danilo Krummrich
@ 2026-03-25 14:55 ` Gary Guo
0 siblings, 0 replies; 10+ messages in thread
From: Gary Guo @ 2026-03-25 14:55 UTC (permalink / raw)
To: Danilo Krummrich, Gary Guo
Cc: acourbot, aliceryhl, abdiel.janulgue, daniel.almeida,
robin.murphy, a.hindborg, ojeda, boqun, bjorn3_gh, lossin,
tmgross, ttabi, nouveau, dri-devel, linux-kernel, driver-core,
rust-for-linux
On Wed Mar 25, 2026 at 1:46 PM GMT, Danilo Krummrich wrote:
> On Wed Mar 25, 2026 at 2:01 PM CET, Gary Guo wrote:
>> I think there is a reason, which is that it weakens the strong typing that we
>> have. I think it'll be better to have this be
>>
>> fn write_dma<T>(&mut self, view: View<'_, Coherent<T>, [u8]>, offset: usize) -> Result
Correcting myself, this will be
fn write_dma<T>(&mut self, view: View<'_, Coherent<T>, [u8]>) -> Result
(without offset, similar to the write_slice).
>>
>> once we have I/O view and expect people to create cast views into bytes
>> explicitly.
>>
>
> There is technically no difference between the above and the below, except that
> for the above the user has to create the View first, i.e. it needs additional
> code. What's the advantage?
It prevents misuses. If you have a `[u8]` or `[u8; SIZE]` already, the change is
going to be small:
u.write_dma(c, offset, count)
vs.
u.write_dma(io_project!(c, [offset..][..count]))
[
PS. it looks like with current proposals w/ virtual places, we might be able
to get this written as
u.write_dma(c[offset..][..count])
a few years down the road if we're successful.
]
However, using a view will prevent you from feeding an arbitary
`Coherent<FooBar>` and just write it to user space. `offset` and `count` in the
API really doesn't make too much sense at all if `T` is not array/slice of u8.
Best,
Gary
>
>>> + pub fn write_dma<T: KnownSize + AsBytes + ?Sized>(
>>> + &mut self,
>>> + alloc: &Coherent<T>,
>>> + offset: usize,
>>> + count: usize,
>>> + ) -> Result {
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
` (3 preceding siblings ...)
2026-03-25 13:01 ` Gary Guo
@ 2026-03-26 0:39 ` Alexandre Courbot
2026-03-26 13:33 ` Gary Guo
2026-03-26 18:17 ` Miguel Ojeda
6 siblings, 0 replies; 10+ messages in thread
From: Alexandre Courbot @ 2026-03-26 0:39 UTC (permalink / raw)
To: Danilo Krummrich
Cc: aliceryhl, abdiel.janulgue, daniel.almeida, robin.murphy,
a.hindborg, ojeda, boqun, gary, bjorn3_gh, lossin, tmgross, ttabi,
nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux
On Wed Mar 25, 2026 at 9:39 AM JST, Danilo Krummrich wrote:
> Generalize write_dma() from &Coherent<[u8]> to &Coherent<T> where
> T: KnownSize + AsBytes + ?Sized. The function body only uses as_ptr()
> and size(), which work for any such T, so there is no reason to
> restrict it to byte slices.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
The series,
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
` (4 preceding siblings ...)
2026-03-26 0:39 ` Alexandre Courbot
@ 2026-03-26 13:33 ` Gary Guo
2026-03-26 18:17 ` Miguel Ojeda
6 siblings, 0 replies; 10+ messages in thread
From: Gary Guo @ 2026-03-26 13:33 UTC (permalink / raw)
To: Danilo Krummrich, acourbot, aliceryhl, abdiel.janulgue,
daniel.almeida, robin.murphy, a.hindborg, ojeda, boqun, gary,
bjorn3_gh, lossin, tmgross, ttabi
Cc: nouveau, dri-devel, linux-kernel, driver-core, rust-for-linux
On Wed Mar 25, 2026 at 12:39 AM GMT, Danilo Krummrich wrote:
> Generalize write_dma() from &Coherent<[u8]> to &Coherent<T> where
> T: KnownSize + AsBytes + ?Sized. The function body only uses as_ptr()
> and size(), which work for any such T, so there is no reason to
> restrict it to byte slices.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
For the series:
Acked-by: Gary Guo <gary@garyguo.net>
> ---
> rust/kernel/uaccess.rs | 8 +++++++-
> 1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> index e26ef90ba8ad..6c9c1cce3c63 100644
> --- a/rust/kernel/uaccess.rs
> +++ b/rust/kernel/uaccess.rs
> @@ -12,6 +12,7 @@
> ffi::{c_char, c_void},
> fs::file,
> prelude::*,
> + ptr::KnownSize,
> transmute::{AsBytes, FromBytes},
> };
> use core::mem::{size_of, MaybeUninit};
> @@ -524,7 +525,12 @@ pub fn write_slice(&mut self, data: &[u8]) -> Result {
> /// writer.write_dma(alloc, 0, 256)
> /// }
> /// ```
> - pub fn write_dma(&mut self, alloc: &Coherent<[u8]>, offset: usize, count: usize) -> Result {
> + pub fn write_dma<T: KnownSize + AsBytes + ?Sized>(
> + &mut self,
> + alloc: &Coherent<T>,
> + offset: usize,
> + count: usize,
> + ) -> Result {
> let len = alloc.size();
> if offset.checked_add(count).ok_or(EOVERFLOW)? > len {
> return Err(ERANGE);
>
> base-commit: dff8302ca1d0e773c90dbeeb05e759f995c95482
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T>
2026-03-25 0:39 [PATCH 1/3] rust: uaccess: generalize write_dma() to accept any Coherent<T> Danilo Krummrich
` (5 preceding siblings ...)
2026-03-26 13:33 ` Gary Guo
@ 2026-03-26 18:17 ` Miguel Ojeda
6 siblings, 0 replies; 10+ messages in thread
From: Miguel Ojeda @ 2026-03-26 18:17 UTC (permalink / raw)
To: Danilo Krummrich
Cc: acourbot, aliceryhl, abdiel.janulgue, daniel.almeida,
robin.murphy, a.hindborg, ojeda, boqun, gary, bjorn3_gh, lossin,
tmgross, ttabi, nouveau, dri-devel, linux-kernel, driver-core,
rust-for-linux
On Wed, Mar 25, 2026 at 1:39 AM Danilo Krummrich <dakr@kernel.org> wrote:
>
> Generalize write_dma() from &Coherent<[u8]> to &Coherent<T> where
> T: KnownSize + AsBytes + ?Sized. The function body only uses as_ptr()
> and size(), which work for any such T, so there is no reason to
> restrict it to byte slices.
>
> Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Cheers,
Miguel
^ permalink raw reply [flat|nested] 10+ messages in thread