Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1398790lqd; Thu, 25 Apr 2024 14:34:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUmPBN0AhtSHyozv4PmB+FO+qMzq/QntwdZUhA7djViS6TXq9WDZYdYrKWZhtDs8Cm7uCxD5Vae7ASxwkOGJHcvU7M7ufd8fD4vO12a8g== X-Google-Smtp-Source: AGHT+IGqK1LsvHI2iDPztxEny8/h/cGSiZ1hUPIFPinTei30QIfeYGM37xSV5lXnyA+a82eLcXs/ X-Received: by 2002:a17:902:ed85:b0:1ea:1d2:90bc with SMTP id e5-20020a170902ed8500b001ea01d290bcmr754913plj.24.1714080897736; Thu, 25 Apr 2024 14:34:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714080897; cv=pass; d=google.com; s=arc-20160816; b=gRqCRIL7xKhLz55xBBsal6hhfwolT9CzH8HkE1B22hLO/RYkzH1uXnozgwLjFnKisA xPt94J5jute4z6gnie0bsx1WxTxejSrb1QiyK+fBa8cMpg+r3nyuuACvJbbiPmnrI6KZ gwBRVsT8rW3oraRX8UU9n/Qg53HYVzHtO2CNYdiuF09PVd3DAT0V3t5pb6FH0Abn7gFd iDXXa+ZUuFQxam5IPlxTd682ET0fdYLiZjfOCweikDMQE5YT/BM67ySlsqAMCx00ohXV lQNsDe7hNWOyhFNQd7fgabRqhRnf0SHq34ouEi88eVA1/0NQq2uwkWtSHk7pHol+9THM Lalg== 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:message-id:subject:cc :from:to:date:dkim-signature; bh=utx6L/Qp4Am0TTK7zosCdDO/Mu0LhitYxGbkj94CChY=; fh=nEaETDv4M2nasq5JfLeaiASHNvZWcEwQ2im83YteH9I=; b=PdXS0sS95N3mD8OuiY8sbfYu9tgVqN+68y7yIhcX8OdtwGeJD+Uy7XRN3Atg0WUDAP NR2m7JKZY2Y5R2hZe9BXiBps1YqnlFBXn2zXQ/kB2Qz/GD1h30T6+Zm2Msc3zLlU6fR4 Yt29ZDbQ1CMgxjDZj6GtrtqxjJo4lBQZla69IRDt5/gj6aDUsZJik87sL9inOrqIVYTh ZV06CTlSbrn/CDScU229JBcFn39RErlNYaQKwWVCY2YbKOrOALL02TI1D3F7N6ePc1pF TLSHf6zG3EtZlP63FMtSuyObkTsHKaB/PQjDJxIqAnQYEvhZQIZRcKQcKD8wCDgWCedX oWdw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=IFOUnr9Z; 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-159211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159211-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e12-20020a170902b78c00b001e236d13214si13834261pls.175.2024.04.25.14.34.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 14:34:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=IFOUnr9Z; 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-159211-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159211-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 4E898281E39 for ; Thu, 25 Apr 2024 21:34:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77F3B155751; Thu, 25 Apr 2024 21:34:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=proton.me header.i=@proton.me header.b="IFOUnr9Z" Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) (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 10133153812; Thu, 25 Apr 2024 21:34:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.70.43.22 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714080888; cv=none; b=Y5rbsX5i/YXaqXcyQ8o5WBm15Lg+9Zu9z28cIfgEMmA8NJoif+50ZimniOrsqyCHdgbxQQna9jh8pSPdr0GUfWeGDGdx5m+cO4ok6KqBYvK+Kjtgn18RleUfzI78rq5iFNPzkI6pAELyj4YWne2f+47RoaNn8dZxEgT7yOm+9E0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714080888; c=relaxed/simple; bh=1jEwCYzNB8KZbUyYi19vFP2ySyNVjrczet9hqIFk0KA=; h=Date:To:From:Cc:Subject:Message-ID:MIME-Version:Content-Type; b=M4X9Gif14XQBh2u16fmbOAyInSwD+6+8SnAJyJD/qAaBpJDXeSkpeBgernx4ogNoL7cKKo1FV8PXmjpQ/8on7IdA66tVPOa5Rhs1ZrIa6nfphuVJPblK1FcC9SEgNjDTEwv8TVcPscQZ8tVc8jXA/Av+xXdFkIrpRC4m9KWhMnc= 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=IFOUnr9Z; arc=none smtp.client-ip=185.70.43.22 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=1714080885; x=1714340085; bh=utx6L/Qp4Am0TTK7zosCdDO/Mu0LhitYxGbkj94CChY=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=IFOUnr9ZehTGA8hkvg8hxUk6wygwVMDLyz/NoqDhDKia/ZLgpTsVq03UlbXfD5G5H zVIasK8El/NVnbd3SxIVm0hRIHHp1JmCot+Rs6oXEm+ETdiv43FJkSUj10bfudooC8 0ZjN703WyJtEKB7XPHfK6SyHj/bwcRP3kXtAd93GFyxUzgWkXI1FaBjqKYAfI7NQrx 44x5CtJzWflaZW4iEvhldAoLF5tyUgqtZLUto//tvDTHi7Ccz3GbU5a3z3oNAVwc4W 95Q9SQDZV+T3O+8IKocU2edXYy2M5mTcB5/059hGXaGm/Eh1uJqBAa2ZaSdJGBEpb9 tetH8mslPmbvQ== Date: Thu, 25 Apr 2024 21:34:38 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] rust: kernel: add `drop_contents` to `BoxExt` Message-ID: <20240425213419.3904105-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton X-Pm-Message-ID: aaeb4b917333a0c3cb20c9072f8f6dfc323866c0 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 Sometimes (see [1]) it is necessary to drop the value inside of a `Box`, but retain the allocation. For example to reuse the allocation in the future. Introduce a new function `drop_contents` that turns a `Box` into `Box>` by dropping the value. Signed-off-by: Benno Lossin Link: https://lore.kernel.org/rust-for-linux/20240418-b4-rbtree-v3-5-323e13= 4390ce@google.com/ [1] --- rust/kernel/alloc/box_ext.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs index cdbb5ad166d9..3ddb353b776e 100644 --- a/rust/kernel/alloc/box_ext.rs +++ b/rust/kernel/alloc/box_ext.rs @@ -5,6 +5,7 @@ use super::{AllocError, Flags}; use alloc::boxed::Box; use core::mem::MaybeUninit; +use core::ptr; use core::result::Result; =20 /// Extensions to [`Box`]. @@ -18,6 +19,18 @@ pub trait BoxExt: Sized { /// /// The allocation may fail, in which case an error is returned. fn new_uninit(flags: Flags) -> Result>, AllocError>= ; + + /// Drops the contents, but keeps the allocation. + /// + /// # Examples + /// + /// ``` + /// let value =3D Box::new([0; 32], flags::GFP_KERNEL) + /// let value =3D value.drop_contents(); + /// // Now we can re-use `value`: + /// Box::write(value, [1; 32]); + /// ``` + fn drop_contents(self) -> Box>; } =20 impl BoxExt for Box { @@ -54,4 +67,12 @@ fn new_uninit(flags: Flags) -> Result= >, AllocError> { // zero-sized types, we use `NonNull::dangling`. Ok(unsafe { Box::from_raw(ptr) }) } + + fn drop_contents(self) -> Box> { + let ptr =3D Box::into_raw(self); + // SAFETY: `ptr` is valid, because it came from `Box::into_raw`. + unsafe { ptr::drop_in_place(ptr) }; + // SAFETY: `ptr` is valid, because it came from `Box::into_raw`. + unsafe { Box::from_raw(ptr) } + } } --=20 2.44.0