From nobody Mon Feb 9 09:32:53 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 DE0AB13A899 for ; Fri, 16 Aug 2024 20:48:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723841286; cv=none; b=LSVHZRM8If1StyWKM6k/GYiiJHfLAfj7bMl7MvvzlR2PcSx/CX201wRpmfO6h+1NI2BrVsp4pPEqUvmXH6WOjLNCw0BnTUs4PssQHudwonO8iZx0DU8W/JBoEDk+zvPbhmVK9DqIAtdW9kNXwz1U1b8Z1xjc/KCy0217gvS1urU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723841286; c=relaxed/simple; bh=BI8PHNJTfXcLO+UUAm58BuWx0Vjek3ATKPy+xwLajGs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BfI7gBcmH1QbyTyC1ySGL94vKOCOgM7bLvFZw8w5tmGs7oG5PTkNhQIUCOOj9uSb/ykWmHLp2PL/+dJcPKYxg8RRh7HedawX0nt63d0ngxZnKWZUtXX4hvoAjHKZFULn5Q9cWpIxg2s/D3vIx7xa86wzplrwxCfkzqXEbD1X7z8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=car5xSHF; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mattgilbride.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="car5xSHF" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e1159fb161fso3517515276.1 for ; Fri, 16 Aug 2024 13:48:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723841284; x=1724446084; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=6UpT8wOwNJORmlebbf/8Uat6NCTU+oJ243lKTzUmD5Y=; b=car5xSHFH3i0Cq7jF+LWSeABplesvyugBes6LyAKkfoRBX8TS0UGHRdTDTA/f41dil +6auscWedm984lycttAQaMZVGgpyVmTEReicovmu+R6dIlLwVavp8pwloSle66fn+Vlz aMyrPURvIPKpGk3qsPqs3kNkrd0Uwd1EucsSplDTkWZ+I8x8JjK7e5dvpnkZG6qgNqd5 3YvCqf19v9K/u5RzHw5brXurXc2trRkyvZFKUxjswmzCji6eZoT3/z7NYEQ7k/rqKPya xqZZuiEt42Jx4Vx0mbPHBGGYkZbk9gRU56s4d3ZUXz8zgkLkXKDA8eeQefRD7Ffbm72d Ybvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723841284; x=1724446084; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6UpT8wOwNJORmlebbf/8Uat6NCTU+oJ243lKTzUmD5Y=; b=aQyHeaUKocjN7amRrykrpuiB5GfKvD7KuAmmHejPIp9MdzEFcT9tAoctAiFPrG6GTO M4IVf5/J20FgyA2aD70+zkRY3k6TSQPqoha5BB1VnYA6HYwisC7FYddKB0BYbOy/MHjM b3wfllqOpPnzLNFydQytQM6Bqmr7xBGxvoNWzl4qfxKbNzOxJwQj6+dEN6OiR1/jzla8 NBkoNAQ4gEIhW+YvrJ2NBtlYjhJSvFsjYpm/bwsY2iKv6fCMZq1HsWouat1dsKo+pIeJ j+cr6eb/PgVbYU1631n8gr9hQHgA1ZUG3yg/ppScmc8oKrMnPFZFJb+7r0zl/i6Y6ZWI qWQw== X-Forwarded-Encrypted: i=1; AJvYcCUmaDQI9GLma8ff9btHvxHSqIxC/NeZYvKD6Ao825j9xN0BHdk17MhicgsExwrEr3bo/4NWGw+qJtjgLZI=@vger.kernel.org X-Gm-Message-State: AOJu0YxMXVlk2+cVqMliQpRE30O+BgicihzV7Z+EpQTU/d0qRcjVzi0T LDSCNBaVcb3LNcuv2zBbLXhthsg5tJZHBkivw82QlwlSc7DYMzbQPSeI32nwYBdpl7eAizOtJJj qeL7BA0DbZyKZD3hLseK/wqW49A== X-Google-Smtp-Source: AGHT+IELGeBoRm8VfWBBL2EoMJHsshIWLt/5bDCdxr1SXybvD7gnLyzHvlUcyMT2Y8qs0F5lik0YliPldIynDvQbPT0= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a25:910e:0:b0:e03:59e2:e82 with SMTP id 3f1490d57ef6-e1180fb2b4dmr6070276.10.1723841283714; Fri, 16 Aug 2024 13:48:03 -0700 (PDT) Date: Fri, 16 Aug 2024 20:47:58 +0000 In-Reply-To: <20240816-b4-rbtree-v9-0-425b442af7e5@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240816-b4-rbtree-v9-0-425b442af7e5@google.com> X-Mailer: b4 0.13.0 Message-ID: <20240816-b4-rbtree-v9-1-425b442af7e5@google.com> Subject: [PATCH v9 1/6] rust: kernel: add `drop_contents` to `BoxExt` From: Matt Gilbride 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 , Greg Kroah-Hartman , "=?utf-8?q?Arve_Hj=C3=B8nnev=C3=A5g?=" , Todd Kjos , Martijn Coenen , Joel Fernandes , Carlos Llamas , Suren Baghdasaryan , Christian Brauner Cc: Rob Landley , Davidlohr Bueso , Michel Lespinasse , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Matt Gilbride Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Benno Lossin 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] Reviewed-by: Boqun Feng Reviewed-by: Alice Ryhl --- rust/kernel/alloc/box_ext.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs index 829cb1c1cf9e..6cf79f96d6c7 100644 --- a/rust/kernel/alloc/box_ext.rs +++ b/rust/kernel/alloc/box_ext.rs @@ -5,6 +5,8 @@ use super::{AllocError, Flags}; use alloc::boxed::Box; use core::mem::MaybeUninit; +use core::ptr; +use core::result::Result; =20 /// Extensions to [`Box`]. pub trait BoxExt: Sized { @@ -17,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 { @@ -53,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.cast()) } + } } --=20 2.46.0.184.g6999bdac58-goog