From patchwork Mon Aug 5 15:19:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753734 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3DD84C3DA4A for ; Mon, 5 Aug 2024 15:22:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C35378D0008; Mon, 5 Aug 2024 11:22:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE6418D0001; Mon, 5 Aug 2024 11:22:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A5E8D8D0008; Mon, 5 Aug 2024 11:22:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 870418D0001 for ; Mon, 5 Aug 2024 11:22:35 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 456981A02CC for ; Mon, 5 Aug 2024 15:22:35 +0000 (UTC) X-FDA: 82418558670.30.991EE86 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id 967D5140020 for ; Mon, 5 Aug 2024 15:22:33 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LZruvTTd; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722871308; a=rsa-sha256; cv=none; b=d0lIJYD+EfUhaEm10ruX3mysGtmKHtJNiaev0VE9VU/Jmdmu91bhGwBvokN2GjkMK74bR0 MBHEGtdLcqt2O3of09Ug6z4KBvorxXlWtFzn32NRyNTlStHdsdRai509Gblxbf3oyDVHlE MbcKSJVEx+rnZeJ9Q/e5tRmTp7ZfOMQ= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LZruvTTd; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722871308; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SHvXsSdvR8gV9nOi7FMdYpjEUU1d+21ihBQXbQa3SRM=; b=NHuPwULxiMie2qqk/KiEEAYKZHwFrT8MICp/N7V6obENGYlq1YqvCCs5a4HgDQG+y2T46K skED3avHAtMqc7Y1aad3fvDnE7nl61ymeQLrBwhEcsVh/7XZmQX5oWV8N0vZQ4A7Pt/qS3 QwjkYMuPHvuo2MG7DrN4cYxClR56JM0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id C85EB60CFB; Mon, 5 Aug 2024 15:22:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6509BC4AF19; Mon, 5 Aug 2024 15:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871352; bh=iMS9KiH08GatcQmOb0IQ1b8oJCGscNUEkjpp2MaItTs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LZruvTTdj2MIMJkKEyIhRIp90mWSRe8963+FYjpGWG7TDpKeL61kjFpJq6XTHRRsR eJ+XQFqJGJmV7k8G/0tlaqfID7YOd2M4FckoJbAm9lYxR8q2AapulfeMNA3ncLjQ2k 5olemvFRi749j1zdg6uxd6KavaIYdMpYze3FXFv1vsJ8vaKPX/KNZV+yL4qJFAnhlV NnWSpoAORm9LM5eYLguKloPcUkXwkZsC7f52Oyu0NNzLc6HjUjgsfIsUqMIlQFIUOk Qfr5K7Sl3G2P96mhhtgNIOu3QGl0TmfOI8HUNePueD1JH5f/4tRXiodHg4fwi0eNd9 rczgS8BGkBgxA== 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, acurrid@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 v4 25/28] rust: alloc: implement `Cmalloc` in module allocator_test Date: Mon, 5 Aug 2024 17:19:44 +0200 Message-ID: <20240805152004.5039-26-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240805152004.5039-1-dakr@kernel.org> References: <20240805152004.5039-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 967D5140020 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: x951t1mjb7fu3fipqfd1perwnqxpcwdo X-HE-Tag: 1722871353-795570 X-HE-Meta: U2FsdGVkX19GRsasn5XWVkkBLVgaFzoQZgbf0vA4bAdaRBTJBY13ieG8/sKkXKKK8L4RanICwnYRXWMlWV/jRGZjR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ZJF0935lo+hvFymuOB1kq3l6ofabv+2kgB+nYvPHi9JIkO585vdfS9fvj/5tbK2PNrFkItByV28b9oSf3KRcBt5bNkVlqYr2DFExzHLE+fH0HggJ+hzNnFoIp9mF1Rxk0h1B4CnQEpzCbfREIYMdTLFGabTAzAnrWJo3HjQxNBa/V5yzIA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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. Signed-off-by: Danilo Krummrich --- I know, having an `old_size` parameter would indeed help implementing `Cmalloc`. However, I really don't want test infrastructure to influence the design of kernel internal APIs. Besides that, adding the `old_size` parameter would have the consequence that we'd either need to honor it for kernel allocators too (which adds another source of potential failure) or ignore it for all kernel allocators (which potentially tricks people into taking wrong assumptions on how the API works). --- rust/kernel/alloc/allocator_test.rs | 91 ++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/rust/kernel/alloc/allocator_test.rs b/rust/kernel/alloc/allocator_test.rs index 1b2642c547ec..05fd75b89238 100644 --- a/rust/kernel/alloc/allocator_test.rs +++ b/rust/kernel/alloc/allocator_test.rs @@ -2,20 +2,97 @@ #![allow(missing_docs)] -use super::{AllocError, Allocator, Flags}; +use super::{flags::*, AllocError, Allocator, Flags}; use core::alloc::Layout; +use core::ptr; use core::ptr::NonNull; -pub struct Kmalloc; +pub struct Cmalloc; +pub type Kmalloc = Cmalloc; pub type Vmalloc = Kmalloc; pub type KVmalloc = Kmalloc; -unsafe impl Allocator for Kmalloc { +extern "C" { + #[link_name = "aligned_alloc"] + fn libc_aligned_alloc(align: usize, size: usize) -> *mut core::ffi::c_void; + + #[link_name = "free"] + fn libc_free(ptr: *mut core::ffi::c_void); + + // Do not use this function for production code! For test cases only it's + // probably fine if used with care. + #[link_name = "malloc_usable_size"] + fn libc_malloc_usable_size(ptr: *mut core::ffi::c_void) -> usize; +} + +unsafe impl Allocator for Cmalloc { + fn alloc(layout: Layout, flags: Flags) -> Result, AllocError> { + let layout = layout.pad_to_align(); + + // SAFETY: Returns either NULL or a pointer to a memory allocation that satisfies or + // exceeds the given size and alignment requirements. + let raw_ptr = unsafe { libc_aligned_alloc(layout.align(), layout.size()) } as *mut u8; + + if flags.contains(__GFP_ZERO) && !raw_ptr.is_null() { + // SAFETY: `raw_ptr` points to memory successfully allocated with `libc_aligned_alloc`. + let size = unsafe { libc_malloc_usable_size(raw_ptr.cast()) }; + + // SAFETY: `raw_ptr` points to memory successfully allocated with `libc_aligned_alloc` + // of at least `size` bytes. + unsafe { core::ptr::write_bytes(raw_ptr, 0, size) }; + } + + let ptr = if layout.size() == 0 { + NonNull::dangling() + } else { + NonNull::new(raw_ptr).ok_or(AllocError)? + }; + + Ok(NonNull::slice_from_raw_parts(ptr, layout.size())) + } + unsafe fn realloc( - _ptr: Option>, - _layout: Layout, - _flags: Flags, + ptr: Option>, + layout: Layout, + flags: Flags, ) -> Result, AllocError> { - panic!(); + let layout = layout.pad_to_align(); + let src: *mut u8 = if let Some(src) = ptr { + src.as_ptr().cast() + } else { + ptr::null_mut() + }; + + if layout.size() == 0 { + // SAFETY: `src` is either NULL or has previously been allocatored with this + // `Allocator`. + unsafe { libc_free(src.cast()) }; + + return Ok(NonNull::slice_from_raw_parts(NonNull::dangling(), 0)); + } + + let dst = Self::alloc(layout, flags)?; + + if src.is_null() { + return Ok(dst); + } + + // SAFETY: `src` is either NULL or has previously been allocatored with this `Allocator`. + let old_size = unsafe { libc_malloc_usable_size(src.cast()) }; + + // SAFETY: `src` has previously been allocated with this `Allocator`; `dst` has just been + // newly allocated. Taking the minimum of their sizes guarantees that we do not exceed + // either bounds. + unsafe { + // Always copy -- do not rely on potential spare memory reported by + // malloc_usable_size() which technically may still be sufficient. + ptr::copy_nonoverlapping( + src, + dst.as_ptr().cast(), + core::cmp::min(layout.size(), old_size), + ) + }; + + Ok(dst) } }