From patchwork Wed Sep 11 22:52:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13801175 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 054ABEE57DC for ; Wed, 11 Sep 2024 22:55:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FB536B0088; Wed, 11 Sep 2024 18:55:15 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8851F6B0089; Wed, 11 Sep 2024 18:55:15 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6FD856B008A; Wed, 11 Sep 2024 18:55:15 -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 4F80A6B0088 for ; Wed, 11 Sep 2024 18:55:15 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 88920160AB3 for ; Wed, 11 Sep 2024 22:55:14 +0000 (UTC) X-FDA: 82553964948.23.F7A562B Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf24.hostedemail.com (Postfix) with ESMTP id 0558A180010 for ; Wed, 11 Sep 2024 22:55:12 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="D/WgSl3S"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf24.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=1726095236; a=rsa-sha256; cv=none; b=VeCf8ArWd9r3DJjErMF1zzQWgGfpDf8VwwE1kRTCDYJ4/iLysxxfN1B0JQrL+iNA1+Dv6s 89OaUgMPbc3JmA3V0eRpsjnQH1Y+L4/2EI6eJhd5rKx1204+OwHedy8CobW4gEK1vhFbx2 2TBfixqKxlTAVgMjvhpXfiINHF7zsVQ= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="D/WgSl3S"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf24.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=1726095236; 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=pmgYwt1xqnnNHM3FNr1fcsHPTRYEOoEUaJzx3PaKkEM=; b=DqL/tGYDUTw4XLx3Sl5LJMWbj6OWuBdDuiYbTA6jzBv5l/Xxpk7pHBHxw8pdpt0OjAlfU8 fS8R36mtifRhmCnjPtW40oya5A4WF/LhI3ANoWu6jEPUZTJ62BT54F+kXm6FUd+WQH9Zcx w5ni4aHjKOE/RWt+FDcn6fYh2XQccuY= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 53737A45533; Wed, 11 Sep 2024 22:55:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5D95C4CECF; Wed, 11 Sep 2024 22:55:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726095311; bh=KguwORfCY7YFKN8ylS2gRnroFHbMzQZYk2Q9X4DUkvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=D/WgSl3S/dVR8lPldARNWqtzBxXX0S7eYokMIzvH+tUsNddrFOKgY8H7hg3IL8v7J 5v3sEjSAvsQtbpDLlySeaWqNttFjmU7ML31CGvfUsC/plF1j/aqfY37ymrpz7dLwr4 m8SN45KADpIY+l8OIBaofroxoa8zToBc0GZpMJXQ/2HCUq50F82aulAVsB08GbMIFM FNi8Phjw2JIHJl0FBTLH8aa4ZAiEFoRbkkVQJyW3tWl7BSKy9ThyabCKX9zCaNhquT vId+Sd/uYzs29X0EN8pG29Xhh5iduiyOOXP7EIzE8u8Tqx/zcgaocb/94BA3Zze16h aGi5EIDGU9qFQ== 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 v7 02/26] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Thu, 12 Sep 2024 00:52:38 +0200 Message-ID: <20240911225449.152928-3-dakr@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240911225449.152928-1-dakr@kernel.org> References: <20240911225449.152928-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0558A180010 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: u76aj9e9rw6sdfrmtdym14s58edx1o78 X-HE-Tag: 1726095312-529505 X-HE-Meta: U2FsdGVkX1/WmhHzu56h+pGXvRKyVHKoV5UwH5Zzr/FAPwZ6h5p1kwurG2kcdCeVxZqY8Z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s2CsISbt/dwSjGTW4cA/dHbyEBRpI/7HQHZ6hxgCjVjmTsvOnEyUCVN7TL6xKGIeaiWzPO/BMjyFm8/8E7nAfUuHuS1MbFcaBSPkSrkzhC1+CqcmJS4GwKJc9WZaA2fYthudK40X6KrhzzAlPcmeOkMEA1zXYD0PbIlaYg9opJeQAeCBNCLkjYcMyQ17ZsMhfCPMj4naRK44T50bg/X0+Ug8/G6sV3SkCloE5U9gDuP8l2T7sZjZ0ZK+Fp1SNP3yCm2fIDvYgcngA== 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