From fa94b1b29c7aa8927ffa29aa3bf3e66e6e4d5342 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Mon, 2 Jun 2025 18:26:45 +0200 Subject: [PATCH] rusticl: check the returned pointer of mmap Prior Linux 4.17 MAP_FIXED_NOREPLACE might not be respected and might return a pointer different than the requested one. Fixes: da4de8d7e33 ("rusticl: add support for coarse-grain buffer SVM") Part-of: --- src/gallium/frontends/rusticl/core/context.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/gallium/frontends/rusticl/core/context.rs b/src/gallium/frontends/rusticl/core/context.rs index 6eef5377213..5643f54e196 100644 --- a/src/gallium/frontends/rusticl/core/context.rs +++ b/src/gallium/frontends/rusticl/core/context.rs @@ -319,7 +319,7 @@ impl Context { unreachable!("SVM supported only on Linux") } - let res = unsafe { + let mut res = unsafe { mmap( vma.get() as usize as *mut c_void, size.get() as usize, @@ -335,6 +335,14 @@ impl Context { return Err(CL_OUT_OF_HOST_MEMORY); } + if res as usize != vma.get() as usize { + unsafe { + let ret = munmap(res, size.get() as usize); + debug_assert_eq!(0, ret); + } + res = ptr::null_mut(); + } + res.cast() } else { unsafe { alloc::alloc(layout) }.cast()