rust: file: mark `LocalFile` as `repr(transparent)`
PATCH 1/2 rust: file: mark `LocalFile` as `repr(transparent)`
Unsafe code in LocalFile's methods assumes that the type has the same layout as the inner bindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying the transparent representation.
The File struct (which also wraps bindings::file) is already marked as repr(transparent), so this change makes their layouts equivalent.
--- rust/kernel/fs/file.rs | 1 + 1 file changed, 1 insertion(+)
rust/kernel/fs/file.rs +1 -0
@@ -219,6 +219,7 @@ unsafe fn dec_ref(obj: ptr::NonNull<File>) {/// must be on the same thread as this file. /// /// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos +#[repr(transparent)] pub struct LocalFile { inner:Opaque <bindings::file >, }
Signature · 1 line
2.49.0
PATCH 2/2 rust: file: improve safety comments
Some of the safety comments in LocalFile's methods incorrectly refer to the File type instead of LocalFile, so fix them to use the correct type.
Also add missing Markdown code spans around lifetimes in the safety comments, i.e. change 'a to 'a.
--- rust/kernel/fs/file.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
rust/kernel/fs/file.rs +5 -4
@@ -225,7 +225,7 @@ pub struct LocalFile { }// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation -// makes `ARef<File>` own a normal refcount. +// makes `ARef<LocalFile>` own a normal refcount. unsafe impl AlwaysRefCounted for LocalFile {#[inline] fn inc_ref (&self) {
@@ -236,7 +236,8 @@ fn inc_ref(&self) {#[inline] unsafe fn dec_ref (obj: ptr::NonNull <LocalFile >) {// SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we -// may drop it. The cast is okay since `File` has the same representation as `struct file`. +// may drop it. The cast is okay since `LocalFile` has the same representation as +// `struct file`. unsafe { bindings::fput (obj.cast ().as_ptr ()) } } }
@@ -274,7 +275,7 @@ pub fn fget(fd: u32) -> Result<ARef<LocalFile>, BadFdError> {#[inline] pub unsafe fn from_raw_file <'a>(ptr: *const bindings::file ) -> &'aLocalFile {// SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the -// duration of 'a. The cast is okay because `File` is `repr(transparent)`. +// duration of `'a`. The cast is okay because `LocalFile` is `repr(transparent)`. // // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls. unsafe { &*ptr.cast () }
@@ -348,7 +349,7 @@ impl File {#[inline] pub unsafe fn from_raw_file <'a>(ptr: *const bindings::file ) -> &'aFile {// SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the -// duration of 'a. The cast is okay because `File` is `repr(transparent)`. +// duration of `'a`. The cast is okay because `File` is `repr(transparent)`. // // INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls. unsafe { &*ptr.cast () }
Signature · 1 line
2.49.0
Re: [PATCH 2/2] rust: file: improve safety comments
Quoted 9 lines · Pekka Ristola
Some of the safety comments in
LocalFile's methods incorrectly refer to theFiletype instead ofLocalFile, so fix them to use the correct type.Also add missing Markdown code spans around lifetimes in the safety comments, i.e. change 'a to
'a.
Re: [PATCH 2/2] rust: file: improve safety comments
In reply to Pekka RistolaQuoted 9 lines · Pekka Ristola
Some of the safety comments in
LocalFile's methods incorrectly refer to theFiletype instead ofLocalFile, so fix them to use the correct type.Also add missing Markdown code spans around lifetimes in the safety comments, i.e. change 'a to
'a.
---
Cheers,
Benno
Quoted 3 lines
--- rust/kernel/fs/file.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
Quoted 11 lines · Pekka Ristola
Unsafe code in
LocalFile's methods assumes that the type has the same layout as the innerbindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying thetransparentrepresentation.The
Filestruct (which also wrapsbindings::file) is already marked asrepr(transparent), so this change makes their layouts equivalent.Fixes 851849824bb5 ("rust: file: add Rust abstraction for `struct file`")Signed-off-by Pekka Ristola <[email protected]>
Quoted 6 lines · Pekka Ristola
Unsafe code in
LocalFile's methods assumes that the type has the same layout as the innerbindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying thetransparentrepresentation.
Quoted 6 lines
The
Filestruct (which also wrapsbindings::file) is already marked asrepr(transparent), so this change makes their layouts equivalent.Fixes 851849824bb5 ("rust: file: add Rust abstraction for `struct file`")Signed-off-by Pekka Ristola <[email protected]>
Thanks Pekka, both patches look good to me. I will close the issue when Christian applies them (or if I should take them, that is good too).
Cheers,
Miguel
Quoted 10 lines · Miguel Ojeda
Quoted 8 lines · Pekka Ristola
Unsafe code in
LocalFile's methods assumes that the type has the same layout as the innerbindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying thetransparentrepresentation.The
Filestruct (which also wrapsbindings::file) is already marked asrepr(transparent), so this change makes their layouts equivalent.
Quoted 8 lines
Quoted 3 lines
Fixes 851849824bb5 ("rust: file: add Rust abstraction for `struct file`")Signed-off-by Pekka Ristola [email protected]Thanks Pekka, both patches look good to me. I will close the issue when Christian applies them (or if I should take them, that is good too).
Thanks, I'm glad it went smoothly since this was my first patch to the mailing list.
Pekka
Quoted 11 lines · Pekka Ristola
Unsafe code in
LocalFile's methods assumes that the type has the same layout as the innerbindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying thetransparentrepresentation.The
Filestruct (which also wrapsbindings::file) is already marked asrepr(transparent), so this change makes their layouts equivalent.Fixes 851849824bb5 ("rust: file: add Rust abstraction for `struct file`")Signed-off-by Pekka Ristola <[email protected]>
---
Cheers,
Benno
Quoted 9 lines · Pekka Ristola
Unsafe code in
LocalFile's methods assumes that the type has the same layout as the innerbindings::file. This is not guaranteed by the default struct representation in Rust, but requires specifying thetransparentrepresentation.The
Filestruct (which also wrapsbindings::file) is already marked asrepr(transparent), so this change makes their layouts equivalent.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/2] rust: file: mark LocalFile as repr(transparent)
https://git.kernel.org/vfs/vfs/c/15ecd83dc062 [2/2] rust: file: improve safety comments https://git.kernel.org/vfs/vfs/c/946026ba4293