Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp452640lqt; Thu, 6 Jun 2024 08:18:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUY24+le53Ds1ag2Roi1wgJcWIJwBrfXijniHf+9auzOzyS5MaAih1oFLRTcrdnK04zsBV6KaG/hiK/UyHMfk6ReH9FilotNSmPccsWlA== X-Google-Smtp-Source: AGHT+IFrBg/AmhbT2SVvPrkKg6rye3gPw/5xLhTsA9jS+ww7RCw73S3HmwnZ0sLpvHE37Imh6sr0 X-Received: by 2002:a17:903:2281:b0:1f4:962f:6338 with SMTP id d9443c01a7336-1f6a5a12daamr67503375ad.28.1717687139029; Thu, 06 Jun 2024 08:18:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717687139; cv=pass; d=google.com; s=arc-20160816; b=In3Ku/rlSAmTVkeQjeOCgrWgz2dERCY8txBl1Vqp1GsnbB+3EFg+kexJhfENZqLPUA ltHg6IO9xOt+GwCou5ZkM9AWpLREXKzn0MV+riKamWD8sh33ZhUUnwmXI7wrI+vJkw1M Bglf3yjj1Li5CKhMqZHp3V3ucRrP0xImLdXbWG5Yzg7y+XW0yH/rZx8kjvuS2/AvmeUx 89jJ1mOgeWIXBzrt5zseYSTHFsJKPm8JgAZk6t8JEZM/VbtlA5z+kpyfHThzqfXWGWJ4 2L2RDRMnPYP0yzKOkkZ2aEdTgW8CibHlm7j1NnGn/P6zdR2pup9uEBde3AvbsjetNahr gHGg== 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=D83xpw4zc0nTnhAHOcVsYRJWLRnvj7gUU4+SqE71h3c=; fh=F+VDBlQ+/t54J/adHuIYTkLpTMpBpSey7seykFdGoz4=; b=OsKBs0nwXvcHLJWMIv5/Xj8DhBjkmCEYY2Jc+n04SmA8VlUc5ZexAVIxClh1JsHn4C NkdQHIuWrMPTJWGQPGicSgwLBDxVjPhiEc8yR5FLltM/7qCdRgGvw5ad0flWGd3BgUed w5gL/EaUJqScYlmTjqKmmyGiJMv9x6AgFRqh2woM2XQYk2CYlvOjM5eR9d5jYRWxTf2V YPEuy6h+19aoLRF9jaIZOE5LwAR6O6WqYJe2vEUDfc3ml4Woa+p4DlCBrzox6bQQ0Fay KU37q/EICrbZXWjo6jexir2klHtzs4lp2UoG7gqxgTMG5us1ojWWfiO4mSzLuhYnnENX EGRA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=V8tpqz01; 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-204521-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204521-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 41be03b00d2f7-6de28c3a5c0si1309026a12.637.2024.06.06.08.18.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 08:18:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204521-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=@google.com header.s=20230601 header.b=V8tpqz01; 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-204521-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204521-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id E792628B29B for ; Thu, 6 Jun 2024 15:10:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1533E19FA84; Thu, 6 Jun 2024 14:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="V8tpqz01" 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 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-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" 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.467.gbab1589fc0-goog