From nobody Sun Feb 8 08:27:59 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16843C41513 for ; Sun, 30 Jul 2023 01:30:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229637AbjG3B35 (ORCPT ); Sat, 29 Jul 2023 21:29:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54542 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229530AbjG3B3y (ORCPT ); Sat, 29 Jul 2023 21:29:54 -0400 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [IPv6:2a00:1450:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C69F2D47; Sat, 29 Jul 2023 18:29:53 -0700 (PDT) Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-9922d6f003cso529230466b.0; Sat, 29 Jul 2023 18:29:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690680592; x=1691285392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=9OgITT2yiqZUUNG1U4xDDumpCjudK5Ub6jXVaWbbuKI=; b=lZMgU3gjYlD/diNHnqBRZUPy+ReTapzTL8Hv9c4ilG8HeXJNSg2rFnNGnV3ZzAGg7w IL8+AAVY4nLIBHOo+upAu+tEy1C1iCWvP8NhXcjU9iY/k+l1v5XEyXY2U+bBD9Bk0gkS tHIjW2ktEvoAN7ly0mxkm4JLak3mt6oZKjYDyy2hxzfzhn+SunbC9syqdmYOD4kAIQ30 rq+4qDFtBRZryd194tNihVvhhQUg6CIN+6X5AMaLfovokf5ZtROyHVa7sTZe8/9PvEY8 gBWTjKrD1Eo9nqbpMzlZFiqzeTUHGVOL+EWw/+cLY0/v395htQ1L5LajaG3ZwHFskWl8 ettw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690680592; x=1691285392; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9OgITT2yiqZUUNG1U4xDDumpCjudK5Ub6jXVaWbbuKI=; b=GhFGqYaJ2uCgtwYpUHRC8oBGw1MXSS1AWtpF6uKgCCW2UHxuI4HjEJ04hsuZWCGbyz Xm76tudXy650ivnAU9hHgXTluJLSWCgoHi+573NtxuYr5cfQUfVoB6594W0sq1vk4kS7 B0DIPpUOp+4i5HNQ9vcsIloRgzSWLXZM984BxeSdh7NR2IEKMe8SOQ7W66wH8f3JIjYZ sWPqIVd9Og9Yc6Q04bd8RlRFcbF9IMHHsBX0hB2wLGdonkVQa/sabkXKL7QkTjExSQ6W UWH2/UCO02Mm+Y1vQOndUbwuGyt3wjApRFri8lkna9GkRrp+5O7NH43gsSMnpik5O0tY Y1BQ== X-Gm-Message-State: ABy/qLYvP9TtIymqmfNBlU82MWVIzxoXDXMFlkHucFS3c7DT/m8JwsUc 398jN7QQGuJ4XgFSRsSQJgg= X-Google-Smtp-Source: APBJJlFTQMGWBtK8SuCFhD9DwEnEvBYfJNamT3jZ0CGl1sRmkV+QglVHxqdJsDKKHLYXGG7Mx8AM1g== X-Received: by 2002:a17:906:8a5c:b0:991:37d2:c9f0 with SMTP id gx28-20020a1709068a5c00b0099137d2c9f0mr3658931ejc.68.1690680591709; Sat, 29 Jul 2023 18:29:51 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id ks18-20020a170906f85200b00977ca5de275sm4046161ejb.13.2023.07.29.18.29.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 18:29:51 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id 372F827C005B; Sat, 29 Jul 2023 21:29:48 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sat, 29 Jul 2023 21:29:48 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrieelgdegiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeuheeg gefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsg hoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieeg qddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigi hmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 29 Jul 2023 21:29:47 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Martin Rodriguez Reboredo , Alice Ryhl , Dariusz Sosnowski , Geoffrey Thomas , Fox Chen , John Baublitz , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Kees Cook , stable@vger.kernel.org, Andreas Hindborg Subject: [PATCH 1/3] rust: allocator: Prevent mis-aligned allocation Date: Sat, 29 Jul 2023 18:29:02 -0700 Message-ID: <20230730012905.643822-2-boqun.feng@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230730012905.643822-1-boqun.feng@gmail.com> References: <20230730012905.643822-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently the rust allocator simply passes the size of the type Layout to krealloc(), and in theory the alignment requirement from the type Layout may be larger than the guarantee provided by SLAB, which means the allocated object is mis-aligned. Fix this by adjusting the allocation size to the nearest power of two, which SLAB always guarantees a size-aligned allocation. And because Rust guarantees that the original size must be a multiple of alignment and the alignment must be a power of two, then the alignment requirement is satisfied. Suggested-by: Vlastimil Babka Co-developed-by: Andreas Hindborg (Samsung) Signed-off-by: Andreas Hindborg (Samsung) Signed-off-by: Boqun Feng Cc: stable@vger.kernel.org # v6.1+ Acked-by: Vlastimil Babka Reviewed-bys, appreciate anyone to take a look, thanks! --- rust/bindings/bindings_helper.h | 1 + rust/kernel/allocator.rs | 74 ++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helpe= r.h index 3e601ce2548d..058954961bfc 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -13,5 +13,6 @@ #include =20 /* `bindgen` gets confused at certain things. */ +const size_t BINDINGS_ARCH_SLAB_MINALIGN =3D ARCH_SLAB_MINALIGN; const gfp_t BINDINGS_GFP_KERNEL =3D GFP_KERNEL; const gfp_t BINDINGS___GFP_ZERO =3D __GFP_ZERO; diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs index 397a3dd57a9b..fae11d1fdba7 100644 --- a/rust/kernel/allocator.rs +++ b/rust/kernel/allocator.rs @@ -9,6 +9,36 @@ =20 struct KernelAllocator; =20 +/// 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 th= is allocator. +/// - `new_layout` must have a non-zero size. +unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindin= gs::gfp_t) -> *mut u8 { + // Customized layouts from `Layout::from_size_align()` can have size <= align, so pad first. + let layout =3D new_layout.pad_to_align(); + + let mut size =3D layout.size(); + + if layout.align() > bindings::BINDINGS_ARCH_SLAB_MINALIGN { + // The alignment requirement exceeds the slab guarantee, thus try = to enlarge the size + // to use the "power-of-two" size/alignment guarantee (see comment= s in `kmalloc()` for + // more information). + // + // Note that `layout.size()` (after padding) is guaranteed to be a= multiple of + // `layout.align()`, so `next_power_of_two` gives enough alignment= guarantee. + size =3D size.next_power_of_two(); + } + + // 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 either a `layout.size()` (whi= ch cannot be zero + // according to the function safety requirement) or a result from `= next_power_of_two()`. + unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, fla= gs) as *mut u8 } +} + unsafe impl GlobalAlloc for KernelAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { // `krealloc()` is used instead of `kmalloc()` because the latter = is @@ -30,10 +60,20 @@ unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout)= { // to extract the object file that has them from the archive. For the mome= nt, // let's generate them ourselves instead. // +// Note: Although these are *safe* functions, but they are only generated = at +// `GlobalAlloc` callsites, hence we assume the parameters obey the same +// `GlobalAlloc` function safety requirements: size and align should form a +// valid layout, and size is greater than 0. +// // Note that `#[no_mangle]` implies exported too, nowadays. #[no_mangle] -fn __rust_alloc(size: usize, _align: usize) -> *mut u8 { - unsafe { bindings::krealloc(core::ptr::null(), size, bindings::GFP_KER= NEL) as *mut u8 } +fn __rust_alloc(size: usize, align: usize) -> *mut u8 { + // SAFETY: See assumption above. + let layout =3D unsafe { Layout::from_size_align_unchecked(size, align)= }; + + // SAFETY: `ptr::null_mut()` is null, per assumption above the size of= `layout` is greater + // than 0. + unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNE= L) } } =20 #[no_mangle] @@ -42,23 +82,27 @@ fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: u= size) { } =20 #[no_mangle] -fn __rust_realloc(ptr: *mut u8, _old_size: usize, _align: usize, new_size:= usize) -> *mut u8 { - unsafe { - bindings::krealloc( - ptr as *const core::ffi::c_void, - new_size, - bindings::GFP_KERNEL, - ) as *mut u8 - } +fn __rust_realloc(ptr: *mut u8, _old_size: usize, align: usize, new_size: = usize) -> *mut u8 { + // SAFETY: See assumption above. + let new_layout =3D unsafe { Layout::from_size_align_unchecked(new_size= , align) }; + + // SAFETY: Per assumption above, `ptr` is allocated by `__rust_*` befo= re, and the size of + // `new_layout` is greater than 0. + unsafe { krealloc_aligned(ptr, new_layout, bindings::GFP_KERNEL) } } =20 #[no_mangle] -fn __rust_alloc_zeroed(size: usize, _align: usize) -> *mut u8 { +fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 { + // SAFETY: See assumption above. + let layout =3D unsafe { Layout::from_size_align_unchecked(size, align)= }; + + // SAFETY: `ptr::null_mut()` is null, per assumption above the size of= `layout` is greater + // than 0. unsafe { - bindings::krealloc( - core::ptr::null(), - size, + krealloc_aligned( + ptr::null_mut(), + layout, bindings::GFP_KERNEL | bindings::__GFP_ZERO, - ) as *mut u8 + ) } } --=20 2.41.0 From nobody Sun Feb 8 08:27:59 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E981AC001DC for ; Sun, 30 Jul 2023 01:30:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229651AbjG3B37 (ORCPT ); Sat, 29 Jul 2023 21:29:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54550 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229577AbjG3B34 (ORCPT ); Sat, 29 Jul 2023 21:29:56 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C3162D42; Sat, 29 Jul 2023 18:29:55 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id a640c23a62f3a-99c0cb7285fso23510466b.0; Sat, 29 Jul 2023 18:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690680593; x=1691285393; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=PhtA3S/JDjazeAwjyLjhNAHRYNrsVmh0Eu0g8T30AIE=; b=GxodfzLoeHEML6pPfMb2i5ubNXaelxwAO2W34igO8jTtDBLWAt2ufATykcytjJB8XK vRDO87nLa5BC6iBaAXZRhxA1WdUZnyXa6Oka3eCRskFpe0sphP2EKno+HJbCKkry8KOy /e7Kp1UbvgRvyH3FITUQ897aimqik+1v8IU1zDld5RkiJkubHET2EMfJ3SVYKX9+yDrW uci5ip+cKB0n4I4oupu75FnnBpkAAnFeRtrcpnzq8QYdWnzCksKBWKPhwd5vBad4Dkjw /XZJ4UfgI2saPGQ26eXmXctum+25eDOYCjhFIfpibT9wOEhjqBpY8pdiT0Lr+uNJjt+w p0jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690680593; x=1691285393; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=PhtA3S/JDjazeAwjyLjhNAHRYNrsVmh0Eu0g8T30AIE=; b=H4xrd1bM20/zSK6tgcu29WAB6swX21hPDrD0Ec3esSOkeNkg9ZSCo6OgN9MKq01fg2 RznhtbxAORJCYkpMyAAcMRKt6cKcMp3e3Hp0xA01V3B92VRrtlYlbE0cO4mDuuZ9UEsN 9uNOFPQgMQ2fZFOXmZ7c8nFwhdCJ8hzYLm4EGohkmeOv1Ild2JGzV5wjNb4Efjr2mB6i rO5g1hgyNGUbvL/f5C3z4eXUR/BCoBo0vRB7dDj+W83nLj4NimtZJrLqwRLzN/d3eag7 VVf9FV0jZCRGlhMHMfcjksftS2fuZGWgxqpRshlkwroaCw8uOOi+UcGzMPkVkRQoe86Z N8Mg== X-Gm-Message-State: ABy/qLb4gPURkEpyTUTawhG20oEBkz572m0XwiAGjxi4stAWCUfQxv28 HKjRVdPkHdpf1ahcwArJyfY= X-Google-Smtp-Source: APBJJlEWZ4fEpFiRMrw7EC3MVm+HdQDWtlmx77hRPTswhgISe7d/3BzMSMsMz0JaEbUxAOd2etsRKQ== X-Received: by 2002:a17:906:32c3:b0:970:71c:df58 with SMTP id k3-20020a17090632c300b00970071cdf58mr3269835ejk.42.1690680593477; Sat, 29 Jul 2023 18:29:53 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id s4-20020a170906c30400b009926928d486sm3948593ejz.35.2023.07.29.18.29.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 18:29:53 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 45DFB27C0054; Sat, 29 Jul 2023 21:29:50 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Sat, 29 Jul 2023 21:29:50 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrieelgdegiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfevffeiueejhfeuiefggeeuheeg gefgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsg hoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieeg qddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigi hmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 29 Jul 2023 21:29:49 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Martin Rodriguez Reboredo , Alice Ryhl , Dariusz Sosnowski , Geoffrey Thomas , Fox Chen , John Baublitz , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Kees Cook , stable@vger.kernel.org Subject: [PATCH 2/3] rust: allocator: Use krealloc_aligned() in KernelAllocator::alloc Date: Sat, 29 Jul 2023 18:29:03 -0700 Message-ID: <20230730012905.643822-3-boqun.feng@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230730012905.643822-1-boqun.feng@gmail.com> References: <20230730012905.643822-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This fixes the potential issue that when KernelAllocator is used, the allocation may be mis-aligned due to SLAB's alignment guarantee. Signed-off-by: Boqun Feng Reviewed-by: Andreas Hindborg Reviewed-bys, appreciate anyone to take a look, thanks! --- rust/kernel/allocator.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs index fae11d1fdba7..1aec688cf0e0 100644 --- a/rust/kernel/allocator.rs +++ b/rust/kernel/allocator.rs @@ -41,9 +41,9 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layo= ut, flags: bindings::gf =20 unsafe impl GlobalAlloc for KernelAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - // `krealloc()` is used instead of `kmalloc()` because the latter = is - // an inline function and cannot be bound to as a result. - unsafe { bindings::krealloc(ptr::null(), layout.size(), bindings::= GFP_KERNEL) as *mut u8 } + // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero s= ize by the function safety + // requirement. + unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_K= ERNEL) } } =20 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { --=20 2.41.0 From nobody Sun Feb 8 08:27:59 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25AA5C00528 for ; Sun, 30 Jul 2023 01:30:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229665AbjG3BaC (ORCPT ); Sat, 29 Jul 2023 21:30:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229639AbjG3B36 (ORCPT ); Sat, 29 Jul 2023 21:29:58 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6F0B2D47; Sat, 29 Jul 2023 18:29:56 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id 4fb4d7f45d1cf-5222a38c0a0so4805427a12.1; Sat, 29 Jul 2023 18:29:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690680595; x=1691285395; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=h47PWxUJY4ooXK5fyJ1q9IFAXzuyJeCww5vmtT3W8Jw=; b=WtHtHnhpzrNfanl1IxPrvzjwqQr8inR0FCK6nee5iV+tBAaPDx9mH1DYToQj/mjDPc RNnnhTe6v4sz+AAtY3yrtHjx6jTqME73g7Bed64St1cPzYD8xN0zYMFHOsOMVytLesCI j49pisHPJ1ZHW2PmKpV+jDrJflrLWEqUEdGVeTMJTAMsBSgNPXQiI3HWeiW5hrXZRw3j 69CBZnLGiSJg5JtTlniRzHI0iT5wFrwXWuM1zWmTOM8Yzi8EbdazTRQToaQ/A9v1Czo/ vqouKQ+zDmd5+l/mT4RA9Ku31POAqG1Ixi67yKnKvuJ53fnFHK3dW+sSuQWAlhuM5U92 6VoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690680595; x=1691285395; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=h47PWxUJY4ooXK5fyJ1q9IFAXzuyJeCww5vmtT3W8Jw=; b=BUPbiS78iLJoW5udasBjD8HloPu/Xo71wDbWH5540FKzWs9e+cu0KMfYIF4MqIsqwY z9N2l60ud3+TdJjiAQqk0HlAtCoG1m81QCYy27VKuvIzVHUvij/l8QKt5XUOhmKMDoxL 8XQTxfHmXwqW/WHK1CpbtU7iC6JnaVVzflT0w6qyDtkiyFWArJqc73nUZNfg1hasYmOF EXqV/fbv8rh2AMys/MhvBt0JtXgGCr7ZziresBPHj+qDBtXYl5+aVKnCTcz5UCW9a8nZ ldz/eNrtz7VBwdZWqYwEVuUfQzXGz+4V5OpVY1mBgZk3A7261DpaILb1yG081GFY41nF dnnA== X-Gm-Message-State: ABy/qLZdV6nJsKWjmnH/Pboxs/rczW5tseg95gTfZkREf3gLYFCf+vdS aYv8kRhvvBytiOFvKxZu9NU= X-Google-Smtp-Source: APBJJlEkJu6s8QxmoLE96Yye3e2+9QK5Uyie7INpCXldI4pQMVQZsqd2MLR7uzaqdFoij/rMiCJy4Q== X-Received: by 2002:a17:907:2c75:b0:997:d627:263a with SMTP id ib21-20020a1709072c7500b00997d627263amr3201488ejc.67.1690680595028; Sat, 29 Jul 2023 18:29:55 -0700 (PDT) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id e1-20020a1709062c0100b0099bd5b72d93sm3931957ejh.43.2023.07.29.18.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Jul 2023 18:29:54 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailauth.nyi.internal (Postfix) with ESMTP id CD2D127C005A; Sat, 29 Jul 2023 21:29:51 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Sat, 29 Jul 2023 21:29:51 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedviedrieelgdegiecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhvfevufffkffojghfgggtgfesthekredtredtjeenucfhrhhomhepuehoqhhu nhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrdgtohhmqeenucggtffrrg htthgvrhhnpeetueehteefjedufeefleeifeehgfduffejfeeiteekheejjeefieeuffeu ffegjeenucffohhmrghinhepghhithhhuhgsrdgtohhmnecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhh phgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunh drfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sat, 29 Jul 2023 21:29:51 -0400 (EDT) From: Boqun Feng To: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Martin Rodriguez Reboredo , Alice Ryhl , Dariusz Sosnowski , Geoffrey Thomas , Fox Chen , John Baublitz , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Kees Cook , stable@vger.kernel.org Subject: [PATCH 3/3] rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl Date: Sat, 29 Jul 2023 18:29:04 -0700 Message-ID: <20230730012905.643822-4-boqun.feng@gmail.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230730012905.643822-1-boqun.feng@gmail.com> References: <20230730012905.643822-1-boqun.feng@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Bj=C3=B6rn Roy Baron While there are default impls for these methods, using the respective C api's is faster. Currently neither the existing nor these new GlobalAlloc method implementations are actually called. Instead the __rust_* function defined below the GlobalAlloc impl are used. With rustc 1.71 these functions will be gone and all allocation calls will go through the GlobalAlloc implementation. Link: https://github.com/Rust-for-Linux/linux/issues/68 Signed-off-by: Bj=C3=B6rn Roy Baron [boqun: add size adjustment for alignment requirement] Signed-off-by: Boqun Feng Reviewed-by: Andreas Hindborg Reviewed-bys, appreciate anyone to take a look, thanks! --- rust/kernel/allocator.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/rust/kernel/allocator.rs b/rust/kernel/allocator.rs index 1aec688cf0e0..6f1f50465ab3 100644 --- a/rust/kernel/allocator.rs +++ b/rust/kernel/allocator.rs @@ -51,6 +51,33 @@ unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { bindings::kfree(ptr as *const core::ffi::c_void); } } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize= ) -> *mut u8 { + // SAFETY: + // - `new_size`, when rounded up to the nearest multiple of `layou= t.align()`, will not + // overflow `isize` by the function safety requirement. + // - `layout.align()` is a proper alignment (i.e. not zero and mus= t be a power of two). + let layout =3D unsafe { Layout::from_size_align_unchecked(new_size= , layout.align()) }; + + // SAFETY: + // - `ptr` is either null or a pointer allocated by this allocator= by the function safety + // requirement. + // - the size of `layout` is not zero because `new_size` is not ze= ro by the function safety + // requirement. + unsafe { krealloc_aligned(ptr, layout, bindings::GFP_KERNEL) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + // SAFETY: `ptr::null_mut()` is null and `layout` has a non-zero s= ize by the function safety + // requirement. + unsafe { + krealloc_aligned( + ptr::null_mut(), + layout, + bindings::GFP_KERNEL | bindings::__GFP_ZERO, + ) + } + } } =20 #[global_allocator] --=20 2.41.0