From nobody Fri Dec 19 00:03:53 2025 Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) (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 E5602347C7; Sun, 10 Nov 2024 14:14:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248053; cv=none; b=Mu1AO22ojixy7IUAEf9nq6sWM4+Dl+aaxssbDejw/KlE+O4B6EhEl2ESjuPKEIF4at2BdfwSwYJ2FsAbaFRXEfv4ca0RdXmxoSNeF9vNVlZwVrn/hym4G0UgB0TLsGYPvnA1GsYBuD7AwJyeHRetF51SYRnanRxpLX635F7u66A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248053; c=relaxed/simple; bh=mVPg65e9gT1vxURcqZdglQZ53y2yUMaRs82BrD29FIU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KgYaKIS27vPRutbYRLs9n/zmurE2J0WM3f5OEy2T0Oe3Ubne12UtMy8C4pCUmpK+s9H1ttWlO4f1xSaJdoXrn43RR0GztZVdZ74hA1gem4UJJk3Sc20qCjCMKy1507yT6NAgUYytoNgq1vaLfpR8mZSgD8O0aVTkoqRRsKLnOH8= 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=KT5ChYVu; arc=none smtp.client-ip=209.85.219.41 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="KT5ChYVu" Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-6d382677c92so26213286d6.3; Sun, 10 Nov 2024 06:14:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248051; x=1731852851; 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=KT5ChYVuBk3w3icaPg9XsT1GG+NRYgfGT5RqHK/8onLdHjUR2FVICBiKW4zbjIVypr ZugUYKLUAceiuii8JhRYgEPj4KqxWv93iBaJmjhSwkKd0xtxbHRgssduSxVRbZTbp2CQ sowzZzsf/lpm2b8a0A8tUFi4OQpM+5vckwo7EdgZKEU9MunkS2rAJNdV2mbNrGtybzzm mbs5pJJDnnV9EhuulLj4UwYMaxX58Kn3Mc1RLH62FqhNcjy5J9jeEH6Stb/oh0DdszGi 1a9mBFjjqR5kAloCS+F4vEefprxLIXijtzCjOStTiu/AvSUtJlw4QBh9M5XdWa6h/uJY YslA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248051; x=1731852851; 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=g+E/L8iKiKkAmI9jz269dBfH2OksP7U7/0H6sIEBpsqG12qlXCmFzCGuVOWP2k/tv/ 6B6SX3Zc62jLkK6pRbMQ8dEc74xII3GLl4oay42OTFyzmx0ze2XBIKbokSymXkziBWiA s0d+Lysd6lnHA3P2ahcw+T9j2xOjdjiF2Y9OYfnqkBRymIX6x/oSBb7g6OBOB9t5HM+W nkHE0JZqFqPtsvIsf0cv2yZ1WHEEeZdPkrM6HIe5pAfuQSsFU2Ytv/b/X33cFJN+qjz1 78B6/nyseM1B7G/So45yh38ahgD2U3MRTPYTnCcXAGBUXd81xWYbFDeY185FKlWdf58q SJVg== X-Forwarded-Encrypted: i=1; AJvYcCV3rOYAdHeflzy3JSkQ91hnhGyxoHFZ2XyaJyatngqKucvLC2T0KGaLO5PweAcHTywZoUnvg5k+7erI/VA=@vger.kernel.org X-Gm-Message-State: AOJu0YwDPs8lpjOodAEXghkoilrRTLO1qL6YNdVRtwuq5NUks50W2j5m B7BnZa9N13sSiekvAm5aQeXJCUmT+FWWKK365TyQew+k2GVrvmi1 X-Google-Smtp-Source: AGHT+IHeHvqHRlF5eKZJFXYDdi2VCt7t3uVjrA2sFEopCKiRWxTVgI3GgZKiaAmbVsnnsMDufM7yZw== X-Received: by 2002:a05:6214:3d9c:b0:6cb:ec68:aa70 with SMTP id 6a1803df08f44-6d39e114bd5mr140738186d6.18.1731248050931; Sun, 10 Nov 2024 06:14:10 -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.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:09 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:02 -0500 Subject: [PATCH v4 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: <20241110-borrow-mut-v4-1-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 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 Reviewed-by: Andreas Hindborg --- 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 Fri Dec 19 00:03:53 2025 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 A11BE45C1C; Sun, 10 Nov 2024 14:14:14 +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=1731248056; cv=none; b=t6GL9mqosd/hYVSYlb1AXRiuzx2AAmSfCzcZUd+pCxs3Li6M/3GFXkVwgoi4hgohv/415gEksGB3XqLFJQbdB7qhZiMsMapehL/et7DnzxNFdHbr2IS9acP5I1suyKc9Ye63mHnSk004dw8WHwMsWFRujV1HR+nvLbrbgXNvG3U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248056; c=relaxed/simple; bh=yzIxA9Yc3SV2h9u+uX5+lLn+hZyCb87b80nRs5uFvn8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MBUE9W/Xg0E26w1DZQfWtqWxGvoUqViiFb2iPJT+X5mFM6LnnLkl13QAsWdMjGRJQz7Dx8Y4hiM3/1GaQA/y5hGCIx+yOFXR9JY6ps3GFQnxkZiQ7GAlUPJbM0L6PBBP9e2kRIrpr81cQhr7cLpchJ+3WBZ/5nyNd3+nvJawjeo= 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=MOTGfsq2; 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="MOTGfsq2" Received: by mail-qk1-f176.google.com with SMTP id af79cd13be357-7b1539faa0bso237761785a.1; Sun, 10 Nov 2024 06:14:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248054; x=1731852854; 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=reYyPGTg6eKoJotuL98ojk5akNG5NxkowlB57z4VXg4=; b=MOTGfsq2qeKCx8Rrdd5OXJlDNNfm4ll+EZ+SC10IcUKd+DrtQOMcJKzd+32oMDR7q2 NR1qwnmI13NuVSuqzG8VjKItvVmjHfPatCqgbMnwC7RW+MiFL1xjO91ozSDCiKTROsXd F3Odhj4OP2yiA11XqocdNCf642kGyh+tIAdJ62ODl1kFT8bqwvBXOCok5OGl1bbfixvX FYfELOYz3O9Ux4JM/QI076EDNk6eKjUao1Kbaym0/gQBPTSv3oOMhYuFHdQsL6LQnOI9 ovqWebn8r/VYiq6pvJHbwwSgUDjwKbZdMZ+imKjt1gyM2J3jpzFXuRxWvDSiMKnqE68+ J4Eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248054; x=1731852854; 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=reYyPGTg6eKoJotuL98ojk5akNG5NxkowlB57z4VXg4=; b=lRwfkANJddUJwfQZmbybmSC7QHWNVBlcP+apED6t1ZlIiIuzwhkuIRb+zwscTwJ43g JjKwcY4KtZR9BF9N3B1HS4MJhUgCopijMQzlz/rrgLmgAVb+4Iex6KwR4Wb4Na9SH7nj r96nppkcc/Y7zb7N0vflaP7pPPXIspZQSQhTizPFdsm1OHwCNQDUg8EcN5cdsJpYRB93 122OC4kxLJVIPUkiqz5FngXxwtXHvmjpgH0ogbH2uja0L50QUV1/QP+ujFZ9WOiLlcVh ObCHlrLf8gS4A5QRYOtIEL9h8hia/9YwEVvR0li2q4Kbu+Wq8H1nsofvfliWGXtPDI2b 0yXQ== X-Forwarded-Encrypted: i=1; AJvYcCX92It5BUVG5qoTHaeUos8LXAsigJSWs5gtj28iQBkpWXyNziw7ufz+0zOOKDkPUas2d/MtyI2YJRZc9RE=@vger.kernel.org X-Gm-Message-State: AOJu0YwsVBTIpRAnu0PV2k5MA2OOzamglsFCS7v92IbPBokUIG+y4nYU pdJk5VktKn7XNzXU3SRTx59sg/6ztl0wmaQHr5Lpe8Sk98W71etW X-Google-Smtp-Source: AGHT+IGmQYOGNMS2bDxjMyIWZPm9NcM9ErEgj67eoRXksArXIibQ2FBLb4LHaesksaMkugBHwb4IgQ== X-Received: by 2002:a05:6214:449c:b0:6cb:ee7b:7abd with SMTP id 6a1803df08f44-6d39e165b28mr141114156d6.27.1731248053581; Sun, 10 Nov 2024 06:14:13 -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.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:12 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:03 -0500 Subject: [PATCH v4 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: <20241110-borrow-mut-v4-2-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 Replace `as` casts with `cast{,_mut}` calls which are a bit safer. In one instance, remove an unnecessary `as` cast without replacement. Reviewed-by: Alice Ryhl Signed-off-by: Tamir Duberstein Reviewed-by: Andreas Hindborg --- 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..bbb6ca8ff7d0e079c7f0efa7702= a91622759cbd7 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() } =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 Fri Dec 19 00:03:53 2025 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (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 8AFAD77111; Sun, 10 Nov 2024 14:14:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248059; cv=none; b=hfhFh2ORC97vM8kHZnC137kXcpHQD5OeATHLsRqhYb3lYhp7rXgI21Sblx0tdQGgYYjiPReUSZKh6SmhAH121nRAT+7bYytjH8QB6g6wmjhUxzN59LYNlhFJpN+NehtDnwo6PNzDuSeK7ulaK8a7RP40gRocpVACV+UIdicnz00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248059; c=relaxed/simple; bh=hzWQGxu/rAZiOTQu/+JUEORWlJV/Ou14e6oi0kPUT14=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Set1r4QE/6aLQ/6y1Y4RRSrBUXOUvWHDTwHMyu1wp3Y5y5rfUKCsoZvkM5mkMXJZ7gziEjTnrE/4plYGX4jDkPldXYw5jLvJMgD0JbZ7A5AI2NcoXlimdMCz/pASrnE3MTHNs1+u0e5j7ga4teWBgh7+vA8KINcd13sdu5M8BBc= 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=i9Mb2TTu; arc=none smtp.client-ip=209.85.219.50 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="i9Mb2TTu" Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-6cbe9e8bbb1so25439246d6.1; Sun, 10 Nov 2024 06:14:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248056; x=1731852856; 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=vakRcIHXfxkcLdpFkEpbbfACXypHGD3cMUux1BxabFY=; b=i9Mb2TTunyOZ4UcYaoMJPJHaKPyVm87/yLTOCBJVwlkawEsdx6DDZdRKAQAhO9ZFAa xssryW48PlNPT0bBK3CqR4I9G1o6vsO1xPY4KKEq2LFo8X2GDq2ge/LDUXwekRN7VovY fbPKfff2ezPDGFhWHYa7iX65WX/vlcjeIxnPaLlHZ2GX5A5rteNn63PgNXsuTAB8obYO tD78TDJHHGN211ZhzWK3XMCfM0fZvd6lPscCIqzKaMTfEwXbTt8fMdEMmytxVcbbKhJp NfjdrOjhyBcExmG8EW4szTlo8TitAM6h9nAPHp8oXr7ibHFlhS6j8XgYuF9Xj1qA4AES No/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248056; x=1731852856; 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=vakRcIHXfxkcLdpFkEpbbfACXypHGD3cMUux1BxabFY=; b=ksfNVvzjx3dzz2uYbWlwsqlscGDD3GLZPIkDDfCsuXzT2X3RA9U5rAQqHMk0vwLDtz BW/MvjqJTQVhdgK7brLVMkvakhk1nUexOb7blO5CLt0rG7eMuJ+3oWYbP1gtI8cpjo+g V+zU3G3o4WcKkRgSW+aTxuJ0aTx+8M9kJwOfKsH1f5e4m4dicqNelqXgLlZ1qPRjGI0T M/rgeAA7tsPUu4+nhZf6Q0Iskq3YQzHrYoWUqR/ACslwKBdPS1NoTAwgxZBeG4xb8v5t khe5Ghtiza90lg9a0Y/0dv4d37U3tFzkHycaetM0KGXiH3ZeMXnK/Mhqpr1GBb9alp/b Tj5A== X-Forwarded-Encrypted: i=1; AJvYcCUTMNUasRNoFXAd3w1I0dzBU6UEKFeS+xX0hNtzrlqlRgum7ZXbh0TuXWItqebsYI6lk5ZgtP9+merNqZs=@vger.kernel.org X-Gm-Message-State: AOJu0Ywy21KcOpt1+uVe8IPHiJKaT+JaLjwNROXZtR4mAhjp9bYs+mSh Ytcco2IMINKqw1U0oZpHBlDlYFF+/sBLEJZiUsl+HOj+P5OWw/17 X-Google-Smtp-Source: AGHT+IGm/xEUnBlpPMII9+xdE9kvblHpfvhJz1oW96P27QZqoiKVWixeUlTdFcdNRZYNR3Tnhb2Zog== X-Received: by 2002:a05:6214:4411:b0:6cb:ee0b:47ba with SMTP id 6a1803df08f44-6d39e101bb6mr126340816d6.10.1731248056362; Sun, 10 Nov 2024 06:14:16 -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.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:15 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:04 -0500 Subject: [PATCH v4 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: <20241110-borrow-mut-v4-3-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 The new SAFETY comment style is taken from existing comments in `deref` and `drop. Reviewed-by: Alice Ryhl Signed-off-by: Tamir Duberstein Reviewed-by: Andreas Hindborg --- 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 bbb6ca8ff7d0e079c7f0efa7702a91622759cbd7..6f5fe24f0e1bad5ca141091e894= 5b8d2f94e16aa 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 Fri Dec 19 00:03:53 2025 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: 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: <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..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 6f5fe24f0e1bad5ca141091e8945b8d2f94e16aa..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 { + 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 Fri Dec 19 00:03:53 2025 Received: from mail-qv1-f41.google.com (mail-qv1-f41.google.com [209.85.219.41]) (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 07080132103; Sun, 10 Nov 2024 14:14:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248064; cv=none; b=qhgfQF6rzUCtGrBHj9BWszGvHiT6zIfG3l4c2Ovor94kuDpd2Jaiy3d3miKTRDWqth2Q73fQ3fOjzxFcOXwddmhch/9FjNbNZbZmM7o8kUqXsgjC6IJZ/v4eLjAKSJdIOolk2OY8aBbqOVIbpbb431e0W+0kLmau2qoYz/+NUJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248064; c=relaxed/simple; bh=DscdtGEIP6lNbD3bNTf3ET8BAfmlcl7kfLImx6MSw68=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=liLYlFnYqrvDDg+dncYVAf0LFHLHqqKJsyY356G2N/QoJ5rWrtIUJ+viZl8vB3dM8ywT/SwfhW/UzSIvZG3TBTp6zISy+J8OKS4wS3JbwPU87CpT1s0LE2R9riwPlal+W6AxgBJkxOICRSiFKOzAUjCJWsLRxatx43dqq5hPDxc= 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=YVfZIiAr; arc=none smtp.client-ip=209.85.219.41 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="YVfZIiAr" Received: by mail-qv1-f41.google.com with SMTP id 6a1803df08f44-6cbf347dc66so25300686d6.3; Sun, 10 Nov 2024 06:14:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248062; x=1731852862; 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=YVfZIiArl8MjqRfFOjW5OIBzDtp2uNhZCL6W6iOE85TrudiNOh9vuPuo9BBiArCn3Y wMgrW685f9RCRME0+LXw/XB6UzkN7dsq7qC590v1TJOPFYBb9/P9/VAf2p/uIUJZ0va4 7zfGGa5hJUx8HMx8XFdNxbGQt4rb+Lu9HJID1YhX9Ny+usc8qmYJHz5DukJBu4DwCYT8 oxnwnQ8FOp+nPFaG7VA1bfKYuZwj950qAseKxkKTx6VY+Zn7Xh57APvowie+7IqrIxZL wkQfO4TWlyyHr6lIsnXDn/uYIVBnkk8uLNNHCvK2IxJA8NEYY4AOHSnjWceaB6M9D12K 7lwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248062; x=1731852862; 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=JkHSsxCjveyIygziqA4MMRv1+4cy0ttK+Ue9Nz2R/58X9lK1znfaOO5NROVwt7tKO5 5lLGa6f8rk2TBzORwMdJo8/88QZXesUe46MpSgEpExbGpHvdDDW06pljm0fSxhaXXuTq B57mlXtNrcZ7x25fQex9XiGWj0NNIpY1Xo3zX0C0Ae9psrUEesTp8Wtca89xUxHf+3Fd 6AJrBIu5K9wPwaOy9DV307QuU6NRcN2MBC8sHst9UUVdtYxT9DOMwYYnpPOQxjmUn0S1 kSt5RtngwAIguejeLeSzDLikna5O5FS9pc7SQf+4GZJlh50Dnq/QZ0jqgdYDijvDCXbD Z9bg== X-Forwarded-Encrypted: i=1; AJvYcCXCRnMtZeKJQkWpu6t9IFlW0L8tHqbtpSxDKe7PYq7QzvLcOHfnJCiPPBfnadVIQa1TkGh96AKYb5nJtBA=@vger.kernel.org X-Gm-Message-State: AOJu0YztATqqTliGZGCfP/FGWt2qQnqRv+ersSAZDyaM5xnBPPD41ZAf OsvqC+EoUo9nKboACLhZqvJ3eNTwPtQTYKUM5EHvD6OOTdn9Qk/T X-Google-Smtp-Source: AGHT+IGMMTOjAbpcHcn7wEsGpOveAaSgymjujiD1RVgTNxz5EW1I9oezGzivWHpyvCIE1ucCcPfOoQ== X-Received: by 2002:a05:6214:5886:b0:6cb:faee:76bd with SMTP id 6a1803df08f44-6d39e19d781mr126937976d6.37.1731248061925; Sun, 10 Nov 2024 06:14:21 -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.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:20 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:06 -0500 Subject: [PATCH v4 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: <20241110-borrow-mut-v4-5-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 `{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 Reviewed-by: Andreas Hindborg --- 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 Fri Dec 19 00:03:53 2025 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (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 E01DB137775; Sun, 10 Nov 2024 14:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248067; cv=none; b=ifDICOugPyN29hkZxp4QegBPC3xjlKuo1w+IrqcZEhYYr9G7+45PPO/lQ80GXRvmsyo5ttfRYy9l13pR77DAqFbrhglm8Xd/CPIs572ZDtZYkE+oRBPbTCjImQZtMdljReogYVJRjlWZg35e80MrDHx7PNbdvXYCOA0OZpSTKJA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731248067; c=relaxed/simple; bh=u+NEbyOJWpdDSeZChuV1qe4SlZNC/slJCrEWdMuLbS4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XPfDLVI9ie7/Tel5ZR00UUURUmk4FKKSKbWL0e/Iw6KHYz+sUzefLS7E2EtVSmZReIB6g+sZucm8OKMSjdOADVi+X24ZJjQgsohaWYexAqjN6mg1yQDpkgJwEstA+c+jQfvCx+DWx2Asd3f4pYTwMMf8vUA5OSajX7ozX5l/FX0= 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=EvS38wuV; arc=none smtp.client-ip=209.85.219.47 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="EvS38wuV" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-6cbd57cc35bso41286056d6.1; Sun, 10 Nov 2024 06:14:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731248065; x=1731852865; 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=EvS38wuVyk9uz68vj5ZIe+4SltGQyjVqqGvOPSNc6rs7Laprfw1GgslMNjWKxRhwYZ kjufzuSCjv0ltw3jZtP47D3TtbQffJRQ6khJ4az9uMdhvO+BJudEIABZECJXi69gUff4 RBevJhUFIolcwJjP0UuLw+eldc775pry2UVf3CLcqC5IjZ5eq0Wexo65Kigo+3X98NU6 xSvj448/E2IbK77V2c7xV8bWYLSmzx5vDPRDW9lg0krZwZmkFsm/IIYawVSDl6uq2bqZ ol3qKwUyRGLcX6OkV5ITpuO3zKtK50eFpgytJ0NnzwhYGLtMKPrtY1z4Vn7++kXSGC3g CCqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731248065; x=1731852865; 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=cYuse1G8m1p7OITItgWvgR6gzvzZ5Q/fmJULoUBoUONsT4jb898P54W4yCHBBoWpjk DeWvIfuEbrRDm0DrlXwsJcJNsA22CuhXEcxg+6d7uiP0WQkbDcBkqmc28krusWhs4Pye dReUz/Z0rW+l+9EbPKDBiCbdPBULUG1uPjXty0AR8fewwuWmh7v1m3Y9QBq9+yKMK8jF taf6lKzDLaJTH+S002q9tE0Diua8tFi/GilnzHALGWRknvH95LaMnTr+0/VMOAs9psSQ DNX9faiqG0Hx302ZLC6Rs5qay27MNz9b8yqc4ESj1l8TJxJdta/dwsJ43CiSMCSIXG93 JWMQ== X-Forwarded-Encrypted: i=1; AJvYcCU8rfUUOx1j8jPhdcoHSL3RZx5m2M/03gwm0uJjlAEFP3iTWYLPFnk6pVNvYBVAsZG6zeTJexRHvDP0oME=@vger.kernel.org X-Gm-Message-State: AOJu0YzaaREYyFVhGb9Db2v1ng0u4ZAxOZRyXNMXtC1T3E+Xgg6UFpAc xJUIfgunzpWIqj9uPz6ozhedrJb3vhzALaNlhtl/d9WAAcvoRgis X-Google-Smtp-Source: AGHT+IFq9SKg/0IlNr8Mq1Ai2unC/AzauPlbHdhi8VHDrOTroCSPVgdhIv6biildB+hPva5MmJ/VUQ== X-Received: by 2002:a05:6214:2f8a:b0:6d3:5bee:33c9 with SMTP id 6a1803df08f44-6d39e564f78mr126603766d6.21.1731248064635; Sun, 10 Nov 2024 06:14:24 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Nov 2024 06:14:23 -0800 (PST) From: Tamir Duberstein Date: Sun, 10 Nov 2024 09:14:07 -0500 Subject: [PATCH v4 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: <20241110-borrow-mut-v4-6-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 , 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