Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1064606lqj; Mon, 3 Jun 2024 09:07:31 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUvmdK87SquuEGJOfywNbR3EgJIHgRnABrc0dknzVFk7ame2f2PZjYyKgUcmY2ezHh4QbXEurWUIp9cI/+d8/jj2UXjv/wFaWClL+sNSw== X-Google-Smtp-Source: AGHT+IG3fjIZMo/By5i0z4iezuOkclYY+vLdFYjrFwheFbPZD7nnby8lZfbmbYP7Bwgw75gKaO7q X-Received: by 2002:a17:90b:60e:b0:2bd:b21d:cfd6 with SMTP id 98e67ed59e1d1-2c1dc5cce8emr7960209a91.33.1717430850842; Mon, 03 Jun 2024 09:07:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717430850; cv=pass; d=google.com; s=arc-20160816; b=ot4fb8DYZCdpP31kBib/sl6o+LcchhfO98MJiFfWn6h1WENTie9SVIG8K9Kc9iTkiL W2Ct3b3nn9TGLEk/OgYpaFtLkOezLmzBnLi9Tyj8Vu3CcvcyTFSWZzAJUGpJP2XczpZr 2OHp40LwshszLMD4V+kRsjvussxATX8E0LjR6BqkjsoZ/vChwDEkHGamiJW2/U8oM/pK 9GyvD8n3mLKyI/QqOM2VDsVR9/pvDOnZGv3QXgIkJe8Yz+fQAP7knRPEeXw7FUIlYHYB RW4YZepBx9V7DV9MV+4SqRNykF5e3E5GaETYd3V52cRf46Hs1yNcwFA7zefi4M02RNAT bVuQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=dXknpeAcJEK6e2DRJM/PLI3NtocdR4jywa4B5EX8TU0=; fh=SnwyHwzRXCFZfwBpoSKkf7Hl8amw9KEVKgOU7UOShLg=; b=hJ+Q2/NfjgoTYUuFZ6Db35KK9qXF1LLzO/04E5+C5NEeYHAVw+oy46WOmOrKaW39y+ +t3Y+LQoZSvQIez1Ue256odjmMeQDnfhiRyVIIHEwrUMCeTEtAkSUcsiHbjMrMRPa5Sp 0rSKPDI4dw7i+o0aATOj9g/jVR+pO93qK6JwK8t6zbtbvTnSAkUh7DKfHV522G0ZYtSB aQHK3TdedNHdUSWzZ8u+33XAIL+B7SeNfEkjL0dmLP008C4sCCZBOFqCzyj2KBG55nWh JlS3byXIHENhKQsu5WWkLz7CSA0UAK4PYcCj9UCa63Kkr98MSShF8BktGSdWbpz0RTSs eDag==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WS+qJ4Cw; arc=pass (i=1 spf=pass spfdomain=flex--mattgilbride.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-199407-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199407-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2c1c2852a02si6591682a91.189.2024.06.03.09.07.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 09:07:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199407-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=WS+qJ4Cw; arc=pass (i=1 spf=pass spfdomain=flex--mattgilbride.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-199407-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199407-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 754CEB22E96 for ; Mon, 3 Jun 2024 16:05:45 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DA91913440A; Mon, 3 Jun 2024 16:05:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WS+qJ4Cw" 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 90785133993 for ; Mon, 3 Jun 2024 16:05:27 +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=1717430729; cv=none; b=pxBRdnDkwm+A91TlwZHyJgmnnKkv9B6KM82ss5JCNq9QN57xUbNlcGxetoBIbEq2GQL61yJrY9rn+47AkDqC6uJqbiv9NbwOETLYKrZa2kM+ga9e4o5bmz5lRL2e+wgi6ptZSgnnQ5mFH5sjW46kADhGeuyrCDTOA2VA4zlaC38= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717430729; c=relaxed/simple; bh=SsPguepMYl1k5J1fgsDrPhfob8d08e5oz0Kj/0ra8gw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qz8HkwBZdTEfQ+extvRHwMu+SIYq5zK8dbQlVLMbzj4HIn76qyOfbd0smZqqgTrDnAdc88kkiekf/9dMiFGbNYcUAtFT07ZAiunjQyVjhYu8tK21CYjasrz87CNr19ZGRYqOdUvjG2npseMcYiVmbzWrJ3ZESafJGwzGNq2B+RY= 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=WS+qJ4Cw; 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 Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-df7bdb0455bso6963811276.2 for ; Mon, 03 Jun 2024 09:05:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1717430726; x=1718035526; 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=dXknpeAcJEK6e2DRJM/PLI3NtocdR4jywa4B5EX8TU0=; b=WS+qJ4CwWXQzRmxxXEFRjsCabQBtMi9sjzX8RBFcVjZUm9j+WGL2UUCgOiY/RbV48w mc5AT683LCFQIr4rDOCLP160VHaxtWqUQIL9HdvZU5fEhMlQGBNWmpD/rg8s7UTx2kJK bGvUO7P31dhIzeDI5oq2brpuZ1n0RIIk+2PGc1N4ATCcov1zteh26WHhVq3PX7YV/fVw jrI/iIkMiXvS1kfyGHZwB0s4/T5J/H60+EYokJZoYenVeKW4NA0MXMEuX2stf2SOC3+o SmsNZfreknyGLlTsjnYTmb58NPwYmzXtutgzaBoCDHEcZjjPFhh9NogeRnhWl8mYHAh+ xHRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717430726; x=1718035526; 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=dXknpeAcJEK6e2DRJM/PLI3NtocdR4jywa4B5EX8TU0=; b=AQQnKPkgstDIiHoC/S+nyJ7TqScLwO3X2sG5GC+5xZDcG78pLPEOB0PcM0N4Z/Gzq8 gabAA2qVdbgbiKxYtaI3ipRuvnGAROHFX0DGpOlLieOWV5qD+eXhEE0Dvy9A8eBwoH0A pbkpxkAnHsBzmj7u6wccgoh0CQ8IYmUxwcvTXv6D40ytyTp38ervMbcJ5Qccgd2C+yEr zQA1D4/yaTQhVRP6rOtBviZrMnl7Gkey6FsuhMTgd8I+hCvpJ+yp9MxjZo73qqs8JyZ6 9ER43kDMULLfMfV5fYrl3jPpzY2ZVpLbsmTta0eH596K3B3A4YCxSpp7tVJ67+NKi4fI UyUA== X-Forwarded-Encrypted: i=1; AJvYcCXm0E/LIcktUH4SLH5wJQ8mUcFavsEETUMzncMGF7bUX17OkmbKjHMQZoe/nHTuCPMvmjMTHKkJhlaxCPqSLHY7SfzEk7oxocUKGmJI X-Gm-Message-State: AOJu0Yzg4HmNFL0KxOf65JADmvQwKENPRewdtu1bjluhCqzBHC6FjoP3 Jtg+TzHHyhxICyZAx0mLcIwOgT9IE5BtOIPsvnhPwsr5h6uwZSCYmH31HCqLgmS1xaH/H4B8IG5 LrgHpvz1C8ttvJeX6Pd4JBsS0vQ== X-Received: from mattgilbride.c.googlers.com ([fda3:e722:ac3:cc00:2b:7d90:c0a8:2ac5]) (user=mattgilbride job=sendgmr) by 2002:a05:6902:1247:b0:dee:6f9d:b753 with SMTP id 3f1490d57ef6-dfa73bfd47fmr467536276.6.1717430726426; Mon, 03 Jun 2024 09:05:26 -0700 (PDT) Date: Mon, 03 Jun 2024 16:05:16 +0000 In-Reply-To: <20240603-b4-rbtree-v4-0-308e43d6abfc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240603-b4-rbtree-v4-0-308e43d6abfc@google.com> X-Mailer: b4 0.12.4 Message-ID: <20240603-b4-rbtree-v4-1-308e43d6abfc@google.com> Subject: [PATCH v4 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.1.288.g0e0cd299f1-goog