From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f48.google.com (mail-qv1-f48.google.com [209.85.219.48]) (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 5745345C1C; Sun, 10 Nov 2024 14:14:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.48 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248062; cv=none; b=aVSWcJyvUEFaS+fKFRA74y25KJfvseHnyio5R/DhqcUjBG8mhYdNW1H9iLVtbymWEHFEQvh6dbPn9w/u88hzDXIj/Yq7R6vPDbWEAu0duHtzp/8vPhinJ/VWC0bp0EwU52HOoSplgDWV4QPmMjgTpRuc36aA36nNF1aETukG1N8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248062; c=relaxed/simple; bh=HeSbIvI6Rj/+rCdtsXrbzuXtaFPM4uCUYjCDAbrviRQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=rAOjgqHuI8cTXqifY1D7xHMqJ5Xdi9qadYNEnu7k/lMUjLRiOJtXz7wVO1IUA2Y+ZWu0Mv51d8TcVVht6pbZ7x5cZykb2yh6swNYmsou3yzFnNnS3K/kk5gLOQhdUpC/0NF6KYiduCJz+0a4Ajvq8+d58jZkPkdmF+ojZnGZyNE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=CXzkv8ua; arc=none smtp.client-ip=209.85.219.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CXzkv8ua" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-6cbce16d151so23851116d6.2; Sun, 10 Nov 2024 06:14:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248059; x=1731852859; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Wbjjnib15rpaO1QKjhvqYpmXAuqeqOHVtjKNCDpuuDg=; b=CXzkv8ua2aotMFUNrPElX6qwDdIcF0VYO0MVRnAc28q7KB3qr/zNxzJCE56KsLxLsS seoa8kvpiwHgITh0ijDtKuc/UapGf/5xPDOsZhjeKq0aRH6HWiNHl4qjdnHCPpfWDxHN lG+agZW7dodwe6Sf8xKUql7C/5fk3erWM7wmaVAI5aojH8bDgJmCq3rWvLcXaw7pwuuL 1jF+N1zawnE6rp5IjhWdqfrziOHnXiwgbA+n3cIfGb6+Negzrj8zZHd2EMwy65VZNH6A Rj/g65aXnHvz6PYRA5TqbXGRpPFkEql/36BZ4QeEZO36LMFgAXsq3qLTsgWy4qXdOvCn 8Wng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248059; x=1731852859; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wbjjnib15rpaO1QKjhvqYpmXAuqeqOHVtjKNCDpuuDg=; b=t1HswKdAPGgmh95uKX5mNy3bM0Y855aCIV2qdBht+qKl5w+pH5WreS4OM2ZRhazTFK Yil0LDbHnMEEeQtyWwtUk3LQ8rErUFmlfsmQLetyK4miCZHTNKgT8NZfwPldCcZHgd6H +/i8TvXZLfGcXA+5h4JincgK1aTER6UKX6tPBmosN+1zpWkfhCSV/GpqxHUxnbjzqrlo IDjPiGmjLaT11BXWglM/K7s4UglLM4ym+qqaFy9/IsJDzu5MrcX/13xDgo4/GquNWaoS OGvagNNoKdCrkIXMVGM1YZGpmPG0ouDtUWleCvD18wel5MmoLvLQYQ3Jzb99j/xo/FW+ q0QA== X-Forwarded-Encrypted: i=1; AJvYcCUCRNlG4poj4jruLnSNwDsOH9gkM5h7TucvPkN6DdgRlL5StWdqgYuBerxYXDewc+HvuY4syyI317nfRC8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9dNFeFeacqDp/joVf3gK0y1494Q56hiWn6EDviaYggZ5gj7WC DEGASHVCpk+acyq6G8HkvdQxIIg9HbN5wn8ECUKH3jI+UgoKOPWq X-Google-Smtp-Source: AGHT+IF0DjRLbd/CylvjMtEd7tll6/GcaDwUsE8FvGqWuyS9qsaRKqt8wqrCd4zN0+xLXRSDGz3UKA== X-Received: by 2002:a05:6214:4406:b0:6cb:ced4:eafd with SMTP id 6a1803df08f44-6d39e1e1a43mr116696406d6.35.1731248059176; Sun, 10 Nov 2024 06:14:19 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:3d4c:5fc2:8f61:bb43]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d3961dfa0csm44570046d6.14.2024.11.10.06.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:17 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:05 -0500 Subject: [PATCH v4 4/6] rust: change `ForeignOwnable` pointer to mut 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; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20241110-borrow-mut-v4-4-053976068215@gmail.com> References: <20241110-borrow-mut-v4-0-053976068215@gmail.com> In-Reply-To: <20241110-borrow-mut-v4-0-053976068215@gmail.com> To: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Tamir Duberstein X-Mailer: b4 0.15-dev It is slightly more convenient to operate on mut pointers, and this also properly conveys the desired ownership semantics of the trait. Reviewed-by: Alice Ryhl Reviewed-by: Andreas Hindborg Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kbox.rs | 16 ++++++++-------- rust/kernel/sync/arc.rs | 10 +++++----- rust/kernel/types.rs | 14 +++++++------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 03b7107c60344d4f03d49c51230e114d3ab38027..99d0fc0148bb8779e5a769a6e74291ef8101bf77 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -355,17 +355,17 @@ impl ForeignOwnable for Box { type Borrowed<'a> = &'a T; - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { Box::into_raw(self).cast() } - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous // call to `Self::into_foreign`. - unsafe { Box::from_raw(ptr.cast_mut().cast()) } + unsafe { Box::from_raw(ptr.cast()) } } - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T { + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> &'a T { // SAFETY: The safety requirements of this method ensure that the object remains alive and // immutable for the duration of 'a. unsafe { &*ptr.cast() } @@ -378,18 +378,18 @@ impl ForeignOwnable for Pin> { type Borrowed<'a> = Pin<&'a T>; - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { // SAFETY: We are still treating the box as pinned. Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast() } - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function ensure that `ptr` comes from a previous // call to `Self::into_foreign`. - unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast_mut().cast())) } + unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast())) } } - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> { + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> Pin<&'a T> { // SAFETY: The safety requirements for this function ensure that the object is still alive, // so it is safe to dereference the raw pointer. // The safety requirements of `from_foreign` also ensure that the object remains alive for diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 6f5fe24f0e1bad5ca141091e8945b8d2f94e16aa..10819dc28b64038b9abc55b01c069826d1e5befa 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -333,24 +333,24 @@ pub fn into_unique_or_drop(self) -> Option>> { impl ForeignOwnable for Arc { type Borrowed<'a> = ArcBorrow<'a, T>; - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { ManuallyDrop::new(self).ptr.as_ptr().cast() } - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> { + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> ArcBorrow<'a, T> { // 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.cast_mut().cast::>()) }; + let inner = unsafe { NonNull::new_unchecked(ptr.cast::>()) }; // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive // for the lifetime of the returned value. unsafe { ArcBorrow::new(inner) } } - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + unsafe fn from_foreign(ptr: *mut 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.cast_mut().cast::>()) }; + let inner = unsafe { NonNull::new_unchecked(ptr.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 diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 364dd2dc438eb7d1c4d0a4525bf2305a42297b2b..59e71bd158713bb8e12cac95e134f57a277c1b49 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -29,7 +29,7 @@ pub trait ForeignOwnable: Sized { /// For example, it might be invalid, dangling or pointing to uninitialized memory. Using it in /// any way except for [`ForeignOwnable::from_foreign`], [`ForeignOwnable::borrow`], /// [`ForeignOwnable::try_from_foreign`] can result in undefined behavior. - fn into_foreign(self) -> *const core::ffi::c_void; + fn into_foreign(self) -> *mut core::ffi::c_void; /// Borrows a foreign-owned object. /// @@ -37,7 +37,7 @@ pub trait ForeignOwnable: Sized { /// /// `ptr` must have been returned by a previous call to [`ForeignOwnable::into_foreign`] for /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. - unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Self::Borrowed<'a>; + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> Self::Borrowed<'a>; /// Converts a foreign-owned object back to a Rust-owned one. /// @@ -47,7 +47,7 @@ pub trait ForeignOwnable: Sized { /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't been called yet. /// Additionally, all instances (if any) of values returned by [`ForeignOwnable::borrow`] for /// this object must have been dropped. - unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self; + unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self; /// Tries to convert a foreign-owned object back to a Rust-owned one. /// @@ -58,7 +58,7 @@ pub trait ForeignOwnable: Sized { /// /// `ptr` must either be null or satisfy the safety requirements for /// [`ForeignOwnable::from_foreign`]. - unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option { + unsafe fn try_from_foreign(ptr: *mut core::ffi::c_void) -> Option { if ptr.is_null() { None } else { @@ -72,13 +72,13 @@ unsafe fn try_from_foreign(ptr: *const core::ffi::c_void) -> Option { impl ForeignOwnable for () { type Borrowed<'a> = (); - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { core::ptr::NonNull::dangling().as_ptr() } - unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a> {} + unsafe fn borrow<'a>(_: *mut core::ffi::c_void) -> Self::Borrowed<'a> {} - unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} + unsafe fn from_foreign(_: *mut core::ffi::c_void) -> Self {} } /// Runs a cleanup function/closure when dropped. -- 2.47.0