From nobody Thu Nov 28 14:54:25 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 D9DAB84047; Tue, 1 Oct 2024 15:00:27 +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=1727794827; cv=none; b=UOL1KpndvZpgrFAICW+HG29nPsIczg/sRkD/xrFqfcntZ+CwLIJ1ms5S4HgVwho4cT43OVv4obs2ClBV9fDxni5ayqeY44v2yBjC4r7OwElYg6Q4PUO0tiYBsieegopyJIcfgNpUNZjkGibHcLzDS2zLS9K5AibWgC8lrD9YPCE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727794827; c=relaxed/simple; bh=jkwVy/FbAq0lpqNfRj/9CuxBTqWwT6EE++N0UM3hdBg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=boCr0uOTO3S3UbJ8c3A/4SmlZBkbjmvgHiDMae+lq5QBrTC3f+nr3tudrWBpffYiLKibeGxVqBNVEe340g/Iq1g0NAlQHzU1Xvwu1+WNAhtTEEQdmhoLSdu7W+e8lHN72WJy/CEN3GcgVudvGcJg83S9Ta0yTwbmrZlPafzL23o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BGo62r01; 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="BGo62r01" 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> 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 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 @@ =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.1