From nobody Sun Nov 24 00:03:34 2024 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (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 C4E9E21B422; Fri, 8 Nov 2024 15:46:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080802; cv=none; b=NCEMk/dRufvEINar0Sg8UsMis5LXM0cbdFXQXmRuHc6PmvvW6TNXqlqRmUsowgZsUo7dPqjPoM5rIqB8F0bmesW0RXOfGccGuE30+NUigtEXh1a4Fxyh4dIJExat4BcWpCZ42MOQTuX+nQRPJm/z9HaGPkht2WHU1wyJ7sy2gfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080802; c=relaxed/simple; bh=mVPg65e9gT1vxURcqZdglQZ53y2yUMaRs82BrD29FIU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r767EmL0BoFZ/7aw1s+l+qLO9u4SEi8PoeNKEKec5rqH6Tg+ESqyVgaTDfENJKt86P7UySR+mxSG1VXV93epTeCBSevPfPY8cNR+3MGfQjeuBe/YSg12c7wvawFH+RvLdI8+Rg2ibthVMQXHMkaDK5SLbCEtNj2/3iSVjEXJZdE= 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=WYGuYYq5; arc=none smtp.client-ip=209.85.219.54 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="WYGuYYq5" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6d382677c92so12952266d6.3; Fri, 08 Nov 2024 07:46:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080800; x=1731685600; 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=PS/NMaAouw4ZZ0z6stITEGzGbTE7qQ4nR99gHkd2jPk=; b=WYGuYYq5m1QZFfZRV0Xgpwbq7OGFoZwzC8RTHE+cxiUfF1CrLS92cvy0Srzcl8E6+g XEIqTHxrlLEBAKmcJIL4mTvDU+1rAC2rwh4kppXk/DZ+vjwPyffKPcy9I+uBcGTKMFYu oCZjik7iuNyUGUJpV6frLNI5Fe3xVIrZX9jHCWgQ5xWPUG9Bes22R8chHJOnzkK3tqM8 w/obP3TYsNyM4ELLm9c9nUbETaBJ5hSza05VBdEg6RW9y5EM57StWnY9cOpaVmTRs1RH YNm4FLqZcoJtJeCbBCqwYNZWHPlFNr0ClacT5WbHK2sqG4c5XaQg90zB9d0nFx66gTMR rEQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080800; x=1731685600; 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=PS/NMaAouw4ZZ0z6stITEGzGbTE7qQ4nR99gHkd2jPk=; b=bPQzSTKidMSD7r7hiGS1uwRyzyDKSQwynZP+fmXt6/AIRG86mrbp0J+NHhH/XUXnil CDGXRj6ebSbUWvvhwbF9zFmflVqs9+u8PmHDEAdJXLsnImla6KNj/XXuHZe8hFqSeNFH qNNbQh+hivveVmCAa0OohxegqP7bkLNyhbaqGOVI2Qrly168atRqEx+9b+21ajNlOGxG j6uavXAjUwIIVPwzrCmQPMu/sc/H7mMnXTgRUpGiMl+bRg73uDCMT1GlfwsC4gGmjFBi X8D3dZkAiVZr4BsaYFg5ufa4gtR6mvFyuoDC6dFozVGz1X02+Zk3+LcrYrrsR0jDrwxB dcuA== X-Forwarded-Encrypted: i=1; AJvYcCWXfCOJiqtlhdX4jL0K8L+31Z2HxxNe8WaTutob7BfJj7vNM60pBNHYHlHRadXlRRf7N8Q8edFhG4YjtYk=@vger.kernel.org X-Gm-Message-State: AOJu0YxKvhbK9qaCI3d6ZU0dzuFu7b8JZdBP+eeBsNgK+NlZPWx9VFSF dlTz6LhMx7hBFIAU49Jipb2zjXUodXun+n4EhFkHAcZlQcgkfdRU X-Google-Smtp-Source: AGHT+IGXt5flUp3p3gsulpcv3X35u1D4bibYthP2EOhjeW7urXZjXkCjc78QIIUxUQE8rKOUwaHdIw== X-Received: by 2002:a05:6214:5885:b0:6ce:3ab7:adf3 with SMTP id 6a1803df08f44-6d39e1a98d7mr35964386d6.41.1731080799720; Fri, 08 Nov 2024 07:46:39 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:39 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:16 -0500 Subject: [PATCH v3 1/6] rust: arc: use `NonNull::new_unchecked` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-1-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 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`. Reviewed-by: Alice Ryhl 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..2c9b7d4a2554278ce8608f4f4c7= f9cfe87b21492 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 { } =20 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 =3D NonNull::new(ptr as *mut ArcInner).unwrap(); + // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous + // call to `Self::into_foreign`. + let inner =3D unsafe { NonNull::new_unchecked(ptr as *mut ArcInner= ) }; =20 // SAFETY: The safety requirements of `from_foreign` ensure that t= he 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> { } =20 unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { + // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous + // call to `Self::into_foreign`. + let inner =3D unsafe { NonNull::new_unchecked(ptr as *mut ArcInner= ) }; + // SAFETY: By the safety requirement of this function, we know tha= t `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) } } } =20 --=20 2.47.0 From nobody Sun Nov 24 00:03:34 2024 Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54]) (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 3608421B454; Fri, 8 Nov 2024 15:46:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080803; cv=none; b=bDeigoDYfOzswyaXLlrOKMcz+aESbUir1zzptzeE09ejkzJg5v4bn90E68mEVXBW2TpHG5rNjdUT/7y8zb/JMH9fhLWbM4q0s+OM3z5KowG1VvK+Jmo2gOEkIiZeeRNLWzScMAnGOeob6oRHztJTknjrRgU/z2tIRx+EsGe6PG8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080803; c=relaxed/simple; bh=6lKGTEiWHqnjjUScqoKiXzOQ/By7UnBTXRZwWpY38YA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bWm1jqaDS7ef0SSlkJPU6ZbciSMXpB7h9LSZNBe33H+Q0wbD7rI/hUNYPps/s1p2i4sN+r0oHMv/ao70kXcHj9EPSMluKSnTyT7Ec5W7guoa16z9nPvmU8w9urQRT2ynVUyb2YaZh0Z2pNDrdd5ABfFJ0Q49xcbCdAJHNJa6BDg= 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=G/P3DRV1; arc=none smtp.client-ip=209.85.219.54 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="G/P3DRV1" Received: by mail-qv1-f54.google.com with SMTP id 6a1803df08f44-6cbe53a68b5so14509886d6.1; Fri, 08 Nov 2024 07:46:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080801; x=1731685601; 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=aAw1WzI8iPuU5lh1Dp3vfnx7DLOVmd7TGCi0165fBLo=; b=G/P3DRV1FaKDXTvHbMY+25ED5HztR/PR0cxCqSCdL46TJ5i2eqcIu+kpHzV2D26EDA lTFdlEICUa2YnNnd1SF+Ey+2JlCdkDZh91/iazKv9HwHTVLMDgG/Ekz0hmVz86653z+J +UFDy+MVzGrFUJyZUgFQfd+YdjRJAnshMWCYXs52C2iVaMPBIBq/wozJVI9mSW8jrdNG j8EsIEqz5ulaa0R6LcbiRTNpD2BGgKpEzW4yX8hqA8c+zc1SanCeUzGWlm2tDVGYciaT oTVsfPVp/Es9un1/FzbwHaFMIhGAAAUFR+GIXsdpLCYgbGbQrmmmxxOqIK+RvZvkfynO U+9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080801; x=1731685601; 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=aAw1WzI8iPuU5lh1Dp3vfnx7DLOVmd7TGCi0165fBLo=; b=WiYX0d585+CqU4hXO062w9eO9Vp9ADEuE1VbIok5ZCgremP10IOWX4i4vpiaTHw/2Z qk0dQrvoSuNHhPubtp7ghBF8iL6UQ88FyDxWiJB8FqmDTRGPaGwO2RhUct4Te/nJbgs4 hQvkd7yq1usnjh914wUgHIi2RMfh8fnaJ5XtezCzaZlnNWGK+xsJFCT3Z4o5ezhZly1W ipZC/Bd0uFjYWhqo7YJkSXOrvBjuiS/7og8uYURkTk0FTpmgbHHrqZTMm/SCsm/H5mu6 UBLGxe4kQxvlwsmSJFR7Wko0Rgo/9nOzCMJlAEcdBylSdh2nmbtsls3V073K2EUDmXoi Tvzw== X-Forwarded-Encrypted: i=1; AJvYcCU1nOgRcdGFQthsgkpPGeslx6r8GZgoAJrnjrb2N3njMDFlq13+0NGafwTDVsMyGrurZc811lxB/eYRCwc=@vger.kernel.org X-Gm-Message-State: AOJu0YysO/u44hixaXk3btmbcbeS+P8T/8nGvhIyEnhS+H/K8HTw22pt zdH80aLsKTrdyaxQMku814jElnxaCs3VRVRW6pAukQkH2l3EqH07 X-Google-Smtp-Source: AGHT+IE3g2oYBF7USCJyql+6ZFCUdNvfvo5ZVD+rGTQtScXGyeFL1bZfs0OfzaEbb6IFMWsqvtjmeQ== X-Received: by 2002:a05:6214:5789:b0:6cb:9c55:4215 with SMTP id 6a1803df08f44-6d39e19ff73mr38427126d6.33.1731080800881; Fri, 08 Nov 2024 07:46:40 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:40 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:17 -0500 Subject: [PATCH v3 2/6] rust: types: avoid `as` casts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-2-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 Replace `as` casts with `cast{,_mut}` calls which are a bit safer. In one instance, remove an unnecessary `as` cast without replacement. Signed-off-by: Tamir Duberstein Reviewed-by: Alice Ryhl --- rust/kernel/alloc/kbox.rs | 8 ++++---- rust/kernel/sync/arc.rs | 9 +++++---- rust/kernel/types.rs | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index d69c32496b86a2315f81cafc8e6771ebb0cf10d1..03b7107c60344d4f03d49c51230= e114d3ab38027 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -356,13 +356,13 @@ impl ForeignOwnable for Box type Borrowed<'a> =3D &'a T; =20 fn into_foreign(self) -> *const core::ffi::c_void { - Box::into_raw(self) as _ + Box::into_raw(self).cast() } =20 unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. - unsafe { Box::from_raw(ptr as _) } + unsafe { Box::from_raw(ptr.cast_mut().cast()) } } =20 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> &'a T { @@ -380,13 +380,13 @@ impl ForeignOwnable for Pin> =20 fn into_foreign(self) -> *const core::ffi::c_void { // SAFETY: We are still treating the box as pinned. - Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }) as _ + Box::into_raw(unsafe { Pin::into_inner_unchecked(self) }).cast() } =20 unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. - unsafe { Pin::new_unchecked(Box::from_raw(ptr as _)) } + unsafe { Pin::new_unchecked(Box::from_raw(ptr.cast_mut().cast())) } } =20 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> Pin<&'a T> { diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 2c9b7d4a2554278ce8608f4f4c7f9cfe87b21492..af383bcd003e1122ebe1b62a49f= e40279458e379 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -201,10 +201,11 @@ pub fn new(contents: T, flags: Flags) -> Result { }; =20 let inner =3D KBox::new(value, flags)?; + let inner =3D KBox::leak(inner).into(); =20 // SAFETY: We just created `inner` with a reference count of 1, wh= ich is owned by the new // `Arc` object. - Ok(unsafe { Self::from_inner(KBox::leak(inner).into()) }) + Ok(unsafe { Self::from_inner(inner) }) } } =20 @@ -333,13 +334,13 @@ impl ForeignOwnable for Arc { type Borrowed<'a> =3D ArcBorrow<'a, T>; =20 fn into_foreign(self) -> *const core::ffi::c_void { - ManuallyDrop::new(self).ptr.as_ptr() as _ + ManuallyDrop::new(self).ptr.as_ptr().cast_const().cast() } =20 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T= > { // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr as *mut ArcInner= ) }; + let inner =3D unsafe { NonNull::new_unchecked(ptr.cast_mut().cast:= :>()) }; =20 // SAFETY: The safety requirements of `from_foreign` ensure that t= he object remains alive // for the lifetime of the returned value. @@ -349,7 +350,7 @@ 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 `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr as *mut ArcInner= ) }; + let inner =3D unsafe { NonNull::new_unchecked(ptr.cast_mut().cast:= :>()) }; =20 // SAFETY: By the safety requirement of this function, we know tha= t `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 fae80814fa1c5e0f11933f2f15e173f0e3a10fe0..364dd2dc438eb7d1c4d0a4525bf= 2305a42297b2b 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -418,7 +418,7 @@ pub unsafe fn from_raw(ptr: NonNull) -> Self { /// } /// /// let mut data =3D Empty {}; - /// let ptr =3D NonNull::::new(&mut data as *mut _).unwrap(); + /// let ptr =3D NonNull::::new(&mut data).unwrap(); /// # // SAFETY: TODO. /// let data_ref: ARef =3D unsafe { ARef::from_raw(ptr) }; /// let raw_ptr: NonNull =3D ARef::into_raw(data_ref); --=20 2.47.0 From nobody Sun Nov 24 00:03:34 2024 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 B301F21C17F; Fri, 8 Nov 2024 15:46:43 +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=1731080805; cv=none; b=dx5WRlXtWwM8wvPAfmsnaw6KKR8c3c6Dbz9eVmaWyqX2TP/44kVsr1wYKGBhRcGtrkd+jvOie/jWHx0SxwUZGnE7vd8910FOMKrOKwFhxtDgHcQce0RsCEssEvUTN0n8jVCFLJPPNEZcBxzoaFyLSLH2uY3COUvYzvgVDahp+nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080805; c=relaxed/simple; bh=0PUCDqRECIsSc7WP7EJgJ2Z68b97T/vjCk3CjolRV2U=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XqSYzo95rzA89Szw1yAP2EUP4okIf8obtCp/vCNlpPM83l9BIO8aj+lsmDJHvx2XHbXjX/NwwcYLVWwGHEVolTMDbm/kwgUDZI/zaBA9fDfk034ZsA40RgGPMuUpxRc5+aceDBwufNwsicP4aNrl6q36sKVwEXu5rw8+I3MXKw4= 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=ZqTndn5p; 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="ZqTndn5p" Received: by mail-qv1-f48.google.com with SMTP id 6a1803df08f44-6cbe68f787dso13706476d6.2; Fri, 08 Nov 2024 07:46:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080803; x=1731685603; 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=oVzlBkM+Wc876Gp5SkfK423mmdncVXf8VEuEJTdHg/g=; b=ZqTndn5p67sM7b3Tf03wzI8H/YNhR4aBFecgPLdO2/B9pNgfTHqLKld5wBqJ3NXmFa unaveAyz1jF8qYDJ69jCwWo9b2yFb4oBv5QesUvILMriy3BFnBJ7mzTnyidNDKSGCSv2 1r7cRwmbGnDmsYiAvjlCDWZb64VF6SaSX4U52VPRzIjPrquec7eIFEvG7sm0ci2bPIsd fFX+NG7vlzYUSaNISYQaFv9lwbQbW1UJ1F+8UFJ7YwJmBn4AlctGV7VTpoZAKzjOyFxw HQoi8vICij3mwp3pwZJ3pMrV7Q020KpsxRn6iB3xWvNWGBnPRmANyxNuE/WxvHk3NHNO jozQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080803; x=1731685603; 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=oVzlBkM+Wc876Gp5SkfK423mmdncVXf8VEuEJTdHg/g=; b=lnpqH7KHwNlI2RpGwpn5I6vUKz6F/gwwzYfzMgw5lVQqlZvV4DnYKDkbsfg/NsmVKJ T6IITrrDu1MKGcQ6MnLndP+2Bjf1B+NRjnVjW8yM/6vxl1IW4AMvZnVpueZ3NSfjxC6y cysQtbMIePUoUZgozzSIffSk6PkptKscFjyC7rpvqx/Dru51b0zVXvk0KUAx92SsMHeU 5t9xp4e27JfkA5XKftC1txDSMrnMwmVCvyBw7MavRycad349lsY1gW299r+18dKYqqlp PLTfZ0ALF1qBozN39lzZR8z9acmeOZrQBYEgaAj8cX6csSFoeV2BJ+AnuvM8iCPDBsG9 41SA== X-Forwarded-Encrypted: i=1; AJvYcCVISm5HOKVrLEVSuoOuXkLwSs0gBcgkFGqsRRqjoT0R93AQsDHOHRJFd6Sb1joEtEHZC0486HEW03/RqBI=@vger.kernel.org X-Gm-Message-State: AOJu0YypR5OpoQYnv/BWGpaj+A7XxWQoHfO1O9oLMmXpILEYgaj+oERE b55yz/7Kys/G7GtH80uZ1TAkQdZhnSxhEUHGHCxTxOaeqKNIFzd1 X-Google-Smtp-Source: AGHT+IESGeF16hp3lQvUiFUuFCNgF97breOCox9NCsZVBK1UPuiznIgptG8W43mATPiC8+RfDy4Rzg== X-Received: by 2002:a05:6214:449c:b0:6cb:ee7b:7abd with SMTP id 6a1803df08f44-6d39e165b28mr41411356d6.27.1731080802684; Fri, 08 Nov 2024 07:46:42 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:42 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:18 -0500 Subject: [PATCH v3 3/6] rust: arc: split unsafe block, add missing comment Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-3-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 The new SAFETY comment style is taken from existing comments in `deref` and `drop. Reviewed-by: Alice Ryhl Signed-off-by: Tamir Duberstein --- rust/kernel/sync/arc.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index af383bcd003e1122ebe1b62a49fe40279458e379..9adea755a5ad1a7b03f7fc30a7a= bc76c1f966c6c 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -377,10 +377,14 @@ fn as_ref(&self) -> &T { =20 impl Clone for Arc { fn clone(&self) -> Self { + // SAFETY: By the type invariant, there is necessarily a reference= to the object, so it is + // safe to dereference it. + let refcount =3D unsafe { self.ptr.as_ref() }.refcount.get(); + // INVARIANT: C `refcount_inc` saturates the refcount, so it canno= t overflow to zero. // SAFETY: By the type invariant, there is necessarily a reference= to the object, so it is // safe to increment the refcount. - unsafe { bindings::refcount_inc(self.ptr.as_ref().refcount.get()) = }; + unsafe { bindings::refcount_inc(refcount) }; =20 // SAFETY: We just incremented the refcount. This increment is now= owned by the new `Arc`. unsafe { Self::from_inner(self.ptr) } --=20 2.47.0 From nobody Sun Nov 24 00:03:34 2024 Received: from mail-qv1-f52.google.com (mail-qv1-f52.google.com [209.85.219.52]) (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 AA89121F4B2; Fri, 8 Nov 2024 15:46:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080807; cv=none; b=VJjkwaP0KjGvlGzwBQRQ65hOIw8lvh5b/NqE8Qx/kmmEz+c664JGbPI7MZgehSqEgWjvhfyy9dLYAPaHypBH1uWvl+xtgiH/qwHxxnec5IStXHaxThxpGNr4CBY0n+IabKMvvkAGzI3qoR6jCCdm1Jzx9EhZCNcJht5xWD10gCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080807; c=relaxed/simple; bh=LxXmDYzf/qk0hlOYWjG5qw67ZQPOAMQ/ynasJJsw4aM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=i+YovTtGX9YjWjCK3IsG1menJVgF1OPMBTFo80NMRv9+FXyi651ikyFI5HwYEUpDyevb6lhepC2XyXORxo+wry+zI3+kvodUc7NR04YqNmXGDqucMYeSxMuRho+yrHexUSP6CqTMb2JQ9VcIVxKzPkc3UUV/IgE4gxothgQkKfg= 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=JcD4wIph; arc=none smtp.client-ip=209.85.219.52 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="JcD4wIph" Received: by mail-qv1-f52.google.com with SMTP id 6a1803df08f44-6cd7a2ed34bso13643686d6.2; Fri, 08 Nov 2024 07:46:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080804; x=1731685604; 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=yQ9+l+go52xGjktFzaIhj3DAnoLHb0WXUKoHJUC3hQc=; b=JcD4wIphJBy95KWSDSjDMW/zZVKA59R7AfJ9tyAK/zS+BICsrPzdvTexJ+mYGENakz H/H+ZF72dpWyaXO+KqXeFHMUm2mZZatkOjUgnTVUcadpvgTU+nNO/mg4GBCUa5JGHKPG Co9gu4sp557J/Llr59PNjc+hwouKeTjFFhgY83fX3fdJHgKXiWImIYptng5OZh12PRR5 8ssmDW2OYSOc108Bm6/3kBthEQPn2yydgqqbayHQ83ADexaP2lMDWCIrf2zOa2g3b94o IwKKgYICV2gPbAiuFl1C+kO6zVQnzFnBvtw5jW3OjMcdZOSbfNPITQ6L7z0SNpH11+Di 7qsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080804; x=1731685604; 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=yQ9+l+go52xGjktFzaIhj3DAnoLHb0WXUKoHJUC3hQc=; b=El/BpeRVv2qrD2R7Q5AROXRS+aijITk3Z5DD7Yvq7GNKpyB2pmn+GKoxjteLKbLX23 cbBmkpabBS0vEBQP0iEFe8DmRXEy96pJHLiVZ7FBFzy03CdCSeJXedaRGwz2qluKvgWA mU6WRBymHIYviDxe4VVD9pOvdmvpqTtJdykKvWQMLhNQSespmq+xbK++VbVz66Mfk9HE zOxVa9LHqFPc1OD5wjXlOV0IDuD4VJxvwZmO1ZxgWmSWTBT9zA0zF5MQdBpWJP9sbLnh yPxFZeK+S39UUwml/xMCHtucqwvR3EsxeDEKsnY8nKLlrQc25kbXSyJSzUtW8ZrOlCO8 cJ1A== X-Forwarded-Encrypted: i=1; AJvYcCWPGZZcZj5ksQ/mK0Ae5yASABm8ABxiZOervGBPDnTs2hPN8nVgK+bFTR8LwqlfbXu0EETXfqGWVYWgzNQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx/T3Y7FZAcjCnv746KDthnuPAyJ51QPpwOa1kW6EjgzD9pFKKY oXtDAFjTqlaU+lKaSXkNMIGhtmLupgvMgZ0ocFn67ThiJx5Yurcl X-Google-Smtp-Source: AGHT+IGNVrmIfU/ItuqHWn5AIkcsKFEN4P6uKtarxXcvJOoYsAcj8tUgR68Q4AnseTHhKBZ2JLzSuA== X-Received: by 2002:a05:6214:3198:b0:6cc:11cc:6f36 with SMTP id 6a1803df08f44-6d39e0f7560mr36220416d6.3.1731080804522; Fri, 08 Nov 2024 07:46:44 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:43 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:19 -0500 Subject: [PATCH v3 4/6] rust: change `ForeignOwnable` pointer to mut Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-4-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 | 12 ++++++------ rust/kernel/types.rs | 14 +++++++------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 03b7107c60344d4f03d49c51230e114d3ab38027..99d0fc0148bb8779e5a769a6e74= 291ef8101bf77 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -355,17 +355,17 @@ impl ForeignOwnable for Box { type Borrowed<'a> =3D &'a T; =20 - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { Box::into_raw(self).cast() } =20 - 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 `p= tr` comes from a previous // call to `Self::into_foreign`. - unsafe { Box::from_raw(ptr.cast_mut().cast()) } + unsafe { Box::from_raw(ptr.cast()) } } =20 - 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> =3D Pin<&'a T>; =20 - 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() } =20 - 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 `p= tr` 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())) } } =20 - 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 t= he 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 9adea755a5ad1a7b03f7fc30a7abc76c1f966c6c..10819dc28b64038b9abc55b01c0= 69826d1e5befa 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> =3D ArcBorrow<'a, T>; =20 - fn into_foreign(self) -> *const core::ffi::c_void { - ManuallyDrop::new(self).ptr.as_ptr().cast_const().cast() + fn into_foreign(self) -> *mut core::ffi::c_void { + ManuallyDrop::new(self).ptr.as_ptr().cast() } =20 - 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 `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr.cast_mut().cast:= :>()) }; + let inner =3D unsafe { NonNull::new_unchecked(ptr.cast::>()) }; =20 // SAFETY: The safety requirements of `from_foreign` ensure that t= he object remains alive // for the lifetime of the returned value. unsafe { ArcBorrow::new(inner) } } =20 - 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 `p= tr` comes from a previous // call to `Self::into_foreign`. - let inner =3D unsafe { NonNull::new_unchecked(ptr.cast_mut().cast:= :>()) }; + let inner =3D unsafe { NonNull::new_unchecked(ptr.cast::>()) }; =20 // SAFETY: By the safety requirement of this function, we know tha= t `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..59e71bd158713bb8e12cac95e13= 4f57a277c1b49 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 uninitia= lized memory. Using it in /// any way except for [`ForeignOwnable::from_foreign`], [`ForeignOwna= ble::borrow`], /// [`ForeignOwnable::try_from_foreign`] can result in undefined behav= ior. - fn into_foreign(self) -> *const core::ffi::c_void; + fn into_foreign(self) -> *mut core::ffi::c_void; =20 /// Borrows a foreign-owned object. /// @@ -37,7 +37,7 @@ pub trait ForeignOwnable: Sized { /// /// `ptr` must have been returned by a previous call to [`ForeignOwnab= le::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= >; =20 /// 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 [`Forei= gnOwnable::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; =20 /// 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_voi= d) -> Option { impl ForeignOwnable for () { type Borrowed<'a> =3D (); =20 - fn into_foreign(self) -> *const core::ffi::c_void { + fn into_foreign(self) -> *mut core::ffi::c_void { core::ptr::NonNull::dangling().as_ptr() } =20 - unsafe fn borrow<'a>(_: *const core::ffi::c_void) -> Self::Borrowed<'a= > {} + unsafe fn borrow<'a>(_: *mut core::ffi::c_void) -> Self::Borrowed<'a> = {} =20 - unsafe fn from_foreign(_: *const core::ffi::c_void) -> Self {} + unsafe fn from_foreign(_: *mut core::ffi::c_void) -> Self {} } =20 /// Runs a cleanup function/closure when dropped. --=20 2.47.0 From nobody Sun Nov 24 00:03:34 2024 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) (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 6B77421F4D1; Fri, 8 Nov 2024 15:46:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080809; cv=none; b=Fy7WcbyM/9gJ+D94c2sJSuClWPc5FxarP1XTyVtaFZF6iaGT54C9rPo1HA4Z/cBZqC8NNdi1bcD6Ysw2YRidP9JVrCpj8Mh3o/SLtKCpx3SPQC+Y+V5l4IkD/pxasn2fw9BmuYw7nQfhNQBDXrmeofmxVg/SMPZFwQnubrA2lFc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080809; c=relaxed/simple; bh=DscdtGEIP6lNbD3bNTf3ET8BAfmlcl7kfLImx6MSw68=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZlFPhKCkhcI/mzXCBQirqjIqRlry6rProObhLP39SX2HmtKXtIgx4ZnjMysqjTuho+4F14fGS8Ki43+BEZ+AmNIffxYq5m8A54MFUKSITKrY3z85fwYOTBxI1cSY0DtgK34C/HcDbc0JKUJWoJgq7AbNh+xGpYnmgKB+Mcr6jb0= 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=E4CtaAfb; arc=none smtp.client-ip=209.85.219.53 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="E4CtaAfb" Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-6cbe700dcc3so13757976d6.3; Fri, 08 Nov 2024 07:46:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080806; x=1731685606; 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=vei6IGay3rEvWDI111yr234z1idQdutVPTK6aMtpgJQ=; b=E4CtaAfbQGzkZ3YSZDInmc2xWZbiD4UsGvD4MzNDuQsty8W5ymKY9ET67HXjxGjiY+ zf94oNx+b9YUEy4sfvygokUEtbFs37lNMWY2jZl4ky+VG8DJV2rNH9+C7B/H3BJNvdce 01PUwgSy0SPNwOgidBZBC8YlXYoYLV/vPjelmuCmatnVpbw1vD6Z4NqxakBS7rQ2Tpmq QQVx00b9VBy2AgD+ly450O6YpzG+/uKr4n6VZoi+Zwk92tNVs/N5ww542lDejsSgatNO J5y5Y6nNI2PauH0eYijf3NLUmpEC+jDfQGUFg7InvLolfOp23i9+cJXrXMQ19jVZL+/R Gn4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080806; x=1731685606; 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=vei6IGay3rEvWDI111yr234z1idQdutVPTK6aMtpgJQ=; b=Udd+886REsyPBQWttNPxG3CvyPNwD8CDLss9J5Ssaz971xWM30txfzImZzNW+iTOih h5HklHb/L2PCGM/nyjPbBEuJxQWJp3uSw6V1PEtkcuulFPMvku38yL3vA47SlzmqEgeS wGAdZIyOhFU+EnD6R8w0F0j1Jj2VBTpib9bC4ESr4d7hLwAgIt7gEP4d7NJyKUYQKv1/ R2Rd8Fyo5bCzA8W5gm6JQu3YnhhPklbqEBeH5i+uBGEIgQJmZzDvVvIcvMrNGKqt21l6 TdHmuhD44LuZR0yG2/uWhsm1d2nO2TFS59E0hrGJP2PCVX/fxBPAEhWyhILFH88+ndk8 5QnA== X-Forwarded-Encrypted: i=1; AJvYcCXZqOXqWWxkFwRnDf6htfK32cvnXa+qian93FdGuDr1EU3xnHusqzsc6brV26HmrU4iJWTHLiE1WKvwitw=@vger.kernel.org X-Gm-Message-State: AOJu0YyynPUo9n8ssf6vqYhEa3wErEpxWFUkvhq9Xzr74YP/Ayi9NS4T u9a3lvB3OobhmOzai6IiiVaIov96PcAKFrcGpLeYIplRBYD+SR3x X-Google-Smtp-Source: AGHT+IHgECdzsK7mTZqpJGJP/F0dEYOfmrr9wDoV2NmEBtcxMonvVMsiUFsgAtNCxbxIRmKVnELOMQ== X-Received: by 2002:a05:6214:5501:b0:6d1:9f63:6d5e with SMTP id 6a1803df08f44-6d39e1554dbmr42499476d6.10.1731080806391; Fri, 08 Nov 2024 07:46:46 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:45 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:20 -0500 Subject: [PATCH v3 5/6] rust: reorder `ForeignOwnable` items Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-5-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 `{into,from}_foreign` before `borrow` is slightly more logical. This removes an inconsistency with `kbox.rs` which already uses this ordering. Reviewed-by: Alice Ryhl Signed-off-by: Tamir Duberstein --- rust/kernel/sync/arc.rs | 18 +++++++++--------- rust/kernel/types.rs | 20 ++++++++++---------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 10819dc28b64038b9abc55b01c069826d1e5befa..3c779b343aa8c396d2d4b7efdbc= 0f1ef524a0f1c 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -337,25 +337,25 @@ fn into_foreign(self) -> *mut core::ffi::c_void { ManuallyDrop::new(self).ptr.as_ptr().cast() } =20 - unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> ArcBorrow<'a, T> { + unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self { // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. let inner =3D unsafe { NonNull::new_unchecked(ptr.cast::>()) }; =20 - // SAFETY: The safety requirements of `from_foreign` ensure that t= he object remains alive - // for the lifetime of the returned value. - unsafe { ArcBorrow::new(inner) } + // SAFETY: By the safety requirement of this function, we know tha= t `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(inner) } } =20 - unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self { + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> ArcBorrow<'a, T> { // SAFETY: The safety requirements of this function ensure that `p= tr` comes from a previous // call to `Self::into_foreign`. let inner =3D unsafe { NonNull::new_unchecked(ptr.cast::>()) }; =20 - // SAFETY: By the safety requirement of this function, we know tha= t `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(inner) } + // SAFETY: The safety requirements of `from_foreign` ensure that t= he object remains alive + // for the lifetime of the returned value. + unsafe { ArcBorrow::new(inner) } } } =20 diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index 59e71bd158713bb8e12cac95e134f57a277c1b49..b8f3594737401a3df841f30a20c= 4bd85743853ef 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -31,14 +31,6 @@ pub trait ForeignOwnable: Sized { /// [`ForeignOwnable::try_from_foreign`] can result in undefined behav= ior. fn into_foreign(self) -> *mut core::ffi::c_void; =20 - /// Borrows a foreign-owned object. - /// - /// # Safety - /// - /// `ptr` must have been returned by a previous call to [`ForeignOwnab= le::into_foreign`] for - /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't = been called yet. - unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> Self::Borrowed<'a= >; - /// Converts a foreign-owned object back to a Rust-owned one. /// /// # Safety @@ -67,6 +59,14 @@ unsafe fn try_from_foreign(ptr: *mut core::ffi::c_void) = -> Option { unsafe { Some(Self::from_foreign(ptr)) } } } + + /// Borrows a foreign-owned object. + /// + /// # Safety + /// + /// `ptr` must have been returned by a previous call to [`ForeignOwnab= le::into_foreign`] for + /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't = been called yet. + unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> Self::Borrowed<'a= >; } =20 impl ForeignOwnable for () { @@ -76,9 +76,9 @@ fn into_foreign(self) -> *mut core::ffi::c_void { core::ptr::NonNull::dangling().as_ptr() } =20 - unsafe fn borrow<'a>(_: *mut core::ffi::c_void) -> Self::Borrowed<'a> = {} - unsafe fn from_foreign(_: *mut core::ffi::c_void) -> Self {} + + unsafe fn borrow<'a>(_: *mut core::ffi::c_void) -> Self::Borrowed<'a> = {} } =20 /// Runs a cleanup function/closure when dropped. --=20 2.47.0 From nobody Sun Nov 24 00:03:34 2024 Received: from mail-qk1-f176.google.com (mail-qk1-f176.google.com [209.85.222.176]) (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 A0DE721FD99; Fri, 8 Nov 2024 15:46:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080811; cv=none; b=Q08CM/eC61k67QVaymQwYZPAc/KYbLt7V9JE/U/gxslqNry3ROoZTFtKK/ONqfxJ3MM3ZZ7P0fyDh1X5sGf+t3qHxJ131banmPv8NUPMS3EpebOJ0DzsIv6eJuIO7VpLG4d2faqK/OHk3vZgCLoLUHnNiG3T6LJ30KNaVkdLmvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731080811; c=relaxed/simple; bh=u+NEbyOJWpdDSeZChuV1qe4SlZNC/slJCrEWdMuLbS4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Jn7Lb3K2K2UbN33oYlsph99Fbb7LKbeIhHj84QVbJ7QZQmfqSCOzoMs6OuZgMttn3MLrDu479Al6RGF0QX7zCew5lQ5msmriwjFk2Fk3zjqh1abu3TBu9egCr0A0ZPNrlIImDh9HMH1xWvJD9+YikVGWhprmCJqt/6Fz46TziiQ= 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=F3/BIjW8; arc=none smtp.client-ip=209.85.222.176 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="F3/BIjW8" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7b147a2ff04so129150585a.3; Fri, 08 Nov 2024 07:46:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731080808; x=1731685608; 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=ZVSKDJntyrJ4XBJ4eP6/vgd1kjKfRwF8eMDahS+L+uU=; b=F3/BIjW8eCNI/FlttUEk+3gAycQP0WSKz5Yx4rgofy0XblmLuJfARgza1ZCRFpdD3s piDcfNAP+ldVhAn9t2/ginM+huTzeMT9MD1+ROKYjU/BPh2FCENdxDByTsk+oMiD6APc Yz1gsA3SN3RjypT19e9KQcJSIsKjnL0vAyVycBUVwMcIYPGi9OLFfa3jcV65oTqjt2uQ srNnfsib1UX1ad9T7cYE7h0TY1uy68rxkQXtwN+3nameswKv0JcGN/XMKSUbk6eQo+Wq m7LEyWRDGYW/cP60UhOvOkuEK11G3y67gVGmgmIEc0OptdoPDnVNBkUY+F1Dx8MeQm/4 Q6QA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731080808; x=1731685608; 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=ZVSKDJntyrJ4XBJ4eP6/vgd1kjKfRwF8eMDahS+L+uU=; b=Jj6kmkp3kxPQjSxruBdQ13+zOEtPa3nUIplNVVFVdZV31+PLLyR6wOrd+ENlU9x5cn f0iWSUw+A1CNtT3uoLNt7YvfgCTf3HigE4FY618/7p1zWx3yfVR/IPDMRlGy46sNqvnV N227a2ISpk8ArgiasbIxVXRdNfe1r6nlBuIjwT5hmeZRRFxG/BADI2fB8Mo6jbtzpKYk NVSN5IZOtJe4flIil/vLfKkmmHNxTEg6FyS+TyUXyj//lqZRcVjugtYI06FZDgy0vOoh 12LCQyo7t5swGCPPEBOrAeHimAN89TJWJheicMSPREtA+yWfxCmsF52f/73enGaHTNAI vZLw== X-Forwarded-Encrypted: i=1; AJvYcCUD6AKGXvI4TpXtOEbsXErn/kpWoNc30ykNg6jkD/J1TGH6ulLjHcT8+0atBMeV2I7Qlt4Eb8HGbmf7A3Y=@vger.kernel.org X-Gm-Message-State: AOJu0YyfS+K/wFdtnWnrg7ujih0LSpthUtBaHZkbboS4uhKRZdgEaTA+ b5btdcjG8A+XPNYEs+vQ0OkxTjXad/19RJs5Zn8kF1gDR41LHxKZ X-Google-Smtp-Source: AGHT+IF2qY/fn8FyBjqItC0Y7+MTALIJTw8ex96uVtE07xNxxWZ7Y18IuStyPkBO+ihvKjFcJ7FxWw== X-Received: by 2002:a05:6214:4387:b0:6cb:82fc:2741 with SMTP id 6a1803df08f44-6d39e1abb1fmr43490716d6.49.1731080808377; Fri, 08 Nov 2024 07:46:48 -0800 (PST) Received: from 159.1.168.192.in-addr.arpa ([2600:4041:5be7:7c00:348d:4cbf:ba75:f557]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6d396631118sm20673676d6.110.2024.11.08.07.46.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Nov 2024 07:46:47 -0800 (PST) From: Tamir Duberstein Date: Fri, 08 Nov 2024 10:46:21 -0500 Subject: [PATCH v3 6/6] rust: add improved version of `ForeignOwnable::borrow_mut` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241108-borrow-mut-v3-6-b7144945714e@gmail.com> References: <20241108-borrow-mut-v3-0-b7144945714e@gmail.com> In-Reply-To: <20241108-borrow-mut-v3-0-b7144945714e@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 , Martin Rodriguez Reboredo X-Mailer: b4 0.15-dev From: Alice Ryhl Previously, the `ForeignOwnable` trait had a method called `borrow_mut` that was intended to provide mutable access to the inner value. However, the method accidentally made it possible to change the address of the object being modified, which usually isn't what we want. (And when we want that, it can be done by calling `from_foreign` and `into_foreign`, like how the old `borrow_mut` was implemented.) In this patch, we introduce an alternate definition of `borrow_mut` that solves the previous problem. Conceptually, given a pointer type `P` that implements `ForeignOwnable`, the `borrow_mut` method gives you the same kind of access as an `&mut P` would, except that it does not let you change the pointer `P` itself. This is analogous to how the existing `borrow` method provides the same kind of access to the inner value as an `&P`. Note that for types like `Arc`, having an `&mut Arc` only gives you immutable access to the inner `T`. This is because mutable references assume exclusive access, but there might be other handles to the same reference counted value, so the access isn't exclusive. The `Arc` type implements this by making `borrow_mut` return the same type as `borrow`. Signed-off-by: Alice Ryhl Reviewed-by: Boqun Feng Reviewed-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Andreas Hindborg Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kbox.rs | 21 ++++++++++++++ rust/kernel/sync/arc.rs | 7 +++++ rust/kernel/types.rs | 71 ++++++++++++++++++++++++++++++++++++++-----= ---- 3 files changed, 86 insertions(+), 13 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index 99d0fc0148bb8779e5a769a6e74291ef8101bf77..c7edcd970fe6abe2afce5364a5f= 6c565452da85e 100644 --- a/rust/kernel/alloc/kbox.rs +++ b/rust/kernel/alloc/kbox.rs @@ -354,6 +354,7 @@ impl ForeignOwnable for Box A: Allocator, { type Borrowed<'a> =3D &'a T; + type BorrowedMut<'a> =3D &'a mut T; =20 fn into_foreign(self) -> *mut core::ffi::c_void { Box::into_raw(self).cast() @@ -370,6 +371,13 @@ unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> &= 'a T { // immutable for the duration of 'a. unsafe { &*ptr.cast() } } + + unsafe fn borrow_mut<'a>(ptr: *mut core::ffi::c_void) -> &'a mut T { + let ptr =3D ptr.cast(); + // SAFETY: The safety requirements of this method ensure that the = pointer is valid and that + // nothing else will access the value for the duration of 'a. + unsafe { &mut *ptr } + } } =20 impl ForeignOwnable for Pin> @@ -377,6 +385,7 @@ impl ForeignOwnable for Pin> A: Allocator, { type Borrowed<'a> =3D Pin<&'a T>; + type BorrowedMut<'a> =3D Pin<&'a mut T>; =20 fn into_foreign(self) -> *mut core::ffi::c_void { // SAFETY: We are still treating the box as pinned. @@ -399,6 +408,18 @@ unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> P= in<&'a T> { // SAFETY: This pointer originates from a `Pin>`. unsafe { Pin::new_unchecked(r) } } + + unsafe fn borrow_mut<'a>(ptr: *mut core::ffi::c_void) -> Pin<&'a mut T= > { + let ptr =3D ptr.cast(); + // SAFETY: The safety requirements for this function ensure that t= he 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 + // the lifetime of the returned value. + let r =3D unsafe { &mut *ptr }; + + // SAFETY: This pointer originates from a `Pin>`. + unsafe { Pin::new_unchecked(r) } + } } =20 impl Deref for Box diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs index 3c779b343aa8c396d2d4b7efdbc0f1ef524a0f1c..8a0f44da8f732afca6009a078e9= 0bd7a14034240 100644 --- a/rust/kernel/sync/arc.rs +++ b/rust/kernel/sync/arc.rs @@ -332,6 +332,7 @@ pub fn into_unique_or_drop(self) -> Option>> { =20 impl ForeignOwnable for Arc { type Borrowed<'a> =3D ArcBorrow<'a, T>; + type BorrowedMut<'a> =3D Self::Borrowed<'a>; =20 fn into_foreign(self) -> *mut core::ffi::c_void { ManuallyDrop::new(self).ptr.as_ptr().cast() @@ -357,6 +358,12 @@ unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> A= rcBorrow<'a, T> { // for the lifetime of the returned value. unsafe { ArcBorrow::new(inner) } } + + unsafe fn borrow_mut<'a>(ptr: *mut core::ffi::c_void) -> ArcBorrow<'a,= T> { + // SAFETY: The safety requirements for `borrow_mut` are a superset= of the safety + // requirements for `borrow`. + unsafe { Self::borrow(ptr) } + } } =20 impl Deref for Arc { diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index b8f3594737401a3df841f30a20c4bd85743853ef..c74223579111fe36c7c7cd135ba= 95f25f0b33fab 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -19,26 +19,33 @@ /// This trait is meant to be used in cases when Rust objects are stored i= n C objects and /// eventually "freed" back to Rust. pub trait ForeignOwnable: Sized { - /// Type of values borrowed between calls to [`ForeignOwnable::into_fo= reign`] and - /// [`ForeignOwnable::from_foreign`]. + /// Type used to immutably borrow a value that is currently foreign-ow= ned. type Borrowed<'a>; =20 + /// Type used to mutably borrow a value that is currently foreign-owne= d. + type BorrowedMut<'a>; + /// Converts a Rust-owned object to a foreign-owned one. /// /// The foreign representation is a pointer to void. There are no guar= antees for this pointer. /// For example, it might be invalid, dangling or pointing to uninitia= lized memory. Using it in - /// any way except for [`ForeignOwnable::from_foreign`], [`ForeignOwna= ble::borrow`], - /// [`ForeignOwnable::try_from_foreign`] can result in undefined behav= ior. + /// any way except for [`from_foreign`], [`try_from_foreign`], [`borro= w`], or [`borrow_mut`] can + /// result in undefined behavior. + /// + /// [`from_foreign`]: Self::from_foreign + /// [`try_from_foreign`]: Self::try_from_foreign + /// [`borrow`]: Self::borrow + /// [`borrow_mut`]: Self::borrow_mut fn into_foreign(self) -> *mut core::ffi::c_void; =20 /// Converts a foreign-owned object back to a Rust-owned one. /// /// # Safety /// - /// `ptr` must have been returned by a previous call to [`ForeignOwnab= le::into_foreign`] for - /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't = been called yet. - /// Additionally, all instances (if any) of values returned by [`Forei= gnOwnable::borrow`] for - /// this object must have been dropped. + /// The provided pointer must have been returned by a previous call to= [`into_foreign`], and it + /// must not be passed to `from_foreign` more than once. + /// + /// [`into_foreign`]: Self::into_foreign unsafe fn from_foreign(ptr: *mut core::ffi::c_void) -> Self; =20 /// Tries to convert a foreign-owned object back to a Rust-owned one. @@ -48,8 +55,9 @@ pub trait ForeignOwnable: Sized { /// /// # Safety /// - /// `ptr` must either be null or satisfy the safety requirements for - /// [`ForeignOwnable::from_foreign`]. + /// `ptr` must either be null or satisfy the safety requirements for [= `from_foreign`]. + /// + /// [`from_foreign`]: Self::from_foreign unsafe fn try_from_foreign(ptr: *mut core::ffi::c_void) -> Option { if ptr.is_null() { None @@ -60,17 +68,53 @@ unsafe fn try_from_foreign(ptr: *mut core::ffi::c_void)= -> Option { } } =20 - /// Borrows a foreign-owned object. + /// Borrows a foreign-owned object immutably. + /// + /// This method provides a way to access a foreign-owned value from Ru= st immutably. It provides + /// you with exactly the same abilities as an `&Self` when the value i= s Rust-owned. /// /// # Safety /// - /// `ptr` must have been returned by a previous call to [`ForeignOwnab= le::into_foreign`] for - /// which a previous matching [`ForeignOwnable::from_foreign`] hasn't = been called yet. + /// The provided pointer must have been returned by a previous call to= [`into_foreign`], and if + /// the pointer is ever passed to [`from_foreign`], then that call mus= t happen after the end of + /// the lifetime 'a. + /// + /// [`into_foreign`]: Self::into_foreign + /// [`from_foreign`]: Self::from_foreign unsafe fn borrow<'a>(ptr: *mut core::ffi::c_void) -> Self::Borrowed<'a= >; + + /// Borrows a foreign-owned object mutably. + /// + /// This method provides a way to access a foreign-owned value from Ru= st mutably. It provides + /// you with exactly the same abilities as an `&mut Self` when the val= ue is Rust-owned, except + /// that the address of the object must not be changed. + /// + /// Note that for types like [`Arc`], an `&mut Arc` only gives you = immutable access to the + /// inner value, so this method also only provides immutable access in= that case. + /// + /// In the case of `Box`, this method gives you the ability to modi= fy the inner `T`, but it + /// does not let you change the box itself. That is, you cannot change= which allocation the box + /// points at. + /// + /// # Safety + /// + /// The provided pointer must have been returned by a previous call to= [`into_foreign`], and if + /// the pointer is ever passed to [`from_foreign`], then that call mus= t happen after the end of + /// the lifetime 'a. + /// + /// The lifetime 'a must not overlap with the lifetime of any other ca= ll to [`borrow`] or + /// `borrow_mut` on the same object. + /// + /// [`into_foreign`]: Self::into_foreign + /// [`from_foreign`]: Self::from_foreign + /// [`borrow`]: Self::borrow + /// [`Arc`]: crate::sync::Arc + unsafe fn borrow_mut<'a>(ptr: *mut core::ffi::c_void) -> Self::Borrowe= dMut<'a>; } =20 impl ForeignOwnable for () { type Borrowed<'a> =3D (); + type BorrowedMut<'a> =3D (); =20 fn into_foreign(self) -> *mut core::ffi::c_void { core::ptr::NonNull::dangling().as_ptr() @@ -79,6 +123,7 @@ fn into_foreign(self) -> *mut core::ffi::c_void { unsafe fn from_foreign(_: *mut core::ffi::c_void) -> Self {} =20 unsafe fn borrow<'a>(_: *mut core::ffi::c_void) -> Self::Borrowed<'a> = {} + unsafe fn borrow_mut<'a>(_: *mut core::ffi::c_void) -> Self::BorrowedM= ut<'a> {} } =20 /// Runs a cleanup function/closure when dropped. --=20 2.47.0