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 815672741A2; Wed, 11 Jun 2025 10:44:05 +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=1749638645; cv=none; b=Q9x0nox6mcY5ZMdxCafyTo6whNmuePUJUlysWQqDEhMRtHC6/HOnd74nWw9N11IBm14cR/sqLqufHBAU/KOjCoMnqXnsbjtJrzQjBx7Fl9TJkJ78qm40ASXWptezTn0mBEGYDFd4aCUkPUsPrJiAiMWyZ9KklQauxVNc6WmFmXg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749638645; c=relaxed/simple; bh=Y+E0RernoPBy6Uu3TI84qynsG952E7mhrc4IsFrLBMw=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=ssse8i2HAk3q3K5ZkH838PXXJ5HjkpIpEws16umrv3lXihK5Ss9xVtMwJExoofubFwfnYX2f3DWZsioQLayAMD1ISz3fMeY1zMTEOEzpq0txEoivL4DZwjarBQEK79/RpjqoICSrD81I1zGHY7cC67afdK9SCtKMQVdFD31cUlc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=I5y97M6f; 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="I5y97M6f" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F408C4CEF1; Wed, 11 Jun 2025 10:44:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749638645; bh=Y+E0RernoPBy6Uu3TI84qynsG952E7mhrc4IsFrLBMw=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=I5y97M6f50cvQr9uKp0kUq+9BZMTImITmMRenueRNabxOzKVQyQnTLxbxmtTZxO5U MuDdYfxHemloZH4Cae0vXdincPivKew92K2fsYKBHvufaFJ/6hh27G0I3cVM3HsxR/ 12P5c9q3qgMr0wbH+6gYGusSngdMQNcdUaZ5O+VMO6WFUjTDstfcZjjKhCunYBUlR2 iA/84eicv9QqagjXKqAOgVl4u17gVrqDc4fHejiYpneNgSaHf9RORlssoWb3SghNBz R27yj1lvJ5M7ad3VBUwV+z1ppAVFVDZFKOVd8PQjCyAgkHTt1rLqEelEff47K1kHdi y8iJweniGuBfQ== From: Andreas Hindborg To: "Benno Lossin" 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" , Krzysztof =?utf-8?Q?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 In-Reply-To: (Benno Lossin's message of "Wed, 11 Jun 2025 08:36:50 +0200") References: <20250610-pointed-to-v2-1-fad8f92cf1e5@kernel.org> <87v7p3znz6.fsf@kernel.org> User-Agent: mu4e 1.12.9; emacs 30.1 Date: Wed, 11 Jun 2025 12:43:56 +0200 Message-ID: <87jz5izhg3.fsf@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@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 "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 pointe= r which meets the alignment >>> >> -/// requirements of [`PointedTo`]. >>> >> -/// >>> >> -/// [`into_foreign`]: Self::into_foreign >>> >> -/// [`PointedTo`]: Self::PointedTo >>> >> +/// Implementers must ensure that [`Self::into_foreign`] returns po= inters aligned to >>> >> +/// [`Self::FOREIGN_ALIGN`]. >>> >> pub unsafe trait ForeignOwnable: Sized { >>> >> - /// Type used when the value is foreign-owned. In practical ter= ms 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 for= eign-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 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?) >>> >>> It is probably not going to be null, but it is allowed to. I can add it. >>> >>> 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. Best regards, Andreas Hindborg