<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Sep 15, 2021 at 5:11 PM Chia-I Wu <<a href="mailto:olvaffe@gmail.com">olvaffe@gmail.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"> i<br>
<br>
On Tue, Sep 14, 2021 at 6:26 PM Gurchetan Singh<br>
<<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>> wrote:<br>
><br>
><br>
><br>
> On Tue, Sep 14, 2021 at 10:53 AM Chia-I Wu <<a href="mailto:olvaffe@gmail.com" target="_blank">olvaffe@gmail.com</a>> wrote:<br>
>><br>
>> ,On Mon, Sep 13, 2021 at 6:57 PM Gurchetan Singh<br>
>> <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>> wrote:<br>
>> ><br>
>> ><br>
>> ><br>
>> ><br>
>> > On Mon, Sep 13, 2021 at 11:52 AM Chia-I Wu <<a href="mailto:olvaffe@gmail.com" target="_blank">olvaffe@gmail.com</a>> wrote:<br>
>> >><br>
>> >> .<br>
>> >><br>
>> >> On Mon, Sep 13, 2021 at 10:48 AM Gurchetan Singh<br>
>> >> <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>> wrote:<br>
>> >> ><br>
>> >> ><br>
>> >> ><br>
>> >> > On Fri, Sep 10, 2021 at 12:33 PM Chia-I Wu <<a href="mailto:olvaffe@gmail.com" target="_blank">olvaffe@gmail.com</a>> wrote:<br>
>> >> >><br>
>> >> >> On Wed, Sep 8, 2021 at 6:37 PM Gurchetan Singh<br>
>> >> >> <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>> wrote:<br>
>> >> >> ><br>
>> >> >> > We don't want fences from different 3D contexts (virgl, gfxstream,<br>
>> >> >> > venus) to be on the same timeline.  With explicit context creation,<br>
>> >> >> > we can specify the number of ring each context wants.<br>
>> >> >> ><br>
>> >> >> > Execbuffer can specify which ring to use.<br>
>> >> >> ><br>
>> >> >> > Signed-off-by: Gurchetan Singh <<a href="mailto:gurchetansingh@chromium.org" target="_blank">gurchetansingh@chromium.org</a>><br>
>> >> >> > Acked-by: Lingfeng Yang <<a href="mailto:lfy@google.com" target="_blank">lfy@google.com</a>><br>
>> >> >> > ---<br>
>> >> >> >  drivers/gpu/drm/virtio/virtgpu_drv.h   |  3 +++<br>
>> >> >> >  drivers/gpu/drm/virtio/virtgpu_ioctl.c | 34 ++++++++++++++++++++++++--<br>
>> >> >> >  2 files changed, 35 insertions(+), 2 deletions(-)<br>
>> >> >> ><br>
>> >> >> > diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
>> >> >> > index a5142d60c2fa..cca9ab505deb 100644<br>
>> >> >> > --- a/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
>> >> >> > +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h<br>
>> >> >> > @@ -56,6 +56,7 @@<br>
>> >> >> >  #define STATE_ERR 2<br>
>> >> >> ><br>
>> >> >> >  #define MAX_CAPSET_ID 63<br>
>> >> >> > +#define MAX_RINGS 64<br>
>> >> >> ><br>
>> >> >> >  struct virtio_gpu_object_params {<br>
>> >> >> >         unsigned long size;<br>
>> >> >> > @@ -263,6 +264,8 @@ struct virtio_gpu_fpriv {<br>
>> >> >> >         uint32_t ctx_id;<br>
>> >> >> >         uint32_t context_init;<br>
>> >> >> >         bool context_created;<br>
>> >> >> > +       uint32_t num_rings;<br>
>> >> >> > +       uint64_t base_fence_ctx;<br>
>> >> >> >         struct mutex context_lock;<br>
>> >> >> >  };<br>
>> >> >> ><br>
>> >> >> > diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
>> >> >> > index f51f3393a194..262f79210283 100644<br>
>> >> >> > --- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
>> >> >> > +++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c<br>
>> >> >> > @@ -99,6 +99,11 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,<br>
>> >> >> >         int in_fence_fd = exbuf->fence_fd;<br>
>> >> >> >         int out_fence_fd = -1;<br>
>> >> >> >         void *buf;<br>
>> >> >> > +       uint64_t fence_ctx;<br>
>> >> >> > +       uint32_t ring_idx;<br>
>> >> >> > +<br>
>> >> >> > +       fence_ctx = vgdev->fence_drv.context;<br>
>> >> >> > +       ring_idx = 0;<br>
>> >> >> ><br>
>> >> >> >         if (vgdev->has_virgl_3d == false)<br>
>> >> >> >                 return -ENOSYS;<br>
>> >> >> > @@ -106,6 +111,17 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,<br>
>> >> >> >         if ((exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS))<br>
>> >> >> >                 return -EINVAL;<br>
>> >> >> ><br>
>> >> >> > +       if ((exbuf->flags & VIRTGPU_EXECBUF_RING_IDX)) {<br>
>> >> >> > +               if (exbuf->ring_idx >= vfpriv->num_rings)<br>
>> >> >> > +                       return -EINVAL;<br>
>> >> >> > +<br>
>> >> >> > +               if (!vfpriv->base_fence_ctx)<br>
>> >> >> > +                       return -EINVAL;<br>
>> >> >> > +<br>
>> >> >> > +               fence_ctx = vfpriv->base_fence_ctx;<br>
>> >> >> > +               ring_idx = exbuf->ring_idx;<br>
>> >> >> > +       }<br>
>> >> >> > +<br>
>> >> >> >         exbuf->fence_fd = -1;<br>
>> >> >> ><br>
>> >> >> >         virtio_gpu_create_context(dev, file);<br>
>> >> >> > @@ -173,7 +189,7 @@ static int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,<br>
>> >> >> >                         goto out_memdup;<br>
>> >> >> >         }<br>
>> >> >> ><br>
>> >> >> > -       out_fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context, 0);<br>
>> >> >> > +       out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);<br>
>> >> >> >         if(!out_fence) {<br>
>> >> >> >                 ret = -ENOMEM;<br>
>> >> >> >                 goto out_unresv;<br>
>> >> >> > @@ -691,7 +707,7 @@ static int virtio_gpu_context_init_ioctl(struct drm_device *dev,<br>
>> >> >> >                 return -EINVAL;<br>
>> >> >> ><br>
>> >> >> >         /* Number of unique parameters supported at this time. */<br>
>> >> >> > -       if (num_params > 1)<br>
>> >> >> > +       if (num_params > 2)<br>
>> >> >> >                 return -EINVAL;<br>
>> >> >> ><br>
>> >> >> >         ctx_set_params = memdup_user(u64_to_user_ptr(args->ctx_set_params),<br>
>> >> >> > @@ -731,6 +747,20 @@ static int virtio_gpu_context_init_ioctl(struct drm_device *dev,<br>
>> >> >> ><br>
>> >> >> >                         vfpriv->context_init |= value;<br>
>> >> >> >                         break;<br>
>> >> >> > +               case VIRTGPU_CONTEXT_PARAM_NUM_RINGS:<br>
>> >> >> > +                       if (vfpriv->base_fence_ctx) {<br>
>> >> >> > +                               ret = -EINVAL;<br>
>> >> >> > +                               goto out_unlock;<br>
>> >> >> > +                       }<br>
>> >> >> > +<br>
>> >> >> > +                       if (value > MAX_RINGS) {<br>
>> >> >> > +                               ret = -EINVAL;<br>
>> >> >> > +                               goto out_unlock;<br>
>> >> >> > +                       }<br>
>> >> >> > +<br>
>> >> >> > +                       vfpriv->base_fence_ctx = dma_fence_context_alloc(value);<br>
>> >> >> With multiple fence contexts, we should do something about implicit fencing.<br>
>> >> >><br>
>> >> >> The classic example is Mesa and X server.  When both use virgl and the<br>
>> >> >> global fence context, no dma_fence_wait is fine.  But when Mesa uses<br>
>> >> >> venus and the ring fence context, dma_fence_wait should be inserted.<br>
>> >> ><br>
>> >> ><br>
>> >> >  If I read your comment correctly, the use case is:<br>
>> >> ><br>
>> >> > context A (venus)<br>
>> >> ><br>
>> >> > sharing a render target with<br>
>> >> ><br>
>> >> > context B (Xserver backed virgl)<br>
>> >> ><br>
>> >> > ?<br>
>> >> ><br>
>> >> > Which function do you envisage dma_fence_wait(...) to be inserted?  Doesn't implicit synchronization mean there's no fence to share between contexts (only buffer objects)?<br>
>> >><br>
>> >> Fences can be implicitly shared via reservation objects associated<br>
>> >> with buffer objects.<br>
>> >><br>
>> >> > It may be possible to wait on the reservation object associated with a buffer object from a different context (userspace can also do DRM_IOCTL_VIRTGPU_WAIT), but not sure if that's what you're looking for.<br>
>> >><br>
>> >> Right, that's what I am looking for.  Userspace expects implicit<br>
>> >> fencing to work.  While there are works to move the userspace to do<br>
>> >> explicit fencing, it is not there yet in general and we can't require<br>
>> >> the userspace to do explicit fencing or DRM_IOCTL_VIRTGPU_WAIT.<br>
>> ><br>
>> ><br>
>> > Another option would be to use the upcoming DMA_BUF_IOCTL_EXPORT_SYNC_FILE + VIRTGPU_EXECBUF_FENCE_FD_IN (which checks the dma_fence context).<br>
>> That requires the X server / compositors to be modified.  For example,<br>
>> venus works under Android (where there is explicit fencing) or under a<br>
>> modified compositor (which does DMA_BUF_IOCTL_EXPORT_SYNC_FILE or<br>
>> DRM_IOCTL_VIRTGPU_WAIT).  But it does not work too well with an<br>
>> unmodified X server.<br>
><br>
><br>
> Some semi-recent virgl modifications will be needed regardless for interop, such as VIRGL_CAP_V2_UNTYPED_RESOURCE (?).<br>
><br>
> Not sure aren't too many virgl users (most developers)<br>
><br>
> Does Xserver just pick up the latest Mesa release (including virgl/venus)?  Suppose context types land in 5.16, the userspace changes land (both Venus/Virgl) in 21.2 stable releases.<br>
><br>
> <a href="https://docs.mesa3d.org/release-calendar.html" rel="noreferrer" target="_blank">https://docs.mesa3d.org/release-calendar.html</a><br>
><br>
>><br>
>> ><br>
>> > Generally, if it only requires virgl changes, userspace changes are fine since OpenGL drivers implement implicit sync in many ways.  Waiting on the reservation object in the kernel is fine too though.<br>
>> I don't think we want to assume virgl to be the only consumer of<br>
>> dma-bufs, despite that it is the most common use case.<br>
>><br>
>><br>
>> ><br>
>> > Though venus doesn't use the NUM_RINGS param yet.  Getting all permutations of context type + display integration working would take some time (patchset mostly tested with wayland + gfxstream/Android [no implicit sync]).<br>
>> ><br>
>> > WDYT of someone figuring out virgl/venus interop later, independently of this patchset?<br>
>><br>
>> I think we should understand the implications of multiple fence<br>
>> contexts better, even if some changes are not included in this<br>
>> patchset.<br>
>><br>
>> From my view, we don't need implicit fencing in most cases and<br>
>> implicit fencing should be considered a legacy path.  But X server /<br>
>> compositors today happen to require it.  Other drivers seem to use a<br>
>> flag to control whether implicit fences are set up or waited (e.g.,<br>
>> AMDGPU_GEM_CREATE_EXPLICIT_SYNC, MSM_SUBMIT_NO_IMPLICIT, or<br>
>> EXEC_OBJECT_WRITE).  It seems to be the least surprising thing to do.<br>
><br>
><br>
> IMO, the easiest way is just to limit the change to userspace if possible since implicit sync is legacy/something we want to deprecate over time.<br>
><br>
> Another option is to add something like VIRTGPU_EXECBUF_EXPLICIT_SYNC (similar to MSM_SUBMIT_NO_IMPLICIT), where the reservation objects are waited on / added to without that flag.  Since explicit sync will need new hypercalls/params and is a major, that feature is expected to be independent of context types.<br>
><br>
> With that option, waiting on the reservation object would just be another bug fix + addition to 5.16 (perhaps by you) so we can proceed in parallel faster.  VIRTGPU_EXECBUF_EXPLICIT_SYNC (or an equivalent) would be added later.<br>
<br>
It is fine to add it later, but VIRTGPU_EXECBUF_EXPLICIT_SYNC sounds<br>
better to me than a userspace solution.  I don't think we need a new<br>
hypercall as the wait can be a guest-side wait, similar to how<br>
VIRTGPU_EXECBUF_FENCE_FD_IN is a guest-side wait.  The flag can also<br>
suppress VIRTIO_GPU_FLAG_FENCE and make the SUBMIT_3D hypercall<br>
cheaper.<br></blockquote><div><br></div><div>Okay, I will add a note regarding the plan in patch 9 of v2 for context types that need implicit sync.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Even before this patchset, unless I miss something, it seems the fact<br>
that we have a global fence context and assume all host GL contexts<br>
are on the same timeline is not exactly correct.  When glFlush is<br>
called on two host GL contexts, the flush order is not exactly the<br>
same as the execution order.  But that is a different issue that can<br>
be solved in virglrenderer.<br>
<br>
<br>
><br>
>><br>
>><br>
>> ><br>
>> >><br>
>> >><br>
>> >><br>
>> >><br>
>> >> ><br>
>> >> >><br>
>> >> >><br>
>> >> >> > +                       vfpriv->num_rings = value;<br>
>> >> >> > +                       break;<br>
>> >> >> >                 default:<br>
>> >> >> >                         ret = -EINVAL;<br>
>> >> >> >                         goto out_unlock;<br>
>> >> >> > --<br>
>> >> >> > 2.33.0.153.gba50c8fa24-goog<br>
>> >> >> ><br>
>> >> >> ><br>
>> >> >> > ---------------------------------------------------------------------<br>
>> >> >> > To unsubscribe, e-mail: <a href="mailto:virtio-dev-unsubscribe@lists.oasis-open.org" target="_blank">virtio-dev-unsubscribe@lists.oasis-open.org</a><br>
>> >> >> > For additional commands, e-mail: <a href="mailto:virtio-dev-help@lists.oasis-open.org" target="_blank">virtio-dev-help@lists.oasis-open.org</a><br>
>> >> >> ><br>
</blockquote></div></div>