Skip to content

Commit ca84095

Browse files
wedsonaffbq
authored andcommitted
rust: file: add Rust abstraction for struct file
This abstraction makes it possible to manipulate the open files for a process. The new `File` struct wraps the C `struct file`. When accessing it using the smart pointer `ARef<File>`, the pointer will own a reference count to the file. When accessing it as `&File`, then the reference does not own a refcount, but the borrow checker will ensure that the reference count does not hit zero while the `&File` is live. Since this is intended to manipulate the open files of a process, we introduce an `fget` constructor that corresponds to the C `fget` method. In future patches, it will become possible to create a new fd in a process and bind it to a `File`. Rust Binder will use these to send fds from one process to another. We also provide a method for accessing the file's flags. Rust Binder will use this to access the flags of the Binder fd to check whether the non-blocking flag is set, which affects what the Binder ioctl does. This introduces a struct for the EBADF error type, rather than just using the Error type directly. This has two advantages: * `File::from_fd` returns a `Result<ARef<File>, BadFdError>`, which the compiler will represent as a single pointer, with null being an error. This is possible because the compiler understands that `BadFdError` has only one possible value, and it also understands that the `ARef<File>` smart pointer is guaranteed non-null. * Additionally, we promise to users of the method that the method can only fail with EBADF, which means that they can rely on this promise without having to inspect its implementation. That said, there are also two disadvantages: * Defining additional error types involves boilerplate. * The question mark operator will only utilize the `From` trait once, which prevents you from using the question mark operator on `BadFdError` in methods that return some third error type that the kernel `Error` is convertible into. (However, it works fine in methods that return `Error`.) Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Daniel Xu <[email protected]> Signed-off-by: Daniel Xu <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent f6d29c6 commit ca84095

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed

fs/file.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,13 @@ EXPORT_SYMBOL(task_lookup_next_fdget_rcu);
11271127
*
11281128
* The fput_needed flag returned by fget_light should be passed to the
11291129
* corresponding fput_light.
1130+
*
1131+
* (As an exception to rule 2, you can call filp_close between fget_light and
1132+
* fput_light provided that you capture a real refcount with get_file before
1133+
* the call to filp_close, and ensure that this real refcount is fput *after*
1134+
* the fput_light call.)
1135+
*
1136+
* See also the documentation in rust/kernel/file.rs.
11301137
*/
11311138
static unsigned long __fget_light(unsigned int fd, fmode_t mask)
11321139
{

rust/bindings/bindings_helper.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <kunit/test.h>
1010
#include <linux/errname.h>
1111
#include <linux/ethtool.h>
12+
#include <linux/file.h>
13+
#include <linux/fs.h>
1214
#include <linux/jiffies.h>
1315
#include <linux/mdio.h>
1416
#include <linux/phy.h>

rust/helpers.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <linux/build_bug.h>
2626
#include <linux/err.h>
2727
#include <linux/errname.h>
28+
#include <linux/fs.h>
2829
#include <linux/mutex.h>
2930
#include <linux/refcount.h>
3031
#include <linux/sched/signal.h>
@@ -157,6 +158,12 @@ void rust_helper_init_work_with_key(struct work_struct *work, work_func_t func,
157158
}
158159
EXPORT_SYMBOL_GPL(rust_helper_init_work_with_key);
159160

161+
struct file *rust_helper_get_file(struct file *f)
162+
{
163+
return get_file(f);
164+
}
165+
EXPORT_SYMBOL_GPL(rust_helper_get_file);
166+
160167
/*
161168
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
162169
* use it in contexts where Rust expects a `usize` like slice (array) indices.

rust/kernel/file.rs

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Files and file descriptors.
4+
//!
5+
//! C headers: [`include/linux/fs.h`](../../../../include/linux/fs.h) and
6+
//! [`include/linux/file.h`](../../../../include/linux/file.h)
7+
8+
use crate::{
9+
bindings,
10+
error::{code::*, Error, Result},
11+
types::{ARef, AlwaysRefCounted, Opaque},
12+
};
13+
use core::ptr;
14+
15+
/// Flags associated with a [`File`].
16+
pub mod flags {
17+
/// File is opened in append mode.
18+
pub const O_APPEND: u32 = bindings::O_APPEND;
19+
20+
/// Signal-driven I/O is enabled.
21+
pub const O_ASYNC: u32 = bindings::FASYNC;
22+
23+
/// Close-on-exec flag is set.
24+
pub const O_CLOEXEC: u32 = bindings::O_CLOEXEC;
25+
26+
/// File was created if it didn't already exist.
27+
pub const O_CREAT: u32 = bindings::O_CREAT;
28+
29+
/// Direct I/O is enabled for this file.
30+
pub const O_DIRECT: u32 = bindings::O_DIRECT;
31+
32+
/// File must be a directory.
33+
pub const O_DIRECTORY: u32 = bindings::O_DIRECTORY;
34+
35+
/// Like [`O_SYNC`] except metadata is not synced.
36+
pub const O_DSYNC: u32 = bindings::O_DSYNC;
37+
38+
/// Ensure that this file is created with the `open(2)` call.
39+
pub const O_EXCL: u32 = bindings::O_EXCL;
40+
41+
/// Large file size enabled (`off64_t` over `off_t`).
42+
pub const O_LARGEFILE: u32 = bindings::O_LARGEFILE;
43+
44+
/// Do not update the file last access time.
45+
pub const O_NOATIME: u32 = bindings::O_NOATIME;
46+
47+
/// File should not be used as process's controlling terminal.
48+
pub const O_NOCTTY: u32 = bindings::O_NOCTTY;
49+
50+
/// If basename of path is a symbolic link, fail open.
51+
pub const O_NOFOLLOW: u32 = bindings::O_NOFOLLOW;
52+
53+
/// File is using nonblocking I/O.
54+
pub const O_NONBLOCK: u32 = bindings::O_NONBLOCK;
55+
56+
/// Also known as `O_NDELAY`.
57+
///
58+
/// This is effectively the same flag as [`O_NONBLOCK`] on all architectures
59+
/// except SPARC64.
60+
pub const O_NDELAY: u32 = bindings::O_NDELAY;
61+
62+
/// Used to obtain a path file descriptor.
63+
pub const O_PATH: u32 = bindings::O_PATH;
64+
65+
/// Write operations on this file will flush data and metadata.
66+
pub const O_SYNC: u32 = bindings::O_SYNC;
67+
68+
/// This file is an unnamed temporary regular file.
69+
pub const O_TMPFILE: u32 = bindings::O_TMPFILE;
70+
71+
/// File should be truncated to length 0.
72+
pub const O_TRUNC: u32 = bindings::O_TRUNC;
73+
74+
/// Bitmask for access mode flags.
75+
///
76+
/// # Examples
77+
///
78+
/// ```
79+
/// use kernel::file;
80+
/// # fn do_something() {}
81+
/// # let flags = 0;
82+
/// if (flags & file::flags::O_ACCMODE) == file::flags::O_RDONLY {
83+
/// do_something();
84+
/// }
85+
/// ```
86+
pub const O_ACCMODE: u32 = bindings::O_ACCMODE;
87+
88+
/// File is read only.
89+
pub const O_RDONLY: u32 = bindings::O_RDONLY;
90+
91+
/// File is write only.
92+
pub const O_WRONLY: u32 = bindings::O_WRONLY;
93+
94+
/// File can be both read and written.
95+
pub const O_RDWR: u32 = bindings::O_RDWR;
96+
}
97+
98+
/// Wraps the kernel's `struct file`.
99+
///
100+
/// # Refcounting
101+
///
102+
/// Instances of this type are reference-counted. The reference count is incremented by the
103+
/// `fget`/`get_file` functions and decremented by `fput`. The Rust type `ARef<File>` represents a
104+
/// pointer that owns a reference count on the file.
105+
///
106+
/// Whenever a process opens a file descriptor (fd), it stores a pointer to the file in its `struct
107+
/// files_struct`. This pointer owns a reference count to the file, ensuring the file isn't
108+
/// prematurely deleted while the file descriptor is open. In Rust terminology, the pointers in
109+
/// `struct files_struct` are `ARef<File>` pointers.
110+
///
111+
/// ## Light refcounts
112+
///
113+
/// Whenever a process has an fd to a file, it may use something called a "light refcount" as a
114+
/// performance optimization. Light refcounts are acquired by calling `fdget` and released with
115+
/// `fdput`. The idea behind light refcounts is that if the fd is not closed between the calls to
116+
/// `fdget` and `fdput`, then the refcount cannot hit zero during that time, as the `struct
117+
/// files_struct` holds a reference until the fd is closed. This means that it's safe to access the
118+
/// file even if `fdget` does not increment the refcount.
119+
///
120+
/// The requirement that the fd is not closed during a light refcount applies globally across all
121+
/// threads - not just on the thread using the light refcount. For this reason, light refcounts are
122+
/// only used when the `struct files_struct` is not shared with other threads, since this ensures
123+
/// that other unrelated threads cannot suddenly start using the fd and close it. Therefore,
124+
/// calling `fdget` on a shared `struct files_struct` creates a normal refcount instead of a light
125+
/// refcount.
126+
///
127+
/// Light reference counts must be released with `fdput` before the system call returns to
128+
/// userspace. This means that if you wait until the current system call returns to userspace, then
129+
/// all light refcounts that existed at the time have gone away.
130+
///
131+
/// ## Rust references
132+
///
133+
/// The reference type `&File` is similar to light refcounts:
134+
///
135+
/// * `&File` references don't own a reference count. They can only exist as long as the reference
136+
/// count stays positive, and can only be created when there is some mechanism in place to ensure
137+
/// this.
138+
///
139+
/// * The Rust borrow-checker normally ensures this by enforcing that the `ARef<File>` from which
140+
/// a `&File` is created outlives the `&File`.
141+
///
142+
/// * Using the unsafe [`File::from_ptr`] means that it is up to the caller to ensure that the
143+
/// `&File` only exists while the reference count is positive.
144+
///
145+
/// * You can think of `fdget` as using an fd to look up an `ARef<File>` in the `struct
146+
/// files_struct` and create an `&File` from it. The "fd cannot be closed" rule is like the Rust
147+
/// rule "the `ARef<File>` must outlive the `&File`".
148+
///
149+
/// # Invariants
150+
///
151+
/// * Instances of this type are refcounted using the `f_count` field.
152+
/// * If an fd with active light refcounts is closed, then it must be the case that the file
153+
/// refcount is positive until there are no more light refcounts created from the fd that got
154+
/// closed.
155+
/// * A light refcount must be dropped before returning to userspace.
156+
#[repr(transparent)]
157+
pub struct File(Opaque<bindings::file>);
158+
159+
// SAFETY: By design, the only way to access a `File` is via an immutable reference or an `ARef`.
160+
// This means that the only situation in which a `File` can be accessed mutably is when the
161+
// refcount drops to zero and the destructor runs. It is safe for that to happen on any thread, so
162+
// it is ok for this type to be `Send`.
163+
unsafe impl Send for File {}
164+
165+
// SAFETY: All methods defined on `File` that take `&self` are safe to call even if other threads
166+
// are concurrently accessing the same `struct file`, because those methods either access immutable
167+
// properties or have proper synchronization to ensure that such accesses are safe.
168+
unsafe impl Sync for File {}
169+
170+
impl File {
171+
/// Constructs a new `struct file` wrapper from a file descriptor.
172+
///
173+
/// The file descriptor belongs to the current process.
174+
pub fn fget(fd: u32) -> Result<ARef<Self>, BadFdError> {
175+
// SAFETY: FFI call, there are no requirements on `fd`.
176+
let ptr = ptr::NonNull::new(unsafe { bindings::fget(fd) }).ok_or(BadFdError)?;
177+
178+
// SAFETY: `bindings::fget` either returns null or a valid pointer to a file, and we
179+
// checked for null above.
180+
//
181+
// INVARIANT: `bindings::fget` creates a refcount, and we pass ownership of the refcount to
182+
// the new `ARef<File>`.
183+
Ok(unsafe { ARef::from_raw(ptr.cast()) })
184+
}
185+
186+
/// Creates a reference to a [`File`] from a valid pointer.
187+
///
188+
/// # Safety
189+
///
190+
/// The caller must ensure that `ptr` points at a valid file and that the file's refcount is
191+
/// positive for the duration of 'a.
192+
pub unsafe fn from_ptr<'a>(ptr: *const bindings::file) -> &'a File {
193+
// SAFETY: The caller guarantees that the pointer is not dangling and stays valid for the
194+
// duration of 'a. The cast is okay because `File` is `repr(transparent)`.
195+
//
196+
// INVARIANT: The safety requirements guarantee that the refcount does not hit zero during
197+
// 'a.
198+
unsafe { &*ptr.cast() }
199+
}
200+
201+
/// Returns a raw pointer to the inner C struct.
202+
#[inline]
203+
pub fn as_ptr(&self) -> *mut bindings::file {
204+
self.0.get()
205+
}
206+
207+
/// Returns the flags associated with the file.
208+
///
209+
/// The flags are a combination of the constants in [`flags`].
210+
pub fn flags(&self) -> u32 {
211+
// This `read_volatile` is intended to correspond to a READ_ONCE call.
212+
//
213+
// SAFETY: The file is valid because the shared reference guarantees a nonzero refcount.
214+
//
215+
// TODO: Replace with `read_once` when available on the Rust side.
216+
unsafe { core::ptr::addr_of!((*self.as_ptr()).f_flags).read_volatile() }
217+
}
218+
}
219+
220+
// SAFETY: The type invariants guarantee that `File` is always ref-counted. This implementation
221+
// makes `ARef<File>` own a normal refcount.
222+
unsafe impl AlwaysRefCounted for File {
223+
fn inc_ref(&self) {
224+
// SAFETY: The existence of a shared reference means that the refcount is nonzero.
225+
unsafe { bindings::get_file(self.as_ptr()) };
226+
}
227+
228+
unsafe fn dec_ref(obj: ptr::NonNull<File>) {
229+
// SAFETY: To call this method, the caller passes us ownership of a normal refcount, so we
230+
// may drop it. The cast is okay since `File` has the same representation as `struct file`.
231+
unsafe { bindings::fput(obj.cast().as_ptr()) }
232+
}
233+
}
234+
235+
/// Represents the `EBADF` error code.
236+
///
237+
/// Used for methods that can only fail with `EBADF`.
238+
#[derive(Copy, Clone, Eq, PartialEq)]
239+
pub struct BadFdError;
240+
241+
impl From<BadFdError> for Error {
242+
fn from(_: BadFdError) -> Error {
243+
EBADF
244+
}
245+
}
246+
247+
impl core::fmt::Debug for BadFdError {
248+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
249+
f.pad("EBADF")
250+
}
251+
}

rust/kernel/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern crate self as kernel;
3434
mod allocator;
3535
mod build_assert;
3636
pub mod error;
37+
pub mod file;
3738
pub mod init;
3839
pub mod ioctl;
3940
#[cfg(CONFIG_KUNIT)]

0 commit comments

Comments
 (0)