util/rust: Add handle type detection to descriptor API

This enhances the DescriptorType enum to include the
memory handle type (DMABUF vs SHM) alongside the size,
allowing consumers to differentiate between DMA-BUF and
shared memory file descriptors without redundant code.

Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/38677>
This commit is contained in:
Dorinda Bassey 2025-11-26 14:01:38 +01:00 committed by Marge Bot
parent 6b3f115c68
commit 4ace478545
2 changed files with 23 additions and 2 deletions

View file

@ -86,7 +86,7 @@ pub const WAIT_CONTEXT_MAX: usize = 16;
pub enum DescriptorType {
Unknown,
Memory(u32),
Memory(u32, u32), // (size, handle_type)
WritePipe,
}

View file

@ -1,6 +1,7 @@
// Copyright 2025 Google
// SPDX-License-Identifier: MIT
use std::fs::read_link;
use std::fs::File;
use std::io::Error;
use std::io::ErrorKind;
@ -22,6 +23,8 @@ use crate::descriptor::AsRawDescriptor;
use crate::descriptor::FromRawDescriptor;
use crate::descriptor::IntoRawDescriptor;
use crate::DescriptorType;
use crate::MESA_HANDLE_TYPE_MEM_DMABUF;
use crate::MESA_HANDLE_TYPE_MEM_SHM;
pub type RawDescriptor = RawFd;
pub const DEFAULT_RAW_DESCRIPTOR: RawDescriptor = -1;
@ -43,7 +46,10 @@ impl OwnedDescriptor {
let size: u32 = seek_size
.try_into()
.map_err(|_| Error::from(ErrorKind::Unsupported))?;
Ok(DescriptorType::Memory(size))
let handle_type = self.get_memory_handle_type()?;
Ok(DescriptorType::Memory(size, handle_type))
}
_ => {
let flags = fcntl_getfl(&self.owned)?;
@ -54,6 +60,21 @@ impl OwnedDescriptor {
}
}
}
fn get_memory_handle_type(&self) -> Result<u32> {
let fd_path = read_link(format!("/proc/self/fd/{}", self.as_raw_descriptor()))
.map_err(|_| Error::from(ErrorKind::Unsupported))?;
let path_str = fd_path.to_string_lossy();
if path_str.starts_with("/dmabuf:") {
Ok(MESA_HANDLE_TYPE_MEM_DMABUF)
} else if path_str.starts_with("/memfd:") {
Ok(MESA_HANDLE_TYPE_MEM_SHM)
} else {
// Default to SHM for unknown types
Ok(MESA_HANDLE_TYPE_MEM_SHM)
}
}
}
impl AsRawDescriptor for OwnedDescriptor {