From mboxrd@z Thu Jan 1 00:00:00 1970 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 A33AD13440A for ; Mon, 3 Jun 2024 16:05:27 +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=1717430729; cv=none; b=QZ0Filw0EIYJUaehWmoZYKSwfVY9WwMMgELM2Xeem/gWK4SOZ5/rZ0HAb0IeHmRHXFsoRyhn1XjeGM7dVD3mhvfaeBTt70SptU0VE7wTQIA0QIg8XFfusdQdT/FhiHJ92jab4h29pOYbNCXBavGivvt3w2H7zNv3hOST1jALnm8= 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.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="WS+qJ4Cw" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dfa7464759cso18589276.0 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=UhYbMnIf58y73RC43nFOewNwvCBK67kLN49xLDaCs7FVFQSSFj6rHwN7cxljYmxXZi 0s17zQZWQkfIHPXj4+unqDpN80IGZvibMyF1Y8vOcfi0y/Mt0LGp1qkUMM2l60Ouj5kI o0Mr7NSR90jLYoeitAUtRHbVLcL8RHdWuE87yE26nb5YeWzOfbN+tgp9KRzYJBdbCgR0 Q8Ls4Fh6t9THz33pweuP6VKX+xJArjEuUcTj/W7eYvp9WBGkRIWmLX9IfyDc8cl9QNRz p3CjQP9ArotRLU3YBqZ4FWfsxUg+bLYEEVyPG6w0zt3Fhv8fajpumDdBz4mhhw4z8MUO 2Qiw== X-Forwarded-Encrypted: i=1; AJvYcCW/tTgQR1MiASOYA9jsd96u3SxfqiPw1PR2Zqz+98E/32oKSiSUgzrxGhx6oD12Bb7HmironVKzfV2lMJfLEED1+Ru6JIZYxg1N1dtzoHo= X-Gm-Message-State: AOJu0YxnhRZo9fsnXP8y7MvkrvNDcIQri3/R0Qy1VShRG0ANdTUd5yCr wzJpqO2bGePsbexx77mVOwStRmo6HAoVZXnyMwCR6qR04FLLiMAmS63c8i/oN3G0l2wNf/vFprm 4gDQUi/MRE2jPgAR/eWnutCAoJA== X-Google-Smtp-Source: AGHT+IF4YXLHy6VFUWTWeyZSm7amZ/UK0/Qz62/zdwwFfaiQT5/oqh5lb+Ym9APQjs7f9n368VAtKpMTStJM1AYiJgc= 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: rust-for-linux@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