Skip to contentSkip to conversation

v3d: Perfmon cleanup

PATCH In review Tvrtko Ursulin 2 participants 13 messages Jul 11, 2024 – Jul 13, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v4 cover v3d: Perfmon cleanup

From: Tvrtko Ursulin <[email protected]>

When we had to quickly deal with a tree build issue via merging 792d16b5375d ("drm/v3d: Move perfmon init completely into own unit"), we promised to follow up with a nicer solution.

As in the process of eliminating the hardcoded defines we have discovered a few issues in handling of corner cases and userspace input validation, the fix has turned into a larger series, but hopefully the end result is a justifiable cleanup.

v2:
* Re-order the patches so fixes come first while last three are optional cleanups.

v3:
* Fixed a bunch of rebase errors I made when re-ordering patches from v1 to v2.
* Dropped the double underscore from __v3d_timestamp_query_info_free.
* Added v3d prefix to v3d_copy_query_info.
* Renamed qinfo to query_info.
* Fixed some spelling errors and bad patch references.
* Added mention to get_user to one commit message.
* Dropped one patch from the series which became redundant due other re-ordering.
* Re-ordered last two patches with the view of dropping the last.

v4:
* Fixed more rebase errors and details in commit messages.

Cc Maíra Canal <[email protected]>

Tvrtko Ursulin (11):
drm/v3d: Prevent out of bounds access in performance query extensions drm/v3d: Fix potential memory leak in the timestamp extension drm/v3d: Fix potential memory leak in the performance extension drm/v3d: Validate passed in drm syncobj handles in the timestamp
extension
drm/v3d: Validate passed in drm syncobj handles in the performance
extension
drm/v3d: Move part of copying of reset/copy performance extension to a
helper
drm/v3d: Size the kperfmon_ids array at runtime
drm/v3d: Do not use intermediate storage when copying performance
query results
drm/v3d: Move perfmon init completely into own unit drm/v3d: Prefer get_user for scalar types
drm/v3d: Add some local variables in queries/extensions

 drivers/gpu/drm/v3d/v3d_drv.c                 |   9 +-
 drivers/gpu/drm/v3d/v3d_drv.h                 |  16 +-
 drivers/gpu/drm/v3d/v3d_perfmon.c             |  44 +--
 .../gpu/drm/v3d/v3d_performance_counters.h    |  16 +-
 drivers/gpu/drm/v3d/v3d_sched.c               | 105 +++++--
 drivers/gpu/drm/v3d/v3d_submit.c              | 294 +++++++++++-------
 6 files changed, 290 insertions(+), 194 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH v4 1/11 drm/v3d: Prevent out of bounds access in performance query extensions

From: Tvrtko Ursulin <[email protected]>

Check that the number of perfmons userspace is passing in the copy and reset extensions is not greater than the internal kernel storage where the ids will be copied into.

Fixes bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job")
Cc Maíra Canal <[email protected]>
Cc Iago Toral Quiroga <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/v3d/v3d_submit.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +6 -0
index 88f63d526b22..263fefc1d04f 100644
@@ -637,6 +637,9 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 	if (copy_from_user(&reset, ext, sizeof(reset)))
 		return -EFAULT;
 
+	if (reset.nperfmons > V3D_MAX_PERFMONS)
+		return -EINVAL;
+
 	job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
 
 	job->performance_query.queries = kvmalloc_array(reset.count,
@@ -708,6 +711,9 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 	if (copy.pad)
 		return -EINVAL;
 
+	if (copy.nperfmons > V3D_MAX_PERFMONS)
+		return -EINVAL;
+
 	job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
 
 	job->performance_query.queries = kvmalloc_array(copy.count,
Signature · 1 line
2.44.0
#2 lore raw

PATCH v4 2/11 drm/v3d: Fix potential memory leak in the timestamp extension

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

If fetching of userspace memory fails during the main loop, all drm sync objs looked up until that point will be leaked because of the missing drm_syncobj_put.

Fix it by exporting and using a common cleanup helper.

Fixes 9ba0ff3e083f ("drm/v3d: Create a CPU job extension for the timestamp query job")
Cc Maíra Canal <[email protected]>
Cc Iago Toral Quiroga <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/v3d/v3d_drv.h    |  2 ++
 drivers/gpu/drm/v3d/v3d_sched.c  | 22 +++++++++++-----
 drivers/gpu/drm/v3d/v3d_submit.c | 43 ++++++++++++++++++++++----------
 3 files changed, 48 insertions(+), 19 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +2 -0
index 099b962bdfde..e208ffdfba32 100644
@@ -563,6 +563,8 @@ void v3d_mmu_insert_ptes(struct v3d_bo *bo);
 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
 
 /* v3d_sched.c */
+void v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
+				   unsigned int count);
 void v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue);
 int v3d_sched_init(struct v3d_dev *v3d);
 void v3d_sched_fini(struct v3d_dev *v3d);
drivers/gpu/drm/v3d/v3d_sched.c +16 -6
index 03df37a3acf5..59dc0287dab9 100644
@@ -73,18 +73,28 @@ v3d_sched_job_free(struct drm_sched_job *sched_job)
 	v3d_job_cleanup(job);
 }
 
+void
+v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
+			      unsigned int count)
+{
+	if (query_info->queries) {
+		unsigned int i;
+
+		for (i = 0; i < count; i++)
+			drm_syncobj_put(query_info->queries[i].syncobj);
+
+		kvfree(query_info->queries);
+	}
+}
+
 static void
 v3d_cpu_job_free(struct drm_sched_job *sched_job)
 {
 	struct v3d_cpu_job *job = to_cpu_job(sched_job);
-	struct v3d_timestamp_query_info *timestamp_query = &job->timestamp_query;
 	struct v3d_performance_query_info *performance_query = &job->performance_query;
 
-	if (timestamp_query->queries) {
-		for (int i = 0; i < timestamp_query->count; i++)
-			drm_syncobj_put(timestamp_query->queries[i].syncobj);
-		kvfree(timestamp_query->queries);
-	}
+	v3d_timestamp_query_info_free(&job->timestamp_query,
+				      job->timestamp_query.count);
 
 	if (performance_query->queries) {
 		for (int i = 0; i < performance_query->count; i++)
drivers/gpu/drm/v3d/v3d_submit.c +30 -13

115 lines not on this page · raw message

Signature · 1 line
2.44.0
#3 lore raw

PATCH v4 3/11 drm/v3d: Fix potential memory leak in the performance extension

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

If fetching of userspace memory fails during the main loop, all drm sync objs looked up until that point will be leaked because of the missing drm_syncobj_put.

Fix it by exporting and using a common cleanup helper.

Fixes bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job")
Cc Maíra Canal <[email protected]>
Cc Iago Toral Quiroga <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/v3d/v3d_drv.h    |  2 ++
 drivers/gpu/drm/v3d/v3d_sched.c  | 22 ++++++++++----
 drivers/gpu/drm/v3d/v3d_submit.c | 52 ++++++++++++++++++++------------
 3 files changed, 50 insertions(+), 26 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +2 -0
index e208ffdfba32..dd3ead4cb8bd 100644
@@ -565,6 +565,8 @@ void v3d_mmu_remove_ptes(struct v3d_bo *bo);
 /* v3d_sched.c */
 void v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
 				   unsigned int count);
+void v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
+				     unsigned int count);
 void v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue);
 int v3d_sched_init(struct v3d_dev *v3d);
 void v3d_sched_fini(struct v3d_dev *v3d);
drivers/gpu/drm/v3d/v3d_sched.c +16 -6
index 59dc0287dab9..5fbbee47c6b7 100644
@@ -87,20 +87,30 @@ v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
 	}
 }
 
+void
+v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
+				unsigned int count)
+{
+	if (query_info->queries) {
+		unsigned int i;
+
+		for (i = 0; i < count; i++)
+			drm_syncobj_put(query_info->queries[i].syncobj);
+
+		kvfree(query_info->queries);
+	}
+}
+
 static void
 v3d_cpu_job_free(struct drm_sched_job *sched_job)
 {
 	struct v3d_cpu_job *job = to_cpu_job(sched_job);
-	struct v3d_performance_query_info *performance_query = &job->performance_query;
 
 	v3d_timestamp_query_info_free(&job->timestamp_query,
 				      job->timestamp_query.count);
 
-	if (performance_query->queries) {
-		for (int i = 0; i < performance_query->count; i++)
-			drm_syncobj_put(performance_query->queries[i].syncobj);
-		kvfree(performance_query->queries);
-	}
+	v3d_performance_query_info_free(&job->performance_query,
+					job->performance_query.count);
 
 	v3d_job_cleanup(&job->base);
 }
drivers/gpu/drm/v3d/v3d_submit.c +32 -20

123 lines not on this page · raw message

Signature · 1 line
2.44.0
#4 lore raw

PATCH v4 4/11 drm/v3d: Validate passed in drm syncobj handles in the timestamp extension

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

If userspace provides an unknown or invalid handle anywhere in the handle array the rest of the driver will not handle that well.

Fix it by checking handle was looked up successfully or otherwise fail the extension by jumping into the existing unwind.

Fixes 9ba0ff3e083f ("drm/v3d: Create a CPU job extension for the timestamp query job")
Cc Maíra Canal <[email protected]>
Cc Iago Toral Quiroga <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/v3d/v3d_submit.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +12 -0
index 50be4e8a7512..9a3e32075ebe 100644
@@ -498,6 +498,10 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
 		}
 
 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
+		if (!job->timestamp_query.queries[i].syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
 	}
 	job->timestamp_query.count = timestamp.count;
 
@@ -552,6 +556,10 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
 		}
 
 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
+		if (!job->timestamp_query.queries[i].syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
 	}
 	job->timestamp_query.count = reset.count;
 
@@ -616,6 +624,10 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 		}
 
 		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
+		if (!job->timestamp_query.queries[i].syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
 	}
 	job->timestamp_query.count = copy.count;
 
Signature · 1 line
2.44.0
#5 lore raw

PATCH v4 5/11 drm/v3d: Validate passed in drm syncobj handles in the performance extension

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

If userspace provides an unknown or invalid handle anywhere in the handle array the rest of the driver will not handle that well.

Fix it by checking handle was looked up successfully or otherwise fail the extension by jumping into the existing unwind.

Fixes bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job")
Cc Maíra Canal <[email protected]>
Cc Iago Toral Quiroga <[email protected]>
Cc <[email protected]> # v6.8+
---
 drivers/gpu/drm/v3d/v3d_submit.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/gpu/drm/v3d/v3d_submit.c +8 -0
index 9a3e32075ebe..4cdfabbf4964 100644
@@ -710,6 +710,10 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 		}
 
 		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
+		if (!job->performance_query.queries[i].syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
 	}
 	job->performance_query.count = reset.count;
 	job->performance_query.nperfmons = reset.nperfmons;
@@ -790,6 +794,10 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 		}
 
 		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
+		if (!job->performance_query.queries[i].syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
 	}
 	job->performance_query.count = copy.count;
 	job->performance_query.nperfmons = copy.nperfmons;
Signature · 1 line
2.44.0
#6 lore raw

PATCH v4 6/11 drm/v3d: Move part of copying of reset/copy performance extension to a helper

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

The loop which looks up the syncobj and copies the kperfmon ids is identical so lets move it to a helper.

The only change is replacing copy_from_user with get_user when copying a scalar.

---
 drivers/gpu/drm/v3d/v3d_submit.c | 152 ++++++++++++++-----------------
 1 file changed, 68 insertions(+), 84 deletions(-)
drivers/gpu/drm/v3d/v3d_submit.c +68 -84
index 4cdfabbf4964..ce56e31a027d 100644
@@ -644,15 +644,64 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 	return err;
 }
 
+static int
+v3d_copy_query_info(struct v3d_performance_query_info *query_info,
+		    unsigned int count,
+		    unsigned int nperfmons,
+		    u32 __user *syncs,
+		    u64 __user *kperfmon_ids,
+		    struct drm_file *file_priv)
+{
+	unsigned int i, j;
+	int err;
+
+	for (i = 0; i < count; i++) {
+		struct v3d_performance_query *query = &query_info->queries[i];
+		u32 __user *ids_pointer;
+		u32 sync, id;
+		u64 ids;
+
+		if (get_user(sync, syncs++)) {
+			err = -EFAULT;
+			goto error;
+		}
+
+		if (get_user(ids, kperfmon_ids++)) {
+			err = -EFAULT;
+			goto error;
+		}
+
+		ids_pointer = u64_to_user_ptr(ids);
+
+		for (j = 0; j < nperfmons; j++) {
+			if (get_user(id, ids_pointer++)) {
+				err = -EFAULT;
+				goto error;
+			}
+
+			query->kperfmon_ids[j] = id;
+		}
+
+		query->syncobj = drm_syncobj_find(file_priv, sync);
+		if (!query->syncobj) {
+			err = -ENOENT;
+			goto error;
+		}
+	}
+
+	return 0;
+
+error:
+	v3d_performance_query_info_free(query_info, i);
+	return err;
+}
+
 static int
 v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 				     struct drm_v3d_extension __user *ext,
 				     struct v3d_cpu_job *job)
 {
-	u32 __user *syncs;
-	u64 __user *kperfmon_ids;
 	struct drm_v3d_reset_performance_query reset;
-	unsigned int i, j;
 	int err;
 
 	if (!job) {
@@ -679,50 +728,19 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 	if (!job->performance_query.queries)
 		return -ENOMEM;
 
-	syncs = u64_to_user_ptr(reset.syncs);
-	kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids);
+	err = v3d_copy_query_info(&job->performance_query,
+				  reset.count,
+				  reset.nperfmons,
+				  u64_to_user_ptr(reset.syncs),
+				  u64_to_user_ptr(reset.kperfmon_ids),
+				  file_priv);
+	if (err)
+		return err;
 
-	for (i = 0; i < reset.count; i++) {
-		u32 sync;
-		u64 ids;
-		u32 __user *ids_pointer;
-		u32 id;
-
-		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
-			err = -EFAULT;
-			goto error;
-		}
-
-		if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
-			err = -EFAULT;
-			goto error;
-		}
-
-		ids_pointer = u64_to_user_ptr(ids);
-
-		for (j = 0; j < reset.nperfmons; j++) {
-			if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
-				err = -EFAULT;
-				goto error;
-			}
-
-			job->performance_query.queries[i].kperfmon_ids[j] = id;
-		}
-
-		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
-		if (!job->performance_query.queries[i].syncobj) {
-			err = -ENOENT;
-			goto error;
-		}
-	}
 	job->performance_query.count = reset.count;
 	job->performance_query.nperfmons = reset.nperfmons;
 
 	return 0;
-
-error:
-	v3d_performance_query_info_free(&job->performance_query, i);
-	return err;
 }
 
 static int
@@ -730,10 +748,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 					  struct drm_v3d_extension __user *ext,
 					  struct v3d_cpu_job *job)
 {
-	u32 __user *syncs;
-	u64 __user *kperfmon_ids;
 	struct drm_v3d_copy_performance_query copy;
-	unsigned int i, j;
 	int err;
 
 	if (!job) {
@@ -763,42 +778,15 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 	if (!job->performance_query.queries)
 		return -ENOMEM;
 
-	syncs = u64_to_user_ptr(copy.syncs);
-	kperfmon_ids = u64_to_user_ptr(copy.kperfmon_ids);
+	err = v3d_copy_query_info(&job->performance_query,
+				  copy.count,
+				  copy.nperfmons,
+				  u64_to_user_ptr(copy.syncs),
+				  u64_to_user_ptr(copy.kperfmon_ids),
+				  file_priv);
+	if (err)
+		return err;
 
-	for (i = 0; i < copy.count; i++) {
-		u32 sync;
-		u64 ids;
-		u32 __user *ids_pointer;
-		u32 id;
-
-		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
-			err = -EFAULT;
-			goto error;
-		}
-
-		if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) {
-			err = -EFAULT;
-			goto error;
-		}
-
-		ids_pointer = u64_to_user_ptr(ids);
-
-		for (j = 0; j < copy.nperfmons; j++) {
-			if (copy_from_user(&id, ids_pointer++, sizeof(id))) {
-				err = -EFAULT;
-				goto error;
-			}
-
-			job->performance_query.queries[i].kperfmon_ids[j] = id;
-		}
-
-		job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
-		if (!job->performance_query.queries[i].syncobj) {
-			err = -ENOENT;
-			goto error;
-		}
-	}
 	job->performance_query.count = copy.count;
 	job->performance_query.nperfmons = copy.nperfmons;
 	job->performance_query.ncounters = copy.ncounters;
@@ -810,10 +798,6 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 	job->copy.stride = copy.stride;
 
 	return 0;
-
-error:
-	v3d_performance_query_info_free(&job->performance_query, i);
-	return err;
 }
 
 /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data
Signature · 1 line
2.44.0
#7 lore raw

PATCH v4 7/11 drm/v3d: Size the kperfmon_ids array at runtime

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Instead of statically reserving pessimistic space for the kperfmon_ids array, make the userspace extension code allocate the exactly required amount of space.

Apart from saving some memory at runtime, this also removes the need for the V3D_MAX_PERFMONS macro whose removal will benefit further driver cleanup.

---
 drivers/gpu/drm/v3d/v3d_drv.h    |  6 +-----
 drivers/gpu/drm/v3d/v3d_sched.c  |  4 +++-
 drivers/gpu/drm/v3d/v3d_submit.c | 17 +++++++++++------
 3 files changed, 15 insertions(+), 12 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.h +1 -5
index dd3ead4cb8bd..b1dfec49ba7d 100644
@@ -351,13 +351,9 @@ struct v3d_timestamp_query {
 	struct drm_syncobj *syncobj;
 };
 
-/* Number of perfmons required to handle all supported performance counters */
-#define V3D_MAX_PERFMONS DIV_ROUND_UP(V3D_MAX_COUNTERS, \
-				      DRM_V3D_MAX_PERF_COUNTERS)
-
 struct v3d_performance_query {
 	/* Performance monitor IDs for this query */
-	u32 kperfmon_ids[V3D_MAX_PERFMONS];
+	u32 *kperfmon_ids;
 
 	/* Syncobj that indicates the query availability */
 	struct drm_syncobj *syncobj;
drivers/gpu/drm/v3d/v3d_sched.c +3 -1
index 5fbbee47c6b7..7b2195ba4248 100644
@@ -94,8 +94,10 @@ v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
 	if (query_info->queries) {
 		unsigned int i;
 
-		for (i = 0; i < count; i++)
+		for (i = 0; i < count; i++) {
 			drm_syncobj_put(query_info->queries[i].syncobj);
+			kvfree(query_info->queries[i].kperfmon_ids);
+		}
 
 		kvfree(query_info->queries);
 	}
drivers/gpu/drm/v3d/v3d_submit.c +11 -6
index ce56e31a027d..d1060e60aafa 100644
@@ -671,10 +671,20 @@ v3d_copy_query_info(struct v3d_performance_query_info *query_info,
 			goto error;
 		}
 
+		query->kperfmon_ids =
+			kvmalloc_array(nperfmons,
+				       sizeof(struct v3d_performance_query *),
+				       GFP_KERNEL);
+		if (!query->kperfmon_ids) {
+			err = -ENOMEM;
+			goto error;
+		}
+
 		ids_pointer = u64_to_user_ptr(ids);
 
 		for (j = 0; j < nperfmons; j++) {
 			if (get_user(id, ids_pointer++)) {
+				kvfree(query->kperfmon_ids);
 				err = -EFAULT;
 				goto error;
 			}
@@ -684,6 +694,7 @@ v3d_copy_query_info(struct v3d_performance_query_info *query_info,
 
 		query->syncobj = drm_syncobj_find(file_priv, sync);
 		if (!query->syncobj) {
+			kvfree(query->kperfmon_ids);
 			err = -ENOENT;
 			goto error;
 		}
@@ -717,9 +728,6 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 	if (copy_from_user(&reset, ext, sizeof(reset)))
 		return -EFAULT;
 
-	if (reset.nperfmons > V3D_MAX_PERFMONS)
-		return -EINVAL;
-
 	job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
 
 	job->performance_query.queries = kvmalloc_array(reset.count,
@@ -767,9 +775,6 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 	if (copy.pad)
 		return -EINVAL;
 
-	if (copy.nperfmons > V3D_MAX_PERFMONS)
-		return -EINVAL;
-
 	job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
 
 	job->performance_query.queries = kvmalloc_array(copy.count,
Signature · 1 line
2.44.0
#8 lore raw

PATCH v4 8/11 drm/v3d: Do not use intermediate storage when copying performance query results

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Removing the intermediate buffer removes the last use of the V3D_MAX_COUNTERS define, which will enable further driver cleanup.

While at it pull the 32 vs 64 bit copying decision outside the loop in order to reduce the number of conditional instructions.

---
 drivers/gpu/drm/v3d/v3d_sched.c | 59 +++++++++++++++++++++------------
 1 file changed, 37 insertions(+), 22 deletions(-)
drivers/gpu/drm/v3d/v3d_sched.c +37 -22
index 7b2195ba4248..d193072703f3 100644
@@ -421,18 +421,23 @@ v3d_reset_timestamp_queries(struct v3d_cpu_job *job)
 	v3d_put_bo_vaddr(bo);
 }
 
+static void write_to_buffer_32(u32 *dst, unsigned int idx, u32 value)
+{
+	dst[idx] = value;
+}
+
+static void write_to_buffer_64(u64 *dst, unsigned int idx, u64 value)
+{
+	dst[idx] = value;
+}
+
 static void
-write_to_buffer(void *dst, u32 idx, bool do_64bit, u64 value)
+write_to_buffer(void *dst, unsigned int idx, bool do_64bit, u64 value)
 {
-	if (do_64bit) {
-		u64 *dst64 = (u64 *)dst;
-
-		dst64[idx] = value;
-	} else {
-		u32 *dst32 = (u32 *)dst;
-
-		dst32[idx] = (u32)value;
-	}
+	if (do_64bit)
+		write_to_buffer_64(dst, idx, value);
+	else
+		write_to_buffer_32(dst, idx, value);
 }
 
 static void
@@ -505,18 +510,24 @@ v3d_reset_performance_queries(struct v3d_cpu_job *job)
 }
 
 static void
-v3d_write_performance_query_result(struct v3d_cpu_job *job, void *data, u32 query)
+v3d_write_performance_query_result(struct v3d_cpu_job *job, void *data,
+				   unsigned int query)
 {
-	struct v3d_performance_query_info *performance_query = &job->performance_query;
-	struct v3d_copy_query_results_info *copy = &job->copy;
+	struct v3d_performance_query_info *performance_query =
+						&job->performance_query;
 	struct v3d_file_priv *v3d_priv = job->base.file->driver_priv;
+	struct v3d_performance_query *perf_query =
+			&performance_query->queries[query];
 	struct v3d_dev *v3d = job->base.v3d;
-	struct v3d_perfmon *perfmon;
-	u64 counter_values[V3D_MAX_COUNTERS];
+	unsigned int i, j, offset;
+
+	for (i = 0, offset = 0;
+	     i < performance_query->nperfmons;
+	     i++, offset += DRM_V3D_MAX_PERF_COUNTERS) {
+		struct v3d_perfmon *perfmon;
 
-	for (int i = 0; i < performance_query->nperfmons; i++) {
 		perfmon = v3d_perfmon_find(v3d_priv,
-					   performance_query->queries[query].kperfmon_ids[i]);
+					   perf_query->kperfmon_ids[i]);
 		if (!perfmon) {
 			DRM_DEBUG("Failed to find perfmon.");
 			continue;
@@ -524,14 +535,18 @@ v3d_write_performance_query_result(struct v3d_cpu_job *job, void *data, u32 quer
 
 		v3d_perfmon_stop(v3d, perfmon, true);
 
-		memcpy(&counter_values[i * DRM_V3D_MAX_PERF_COUNTERS], perfmon->values,
-		       perfmon->ncounters * sizeof(u64));
+		if (job->copy.do_64bit) {
+			for (j = 0; j < perfmon->ncounters; j++)
+				write_to_buffer_64(data, offset + j,
+						   perfmon->values[j]);
+		} else {
+			for (j = 0; j < perfmon->ncounters; j++)
+				write_to_buffer_32(data, offset + j,
+						   perfmon->values[j]);
+		}
 
 		v3d_perfmon_put(perfmon);
 	}
-
-	for (int i = 0; i < performance_query->ncounters; i++)
-		write_to_buffer(data, i, copy->do_64bit, counter_values[i]);
 }
 
 static void
Signature · 1 line
2.44.0
#9 lore raw

PATCH v4 9/11 drm/v3d: Move perfmon init completely into own unit

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Now that the build time dependencies on various array sizes have been removed, we can move the perfmon init completely into its own compilation unit and remove the hardcoded defines.

This improves on the temporary fix quickly delivered in commit 9c3951ec27b9 ("drm/v3d: Fix perfmon build error/warning").

References: 9c3951ec27b9 ("drm/v3d: Fix perfmon build error/warning")

---
 drivers/gpu/drm/v3d/v3d_drv.c                 |  9 +---
 drivers/gpu/drm/v3d/v3d_drv.h                 |  6 +--
 drivers/gpu/drm/v3d/v3d_perfmon.c             | 44 +++++++++++--------
 .../gpu/drm/v3d/v3d_performance_counters.h    | 16 ++++---
 4 files changed, 40 insertions(+), 35 deletions(-)
drivers/gpu/drm/v3d/v3d_drv.c +2 -7
index a47f00b443d3..491c638a4d74 100644
@@ -95,7 +95,7 @@ static int v3d_get_param_ioctl(struct drm_device *dev, void *data,
 		args->value = 1;
 		return 0;
 	case DRM_V3D_PARAM_MAX_PERF_COUNTERS:
-		args->value = v3d->max_counters;
+		args->value = v3d->perfmon_info.max_counters;
 		return 0;
 	default:
 		DRM_DEBUG("Unknown parameter %d\n", args->param);
@@ -298,12 +298,7 @@ static int v3d_platform_drm_probe(struct platform_device *pdev)
 	v3d->cores = V3D_GET_FIELD(ident1, V3D_HUB_IDENT1_NCORES);
 	WARN_ON(v3d->cores > 1); /* multicore not yet implemented */
 
-	if (v3d->ver >= 71)
-		v3d->max_counters = V3D_V71_NUM_PERFCOUNTERS;
-	else if (v3d->ver >= 42)
-		v3d->max_counters = V3D_V42_NUM_PERFCOUNTERS;
-	else
-		v3d->max_counters = 0;
+	v3d_perfmon_init(v3d);
 
 	v3d->reset = devm_reset_control_get_exclusive(dev, NULL);
 	if (IS_ERR(v3d->reset)) {
drivers/gpu/drm/v3d/v3d_drv.h +2 -4
index b1dfec49ba7d..8524761bc62d 100644
@@ -104,10 +104,7 @@ struct v3d_dev {
 	int ver;
 	bool single_irq_line;
 
-	/* Different revisions of V3D have different total number of performance
-	 * counters
-	 */
-	unsigned int max_counters;
+	struct v3d_perfmon_info perfmon_info;
 
 	void __iomem *hub_regs;
 	void __iomem *core_regs[3];
@@ -568,6 +565,7 @@ int v3d_sched_init(struct v3d_dev *v3d);
 void v3d_sched_fini(struct v3d_dev *v3d);
 
 /* v3d_perfmon.c */
+void v3d_perfmon_init(struct v3d_dev *v3d);
 void v3d_perfmon_get(struct v3d_perfmon *perfmon);
 void v3d_perfmon_put(struct v3d_perfmon *perfmon);
 void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon);
drivers/gpu/drm/v3d/v3d_perfmon.c +25 -19

64 lines not on this page · raw message

drivers/gpu/drm/v3d/v3d_performance_counters.h +11 -5
index 131b2909522a..d919a2fc9449 100644
@@ -19,11 +19,17 @@ struct v3d_perf_counter_desc {
 	char description[256];
 };
 
+struct v3d_perfmon_info {
+	/*
+	 * Different revisions of V3D have different total number of
+	 * performance counters.
+	 */
+	unsigned int max_counters;
 
-#define V3D_V42_NUM_PERFCOUNTERS (87)
-#define V3D_V71_NUM_PERFCOUNTERS (93)
-
-/* Maximum number of performance counters supported by any version of V3D */
-#define V3D_MAX_COUNTERS (93)
+	/*
+	 * Array of counters valid for the platform.
+	 */
+	const struct v3d_perf_counter_desc *counters;
+};
 
 #endif
Signature · 1 line
2.44.0
#10 lore raw

PATCH v4 10/11 drm/v3d: Prefer get_user for scalar types

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

It makes it just a tiny bit more obvious what is going on.

---
 drivers/gpu/drm/v3d/v3d_submit.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/gpu/drm/v3d/v3d_submit.c +5 -5
index d1060e60aafa..b282d12571b5 100644
@@ -485,14 +485,14 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
 	for (i = 0; i < timestamp.count; i++) {
 		u32 offset, sync;
 
-		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
+		if (get_user(offset, offsets++)) {
 			err = -EFAULT;
 			goto error;
 		}
 
 		job->timestamp_query.queries[i].offset = offset;
 
-		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
+		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
@@ -550,7 +550,7 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
 
 		job->timestamp_query.queries[i].offset = reset.offset + 8 * i;
 
-		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
+		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
@@ -611,14 +611,14 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 	for (i = 0; i < copy.count; i++) {
 		u32 offset, sync;
 
-		if (copy_from_user(&offset, offsets++, sizeof(offset))) {
+		if (get_user(offset, offsets++)) {
 			err = -EFAULT;
 			goto error;
 		}
 
 		job->timestamp_query.queries[i].offset = offset;
 
-		if (copy_from_user(&sync, syncs++, sizeof(sync))) {
+		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
Signature · 1 line
2.44.0
#11 lore raw

PATCH v4 11/11 drm/v3d: Add some local variables in queries/extensions

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Add some local variables to make the code a bit less verbose, with the main benefit being pulling some lines to under 80 columns wide.

---
 drivers/gpu/drm/v3d/v3d_submit.c | 88 ++++++++++++++++++--------------
 1 file changed, 49 insertions(+), 39 deletions(-)
drivers/gpu/drm/v3d/v3d_submit.c +49 -39
index b282d12571b5..d607aa9c4ec2 100644
@@ -452,6 +452,7 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
 {
 	u32 __user *offsets, *syncs;
 	struct drm_v3d_timestamp_query timestamp;
+	struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
 	unsigned int i;
 	int err;
 
@@ -473,10 +474,10 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
 
 	job->job_type = V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY;
 
-	job->timestamp_query.queries = kvmalloc_array(timestamp.count,
-						      sizeof(struct v3d_timestamp_query),
-						      GFP_KERNEL);
-	if (!job->timestamp_query.queries)
+	query_info->queries = kvmalloc_array(timestamp.count,
+					     sizeof(struct v3d_timestamp_query),
+					     GFP_KERNEL);
+	if (!query_info->queries)
 		return -ENOMEM;
 
 	offsets = u64_to_user_ptr(timestamp.offsets);
@@ -490,20 +491,21 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv,
 			goto error;
 		}
 
-		job->timestamp_query.queries[i].offset = offset;
+		query_info->queries[i].offset = offset;
 
 		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
 
-		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
-		if (!job->timestamp_query.queries[i].syncobj) {
+		query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
+								  sync);
+		if (!query_info->queries[i].syncobj) {
 			err = -ENOENT;
 			goto error;
 		}
 	}
-	job->timestamp_query.count = timestamp.count;
+	query_info->count = timestamp.count;
 
 	return 0;
 
@@ -519,6 +521,7 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
 {
 	u32 __user *syncs;
 	struct drm_v3d_reset_timestamp_query reset;
+	struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
 	unsigned int i;
 	int err;
 
@@ -537,10 +540,10 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
 
 	job->job_type = V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY;
 
-	job->timestamp_query.queries = kvmalloc_array(reset.count,
-						      sizeof(struct v3d_timestamp_query),
-						      GFP_KERNEL);
-	if (!job->timestamp_query.queries)
+	query_info->queries = kvmalloc_array(reset.count,
+					     sizeof(struct v3d_timestamp_query),
+					     GFP_KERNEL);
+	if (!query_info->queries)
 		return -ENOMEM;
 
 	syncs = u64_to_user_ptr(reset.syncs);
@@ -548,20 +551,21 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv,
 	for (i = 0; i < reset.count; i++) {
 		u32 sync;
 
-		job->timestamp_query.queries[i].offset = reset.offset + 8 * i;
+		query_info->queries[i].offset = reset.offset + 8 * i;
 
 		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
 
-		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
-		if (!job->timestamp_query.queries[i].syncobj) {
+		query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
+								  sync);
+		if (!query_info->queries[i].syncobj) {
 			err = -ENOENT;
 			goto error;
 		}
 	}
-	job->timestamp_query.count = reset.count;
+	query_info->count = reset.count;
 
 	return 0;
 
@@ -578,6 +582,7 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 {
 	u32 __user *offsets, *syncs;
 	struct drm_v3d_copy_timestamp_query copy;
+	struct v3d_timestamp_query_info *query_info = &job->timestamp_query;
 	unsigned int i;
 	int err;
 
@@ -599,10 +604,10 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 
 	job->job_type = V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY;
 
-	job->timestamp_query.queries = kvmalloc_array(copy.count,
-						      sizeof(struct v3d_timestamp_query),
-						      GFP_KERNEL);
-	if (!job->timestamp_query.queries)
+	query_info->queries = kvmalloc_array(copy.count,
+					     sizeof(struct v3d_timestamp_query),
+					     GFP_KERNEL);
+	if (!query_info->queries)
 		return -ENOMEM;
 
 	offsets = u64_to_user_ptr(copy.offsets);
@@ -616,20 +621,21 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv,
 			goto error;
 		}
 
-		job->timestamp_query.queries[i].offset = offset;
+		query_info->queries[i].offset = offset;
 
 		if (get_user(sync, syncs++)) {
 			err = -EFAULT;
 			goto error;
 		}
 
-		job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync);
-		if (!job->timestamp_query.queries[i].syncobj) {
+		query_info->queries[i].syncobj = drm_syncobj_find(file_priv,
+								  sync);
+		if (!query_info->queries[i].syncobj) {
 			err = -ENOENT;
 			goto error;
 		}
 	}
-	job->timestamp_query.count = copy.count;
+	query_info->count = copy.count;
 
 	job->copy.do_64bit = copy.do_64bit;
 	job->copy.do_partial = copy.do_partial;
@@ -712,6 +718,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 				     struct drm_v3d_extension __user *ext,
 				     struct v3d_cpu_job *job)
 {
+	struct v3d_performance_query_info *query_info = &job->performance_query;
 	struct drm_v3d_reset_performance_query reset;
 	int err;
 
@@ -730,13 +737,14 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 
 	job->job_type = V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY;
 
-	job->performance_query.queries = kvmalloc_array(reset.count,
-							sizeof(struct v3d_performance_query),
-							GFP_KERNEL);
-	if (!job->performance_query.queries)
+	query_info->queries =
+		kvmalloc_array(reset.count,
+			       sizeof(struct v3d_performance_query),
+			       GFP_KERNEL);
+	if (!query_info->queries)
 		return -ENOMEM;
 
-	err = v3d_copy_query_info(&job->performance_query,
+	err = v3d_copy_query_info(query_info,
 				  reset.count,
 				  reset.nperfmons,
 				  u64_to_user_ptr(reset.syncs),
@@ -745,8 +753,8 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv,
 	if (err)
 		return err;
 
-	job->performance_query.count = reset.count;
-	job->performance_query.nperfmons = reset.nperfmons;
+	query_info->count = reset.count;
+	query_info->nperfmons = reset.nperfmons;
 
 	return 0;
 }
@@ -756,6 +764,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 					  struct drm_v3d_extension __user *ext,
 					  struct v3d_cpu_job *job)
 {
+	struct v3d_performance_query_info *query_info = &job->performance_query;
 	struct drm_v3d_copy_performance_query copy;
 	int err;
 
@@ -777,13 +786,14 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 
 	job->job_type = V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY;
 
-	job->performance_query.queries = kvmalloc_array(copy.count,
-							sizeof(struct v3d_performance_query),
-							GFP_KERNEL);
-	if (!job->performance_query.queries)
+	query_info->queries =
+		kvmalloc_array(copy.count,
+			       sizeof(struct v3d_performance_query),
+			       GFP_KERNEL);
+	if (!query_info->queries)
 		return -ENOMEM;
 
-	err = v3d_copy_query_info(&job->performance_query,
+	err = v3d_copy_query_info(query_info,
 				  copy.count,
 				  copy.nperfmons,
 				  u64_to_user_ptr(copy.syncs),
@@ -792,9 +802,9 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv,
 	if (err)
 		return err;
 
-	job->performance_query.count = copy.count;
-	job->performance_query.nperfmons = copy.nperfmons;
-	job->performance_query.ncounters = copy.ncounters;
+	query_info->count = copy.count;
+	query_info->nperfmons = copy.nperfmons;
+	query_info->ncounters = copy.ncounters;
 
 	job->copy.do_64bit = copy.do_64bit;
 	job->copy.do_partial = copy.do_partial;
Signature · 1 line
2.44.0
maintainer
#12 lore raw
In reply to Tvrtko Ursulin
Quoted 31 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

When we had to quickly deal with a tree build issue via merging 792d16b5375d ("drm/v3d: Move perfmon init completely into own unit"), we promised to follow up with a nicer solution.

As in the process of eliminating the hardcoded defines we have discovered a few issues in handling of corner cases and userspace input validation, the fix has turned into a larger series, but hopefully the end result is a justifiable cleanup.

v2:
* Re-order the patches so fixes come first while last three are optional cleanups.

v3:
* Fixed a bunch of rebase errors I made when re-ordering patches from v1 to v2.
* Dropped the double underscore from __v3d_timestamp_query_info_free.
* Added v3d prefix to v3d_copy_query_info.
* Renamed qinfo to query_info.
* Fixed some spelling errors and bad patch references.
* Added mention to get_user to one commit message.
* Dropped one patch from the series which became redundant due other re-ordering.
* Re-ordered last two patches with the view of dropping the last.

v4:
* Fixed more rebase errors and details in commit messages.

Cc Maíra Canal <[email protected]>
Quoted 16 lines

Tvrtko Ursulin (11):
drm/v3d: Prevent out of bounds access in performance query extensions drm/v3d: Fix potential memory leak in the timestamp extension drm/v3d: Fix potential memory leak in the performance extension drm/v3d: Validate passed in drm syncobj handles in the timestamp
extension
drm/v3d: Validate passed in drm syncobj handles in the performance
extension
drm/v3d: Move part of copying of reset/copy performance extension to a
helper
drm/v3d: Size the kperfmon_ids array at runtime
drm/v3d: Do not use intermediate storage when copying performance
query results
drm/v3d: Move perfmon init completely into own unit drm/v3d: Prefer get_user for scalar types
drm/v3d: Add some local variables in queries/extensions

I just applied all patches to drm-misc/drm-misc-next!

@Maxime, @Thomas or @Maarten, is it possible to cherry-pick the following patches to drm-misc-fixes?

f32b5128d2c4 drm/v3d: Prevent out of bounds access in performance query extensions 753ce4fea621 drm/v3d: Fix potential memory leak in the timestamp extension 484de39fa5f5 drm/v3d: Fix potential memory leak in the performance extension 8d1276d1b8f7 drm/v3d: Validate passed in drm syncobj handles in the timestamp extension a546b7e4d73c drm/v3d: Validate passed in drm syncobj handles in the performance extension

Tvrtko made sure to make them independent (Thanks Tvrtko!), so I believe it is going to be pretty straight forward to cherry-pick them.

Thanks Tvrtko for the patches and all the maintainers for the great work!

Best Regards,
- Maíra

Quoted 9 lines
  drivers/gpu/drm/v3d/v3d_drv.c                 |   9 +-
  drivers/gpu/drm/v3d/v3d_drv.h                 |  16 +-
  drivers/gpu/drm/v3d/v3d_perfmon.c             |  44 +--
  .../gpu/drm/v3d/v3d_performance_counters.h    |  16 +-
  drivers/gpu/drm/v3d/v3d_sched.c               | 105 +++++--
  drivers/gpu/drm/v3d/v3d_submit.c              | 294 +++++++++++-------
  6 files changed, 290 insertions(+), 194 deletions(-)