From patchwork Sun Jul 30 01:29:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 13333329 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 CF478C04A6A for ; Sun, 30 Jul 2023 01:29:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0EBE68D0003; Sat, 29 Jul 2023 21:29:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09D108D0001; Sat, 29 Jul 2023 21:29:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E31768D0003; Sat, 29 Jul 2023 21:29:55 -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 D3A678D0001 for ; Sat, 29 Jul 2023 21:29:55 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9C9531C9458 for ; Sun, 30 Jul 2023 01:29:55 +0000 (UTC) X-FDA: 81066546750.19.55222A9 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf26.hostedemail.com (Postfix) with ESMTP id 65941140006 for ; Sun, 30 Jul 2023 01:29:53 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=lZMgU3gj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690680593; 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=9OgITT2yiqZUUNG1U4xDDumpCjudK5Ub6jXVaWbbuKI=; b=H0D6tlz30J3h7kPELdQoFgPax0kNV9UPdtXJ80fNO1FDqUuvO9a/mp2oCZCXFkisID4cme VXHuSbH7xO7mcD8XBz/W0mcH6zqiaqHvhNCIC2XOA71dWFajvg6Uip75meSCsnRu+J6ADR bh4R6VGAWQ5g7EFZ7Jd3F+aFtdI5CLo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=lZMgU3gj; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf26.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690680593; a=rsa-sha256; cv=none; b=59yXQSRgk8oSlfmQdMpXwsElcunHRJ9t7HQV+QYLOGy/Jd9nY8CJI4GWuXwEnfb2TO6Qtf o66WLp1GWtJBYb5Ry7UkOj/L/5TwyLuv282cjJZQusQ7ihnJZoVUNVDOhw3QCDQN7g1sfZ 1CG/3vDJMLvFUffHxOlweE6FNlzFiJs= Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-99bc0a20b54so527377066b.1 for ; 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=i6dCQBWJw4XtwhG+JzfjqZgoSN4wHbzXmCNmhEQzW1LgeoW7ZjAjAOBxTo5P91IN29 yO6qMokth8WLEoQ1Y1KkVpM3RKDEVdz5YQqFD2dhKtnvy4bqG9/lYZdlSdOfbhpoJL5y wsmzklT50H489HbIi4aeYt2F2ALujqwIB3td6Rzb6uND8TBdrzGtDf6k52NuutP1z3VH Ly/HadTnjvjGfBkFjnEn3JmCFtXAsv9uxNg28tb86TxI2vCRTJjD8SPUn071HFxEC3Iu 4yrkZ8oCeKs4/7FvphF4s5aJ6wJnLYw/9DpgWbL2hHfaDE3w89JP/z9DlrnQZ1uqSMX9 XlgQ== X-Gm-Message-State: ABy/qLbGvEToNTRA2U1vX7soaFpGhAn4txsuRsUIkaaGDhKsTwnA3mNW 3uRiLj65e4MbNS/pRSCg/Svyt0l7BoE= 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_Roy_Baron?= , 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 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 65941140006 X-Stat-Signature: gir9zmujbffd3ro44fo39o73ynbi1p8y X-HE-Tag: 1690680593-201220 X-HE-Meta: U2FsdGVkX1+lKrGcYGZ8W/iMkp4ZWPjt4rYxzgSCQKo2vFgcgemCm8Cf+4geDitnoCoMvHuY5FN49dOmC0KqvvRtePTlmma/pDmWije7CZQg97n5lhLftToA1mfJYrqJ87SumEorpqOxN3TKY/8bM3rnrFUE6MzX5AEdd9sOUqPWtQfZmk32rfho61xsD/sd62SaRQwZd/jrYaAq5FzPqKUnMyPhuTaWp3wxTblEFpO3aST/IUMeggs4MILZ56FqoFiJC792ajvQ/22MWGKaJtl/OataKTamj1O4GT1E4Cz1nD5GfFzHxM91tZeulLijnvSg0BTgBC1OVmg4/3XZ0M2kOWhMq9hGDtNMclPI7DTD01jfxJSUp2WDh9lbmmIPSczqDUWDTQJlzOph0JrNk+VhxMtcGnB/QjxA2w3hjapPDtwwkw8eDZ/KautyDAxoge5w+rcZADldz62BU89yBUD0r8k3kaCnXZqVr1zb79Jvmhj999U+hZXPdnjJwSGAQ0npbCfNdj/Fp0A1RNQxZlFHQFIAAzG/COnMzAw83dpC7E8eBCZs9c4Tm9pFb301dRh06bWPBvzzSmcxbl65OUAqIpndvqn82Z15L5g/17eBZjy9a28rH4T4uNNWCpfxZV6b1UFYY5OGKGlVy78jYYFCXYUbv9KGxxfXcquLk2QLzi5xgMYkkCIJZAPgIw9zyxQS6luSTltIK8c2pJfw4/fm/EEQJi/pLxdqhLiRtpoMc3oCGnME6BBsFf7d1Rn+KNGBEyWisgmnNGepsHDT7QOGyeTuR5z1ezCAXyJSs4NZTGDNJFwM20CUgwb6qeNBc6Pq8ObyYUEul2jeJsjAKmwkHQ44wxYDk1fSOJJ8bIKKuGXj8v4TLptXXCJbMLzw4wBEy5Q6gd2gTzmxTQ6W035o6aMFSWC8Ddir8a5fu1FUO9fhsmWZvhwbvxL0DWUZJTjPA8dmKgzH1/a5N3K 9QG49IXW B2wLlsWvG7FZNkaEDgOg1ET9U0aqO5OrhqpLPcyI6pHGFFMgEKxtUx57EpnMExznvP7C14HDJDbHSZxHVOdaZiW57/g07TgyDER/5d3uHMut5Rkq08PA6yxsmmNkyJUVTknzqpZ4kqDjHVGqdnk2UU5pNNHiT/lldG/Dwfpm1WZLxXOsvuSTbphnm7ZBdqxFfFIfSpBFAioauiQda8m+21B+GJM4wzhHcZmKmVNATsQ20e9Vh0EX61gwX30iZ7Vuf9y7DZEFzriirS/UXe7ORetYdW+IUZj4diXYTJpe7vK7yNoIKwTmjArUWc5v40iEuI1TwrGAf0DvUIzkXHpUgat4pd/JKzhio1gojazz6nH+e2jhCEnIhMCNxL8cWEWwws3wSNnG3bneyND4HJgYiiPrYm+9Ji2iRwspDOisv0EXNA1WUeSvf1QfuyHKaBYMPrOBPPNJajoM1+x0rS6KLcN+BYGpP6+/u6QP01IfsTNwo+Wy9JGEvKPOmJTs5v0ZY079WLrg9MSS+AdYBHMwt5faLTW3kvxgWVE1uypiGHSXxSyTR/J0VEmmpAogIcP5tRjSYxtxjzRdWmwAm/YIxG43cDg== 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: 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 --- 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_helper.h index 3e601ce2548d..058954961bfc 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -13,5 +13,6 @@ #include /* `bindgen` gets confused at certain things. */ +const size_t BINDINGS_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN; const gfp_t BINDINGS_GFP_KERNEL = GFP_KERNEL; const gfp_t BINDINGS___GFP_ZERO = __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 @@ 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. +unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gfp_t) -> *mut u8 { + // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. + let layout = new_layout.pad_to_align(); + + let mut size = 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 comments 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 = 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()` (which 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, flags) 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 moment, // 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_KERNEL) as *mut u8 } +fn __rust_alloc(size: usize, align: usize) -> *mut u8 { + // SAFETY: See assumption above. + let layout = 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_KERNEL) } } #[no_mangle] @@ -42,23 +82,27 @@ fn __rust_dealloc(ptr: *mut u8, _size: usize, _align: usize) { } #[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 = unsafe { Layout::from_size_align_unchecked(new_size, align) }; + + // SAFETY: Per assumption above, `ptr` is allocated by `__rust_*` before, and the size of + // `new_layout` is greater than 0. + unsafe { krealloc_aligned(ptr, new_layout, bindings::GFP_KERNEL) } } #[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 = 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 + ) } } From patchwork Sun Jul 30 01:29:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 13333330 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 05D2EC04FE0 for ; Sun, 30 Jul 2023 01:29:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83F998D0005; Sat, 29 Jul 2023 21:29:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7C9048D0001; Sat, 29 Jul 2023 21:29:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F3CD8D0005; Sat, 29 Jul 2023 21:29:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 500D48D0001 for ; Sat, 29 Jul 2023 21:29:58 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1D78040461 for ; Sun, 30 Jul 2023 01:29:57 +0000 (UTC) X-FDA: 81066546834.09.EFD1642 Received: from mail-ej1-f42.google.com (mail-ej1-f42.google.com [209.85.218.42]) by imf21.hostedemail.com (Postfix) with ESMTP id E57341C0004 for ; Sun, 30 Jul 2023 01:29:54 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=GxodfzLo; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690680595; 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=PhtA3S/JDjazeAwjyLjhNAHRYNrsVmh0Eu0g8T30AIE=; b=cnyA3zeRMGvgZGTyegge11zsAX+yGpTkuOVsHeNot3XC9J+72l6o0xpibeHrViJmxRrumC CgFFOi2uBfJjztcs+T2QneAP/e1rDbNOXN0edtgE1Xq5SEJxXoAyuH3OmxYhhDe3rv1YQ7 LOhWN7YYJnsqGG4jXOKFmN72qL3sPCA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=GxodfzLo; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.42 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690680595; a=rsa-sha256; cv=none; b=dheKi3oa/QLE543Aje61m9swJjtEkG6YiORzK9hu9GuTb9bjHArH0R9dG/dnNni/a+XlAL lP6XnsgQGiReA5wlrTaucML9KimmzQEpWJ4Ej1ZW3ze7NhqAAb5juDOCdLXGrjAyCWNzpk +cBJKe0A+MzXFePpt5Dpz3fBtfABtkk= Received: by mail-ej1-f42.google.com with SMTP id a640c23a62f3a-99c0290f0a8so91647066b.1 for ; Sat, 29 Jul 2023 18:29:54 -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=XOYDluHHHafrSsrm+44+9wqmNmyyTvKj1LImpurEuSwwHDJeokjPwKv04lBkmErI4z cyyqMKhu+M8CwqG5mQ47IZ3B0ug4NEAD9jLMQDDxn3NQlD8RotPIP55r5RIkgPyFJ1u5 mMvgFm4BtuIj+VbFL/dz42tKo0jSXhSDT50RjS4VK89jhr4L4KFvss8GCtJnRw4D4yor /vg18PyMPn37S3LsgHU5eTfZo7NXG5USf+soZeiJ9dTP+6BwzNR4gnvIB5j79kG4bbvV BKqHvDLh26drac3TrLaIBbMuirrud/jN67IZiPxpGsr9Oh6CmlpM2/MVbyEihjCte2sf c3ZQ== X-Gm-Message-State: ABy/qLZV5GcvrkTlqoe/Nxd41T4eVgBABLY/ELhj3m9SjY9nYCG9jy/p NIKp6aNpRZlZddP5BzazMKdUphfWPcg= 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_Roy_Baron?= , 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 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: E57341C0004 X-Stat-Signature: gfq3y1g3spe4uxx3cindizw6ebo8xq54 X-HE-Tag: 1690680594-297416 X-HE-Meta: U2FsdGVkX1/zBf2Nll3XfFeJdsWTyPfMwor0triaBqf0QMIKGH92y9brg8gXFpikEbhuCjldBOwEvPFcsQC6aimhvGxYDtClbhSe9s+jHkLwEaooSjDKi6Sg1ntNUzAR+H7jeKEfs9D1e8SJY6cgFLhPeJLpNLTNrQjDYaYSgHMXkqZbVofkPgwBL4EbnshTchfDt5uiHQBB4HhcMbO89XMfBchc4B62Z7imJjNhKFXU5mevNBAPmT17dZ/t1t0Be3ivk6+/fMEfzMCAm5P090igMVG13MKCC7Gu+MKYVSO5hQ60f8QmMU3M8Vkwpu1tPBl1aiDVOEfgT6E4OadTXB/pjc3S6mas07jNXmZQL+onL1LUNA1UYLVTZEsfaTc/B3NcVi9yX6S0sHozVfLK0XjltkShxSkZB6fA7Ve0f7J8uS7p+g8NqgHUklIz5UjGaMiB4biyfSjF3BbZuz4oiDkZ8jSGXoQBZrti9hua5T0oGX7O/vK8eQRa+Uzu/29uR3kCLFtMump8795rVS1mm8xdrblIbqZ7zrH0U7tzkLfwTnDllqnLUIx5h719SzIWrk231D9a4P0l1Goxs9GoXG/iLul6KWaD7wXrnh6YjteSIeabgCglWTVlYN4WpC3tWmYJfXMdHCwDYH1KGd6BK1PKeKn4IzFyqebsD4muR28FGLwOMLbLnXK5yVOgCRWVtqizMeTs+yksTJzEWOvkc3fJ5+9xLf+k2VoaYAoBEVpgFgutTTEB5iotgiDM4xDtaWfsq9wWtbwNh8iil+5qCbRYO+LOl1d/hSyyKHy8Y/ofp4q45tIkLK87SPXS1WJTXBTslCRi3rD2brRMX25DUBlYTjG+izJloRH1AJaq0Yb7b6pozXvHfoHgAsAW+Tcw/BIcOUz1u8hVD8pdWTNNueAJJLxg0y0MvjmrEMNIFvXteMD5+0ESaXArq4Ymibb1wQaWN/B7pVEuJEZjDui h+dT9NNh Flub2xCCqrWDJZC9cLLd8hGs0tjSfHZ9EesiszAIwvQz4OrwnWTuPEkc25Hzh2/KWHySTuRukIPjjF4pl8GXSf6eVvxnYqo19g8aROH2WrsUTZvjxy7lyo6vCAcQTxevtRHLuScXfLeU9P0SPswqXfXSkLvXRX0lCdDNJAtZQ6WuJJp/4Om4IM7fcUEE8Avx+ggllGvOzmOsEP9SX7JS4LTU6WOLV9njTbuR1zoqESBw8dHQPXQfWhAoi6Q8s3M11eoeb1gdft76tQrWtv73I+pG9yzfqe7fSUExxBnohOwIT3kp+Cncwj+37xIhgAOMBimT8uTlmlvGVi0LmaH92wD+9G3gzTSTEqk7Z8ELSDEf3h2oD0Bd2iqB8pYsRZ4rMJMCwooizYkcc0sF0I/TSJzU4kqFCI1zbv6qEInOBjjhxi6/qXivNz1e8KcCUDQpX9ETzVjgmU/3v6Xhgi2GtJhvzu4rvVa9euxWpQDGiYrjB7lVkF/97Zq12F/waHXZgI21Lf4aL+Uj3Jjx5QMHB+PzDF0xQh6tBclry2eG2Osxc9LHFu1uLK/fOi69UCNaxwm+giyl9qdDW394= 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: 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 --- 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: Layout, flags: bindings::gf 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 size by the function safety + // requirement. + unsafe { krealloc_aligned(ptr::null_mut(), layout, bindings::GFP_KERNEL) } } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { From patchwork Sun Jul 30 01:29:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Boqun Feng X-Patchwork-Id: 13333331 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 8D8A8C00528 for ; Sun, 30 Jul 2023 01:30:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D1A88D0006; Sat, 29 Jul 2023 21:29:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5596D8D0001; Sat, 29 Jul 2023 21:29:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C0BD8D0006; Sat, 29 Jul 2023 21:29:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 14CBB8D0001 for ; Sat, 29 Jul 2023 21:29:59 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id D14E38065A for ; Sun, 30 Jul 2023 01:29:58 +0000 (UTC) X-FDA: 81066546876.01.17EA3E6 Received: from mail-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) by imf23.hostedemail.com (Postfix) with ESMTP id 8BF25140003 for ; Sun, 30 Jul 2023 01:29:56 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WtHtHnhp; spf=pass (imf23.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.45 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690680596; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=h47PWxUJY4ooXK5fyJ1q9IFAXzuyJeCww5vmtT3W8Jw=; b=5qy2In9/3R1rxCIKrxbAmb0SG1ok6nQHcj6Zex6fso4icsKWx9zfqLHJR6tmhF1GfYfP1l 8e/F02dBfbz2uM+BHiAQjKjckpHngGSH/L4UUKDyqIFkLI1kO47gJ/LfzFeT6glZpJL1yY c7zfNxVAxdEjuO5hQqR1cRhnFNWdxWU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690680596; a=rsa-sha256; cv=none; b=PksLmeIwUF3IqQ2TmPZ5t1DM3pNdpOqXgYIlbWDdv9ZxN3uMBuj7lH+xVJbIaWXgwK4kFP EGl9I03l/PpJmwuzCcJaWTF1bOdjszi85C1OU4HTQBSC6NgfTwChECxhghuWviato7VkPQ Bz9TvhztSPLKzrpl3Kp7HQ2/yX9CpII= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WtHtHnhp; spf=pass (imf23.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.218.45 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-99bd1d0cf2fso507756866b.3 for ; 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=jmJ5T8OyeLljooD09mz9e6JwTBL4e4t38opLQMqBpy9p8MrGiAQ4Ml3s6Wswg4LeAQ R9Wf8FOVCpJlTZInhg5eQVFv7WEJzCyREg9Wq8zy1Z548FoUVuxFGqJbpsQR6y/4lOyL uI+1wz++KWR5NWckVXw6N1fGNMLZkvuZziCURoQ/96dG2xqNHlPn/GwVIL06xXKQ3fxD Ni936q/cPfqWcPeOuhCJm6KDobRn2FTuZwAFvX9yNtQW1CFvqdVbjWnyF/jA/z97Z2De jQ/tso7aCfEeq2lATiDlU1fobf8zhNZJ6p7Ew7amDNwXn5onTfjc9530sdEWIoAa/SxA uuNQ== X-Gm-Message-State: ABy/qLYwYQv9qMFWnoPfTidNFHbBFzPgeonEKgsl+iFeulUfpz+bxRcA umLuyITQ8cPEr/17VkHK0QM= 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_Roy_Baron?= , 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 X-Stat-Signature: p137b4hxcyah7666b9xf9u1e5b653kqk X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8BF25140003 X-Rspam-User: X-HE-Tag: 1690680596-606612 X-HE-Meta: U2FsdGVkX19ilhNf2mVCkStWPTKHrQ1Vg7gVgp3T/eztHh4KK0vNq/B9hF4gThMQwRtRvTjqjQegz2lNT+4PpnNUb+3Om2lA8j39192srJz0NKxaWxnpNACCSoEoHbv4Jrtarqd01wxOa8XJc+BkKRxGlKThv4Q2krFnaQwiWBoEyoYLdoqXAVUsQtmY0o1FgfIvVgm8R8/T9cPDsf//NcXuXeYPuSuSH7tXsyPIzEahkHgbFaJwVVHAgR8S4LuUc6groZ+tHjUouZGZG+ui4iQzMVj1onfkrN6oFqSXtpggCVq1bCIzWj6LW2sbvJbRZkQ9nwhx2SQ09Ceaog0Py0PDB9hZUJtRj0hW97y/W56hnQGufPsJZM0yjIQ8DyYDz5KBlcNtAR7k59FFIh824kparYL/PuJxRLhP9WyYIDJ7FwqXbyOygsEu4WE/r14St+t+1LHbJIpAUXt8kf8TVLViUHK1LFtt8XJTGZGdPn5ZJjJxSHo2F2BsIdxvReABfujtVW32wiefM4w4553KmbZHAMYj8jMlVPbZvY6bsPlG/KxpXcBpEmyYM5AcpzSSjx9wun22/PbOMH1SlmRpi2B+wX9PUFavAVhQLbfw6nZ1RswJxvW/EeKplejMDP/0M7uOUy/158Q7JTiHy2ZGOzu8540C7TPk32ISh5U9NtBHwyaQtOdg8z3oV1gBnfm1IXVJybzdkAm4Qf61qRt1LAyRv6SCoecf+M4ApIjYVxjqbFl+ENpWONILizpJJGAQG9Y857ducjRwHcC7zyVrKmfS1WTDVYcAQ9gXd2H0KZlJMVW1qcGVOSst6Ux+svvZHGctr+C4dJHLh+jaywYsbDhH07q11I50iVM2zC8uDj1txE+HANyozsijOUWeBHOEYz3W8qjiwx3VRJhpa2T3zOfeDeKX3ilJzD9/Xz5rla1QRHN/w/NStXbSHfX+9UVGiGmTVZe/I+FR5LciWr9 sVlgd1zD bHmx1XUreveDhNGdV3AB8HOZoTMtZeLJDuCU1wsVO0fIX1/o2MB0DQ/iIklzYl/cgnAjK//p4qo6/FpGfH2GcEsaB6xipPNDuoEWORrmfXHq7x3mTeOZxVoWQQ+aaVdXTPmt7FyAwgnZ75UuXSUgVPgNXfsRfBaeWchAVBGhwuNYYY9Rm4m1CeEoOjHN5CGrEnnBFJ/sonQuJytpynwPrPwhHbdQ3XyqrcwtOvu81gZkSMZcNwmliOtgz45IjI6tsSx/3aNUxUeWichgqU3yww9kHtMDd0kLlMC1sLrRfUFeZPfo52h3yBmRJTIQXYUm/ZHrTUrJQrCI5tGOj/RQ4bBiebeo7wSWu4niQGaIJztturSXjn1wuul39SLUWHyugypbO3LtLOMvfPqNaXg1abqMObFu5UEyzeu+5Bu/8b7A4Nfr1/eqmZUUv/XJem0IkDlmhvcvwBqI66LK9OA5CjXZZO7o46vrTtqYvyU4U9GIhkmBNPpbY6+TPoD7MmyRN7LK8njm5ibkbH1ImOOg2wbsj2FiFTI2gMHOTcmvonESdyTbO7W1SpkEfPixeMN9lXNNvj5iE3RSErb+w5x2M7kFwnnhJT/ZI9LSOyWprD0WSb5DQSkrNiQZWYDjYY/Ti5zo1ew2lBI6C/ChfoRvCHT9zzUAw/zU5gyJh 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: From: Björn 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örn Roy Baron [boqun: add size adjustment for alignment requirement] Signed-off-by: Boqun Feng Reviewed-by: Andreas Hindborg --- 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 `layout.align()`, will not + // overflow `isize` by the function safety requirement. + // - `layout.align()` is a proper alignment (i.e. not zero and must be a power of two). + let layout = 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 zero 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 size by the function safety + // requirement. + unsafe { + krealloc_aligned( + ptr::null_mut(), + layout, + bindings::GFP_KERNEL | bindings::__GFP_ZERO, + ) + } + } } #[global_allocator]