From patchwork Mon Aug 12 18:22:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13760919 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 EF3ABC3DA7F for ; Mon, 12 Aug 2024 18:24:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F8726B00A5; Mon, 12 Aug 2024 14:24:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A8146B00A6; Mon, 12 Aug 2024 14:24:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6232B6B00A7; Mon, 12 Aug 2024 14:24:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 3FE0B6B00A5 for ; Mon, 12 Aug 2024 14:24:25 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id EDC271A0351 for ; Mon, 12 Aug 2024 18:24:24 +0000 (UTC) X-FDA: 82444418448.05.40AAC68 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf08.hostedemail.com (Postfix) with ESMTP id AB990160008 for ; Mon, 12 Aug 2024 18:24:22 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=c0I8hd5A; spf=pass (imf08.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=1723487008; 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=xjsKxaqIZJM0tVNeKe9Muttd7ZI8h4gkDNAWBc8bk0w=; b=RLW58nPNktFZacTB1sHLXUdtY7IR5gzkVIvGfiSy6T5m3djaBJHptLGZFLSqKjHuLD70CA rHeITMsQOJLTBpArrxWGhUTBgWNLeYYgVrK6Yse9id8eIB0TAI+jkHtMjgxwtIJlTH0ysz /ib/VAKcQMoKyn38QY20NdtOQJ73kpU= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=c0I8hd5A; spf=pass (imf08.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723487008; a=rsa-sha256; cv=none; b=tKv8yG1koLIWoZ8vEecl7fhWg3oCVBRkBcCNoRWx1M1bfNmAhRB2A1Nr9oDlUv+gNhDCk+ M5awFBW2oaPa9gAnI28eo0TXh6pWvVogLXC3LU4AJAkfEMTP6uWuDPjt8VJ/tjsaRAaAlP hR63KbXPYbReqM6oTc46XDD9S35B55s= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id B80F9CE102E; Mon, 12 Aug 2024 18:24:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F08F9C4AF0D; Mon, 12 Aug 2024 18:24:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723487058; bh=p++mQr6O15JhVUHtMmsU1D7fc1KQnWeRmH3GDyn4naw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c0I8hd5AqEtUJ1AqcltnJQJXgDifadIagb8OT4ht7RND3/LIpX13gvQjzt3gLQowI ftjf2eUBi+kRnz1lX7eL9vGAeiEQ8VSt/93xhlmq3bo51SkTTtLhVlA1JeSJlhL1vO kKy1fu1ZpAWRTSx0A6dHnPwjSFST7YO372xKlpVClxYIjCcUPHvkSj+fQ1ApyURzYL iEsA/zOqK25c8hP18hXK60sIIPLcYcpYrmTHnk0vQhpz7wJPKOG5cBX2AXOvB7Pt59 OuPvw6w4Pra2zExwy8Z5Xaqski4YFdaIJkkvFVea5Z2LMfKRfUtJYYqefeQFClV0Vt gsbOh1+Jy/6+Q== 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 v5 02/26] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Mon, 12 Aug 2024 20:22:48 +0200 Message-ID: <20240812182355.11641-3-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240812182355.11641-1-dakr@kernel.org> References: <20240812182355.11641-1-dakr@kernel.org> MIME-Version: 1.0 X-Stat-Signature: gigp4de3rr8yxtkhikh5mpbxhxu9pn5e X-Rspam-User: X-Rspamd-Queue-Id: AB990160008 X-Rspamd-Server: rspam02 X-HE-Tag: 1723487062-813833 X-HE-Meta: U2FsdGVkX18Cz24RM3jpWfX6eewh7LJ4Ihp5/BqKBKXiG8HlROGYlWOPub8GZYHVxN9VJlkSMkP9Asj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QYsu9lPOEfqhuLu2hLwxBSFYFPEAoqdK/t2GLnngXfw1vfmnvgwuZ1Kyw9Bx/j0SXdHLZgQwHnlFxXNKSnRkbt0b+Zq+7OrQIE7hI7surnaOSOXzn/vc8rH16BxFMsGPGdzel5PLFUiJLZV6GolAxNH8W8/smznS/vj6tT3laIJkX0wTUYj1FMl2f20TXDAkREqhQ+u1milgVnhUHM5UzPq+MVQ== 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 --- 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