From patchwork Tue Oct 1 14:59:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13818221 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 48E51CEACDE for ; Tue, 1 Oct 2024 15:00:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D35C4680006; Tue, 1 Oct 2024 11:00:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D1658280068; Tue, 1 Oct 2024 11:00:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BAFAE680006; Tue, 1 Oct 2024 11:00:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 99C60280068 for ; Tue, 1 Oct 2024 11:00:30 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 49F97815D1 for ; Tue, 1 Oct 2024 15:00:30 +0000 (UTC) X-FDA: 82625344620.24.20E9C6A Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf16.hostedemail.com (Postfix) with ESMTP id 8E50D180039 for ; Tue, 1 Oct 2024 15:00:28 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BGo62r01; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=dakr@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727794789; a=rsa-sha256; cv=none; b=Unwdtp8q5Lu1K8TFRtY05XcogDaLxeR5YEgyMpTShT23lI/ck3paNKdArvCLIK8ksZS8qU xMdpqPoqCR9Ft5VwLOFsIoyZ/hT+YUU18F8XFFafgEmjGSgdHgk2eSF1pnbmlZ23RqbDCL GlsATvIo607sICX4wUA1PGyYDzp4viM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=BGo62r01; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of dakr@kernel.org designates 147.75.193.91 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=1727794789; 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=uOHEks+bEcqRvCkbiLubZ6JHXOyJKoVQ/J784rpHDbk=; b=FRGCcdCER3d40/PtqAgnFFyHv3KSyKkt42NTd2+kOLoGe+IydSs+bQ91LGsNLeQka2f/tw ZMC8i3B2o7aG0L5Q1IAx0VfAyh27BoQVPtJBbaBXWZPl8ad6FiS/qaWHt4BloPsXppICaf HMu3a0dBCujS7WOTDYFL/hh2FiLyCms= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 78D64A41B68; Tue, 1 Oct 2024 15:00:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88260C4CECF; Tue, 1 Oct 2024 15:00:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1727794827; bh=jkwVy/FbAq0lpqNfRj/9CuxBTqWwT6EE++N0UM3hdBg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BGo62r01HZ4eL65H3mCyFpgGOTQKvCWr2+bW2JgJ1Ko1mMbKZlA6duZ2ud2iVEtpT KQ88lS2emAf0fKqC2+u4I6M43AEqks4SCWnLzlBrL5mT9m/Y/oO8bsz/MoBt+bQEcS EMRYn/Vpns3NG8LnnIhww9TPSL00Q3jwY4NY1jogyeFSs8GfWp9qiyqrnY1Frp4cGK B9fSPlq066mUidESfW6zuiumnHN8q9tIdADqjrj167+nDyhNLvOr3KISdUBHFa00gw jc/Sp+9Mlyc7xwtZwaMWE1DUFxev08Lz/pZYwgLLfKvcYzGEiW0npAYhoX662y1GUh lWrI0YugSlX7g== 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 v8 02/29] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Tue, 1 Oct 2024 16:59:37 +0200 Message-ID: <20241001150008.183102-3-dakr@kernel.org> X-Mailer: git-send-email 2.46.1 In-Reply-To: <20241001150008.183102-1-dakr@kernel.org> References: <20241001150008.183102-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 8E50D180039 X-Rspamd-Server: rspam01 X-Stat-Signature: oh8x9abexktugc553syw1b4prioecd7h X-HE-Tag: 1727794828-799537 X-HE-Meta: U2FsdGVkX19XUqa4Bz+NE6ZsPd0zMqoIZrTyF2d7ImQJTL6heHXaIMuwswwHPrhUqi4iKhk+t4TzKDoSXsv3OX+4KXSaAJwiqUZFd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4/k0Bs0G4LryXvZJa/XQSMJlGEqqkoS78I1w8IJdPsSgtY83iY1cFhswrwH92I+NV4QwTbt5hhTy9ZM8Pj7RdRm3YcXubZoD9i3I6WPODqxi0KAUWyjzoIOjqVJ9dGxv+pWbvNuJvoKoK8ZgKEmMF1x0SgjV4P9XQqWCBeWKYwmgfCESk5bE4uOgZp88gFHJ0PUhN7qmtOoO63chVlvCnxdCOlQx8YmP1sEgQjLirhqFFUtsQ== 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 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 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