From nobody Sun Nov 24 16:29:30 2024 Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 35CBD1FDFA0; Mon, 4 Nov 2024 21:20:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730755241; cv=none; b=BjDh6GecvPQwB0PnfynzfeQBbzI1asrnqTCENZ8DXrHKuXC4XdkfmlFf+vY7muaU2BvGWY0cQLJ/c1Ed+F/wiEAbAJPLO6TCUSLEEk7SNQtEBf/2qGj4brYh9ifz/GzeeYFjpBg9azEmiUjPi1U3j/GmIGAqaOofVWABnAlybLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730755241; c=relaxed/simple; bh=NBPpIjCfCBzFzTqVitvlZTNxC1N977x8lAE5YiMeWig=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HuZJK98Fy0AYNKN+uvdbxr2CunBHyYKiZFvLFuw9uKXmoeVP/XwFRfAuYzTpRO1j2735hP70iiMo6ECyJ5U7j3uxzLV1rFUX8pSW0YZ1qDh5IVuOZE3a/KHGabITWtUacN73kHfVmsqi/TJVWTjMhI6oDxh/3Ze7o9QZ7xIqWCo= 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=WR1ghJ62; arc=none smtp.client-ip=209.85.128.170 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="WR1ghJ62" Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-6e5e5c43497so37588467b3.3; Mon, 04 Nov 2024 13:20:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730755239; x=1731360039; 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=7DkzMNh/JmDESgjanxg0KNSdGmmyOpzh6cmcoyZX928=; b=WR1ghJ62C2HM4vi41WctM+w1SI1G9hXm5efyQFDV5hEAd3gwrLag1Q0bAG08pOCAMH OCy62eUEyGqqUY4WosXRIRQ3XkXrTNW/aIxUNnBeVrou6J7pn2gs/Rppx0d5SExiS4S6 F9tAUtppK/VeZh9P/8DVEuuK6gTtcDOfIQ0TyFlAgF65aoBeCd0C8WXpZCFjx03hny4F EvoMGujvGMTkeF+Ta5ePD0BeEexdcPJn1bEyCyCGZHrWxodAtxsUCW7BrSaZ7sJ2mquL QiaXG7WMP1W6npo2w0McVEYMw955IRbp+Q+6ptFXK5/WkIWjyeoMIiBCkB4mS/wu5NaY R1GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730755239; x=1731360039; 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=7DkzMNh/JmDESgjanxg0KNSdGmmyOpzh6cmcoyZX928=; b=MwtE49s/8Dr4IhryheTVfhsB6nQZgLx7NOFK3wtmN0idghbgRKbrOvDq3Nhn31d5ew cOYzkZk1PEo/h3wjxOy9Otu35TcVsr3NAVrDUtZXgjbWTD8D1ZxqGR2Mi2d+Y/Xm6sLw TXTgqrkceSznOAgJjkoch3ABltLCoaU0BxKowMVbpG6BCN9wondT2MfcWqtSrHHMWZ3w Sx7Rcc1h/A+C4s3BsEQJ04F/k92rEmYDpqqahZ5f2bLvUv642wFNC1CsTRa0eMpGA//Y KfIytoerKYzgiV+T8bxxnELgpVT/M8Qt4x83NGWrbA1MwMIHQnXeuKM/x2nHPVK4Ms4r z/0g== X-Forwarded-Encrypted: i=1; AJvYcCWRFuyULzsdtF1NZYPbCxtWNVWUtUZTahGcsDb8pg8fRML3BcvFKWxxpzFQ9UwpW2YHTQjqTz2vlo9nZVo=@vger.kernel.org X-Gm-Message-State: AOJu0YxkEvRyVuSh1xKCUiQlu9YOMbYjuzqukwkhOlRPrvMq+WsOzO9R zPrrJyFMvMbFJLDkM6DJwu41u7fBLxUJa/c0hm1PBukvhdWfCXRD X-Google-Smtp-Source: AGHT+IHuI/ppzNLVFJvU7dEErxKjuD+rFC6iqbqk2sB3RSCpFoloekkcinx3gYx4DNbp3qnj3yNTjA== X-Received: by 2002:a05:690c:6f02:b0:6ea:1f5b:1f64 with SMTP id 00721157ae682-6ea521cb7edmr188939137b3.0.1730755239016; Mon, 04 Nov 2024 13:20:39 -0800 (PST) Received: from 1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa ([102.129.152.180]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6ea55b1ed29sm19555817b3.53.2024.11.04.13.20.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Nov 2024 13:20:38 -0800 (PST) From: Tamir Duberstein Date: Mon, 04 Nov 2024 17:20:28 -0400 Subject: [PATCH v2 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: <20241104-borrow-mut-v2-2-de650678648d@gmail.com> References: <20241104-borrow-mut-v2-0-de650678648d@gmail.com> In-Reply-To: <20241104-borrow-mut-v2-0-de650678648d@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{,_const,_mut}` which are a bit safer. Signed-off-by: Tamir Duberstein --- rust/kernel/alloc/kbox.rs | 10 ++++++---- rust/kernel/sync/arc.rs | 9 +++++---- rust/kernel/types.rs | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs index d69c32496b86a2315f81cafc8e6771ebb0cf10d1..b6b6723098b6b30743bf38c97aa= b0e701a5a1be4 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_const().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,15 @@ 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_const() + .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