From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2CCD12980BC; Tue, 10 Jun 2025 12:49:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749559792; cv=none; b=iozG9rQUxKEGaRCstt1DPE6yfMbkNQw81F16DbZkwVNJRxFqVErIip6ds0COfJSq98XcjQHLu/TOJqsiFo0AS8DEapR8EE84GUtHSzQX6aJDEocQCAVtbcxNZdxlBaEMORtRbUouizFsBGUY78kgqsALRLZ5vEhOqPI2G11462w= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749559792; c=relaxed/simple; bh=W25gwF8YLypX00uVpfkkP6wlbnXPxSb2jvfHBAnd+vY=; h=Mime-Version:Content-Type:Date:Message-Id:Cc:Subject:From:To: References:In-Reply-To; b=lJubkXEyRNO7jdi++gHu7bfUrSJGBBAnGd+BIolsmEnUK1XWrW6BFB3HDkL61aGyOjiEXk+ILt+dKI1t8ujKVARDfxPZmNs5egQXCzBFza/AWBJZakZp5QZzueSe1Ptyb6J8ihdFI5DePMllyGFCV90VKxwjlfmBvr3T845AUK0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VwUg6wmW; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="VwUg6wmW" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AF2F3C4CEED; Tue, 10 Jun 2025 12:49:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749559792; bh=W25gwF8YLypX00uVpfkkP6wlbnXPxSb2jvfHBAnd+vY=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=VwUg6wmWcuTukn6x1MQGWJPG+DKhnF59n+5X7dRAFpNe9wLyOZUORUruold83pPJB bj9cUFcdGcwRTmqRe1QJrvcNaGpT4Th/3mWc72HdtaMPxBjuw3O5XQblI2i2oY8nXT L3taybLarOHsO/T1+5z3pIpkQdsF4ZnOF8zKXn8BaNBkC7oX6XCBQYxvLxoPp9NhLy 1nk/dp4mTL8TcPpEScGuJW6VI+BWDrvXhKEfsxcmbs7RzsSBqetC126UdT5W2iZFMp gdfi9PFgSsMe3rNi1AC5u9W0TCBRza9ek1otV993a1VUp7GYyGo2rrL+0+2dXcG4YJ 8EDLEncuDDzvg== Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 10 Jun 2025 14:49:47 +0200 Message-Id: Cc: , , , =?utf-8?q?Ma=C3=ADra_Canal?= Subject: Re: [PATCH v2] rust: types: add FOREIGN_ALIGN to ForeignOwnable From: "Benno Lossin" To: "Andreas Hindborg" , "Danilo Krummrich" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Alice Ryhl" , "Trevor Gross" , "Bjorn Helgaas" , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , "Greg Kroah-Hartman" , "Rafael J. Wysocki" , "Tamir Duberstein" , "Viresh Kumar" X-Mailer: aerc 0.20.1 References: <20250610-pointed-to-v2-1-fad8f92cf1e5@kernel.org> In-Reply-To: <20250610-pointed-to-v2-1-fad8f92cf1e5@kernel.org> On Tue Jun 10, 2025 at 1:30 PM CEST, Andreas Hindborg wrote: > diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs > index 22985b6f6982..0ccef6b5a20a 100644 > --- a/rust/kernel/types.rs > +++ b/rust/kernel/types.rs > @@ -21,15 +21,11 @@ > /// > /// # Safety > /// > -/// Implementers must ensure that [`into_foreign`] returns a pointer whi= ch meets the alignment > -/// requirements of [`PointedTo`]. > -/// > -/// [`into_foreign`]: Self::into_foreign > -/// [`PointedTo`]: Self::PointedTo > +/// Implementers must ensure that [`Self::into_foreign`] returns pointer= s aligned to > +/// [`Self::FOREIGN_ALIGN`]. > pub unsafe trait ForeignOwnable: Sized { > - /// Type used when the value is foreign-owned. In practical terms on= ly defines the alignment of > - /// the pointer. > - type PointedTo; > + /// The alignment of pointers returned by `into_foreign`. > + const FOREIGN_ALIGN: usize; > =20 > /// Type used to immutably borrow a value that is currently foreign-= owned. > type Borrowed<'a>; > @@ -39,18 +35,20 @@ pub unsafe trait ForeignOwnable: Sized { > =20 > /// Converts a Rust-owned object to a foreign-owned one. > /// > + /// The foreign representation is a pointer to void. Aside from the = guarantees listed below, I feel like this reads better: s/guarantees/ones/ > + /// there are no other guarantees for this pointer. For example, it = might be invalid, dangling We should also mention that it could be null. (or is that assumption wrong?) --- Cheers, Benno > + /// or pointing to uninitialized memory. Using it in any way except = for [`from_foreign`], > + /// [`try_from_foreign`], [`borrow`], or [`borrow_mut`] can result i= n undefined behavior. > + /// > /// # Guarantees > /// > - /// The return value is guaranteed to be well-aligned, but there are= no other guarantees for > - /// this pointer. For example, it might be null, dangling, or point = to uninitialized memory. > - /// Using it in any way except for [`ForeignOwnable::from_foreign`],= [`ForeignOwnable::borrow`], > - /// [`ForeignOwnable::try_from_foreign`] can result in undefined beh= avior. > + /// - Minimum alignment of returned pointer is [`Self::FOREIGN_ALIGN= `]. > /// > /// [`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 Self::PointedTo; > + fn into_foreign(self) -> *mut crate::ffi::c_void; > =20 > /// Converts a foreign-owned object back to a Rust-owned one. > ///