From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D2F78F5C; Thu, 31 Oct 2024 08:50:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730364651; cv=none; b=HDbEYukaezx5ca81GAPWC556hX5Loe+bKZn66fLonTG+6s6Ik9qpOMqErZxlwYTYeFxKBOqxjwOOqskZ6PLPBGqezgZAk07Bzasncj9fVsK4LHA3a/tLB+rwazIbF85REZhM7QjjhVPP1llqJyZ70Fpg2KcLZfASU2IRdDJ93ec= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730364651; c=relaxed/simple; bh=uDyTxObPZQhe/Lb4VIdpRaXVBtmoGytmy4ea0Z8chBk=; h=From:To:Cc:Subject:In-Reply-To:Date:Message-ID:References: MIME-Version:Content-Type; b=gGeGESQyh9kuQx/WzNuQ0hUuWXTSGmChvla9JnggweFuYfIo6670w7do9O6UJ8TzNpwOXcV4nKao/RodjC0brzANXGArCjygYftcgPf1Jmwu0KFoOJocYJSQpaST+BNwl1ct9Zeld/eJNCiIGnMx46Fz7dRRkE1POeRWGfwnW6Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KzzTOIio; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KzzTOIio" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9638DC4CEC3; Thu, 31 Oct 2024 08:50:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1730364650; bh=uDyTxObPZQhe/Lb4VIdpRaXVBtmoGytmy4ea0Z8chBk=; h=From:To:Cc:Subject:In-Reply-To:Date:References:From; b=KzzTOIioPnYOHCLHWILID2exXZcX6Ib0PDdDcY1jtUD+QU5dMyYtd1JQR0PSJUdOV sQ3nGVMJjfnL1YhNj/FVgmB9yp+n8qASVceYUVtP0mCkZSDYiDKDaoqPHJP3xdPO2J /mIhf2sSJXAXe/vRoMpoRbKkPGPYF9Scipx3HB6loCkUesS3dn8S8b/h6jh8kjMULP BnTJ8M13Nofqs4DXPfo9ljQfoyp3WTBmHf+vcI2pqU+E3LBJCYa067QIxR4M/UXnXd jV0IDAGCs35XljJKGvQg+JXOr/hV3HSCJ2DVzdHTVaSqqvJ+X10XyGfDx5p/FERnOt 7e2FuL3MmLJeA== From: Andreas Hindborg To: "Tamir Duberstein" Cc: "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?us-ascii?Q?=3D=3Futf-8=3FQ=3FBj=3DC3=3DB6rn=3F=3D?= Roy Baron , "Benno Lossin" , "Alice Ryhl" , "Trevor Gross" , "Danilo Krummrich" , , Subject: Re: [PATCH 1/5] rust: arc: use `NonNull::new_unchecked` In-Reply-To: <20241030-borrow-mut-v1-1-8f0ceaf78eaf@gmail.com> (Tamir Duberstein's message of "Wed, 30 Oct 2024 16:46:38 -0400") Date: Thu, 31 Oct 2024 09:27:43 +0100 Message-ID: <87jzdopthc.fsf@kernel.org> References: <20241030-borrow-mut-v1-0-8f0ceaf78eaf@gmail.com> <4Gt8heJcUIL12zJWx8G_MkgF3vZQPoFAEagztu_-UzAVBIVIx_qJn6E7bWdq3QMKQ4n3GcuXitfXwY5HMxX7KQ==@protonmail.internalid> <20241030-borrow-mut-v1-1-8f0ceaf78eaf@gmail.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain "Tamir Duberstein" writes: > There is no need to check (and panic on violations of) the safety > requirements on `ForeignOwnable` functions. Avoiding the check is > consistent with the implementation of `ForeignOwnable` for `Box`. > > Signed-off-by: Tamir Duberstein > --- > rust/kernel/sync/arc.rs | 12 ++++++++---- > 1 file changed, 8 insertions(+), 4 deletions(-) > > diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs > index db9da352d588f65348aa7a5204abbb165b70197f..4857230bd8d410bcca97b2081c3ce2f617ee7921 100644 > --- a/rust/kernel/sync/arc.rs > +++ b/rust/kernel/sync/arc.rs > @@ -337,9 +337,9 @@ fn into_foreign(self) -> *const core::ffi::c_void { > } > > unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { > - // By the safety requirement of this function, we know that `ptr` came from > - // a previous call to `Arc::into_foreign`. > - let inner = NonNull::new(ptr as *mut ArcInner).unwrap(); > + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous > + // call to `Self::into_foreign`. > + let inner = unsafe { NonNull::new_unchecked(ptr as _) }; Please use an explicit cast. > > // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive > // for the lifetime of the returned value. > @@ -347,10 +347,14 @@ unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { > } > > unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { > + // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous > + // call to `Self::into_foreign`. > + let inner = unsafe { NonNull::new_unchecked(ptr as _) }; Please use an explicit cast. > + > // SAFETY: By the safety requirement of this function, we know that `ptr` came from > // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and > // holds a reference count increment that is transferrable to us. > - unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) } > + unsafe { Self::from_inner(inner) } > } > } Otherwise lgtm. Best regards, Andreas