Skip to content

Commit

Permalink
Move anonymous mappings to server code
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <[email protected]>
  • Loading branch information
SUPERCILEX committed Jul 31, 2024
1 parent a4c4ca2 commit c833ef8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 28 deletions.
1 change: 0 additions & 1 deletion core/api.golden
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,6 @@ pub fn clipboard_history_core::ring::Mmap::from<Fd: std::os::fd::owned::AsFd>(fd
pub const fn clipboard_history_core::ring::Mmap::is_empty(&self) -> bool
pub const fn clipboard_history_core::ring::Mmap::len(&self) -> usize
pub fn clipboard_history_core::ring::Mmap::new<Fd: std::os::fd::owned::AsFd>(fd: Fd, len: usize) -> rustix::io::errno::Result<Self>
pub fn clipboard_history_core::ring::Mmap::new_anon(len: usize) -> rustix::io::errno::Result<Self>
pub const fn clipboard_history_core::ring::Mmap::ptr(&self) -> core::ptr::non_null::NonNull<u8>
pub fn clipboard_history_core::ring::Mmap::remap(&mut self, len: usize) -> rustix::io::errno::Result<()>
impl core::convert::AsRef<[u8]> for clipboard_history_core::ring::Mmap
Expand Down
18 changes: 1 addition & 17 deletions core/src/ring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::{

use rustix::{
fs::{openat, statx, AtFlags, Mode, OFlags, StatxFlags, CWD},
mm::{mmap, mmap_anonymous, mremap, munmap, MapFlags, MremapFlags, ProtFlags},
mm::{mmap, mremap, munmap, MapFlags, MremapFlags, ProtFlags},
path::Arg,
};

Expand Down Expand Up @@ -176,22 +176,6 @@ impl Mmap {
})
}

pub fn new_anon(len: usize) -> rustix::io::Result<Self> {
Ok(Self {
ptr: unsafe {
NonNull::new_unchecked(mmap_anonymous(
ptr::null_mut(),
len,
ProtFlags::READ | ProtFlags::WRITE,
MapFlags::PRIVATE,
)?)
}
.cast(),
requested_len: len,
backing_len: len,
})
}

pub fn remap(&mut self, len: usize) -> rustix::io::Result<()> {
if len >= self.requested_len && len <= self.backing_len {
self.requested_len = len;
Expand Down
57 changes: 47 additions & 10 deletions server/src/io_uring.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::io;
use std::{io, ptr, ptr::NonNull};

use io_uring::Submitter;
use ringboard_core::ring::Mmap;
use rustix::io_uring::io_uring_buf;
use rustix::{
io_uring::io_uring_buf,
mm::{mmap_anonymous, munmap, MapFlags, ProtFlags},
};

use crate::io_uring::buf_ring::BufRing;

Expand All @@ -18,13 +20,42 @@ pub fn register_buf_ring(
)
};

let ring = Mmap::new_anon(bytes().ok_or(io::ErrorKind::InvalidInput)?)?;
let ring = MmapAnon::new(bytes().ok_or(io::ErrorKind::InvalidInput)?)?;
unsafe {
submitter.register_buf_ring(ring.ptr().as_ptr() as u64, ring_entries, bgid)?;
submitter.register_buf_ring(ring.ptr.as_ptr() as u64, ring_entries, bgid)?;
}
Ok(BufRing::init(ring, ring_entries, entry_size, bgid))
}

#[derive(Debug)]
pub struct MmapAnon {
ptr: NonNull<u8>,
len: usize,
}

impl MmapAnon {
pub fn new(len: usize) -> io::Result<Self> {
Ok(Self {
ptr: unsafe {
NonNull::new_unchecked(mmap_anonymous(
ptr::null_mut(),
len,
ProtFlags::READ | ProtFlags::WRITE,
MapFlags::PRIVATE,
)?)
}
.cast(),
len,
})
}
}

impl Drop for MmapAnon {
fn drop(&mut self) {
let _ = unsafe { munmap(self.ptr.as_ptr().cast(), self.len) };
}
}

pub mod types {
use std::slice;

Expand Down Expand Up @@ -177,18 +208,24 @@ pub mod buf_ring {
};

use io_uring::Submitter;
use ringboard_core::ring::Mmap;
use rustix::io_uring::{io_uring_buf, IORING_CQE_BUFFER_SHIFT};

use crate::io_uring::MmapAnon;

pub struct BufRing {
ring: Mmap,
ring: MmapAnon,
ring_entries: u16,
entry_size: u32,
group_id: u16,
}

impl BufRing {
pub(super) fn init(ring: Mmap, ring_entries: u16, entry_size: u32, group_id: u16) -> Self {
pub(super) fn init(
ring: MmapAnon,
ring_entries: u16,
entry_size: u32,
group_id: u16,
) -> Self {
let mut this = Self {
ring,
ring_entries,
Expand All @@ -209,8 +246,8 @@ pub mod buf_ring {

#[allow(clippy::cast_ptr_alignment)]
pub fn submissions(&mut self) -> BufRingSubmissions<'_> {
let ring_ptr = self.ring.ptr().as_ptr().cast::<io_uring_buf>();
let tail_ptr = unsafe { self.ring.ptr().as_ptr().add(8 + 4 + 2) };
let ring_ptr = self.ring.ptr.as_ptr().cast::<io_uring_buf>();
let tail_ptr = unsafe { self.ring.ptr.as_ptr().add(8 + 4 + 2) };
let ring_entries = usize::from(self.ring_entries);
BufRingSubmissions {
ring_ptr,
Expand Down

0 comments on commit c833ef8

Please sign in to comment.