virgl: do not share virgl_screen between different drm_files
authorFeng Jiang <jiangfeng@kylinos.cn>
Fri, 27 May 2022 08:24:11 +0000 (16:24 +0800)
committerMarge Bot <emma+marge@anholt.net>
Tue, 2 Aug 2022 10:30:08 +0000 (10:30 +0000)
commit2926a1aa76d4008bbb53f7a0ed9cb497b4923f01
tree0cb9c3164c98f50ec8a8039e1bf5eee4a0619298
parent883acc41505cc42706387c1b88921f3cd79603c5
virgl: do not share virgl_screen between different drm_files

Now, only one instance of virgl_screen exists for a device
(/dev/dri/cardX), and it is shared by different frontends (eg GLX,
GBM, etc.). There is a problem with this, as follows:

  /* Init GLX */
  ...
  glXCreateContext(...);
  ...

  /* GBM */
  gbm_fd = open("/dev/dri/card0", O_RDWR);
  dev = gbm_create_device(gbm_fd);
  bo = gbm_bo_create(dev, ...);
  plane_handle = gbm_bo_get_handle_for_plane(bo, ...);
  drmPrimeHandleToFD(gbm_fd, handle.u32, flags, &plane_fd);

The above drmPrimeHandleToFD() call will fail with ENOENT.
The reason is that GBM and GLX share the same virgl_screen (file
descriptor), and it is not gbm_fd that is used to create gbm_bo,
but other fd (opened during GLX initialization). Since the scope
of prime handle is limited to drm_file, the above plane_handle is
invalid under gbm_fd.

By canceling the sharing of virgl_screen between different drm_files,
GBM can use the correct fd to create resources, thereby avoiding the
problem of invalid prime handle.

Signed-off-by: Feng Jiang <jiangfeng@kylinos.cn>
Reviewed-by: Gert Wollny <gert.wollny@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16738>
src/gallium/winsys/virgl/drm/virgl_drm_winsys.c