From patchwork Thu Aug 1 00:02:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Danilo Krummrich X-Patchwork-Id: 13749341 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 488BDC3DA64 for ; Thu, 1 Aug 2024 00:07:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 023266B008A; Wed, 31 Jul 2024 20:07:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EEDF76B008C; Wed, 31 Jul 2024 20:07:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CF20F6B0092; Wed, 31 Jul 2024 20:07:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id A980F6B008A for ; Wed, 31 Jul 2024 20:07:09 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 67DE31C2DC8 for ; Thu, 1 Aug 2024 00:07:09 +0000 (UTC) X-FDA: 82401736578.29.799A78B Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id 373B9180011 for ; Thu, 1 Aug 2024 00:07:06 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=caZue2SC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 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=1722470765; 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=Q+939kC/O+kSEIr8mFyDMfc8xtGxKWUWzEi/7lcM42c=; b=W7NcAbH2OFwoxMDP3x/331iWDxxa6zTJ2gctOpJu3yiZ2xfM7NPTN6PqjbgrP9iOWUpiPz oLVEhuFBZNgRYQLxNxyVxQMyuxA5q7HfKk9qLnb8webHq4euhwd+/XpTdkwrV7aRDHG5VS VeD5jFcxALa3Y5+TAWaMCu4eEyG5njQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722470765; a=rsa-sha256; cv=none; b=hYc5golflIsyeAxEKaCET62Q9Xk8M33rskZe+OelswH4JY4WnwuAADSKMj53FG5FuAGf/L EDD8UiVTmKBPiGCyw8K01NNhyYyp7gnayp6Wn6RwxthyX1KXpdrW55sYuQXQm5fgkWwMEG 4o9hGkNo7xeQdi11YNRWyYkGVk9a8oY= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=caZue2SC; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf06.hostedemail.com: domain of dakr@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=dakr@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 02871CE182B; Thu, 1 Aug 2024 00:07:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 492AAC32786; Thu, 1 Aug 2024 00:06:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722470823; bh=EF5TVGHtjh4ZWyprrgf/Kmvg5Hex1RNnX1bjJzrWn+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=caZue2SCBQ/qK9rQ+EruSBuRQ8G1EK1zbXMqxoKz6hYHoeDLfVV3Gxkt91OXG61Jc jboqn22gfBqYo8AszpXaPezc0qwmvpK59IAAnLbnK6j7bfBlMWU3ZESLJvEMvHoSsV 90go6u9n/yUL1GwZdoiiSaeWK1rQer+UGHh0Jpy7eHRVTcLnOFrVgHogxeDuZ4WL/c 61FPwrDO2wlP4l87Exc3c4iwlqLRe37EFWUI8qmB+poi8TY0THXzlmbvDQ4hliWjjX HtyGuALr4g8rvlLIDZ4FPHixvzc8lLtbwt9NMN7Sel4SVPjU4LDA5ljn57FjrJnAoW KEZMZ4RqRCpBA== 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, acurrid@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 v3 02/25] rust: alloc: separate `aligned_size` from `krealloc_aligned` Date: Thu, 1 Aug 2024 02:02:01 +0200 Message-ID: <20240801000641.1882-3-dakr@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801000641.1882-1-dakr@kernel.org> References: <20240801000641.1882-1-dakr@kernel.org> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 373B9180011 X-Stat-Signature: 7tg14su67r31wp316jghcr634ybgn7aq X-Rspam-User: X-HE-Tag: 1722470826-749614 X-HE-Meta: U2FsdGVkX19gHV+U/3o04QNh1n3jYRoW1PMKWRyynihayckVTA8GtHL44RK0dIZHHpQympgqYAEfvcdc/cI3yQlbgb/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euzxAxO+uLS/8mPe4P3PdxN4DEYOctn5uyanz7SzrOqcR3lYgggzhCer7ZupWBxvgqxh9iei5SzNQgn1+7Bpvna15H2kOSkvaDxN6PjL7lt+nSjLIulLPFZbLLGeZVXOofhMqp5ujROL+jxwNd8+E/MF0+ijppC+eKSfF8Tq0UuyTDjXH1GAs1wrTwU= 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`. Signed-off-by: Danilo Krummrich Reviewed-by: Alice Ryhl --- rust/kernel/alloc/allocator.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs index e6ea601f38c6..6e133780a4a1 100644 --- a/rust/kernel/alloc/allocator.rs +++ b/rust/kernel/alloc/allocator.rs @@ -8,27 +8,35 @@ struct KernelAllocator; -/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. -/// -/// # Safety -/// -/// - `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 { +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). - let size = layout.size(); + layout.size() +} +/// Calls `krealloc` with a proper size to alloc a new object aligned to `new_layout`'s alignment. +/// +/// # Safety +/// +/// - `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 { // SAFETY: // - `ptr` is either null or a pointer returned from a previous `k{re}alloc()` by the // function safety requirement. // - `size` is greater than 0 since it's from `layout.size()` (which cannot be zero according // to the function safety requirement) - unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags.0) as *mut u8 } + unsafe { + bindings::krealloc( + ptr as *const core::ffi::c_void, + aligned_size(new_layout), + flags.0, + ) as *mut u8 + } } unsafe impl GlobalAlloc for KernelAllocator {