From nobody Thu Nov 14 16:23:03 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 C48F41DF845; Fri, 4 Oct 2024 15:42:16 +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=1728056538; cv=none; b=FD2sEx5nK1sytPDkXqV711TSXQRh6Bz5xpobRWMC8DknWgP7Hy4hUSjLsFF47GNr3jOBE/mB96Qb2q3NqRe6+4p7NGYVOPgdjUrIez7cEa9k7sZSjUG9j4nJInChGDWSt8cfrGtbwI0TINsRzf77pcFZQL/CSoI1ZAniG8Zw4pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728056538; c=relaxed/simple; bh=RKYOxaZlfRNBRkHBIioYvHfe6spLUu1mZk00Wam8Has=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aWzWufMQt2t3Rnz2CNUEPCqaRR7NuwjVDyd3VcnxnDqxltZp6FXHquCkpBXe16toOYS6an0pdEj76wTRBferYQeQrBqpHGXYnvnpHrYZySuBQSMnvr/NgHdqN24umeE6UQQsuFIKTQWkUOfpj6/JhUAewL/VkBReXZpqibd25Sw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f4eAqOAq; 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="f4eAqOAq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66D15C4CED0; Fri, 4 Oct 2024 15:42:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1728056536; bh=RKYOxaZlfRNBRkHBIioYvHfe6spLUu1mZk00Wam8Has=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f4eAqOAq1FX1Ya1okjVZkz82mLxBGix6bkOeHOi5qGpTXizdIdI1HGcng3ZO30+Su 1ibtaobqiZMH2mWJtOhI2E22BuQcm7mIPwwS5ULuccazCkaUDYJw+2hT711xDBX7Ey 2oeYgJ2/Kmu3UizrBDNvRBFrFbYJ2zlcfPxejGlYIRW7zYvSuqi9yZcmfyaXj97ykj 3g0kRigDy/sekuNsh9ECtUeQiQxI41drqNILzFdrd1NaAbk/ATarkl7sM3abOV40da 892mB3SXd74TS2mDLL/LLHLP1h9PKePh79zBPjVLoVaqcxEfPsIOcDe7UlJaWr47Tu 9+LAnKXkQLjWA== 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 02/29] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Fri, 4 Oct 2024 17:41:06 +0200 Message-ID: <20241004154149.93856-3-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" Separate `aligned_size` from `krealloc_aligned`. Subsequent patches implement `Allocator` derivates, such as `Kmalloc`, that require `aligned_size` and replace the original `krealloc_aligned`. Reviewed-by: Alice Ryhl Reviewed-by: Benno Lossin Reviewed-by: Gary Guo Signed-off-by: Danilo Krummrich --- rust/kernel/alloc/allocator.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index 91216b36af69..765f72d5bc21 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -8,6 +8,17 @@ =20 struct KernelAllocator; =20 +/// Returns a proper size to alloc a new object aligned to `new_layout`'s = alignment. +fn aligned_size(new_layout: Layout) -> usize { + // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. + let layout =3D new_layout.pad_to_align(); + + // Note that `layout.size()` (after padding) is guaranteed to be a mul= tiple of `layout.align()` + // which together with the slab guarantees means the `krealloc` will r= eturn a properly aligned + // object (see comments in `kmalloc()` for more information). + layout.size() +} + /// Calls `krealloc` with a proper size to alloc a new object aligned to `= new_layout`'s alignment. /// /// # Safety @@ -15,13 +26,7 @@ /// - `ptr` can be either null or a pointer which has been allocated by th= is allocator. /// - `new_layout` must have a non-zero size. pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, fl= ags: Flags) -> *mut u8 { - // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. - let layout =3D new_layout.pad_to_align(); - - // Note that `layout.size()` (after padding) is guaranteed to be a mul= tiple of `layout.align()` - // which together with the slab guarantees means the `krealloc` will r= eturn a properly aligned - // object (see comments in `kmalloc()` for more information). - let size =3D layout.size(); + let size =3D aligned_size(new_layout); =20 // SAFETY: // - `ptr` is either null or a pointer returned from a previous `k{re}= alloc()` by the --=20 2.46.1