<div dir="ltr">Petri,<div><br></div><div>Got it. I just resubmitted the mmap64() -> mmap() patch:</div><div><a href="https://patchwork.freedesktop.org/patch/506163/">https://patchwork.freedesktop.org/patch/506163/</a><br></div><div><br></div><div>Jake Freeland</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 6, 2022 at 5:45 AM Petri Latvala <<a href="mailto:petri.latvala@intel.com">petri.latvala@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Wed, Oct 05, 2022 at 07:02:01PM -0500, Jake Freeland wrote:<br>
> The mmap64() function exists to point 32-bit architectures to an mmap<br>
> interface with the capability to address a 64-bit memory address space.<br>
> This patch replaces all instances of mmap64() with drm_mmap() to<br>
> ensure that the correct mmap interface is used on varying architectures<br>
> and operating systems.<br>
> <br>
> Signed-off-by: Jake Freeland <<a href="mailto:jfree@freebsd.org" target="_blank">jfree@freebsd.org</a>><br>
> ---<br>
> include/drm-uapi/libdrm.h | 89 ++++++++++++++++++++++++++++++++++++<br>
> lib/i915/gem_mman.c | 5 +-<br>
> lib/igt_vgem.c | 3 +-<br>
> tests/i915/gem_mmap_gtt.c | 23 +++++-----<br>
> tests/i915/gem_mmap_offset.c | 13 +++---<br>
> 5 files changed, 113 insertions(+), 20 deletions(-)<br>
> create mode 100644 include/drm-uapi/libdrm.h<br>
<br>
Ah yeah, that's one snag with just using drm_mmap. The function is<br>
private.<br>
<br>
We can't copy it to include/drm-uapi/, as it is not kernel uapi. And<br>
definitely not with a misleading name.<br>
<br>
Let's go with the original patch that changes mmap64 to mmap. If bug<br>
reports arise that someone's distro doesn't default to<br>
_FILE_OFFSET_BITS=64 on 32bit platforms, we can tune the build system<br>
then.<br>
<br>
<br>
-- <br>
Petri Latvala<br>
<br>
<br>
> <br>
> diff --git a/include/drm-uapi/libdrm.h b/include/drm-uapi/libdrm.h<br>
> new file mode 100644<br>
> index 00000000..acfada5c<br>
> --- /dev/null<br>
> +++ b/include/drm-uapi/libdrm.h<br>
> @@ -0,0 +1,89 @@<br>
> +/*<br>
> + * Copyright © 2014 NVIDIA Corporation<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the "Software"),<br>
> + * to deal in the Software without restriction, including without limitation<br>
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
> + * and/or sell copies of the Software, and to permit persons to whom the<br>
> + * Software is furnished to do so, subject to the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice shall be included in<br>
> + * all copies or substantial portions of the Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR<br>
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
> + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
> + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
> + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR<br>
> + * OTHER DEALINGS IN THE SOFTWARE.<br>
> + */<br>
> +<br>
> +#ifndef LIBDRM_LIBDRM_H<br>
> +#define LIBDRM_LIBDRM_H<br>
> +<br>
> +#if defined(HAVE_VISIBILITY)<br>
> +# define drm_private __attribute__((visibility("hidden")))<br>
> +# define drm_public __attribute__((visibility("default")))<br>
> +#else<br>
> +# define drm_private<br>
> +# define drm_public<br>
> +#endif<br>
> +<br>
> +<br>
> +/**<br>
> + * Static (compile-time) assertion.<br>
> + * Basically, use COND to dimension an array. If COND is false/zero the<br>
> + * array size will be -1 and we'll get a compilation error.<br>
> + */<br>
> +#define STATIC_ASSERT(COND) \<br>
> + do { \<br>
> + (void) sizeof(char [1 - 2*!(COND)]); \<br>
> + } while (0)<br>
> +<br>
> +<br>
> +#include <sys/mman.h><br>
> +<br>
> +#if defined(ANDROID)<br>
> +#include <errno.h> /* for EINVAL */<br>
> +<br>
> +extern void *__mmap2(void *, size_t, int, int, int, size_t);<br>
> +<br>
> +static inline void *drm_mmap(void *addr, size_t length, int prot, int flags,<br>
> + int fd, loff_t offset)<br>
> +{<br>
> + /* offset must be aligned to 4096 (not necessarily the page size) */<br>
> + if (offset & 4095) {<br>
> + errno = EINVAL;<br>
> + return MAP_FAILED;<br>
> + }<br>
> +<br>
> + return __mmap2(addr, length, prot, flags, fd, (size_t) (offset >> 12));<br>
> +}<br>
> +<br>
> +# define drm_munmap(addr, length) \<br>
> + munmap(addr, length)<br>
> +<br>
> +<br>
> +#else<br>
> +<br>
> +/* assume large file support exists */<br>
> +# define drm_mmap(addr, length, prot, flags, fd, offset) \<br>
> + mmap(addr, length, prot, flags, fd, offset)<br>
> +<br>
> +<br>
> +static inline int drm_munmap(void *addr, size_t length)<br>
> +{<br>
> + /* Copied from configure code generated by AC_SYS_LARGEFILE */<br>
> +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + \<br>
> + (((off_t) 1 << 31) << 31))<br>
> + STATIC_ASSERT(LARGE_OFF_T % 2147483629 == 721 &&<br>
> + LARGE_OFF_T % 2147483647 == 1);<br>
> +#undef LARGE_OFF_T<br>
> +<br>
> + return munmap(addr, length);<br>
> +}<br>
> +#endif<br>
> +<br>
> +#endif<br>
> diff --git a/lib/i915/gem_mman.c b/lib/i915/gem_mman.c<br>
> index aa9ac6f3..4734f2af 100644<br>
> --- a/lib/i915/gem_mman.c<br>
> +++ b/lib/i915/gem_mman.c<br>
> @@ -31,6 +31,7 @@<br>
> #include "igt_device.h"<br>
> #include "ioctl_wrappers.h"<br>
> #include "intel_chipset.h"<br>
> +#include "libdrm.h"<br>
> <br>
> #include "gem_create.h"<br>
> #include "gem_mman.h"<br>
> @@ -118,7 +119,7 @@ void *__gem_mmap__gtt(int fd, uint32_t handle, uint64_t size, unsigned prot)<br>
> if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))<br>
> return NULL;<br>
> <br>
> - ptr = mmap64(0, size, prot, MAP_SHARED, fd, mmap_arg.offset);<br>
> + ptr = drm_mmap(0, size, prot, MAP_SHARED, fd, mmap_arg.offset);<br>
> if (ptr == MAP_FAILED)<br>
> ptr = NULL;<br>
> else<br>
> @@ -331,7 +332,7 @@ void *__gem_mmap_offset(int fd, uint32_t handle, uint64_t offset, uint64_t size,<br>
> if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, &arg))<br>
> return NULL;<br>
> <br>
> - ptr = mmap64(0, size, prot, MAP_SHARED, fd, arg.offset + offset);<br>
> + ptr = drm_mmap(0, size, prot, MAP_SHARED, fd, arg.offset + offset);<br>
> <br>
> if (ptr == MAP_FAILED)<br>
> ptr = NULL;<br>
> diff --git a/lib/igt_vgem.c b/lib/igt_vgem.c<br>
> index 7f933b23..a35b3cd3 100644<br>
> --- a/lib/igt_vgem.c<br>
> +++ b/lib/igt_vgem.c<br>
> @@ -29,6 +29,7 @@<br>
> #include "igt_vgem.h"<br>
> #include "igt_core.h"<br>
> #include "ioctl_wrappers.h"<br>
> +#include "libdrm.h"<br>
> <br>
> /**<br>
> * SECTION:igt_vgem<br>
> @@ -76,7 +77,7 @@ void *__vgem_mmap(int fd, struct vgem_bo *bo, unsigned prot)<br>
> if (drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &arg))<br>
> return NULL;<br>
> <br>
> - ptr = mmap64(0, bo->size, prot, MAP_SHARED, fd, arg.offset);<br>
> + ptr = drm_mmap(0, bo->size, prot, MAP_SHARED, fd, arg.offset);<br>
> if (ptr == MAP_FAILED)<br>
> return NULL;<br>
> <br>
> diff --git a/tests/i915/gem_mmap_gtt.c b/tests/i915/gem_mmap_gtt.c<br>
> index e39b9047..643f2f30 100644<br>
> --- a/tests/i915/gem_mmap_gtt.c<br>
> +++ b/tests/i915/gem_mmap_gtt.c<br>
> @@ -46,6 +46,7 @@<br>
> #include "igt.h"<br>
> #include "igt_sysfs.h"<br>
> #include "igt_x86.h"<br>
> +#include "libdrm.h"<br>
> #include "sw_sync.h"<br>
> <br>
> #ifndef PAGE_SIZE<br>
> @@ -113,11 +114,11 @@ test_access(int fd)<br>
> mmap_arg.handle = handle;<br>
> do_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg);<br>
> <br>
> - igt_assert(mmap64(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> + igt_assert(drm_mmap(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> MAP_SHARED, fd, mmap_arg.offset));<br>
> <br>
> /* Check that the same offset on the other fd doesn't work. */<br>
> - igt_assert(mmap64(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> + igt_assert(drm_mmap(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> MAP_SHARED, fd2, mmap_arg.offset) == MAP_FAILED);<br>
> igt_assert(errno == EACCES);<br>
> <br>
> @@ -128,7 +129,7 @@ test_access(int fd)<br>
> <br>
> /* Recheck that it works after flink. */<br>
> /* Check that the same offset on the other fd doesn't work. */<br>
> - igt_assert(mmap64(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> + igt_assert(drm_mmap(0, OBJECT_SIZE, PROT_READ | PROT_WRITE,<br>
> MAP_SHARED, fd2, mmap_arg.offset));<br>
> }<br>
> <br>
> @@ -159,11 +160,11 @@ test_short(int fd)<br>
> for (pages = 1; pages <= OBJECT_SIZE / PAGE_SIZE; pages <<= 1) {<br>
> uint8_t *r, *w;<br>
> <br>
> - w = mmap64(0, pages * PAGE_SIZE, PROT_READ | PROT_WRITE,<br>
> + w = drm_mmap(0, pages * PAGE_SIZE, PROT_READ | PROT_WRITE,<br>
> MAP_SHARED, fd, mmap_arg.offset);<br>
> igt_assert(w != MAP_FAILED);<br>
> <br>
> - r = mmap64(0, pages * PAGE_SIZE, PROT_READ,<br>
> + r = drm_mmap(0, pages * PAGE_SIZE, PROT_READ,<br>
> MAP_SHARED, fd, mmap_arg.offset);<br>
> igt_assert(r != MAP_FAILED);<br>
> <br>
> @@ -384,13 +385,13 @@ test_isolation(int i915)<br>
> <br>
> close(B);<br>
> <br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> igt_assert(ptr != MAP_FAILED);<br>
> munmap(ptr, 4096);<br>
> <br>
> close(A);<br>
> <br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> igt_assert(ptr == MAP_FAILED);<br>
> }<br>
> <br>
> @@ -400,7 +401,7 @@ test_close_race(int i915)<br>
> const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);<br>
> _Atomic uint32_t *handles;<br>
> <br>
> - handles = mmap64(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> + handles = drm_mmap(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> igt_assert(handles != MAP_FAILED);<br>
> <br>
> igt_fork(child, ncpus + 1) {<br>
> @@ -418,7 +419,7 @@ test_close_race(int i915)<br>
> &mmap_arg) != -1) {<br>
> void *ptr;<br>
> <br>
> - ptr = mmap64(0, 4096,<br>
> + ptr = drm_mmap(0, 4096,<br>
> PROT_WRITE, MAP_SHARED, i915,<br>
> mmap_arg.offset);<br>
> if (ptr != MAP_FAILED) {<br>
> @@ -444,7 +445,7 @@ test_flink_race(int i915)<br>
> const int ncpus = sysconf(_SC_NPROCESSORS_ONLN);<br>
> _Atomic uint32_t *handles;<br>
> <br>
> - handles = mmap64(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> + handles = drm_mmap(0, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> igt_assert(handles != MAP_FAILED);<br>
> <br>
> igt_fork(child, ncpus + 1) {<br>
> @@ -469,7 +470,7 @@ test_flink_race(int i915)<br>
> &mmap_arg) != -1) {<br>
> void *ptr;<br>
> <br>
> - ptr = mmap64(0, 4096,<br>
> + ptr = drm_mmap(0, 4096,<br>
> PROT_WRITE, MAP_SHARED, fd,<br>
> mmap_arg.offset);<br>
> if (ptr != MAP_FAILED) {<br>
> diff --git a/tests/i915/gem_mmap_offset.c b/tests/i915/gem_mmap_offset.c<br>
> index 2b416edd..0a6d41aa 100644<br>
> --- a/tests/i915/gem_mmap_offset.c<br>
> +++ b/tests/i915/gem_mmap_offset.c<br>
> @@ -38,6 +38,7 @@<br>
> #include "i915/intel_memory_region.h"<br>
> #include "igt.h"<br>
> #include "igt_x86.h"<br>
> +#include "libdrm.h"<br>
> <br>
> IGT_TEST_DESCRIPTION("Basic MMAP_OFFSET IOCTL tests for mem regions\n");<br>
> <br>
> @@ -67,7 +68,7 @@ __mmap_offset(int i915, uint32_t handle, uint64_t offset, uint64_t size,<br>
> if (mmap_offset_ioctl(i915, &arg))<br>
> return NULL;<br>
> <br>
> - ptr = mmap64(0, size, prot, MAP_SHARED, i915, arg.offset + offset);<br>
> + ptr = drm_mmap(0, size, prot, MAP_SHARED, i915, arg.offset + offset);<br>
> if (ptr == MAP_FAILED)<br>
> ptr = NULL;<br>
> else<br>
> @@ -321,12 +322,12 @@ static void isolation(int i915)<br>
> t->name, B, b, offset_b);<br>
> <br>
> errno = 0;<br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, i915, offset_a);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, i915, offset_a);<br>
> igt_assert(ptr == MAP_FAILED);<br>
> igt_assert_eq(errno, EACCES);<br>
> <br>
> errno = 0;<br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, i915, offset_b);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, i915, offset_b);<br>
> igt_assert(ptr == MAP_FAILED);<br>
> igt_assert_eq(errno, EACCES);<br>
> <br>
> @@ -344,13 +345,13 @@ static void isolation(int i915)<br>
> <br>
> close(B);<br>
> <br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> igt_assert(ptr != MAP_FAILED);<br>
> munmap(ptr, 4096);<br>
> <br>
> close(A);<br>
> <br>
> - ptr = mmap64(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> + ptr = drm_mmap(0, 4096, PROT_READ, MAP_SHARED, A, offset_a);<br>
> igt_assert(ptr == MAP_FAILED);<br>
> }<br>
> }<br>
> @@ -553,7 +554,7 @@ static void close_race(int i915, int timeout)<br>
> _Atomic uint32_t *handles;<br>
> size_t len = ALIGN((ncpus + 1) * sizeof(uint32_t), 4096);<br>
> <br>
> - handles = mmap64(0, len, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> + handles = drm_mmap(0, len, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);<br>
> igt_assert(handles != MAP_FAILED);<br>
> <br>
> igt_fork(child, ncpus + 1) {<br>
> -- <br>
> 2.37.0 (Apple Git-136)<br>
> <br>
</blockquote></div>