From patchwork Mon Aug 5 15:19:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13753711 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 B8C1EC3DA7F for ; Mon, 5 Aug 2024 15:20:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3809A6B0089; Mon, 5 Aug 2024 11:20:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32FE46B008A; Mon, 5 Aug 2024 11:20:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D12F6B008C; Mon, 5 Aug 2024 11:20:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id F3A706B0089 for ; Mon, 5 Aug 2024 11:20:30 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A62BBC03E3 for ; Mon, 5 Aug 2024 15:20:30 +0000 (UTC) X-FDA: 82418553420.28.E303084 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf17.hostedemail.com (Postfix) with ESMTP id 5DE4B40030 for ; Mon, 5 Aug 2024 15:20:27 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=X77UjPV2; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722871167; 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=Q12BLmKhrNaYMBS6KUGNvGkpze+WrX8idHU2MXPizUU=; b=Zil4j5Q3HdM4vDuNzfboKlacrGWYS016dZlbcdkmBLEHGxNTzqI7Aw7tkqzCaRIfzdYgcm +9o3ITUfqHCqTnrj+J6kuNYzsX7LevstCo2gQWnwBzF8Pv1tl2IIGZeKDZl5WsPPHFlkqw vieY0VdfpI2XHK7h8WBMrrNmk9yx6TM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722871167; a=rsa-sha256; cv=none; b=vMQpw1gKUTZLFYnuCxTJLcNpjssGhVOniKAx0ZCx3UKes7CB4xLqjDjojIES1u9lWbtWvg Aq3gZAZsGX/HmHlpD2yVRQZOh+Iql6t7woAotl3iU4Uo3Qky+NqvWNpEJdZQHGv69wTJke FgxyyjnCG8AeJsmhbfvOZnCcu0mLl7w= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=X77UjPV2; spf=pass (imf17.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 2E15ECE0B2D; Mon, 5 Aug 2024 15:20:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53277C32782; Mon, 5 Aug 2024 15:20:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722871224; bh=PKEwZd3yHqQQqT8mEHKKvphM2Pe67I1NR1OPtC0T0To=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X77UjPV25pEFDWaPq84AeyqAqV6uAdZksZPYQUqppUCz33XeJi1l7MK10KLbf0tbV 1gzJyR/gqXeboD6/+PfujdxjK/K4YorMQKV2i+6CtJjJTkdm31OPPP4WGDsy4wM/wE iCsc/dfVvk0YM4EQNp02su+KeYPCYvQp/Qz0O3UCPWA81wABOZR3++cXO71S3jytsL 3j2+CK5sJAdYdHLX5ornikWobgm9ufjXlj46zb+RBB1jQm+QeqMSgCABFndl+5xVe8 aMqMS0NjQ8nrvu61TGZ05I78vnCyrNNzHV9terWU4iL/PgCi/NJd09luI4ht5nHBGL uSXgPuiwLcsJA== 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 02/28] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Mon, 5 Aug 2024 17:19:21 +0200 Message-ID: <20240805152004.5039-3-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5DE4B40030 X-Stat-Signature: s8xs4zr5poeqr1dpmd9hfgprjbi1n3xt X-HE-Tag: 1722871227-248470 X-HE-Meta: U2FsdGVkX1/Pb8u0M+PbAxo2KLWE5nGWMJ2d93A2rpIYa+rXXb2Z3yE9HsIVSESy6soFvw4IjCZi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UmqLGMDAonc96TJfCXagYNBen9e4t8GFERkQdmdbc0kWtoRkcwYEovUV48rUiAaAIxrMB8HJo8Ey1QAlp4Jd9zcyn15pFXJmztTSvMmbsLxDVkSxV6Lbg58F7cD0CucIkigRNR3ORaA4a+dpa36whzS5KZVusQyIGlJZwunXrJKq4H8Mylpy+8hq+V8aBufMiXlC5d35IzvSMDgATp 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 Signed-off-by: Danilo Krummrich Reviewed-by: Benno Lossin --- rust/kernel/alloc/allocator.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index e6ea601f38c6..e7b7eba84acb 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -8,27 +8,36 @@ struct KernelAllocator; -/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. -/// -/// # Safety -/// -/// - `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 { +/// 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). - let size = layout.size(); + layout.size() +} +/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. +/// +/// # Safety +/// +/// - `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 { // SAFETY: // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the // function safety requirement. // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according // to the function safety requirement) - unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 } + unsafe { + bindings::krealloc( + ptr as *const core::ffi::c_void, + aligned_size(new_layout), + flags.0, + ) as *mut u8 + } } unsafe impl GlobalAlloc for KernelAllocator {