From nobody Thu Sep 19 16:02:34 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05713C8E0; Fri, 16 Aug 2024 00:12:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723767156; cv=none; b=YukPgTxUQ66tLj1HXhaKwHUQB5MvazRD4Jx8h6rUG4GvNTZogZiXHmRmDf/gt2X5zjOOGFjxRihA1y+CHvkin6pmRNPZG6NtwjMoPnPeik6gVn1Me9pA3qulyn1hFS+5+mWkcjLcScgAIH4h5AGFIT0HAsYjNcLkNCtJEjxmlno= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723767156; c=relaxed/simple; bh=7ZQlD+OTbqqlfuCqgP0DzO8fypflRQev63uVtG65Y8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Y+p73N7IT8M3+zQRRE3o5fqtO6ZCjvNi1iUIlZz2+e+Z6P3R7oGF7wBQG1BSWkcFQvtMQ7oA96jMxeAv2OWHdZclFQvXHoZg1Yf0V9MU4brZXQ5/k1jrz4BdIXOlfsyXMPNIem9cjJ32WzZUXlOqmmUUOK7taRiKhx8H8KMMwCI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GAnPVWFa; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="GAnPVWFa" 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 @@ =20 struct KernelAllocator; =20 +/// 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 =3D new_layout.pad_to_align(); + + // Note that `layout.size()` (after padding) is guaranteed to be a mul= tiple of `layout.align()` + // which together with the slab guarantees means the `krealloc` will r= eturn 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 th= is allocator. /// - `new_layout` must have a non-zero size. pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, fl= ags: Flags) -> *mut u8 { - // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. - let layout =3D new_layout.pad_to_align(); - - // Note that `layout.size()` (after padding) is guaranteed to be a mul= tiple of `layout.align()` - // which together with the slab guarantees means the `krealloc` will r= eturn a properly aligned - // object (see comments in `kmalloc()` for more information). - let size =3D layout.size(); + let size =3D aligned_size(new_layout); =20 // SAFETY: // - `ptr` is either null or a pointer returned from a previous `k{re}= alloc()` by the --=20 2.46.0