mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2024-12-29 17:25:38 +00:00
rust: file: add FileDescriptorReservation
Allow for the creation of a file descriptor in two steps: first, we reserve a slot for it, then we commit or drop the reservation. The first step may fail (e.g., the current process ran out of available slots), but commit and drop never fail (and are mutually exclusive). This is needed by Rust Binder when fds are sent from one process to another. It has to be a two-step process to properly handle the case where multiple fds are sent: The operation must fail or succeed atomically, which we achieve by first reserving the fds we need, and only installing the files once we have reserved enough fds to send the files. Fd reservations assume that the value of `current` does not change between the call to get_unused_fd_flags and the call to fd_install (or put_unused_fd). By not implementing the Send trait, this abstraction ensures that the `FileDescriptorReservation` cannot be moved into a different process. Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Co-developed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Trevor Gross <tmgross@umich.edu> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Alice Ryhl <aliceryhl@google.com> Link: https://lore.kernel.org/r/20240915-alice-file-v10-6-88484f7a3dcf@google.com Signed-off-by: Christian Brauner <brauner@kernel.org>
This commit is contained in:
parent
94d356c033
commit
5da9857b12
@ -11,7 +11,7 @@
|
||||
bindings,
|
||||
cred::Credential,
|
||||
error::{code::*, Error, Result},
|
||||
types::{ARef, AlwaysRefCounted, Opaque},
|
||||
types::{ARef, AlwaysRefCounted, NotThreadSafe, Opaque},
|
||||
};
|
||||
use core::ptr;
|
||||
|
||||
@ -368,6 +368,79 @@ fn deref(&self) -> &LocalFile {
|
||||
}
|
||||
}
|
||||
|
||||
/// A file descriptor reservation.
|
||||
///
|
||||
/// This allows the creation of a file descriptor in two steps: first, we reserve a slot for it,
|
||||
/// then we commit or drop the reservation. The first step may fail (e.g., the current process ran
|
||||
/// out of available slots), but commit and drop never fail (and are mutually exclusive).
|
||||
///
|
||||
/// Dropping the reservation happens in the destructor of this type.
|
||||
///
|
||||
/// # Invariants
|
||||
///
|
||||
/// The fd stored in this struct must correspond to a reserved file descriptor of the current task.
|
||||
pub struct FileDescriptorReservation {
|
||||
fd: u32,
|
||||
/// Prevent values of this type from being moved to a different task.
|
||||
///
|
||||
/// The `fd_install` and `put_unused_fd` functions assume that the value of `current` is
|
||||
/// unchanged since the call to `get_unused_fd_flags`. By adding this marker to this type, we
|
||||
/// prevent it from being moved across task boundaries, which ensures that `current` does not
|
||||
/// change while this value exists.
|
||||
_not_send: NotThreadSafe,
|
||||
}
|
||||
|
||||
impl FileDescriptorReservation {
|
||||
/// Creates a new file descriptor reservation.
|
||||
pub fn get_unused_fd_flags(flags: u32) -> Result<Self> {
|
||||
// SAFETY: FFI call, there are no safety requirements on `flags`.
|
||||
let fd: i32 = unsafe { bindings::get_unused_fd_flags(flags) };
|
||||
if fd < 0 {
|
||||
return Err(Error::from_errno(fd));
|
||||
}
|
||||
Ok(Self {
|
||||
fd: fd as u32,
|
||||
_not_send: NotThreadSafe,
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns the file descriptor number that was reserved.
|
||||
pub fn reserved_fd(&self) -> u32 {
|
||||
self.fd
|
||||
}
|
||||
|
||||
/// Commits the reservation.
|
||||
///
|
||||
/// The previously reserved file descriptor is bound to `file`. This method consumes the
|
||||
/// [`FileDescriptorReservation`], so it will not be usable after this call.
|
||||
pub fn fd_install(self, file: ARef<File>) {
|
||||
// SAFETY: `self.fd` was previously returned by `get_unused_fd_flags`. We have not yet used
|
||||
// the fd, so it is still valid, and `current` still refers to the same task, as this type
|
||||
// cannot be moved across task boundaries.
|
||||
//
|
||||
// Furthermore, the file pointer is guaranteed to own a refcount by its type invariants,
|
||||
// and we take ownership of that refcount by not running the destructor below.
|
||||
// Additionally, the file is known to not have any non-shared `fdget_pos` calls, so even if
|
||||
// this process starts using the file position, this will not result in a data race on the
|
||||
// file position.
|
||||
unsafe { bindings::fd_install(self.fd, file.as_ptr()) };
|
||||
|
||||
// `fd_install` consumes both the file descriptor and the file reference, so we cannot run
|
||||
// the destructors.
|
||||
core::mem::forget(self);
|
||||
core::mem::forget(file);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for FileDescriptorReservation {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: By the type invariants of this type, `self.fd` was previously returned by
|
||||
// `get_unused_fd_flags`. We have not yet used the fd, so it is still valid, and `current`
|
||||
// still refers to the same task, as this type cannot be moved across task boundaries.
|
||||
unsafe { bindings::put_unused_fd(self.fd) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the `EBADF` error code.
|
||||
///
|
||||
/// Used for methods that can only fail with `EBADF`.
|
||||
|
Loading…
Reference in New Issue
Block a user