Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp136686lqz; Fri, 29 Mar 2024 11:00:17 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVSlt+0kpvcshqj/R7zNwpD9YMAMCIwwk1IIPLVL1GXljGBKLVhxgvkm6ohT1CBq6rGM8jnDjc8HYnsabUzRNmbSsoQ61fKzLrWNovV/w== X-Google-Smtp-Source: AGHT+IFD7pZoAOTXLWcXhNPmeWpTfUFTIB3cqCoqd5XMaJWUm0Fg9I1iE8RdZW156ANS/azB97/E X-Received: by 2002:a17:907:7251:b0:a46:7c9c:10d0 with SMTP id ds17-20020a170907725100b00a467c9c10d0mr2458839ejc.23.1711735217728; Fri, 29 Mar 2024 11:00:17 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711735217; cv=pass; d=google.com; s=arc-20160816; b=ZjseOIrx9sK3yLx7vapbOM50UHo6u5RUdkq3NCJhDsJbL7Jl69fi6PvBCltHSDquvN GEkUiqSOAaBV+H0XJ9znGu7gVx95NfXoksdoyfMZZ0+pGgytX0MdPqCer1Duzrn1FIRb pf91JQs7Ra94I5HSb+jqHxDECqtM+3/lLhhn1/4AenQYl9hypEryXLMSL5qZemAn2Kky iGeBLdcqtLcJoT1vIsMkHv6G+Csn7suGQadIhuDULlOlfaMNkvmKss7xFreShqmkdZUj CzZHAw52LFIm9bZU9wjDSaIg2H5RQTH8yeZdmLwuVRKxoSh8tQtY5/rGRrJ0bjt9KkGk uLzw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=KvHT6XIk5yf7N2YmNeAfioG0CmuxxDnTNjG9kq5b64A=; fh=wDHPZxkvVqsdXzv6UUp1bLnoC/7kVN6CgmK/L2NEby4=; b=Vz76X7MJceS1Reoh4DGzyRUxVdUTtHt4Q4BT3G43SP/gjMsaW5L4GzuIFo0X/qALSf 9QSgy99T1bWsVVyBPmFBDIoeXf34zcYD2P9n17FnTomOaqpUON4eYpHAO43HPhO2ROMx vpj1ttSyr6i7iizT/7cKQw95kWeM81jH91MOlrGQbQNuUZwVfcqp9B5tHufjO+8mAted YZbF0lSDXZFDzkAV/sNmeTIOwkmbZCnlXYMPRRhesJYcCcU/h33dZ1NdBaPi6Rafs/Sg sR3TWL4Iy3A8GjvMC8eHltTKGAkb0ZpgcAo/lkOgIugijWu+IM9hYkKEXPtDr325aOki SU0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bOUarjed; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-125267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125267-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id by9-20020a170906a2c900b00a4e2d05ca2asi1209504ejb.1006.2024.03.29.11.00.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 11:00:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-125267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=bOUarjed; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-125267-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-125267-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4FD501F22957 for ; Fri, 29 Mar 2024 18:00:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E7AC137774; Fri, 29 Mar 2024 18:00:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bOUarjed" Received: from mail-yw1-f171.google.com (mail-yw1-f171.google.com [209.85.128.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4EA6F1E893; Fri, 29 Mar 2024 18:00:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.171 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711735207; cv=none; b=U9TwkIYrYNO+sSwAzRmI43CtOIfNIbKuxgjdNKUNALzGg78Mx++pCR/qWsmYK2CvC60eoXcWoey/N/Or7gKolt8y5jctqcJD+gheWimZ+RFgMxA0oE7ndh6a+k6pk1snlVKbK1e/UrmNW5uH8V707EwGwRbUtvL3zZ+CnBD6vTI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711735207; c=relaxed/simple; bh=emlV9Q5PjY09S2AGsJFyLMrHOzGU+5421Us+5zLpyEU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=bLra/OaPdJF8eyh8eUNpx4Yd5e9Lqio3vAfpwm05YnM98eIXbGUa3EkIYSQU0SIsE2MqSyFj9kNPulf4dbTSTYv878UBQer2FU238X2zikv1VQy+aOs9l1Q6W93fZcG0AOUPNL7U/TXHPF76Nua8o3GEU8rCBP4JfCuEy//cmOo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=bOUarjed; arc=none smtp.client-ip=209.85.128.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-f171.google.com with SMTP id 00721157ae682-61444fb1d2bso6737797b3.3; Fri, 29 Mar 2024 11:00:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711735204; x=1712340004; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=KvHT6XIk5yf7N2YmNeAfioG0CmuxxDnTNjG9kq5b64A=; b=bOUarjedbVsJ7otCl8/tWRd3QSjRriMmpN5VY8RDAIvnXfsXcKpHI5EPHY6rbkENIS kZy8cwt3hCy6otd4Jt4BE8XpIi/kpgersG+XDLZpsr76ndbxiiP7uhBFPTSx5KPAfRrJ oqyL1NZfhGCxtLWqs6KippMasj8dLIGUW1FyryvsnYe6M/bAMlf5TN2LA1RgCkZ3zavA m/fFK76T7DCowd9N0/H62ac2X4B04Pzus4c4AXYD51ID66RmjGqs5xU6L9ck30crfQMK AU9Z/cCqhnTEjZx5xfwVBH8V89crmV4V7wEL9Du032PR5+cpETFZSRs0KMVvOUNu6Jcn mmDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711735204; x=1712340004; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KvHT6XIk5yf7N2YmNeAfioG0CmuxxDnTNjG9kq5b64A=; b=vl2YqvuvqEaTEt1PLw4Sgy532trJ01yu4a7HBKdheHykt2ZpzpzysWKEqMgkYEFNvn WpAKwToQhsQEF7vYyeDvulJ7Co07IjuZ52ijZ9GFOxTKwxKkOtRC6NeKm995nVvWKwiL LGsRrj255a9iOi/7M1QUSEZAkUhB7KJ+dnmF/O5AyPKNIR19rxf4a8YmGtGu4oIWiHEB kAa+sf6DYz54ZzCIQEb4HEftXy1IqwwUPpeO7LvcOlq/HBMGM3XANdfIPOpODziBfCt3 MyC8K09HZnqS0/YPSDovNKPnW79JkeooWvVlLNGtyRTmbbRW4LPXA5ehPHyYPlN1O6xz y66A== X-Forwarded-Encrypted: i=1; AJvYcCWX28UbCWKos/CS6puojotiUoVGgCzNjdtTDAoi14CHB9rn1olXZd5pBD9yf/BhAlIYqfvCpgc9ur9AaK0EVv81yQJflzaNhhfJHpTt X-Gm-Message-State: AOJu0YzQcpovuuyRfXYXqpjw0yJi2dFH5ukDjoDYKxnG0nBreyl6lTUr IQnLx0ioydw2Pip2nQXcuZn4oWq5n2fGL8UEBQ14YaBpQIsAffmFUDnhNKc3 X-Received: by 2002:a81:6c89:0:b0:609:7055:f725 with SMTP id h131-20020a816c89000000b006097055f725mr2928694ywc.18.1711735204101; Fri, 29 Mar 2024 11:00:04 -0700 (PDT) Received: from fauth1-smtp.messagingengine.com (fauth1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id c13-20020a05620a200d00b0078a390fb634sm1529171qka.47.2024.03.29.11.00.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 11:00:03 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfauth.nyi.internal (Postfix) with ESMTP id 3B7191200069; Fri, 29 Mar 2024 14:00:02 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 29 Mar 2024 14:00:02 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddvvddguddtlecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhephedugfduffffteeutddvheeuveelvdfhleelieevtdeguefhgeeuveei udffiedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 29 Mar 2024 14:00:01 -0400 (EDT) Date: Fri, 29 Mar 2024 10:59:18 -0700 From: Boqun Feng To: Wedson Almeida Filho Cc: rust-for-linux@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , 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: References: <20240328013603.206764-1-wedsonaf@gmail.com> <20240328013603.206764-7-wedsonaf@gmail.com> 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=us-ascii Content-Disposition: inline In-Reply-To: <20240328013603.206764-7-wedsonaf@gmail.com> On Wed, Mar 27, 2024 at 10:35:59PM -0300, Wedson Almeida Filho wrote: > From: Wedson Almeida Filho > > 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. > > They also have an extra `flags` parameter that allows callers to pass > flags to the allocator. > > Signed-off-by: Wedson Almeida Filho Reviewed-by: Boqun Feng A small nit below.. > --- > 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 > > diff --git a/rust/kernel/alloc.rs b/rust/kernel/alloc.rs > index fd5541991a22..9bc1b48b5641 100644 > --- a/rust/kernel/alloc.rs > +++ b/rust/kernel/alloc.rs > @@ -5,6 +5,7 @@ > #[cfg(not(test))] > #[cfg(not(testlib))] > mod allocator; > +pub mod box_ext; > pub mod vec_ext; > > /// Flags to be used when allocating memory. > diff --git a/rust/kernel/alloc/allocator.rs b/rust/kernel/alloc/allocator.rs > index 8cc7099d6ae1..ff88bce04fd4 100644 > --- a/rust/kernel/alloc/allocator.rs > +++ b/rust/kernel/alloc/allocator.rs > @@ -16,7 +16,7 @@ > /// > /// - `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: Flags) -> *mut u8 { > +pub(crate) unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: Flags) -> *mut u8 { > // Customized layouts from `Layout::from_size_align()` can have size < align, so pad first. > let layout = new_layout.pad_to_align(); > > diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs > new file mode 100644 > index 000000000000..c62db0aa4dd1 > --- /dev/null > +++ b/rust/kernel/alloc/box_ext.rs > @@ -0,0 +1,60 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +//! Extensions to [`Box`] for fallible allocations. > + > +use super::Flags; > +use alloc::boxed::Box; > +use core::alloc::AllocError; > +use core::mem::MaybeUninit; > +use core::result::Result; > + > +/// Extensions to [`Box`]. > +pub trait BoxExt: Sized { > + /// Allocates a new box. > + /// > + /// The allocation may fail, in which case an error is returned. > + fn new(x: T, flags: Flags) -> Result; > + > + /// Allocates a new uninitialised box. > + /// > + /// The allocation may fail, in which case an error is returned. > + fn new_uninit(flags: Flags) -> Result>, AllocError>; > +} > + > +impl BoxExt for Box { > + fn new(x: T, flags: Flags) -> Result { > + let mut b = >::new_uninit(flags)?; > + b.write(x); > + // SAFETY: The contents were just initialised in the line above. > + Ok(unsafe { b.assume_init() }) , since we have the new_uninit feature selected, we can actually do fn new(x: T, flags: Flags) -> Result { >::new_uninit(flags).map(|b| Box::write(b, x)) } here. Regards, Boqun > + } > + [...]