sys_util: move round_to_page_size to sys_util

This function will be used elsewhere in gpu_display.

TEST=None
BUG=None

Change-Id: I58b820511ea5a55a53ad640fdfe7c96d2dbdc73b
Reviewed-on: https://chromium-review.googlesource.com/1105481
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
Zach Reizner 2018-06-18 16:18:52 -07:00 committed by chrome-bot
parent 22272dd4b2
commit 298b41cf82
2 changed files with 11 additions and 9 deletions

View file

@ -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<WlOp> {
const ID_OFFSET: u64 = 8;
const FLAGS_OFFSET: u64 = 12;
@ -639,7 +633,7 @@ impl WlVfd {
}
fn allocate(vm: VmRequester, size: u64) -> WlResult<WlVfd> {
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))?;

View file

@ -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)]