rust: alloc: Fix `ArrayLayout` allocations
Subsystems (2)
PATCH 1/1 rust: alloc: Fix `ArrayLayout` allocations
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.
--- rust/kernel/alloc/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/layout.rs +1 -1
@@ -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
Quoted 31 lines · 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 <[email protected]>Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")Signed-off-by Asahi Lina <[email protected]>--- rust/kernel/alloc/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)rust/kernel/alloc/layout.rs +1
-2@@ -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
Quoted 4 lines
Cheers,
~~ Lina
The joy of logic bugs. :(
Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!
Quoted 3 lines · Asahi Lina
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
Quoted 2 lines · Miguel Ojeda
(Square?)
Ah, you mean eventually on the actual allocation, never mind!
Cheers,
Miguel
Quoted 7 lines · Miguel Ojeda
Quoted 3 lines · Asahi Lina
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
rust/kernel/alloc/layout.rs +15 -0
@@ -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 => {
Quoted 5 lines · Boqun Feng
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
Quoted 8 lines · 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 <[email protected]>Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")Signed-off-by Asahi Lina <[email protected]>
Good catch!
(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
Quoted 25 lines
--- rust/kernel/alloc/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)rust/kernel/alloc/layout.rs +1
-2@@ -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-0b1009d89fb7Cheers,
~~ Lina
Quoted 12 lines · Danilo Krummrich
Quoted 8 lines · 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 <[email protected]>Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")Signed-off-by Asahi Lina <[email protected]>Good catch!
Quoted 7 lines
Acked-by Danilo Krummrich <[email protected]>(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
Miguel Ojeda
AppliedQuoted 9 lines · 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 <[email protected]>Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")Signed-off-by Asahi Lina <[email protected]>
Applied to rust-next -- thanks everyone!
Cheers,
Miguel