Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp513500lqz; Sat, 30 Mar 2024 06:35:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVydp94jKtdiA0ZRMvd8qz2k2ev992wSt8RsILhp40kJPW1ckc7qNszGhjlQICvAQkVIKhZ1SxTbXxe1GCqOyqssl1DgPm4V/iJtOBNaA== X-Google-Smtp-Source: AGHT+IEkFOJKUBLzk7Km/rUtEp3jacJ9e1Xs/9I5qJgIZzk4PPFXfHrXINpngM++Ml5wSHGD4UmE X-Received: by 2002:a05:622a:199a:b0:432:b4a2:4e7f with SMTP id u26-20020a05622a199a00b00432b4a24e7fmr6643937qtc.22.1711805720937; Sat, 30 Mar 2024 06:35:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711805720; cv=pass; d=google.com; s=arc-20160816; b=z9tBvdhPuRvE7vCOVRseKSU5kYS0uPIQh/Kw95NtsUlyS9hKOiXQp5xKpWmOqVoRDR WG7TNyc5OKyEAW/+JNpxuE4JEWQJoC5PsUMJBS2A1SGzNxv+O3jX8btsONNI6m15SVuo JLhIW3ukspS8YYBPQewtABcLXWYIoWEPo6btxI0yyTXkXU6xOtLyOQIkunRC+VmzBDiJ +y4RSmXidJ0M9kmcNJEQLvZkrGfB19BR8o8A0Ro3xrxEF1vomhUml/NT9Bh21wQioHUS DC4xOklQXDX4VM0I8gIc680/ivfxmc59nqCRy777dNVq16iWK403ZoKKxLl3Dv8wqsF/ y6tw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:feedback-id:references :in-reply-to:message-id:subject:cc:from:to:date:dkim-signature; bh=qijb/PtssBKhbeYXFRSbFkJKp9EFEcX+rCDOKjXfYYQ=; fh=ILXzPT3nbZXhvwVwwPDMnlxaRbCVxHwkAsH+01sTnAQ=; b=DXTboj4v8pOG0Xd3/Lqi3a3gm1ZXUPxmfG1eDqPONsFjDj3PpOTC3uhB4BAcK//NLH /QvrD6gGzf/x/EkxUajQaQmlhIthv+0lp0qgYbsg4SntDXQTGxPuV0+GEWvxlp2+8hYU h9qBwFpxqMy3xniJLiD0rEj0y1eL9QFlgnChCWo0ekjZjAjfgA6MC7A/eZOlLLmsZ2qu rcqstigz+kDcO3hZZ+6c4tnlJovt4oQyBO21i99RWTAKN9rU5kayBYP8PDNdcLLxGt8e J6XrzXzR6SxmfA0TE7c+Ma9Idf87HMd4fO8z2A4GD+Pn395H2ifeAQ3ohlxVqnymnxoz x8eg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=TWLcUnn4; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-125735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125735-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c10-20020ac87d8a000000b0043135a48b37si5821167qtd.209.2024.03.30.06.35.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Mar 2024 06:35:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=TWLcUnn4; arc=pass (i=1 spf=pass spfdomain=proton.me dkim=pass dkdomain=proton.me dmarc=pass fromdomain=proton.me); spf=pass (google.com: domain of linux-kernel+bounces-125735-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125735-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A6ABD1C2100D for ; Sat, 30 Mar 2024 13:35:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 791FD2AF18; Sat, 30 Mar 2024 13:35:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="TWLcUnn4" Received: from mail-40134.protonmail.ch (mail-40134.protonmail.ch [185.70.40.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A60111EA74; Sat, 30 Mar 2024 13:35:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.40.134 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711805712; cv=none; b=TunKf1+3fBL6EYFAYNZjolgG9rmUcDlE6fBbgtIfXFASNsOAU+Ycgl/s8A9GzkGUq6xYAStRqbHfc81p8AdDy4n9rUY4psNXtfm2R3ESNO9CRx+zwohEBAx9wddItP4iKZqCq9xgRx1lZSSo2JoOaL6ZJ0qRJ0foz8WWH5hpo1I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711805712; c=relaxed/simple; bh=QYb6/7sQ3bPgMY58EPb6y4dlsMY33z1x/wCF5aSqz2M=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=P1DA9UdkunJyZszpVO0e5sttFYdBgFFhuhrL9avM76VwhKd8uw5tqx8+oJXQ9dXwELufoO/WEp55m9J0iSh7rEIQ4TSuARVv1nmHYLm0jbhW8ESfcJ/NoIkZJQMa/N+6u7NSi2cNyQ+XMt5ubBhWqhKUrMBtspdJ9wfXMkCcPLI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me; spf=pass smtp.mailfrom=proton.me; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b=TWLcUnn4; arc=none smtp.client-ip=185.70.40.134 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=proton.me Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=proton.me DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1711805708; x=1712064908; bh=qijb/PtssBKhbeYXFRSbFkJKp9EFEcX+rCDOKjXfYYQ=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=TWLcUnn4mXryIbdSIVepkWPLIHpkLvguRNx42gykn6NrTrJTUEtBl1ivYFCYXUVlm JCu3nCpyheZr037QaGoaaQlfYKoG5g8II0ShJMFm8IOYrMwEEaF2GCLz0+npGEOrPO 6St1RsoFwqusft2iLmGyCAjS4iCzHO2qJ8TCMrBCAOWrKpR24Ga5d4ZeLC7O/IQumW jIFV12U86VfvOd3pcRQG9UqYOxxqcmmmsUo2dNlgZ5DH+0h4k6OqVmzOdGlGyMWIGP 1dUn6KjW6tXdsHLWHhmib11hbyKQyHGrRwbc269t3lpsii1G2Jt5MaRw3xMzBD/Yy8 uKorhl27B5Bvw== Date: Sat, 30 Mar 2024 13:35:04 +0000 To: Wedson Almeida Filho , rust-for-linux@vger.kernel.org From: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Andreas Hindborg , Alice Ryhl , linux-kernel@vger.kernel.org, Wedson Almeida Filho Subject: Re: [PATCH v3 06/10] rust: alloc: introduce the `BoxExt` trait Message-ID: <6a684f4d-6f95-4c52-b515-73c832f050a8@proton.me> In-Reply-To: <20240328013603.206764-7-wedsonaf@gmail.com> References: <20240328013603.206764-1-wedsonaf@gmail.com> <20240328013603.206764-7-wedsonaf@gmail.com> Feedback-ID: 71780778:user:proton Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 28.03.24 02:35, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho >=20 > Make fallible versions of `new` and `new_uninit` methods available in > `Box` even though it doesn't implement them because we build `alloc` > with the `no_global_oom_handling` config. >=20 > They also have an extra `flags` parameter that allows callers to pass > flags to the allocator. >=20 > Signed-off-by: Wedson Almeida Filho > --- > rust/kernel/alloc.rs | 1 + > rust/kernel/alloc/allocator.rs | 2 +- > rust/kernel/alloc/box_ext.rs | 60 ++++++++++++++++++++++++++++++++++ > rust/kernel/init.rs | 13 ++++---- > rust/kernel/prelude.rs | 2 +- > rust/kernel/sync/arc.rs | 3 +- > 6 files changed, 72 insertions(+), 9 deletions(-) > create mode 100644 rust/kernel/alloc/box_ext.rs With Boqun's suggestion (feel free to take your variant): Reviewed-by: Benno Lossin [...] > +impl BoxExt for Box { > + fn new(x: T, flags: Flags) -> Result { > + let mut b =3D >::new_uninit(flags)?; > + b.write(x); > + // SAFETY: The contents were just initialised in the line above. > + Ok(unsafe { b.assume_init() }) > + } > + > + #[cfg(any(test, testlib))] > + fn new_uninit(_flags: Flags) -> Result>, AllocErr= or> { > + Ok(Box::new_uninit()) > + } > + > + #[cfg(not(any(test, testlib)))] > + fn new_uninit(flags: Flags) -> Result>, AllocErro= r> { > + let ptr =3D if core::mem::size_of::>() =3D=3D 0 { > + core::ptr::NonNull::<_>::dangling().as_ptr() > + } else { > + let layout =3D core::alloc::Layout::new::>(); > + > + // SAFETY: Memory is being allocated (first arg is null). Th= e only other source of > + // safety issues is sleeping on atomic context, which is add= ressed by klint. Lastly, > + // the type is not a SZT (checked above). > + let ptr =3D > + unsafe { super::allocator::krealloc_aligned(core::ptr::n= ull_mut(), layout, flags) }; Personally, I would rather import `krealloc_aligned` and not have this weird formatting here. --=20 Cheers, Benno > + if ptr.is_null() { > + return Err(AllocError); > + } > + > + ptr.cast::>() > + }; > + > + // SAFETY: For non-zero-sized types, we allocate above using the= global allocator. For > + // zero-sized types, we use `NonNull::dangling`. > + Ok(unsafe { Box::from_raw(ptr) }) > + } > +} [...]