From 8b0c7f12046c2ebadcee5e7ab813d5a34ddc08c6 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 31 Aug 2023 12:21:49 -0400 Subject: [PATCH] async_perf_event_array: access inner through async Avoid holding onto raw file descriptors. Remove some implied bounds (BorrowMut implies Borrow). --- aya/src/maps/perf/async_perf_event_array.rs | 52 +++++++++------------ aya/src/maps/perf/perf_event_array.rs | 7 ++- xtask/public-api/aya.txt | 16 +++---- 3 files changed, 33 insertions(+), 42 deletions(-) diff --git a/aya/src/maps/perf/async_perf_event_array.rs b/aya/src/maps/perf/async_perf_event_array.rs index 7c942e26..83e9484a 100644 --- a/aya/src/maps/perf/async_perf_event_array.rs +++ b/aya/src/maps/perf/async_perf_event_array.rs @@ -1,8 +1,5 @@ use bytes::BytesMut; -use std::{ - borrow::{Borrow, BorrowMut}, - os::fd::{AsRawFd as _, RawFd}, -}; +use std::borrow::{Borrow, BorrowMut}; // See https://doc.rust-lang.org/cargo/reference/features.html#mutually-exclusive-features. // @@ -92,7 +89,7 @@ pub struct AsyncPerfEventArray { perf_map: PerfEventArray, } -impl + Borrow> AsyncPerfEventArray { +impl> AsyncPerfEventArray { /// Opens the perf buffer at the given index. /// /// The returned buffer will receive all the events eBPF programs send at the given index. @@ -103,16 +100,11 @@ impl + Borrow> AsyncPerfEventArray { ) -> Result, PerfBufferError> { let Self { perf_map } = self; let buf = perf_map.open(index, page_count)?; - let fd = buf.as_raw_fd(); - Ok(AsyncPerfEventArrayBuffer { - buf, - - #[cfg(feature = "async_tokio")] - async_tokio_fd: AsyncFd::new(fd)?, - - #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] - async_std_fd: Async::new(fd)?, - }) + #[cfg(feature = "async_tokio")] + let buf = AsyncFd::new(buf)?; + #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] + let buf = Async::new(buf)?; + Ok(AsyncPerfEventArrayBuffer { buf }) } } @@ -131,17 +123,18 @@ impl> AsyncPerfEventArray { /// /// See the [`AsyncPerfEventArray` documentation](AsyncPerfEventArray) for an overview of how to /// use perf buffers. -pub struct AsyncPerfEventArrayBuffer { +pub struct AsyncPerfEventArrayBuffer> { + #[cfg(not(any(feature = "async_tokio", feature = "async_std")))] buf: PerfEventArrayBuffer, #[cfg(feature = "async_tokio")] - async_tokio_fd: AsyncFd, + buf: AsyncFd>, #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] - async_std_fd: Async, + buf: Async>, } -impl + Borrow> AsyncPerfEventArrayBuffer { +impl> AsyncPerfEventArrayBuffer { /// Reads events from the buffer. /// /// This method reads events into the provided slice of buffers, filling @@ -155,21 +148,20 @@ impl + Borrow> AsyncPerfEventArrayBuffer { &mut self, buffers: &mut [BytesMut], ) -> Result { - let Self { - buf, - #[cfg(feature = "async_tokio")] - async_tokio_fd, - #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] - async_std_fd, - } = self; + let Self { buf } = self; loop { #[cfg(feature = "async_tokio")] - let mut guard = async_tokio_fd.readable_mut().await?; + let mut guard = buf.readable_mut().await?; + #[cfg(feature = "async_tokio")] + let buf = guard.get_inner_mut(); #[cfg(all(not(feature = "async_tokio"), feature = "async_std"))] - if !buf.readable() { - async_std_fd.readable().await?; - } + let buf = { + if !buf.get_ref().readable() { + buf.readable().await?; + } + buf.get_mut() + }; let events = buf.read_events(buffers)?; const EMPTY: Events = Events { read: 0, lost: 0 }; diff --git a/aya/src/maps/perf/perf_event_array.rs b/aya/src/maps/perf/perf_event_array.rs index bde255ed..1d19d373 100644 --- a/aya/src/maps/perf/perf_event_array.rs +++ b/aya/src/maps/perf/perf_event_array.rs @@ -31,7 +31,7 @@ pub struct PerfEventArrayBuffer { buf: PerfBuffer, } -impl + Borrow> PerfEventArrayBuffer { +impl> PerfEventArrayBuffer { /// Returns true if the buffer contains events that haven't been read. pub fn readable(&self) -> bool { self.buf.readable() @@ -55,7 +55,7 @@ impl + Borrow> PerfEventArrayBuffer { } } -impl + Borrow> AsRawFd for PerfEventArrayBuffer { +impl> AsRawFd for PerfEventArrayBuffer { fn as_raw_fd(&self) -> RawFd { self.buf.as_raw_fd() } @@ -169,7 +169,7 @@ impl> PerfEventArray { } } -impl + Borrow> PerfEventArray { +impl> PerfEventArray { /// Opens the perf buffer at the given index. /// /// The returned buffer will receive all the events eBPF programs send at the given index. @@ -180,7 +180,6 @@ impl + Borrow> PerfEventArray { ) -> Result, PerfBufferError> { // FIXME: keep track of open buffers - // this cannot fail as new() checks that the fd is open let map_data: &MapData = self.map.deref().borrow(); let map_fd = map_data.fd; let buf = PerfBuffer::open(index, self.page_size, page_count.unwrap_or(2))?; diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 388024ce..ae3c865a 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -372,7 +372,7 @@ pub fn aya::maps::perf::PerfBufferError::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::perf::PerfBufferError pub fn aya::maps::perf::PerfBufferError::from(t: T) -> T pub struct aya::maps::perf::AsyncPerfEventArray -impl + core::borrow::Borrow> aya::maps::perf::AsyncPerfEventArray +impl> aya::maps::perf::AsyncPerfEventArray pub fn aya::maps::perf::AsyncPerfEventArray::open(&mut self, index: u32, page_count: core::option::Option) -> core::result::Result, aya::maps::perf::PerfBufferError> impl core::convert::TryFrom for aya::maps::perf::AsyncPerfEventArray pub type aya::maps::perf::AsyncPerfEventArray::Error = aya::maps::MapError @@ -404,8 +404,8 @@ impl core::borrow::BorrowMut for aya::maps::perf::AsyncPerfEventArray w pub fn aya::maps::perf::AsyncPerfEventArray::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::perf::AsyncPerfEventArray pub fn aya::maps::perf::AsyncPerfEventArray::from(t: T) -> T -pub struct aya::maps::perf::AsyncPerfEventArrayBuffer -impl + core::borrow::Borrow> aya::maps::perf::AsyncPerfEventArrayBuffer +pub struct aya::maps::perf::AsyncPerfEventArrayBuffer> +impl> aya::maps::perf::AsyncPerfEventArrayBuffer pub async fn aya::maps::perf::AsyncPerfEventArrayBuffer::read_events(&mut self, buffers: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result impl core::marker::Send for aya::maps::perf::AsyncPerfEventArrayBuffer where T: core::marker::Send + core::marker::Sync impl core::marker::Sync for aya::maps::perf::AsyncPerfEventArrayBuffer where T: core::marker::Send + core::marker::Sync @@ -460,7 +460,7 @@ pub fn aya::maps::perf::Events::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::perf::Events pub fn aya::maps::perf::Events::from(t: T) -> T pub struct aya::maps::perf::PerfEventArray -impl + core::borrow::Borrow> aya::maps::perf::PerfEventArray +impl> aya::maps::perf::PerfEventArray pub fn aya::maps::perf::PerfEventArray::open(&mut self, index: u32, page_count: core::option::Option) -> core::result::Result, aya::maps::perf::PerfBufferError> impl core::convert::TryFrom for aya::maps::perf::PerfEventArray pub type aya::maps::perf::PerfEventArray::Error = aya::maps::MapError @@ -493,10 +493,10 @@ pub fn aya::maps::perf::PerfEventArray::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::perf::PerfEventArray pub fn aya::maps::perf::PerfEventArray::from(t: T) -> T pub struct aya::maps::perf::PerfEventArrayBuffer -impl + core::borrow::Borrow> aya::maps::perf::PerfEventArrayBuffer +impl> aya::maps::perf::PerfEventArrayBuffer pub fn aya::maps::perf::PerfEventArrayBuffer::read_events(&mut self, out_bufs: &mut [bytes::bytes_mut::BytesMut]) -> core::result::Result pub fn aya::maps::perf::PerfEventArrayBuffer::readable(&self) -> bool -impl + core::borrow::Borrow> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer +impl> std::os::fd::raw::AsRawFd for aya::maps::perf::PerfEventArrayBuffer pub fn aya::maps::perf::PerfEventArrayBuffer::as_raw_fd(&self) -> std::os::fd::raw::RawFd impl core::marker::Send for aya::maps::perf::PerfEventArrayBuffer where T: core::marker::Send + core::marker::Sync impl core::marker::Sync for aya::maps::perf::PerfEventArrayBuffer where T: core::marker::Send + core::marker::Sync @@ -1067,7 +1067,7 @@ pub fn aya::maps::array::Array::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::array::Array pub fn aya::maps::array::Array::from(t: T) -> T pub struct aya::maps::AsyncPerfEventArray -impl + core::borrow::Borrow> aya::maps::perf::AsyncPerfEventArray +impl> aya::maps::perf::AsyncPerfEventArray pub fn aya::maps::perf::AsyncPerfEventArray::open(&mut self, index: u32, page_count: core::option::Option) -> core::result::Result, aya::maps::perf::PerfBufferError> impl core::convert::TryFrom for aya::maps::perf::AsyncPerfEventArray pub type aya::maps::perf::AsyncPerfEventArray::Error = aya::maps::MapError @@ -1460,7 +1460,7 @@ pub fn aya::maps::PerCpuValues::borrow_mut(&mut self) -> &mut T impl core::convert::From for aya::maps::PerCpuValues pub fn aya::maps::PerCpuValues::from(t: T) -> T pub struct aya::maps::PerfEventArray -impl + core::borrow::Borrow> aya::maps::perf::PerfEventArray +impl> aya::maps::perf::PerfEventArray pub fn aya::maps::perf::PerfEventArray::open(&mut self, index: u32, page_count: core::option::Option) -> core::result::Result, aya::maps::perf::PerfBufferError> impl core::convert::TryFrom for aya::maps::perf::PerfEventArray pub type aya::maps::perf::PerfEventArray::Error = aya::maps::MapError