mmap with PROT_WRITE of dma-buf FD coming from eglExportDMABUFImageMESA

Daniel Vetter daniel at ffwll.ch
Thu Feb 2 20:52:17 UTC 2017


On Thu, Feb 2, 2017 at 8:46 PM, Volker Vogelhuber
<v.vogelhuber at digitalendoscopy.de> wrote:
> On Thu, Feb 02, 2017 at 05:19:58PM +0100, Volker Vogelhuber wrote:
>>> I'm currently trying to mmap the memory of an OpenGL texture I've created by
>>> doing the following:
>>>
>>> std::vector<EGLint> image_attribs = {
>>>         EGL_WIDTH,              static_cast<int>(m_texWidth & 0x7FFFFFFF),
>>>         EGL_HEIGHT,             static_cast<int>(m_texHeight & 0x7FFFFFFF),
>>>         EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
>>>         EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA
>>>         EGL_NONE
>>>     };
>>> glGenTextures( 1, &texID );
>>> glBindTexture( GL_TEXTURE_2D, texID );
>>> eglImage = eglCreateDRMImageMESA( eglGetCurrentDisplay(), &image_attribs[0]
>>> );
>>> glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglmage);
>>> eglExportDMABUFImageQueryMESA(eglGetCurrentDisplay(), eglImage, &fourcc,
>>> &nplanes, &modifiers)
>>> eglExportDMABUFImageMESA(eglGetCurrentDisplay(), eglImage, &fd, strides,
>>> offsets)
>>>
>>> struct dma_buf_sync sync_args = { .flags = DMA_BUF_SYNC_START };
>>> ioctl( fd, DMA_BUF_IOCTL_SYNC, sync_args);
>>> mmap( NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
>>>
>>> For some reason mmap returns MAP_FAILED when using the PROT_WRITE flag,
>>> while
>>> using only PROT_READ seems to be fine. I'm currently using kernel 4.9.6 on
>>> an Intel CherryTrail (i915).
>>>
>>> Is there an obvious reason why I'm not able to mmap the texture FD with
>>> PROT_WRITE flags?
>>> Reading https://01.org/blogs/2016/sharing-cpu-and-gpu-buffers-linux I would
>>> have guessed that
>>> this should be possible. Or is it only true for specific GEM buffers not
>>> textures created using
>>> eglCreateDRMImageMESA?
>>
> >From a quick look the mesa/libdrm (depending upon driver) handle2fd
>>implementations don't set the DRM_RDWR flag when asking PRIME for a
>>dma-buf fd, which means you fd doesn't have write permissions. Which means
>>mmap with writing allowed will fail.
>>
>>Not sure how to fix that.
> Thanks for the reply. I guess you mean that line of code:
> http://code.metager.de/source/xref/freedesktop/mesa/drm/intel/intel_bufmgr_gem.c#2679
> Is there a good reason not to patch drm_intel_bo_gem_export_to_prime to call
> drmPrimeHandleToFD with DRM_RDWR flag set as well? I just saw a similar discussion
> under https://chromium-review.googlesource.com/c/323990/

Probably none, just a historical accident because the first prime
support didn't have support for mmap. Feel free to submit patches for
this all.

Thanks, Daniel
-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch


More information about the dri-devel mailing list