public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] rust: alloc: add smoke test for zero-size realloc code path
@ 2026-04-24  6:44 Hsiu Che Yu
  0 siblings, 0 replies; only message in thread
From: Hsiu Che Yu @ 2026-04-24  6:44 UTC (permalink / raw)
  To: Danilo Krummrich
  Cc: Hsiu Che Yu, Lorenzo Stoakes, Vlastimil Babka, Liam R. Howlett,
	Uladzislau Rezki, Miguel Ojeda, Boqun Feng, Gary Guo,
	Björn Roy Baron, Benno Lossin, Andreas Hindborg, Alice Ryhl,
	Trevor Gross, rust-for-linux, linux-kernel

The `Allocator` trait documents that reallocating with a target size of
zero must be equivalent to freeing the memory:
    "If the requested size is zero, `realloc` behaves equivalent
    to `free`."
Add a smoke test to verify that `Kmalloc`, `Vmalloc`, and `KVmalloc`
do not error or panic when reallocating with a zero target size.
Note that the actual deallocation performed by the underlying C
functions is not verified here.

Link: https://lore.kernel.org/all/20260424052936.250238-1-yu.whisper.personal@gmail.com
Signed-off-by: Hsiu Che Yu <yu.whisper.personal@gmail.com>
---
v2: clarify that this is a smoke test and does not verify actual
        deallocation

 rust/kernel/alloc/allocator.rs | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs
index 63bfb91b3671..85606c30dab6 100644
--- a/rust/kernel/alloc/allocator.rs
+++ b/rust/kernel/alloc/allocator.rs
@@ -305,4 +305,29 @@ fn is_aligned_to(&self, align: usize) -> bool {
 
         Ok(())
     }
+
+    #[test]
+    fn test_realloc_zero_size_does_not_error() -> Result {
+        // Note: this test only verifies that zero-size realloc completes without
+        // error. The actual deallocation is performed by the underlying C functions
+        // and is not verified here.
+        const INIT_SIZE: usize = 64;
+
+        let old = Layout::from_size_align(INIT_SIZE, 1).unwrap();
+        let new = Layout::from_size_align(0, 1).unwrap();
+
+        let existing = unsafe { Kmalloc::realloc(None, old, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        let existing_ptr = existing.cast::<u8>();
+        unsafe { Kmalloc::realloc(Some(existing_ptr), new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+
+        let existing = unsafe { Vmalloc::realloc(None, old, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        let existing_ptr = existing.cast::<u8>();
+        unsafe { Vmalloc::realloc(Some(existing_ptr), new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+
+        let existing = unsafe { KVmalloc::realloc(None, old, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+        let existing_ptr = existing.cast::<u8>();
+        unsafe { KVmalloc::realloc(Some(existing_ptr), new, old, GFP_KERNEL, NumaNode::NO_NODE)? };
+
+        Ok(())
+    }
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-04-24  6:45 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-24  6:44 [PATCH v2] rust: alloc: add smoke test for zero-size realloc code path Hsiu Che Yu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox