diff mbox series

[3/5] drm/virtio: track whether or not a context has been initiated

Message ID 20200213231805.622-4-gurchetansingh@chromium.org (mailing list archive)
State New, archived
Headers show
Series *** Delay context create cmd *** | expand

Commit Message

Gurchetan Singh Feb. 13, 2020, 11:18 p.m. UTC
Use an atomic variable to track whether a context has been
initiated.

v2: Fix possible race (@olv)

Signed-off-by: Gurchetan Singh <gurchetansingh@chromium.org>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
 drivers/gpu/drm/virtio/virtgpu_ioctl.c | 3 +++
 drivers/gpu/drm/virtio/virtgpu_kms.c   | 1 +
 3 files changed, 5 insertions(+)

Comments

Chia-I Wu Feb. 14, 2020, 7:27 p.m. UTC | #1
On Thu, Feb 13, 2020 at 3:18 PM Gurchetan Singh
<gurchetansingh@chromium.org> wrote:
>
> Use an atomic variable to track whether a context has been
> initiated.
>
> v2: Fix possible race (@olv)
>
> Signed-off-by: Gurchetan Singh <gurchetansingh@chromium.org>
> ---
>  drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
>  drivers/gpu/drm/virtio/virtgpu_ioctl.c | 3 +++
>  drivers/gpu/drm/virtio/virtgpu_kms.c   | 1 +
>  3 files changed, 5 insertions(+)
>
> diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
> index 72c1d9b59dfe..ca505984f8ab 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_drv.h
> +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
> @@ -209,6 +209,7 @@ struct virtio_gpu_device {
>
>  struct virtio_gpu_fpriv {
>         uint32_t ctx_id;
> +       atomic_t context_initiated;
>  };
>
>  /* virtio_ioctl.c */
> diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> index 896c3f419a6d..a98884462944 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> @@ -44,6 +44,9 @@ void virtio_gpu_create_context(struct drm_device *dev,
>         if (!vgdev->has_virgl_3d)
>                 return;
>
> +       if (!atomic_add_unless(&vfpriv->context_initiated, 1, 1))
> +               return;
> +
How does this work?  When thread A and B enter this function at the
same time, and thread B returns early, it is possible that thread B
queues other commands before thread A has the chance to queue
virtio_gpu_cmd_context_create.

>         get_task_comm(dbgname, current);
>         virtio_gpu_cmd_context_create(vgdev, vfpriv->ctx_id,
>                                       strlen(dbgname), dbgname);
> diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c b/drivers/gpu/drm/virtio/virtgpu_kms.c
> index 282558576527..25248bad3fc4 100644
> --- a/drivers/gpu/drm/virtio/virtgpu_kms.c
> +++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
> @@ -263,6 +263,7 @@ int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
>         }
>
>         vfpriv->ctx_id = handle + 1;
> +       atomic_set(&vfpriv->context_initiated, 0);
>         file->driver_priv = vfpriv;
>         virtio_gpu_create_context(dev, file);
>         return 0;
> --
> 2.25.0.265.gbab2e86ba0-goog
>
Gurchetan Singh Feb. 15, 2020, 2:29 a.m. UTC | #2
On Fri, Feb 14, 2020 at 11:27 AM Chia-I Wu <olvaffe@gmail.com> wrote:
>
> On Thu, Feb 13, 2020 at 3:18 PM Gurchetan Singh
> <gurchetansingh@chromium.org> wrote:
> >
> > Use an atomic variable to track whether a context has been
> > initiated.
> >
> > v2: Fix possible race (@olv)
> >
> > Signed-off-by: Gurchetan Singh <gurchetansingh@chromium.org>
> > ---
> >  drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
> >  drivers/gpu/drm/virtio/virtgpu_ioctl.c | 3 +++
> >  drivers/gpu/drm/virtio/virtgpu_kms.c   | 1 +
> >  3 files changed, 5 insertions(+)
> >
> > diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
> > index 72c1d9b59dfe..ca505984f8ab 100644
> > --- a/drivers/gpu/drm/virtio/virtgpu_drv.h
> > +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
> > @@ -209,6 +209,7 @@ struct virtio_gpu_device {
> >
> >  struct virtio_gpu_fpriv {
> >         uint32_t ctx_id;
> > +       atomic_t context_initiated;
> >  };
> >
> >  /* virtio_ioctl.c */
> > diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > index 896c3f419a6d..a98884462944 100644
> > --- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > +++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > @@ -44,6 +44,9 @@ void virtio_gpu_create_context(struct drm_device *dev,
> >         if (!vgdev->has_virgl_3d)
> >                 return;
> >
> > +       if (!atomic_add_unless(&vfpriv->context_initiated, 1, 1))
> > +               return;
> > +
> How does this work?  When thread A and B enter this function at the
> same time, and thread B returns early, it is possible that thread B
> queues other commands before thread A has the chance to queue
> virtio_gpu_cmd_context_create.

Good catch, I'll add a spinlock in v3.

>
> >         get_task_comm(dbgname, current);
> >         virtio_gpu_cmd_context_create(vgdev, vfpriv->ctx_id,
> >                                       strlen(dbgname), dbgname);
> > diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c b/drivers/gpu/drm/virtio/virtgpu_kms.c
> > index 282558576527..25248bad3fc4 100644
> > --- a/drivers/gpu/drm/virtio/virtgpu_kms.c
> > +++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
> > @@ -263,6 +263,7 @@ int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
> >         }
> >
> >         vfpriv->ctx_id = handle + 1;
> > +       atomic_set(&vfpriv->context_initiated, 0);
> >         file->driver_priv = vfpriv;
> >         virtio_gpu_create_context(dev, file);
> >         return 0;
> > --
> > 2.25.0.265.gbab2e86ba0-goog
> >
Chia-I Wu Feb. 15, 2020, 4:29 a.m. UTC | #3
On Fri, Feb 14, 2020 at 6:29 PM Gurchetan Singh
<gurchetansingh@chromium.org> wrote:
>
> On Fri, Feb 14, 2020 at 11:27 AM Chia-I Wu <olvaffe@gmail.com> wrote:
> >
> > On Thu, Feb 13, 2020 at 3:18 PM Gurchetan Singh
> > <gurchetansingh@chromium.org> wrote:
> > >
> > > Use an atomic variable to track whether a context has been
> > > initiated.
> > >
> > > v2: Fix possible race (@olv)
> > >
> > > Signed-off-by: Gurchetan Singh <gurchetansingh@chromium.org>
> > > ---
> > >  drivers/gpu/drm/virtio/virtgpu_drv.h   | 1 +
> > >  drivers/gpu/drm/virtio/virtgpu_ioctl.c | 3 +++
> > >  drivers/gpu/drm/virtio/virtgpu_kms.c   | 1 +
> > >  3 files changed, 5 insertions(+)
> > >
> > > diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
> > > index 72c1d9b59dfe..ca505984f8ab 100644
> > > --- a/drivers/gpu/drm/virtio/virtgpu_drv.h
> > > +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
> > > @@ -209,6 +209,7 @@ struct virtio_gpu_device {
> > >
> > >  struct virtio_gpu_fpriv {
> > >         uint32_t ctx_id;
> > > +       atomic_t context_initiated;
> > >  };
> > >
> > >  /* virtio_ioctl.c */
> > > diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > > index 896c3f419a6d..a98884462944 100644
> > > --- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > > +++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
> > > @@ -44,6 +44,9 @@ void virtio_gpu_create_context(struct drm_device *dev,
> > >         if (!vgdev->has_virgl_3d)
> > >                 return;
> > >
> > > +       if (!atomic_add_unless(&vfpriv->context_initiated, 1, 1))
> > > +               return;
> > > +
> > How does this work?  When thread A and B enter this function at the
> > same time, and thread B returns early, it is possible that thread B
> > queues other commands before thread A has the chance to queue
> > virtio_gpu_cmd_context_create.
>
> Good catch, I'll add a spinlock in v3.
virtio_gpu_cmd_context_create can sleep.  You will need a mutex (or
figure out another way to do the lazy initialization).

>
> >
> > >         get_task_comm(dbgname, current);
> > >         virtio_gpu_cmd_context_create(vgdev, vfpriv->ctx_id,
> > >                                       strlen(dbgname), dbgname);
> > > diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c b/drivers/gpu/drm/virtio/virtgpu_kms.c
> > > index 282558576527..25248bad3fc4 100644
> > > --- a/drivers/gpu/drm/virtio/virtgpu_kms.c
> > > +++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
> > > @@ -263,6 +263,7 @@ int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
> > >         }
> > >
> > >         vfpriv->ctx_id = handle + 1;
> > > +       atomic_set(&vfpriv->context_initiated, 0);
> > >         file->driver_priv = vfpriv;
> > >         virtio_gpu_create_context(dev, file);
> > >         return 0;
> > > --
> > > 2.25.0.265.gbab2e86ba0-goog
> > >
diff mbox series

Patch

diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h
index 72c1d9b59dfe..ca505984f8ab 100644
--- a/drivers/gpu/drm/virtio/virtgpu_drv.h
+++ b/drivers/gpu/drm/virtio/virtgpu_drv.h
@@ -209,6 +209,7 @@  struct virtio_gpu_device {
 
 struct virtio_gpu_fpriv {
 	uint32_t ctx_id;
+	atomic_t context_initiated;
 };
 
 /* virtio_ioctl.c */
diff --git a/drivers/gpu/drm/virtio/virtgpu_ioctl.c b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
index 896c3f419a6d..a98884462944 100644
--- a/drivers/gpu/drm/virtio/virtgpu_ioctl.c
+++ b/drivers/gpu/drm/virtio/virtgpu_ioctl.c
@@ -44,6 +44,9 @@  void virtio_gpu_create_context(struct drm_device *dev,
 	if (!vgdev->has_virgl_3d)
 		return;
 
+	if (!atomic_add_unless(&vfpriv->context_initiated, 1, 1))
+		return;
+
 	get_task_comm(dbgname, current);
 	virtio_gpu_cmd_context_create(vgdev, vfpriv->ctx_id,
 				      strlen(dbgname), dbgname);
diff --git a/drivers/gpu/drm/virtio/virtgpu_kms.c b/drivers/gpu/drm/virtio/virtgpu_kms.c
index 282558576527..25248bad3fc4 100644
--- a/drivers/gpu/drm/virtio/virtgpu_kms.c
+++ b/drivers/gpu/drm/virtio/virtgpu_kms.c
@@ -263,6 +263,7 @@  int virtio_gpu_driver_open(struct drm_device *dev, struct drm_file *file)
 	}
 
 	vfpriv->ctx_id = handle + 1;
+	atomic_set(&vfpriv->context_initiated, 0);
 	file->driver_priv = vfpriv;
 	virtio_gpu_create_context(dev, file);
 	return 0;