From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 1F08B167265 for ; Thu, 11 Jul 2024 16:21:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720714871; cv=none; b=Xhf+15/mme9oERBGlFgX5/EMhUkvNKKa5LxRRT9vXKwdkXHVUsmjlbZ54UydjRuGGCU5OFVVJN2ekSVDRhwP8rd7G6C433O1uZNMks5aTc0D9kBh7JjC2VuMkJMeEkXmJDO9fqM+7Mxu5Qh+naphqVV2WQysg1BIJ6bZ3kaebMg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720714871; c=relaxed/simple; bh=l4drfiF/RmBw22akhuvPKaORF8RgC3jLRDuFcvLUeL8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jgxQEwhX6DtyGqzob1j2B8GSsOEJjZWsIBrneBt8ID1WscwCyTiW1E/x5XtOEkzvLD3n2zrwfofQcJY0xPOn3iUGDX0s47JxMgJrgbxxTAU17VgB1YBu6EFH6Op4BRNrt0nL45V0ZjgkUqzyDdFboc/WqSPypbsCqAIq9WPU7vQ= 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=cUYzuRwa; arc=none smtp.client-ip=209.85.219.202 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="cUYzuRwa" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dfa7a8147c3so1876571276.3 for ; Thu, 11 Jul 2024 09:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720714869; x=1721319669; 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=tdAfz6F1LxB1SVILBmz3im7GucsBrJFjA6wosQSGALA=; b=cUYzuRwa0xSek8kqHb+eRLvj99vKB9FJyTsFq9DhB6TMquvOvBwDJfxlzMscZuvG// +y/z2Od9AMvsneriWYKQmpF4bLbu1FoEvZK8wIQLUWzCMfvlv+w4h1S6QtKKx44FbLWJ t0VItBxJXmXCusWkNekzkIK2i2oOIhml+NpFvKMX3/sQwRdwflVSnabjTwU+ba3yOnc1 jWp/DmXPw7kLiLfJo/VtQngz57fV2zFrAaLQLRStAeoulUoPF6g5ifoa1OsICrq3nxCK gxTjkJk27ZqsgBQffkhPuN1u6Z6HV+IQCVRsSDQwSqC+snA8MKa3SACfCw8jazGG32tE x8zQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720714869; x=1721319669; 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=tdAfz6F1LxB1SVILBmz3im7GucsBrJFjA6wosQSGALA=; b=nUR1QdcES6VBTa1hlbvrAAzmD9O0NWoaTkkTCnN+C+RGN/OQmLUmHqHmIa8tts7vv/ V3MO6rNLf0o7CXFmjUYjBbJz4E/DUrS+rMtpStUPqlOpMhtCqR+lrUvehAsjBK4Zgnye RaNM8q26j2rvujgwD8JPAqZDSQsI9o732W3PgugCx+MSMxujvxxM4dkxLBlSLG+WD4mA 1KE1S1C148j+volqwtLY6a+FdjWuDUzaknSOM6SoEJdvjazqX+kmgRAhBIYWajGV4zwd wyNe5AzL8aQq+q7IoDCxPELv5wvPNbi1c0TqUlrHhSmdcBRrfyTpru5s6Z60TDer42xo kA8g== X-Forwarded-Encrypted: i=1; AJvYcCUbnNYQsGebsJZZ8Y/tr2Lft11hDtmegsfLyk3q9ntR3YErtAZSq3XWFIFsdgvW5RFKylFFHJs8HzrCJcXQYlk+n+lmEMgXNU43h4Ub9v0= X-Gm-Message-State: AOJu0YwefQJE4hSZZZfnRIvzEIp/r6fseBW+8OBGEAMaAAhIZCiQLb1C 9eFpdfnEMQuBbaANBYqX1RLV9wSrSHyoF8C3Wn5NIffYIGDtElxWGCpab+IBLR98hPuq8H4JOXv niIU8nIozamF3z2Dy13tjzBllEA== X-Google-Smtp-Source: AGHT+IFSs76JHmM2LmXNg32ck/ZKUQAWtLq3qigWNXo0ZTjrIc0iQb/8PWxfHMJzU/PwDwBuVbLVyBlgavSzN3LzrU8= X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:200e:b0:e03:a2f7:71d with SMTP id 3f1490d57ef6-e041af3f7edmr322987276.0.1720714869205; Thu, 11 Jul 2024 09:21:09 -0700 (PDT) Date: Thu, 11 Jul 2024 16:20:57 +0000 In-Reply-To: <20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240711-b4-rbtree-v6-0-14bef1a8cdba@google.com> X-Mailer: b4 0.13.0 Message-ID: <20240711-b4-rbtree-v6-1-14bef1a8cdba@google.com> Subject: [PATCH v6 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" 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-323e134390ce@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 @@ use super::{AllocError, Flags}; use alloc::boxed::Box; -use core::mem::MaybeUninit; +use core::{mem::MaybeUninit, ptr}; /// 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 = Box::new([0; 32], flags::GFP_KERNEL); + /// let value = value.unwrap().drop_contents(); + /// // Now we can re-use `value`: + /// Box::write(value, [1; 32]); + /// ``` + fn drop_contents(self) -> Box>; } 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 = 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()) } + } } -- 2.45.2.803.g4e1b14247a-goog