All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] rust: alloc: Fix `ArrayLayout` allocations
@ 2024-11-23 10:29 Asahi Lina
  2024-11-23 10:47 ` Neal Gompa
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Asahi Lina @ 2024-11-23 10:29 UTC (permalink / raw)
  To: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross
  Cc: Janne Grunau, rust-for-linux, linux-kernel, asahi, Asahi Lina

We were accidentally allocating a layout for the *square* of the object
size due to a variable shadowing mishap.

Fixes memory bloat and page allocation failures in drm/asahi.

Reported-by: Janne Grunau <j@jannau.net>
Fixes: 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
Signed-off-by: Asahi Lina <lina@asahilina.net>
---
 rust/kernel/alloc/layout.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -45,7 +45,7 @@ pub const fn empty() -> Self {
     /// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
     pub const fn new(len: usize) -> Result<Self, LayoutError> {
         match len.checked_mul(core::mem::size_of::<T>()) {
-            Some(len) if len <= ISIZE_MAX => {
+            Some(size) if size <= ISIZE_MAX => {
                 // INVARIANT: We checked above that `len * size_of::<T>() <= isize::MAX`.
                 Ok(Self {
                     len,

---
base-commit: b2603f8ac8217bc59f5c7f248ac248423b9b99cb
change-id: 20241123-rust-fix-arraylayout-0b1009d89fb7

Cheers,
~~ Lina


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 10:29 [PATCH] rust: alloc: Fix `ArrayLayout` allocations Asahi Lina
@ 2024-11-23 10:47 ` Neal Gompa
  2024-11-23 17:39 ` Miguel Ojeda
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Neal Gompa @ 2024-11-23 10:47 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 5:30 AM Asahi Lina <lina@asahilina.net> wrote:
>
> We were accidentally allocating a layout for the *square* of the object
> size due to a variable shadowing mishap.
>
> Fixes memory bloat and page allocation failures in drm/asahi.
>
> Reported-by: Janne Grunau <j@jannau.net>
> Fixes: 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
> Signed-off-by: Asahi Lina <lina@asahilina.net>
> ---
>  rust/kernel/alloc/layout.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -45,7 +45,7 @@ pub const fn empty() -> Self {
>      /// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
>      pub const fn new(len: usize) -> Result<Self, LayoutError> {
>          match len.checked_mul(core::mem::size_of::<T>()) {
> -            Some(len) if len <= ISIZE_MAX => {
> +            Some(size) if size <= ISIZE_MAX => {
>                  // INVARIANT: We checked above that `len * size_of::<T>() <= isize::MAX`.
>                  Ok(Self {
>                      len,
>
> ---
> base-commit: b2603f8ac8217bc59f5c7f248ac248423b9b99cb
> change-id: 20241123-rust-fix-arraylayout-0b1009d89fb7
>
> Cheers,
> ~~ Lina
>
>

The joy of logic bugs. :(

Reviewed-by: Neal Gompa <neal@gompa.dev>


-- 
真実はいつも一つ!/ Always, there's only one truth!

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 10:29 [PATCH] rust: alloc: Fix `ArrayLayout` allocations Asahi Lina
  2024-11-23 10:47 ` Neal Gompa
@ 2024-11-23 17:39 ` Miguel Ojeda
  2024-11-23 17:49   ` Miguel Ojeda
  2024-11-25 16:20   ` Boqun Feng
  2024-11-23 20:08 ` Danilo Krummrich
  2024-11-24 23:56 ` Miguel Ojeda
  3 siblings, 2 replies; 9+ messages in thread
From: Miguel Ojeda @ 2024-11-23 17:39 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 11:30 AM Asahi Lina <lina@asahilina.net> wrote:
>
> We were accidentally allocating a layout for the *square* of the object
> size due to a variable shadowing mishap.

Good catch, thanks! (Square?)

`clippy::shadow_reuse` would catch this, but it does catch a lot more
things, sadly.

I sent:

    https://github.com/rust-lang/rust-clippy/issues/3433#issuecomment-2495547322

and added it to the Clippy list. If one of the ideas in the issue are
implemented, then I think we should easily enable it.

Similarly, we could do `shadow_unrelated` -- that one seems easier,
and perhaps we should do it anyway, at least a couple cases I saw
would make the code clearer.

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 17:39 ` Miguel Ojeda
@ 2024-11-23 17:49   ` Miguel Ojeda
  2024-11-25 16:20   ` Boqun Feng
  1 sibling, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2024-11-23 17:49 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 6:39 PM Miguel Ojeda
<miguel.ojeda.sandonis@gmail.com> wrote:
>
> (Square?)

Ah, you mean eventually on the actual allocation, never mind!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 10:29 [PATCH] rust: alloc: Fix `ArrayLayout` allocations Asahi Lina
  2024-11-23 10:47 ` Neal Gompa
  2024-11-23 17:39 ` Miguel Ojeda
@ 2024-11-23 20:08 ` Danilo Krummrich
  2024-11-23 21:55   ` Janne Grunau
  2024-11-24 23:56 ` Miguel Ojeda
  3 siblings, 1 reply; 9+ messages in thread
From: Danilo Krummrich @ 2024-11-23 20:08 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 07:29:38PM +0900, Asahi Lina wrote:
> We were accidentally allocating a layout for the *square* of the object
> size due to a variable shadowing mishap.
> 
> Fixes memory bloat and page allocation failures in drm/asahi.
> 
> Reported-by: Janne Grunau <j@jannau.net>
> Fixes: 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
> Signed-off-by: Asahi Lina <lina@asahilina.net>

Good catch!

Acked-by: Danilo Krummrich <dakr@kernel.org>

(I'm just back from moving and just starting to catch up on what was going on
in the last few weeks.)

Is this related to the performance regression that has been observed by Andreas?
Or did it turn out to be a false positive?

- Danilo

> ---
>  rust/kernel/alloc/layout.rs | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
> index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
> --- a/rust/kernel/alloc/layout.rs
> +++ b/rust/kernel/alloc/layout.rs
> @@ -45,7 +45,7 @@ pub const fn empty() -> Self {
>      /// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
>      pub const fn new(len: usize) -> Result<Self, LayoutError> {
>          match len.checked_mul(core::mem::size_of::<T>()) {
> -            Some(len) if len <= ISIZE_MAX => {
> +            Some(size) if size <= ISIZE_MAX => {
>                  // INVARIANT: We checked above that `len * size_of::<T>() <= isize::MAX`.
>                  Ok(Self {
>                      len,
> 
> ---
> base-commit: b2603f8ac8217bc59f5c7f248ac248423b9b99cb
> change-id: 20241123-rust-fix-arraylayout-0b1009d89fb7
> 
> Cheers,
> ~~ Lina
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 20:08 ` Danilo Krummrich
@ 2024-11-23 21:55   ` Janne Grunau
  0 siblings, 0 replies; 9+ messages in thread
From: Janne Grunau @ 2024-11-23 21:55 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Asahi Lina, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 09:08:35PM +0100, Danilo Krummrich wrote:
> On Sat, Nov 23, 2024 at 07:29:38PM +0900, Asahi Lina wrote:
> > We were accidentally allocating a layout for the *square* of the object
> > size due to a variable shadowing mishap.
> > 
> > Fixes memory bloat and page allocation failures in drm/asahi.
> > 
> > Reported-by: Janne Grunau <j@jannau.net>
> > Fixes: 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
> > Signed-off-by: Asahi Lina <lina@asahilina.net>
> 
> Good catch!
> 
> Acked-by: Danilo Krummrich <dakr@kernel.org>
> 
> (I'm just back from moving and just starting to catch up on what was going on
> in the last few weeks.)
> 
> Is this related to the performance regression that has been observed by Andreas?
> Or did it turn out to be a false positive?

No idea. We noticed KVec allocation errors with page order 4 to 8 under
memory pressure which weren't observed with the previous allocator (or at
least not that easily).

I haven't noticed performance regressions with asahi's usage. glmark2
score was roughly at the expected value.

Janne

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 10:29 [PATCH] rust: alloc: Fix `ArrayLayout` allocations Asahi Lina
                   ` (2 preceding siblings ...)
  2024-11-23 20:08 ` Danilo Krummrich
@ 2024-11-24 23:56 ` Miguel Ojeda
  3 siblings, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2024-11-24 23:56 UTC (permalink / raw)
  To: Asahi Lina
  Cc: Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 11:30 AM Asahi Lina <lina@asahilina.net> wrote:
>
> We were accidentally allocating a layout for the *square* of the object
> size due to a variable shadowing mishap.
>
> Fixes memory bloat and page allocation failures in drm/asahi.
>
> Reported-by: Janne Grunau <j@jannau.net>
> Fixes: 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
> Signed-off-by: Asahi Lina <lina@asahilina.net>

Applied to `rust-next` -- thanks everyone!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-23 17:39 ` Miguel Ojeda
  2024-11-23 17:49   ` Miguel Ojeda
@ 2024-11-25 16:20   ` Boqun Feng
  2024-11-25 16:26     ` Miguel Ojeda
  1 sibling, 1 reply; 9+ messages in thread
From: Boqun Feng @ 2024-11-25 16:20 UTC (permalink / raw)
  To: Miguel Ojeda
  Cc: Asahi Lina, Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Sat, Nov 23, 2024 at 06:39:23PM +0100, Miguel Ojeda wrote:
> On Sat, Nov 23, 2024 at 11:30 AM Asahi Lina <lina@asahilina.net> wrote:
> >
> > We were accidentally allocating a layout for the *square* of the object
> > size due to a variable shadowing mishap.
> 
> Good catch, thanks! (Square?)
> 

While we are at it, I think it'll be good to add some example/tests for
those functions of ArrayLayout, for example, the below will catch this:

I will open a good-first-issue.

Regards,
Boqun

------------------------>8
diff --git a/rust/kernel/alloc/layout.rs b/rust/kernel/alloc/layout.rs
index 7e0c2f46157b..bb3ce3b2218b 100644
--- a/rust/kernel/alloc/layout.rs
+++ b/rust/kernel/alloc/layout.rs
@@ -7,6 +7,7 @@
 use core::{alloc::Layout, marker::PhantomData};
 
 /// Error when constructing an [`ArrayLayout`].
+#[derive(Debug)]
 pub struct LayoutError;
 
 /// A layout for an array `[T; n]`.
@@ -43,6 +44,20 @@ pub const fn empty() -> Self {
     /// # Errors
     ///
     /// When `len * size_of::<T>()` overflows or when `len * size_of::<T>() > isize::MAX`.
+    ///
+    /// # Examples
+    ///
+    /// ```rust
+    /// use kernel::alloc::layout::ArrayLayout;
+    ///
+    /// // No overflow.
+    /// let layout = ArrayLayout::<i32>::new(12);
+    /// assert_eq!(layout.expect("sizeof(i32) * 12 is 48, not overflow").len(), 12);
+    ///
+    /// // Overflow, should return `Err`.
+    /// let layout = ArrayLayout::<i32>::new(isize::MAX as usize);
+    /// assert!(layout.is_err());
+    /// ```
     pub const fn new(len: usize) -> Result<Self, LayoutError> {
         match len.checked_mul(core::mem::size_of::<T>()) {
             Some(len) if len <= ISIZE_MAX => {

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] rust: alloc: Fix `ArrayLayout` allocations
  2024-11-25 16:20   ` Boqun Feng
@ 2024-11-25 16:26     ` Miguel Ojeda
  0 siblings, 0 replies; 9+ messages in thread
From: Miguel Ojeda @ 2024-11-25 16:26 UTC (permalink / raw)
  To: Boqun Feng
  Cc: Asahi Lina, Danilo Krummrich, Miguel Ojeda, Alex Gaynor, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, Janne Grunau, rust-for-linux, linux-kernel, asahi

On Mon, Nov 25, 2024 at 5:20 PM Boqun Feng <boqun.feng@gmail.com> wrote:
>
> While we are at it, I think it'll be good to add some example/tests for
> those functions of ArrayLayout, for example, the below will catch this:
>
> I will open a good-first-issue.

Indeed, thanks!

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-11-25 16:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-23 10:29 [PATCH] rust: alloc: Fix `ArrayLayout` allocations Asahi Lina
2024-11-23 10:47 ` Neal Gompa
2024-11-23 17:39 ` Miguel Ojeda
2024-11-23 17:49   ` Miguel Ojeda
2024-11-25 16:20   ` Boqun Feng
2024-11-25 16:26     ` Miguel Ojeda
2024-11-23 20:08 ` Danilo Krummrich
2024-11-23 21:55   ` Janne Grunau
2024-11-24 23:56 ` Miguel Ojeda

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.