Skip to content

Commit 21fae34

Browse files
committed
Merge patch series "rust: file: mark LocalFile as repr(transparent)"
Mark files as repr(transparent) to ensure identical layout between C and Rust. * patches from https://lore.kernel.org/[email protected]: rust: file: improve safety comments rust: file: mark `LocalFile` as `repr(transparent)` Link: https://lore.kernel.org/[email protected] Signed-off-by: Christian Brauner <[email protected]>
2 parents dd59137 + 946026b commit 21fae34

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

rust/kernel/fs/file.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,13 @@ unsafe impl AlwaysRefCounted for File {
219219
/// must be on the same thread as this file.
220220
///
221221
/// [`assume_no_fdget_pos`]: LocalFile::assume_no_fdget_pos
222+
#[repr(transparent)]
222223
pub struct LocalFile {
223224
inner: Opaque<bindings::file>,
224225
}
225226

226227
// SAFETY: The type invariants guarantee that `LocalFile` is always ref-counted. This implementation
227-
// makes `ARef<File>` own a normal refcount.
228+
// makes `ARef<LocalFile>` own a normal refcount.
228229
unsafe impl AlwaysRefCounted for LocalFile {
229230
#[inline]
230231
fn inc_ref(&self) {
@@ -235,7 +236,8 @@ unsafe impl AlwaysRefCounted for LocalFile {
235236
#[inline]
236237
unsafe fn dec_ref(obj: ptr::NonNull<LocalFile>) {
237238
// SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we
238-
// may drop it. The cast is okay since `File` has the same representation as `struct file`.
239+
// may drop it. The cast is okay since `LocalFile` has the same representation as
240+
// `struct file`.
239241
unsafe { bindings::fput(obj.cast().as_ptr()) }
240242
}
241243
}
@@ -273,7 +275,7 @@ impl LocalFile {
273275
#[inline]
274276
pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a LocalFile {
275277
// SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
276-
// duration of 'a. The cast is okay because `File` is `repr(transparent)`.
278+
// duration of `'a`. The cast is okay because `LocalFile` is `repr(transparent)`.
277279
//
278280
// INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
279281
unsafe { &*ptr.cast() }
@@ -347,7 +349,7 @@ impl File {
347349
#[inline]
348350
pub unsafe fn from_raw_file<'a>(ptr: *const bindings::file) -> &'a File {
349351
// SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
350-
// duration of 'a. The cast is okay because `File` is `repr(transparent)`.
352+
// duration of `'a`. The cast is okay because `File` is `repr(transparent)`.
351353
//
352354
// INVARIANT: The caller guarantees that there are no problematic `fdget_pos` calls.
353355
unsafe { &*ptr.cast() }

0 commit comments

Comments
 (0)