From nobody Thu Feb 12 23:08:04 2026 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 9383A199396 for ; Thu, 6 Jun 2024 14:50:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717685424; cv=none; b=aiw1rJfkaSoItIQOVFYJMBtR7nXMyyTgEZx5C8fiOEh162FxVP/JSkttfJ1Ol6IAKuGP/KRajBMUibH5K2kLw3GjoTBwkuwYHmYrezEWuXbskv/IE2emTxpSOhXLuJkvixLBt/sxpRABsSTI+e1pSq8xH/kwSNC5iJTBLhpOqGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717685424; c=relaxed/simple; bh=6KVODQnYtWNQoMLw85R2yUtqc1xp53xILtMXwocmZ5I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jtIdMbc0cQIDqdolQTTcPpFhQRXIzjDLvjU87A1u0eCf+N0sUbyENT0aDAansMWk8nllYbQa2YqdDU/tEwXregHiPG/GPOJRE2mx1i1axrBI5f/45bCm4NY/HKVKcZpDDgSRWtAI7PeXTeAl8dl+udwuQxueGokqxPGwgFC10UM= 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=V8tpqz01; arc=none smtp.client-ip=209.85.128.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="V8tpqz01" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-627e4afa326so15878747b3.2 for ; Thu, 06 Jun 2024 07:50:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717685421; x=1718290221; 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=D83xpw4zc0nTnhAHOcVsYRJWLRnvj7gUU4+SqE71h3c=; b=V8tpqz01VBw1o6lsZL/rw9ymWya02o6z2r6MtsgVpvnn3oTJq1j3p1Y0lfHud8BzT3 xxxKLeyat7GpL/eyLA+KpwQRXMW62OLYneRX5LWghnRU638TaLjKyvETDtOAde5prLHM q9XcxkvYL4ca48RY9HTpz2RFKtJcWKwIzMf9C9JiE4CkU0Um2mNTuiikYyJiSOZZ2nG0 alUiF+hdZ40oBWcwmK3m/xGWdHW+PL0jBkqv5Wonpmi4N/wNB0juq6l575LD3wQ6qGgd LDntH9q6NV1o30fnb7OMDbf4Bihkagt3w58/yqLzAgPcvENOJbX0oY2PbLyEtG70W0m8 E4tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717685421; x=1718290221; 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=D83xpw4zc0nTnhAHOcVsYRJWLRnvj7gUU4+SqE71h3c=; b=EPhPOhE3wtL4iZRMziWhHwicuIfnWgTn0nEEi6gb0ZsojmK5ZvAcxVeLzMHPJsyx7v B9kRshNO9vIW1ktLAxhBWBGaKFgy6fSZTBxu/aHdyYvBqNrEly0g8uasFo428BHQBtAy t2hWFA7YJhST8OqHhlxwy4Gu0asSmqlWS7uWIJaI7YDxfxy6G0I56arSh0VhmYFfs5fT 0XNZaNwRN+JtiU0+jzd4zj5377yGciHeBT5nylPIDJC8oiu00c5TViHVBefs+L7/Gf/y f/tKWxX8Sqm3W/kWvCm064jkiEr8s9KStU0DwbTI3KepaSK8hZ5t7yEsSTYgTXix4juX JdCA== X-Forwarded-Encrypted: i=1; AJvYcCX7KrzTOPWARQ6asKYdAlcevsNfX38FgrLPxW5MHAZRb+9iQgf1yut13j59cJU9w8T7HjSoymx0qqDG4lgC1qUw8BPjdIlPNTl+DRyL X-Gm-Message-State: AOJu0Yyeink0ziY9iJ0Io/7KqmY2PaW4sNheWFnMGoU6C8Voqxuu4pJu VipEMAJgbGG3VxuPh1nCKnpFi8zaJjN3CP4nXmpUff9Ozx8P+l5idTBgD3TzWD+kjlxOSI5efIz gf8ww7sadfQAqpvwa+GzHUDJJnQ== X-Google-Smtp-Source: AGHT+IGw3wLc7XnxXpEgcjIpc9GAoF7EZlm4CA11KQkZmy3hezSnaWe5GPHz2Zn7qS7Ou2teHPex30H7A4lXSUxWEmE= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:18d4:b0:dfa:56fa:bb52 with SMTP id 3f1490d57ef6-dfaca7df41cmr347156276.0.1717685421503; Thu, 06 Jun 2024 07:50:21 -0700 (PDT) Date: Thu, 06 Jun 2024 14:50:04 +0000 In-Reply-To: <20240606-b4-rbtree-v5-0-96fe1a0e97c0@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240606-b4-rbtree-v5-0-96fe1a0e97c0@google.com> X-Mailer: b4 0.12.4 Message-ID: <20240606-b4-rbtree-v5-1-96fe1a0e97c0@google.com> Subject: [PATCH v5 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] --- rust/kernel/alloc/box_ext.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs index 829cb1c1cf9e..557895db4f48 100644 --- a/rust/kernel/alloc/box_ext.rs +++ b/rust/kernel/alloc/box_ext.rs @@ -4,7 +4,7 @@ =20 use super::{AllocError, Flags}; use alloc::boxed::Box; -use core::mem::MaybeUninit; +use core::{mem::MaybeUninit, ptr}; =20 /// Extensions to [`Box`]. pub trait BoxExt: Sized { @@ -17,6 +17,20 @@ 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 + /// + /// ``` + /// use kernel::alloc::flags; + /// + /// let value =3D Box::new([0; 32], flags::GFP_KERNEL); + /// let value =3D value.unwrap().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.45.1.467.gbab1589fc0-goog