From patchwork Fri Aug 16 00:10: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: 13765283 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 96AB9C52D7D for ; Fri, 16 Aug 2024 00:12:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2388B6B027E; Thu, 15 Aug 2024 20:12:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E8E76B027F; Thu, 15 Aug 2024 20:12:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0897B6B0280; Thu, 15 Aug 2024 20:12:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id DBB936B027E for ; Thu, 15 Aug 2024 20:12:38 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 56BA84193E for ; Fri, 16 Aug 2024 00:12:38 +0000 (UTC) X-FDA: 82456182396.16.B8C3D49 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf10.hostedemail.com (Postfix) with ESMTP id B1F27C0016 for ; Fri, 16 Aug 2024 00:12:36 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GAnPVWFa; spf=pass (imf10.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723767143; 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=w6+8tdY4TLtLsqtHG92YkQOgq074S9VZCBc3fF0+vIk=; b=pTt+WFtJC2GRSycL49Qt3kA69rGxmYBOnUUJovWDmZE83CCAejF+MAjAmr9QimielAcBSj ZqCaO1EKYCpuxXOUe1Xam6DznuexylsBWi9dOOnlOwx/qCRAys9r0EBr7hXUsKObCBz3GS cnC9KeDkweCiQl5YyVHEhOT+4abew3E= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=GAnPVWFa; spf=pass (imf10.hostedemail.com: domain of dakr@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723767143; a=rsa-sha256; cv=none; b=laQ8ifkzSaSPRWCfJJ+SNkA9QUdZi5qJFkdF+nXYNb29f4cXJcReP6FI4eeZrv+rfmsm99 iHSnDO2xVUdER16tsEnKUaPnyqUAfbCEnUhj5Oi6Dg2YL8Hx5Rm+buRz85VtSJ/+RmBmoc LaMixJ7IasLCSC+ZEG2lRBDBUZG4/0A= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id DF53162065; Fri, 16 Aug 2024 00:12:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7B7FC4AF0C; Fri, 16 Aug 2024 00:12:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723767155; bh=7ZQlD+OTbqqlfuCqgP0DzO8fypflRQev63uVtG65Y8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GAnPVWFalHNdYwWYd3lbE6b3W6p6ZwKVuxqnVgvpj9tEBAPTdFpARBsEqVS554dCY x0NhR+80UCX503tsN/Zl4/5G8uk/MKNgqcFT4kWeEu9VLnfV06EaQhZKUAjwt3YzO8 vzu91NxYDN+XFsqD5RCW5KkUiqljpYr1hJ4u1Gw6lvWT7yR21vtHY503Aipp8Yn2bJ e6eagH4QRRJwFA8Rj2WOeKezAP0hVOu5hguQmpfKLWUaEpZcSo/xOSJO8NsfsICceo DP86xC/KOTAyZM1b6xPeL3iy5L8iqk0m5Fi74PkNr01jms09tq4gemNVH3g3Wj5BhF b+Pk0RFEyXfBg== 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 v6 02/26] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Fri, 16 Aug 2024 02:10:44 +0200 Message-ID: <20240816001216.26575-3-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240816001216.26575-1-dakr@kernel.org> References: <20240816001216.26575-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: xe87npreiqs3brrt4kzxg7s165tcfdck X-Rspamd-Queue-Id: B1F27C0016 X-Rspamd-Server: rspam11 X-HE-Tag: 1723767156-387557 X-HE-Meta: U2FsdGVkX1+mLiPSYl3OjDXSX0MdezPmhDLvnuszPzJj6y36dhwTHCFvE0zJ1Dn6EuIURkfJc+6ecly4LuH8e8lVJ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p1GyoiktYs+RmnxqgFZ/Z8sItJ+R4+qP4s7LYPWQ7V7+J/W48YFADYhRkRL5z2pIi1EEmGk95lgWpghqyW33lGuqLGCm5QHysYPxD34QBi5i7VNPCnlDY4tj8U80XV0mBdJV68Epqk/UGeBbj7YJtaOPBQgg39aqNadYpkhm+pLsc1rWtnxkkTqMvSgQKwEUjhmCZtXBn 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: 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 Signed-off-by: Danilo Krummrich Reviewed-by: Gary Guo --- 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 e6ea601f38c6..c83b6dff896d 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -8,6 +8,17 @@ struct KernelAllocator; +/// 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 = new_layout.pad_to_align(); + + // Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()` + // which together with the slab guarantees means the `krealloc` will return 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 this allocator. /// - `new_layout` must have a non-zero size. pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 { - // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. - let layout = new_layout.pad_to_align(); - - // Note that `layout.size()` (after padding) is guaranteed to be a multiple of `layout.align()` - // which together with the slab guarantees means the `krealloc` will return a properly aligned - // object (see comments in `kmalloc()` for more information). - let size = layout.size(); + let size = aligned_size(new_layout); // SAFETY: // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the