Skip to contentSkip to conversation

mmap with PROT_WRITE of dma-buf FD coming from eglExportDMABUFImageMESA

DISCUSSION Volker Vogelhuber 2 participants 4 messages Feb 2, 2017

lore
Share & tools
lore
#0 lore raw

Hi,

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?

Regards,
Volker

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw
Quoted 31 lines · Volker Vogelhuber

Hi,

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).

Quoted 7 lines

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.
-Daniel

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 37 lines · Volker Vogelhuber
Quoted 29 lines

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).

Quoted 7 lines

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?

Quoted 6 lines

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/

Regards,
    Volker

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw
Quoted 43 lines · Volker Vogelhuber
Quoted 42 lines · Volker Vogelhuber
Quoted 29 lines

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).

Quoted 7 lines

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.

Quoted 6 lines
Quoted 1 line

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

Signature · 7 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel