diff --git a/devices/src/virtio/wl.rs b/devices/src/virtio/wl.rs index dc736defd2..1ec6873e7f 100644 --- a/devices/src/virtio/wl.rs +++ b/devices/src/virtio/wl.rs @@ -59,7 +59,7 @@ use data_model::*; use data_model::VolatileMemoryError; use sys_util::{Error, Result, EventFd, Scm, SharedMemory, GuestAddress, GuestMemory, - GuestMemoryError, PollContext, PollToken, FileFlags, pipe}; + GuestMemoryError, PollContext, PollToken, FileFlags, pipe, round_up_to_page_size}; #[cfg(feature = "wl-dmabuf")] use sys_util::ioctl_with_ref; @@ -118,12 +118,6 @@ struct dma_buf_sync { #[cfg(feature = "wl-dmabuf")] ioctl_iow_nr!(DMA_BUF_IOCTL_SYNC, DMA_BUF_IOCTL_BASE, 0, dma_buf_sync); -const PAGE_MASK: u64 = 0x0fff; - -fn round_to_page_size(v: u64) -> u64 { - (v + PAGE_MASK) & !PAGE_MASK -} - fn parse_new(addr: GuestAddress, mem: &GuestMemory) -> WlResult { const ID_OFFSET: u64 = 8; const FLAGS_OFFSET: u64 = 12; @@ -639,7 +633,7 @@ impl WlVfd { } fn allocate(vm: VmRequester, size: u64) -> WlResult { - let size_page_aligned = round_to_page_size(size); + let size_page_aligned = round_up_to_page_size(size as usize) as u64; let mut vfd_shm = SharedMemory::new(Some(CStr::from_bytes_with_nul(b"virtwl_alloc\0") .unwrap())) .map_err(WlError::NewAlloc)?; @@ -728,7 +722,7 @@ impl WlVfd { // fails, we assume it's a socket or pipe with read/write semantics. match fd.seek(SeekFrom::End(0)) { Ok(fd_size) => { - let size = round_to_page_size(fd_size); + let size = round_up_to_page_size(fd_size as usize) as u64; let register_response = vm.request(VmRequest::RegisterMemory(MaybeOwnedFd::Borrowed(fd.as_raw_fd()), size as usize))?; diff --git a/sys_util/src/lib.rs b/sys_util/src/lib.rs index 701622bcbe..caef4c20e8 100644 --- a/sys_util/src/lib.rs +++ b/sys_util/src/lib.rs @@ -75,6 +75,14 @@ pub fn pagesize() -> usize { unsafe { sysconf(_SC_PAGESIZE) as usize } } + +/// Uses the system's page size in bytes to round the given value up to the nearest page boundary. +#[inline(always)] +pub fn round_up_to_page_size(v: usize) -> usize { + let page_mask = pagesize() - 1; + (v + page_mask) & !page_mask +} + /// This bypasses `libc`'s caching `getpid(2)` wrapper which can be invalid if a raw clone was used /// elsewhere. #[inline(always)]