Re: [PATCH v3] drm/v3d: Add DRM_IOCTL_V3D_PERFMON_SET_GLOBAL

From: Christian Gmeiner
Date: Mon Nov 18 2024 - 18:14:26 EST


Hi Maira,

>
> The patch overall LGTM, I just have one small nit.
>

Hopefully v4 will be the last iteration.

> On 17/11/24 18:41, Christian Gmeiner wrote:
> > From: Christian Gmeiner <cgmeiner@xxxxxxxxxx>
> >
> > Add a new ioctl, DRM_IOCTL_V3D_PERFMON_SET_GLOBAL, to allow
> > configuration of a global performance monitor (perfmon).
> > Use the global perfmon for all jobs to ensure consistent
> > performance tracking across submissions. This feature is
> > needed to implement a Perfetto datasources in user-space.
> >
> > Signed-off-by: Christian Gmeiner <cgmeiner@xxxxxxxxxx>
> > ---
> > Changes in v3:
> > - Reworked commit message.
> > - Refined some code comments.
> > - Added missing v3d_perfmon_stop(..) call to v3d_perfmon_destroy_ioctl(..).
> >
> > Changes in v2:
> > - Reworked commit message.
> > - Removed num_perfmon counter for tracking perfmon allocations.
> > - Allowing allocation of perfmons when the global perfmon is active.
> > - Return -EAGAIN for submissions with a per job perfmon if the global perfmon is active.
> > ---
> >
> > ---
> > drivers/gpu/drm/v3d/v3d_drv.c | 1 +
> > drivers/gpu/drm/v3d/v3d_drv.h | 8 ++++++
> > drivers/gpu/drm/v3d/v3d_perfmon.c | 42 +++++++++++++++++++++++++++++++
> > drivers/gpu/drm/v3d/v3d_sched.c | 14 ++++++++---
> > drivers/gpu/drm/v3d/v3d_submit.c | 10 ++++++++
> > include/uapi/drm/v3d_drm.h | 15 +++++++++++
> > 6 files changed, 87 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/v3d/v3d_drv.c b/drivers/gpu/drm/v3d/v3d_drv.c
> > index d7ff1f5fa481..3c89f0daa5b8 100644
> > --- a/drivers/gpu/drm/v3d/v3d_drv.c
> > +++ b/drivers/gpu/drm/v3d/v3d_drv.c
> > @@ -214,6 +214,7 @@ static const struct drm_ioctl_desc v3d_drm_ioctls[] = {
> > DRM_IOCTL_DEF_DRV(V3D_PERFMON_GET_VALUES, v3d_perfmon_get_values_ioctl, DRM_RENDER_ALLOW),
> > DRM_IOCTL_DEF_DRV(V3D_SUBMIT_CPU, v3d_submit_cpu_ioctl, DRM_RENDER_ALLOW | DRM_AUTH),
> > DRM_IOCTL_DEF_DRV(V3D_PERFMON_GET_COUNTER, v3d_perfmon_get_counter_ioctl, DRM_RENDER_ALLOW),
> > + DRM_IOCTL_DEF_DRV(V3D_PERFMON_SET_GLOBAL, v3d_perfmon_set_global_ioctl, DRM_RENDER_ALLOW),
> > };
> >
> > static const struct drm_driver v3d_drm_driver = {
> > diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h
> > index cf4b23369dc4..a0d920ec2b1d 100644
> > --- a/drivers/gpu/drm/v3d/v3d_drv.h
> > +++ b/drivers/gpu/drm/v3d/v3d_drv.h
> > @@ -179,6 +179,12 @@ struct v3d_dev {
> > u32 num_allocated;
> > u32 pages_allocated;
> > } bo_stats;
> > +
> > + /* To support a performance analysis tool in user space, we require
> > + * a single, globally configured performance monitor (perfmon) for
> > + * all jobs.
> > + */
> > + struct v3d_perfmon *global_perfmon;
> > };
> >
> > static inline struct v3d_dev *
> > @@ -584,6 +590,8 @@ int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
> > struct drm_file *file_priv);
> > int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data,
> > struct drm_file *file_priv);
> > +int v3d_perfmon_set_global_ioctl(struct drm_device *dev, void *data,
> > + struct drm_file *file_priv);
> >
> > /* v3d_sysfs.c */
> > int v3d_sysfs_init(struct device *dev);
> > diff --git a/drivers/gpu/drm/v3d/v3d_perfmon.c b/drivers/gpu/drm/v3d/v3d_perfmon.c
> > index 156be13ab2ef..df72171d8c80 100644
> > --- a/drivers/gpu/drm/v3d/v3d_perfmon.c
> > +++ b/drivers/gpu/drm/v3d/v3d_perfmon.c
> > @@ -312,6 +312,9 @@ static int v3d_perfmon_idr_del(int id, void *elem, void *data)
> > if (perfmon == v3d->active_perfmon)
> > v3d_perfmon_stop(v3d, perfmon, false);
> >
> > + /* If the global perfmon is being destroyed, set it to NULL */
> > + cmpxchg(&v3d->global_perfmon, perfmon, NULL);
> > +
> > v3d_perfmon_put(perfmon);
> >
> > return 0;
> > @@ -383,6 +386,7 @@ int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
> > {
> > struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
> > struct drm_v3d_perfmon_destroy *req = data;
> > + struct v3d_dev *v3d = v3d_priv->v3d;
> > struct v3d_perfmon *perfmon;
> >
> > mutex_lock(&v3d_priv->perfmon.lock);
> > @@ -392,6 +396,13 @@ int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
> > if (!perfmon)
> > return -EINVAL;
> >
> > + /* If the active perfmon is being destroyed, stop it first */
> > + if (perfmon == v3d->active_perfmon)
> > + v3d_perfmon_stop(v3d, perfmon, false);
>
> Could you please move this snippet to different patch? This snippet is a
> global fix as it can prevent NULL pointer dereferences triggered by the
> user-space. I would like to push this snippet to drm-misc-fixes.
>

Done.

> > +
> > + /* If the global perfmon is being destroyed, set it to NULL */
> > + cmpxchg(&v3d->global_perfmon, perfmon, NULL);
> > +
> > v3d_perfmon_put(perfmon);
> >
> > return 0;
> > @@ -451,3 +462,34 @@ int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data,
> >
> > return 0;
> > }
> > +
> > +int v3d_perfmon_set_global_ioctl(struct drm_device *dev, void *data,
> > + struct drm_file *file_priv)
> > +{
> > + struct v3d_file_priv *v3d_priv = file_priv->driver_priv;
> > + struct drm_v3d_perfmon_set_global *req = data;
> > + struct v3d_dev *v3d = to_v3d_dev(dev);
> > + struct v3d_perfmon *perfmon;
> > +
> > + if (req->flags & ~DRM_V3D_PERFMON_CLEAR_GLOBAL)
> > + return -EINVAL;
> > +
> > + perfmon = v3d_perfmon_find(v3d_priv, req->id);
> > + if (!perfmon)
> > + return -EINVAL;
> > +
> > + /* If the request is to clear the global performance monitor */
> > + if (req->flags & DRM_V3D_PERFMON_CLEAR_GLOBAL) {
> > + if (!v3d->global_perfmon)
> > + return -EINVAL;
> > +
> > + xchg(&v3d->global_perfmon, NULL);
>
> What if v3d->global_perfmon is running?
>

It will be stopped by the next job that hits v3d_switch_perfmon(..).

> > +
> > + return 0;
> > + }
> > +
> > + if (cmpxchg(&v3d->global_perfmon, NULL, perfmon))
> > + return -EBUSY;
> > +
> > + return 0;
> > +}
>
> [...]
>
> > diff --git a/include/uapi/drm/v3d_drm.h b/include/uapi/drm/v3d_drm.h
> > index 87fc5bb0a61e..7c2ad9d0270d 100644
> > --- a/include/uapi/drm/v3d_drm.h
> > +++ b/include/uapi/drm/v3d_drm.h
> > @@ -43,6 +43,7 @@ extern "C" {
> > #define DRM_V3D_PERFMON_GET_VALUES 0x0a
> > #define DRM_V3D_SUBMIT_CPU 0x0b
> > #define DRM_V3D_PERFMON_GET_COUNTER 0x0c
> > +#define DRM_V3D_PERFMON_SET_GLOBAL 0x0d
> >
> > #define DRM_IOCTL_V3D_SUBMIT_CL DRM_IOWR(DRM_COMMAND_BASE + DRM_V3D_SUBMIT_CL, struct drm_v3d_submit_cl)
> > #define DRM_IOCTL_V3D_WAIT_BO DRM_IOWR(DRM_COMMAND_BASE + DRM_V3D_WAIT_BO, struct drm_v3d_wait_bo)
> > @@ -61,6 +62,8 @@ extern "C" {
> > #define DRM_IOCTL_V3D_SUBMIT_CPU DRM_IOW(DRM_COMMAND_BASE + DRM_V3D_SUBMIT_CPU, struct drm_v3d_submit_cpu)
> > #define DRM_IOCTL_V3D_PERFMON_GET_COUNTER DRM_IOWR(DRM_COMMAND_BASE + DRM_V3D_PERFMON_GET_COUNTER, \
> > struct drm_v3d_perfmon_get_counter)
> > +#define DRM_IOCTL_V3D_PERFMON_SET_GLOBAL DRM_IOW(DRM_COMMAND_BASE + DRM_V3D_PERFMON_SET_GLOBAL, \
> > + struct drm_v3d_perfmon_set_global)
> >
> > #define DRM_V3D_SUBMIT_CL_FLUSH_CACHE 0x01
> > #define DRM_V3D_SUBMIT_EXTENSION 0x02
> > @@ -765,6 +768,18 @@ struct drm_v3d_perfmon_get_counter {
> > __u8 reserved[7];
> > };
> >
> > +#define DRM_V3D_PERFMON_CLEAR_GLOBAL 0x0001
> > +
> > +/**
> > + * struct drm_v3d_perfmon_set_global - ioctl to define a global performance
> > + * monitor that is used for all job. If a global performance monitor is
>
> s/job/jobs
>

Ah.. thanks.

> Best Regards,
> - Maíra
>
> > + * defined, jobs with a self-defined performance monitor are not allowed.
> > + */
> > +struct drm_v3d_perfmon_set_global {
> > + __u32 flags;
> > + __u32 id;
> > +};
> > +
> > #if defined(__cplusplus)
> > }
> > #endif
>

--
greets
--
Christian Gmeiner, MSc

https://christian-gmeiner.info/privacypolicy