rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Abdiel Janulgue <abdiel.janulgue@gmail.com>
To: Daniel Almeida <daniel.almeida@collabora.com>
Cc: rust-for-linux@vger.kernel.org, aliceryhl@google.com,
	a.hindborg@kernel.org, dakr@redhat.com, airlied@redhat.com,
	miguel.ojeda.sandonis@gmail.com, wedsonaf@gmail.com,
	a.hindborg@samsung.com
Subject: Re: [PATCH v4 2/2] rust: add dma coherent allocator abstraction.
Date: Thu, 7 Nov 2024 15:01:03 +0200	[thread overview]
Message-ID: <2afe6041-20c6-4121-acb0-ffb295575619@gmail.com> (raw)
In-Reply-To: <81E07830-0A54-49C8-AC07-6511F6C816C4@collabora.com>

Hi Daniel,

Thanks for the feedback!

On 06/11/2024 18:40, Daniel Almeida wrote:
> Calling `get/get_mut()` for each T seems a bit of too much overhead.
> 
> If T is u32, for example, I expect a driver to want to read or write
> a significant number of them at once. This should be common
> if you want to read or write raw bytes.
> 
> You should probably make the CPU-addressable region available
> to users after validating if the range asked is within bounds.
> 
> Luckily, cpu_buf() will already tie the lifetime of the slice to &self,
> so they cannot touch that memory if the mapping is gone.

You mean the read/write helpers could be removed and we expose the 
cpu_buf() helper instead that takes a valid range, returns a checked 
slice and let the user have the flexibility in implementing read/writes 
themselves. Is this what you had in mind?

> 
> The fact that reads/writes will copy the memory is also a bit problematic
> given that you can offer access to the underlying buffer without copying
> anything. Also, the bound on `FromBytes/AsBytes` is, for now, a de facto
> bound on `Copy`, since it is only implemented for types which themselves
> implement Copy, i.e.:
> 
> ```
> // SAFETY: All bit patterns are acceptable values of the types below.
> unsafe impl FromBytes for u8 {}
> unsafe impl FromBytes for u16 {}
> unsafe impl FromBytes for u32 {}
> unsafe impl FromBytes for u64 {}
> unsafe impl FromBytes for usize {}
> unsafe impl FromBytes for i8 {}
> unsafe impl FromBytes for i16 {}
> unsafe impl FromBytes for i32 {}
> unsafe impl FromBytes for i64 {}
> unsafe impl FromBytes for isize {}
> // SAFETY: If all bit patterns are acceptable for individual values in an array, then all bit
> // patterns are also acceptable for arrays of that type.
> unsafe impl<T: FromBytes> FromBytes for [T] {}
> unsafe impl<T: FromBytes, const N: usize> FromBytes for [T; N] {}
> ```
> 
> Also, I think FromBytes is a bit restrictive here. In a lot of places, drivers
> expect to be able to cast the mapping to a given struct whose definition
> was sourced from some technical document or vendor code somewhere.
> 
> For example, here is some code for the H.264 decoder in the `rkvdec`
> driver:
> 
> ```
> 	struct rkvdec_h264_priv_tbl *priv_tbl;
> 
> <cut>
> 
> 	priv_tbl = dma_alloc_coherent(rkvdec->dev, sizeof(*priv_tbl),
> 				      &h264_ctx->priv_tbl.dma, GFP_KERNEL);
> ```

We could just remove the FromBytes/AsBytes bound and let
the constructor handle the ZST restriction there (like in the current 
submission)? If that is the case, there would be no need to wrestle with 
this issue then?

/Abdiel

  reply	other threads:[~2024-11-07 13:01 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-05 10:46 [PATCH v4 0/2] Add dma coherent allocator abstraction Abdiel Janulgue
2024-11-05 10:46 ` [PATCH v4 1/2] rust: error: Add EOVERFLOW Abdiel Janulgue
2024-11-05 10:46 ` [PATCH v4 2/2] rust: add dma coherent allocator abstraction Abdiel Janulgue
2024-11-05 11:23   ` Miguel Ojeda
2024-11-06  8:47     ` Abdiel Janulgue
2024-11-06 10:21     ` Dirk Behme
2024-11-06 11:25       ` Miguel Ojeda
2024-11-06 16:40   ` Daniel Almeida
2024-11-07 13:01     ` Abdiel Janulgue [this message]
2024-11-09 23:42       ` Daniel Almeida
2024-11-11 10:48         ` Abdiel Janulgue
2024-11-11 12:55   ` Andreas Hindborg
2024-11-11 12:59     ` Abdiel Janulgue
2024-11-05 11:23 ` [PATCH v4 0/2] Add " Miguel Ojeda
2024-11-06  8:47   ` Abdiel Janulgue

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2afe6041-20c6-4121-acb0-ffb295575619@gmail.com \
    --to=abdiel.janulgue@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=a.hindborg@samsung.com \
    --cc=airlied@redhat.com \
    --cc=aliceryhl@google.com \
    --cc=dakr@redhat.com \
    --cc=daniel.almeida@collabora.com \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).