public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Gary Guo" <gary@garyguo.net>
To: "Aditya Rajan" <adi.dev.github@gmail.com>, <dakr@kernel.org>,
	<ojeda@kernel.org>
Cc: <abdiel.janulgue@gmail.com>, <daniel.almeida@collabora.com>,
	<robin.murphy@arm.com>, <a.hindborg@kernel.org>,
	<boqun@kernel.org>, <gary@garyguo.net>,
	<bjorn3_gh@protonmail.com>, <lossin@kernel.org>,
	<aliceryhl@google.com>, <tmgross@umich.edu>,
	<driver-core@lists.linux.dev>, <rust-for-linux@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] rust: dma: return EOVERFLOW instead of ENOMEM on size overflow
Date: Sat, 04 Apr 2026 14:15:28 +0100	[thread overview]
Message-ID: <DHKECN57N95Q.2DN7NG4GUA24F@garyguo.net> (raw)
In-Reply-To: <20260403212822.294288-1-adi.dev.github@gmail.com>

On Fri Apr 3, 2026 at 10:28 PM BST, Aditya Rajan wrote:
> In alloc_slice_with_attrs(), the checked_mul() guards against
> arithmetic overflow when computing the total byte size
> (size_of::<T>() * len). If this overflows, the current code returns
> ENOMEM, which is misleading -- the system is not out of memory, the
> requested size simply cannot be represented in a usize.
>
> Return EOVERFLOW instead, which accurately describes the failure. This
> also distinguishes it from the actual allocation failure two lines
> below, which correctly returns ENOMEM when dma_alloc_attrs() yields a
> null pointer.
>
> Fixes: d9aee73c56ee ("rust: dma: add generalized container for types other than slices")
> Link: https://lore.kernel.org/all/20260320194626.36263-3-dakr@kernel.org/
>
> Signed-off-by: Aditya Rajan <adi.dev.github@gmail.com>
> ---
>  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 4995ee5dc689..179bc8832947 100644
> --- a/rust/kernel/dma.rs
> +++ b/rust/kernel/dma.rs
> @@ -832,7 +832,7 @@ fn alloc_slice_with_attrs(
>              Err(EINVAL)?;
>          }
>  
> -        let size = core::mem::size_of::<T>().checked_mul(len).ok_or(ENOMEM)?;
> +        let size = core::mem::size_of::<T>().checked_mul(len).ok_or(EOVERFLOW)?;
>          let mut dma_handle = 0;
>          // SAFETY: Device pointer is guaranteed as valid by the type invariant on `Device`.
>          let addr = unsafe {

Hi Aditya,

Thanks for the patch, but the behaviour here is intended.

Neither our `KVec` implementation nor upstream Rust distinguishes between
allocation error caused by array size exceeding address space or running out of
memory to allocate (`AllocError` is returned and it converts to ENOMEM).

`kmalloc_array` also just returns `NULL` when overflows, so arguably this
behaviour also aligns us with C side.

Abstractly, the system is indeed running out memory because it cannot allocate
something larger than its address space.

Best,
Gary

  reply	other threads:[~2026-04-04 13:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-03 21:28 [PATCH] rust: dma: return EOVERFLOW instead of ENOMEM on size overflow Aditya Rajan
2026-04-04 13:15 ` Gary Guo [this message]
2026-04-04 17:24   ` Aditya Rajan
2026-04-04 19:43     ` Danilo Krummrich
2026-04-04 20:13     ` Gary Guo
2026-04-04 20:28       ` Danilo Krummrich
2026-04-05  9:51 ` kernel test robot

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=DHKECN57N95Q.2DN7NG4GUA24F@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=abdiel.janulgue@gmail.com \
    --cc=adi.dev.github@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=robin.murphy@arm.com \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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