From nobody Thu Nov 14 16:41:59 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6D1472225A0; Fri, 4 Oct 2024 15:44:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728056663; cv=none; b=IGMFVDOvSLjfoPlyTHCcYEt0OdALTKFp/0ygDxacw4O3pnfcOCkwp2kQqoWAmSeU0Oyfsbnaz1N4zN+hajH7IFBNuYkbO0BW7cBUEGnrN4wzN6UHbbTkQMoAakdvJyhN3nMrEc7vbrwjPyQHUOWRcbytOq7ZwbrJEnAh0ClXei0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728056663; c=relaxed/simple; bh=co+B+qJ8g94Julx8Nci+IYL+QVSNE672qLhlTjw/JNg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dTWnWzZga8k9n3rL2XPPDTwiKu+4sK+XlpZoDY1aKrzYx6/SFH7lRY2aXdQXDW5Aw/nnUvhV5gRenJEhxXbd7nRoUMV0bi/UNs23IzCEOVvy5EO7F0uGFRp/dV7UDi4ayXfcgPvhVeofkWQAcxpRj6HX0LGA+4n6uB8EfVppFLM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=isdAQEK+; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="isdAQEK+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5594AC4CEC6; Fri, 4 Oct 2024 15:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056663; bh=co+B+qJ8g94Julx8Nci+IYL+QVSNE672qLhlTjw/JNg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=isdAQEK+fyU9LQiPmikCYp+he+tsGnTfNWAcIIkeyh6V5XNcGEHFQYit/ZlRDcAa2 IOa1mMRJ6iOotZftnaTIpKf1+8I1A3Hg/20Da19AY13ysIGSwD2d6wdd9//ouP18cz y0vK4k+8CyjavjrnlM+d4IT9RtcikiE1zAT01ZjYBvp2LjtiMNLnYUBi+tMpu9okhH 5o0W0NG6ln3yvuNyu4svUcpGRnWXsdthSUKYBLl5LxTgriaBcAVdVYh1O90kT6E8zM 5+MyZjCtVE8HQAt0w4vXmUNmEaGCrcmgt79gmeSW+lbC0PhJyxa1jKO7Uuym76dtZO rkkHlrWKeoMAg== From: Danilo Krummrich To: ojeda@kernel.org, alex.gaynor@gmail.com, wedsonaf@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@samsung.com, aliceryhl@google.com, akpm@linux-foundation.org Cc: daniel.almeida@collabora.com, faith.ekstrand@collabora.com, boris.brezillon@collabora.com, lina@asahilina.net, mcanal@igalia.com, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, airlied@redhat.com, ajanulgu@redhat.com, lyude@redhat.com, linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-mm@kvack.org, Danilo Krummrich Subject: [PATCH v9 25/29] rust: alloc: implement `Cmalloc` in module allocator_test Date: Fri, 4 Oct 2024 17:41:29 +0200 Message-ID: <20241004154149.93856-26-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241004154149.93856-1-dakr@kernel.org> References: <20241004154149.93856-1-dakr@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" So far the kernel's `Box` and `Vec` types can't be used by userspace test cases, since all users of those types (e.g. `CString`) use kernel allocators for instantiation. In order to allow userspace test cases to make use of such types as well, implement the `Cmalloc` allocator within the allocator_test module and type alias all kernel allocators to `Cmalloc`. The `Cmalloc` allocator uses libc's realloc() function as allocator backend. Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- rust/kernel/alloc/allocator_test.rs | 88 ++++++++++++++++++++++++++--- 1 file changed, 81 insertions(+), 7 deletions(-) diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/alloca= tor_test.rs index 1b2642c547ec..47c2b509898e 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -1,21 +1,95 @@ // SPDX-License-Identifier: GPL-2.0 =20 +//! So far the kernel's `Box` and `Vec` types can't be used by userspace t= est cases, since all users +//! of those types (e.g. `CString`) use kernel allocators for instantiatio= n. +//! +//! In order to allow userspace test cases to make use of such types as we= ll, implement the +//! `Cmalloc` allocator within the allocator_test module and type alias al= l kernel allocators to +//! `Cmalloc`. The `Cmalloc` allocator uses libc's realloc() function as a= llocator backend. + #![allow(missing_docs)] =20 -use super::{AllocError, Allocator, Flags}; +use super::{flags::*, AllocError, Allocator, Flags}; use core::alloc::Layout; +use core::cmp; +use core::ptr; use core::ptr::NonNull; =20 -pub struct Kmalloc; +/// The userspace allocator based on libc. +pub struct Cmalloc; + +pub type Kmalloc =3D Cmalloc; pub type Vmalloc =3D Kmalloc; pub type KVmalloc =3D Kmalloc; =20 -unsafe impl Allocator for Kmalloc { +extern "C" { + #[link_name =3D "aligned_alloc"] + fn libc_aligned_alloc(align: usize, size: usize) -> *mut core::ffi::c_= void; + + #[link_name =3D "free"] + fn libc_free(ptr: *mut core::ffi::c_void); +} + +// SAFETY: +// - memory remains valid until it is explicitly freed, +// - passing a pointer to a valid memory allocation created by this `Alloc= ator` is always OK, +// - `realloc` provides the guarantees as provided in the `# Guarantees` s= ection. +unsafe impl Allocator for Cmalloc { unsafe fn realloc( - _ptr: Option>, - _layout: Layout, - _flags: Flags, + ptr: Option>, + layout: Layout, + old_layout: Layout, + flags: Flags, ) -> Result, AllocError> { - panic!(); + let src =3D match ptr { + Some(src) =3D> { + if old_layout.size() =3D=3D 0 { + ptr::null_mut() + } else { + src.as_ptr() + } + } + None =3D> ptr::null_mut(), + }; + + if layout.size() =3D=3D 0 { + // SAFETY: `src` is either NULL or was previously allocated wi= th this `Allocator` + unsafe { libc_free(src.cast()) }; + + return Ok(NonNull::slice_from_raw_parts( + crate::alloc::dangling_from_layout(layout), + 0, + )); + } + + // SAFETY: Returns either NULL or a pointer to a memory allocation= that satisfies or + // exceeds the given size and alignment requirements. + let dst =3D unsafe { libc_aligned_alloc(layout.align(), layout.siz= e()) } as *mut u8; + let dst =3D NonNull::new(dst).ok_or(AllocError)?; + + if flags.contains(__GFP_ZERO) { + // SAFETY: The preceeding calls to `libc_aligned_alloc` and `N= onNull::new` + // guarantee that `dst` points to memory of at least `layout.s= ize()` bytes. + unsafe { dst.as_ptr().write_bytes(0, layout.size()) }; + } + + if !src.is_null() { + // SAFETY: + // - `src` has previously been allocated with this `Allocator`= ; `dst` has just been + // newly allocated, hence the memory regions do not overlap. + // - both` src` and `dst` are properly aligned and valid for r= eads and writes + unsafe { + ptr::copy_nonoverlapping( + src, + dst.as_ptr(), + cmp::min(layout.size(), old_layout.size()), + ) + }; + } + + // SAFETY: `src` is either NULL or was previously allocated with t= his `Allocator` + unsafe { libc_free(src.cast()) }; + + Ok(NonNull::slice_from_raw_parts(dst, layout.size())) } } --=20 2.46.1