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 51A1927C145; Wed, 11 Jun 2025 11:51:30 +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=1749642690; cv=none; b=kr6aU262sfJT0jS1KnI4o5FI6erb4CwUJVjHTxUGeBUeOCxjmuNDUUA2Sf2DfkXzB3ux4G2BygxY8vOO7h277bG0sT8HhkCaWzd8xTUCiAi8q1bmeXhwZvWvoFmv/sMRXIa627OL5KN/ZCgPoQQd7V2no3Lp+yArrcP2iGJfX74= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749642690; c=relaxed/simple; bh=UntChnF5TiA3QQXJoWwoKNQ5Ir99qkBbzql/YHxX4gQ=; h=Mime-Version:Content-Type:Date:Message-Id:From:To:Cc:Subject: References:In-Reply-To; b=fEEpI/UbOcPgVx18yXSHj75oCWFxzBO9uDyVvz3rRDVJvY0nAGvrPlDpNb6Fjmtf1V/qTafn03FD7TFN0zXQc6mIvsozbaq15lCt9BcUL50L+f8LZwRcj01e1Ar4DAGGJ1s6WhApDbt2jBzjX9Pf5LFDGH1yX2l8KY1LXHzU6BQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=b1ON1W7n; 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="b1ON1W7n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F05DC4CEF1; Wed, 11 Jun 2025 11:51:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749642689; bh=UntChnF5TiA3QQXJoWwoKNQ5Ir99qkBbzql/YHxX4gQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=b1ON1W7nmk2q9a+Sy1guAfKKNVdJERx8Xfk2riNPkvD+SpVHLr5ONi28DQHQ3cyim K0Sd8xd3fNR7VI9QpEQyNNyc73p20nNh9O42ME1aQHNr3lRl4FS4x5BKpCNM8YZam0 5dTQ+I/mzElbkdulywvtWKdzDnTd3puG8wBhxFBYxqb7+Dli5DIs+6mfBvxGT0d+Qo UOO8HlMHI9aKXxQwEqdMv9/BdYV4ISJZVvfXS5faxs2komCAyJVsZJlxRdEV2sU8wu +ldnQY7ll2GN55FfhXL+XXKNP9QrXNEcEVTHLeJJYMv1rOXm8B9Opasy5h6qY+Ae5J zosPd5HxaiOIA== 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: Wed, 11 Jun 2025 13:51:21 +0200 Message-Id: From: "Benno Lossin" To: "Andreas Hindborg" Cc: "Alice Ryhl" , "Danilo Krummrich" , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Trevor Gross" , "Bjorn Helgaas" , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , "Greg Kroah-Hartman" , "Rafael J. Wysocki" , "Tamir Duberstein" , "Viresh Kumar" , , , , =?utf-8?q?Ma=C3=ADra_Canal?= Subject: Re: [PATCH v2] rust: types: add FOREIGN_ALIGN to ForeignOwnable X-Mailer: aerc 0.20.1 References: <20250610-pointed-to-v2-1-fad8f92cf1e5@kernel.org> <87v7p3znz6.fsf@kernel.org> <87jz5izhg3.fsf@kernel.org> In-Reply-To: <87jz5izhg3.fsf@kernel.org> On Wed Jun 11, 2025 at 12:43 PM CEST, Andreas Hindborg wrote: > "Benno Lossin" writes: > >> On Tue Jun 10, 2025 at 4:15 PM CEST, Alice Ryhl wrote: >>> On Tue, Jun 10, 2025 at 4:10=E2=80=AFPM Andreas Hindborg wrote: >>>> >>>> "Benno Lossin" writes: >>>> >>>> > 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 point= er which meets the alignment >>>> >> -/// requirements of [`PointedTo`]. >>>> >> -/// >>>> >> -/// [`into_foreign`]: Self::into_foreign >>>> >> -/// [`PointedTo`]: Self::PointedTo >>>> >> +/// Implementers must ensure that [`Self::into_foreign`] returns p= ointers aligned to >>>> >> +/// [`Self::FOREIGN_ALIGN`]. >>>> >> pub unsafe trait ForeignOwnable: Sized { >>>> >> - /// Type used when the value is foreign-owned. In practical te= rms only defines the alignment of >>>> >> - /// the pointer. >>>> >> - type PointedTo; >>>> >> + /// The alignment of pointers returned by `into_foreign`. >>>> >> + const FOREIGN_ALIGN: usize; >>>> >> >>>> >> /// Type used to immutably borrow a value that is currently fo= reign-owned. >>>> >> type Borrowed<'a>; >>>> >> @@ -39,18 +35,20 @@ pub unsafe trait ForeignOwnable: Sized { >>>> >> >>>> >> /// Converts a Rust-owned object to a foreign-owned one. >>>> >> /// >>>> >> + /// The foreign representation is a pointer to void. Aside fro= m the guarantees listed below, >>>> > >>>> > I feel like this reads better: >>>> > >>>> > s/guarantees/ones/ >>>> > >>>> >> + /// there are no other guarantees for this pointer. For exampl= e, it might be invalid, dangling >>>> > >>>> > We should also mention that it could be null. (or is that assumption >>>> > wrong?) >>>> >>>> It is probably not going to be null, but it is allowed to. I can add i= t. >>>> >>>> The list does not claim to be exhaustive, and a null pointer is just a >>>> special case of an invalid pointer. >>> >>> We probably should not allow null pointers. If we do, then >>> try_from_foreign does not make sense. >> >> That's a good point. Then let's add that as a safety requirement for the >> trait. > > I disagree. It does not matter for the safety of the trait. > > From the point of the user, the pointer is opaque and can be any value. > In fact, one could do a safe implementation where the returned value is > a key into some mapping structure. Probably not super fast, but the user > should not care. Then we'll have to remove `try_from_foreign`. --- Cheers, Benno