Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4278871rwb; Mon, 31 Jul 2023 04:41:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlHVpZ99ph2i8UpCyDH27o1r7dGcerWRDlVYkC9VnkGkPGlMCTcrIZLS3Dya/nh0vTELlHwb X-Received: by 2002:a17:902:f7ca:b0:1bb:9b48:ea94 with SMTP id h10-20020a170902f7ca00b001bb9b48ea94mr8836665plw.32.1690803710693; Mon, 31 Jul 2023 04:41:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690803710; cv=none; d=google.com; s=arc-20160816; b=t/OWF2kHcb5Bt//iv+YbT4NX2JJwMANoAWP2Z4pCSpoprbRTKwVBH9krAGmSWlYpHh 9I5lOiLwzWz2vyAcf3V5voQ9X0uEvYnTI5tFi+asx0PQSlqMCtJJhYyOVu+fJK3/NndT ky5VssD8RDwr9qJ5XePcxmWaqxWrAGsZnnVdEclnPTvOL4X/2ze5pnsF7NUaTS187yVS rJyPCmVGYhR/Wf2zTZfh+spY5E70wSIJ4IZATfMNbKsZNXkHXzEWEFQQnwOVPEZTJR+5 xvbuRG8oAVNHY/Gn8KInv1+ExzbqqA4WwHQtbwYtS7zr0NqSxSM6e6hxOafkdQQMoOvV mOvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:in-reply-to:date:subject:cc:to:from:user-agent :references:dkim-signature; bh=a+yXtCYwJ1vp4lh1G4+6nAEpCQWF+yOkcXs9SOROqGU=; fh=khMvK53lMinHaS8XY1OTptwGuwYl9X1/4K/Swvgn6TA=; b=DJfstmyi2Xd4alG3UnwfkxgiGAOY4vFj+dy4cqFlZw8vhawPgN2JWi1PEJ00EAWglV PBPA/XxVvAMcXUSho/7CYLTtqRt+pqzod52mvicJ6TESRmZx0gOzi86NGVefnOEas2AA 5sNsnTm1JrJVKC0o/LFWGwcsHPh0G6fJjAnnlegNyjrDispK0HvNItpAv/uf4kWMHtwT Zz+xBc8+ynRVRm74+spoWbHTlu5fHaNoxUweJCQhOBpErMDQOU2c/ohSyX0RZVcu9QLW +/aSNNwEVY87zOK/c36e5sPgz17kqqz+yIjB/C3br0VOC0B1t1wI1zKwvC+0OWx7ir3d 1kog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=eTpl40uc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p18-20020a170902ebd200b001ae4a01a7e0si5603003plg.236.2023.07.31.04.41.37; Mon, 31 Jul 2023 04:41:50 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@metaspace-dk.20221208.gappssmtp.com header.s=20221208 header.b=eTpl40uc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232492AbjGaLOP (ORCPT + 99 others); Mon, 31 Jul 2023 07:14:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229937AbjGaLOO (ORCPT ); Mon, 31 Jul 2023 07:14:14 -0400 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 422E6E6C for ; Mon, 31 Jul 2023 04:14:11 -0700 (PDT) Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fe1a17f983so11969775e9.3 for ; Mon, 31 Jul 2023 04:14:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20221208.gappssmtp.com; s=20221208; t=1690802050; x=1691406850; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:from:to:cc:subject:date :message-id:reply-to; bh=a+yXtCYwJ1vp4lh1G4+6nAEpCQWF+yOkcXs9SOROqGU=; b=eTpl40ucewCsFMrQBu0kMJ/J9h7WUG8ZwDVthoX9ympKu1DDdzDGoSO4pfrbo1q4lT VMS2+6v5e4TcA1hj9UFwPgxEyUDPkKJEwzZkpfIo8iP9o7XCUCImbYbPBqW+q7CnRKst Lj1gL/d0bPyzvYAqpNII4QDo1S+u14uH2I56NxuYK2EgrlVefWuAwtT8+se6XUkAHXYJ tlimaNnYJp3U8U6ab+VBF2UNMFetpuFVB2TrTC8EDnljcFGNBjnMKHzgPOo/Mf1BCnDm +tC8P4rab6qd/cVIyFbr+CMqsXXpHpuHX2mC8ZCH/+uygP6vpGdG4zxh4axf/DGg1Sz0 LYJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690802050; x=1691406850; h=content-transfer-encoding:mime-version:message-id:in-reply-to:date :subject:cc:to:from:user-agent:references:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=a+yXtCYwJ1vp4lh1G4+6nAEpCQWF+yOkcXs9SOROqGU=; b=h1J4MWviyBXIwcwW6F2UAB4Jy7Md2g2mOVWdpEKn9wNRac3ndRSzsZSWhC4ajJADyd 6L3mfbmb1JQ9ZqzrskvjS55NnVEVGS9KMNoxrrxQzZXXE5ASirr+++HvtpO807v5M+DW Vr7DhfYZ9xhdwmEoM30jc5+a/zZZ1uTUclSgD3COxD3KU9zIxapiAsY4SxX8NtlcC02n voBxhZh0o/AjreZNrXN5yjLQTps9kilLK7kl9jvv6os+2H8syQte1eSosYHGyHGLskUo eMR5b7CfVwj1fO7AypExGUOs7I76lntjBAdfzy0GHwx9xENe5Xhh79woiQk+mYZ7+9Lz 8Q1A== X-Gm-Message-State: ABy/qLZppG1OKtACQkU/YG3DqHpA2MCryep8QtgvLc/HPLtHUx7jxkih /yv3Y6pR7YbArJ6ZYyxuGS5fUQ== X-Received: by 2002:a5d:452f:0:b0:317:6262:87af with SMTP id j15-20020a5d452f000000b00317626287afmr5251217wra.16.1690802049408; Mon, 31 Jul 2023 04:14:09 -0700 (PDT) Received: from localhost ([165.225.194.195]) by smtp.gmail.com with ESMTPSA id z7-20020a5d4407000000b0031766e99429sm12713940wrq.115.2023.07.31.04.14.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jul 2023 04:14:09 -0700 (PDT) References: <20230730012905.643822-1-boqun.feng@gmail.com> <20230730012905.643822-4-boqun.feng@gmail.com> User-agent: mu4e 1.10.5; emacs 28.2.50 From: "Andreas Hindborg (Samsung)" To: Boqun Feng Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , 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: Re: [PATCH 3/3] rust: alloc: Add realloc and alloc_zeroed to the GlobalAlloc impl Date: Mon, 31 Jul 2023 13:12:17 +0200 In-reply-to: <20230730012905.643822-4-boqun.feng@gmail.com> Message-ID: <87y1iwpc3j.fsf@metaspace.dk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Boqun Feng writes: > 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 > 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: usi= ze) -> *mut u8 { > + // SAFETY: > + // - `new_size`, when rounded up to the nearest multiple of `lay= out.align()`, will not > + // overflow `isize` by the function safety requirement. > + // - `layout.align()` is a proper alignment (i.e. not zero and m= ust be a power of two). > + let layout =3D unsafe { Layout::from_size_align_unchecked(new_si= ze, layout.align()) }; > + > + // SAFETY: > + // - `ptr` is either null or a pointer allocated by this allocat= or 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, > + ) > + } > + } > } >=20=20 > #[global_allocator]