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/Pu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7MmyRN7LK8njm5ibkbH1ImOOg2wbsj2FiFTI2gMHOTcmvonESdyTbO7W1SpkEfPixeMN9lXNNvj5iE3RSErb+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]