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+g8NqgHUklIz5UjGaMiB4biyfSjF3BbZuz4oiDkZ8jSGXoQBZr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CNaxwm+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) {