drm/v3d: Replace IDR with XArray for perfmon tracking
Subsystems (4)
Maíra Canal
maintainerPATCH 1/2 drm/v3d: Replace IDR with XArray for perfmon tracking
The IDR interface is deprecated and the XArray API is the recommended replacement. Replace the per-file IDR used to track perfmons with an XArray. This allows us to remove the external mutex that protects the IDR.
While here, introduce the v3d_perfmon_delete() helper to consolidate the perfmon cleanup logic used by both v3d_perfmon_close_file() and v3d_perfmon_destroy_ioctl().
--- drivers/gpu/drm/v3d/v3d_drv.h | 5 +-- drivers/gpu/drm/v3d/v3d_perfmon.c | 55 +++++++++++-------------------- 2 files changed, 21 insertions(+), 39 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -4
@@ -220,10 +220,7 @@ v3d_has_csd(struct v3d_dev *v3d)struct v3d_file_priv {struct v3d_dev *v3d; -struct { -struct idr idr; -struct mutex lock; - } perfmon; +struct xarray perfmons;struct drm_sched_entity sched_entity[V3D_MAX_QUEUES ];
drivers/gpu/drm/v3d/v3d_perfmon.c +20 -35
111 lines not on this page · raw message
Signature · 1 line
2.52.0
Maíra Canal
maintainerPATCH 2/2 drm/vc4: Replace IDR with XArray for perfmon tracking
The IDR interface is deprecated and the XArray API is the recommended replacement. Replace the per-file IDR used to track perfmons with an XArray. This allows us to remove the external mutex that protects the IDR.
While at it, introduce the vc4_perfmon_delete() helper to consolidate the perfmon cleanup logic used by both vc4_perfmon_close_file() and vc4_perfmon_destroy_ioctl(). Also, remove the redundant assignment of vc4file->dev to itself in vc4_perfmon_open_file().
--- drivers/gpu/drm/vc4/vc4_drv.h | 5 +--- drivers/gpu/drm/vc4/vc4_perfmon.c | 49 ++++++++++++++----------------- 2 files changed, 23 insertions(+), 31 deletions(-)
drivers/gpu/drm/vc4/vc4_drv.h +1 -4
@@ -791,10 +791,7 @@ struct vc4_exec_info {struct vc4_file {struct vc4_dev *dev; -struct { -struct idr idr; -struct mutex lock; - } perfmon; +struct xarray perfmons;bool bin_bo_used; };
drivers/gpu/drm/vc4/vc4_perfmon.c +22 -27
106 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH 2/2] drm/vc4: Replace IDR with XArray for perfmon tracking
Quoted 107 lines · Maíra Canal
The IDR interface is deprecated and the XArray API is the recommended replacement. Replace the per-file IDR used to track perfmons with an XArray. This allows us to remove the external mutex that protects the IDR.
While at it, introduce the
vc4_perfmon_delete()helper to consolidate the perfmon cleanup logic used by bothvc4_perfmon_close_file()andvc4_perfmon_destroy_ioctl(). Also, remove the redundant assignment ofvc4file->devto itself invc4_perfmon_open_file().Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/vc4/vc4_drv.h | 5 +--- drivers/gpu/drm/vc4/vc4_perfmon.c | 49 ++++++++++++++----------------- 2 files changed, 23 insertions(+), 31 deletions(-)drivers/gpu/drm/vc4/vc4_drv.h +1
-4@@ -791,10 +791,7 @@ struct vc4_exec_info {struct vc4_file {struct vc4_dev *dev; -struct { -struct idr idr; -struct mutex lock; - } perfmon; +struct xarray perfmons;bool bin_bo_used; };drivers/gpu/drm/vc4/vc4_perfmon.c +14
-16@@ -95,10 +95,10 @@ struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id)if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return NULL ; -mutex_lock (&vc4file->perfmon.lock); - perfmon =idr_find (&vc4file->perfmon.idr, id); +xa_lock (&vc4file->perfmons); + perfmon =xa_load (&vc4file->perfmons, id);vc4_perfmon_get (perfmon); -mutex_unlock (&vc4file->perfmon.lock); +xa_unlock (&vc4file->perfmons);return perfmon; }@@ -110,37 +110,34 @@ void vc4_perfmon_open_file(struct vc4_file *vc4file)if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return ; -mutex_init (&vc4file->perfmon.lock); -idr_init_base (&vc4file->perfmon.idr,VC4_PERFMONID_MIN ); - vc4file->dev = vc4; +xa_init_flags (&vc4file->perfmons,XA_FLAGS_ALLOC1 ); } -static int vc4_perfmon_idr_del (int id,void *elem,void *data) +static void vc4_perfmon_delete (struct vc4_file *vc4file, +struct vc4_perfmon *perfmon) { -struct vc4_perfmon *perfmon = elem; -struct vc4_dev *vc4 = (struct vc4_dev *)data; +struct vc4_dev *vc4 = vc4file->dev;/* If the active perfmon is being destroyed, stop it first */ if (perfmon == vc4->active_perfmon)vc4_perfmon_stop (vc4, perfmon, false);vc4_perfmon_put (perfmon); - -return 0 ; }void vc4_perfmon_close_file (struct vc4_file *vc4file) {struct vc4_dev *vc4 = vc4file->dev; +struct vc4_perfmon *perfmon; +unsigned long id;if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return ; -mutex_lock (&vc4file->perfmon.lock); -idr_for_each (&vc4file->perfmon.idr, vc4_perfmon_idr_del, vc4); -idr_destroy (&vc4file->perfmon.idr); -mutex_unlock (&vc4file->perfmon.lock); -mutex_destroy (&vc4file->perfmon.lock); +xa_for_each (&vc4file->perfmons, id, perfmon) +vc4_perfmon_delete (vc4file, perfmon); + +xa_destroy (&vc4file->perfmons); } int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,@@ -152,6 +149,7 @@ int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,struct vc4_perfmon *perfmon;unsigned int i;int ret; +u32 id;if (WARN_ON_ONCE (vc4->gen >VC4_GEN_4 ))return -ENODEV ;
drivers/gpu/drm/vc4/vc4_perfmon.c int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data, · line 187
Quoted 10 lines
drivers/gpu/drm/vc4/vc4_perfmon.c +2
-5@@ -185,17 +183,16 @@ int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,refcount_set (&perfmon->refcnt,1 ); -mutex_lock (&vc4file->perfmon.lock); - ret =idr_alloc (&vc4file->perfmon.idr, perfmon,VC4_PERFMONID_MIN , -VC4_PERFMONID_MAX ,GFP_KERNEL ); -mutex_unlock (&vc4file->perfmon.lock); - + ret = xa_alloc(&vc4file->perfmons, &id, perfmon, + XA_LIMIT(VC4_PERFMONID_MIN, VC4_PERFMONID_MAX),
Appears the same further cleanup potential with the min/max defines as in v3d. Anyway, conversion looks good to me:
Regards,
Tvrtko
Quoted 30 lines
drivers/gpu/drm/vc4/vc4_perfmon.c +6
-6+ GFP_KERNEL);if (ret <0 ) {kfree (perfmon);return ret; } - req->id = ret; + req->id = id; +return 0 ; }@@ -215,14 +212,12 @@ int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,return -ENODEV ; } -mutex_lock (&vc4file->perfmon.lock); - perfmon =idr_remove (&vc4file->perfmon.idr, req->id); -mutex_unlock (&vc4file->perfmon.lock); - + perfmon =xa_erase (&vc4file->perfmons, req->id);if (!perfmon)return -EINVAL ; -vc4_perfmon_put (perfmon); +vc4_perfmon_delete (vc4file, perfmon); +return 0 ; }
Both patches are:
Quoted 165 lines · Maíra Canal
The IDR interface is deprecated and the XArray API is the recommended replacement. Replace the per-file IDR used to track perfmons with an XArray. This allows us to remove the external mutex that protects the IDR.
While here, introduce the
v3d_perfmon_delete()helper to consolidate the perfmon cleanup logic used by bothv3d_perfmon_close_file()andv3d_perfmon_destroy_ioctl().Signed-off-by Maíra Canal <[email protected]>---
drivers/gpu/drm/v3d/v3d_drv.h | 5 +--drivers/gpu/drm/v3d/v3d_perfmon.c| 55 +++++++++++------------------ --2 files changed, 21 insertions(+), 39 deletions(-)drivers/gpu/drm/v3d/v3d_drv.h +0
-0@@ -220,10 +220,7 @@ v3d_has_csd(struct v3d_dev *v3d) struct v3d_file_priv { struct v3d_dev *v3d;drivers/gpu/drm/v3d/v3d_drv.h +1
-4-struct { -struct idr idr; -struct mutex lock; - } perfmon; +struct xarray perfmons;
structdrm_sched_entitysched_entity[V3D_MAX_QUEUES];drivers/gpu/drm/v3d/v3d_perfmon.c +0
-0@@ -290,24 +290,23 @@ struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id) { struct v3d_perfmon *perfmon;drivers/gpu/drm/v3d/v3d_perfmon.c +2
-2-mutex_lock (&v3d_priv->perfmon.lock); - perfmon =idr_find (&v3d_priv->perfmon.idr, id); +xa_lock (&v3d_priv->perfmons); + perfmon =xa_load (&v3d_priv->perfmons, id);
v3d_perfmon_get(perfmon);drivers/gpu/drm/v3d/v3d_perfmon.c +1
-1-mutex_unlock (&v3d_priv->perfmon.lock); +xa_unlock (&v3d_priv->perfmons);
return perfmon;
}
voidv3d_perfmon_open_file(struct v3d_file_priv *v3d_priv){drivers/gpu/drm/v3d/v3d_perfmon.c +1
-2-mutex_init (&v3d_priv->perfmon.lock); -idr_init_base (&v3d_priv->perfmon.idr,1 ); +xa_init_flags (&v3d_priv->perfmons,XA_FLAGS_ALLOC1 );}
drivers/gpu/drm/v3d/v3d_perfmon.c +2
-1-static int v3d_perfmon_idr_del (int id,void *elem,void *data) +static void v3d_perfmon_delete (struct v3d_file_priv *v3d_priv, + struct v3d_perfmon *perfmon){
drivers/gpu/drm/v3d/v3d_perfmon.c +1
-2-struct v3d_perfmon *perfmon = elem; -struct v3d_dev *v3d = (struct v3d_dev *)data; +struct v3d_dev *v3d = v3d_priv->v3d;
/* If the active perfmon is being destroyed, stop it first */
if (perfmon ==v3d->active_perfmon)drivers/gpu/drm/v3d/v3d_perfmon.c +0
-0@@ -317,19 +316,17 @@ static int v3d_perfmon_idr_del(int id, void *elem, void *data) cmpxchg(&v3d->global_perfmon, perfmon, NULL);
v3d_perfmon_put(perfmon);drivers/gpu/drm/v3d/v3d_perfmon.c +0
-2- -return 0 ;}
voidv3d_perfmon_close_file(struct v3d_file_priv *v3d_priv){drivers/gpu/drm/v3d/v3d_perfmon.c +2
-1-struct v3d_dev *v3d = v3d_priv->v3d; +struct v3d_perfmon *perfmon; +unsigned long id;drivers/gpu/drm/v3d/v3d_perfmon.c +0
-2-mutex_lock (&v3d_priv->perfmon.lock); - idr_for_each(&v3d_priv->perfmon.idr, v3d_perfmon_idr_del,v3d);
drivers/gpu/drm/v3d/v3d_perfmon.c +4
-3-idr_destroy (&v3d_priv->perfmon.idr); -mutex_unlock (&v3d_priv->perfmon.lock); -mutex_destroy (&v3d_priv->perfmon.lock); +xa_for_each (&v3d_priv->perfmons, id, perfmon) +v3d_perfmon_delete (v3d_priv, perfmon); + +xa_destroy (&v3d_priv->perfmons);}
int v3d_perfmon_create_ioctl(structdrm_device*dev, void *data,drivers/gpu/drm/v3d/v3d_perfmon.c +1
-0@@ -341,6 +338,7 @@ int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, struct v3d_perfmon *perfmon; unsigned int i; int ret; +u32 id;
/* Number of monitored counters cannot exceed HW limits. */ if (req->ncounters>DRM_V3D_MAX_PERF_COUNTERS||drivers/gpu/drm/v3d/v3d_perfmon.c +0
-0@@ -366,18 +364,16 @@ int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, refcount_set(&perfmon->refcnt, 1); mutex_init(&perfmon->lock);drivers/gpu/drm/v3d/v3d_perfmon.c +0
-2-mutex_lock (&v3d_priv->perfmon.lock); - ret = idr_alloc(&v3d_priv->perfmon.idr, perfmon,
V3D_PERFMONID_MIN,drivers/gpu/drm/v3d/v3d_perfmon.c +2
-3- V3D_PERFMONID_MAX, GFP_KERNEL); -mutex_unlock (&v3d_priv->perfmon.lock); - + ret = xa_alloc(&v3d_priv->perfmons, &id, perfmon, + XA_LIMIT(V3D_PERFMONID_MIN,
V3D_PERFMONID_MAX),drivers/gpu/drm/v3d/v3d_perfmon.c +1
-0+ GFP_KERNEL);if (ret < 0) {
mutex_destroy(&perfmon->lock);
kfree(perfmon);
return ret;
}drivers/gpu/drm/v3d/v3d_perfmon.c +1
-1- req->id = ret; + req->id = id;
return 0;
}drivers/gpu/drm/v3d/v3d_perfmon.c +0
-1@@ -387,24 +383,13 @@ 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;drivers/gpu/drm/v3d/v3d_perfmon.c +1
-4-mutex_lock (&v3d_priv->perfmon.lock); - perfmon =idr_remove (&v3d_priv->perfmon.idr, req->id); -mutex_unlock (&v3d_priv->perfmon.lock); - + perfmon =xa_erase (&v3d_priv->perfmons, req->id);if (!perfmon)
return-EINVAL;drivers/gpu/drm/v3d/v3d_perfmon.c +0
-1-/* If the active perfmon is being destroyed, stop it first */
drivers/gpu/drm/v3d/v3d_perfmon.c +0
-4-if (perfmon == v3d->active_perfmon) -v3d_perfmon_stop (v3d, perfmon, false); - -/* If the global perfmon is being destroyed, set it to NULL */
drivers/gpu/drm/v3d/v3d_perfmon.c +1
-3-cmpxchg (&v3d->global_perfmon, perfmon,NULL ); - -v3d_perfmon_put (perfmon); +v3d_perfmon_delete (v3d_priv, perfmon);
return 0;
}
Quoted 78 lines · Maíra Canal
The IDR interface is deprecated and the XArray API is the recommended replacement. Replace the per-file IDR used to track perfmons with an XArray. This allows us to remove the external mutex that protects the IDR.
While here, introduce the
v3d_perfmon_delete()helper to consolidate the perfmon cleanup logic used by bothv3d_perfmon_close_file()andv3d_perfmon_destroy_ioctl().Signed-off-by Maíra Canal <[email protected]>--- drivers/gpu/drm/v3d/v3d_drv.h | 5 +-- drivers/gpu/drm/v3d/v3d_perfmon.c | 55 +++++++++++-------------------- 2 files changed, 21 insertions(+), 39 deletions(-)drivers/gpu/drm/v3d/v3d_drv.h +1
-4@@ -220,10 +220,7 @@ v3d_has_csd(struct v3d_dev *v3d)struct v3d_file_priv {struct v3d_dev *v3d; -struct { -struct idr idr; -struct mutex lock; - } perfmon; +struct xarray perfmons;struct drm_sched_entity sched_entity[V3D_MAX_QUEUES ];drivers/gpu/drm/v3d/v3d_perfmon.c +7
-11@@ -290,24 +290,23 @@ struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id) {struct v3d_perfmon *perfmon; -mutex_lock (&v3d_priv->perfmon.lock); - perfmon =idr_find (&v3d_priv->perfmon.idr, id); +xa_lock (&v3d_priv->perfmons); + perfmon =xa_load (&v3d_priv->perfmons, id);v3d_perfmon_get (perfmon); -mutex_unlock (&v3d_priv->perfmon.lock); +xa_unlock (&v3d_priv->perfmons);return perfmon; }void v3d_perfmon_open_file (struct v3d_file_priv *v3d_priv) { -mutex_init (&v3d_priv->perfmon.lock); -idr_init_base (&v3d_priv->perfmon.idr,1 ); +xa_init_flags (&v3d_priv->perfmons,XA_FLAGS_ALLOC1 ); } -static int v3d_perfmon_idr_del (int id,void *elem,void *data) +static void v3d_perfmon_delete (struct v3d_file_priv *v3d_priv, +struct v3d_perfmon *perfmon) { -struct v3d_perfmon *perfmon = elem; -struct v3d_dev *v3d = (struct v3d_dev *)data; +struct v3d_dev *v3d = v3d_priv->v3d;/* If the active perfmon is being destroyed, stop it first */ if (perfmon == v3d->active_perfmon)@@ -317,19 +316,17 @@ static int v3d_perfmon_idr_del(int id, void *elem, void *data)cmpxchg (&v3d->global_perfmon, perfmon,NULL );v3d_perfmon_put (perfmon); - -return 0 ; }void v3d_perfmon_close_file (struct v3d_file_priv *v3d_priv) { -struct v3d_dev *v3d = v3d_priv->v3d;
drivers/gpu/drm/v3d/v3d_perfmon.c static int v3d_perfmon_idr_del(int id, void *elem, void *data) · line 327
Quoted 10 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +4
-5+struct v3d_perfmon *perfmon; +unsigned long id; -mutex_lock (&v3d_priv->perfmon.lock); -idr_for_each (&v3d_priv->perfmon.idr, v3d_perfmon_idr_del, v3d); -idr_destroy (&v3d_priv->perfmon.idr); -mutex_unlock (&v3d_priv->perfmon.lock); -mutex_destroy (&v3d_priv->perfmon.lock); +xa_for_each (&v3d_priv->perfmons, id, perfmon) +v3d_perfmon_delete (v3d_priv, perfmon);
Looking outside the XArray conversion the situation with v3d->active_perfmon caught my eye. It doesn't look that driver global field is protected by any lock, right? So if one client would be exiting, is there enough serialisation between that and perhaps v3d_switch_perfmon() (from run_job()) could see a stale v3d->active_perfmon pointer and attempt to double put it? I am not sure, but gain, that seem outside the scope of this conversion.
Quoted 14 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +3
-0+ +xa_destroy (&v3d_priv->perfmons); } int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,@@ -341,6 +338,7 @@ int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,struct v3d_perfmon *perfmon;unsigned int i;int ret; +u32 id;/* Number of monitored counters cannot exceed HW limits. */ if (req->ncounters > DRM_V3D_MAX_PERF_COUNTERS ||
drivers/gpu/drm/v3d/v3d_perfmon.c int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, · line 371
Quoted 6 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +0
-3@@ -366,18 +364,16 @@ int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,refcount_set (&perfmon->refcnt,1 );mutex_init (&perfmon->lock); -mutex_lock (&v3d_priv->perfmon.lock); - ret =idr_alloc (&v3d_priv->perfmon.idr, perfmon,V3D_PERFMONID_MIN , -V3D_PERFMONID_MAX ,GFP_KERNEL );
Hm it seems V3D_PERFMONID_MAX being U32_MAX used to overflow the end argument of idr_alloc.
drivers/gpu/drm/v3d/v3d_perfmon.c int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, · line 368
Quoted 4 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +2
-2-mutex_unlock (&v3d_priv->perfmon.lock); - + ret = xa_alloc(&v3d_priv->perfmons, &id, perfmon, + XA_LIMIT(V3D_PERFMONID_MIN, V3D_PERFMONID_MAX),
Could perheps even lose the V3D_PERFMON_MIN/MAX defines and just use xa_limit_32b.
Quoted 26 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +3
-6+ GFP_KERNEL);if (ret <0 ) {mutex_destroy (&perfmon->lock);kfree (perfmon);return ret; } - req->id = ret; + req->id = id;return 0 ; }@@ -387,24 +383,13 @@ 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); - perfmon =idr_remove (&v3d_priv->perfmon.idr, req->id); -mutex_unlock (&v3d_priv->perfmon.lock); - + perfmon =xa_erase (&v3d_priv->perfmons, req->id);if (!perfmon)return -EINVAL ;
drivers/gpu/drm/v3d/v3d_perfmon.c int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data, · line 392
Quoted 10 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +1
-8-/* If the active perfmon is being destroyed, stop it first */ -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); +v3d_perfmon_delete (v3d_priv, perfmon);
Nice consolidation.
Regards,
Tvrtko
Quoted 3 lines
drivers/gpu/drm/v3d/v3d_perfmon.c +0
-0return 0 ; }