Skip to contentSkip to conversation

rust: alloc: Fix `ArrayLayout` allocations

PATCH Applied Asahi Lina 6 participants 9 messages Nov 23, 2024 – Nov 25, 2024

Subsystems (2)
lore
Share & tools
lore

Asahi Lina

author
#0 lore raw

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.

Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
---
 rust/kernel/alloc/layout.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/layout.rs +1 -1
index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
@@ -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

Reviewed-by on v1 1/1
#1 lore raw
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.

Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")
---
 rust/kernel/alloc/layout.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/kernel/alloc/layout.rs +1 -2
index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
@@ -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!
#2 lore raw
In reply to Asahi Lina
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

#3 lore raw
Quoted 2 lines · Miguel Ojeda

(Square?)

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

Cheers,
Miguel

#4 lore raw
In reply to Miguel Ojeda
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
index 7e0c2f46157b..bb3ce3b2218b 100644
@@ -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 => {
#5 lore raw
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

maintainer Acked-by on v1 1/1
#6 lore raw
In reply to Asahi Lina
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.

Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")

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
index 7e0c2f46157b772248450a77ff445091e17fdfd7..4b3cd7fdc816c158e63ac74014cbfc0794547e81 100644
@@ -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

#7 lore raw
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.

Fixes 9e7bbfa18276 ("rust: alloc: introduce `ArrayLayout`")

Good catch!

Quoted 7 lines

(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