From patchwork Tue Jul 9 16:34:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728333 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1CAE5C2BD09 for ; Tue, 9 Jul 2024 16:34:36 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8D30B10E5F3; Tue, 9 Jul 2024 16:34:35 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="k6ns9ekd"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 224BC10E5F3 for ; Tue, 9 Jul 2024 16:34:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=2b1k5yU+YmJ6Limrt534OhRCK30OkGrtORCAhUx2zYU=; b=k6ns9ekdD+JZX/qBD93wekKAco UFDXBv0fIZKmQ6aEpDrJ+80XDKgZiVz//Ns34Alu6CnKhJnTGrVYJf29qm3XPvRbyqQp0np5D4AxO xk2ekBzsSmiSxhbFnOgfV3/wLgGlDptE8bqfQn7zYo1pqOiV1+kKUxa3A+wamkoNQRn5QNlfJ6ekO XWagNAWuTx8/3pjCceyzKOLh+DPnxnSW5jkufUu6m/scy/gkvymJgu7T6DVz8Zi0oJE3ns7U/COdo koDOJIYbivgRbUdFU9psn4TWUIr7S6xBxuo6L96cozOZaFb9sjTXebaTIW896wOMGPn3lOiTQ2XZH LTykoTkw==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnT-00Cso1-Vc; Tue, 09 Jul 2024 18:34:32 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin , Iago Toral Quiroga , stable@vger.kernel.org Subject: [PATCH 01/12] drm/v3d: Prevent out of bounds access in performance query extensions Date: Tue, 9 Jul 2024 17:34:14 +0100 Message-ID: <20240709163425.58276-2-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin Fixes: bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job" Cc: Maíra Canal Cc: Iago Toral Quiroga Cc: # v6.8+ --- drivers/gpu/drm/v3d/v3d_submit.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 88f63d526b22..263fefc1d04f 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -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, From patchwork Tue Jul 9 16:34:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728335 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id BE269C2BD09 for ; Tue, 9 Jul 2024 16:34:40 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C3D0C10E5F5; Tue, 9 Jul 2024 16:34:35 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="IoLxkR/r"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5F47710E5F6 for ; Tue, 9 Jul 2024 16:34:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=pe8qLXKb4yUucXgdgauPrXmTrAS7zzDlwoQ4pvEUJ9Y=; b=IoLxkR/r8Xw7N+ejo8pS0EQZrA vLTWE4kuR5QRZLzj0m50kGTZ/Z+TjBYO1IQsLcLUWv6ExMBgVpeM16tR3ARDksOrUrbzxJGvybIeK a6/iqwBDFpPhmQ+5MNDJGQr20XT/7cSC43f1pMJApEX5kyINM4WHNs2dCsv2MMxOQPTjBOXEyE/fI 3KZBHsgIwL+K5MyJ7vn5U/VF0ePA/YxIxLPocGw3kifYGegruglbF5gA7SE582F1zi4RrHqxi6qTH xr/0ItDEbKviZ5BeQZL67fbhkom++TKgzQxSEcbQ4UExL5siycEJxCqpEQlGsUPkmV+4Kb5FWiwOY aREdjSQg==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnU-00Cso6-LE; Tue, 09 Jul 2024 18:34:32 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 02/12] drm/v3d: Prefer get_user for scalar types Date: Tue, 9 Jul 2024 17:34:15 +0100 Message-ID: <20240709163425.58276-3-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin It makes it just a tiny bit more obvious what is going on. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/v3d/v3d_submit.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 263fefc1d04f..5c71e9adfc65 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -483,14 +483,14 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, for (int i = 0; i < timestamp.count; i++) { u32 offset, sync; - if (copy_from_user(&offset, offsets++, sizeof(offset))) { + if (get_user(offset, offsets++)) { kvfree(job->timestamp_query.queries); return -EFAULT; } job->timestamp_query.queries[i].offset = offset; - if (copy_from_user(&sync, syncs++, sizeof(sync))) { + if (get_user(sync, syncs++)) { kvfree(job->timestamp_query.queries); return -EFAULT; } @@ -538,7 +538,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++)) { kvfree(job->timestamp_query.queries); return -EFAULT; } @@ -590,14 +590,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++)) { kvfree(job->timestamp_query.queries); return -EFAULT; } job->timestamp_query.queries[i].offset = offset; - if (copy_from_user(&sync, syncs++, sizeof(sync))) { + if (get_user(sync, syncs++)) { kvfree(job->timestamp_query.queries); return -EFAULT; } @@ -657,14 +657,14 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, u32 __user *ids_pointer; u32 id; - if (copy_from_user(&sync, syncs++, sizeof(sync))) { + if (get_user(sync, syncs++)) { kvfree(job->performance_query.queries); return -EFAULT; } job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) { + if (get_user(ids, kperfmon_ids++)) { kvfree(job->performance_query.queries); return -EFAULT; } @@ -672,7 +672,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, ids_pointer = u64_to_user_ptr(ids); for (int j = 0; j < reset.nperfmons; j++) { - if (copy_from_user(&id, ids_pointer++, sizeof(id))) { + if (get_user(id, ids_pointer++)) { kvfree(job->performance_query.queries); return -EFAULT; } @@ -731,14 +731,14 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, u32 __user *ids_pointer; u32 id; - if (copy_from_user(&sync, syncs++, sizeof(sync))) { + if (get_user(sync, syncs++)) { kvfree(job->performance_query.queries); return -EFAULT; } job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (copy_from_user(&ids, kperfmon_ids++, sizeof(ids))) { + if (get_user(ids, kperfmon_ids++)) { kvfree(job->performance_query.queries); return -EFAULT; } @@ -746,7 +746,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, ids_pointer = u64_to_user_ptr(ids); for (int j = 0; j < copy.nperfmons; j++) { - if (copy_from_user(&id, ids_pointer++, sizeof(id))) { + if (get_user(id, ids_pointer++)) { kvfree(job->performance_query.queries); return -EFAULT; } From patchwork Tue Jul 9 16:34:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728337 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 33496C3DA49 for ; Tue, 9 Jul 2024 16:34:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C7D3D10E5FC; Tue, 9 Jul 2024 16:34:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="VbRIlqEd"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 07A8D10E5F3 for ; Tue, 9 Jul 2024 16:34:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=R/nlwyK0LPORcEoyFLMAVAVb7rqhKlDWQ9mth+IBovU=; b=VbRIlqEdysM//tNdBo8quQ2h9T w9EJoRCoVgomkht+3jd+MazDsyTEm4RCo7nZoB3A3aPGZOa3aFSoPqdUMzJYDfMFcDtCP+QSjPlpI kb1xNgF6a+iUeVqpEV/QVci0y3///MAL4vXUekIxolyctGLSJ2K2z/JXVdgmRjt0FaofMPnVsHz8E 5CBjI9OujSv0MNv3bfnCGqyi6bqV207yyMsz1aNA8V37KDjbCDv0sD3/FIU0nIWGfNdPV0cbIFsiI nK/Q5uH0EFTEXiGT+62h2LmD1mRDyrrJectLZTL2KGQovU3kVxRg65BH5UAs5ZbIo1/BE6jsBqs8i 8AfcFUFQ==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnV-00CsoA-Aa; Tue, 09 Jul 2024 18:34:33 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 03/12] drm/v3d: Add some local variables in queries/extensions Date: Tue, 9 Jul 2024 17:34:16 +0100 Message-ID: <20240709163425.58276-4-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/v3d/v3d_submit.c | 103 ++++++++++++++++--------------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 5c71e9adfc65..f99cd61a3e65 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -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 *qinfo = &job->timestamp_query; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -471,10 +472,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) + qinfo->queries = kvmalloc_array(timestamp.count, + sizeof(struct v3d_timestamp_query), + GFP_KERNEL); + if (!qinfo->queries) return -ENOMEM; offsets = u64_to_user_ptr(timestamp.offsets); @@ -484,20 +485,20 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, u32 offset, sync; if (get_user(offset, offsets++)) { - kvfree(job->timestamp_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->timestamp_query.queries[i].offset = offset; + qinfo->queries[i].offset = offset; if (get_user(sync, syncs++)) { - kvfree(job->timestamp_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); } - job->timestamp_query.count = timestamp.count; + qinfo->count = timestamp.count; return 0; } @@ -509,6 +510,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 *qinfo = &job->timestamp_query; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -525,10 +527,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) + qinfo->queries = kvmalloc_array(reset.count, + sizeof(struct v3d_timestamp_query), + GFP_KERNEL); + if (!qinfo->queries) return -ENOMEM; syncs = u64_to_user_ptr(reset.syncs); @@ -536,16 +538,16 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, for (int i = 0; i < reset.count; i++) { u32 sync; - job->timestamp_query.queries[i].offset = reset.offset + 8 * i; + qinfo->queries[i].offset = reset.offset + 8 * i; if (get_user(sync, syncs++)) { - kvfree(job->timestamp_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); } - job->timestamp_query.count = reset.count; + qinfo->count = reset.count; return 0; } @@ -558,6 +560,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 *qinfo = &job->timestamp_query; int i; if (!job) { @@ -578,10 +581,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) + qinfo->queries = kvmalloc_array(copy.count, + sizeof(struct v3d_timestamp_query), + GFP_KERNEL); + if (!qinfo->queries) return -ENOMEM; offsets = u64_to_user_ptr(copy.offsets); @@ -591,20 +594,20 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, u32 offset, sync; if (get_user(offset, offsets++)) { - kvfree(job->timestamp_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->timestamp_query.queries[i].offset = offset; + qinfo->queries[i].offset = offset; if (get_user(sync, syncs++)) { - kvfree(job->timestamp_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->timestamp_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); } - job->timestamp_query.count = copy.count; + qinfo->count = copy.count; job->copy.do_64bit = copy.do_64bit; job->copy.do_partial = copy.do_partial; @@ -623,6 +626,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, u32 __user *syncs; u64 __user *kperfmon_ids; struct drm_v3d_reset_performance_query reset; + struct v3d_performance_query_info *qinfo = &job->performance_query; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -642,10 +646,10 @@ 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) + qinfo->queries = kvmalloc_array(reset.count, + sizeof(struct v3d_performance_query), + GFP_KERNEL); + if (!qinfo->queries) return -ENOMEM; syncs = u64_to_user_ptr(reset.syncs); @@ -658,14 +662,14 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, u32 id; if (get_user(sync, syncs++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); if (get_user(ids, kperfmon_ids++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } @@ -673,15 +677,15 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, for (int j = 0; j < reset.nperfmons; j++) { if (get_user(id, ids_pointer++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->performance_query.queries[i].kperfmon_ids[j] = id; + qinfo->queries[i].kperfmon_ids[j] = id; } } - job->performance_query.count = reset.count; - job->performance_query.nperfmons = reset.nperfmons; + qinfo->count = reset.count; + qinfo->nperfmons = reset.nperfmons; return 0; } @@ -694,6 +698,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, u32 __user *syncs; u64 __user *kperfmon_ids; struct drm_v3d_copy_performance_query copy; + struct v3d_performance_query_info *qinfo = &job->performance_query; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -716,10 +721,10 @@ 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) + qinfo->queries = kvmalloc_array(copy.count, + sizeof(struct v3d_performance_query), + GFP_KERNEL); + if (!qinfo->queries) return -ENOMEM; syncs = u64_to_user_ptr(copy.syncs); @@ -732,14 +737,14 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, u32 id; if (get_user(sync, syncs++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->performance_query.queries[i].syncobj = drm_syncobj_find(file_priv, sync); + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); if (get_user(ids, kperfmon_ids++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } @@ -747,16 +752,16 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, for (int j = 0; j < copy.nperfmons; j++) { if (get_user(id, ids_pointer++)) { - kvfree(job->performance_query.queries); + kvfree(qinfo->queries); return -EFAULT; } - job->performance_query.queries[i].kperfmon_ids[j] = id; + qinfo->queries[i].kperfmon_ids[j] = id; } } - job->performance_query.count = copy.count; - job->performance_query.nperfmons = copy.nperfmons; - job->performance_query.ncounters = copy.ncounters; + qinfo->count = copy.count; + qinfo->nperfmons = copy.nperfmons; + qinfo->ncounters = copy.ncounters; job->copy.do_64bit = copy.do_64bit; job->copy.do_partial = copy.do_partial; From patchwork Tue Jul 9 16:34:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728336 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2938EC3DA47 for ; Tue, 9 Jul 2024 16:34:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B694010E5F9; Tue, 9 Jul 2024 16:34:38 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="Mb23h5hN"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id A4BAC10E5F5 for ; Tue, 9 Jul 2024 16:34:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=kItrKcxauvdbMdUqNlmo/v9tJIZtp4Sze/1WHrZPF3c=; b=Mb23h5hNX95daNjTjnkWs74GnK h21Tj/uy4sEy0X31jwVA6U+Y6ZrhdmNuTCgO1p6njUxRNFIyHlqt7DvorCguAO9nEhVuqOhQj4Res gC1wakWkeYdqRlUCKlf9GqmMXJ0smcYiwsJWkObCOrfLtBJsuYwjXVRhyAhcHEolusXx+3DgGpmZB PWkeYg2yIBOJ2fzpN2y5C8tN/DWSIAFPe55+C6BK/f902VjBTrcwTIF2ooVwKZDcggl37Ghe/3gif qJJX770NbqvV/4c+p+DrYcOQE0Ts/AG8eOIw1/l9mmMoVjPXA/zwLd/HHI1nXIFNBQaOv+J17EIgB LC1py2vQ==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnV-00CsoE-W1; Tue, 09 Jul 2024 18:34:34 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 04/12] drm/v3d: Align data types of internal and uapi counts Date: Tue, 9 Jul 2024 17:34:17 +0100 Message-ID: <20240709163425.58276-5-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin In the timestamp and performance extensions userspace type for counts is u32 so lets use unsigned in the kernel too. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/v3d/v3d_submit.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index f99cd61a3e65..c960bc6ca32d 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -453,6 +453,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 *qinfo = &job->timestamp_query; + unsigned int i; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -481,7 +482,7 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, offsets = u64_to_user_ptr(timestamp.offsets); syncs = u64_to_user_ptr(timestamp.syncs); - for (int i = 0; i < timestamp.count; i++) { + for (i = 0; i < timestamp.count; i++) { u32 offset, sync; if (get_user(offset, offsets++)) { @@ -511,6 +512,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 *qinfo = &job->timestamp_query; + unsigned int i; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -535,7 +537,7 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, syncs = u64_to_user_ptr(reset.syncs); - for (int i = 0; i < reset.count; i++) { + for (i = 0; i < reset.count; i++) { u32 sync; qinfo->queries[i].offset = reset.offset + 8 * i; @@ -561,7 +563,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 *qinfo = &job->timestamp_query; - int i; + unsigned int i; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -627,6 +629,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, u64 __user *kperfmon_ids; struct drm_v3d_reset_performance_query reset; struct v3d_performance_query_info *qinfo = &job->performance_query; + unsigned int i, j; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -655,7 +658,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, syncs = u64_to_user_ptr(reset.syncs); kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids); - for (int i = 0; i < reset.count; i++) { + for (i = 0; i < reset.count; i++) { u32 sync; u64 ids; u32 __user *ids_pointer; @@ -675,7 +678,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, ids_pointer = u64_to_user_ptr(ids); - for (int j = 0; j < reset.nperfmons; j++) { + for (j = 0; j < reset.nperfmons; j++) { if (get_user(id, ids_pointer++)) { kvfree(qinfo->queries); return -EFAULT; @@ -699,6 +702,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, u64 __user *kperfmon_ids; struct drm_v3d_copy_performance_query copy; struct v3d_performance_query_info *qinfo = &job->performance_query; + unsigned int i, j; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -730,7 +734,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, syncs = u64_to_user_ptr(copy.syncs); kperfmon_ids = u64_to_user_ptr(copy.kperfmon_ids); - for (int i = 0; i < copy.count; i++) { + for (i = 0; i < copy.count; i++) { u32 sync; u64 ids; u32 __user *ids_pointer; @@ -750,7 +754,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, ids_pointer = u64_to_user_ptr(ids); - for (int j = 0; j < copy.nperfmons; j++) { + for (j = 0; j < copy.nperfmons; j++) { if (get_user(id, ids_pointer++)) { kvfree(qinfo->queries); return -EFAULT; From patchwork Tue Jul 9 16:34:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728339 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id EB6E9C3DA4A for ; Tue, 9 Jul 2024 16:34:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id B608710E5FE; Tue, 9 Jul 2024 16:34:39 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="QJObrmnV"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 50EFB10E5F7 for ; Tue, 9 Jul 2024 16:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=83M8A3iugGdLdsh/4jkJjGQJeaIdByksfx/Wh7FQ2xE=; b=QJObrmnV+9HJoDJLWBsRMyMdYz Z/pUmYP1aqwxE15H389vODx3GId93+bbWmY9q+9bjtdETOUiSpL5fRC6gdN8srFcJkcXy36/b8K96 1xbwP8bkghZwlDGTzAXg0yUgEvlgTgu4N6I8llxbpHWmnD9Cfr6R9MhLMslT86gb9fEh0WjBpzdEh 8XO69bI3IurhYNgU2TJYb988TSrRxFWtzNpCFa0goXF2bLiwk3HYlQSF/+iDhSaREQIQl5gwbVKX8 eTk7PrB8sGlx32CVx7Bdi4HJ7KECMfh6wWgEV2AlsW1VF+iNZpt6dts8qp7mWiYgCMcoEeFgoxiEw LlMhJezg==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnW-00CsoJ-L1; Tue, 09 Jul 2024 18:34:34 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin , Iago Toral Quiroga , stable@vger.kernel.org Subject: [PATCH 05/12] drm/v3d: Fix potential memory leak in the timestamp extension Date: Tue, 9 Jul 2024 17:34:18 +0100 Message-ID: <20240709163425.58276-6-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin Fixes: 9ba0ff3e083f ("drm/v3d: Create a CPU job extension for the timestamp query job") Cc: Maíra Canal Cc: Iago Toral Quiroga Cc: # 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 | 35 +++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h index 099b962bdfde..95651c3c926f 100644 --- a/drivers/gpu/drm/v3d/v3d_drv.h +++ b/drivers/gpu/drm/v3d/v3d_drv.h @@ -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 *qinfo, + 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); diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c index 03df37a3acf5..e45d3ddc6f82 100644 --- a/drivers/gpu/drm/v3d/v3d_sched.c +++ b/drivers/gpu/drm/v3d/v3d_sched.c @@ -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 *qinfo, + unsigned int count) +{ + if (qinfo->queries) { + unsigned int i; + + for (i = 0; i < count; i++) + drm_syncobj_put(qinfo->queries[i].syncobj); + + kvfree(qinfo->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++) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index c960bc6ca32d..0f1c900c7d35 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -454,6 +454,7 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, struct drm_v3d_timestamp_query timestamp; struct v3d_timestamp_query_info *qinfo = &job->timestamp_query; unsigned int i; + int err; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -486,15 +487,15 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, u32 offset, sync; if (get_user(offset, offsets++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].offset = offset; if (get_user(sync, syncs++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); @@ -502,6 +503,10 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, qinfo->count = timestamp.count; return 0; + +error: + __v3d_timestamp_query_info_free(qinfo, i); + return err; } static int @@ -513,6 +518,7 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, struct drm_v3d_reset_timestamp_query reset; struct v3d_timestamp_query_info *qinfo = &job->timestamp_query; unsigned int i; + int err; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -543,8 +549,8 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, qinfo->queries[i].offset = reset.offset + 8 * i; if (get_user(sync, syncs++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); @@ -552,6 +558,10 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, qinfo->count = reset.count; return 0; + +error: + __v3d_timestamp_query_info_free(qinfo, i); + return err; } /* Get data for the copy timestamp query results job submission. */ @@ -564,6 +574,7 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, struct drm_v3d_copy_timestamp_query copy; struct v3d_timestamp_query_info *qinfo = &job->timestamp_query; unsigned int i; + int err; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -596,15 +607,15 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, u32 offset, sync; if (get_user(offset, offsets++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].offset = offset; if (get_user(sync, syncs++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); @@ -618,6 +629,10 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, job->copy.stride = copy.stride; return 0; + +error: + __v3d_timestamp_query_info_free(qinfo, i); + return err; } static int From patchwork Tue Jul 9 16:34:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728338 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 24C5DC3DA41 for ; Tue, 9 Jul 2024 16:34:44 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2E7CA10E5FD; Tue, 9 Jul 2024 16:34:39 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="jTnfPpR+"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id F283510E5F7 for ; Tue, 9 Jul 2024 16:34:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=L1YGG/6nLKQNVCT4mNcLHZccHgt/bEXBUIOKvL2HJvM=; b=jTnfPpR+mDtkl3Slq17DIdAyd8 eCy+JfZpbOUxI4Cozs/4Fhp6Uesb68wA+r5E+NfBpD9gSzpUtl2bP74/ZgpeM7rRKcj+BFr+fRuuO Npy+SuscXz0woNC841NnXP8m0p9qa14wblydrerqwJ0mxfYAeAdBcJHjJjfe/FgM1tSVxkpKgajIm ojSe1Lsu38NxaYoj/UuH2Xyp4QkVb525o4kQEqx142g+Z/Qksr8lTUIH9nY5L063BxmiN3odc/V41 F8I+8Pbc/f00cc55rWzIhkZBBtKmGM16i6Xg5yWS59fyWISXZq6rKzYRtR2flAdEoCRM3WqPMc3Nl 13z+6SHA==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnX-00CsoQ-A2; Tue, 09 Jul 2024 18:34:35 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin , Iago Toral Quiroga , stable@vger.kernel.org Subject: [PATCH 06/12] drm/v3d: Fix potential memory leak in the performance extension Date: Tue, 9 Jul 2024 17:34:19 +0100 Message-ID: <20240709163425.58276-7-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin Fixes: bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job" Cc: Maíra Canal Cc: Iago Toral Quiroga Cc: # 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 | 42 ++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h index 95651c3c926f..38c80168da51 100644 --- a/drivers/gpu/drm/v3d/v3d_drv.h +++ b/drivers/gpu/drm/v3d/v3d_drv.h @@ -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 *qinfo, unsigned int count); +void __v3d_performance_query_info_free(struct v3d_performance_query_info *qinfo, + 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); diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c index e45d3ddc6f82..173801aa54ee 100644 --- a/drivers/gpu/drm/v3d/v3d_sched.c +++ b/drivers/gpu/drm/v3d/v3d_sched.c @@ -87,20 +87,30 @@ __v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *qinfo, } } +void +__v3d_performance_query_info_free(struct v3d_performance_query_info *qinfo, + unsigned int count) +{ + if (qinfo->queries) { + unsigned int i; + + for (i = 0; i < count; i++) + drm_syncobj_put(qinfo->queries[i].syncobj); + + kvfree(qinfo->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); } diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 0f1c900c7d35..81afcfccc6bb 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -645,6 +645,7 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, struct drm_v3d_reset_performance_query reset; struct v3d_performance_query_info *qinfo = &job->performance_query; unsigned int i, j; + int err; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -680,32 +681,36 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, u32 id; if (get_user(sync, syncs++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } - qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (get_user(ids, kperfmon_ids++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } ids_pointer = u64_to_user_ptr(ids); for (j = 0; j < reset.nperfmons; j++) { if (get_user(id, ids_pointer++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].kperfmon_ids[j] = id; } + + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); } qinfo->count = reset.count; qinfo->nperfmons = reset.nperfmons; return 0; + +error: + __v3d_performance_query_info_free(qinfo, i); + return err; } static int @@ -718,6 +723,7 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, struct drm_v3d_copy_performance_query copy; struct v3d_performance_query_info *qinfo = &job->performance_query; unsigned int i, j; + int err; if (!job) { DRM_DEBUG("CPU job extension was attached to a GPU job.\n"); @@ -756,27 +762,27 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, u32 id; if (get_user(sync, syncs++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } - qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (get_user(ids, kperfmon_ids++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } ids_pointer = u64_to_user_ptr(ids); for (j = 0; j < copy.nperfmons; j++) { if (get_user(id, ids_pointer++)) { - kvfree(qinfo->queries); - return -EFAULT; + err = -EFAULT; + goto error; } qinfo->queries[i].kperfmon_ids[j] = id; } + + qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); } qinfo->count = copy.count; qinfo->nperfmons = copy.nperfmons; @@ -789,6 +795,10 @@ 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(qinfo, i); + return err; } /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data From patchwork Tue Jul 9 16:34:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728341 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8833EC3DA4C for ; Tue, 9 Jul 2024 16:34:46 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CB11210E5FF; Tue, 9 Jul 2024 16:34:40 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="PF+MYIye"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id A93CD10E5F7 for ; Tue, 9 Jul 2024 16:34:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=koLDN1PR3znJTaiuCGWTae1dfYUoXjyFBExlwnb98oc=; b=PF+MYIye1SgGQP2nPpvf6ldq7D Y0Tlc5gIqFpWZW/W8+vrMDzEigYAIApGmronRc5RgFKlSHntv6ObK4a3tHnF2IKYeV4cAqfQI5J8Z O6d9X/vEjkDhEEw7TgB3h5skFL89GyGoj7gKgu5P4sg/czH//xyE8nl/7RBRo5mpCI4RO1Hlw+BIF 4lmS8EzgezvhdK5HXzH8RZsIuj1GEyFsm4m3QP5ddmHfhal0apoD7MTYr987djnrRLkjVbvzBOIcU UzG/xRhESqzi8L+b9oMvVIeHFEg+5JDnEGSr2bNQU1/pQ84xOSnwbYxAtMtv6SfgNp94MluSQ/wCf xqu022cQ==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnX-00CsoV-WC; Tue, 09 Jul 2024 18:34:36 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin , Iago Toral Quiroga , stable@vger.kernel.org Subject: [PATCH 07/12] drm/v3d: Validate passed in drm syncobj handles in the timestamp extension Date: Tue, 9 Jul 2024 17:34:20 +0100 Message-ID: <20240709163425.58276-8-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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 successfuly or otherwise fail the extension by jumping into the existing unwind. Signed-off-by: Tvrtko Ursulin Fixes: 9ba0ff3e083f ("drm/v3d: Create a CPU job extension for the timestamp query job") Cc: Maíra Canal Cc: Iago Toral Quiroga Cc: # v6.8+ --- drivers/gpu/drm/v3d/v3d_submit.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 81afcfccc6bb..a408db3d3e32 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -499,6 +499,10 @@ v3d_get_cpu_timestamp_query_params(struct drm_file *file_priv, } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); + if (!qinfo->queries[i].syncobj) { + err = -ENOENT; + goto error; + } } qinfo->count = timestamp.count; @@ -554,6 +558,10 @@ v3d_get_cpu_reset_timestamp_params(struct drm_file *file_priv, } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); + if (!qinfo->queries[i].syncobj) { + err = -ENOENT; + goto error; + } } qinfo->count = reset.count; @@ -619,6 +627,10 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); + if (!qinfo->queries[i].syncobj) { + err = -ENOENT; + goto error; + } } qinfo->count = copy.count; From patchwork Tue Jul 9 16:34:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728340 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AFD8EC2BD09 for ; Tue, 9 Jul 2024 16:34:45 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2250110E5F7; Tue, 9 Jul 2024 16:34:40 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="OMHkSiwN"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 5C9CC10E5F7 for ; Tue, 9 Jul 2024 16:34:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=lUWT7OEVdaa9leDIsW4NotJCuqqFsJk3YdxE80TeHrY=; b=OMHkSiwN/08TIkr0ssspvKC10j F132sjirtOFQcAK/2B6eKancnRT2mDA+alvN/+bqRNAXGIwksiuhY62jLSmD5EQt41TwYdOx3AMLu Oek4P/nf1PMiCKOGwc8Kiq7hu1v65HeFuZVXhaNYDG87dpwq0ZXuutay9YXDhOXy+PeTVZa7HO//s I3T7qP4BMSk9vQsvbs2yD+PUqHIynDRJzJjhYa0tFgaAOtee6pZpsiO/1t/F9jG7r7wv29sMIz9cG UgfnAumAzaV2A2TT3BXdTnVk6n+3zhf4Xws3Y9Bxmhl+j+JB185IfbbsKwLcsL/G9euYHBq+oRYt2 Sy2IOLpQ==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnY-00Csoa-L8; Tue, 09 Jul 2024 18:34:36 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin , Iago Toral Quiroga , stable@vger.kernel.org Subject: [PATCH 08/12] drm/v3d: Validate passed in drm syncobj handles in the performance extension Date: Tue, 9 Jul 2024 17:34:21 +0100 Message-ID: <20240709163425.58276-9-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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 successfuly or otherwise fail the extension by jumping into the existing unwind. Signed-off-by: Tvrtko Ursulin Fixes: bae7cb5d6800 ("drm/v3d: Create a CPU job extension for the reset performance query job" Cc: Maíra Canal Cc: Iago Toral Quiroga Cc: # v6.8+ --- drivers/gpu/drm/v3d/v3d_submit.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index a408db3d3e32..2c4bb39c9ac6 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -714,6 +714,10 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); + if (!qinfo->queries[i].syncobj) { + err = -ENOENT; + goto error; + } } qinfo->count = reset.count; qinfo->nperfmons = reset.nperfmons; @@ -795,6 +799,10 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, } qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); + if (!qinfo->queries[i].syncobj) { + err = -ENOENT; + goto error; + } } qinfo->count = copy.count; qinfo->nperfmons = copy.nperfmons; From patchwork Tue Jul 9 16:34:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728343 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 06FB4C3DA47 for ; Tue, 9 Jul 2024 16:34:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 900A510E602; Tue, 9 Jul 2024 16:34:45 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="YvRTJ8xu"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0820C10E5FD for ; Tue, 9 Jul 2024 16:34:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=8JJs3hMMr2c4tDLC3NSsAjZ0ofIvD/DofTN375uFnhE=; b=YvRTJ8xuUKzAAxnecT9A4MiL0D M752cABN9t6maeWvk9VH4EacT5zOY67SFMq50AtcEc0OZoieLK+nuCPot1i6FAK2SyLsvc7mSCuth lTu5Ofv9gxkCHxA6uclqxP0Ftqq/D0kHxpi/QUfCVDgq1um5gzX6GGwOIQ2pGAFpzZzSXdGspYi7g 8eni0KZ5WX6cazmtS11WYPjI/xt3ChRBLEoSaVpVnB6JFCfDsd27NSbhzP/fpCtxJupywQ16kXElT nJyEEo+Q3+81RJLtwWWn5GH7CfQmZZqS/TKeQKEB6AmW5hAZwUxgtHc8C1S+OKUwyHE3Vpch3b41j qPGtz1/A==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnZ-00Csog-AV; Tue, 09 Jul 2024 18:34:37 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 09/12] drm/v3d: Move part of copying of reset/copy performance extension to a helper Date: Tue, 9 Jul 2024 17:34:22 +0100 Message-ID: <20240709163425.58276-10-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin The loop which looks up the syncobj and copies the kperfmon ids is identical so lets move it to a helper. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/v3d/v3d_submit.c | 148 +++++++++++++------------------ 1 file changed, 64 insertions(+), 84 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index 2c4bb39c9ac6..a2e55ba8222b 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -647,16 +647,65 @@ v3d_get_cpu_copy_query_results_params(struct drm_file *file_priv, return err; } +static int +copy_query_info(struct v3d_performance_query_info *qinfo, + unsigned int count, + unsigned int nperfmons, + u32 __user *syncs, + u64 __user *kperfmon_ids, + struct drm_file *fpriv) +{ + unsigned int i, j; + int err; + + for (i = 0; i < count; i++) { + struct v3d_performance_query *query = &qinfo->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(fpriv, sync); + if (!query->syncobj) { + err = -ENOENT; + goto error; + } + } + + return 0; + +error: + __v3d_performance_query_info_free(qinfo, 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; struct v3d_performance_query_info *qinfo = &job->performance_query; - unsigned int i, j; int err; if (!job) { @@ -683,50 +732,17 @@ v3d_get_cpu_reset_performance_params(struct drm_file *file_priv, if (!qinfo->queries) return -ENOMEM; - syncs = u64_to_user_ptr(reset.syncs); - kperfmon_ids = u64_to_user_ptr(reset.kperfmon_ids); + err = copy_query_info(qinfo, 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 (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 < reset.nperfmons; j++) { - if (get_user(id, ids_pointer++)) { - err = -EFAULT; - goto error; - } - - qinfo->queries[i].kperfmon_ids[j] = id; - } - - qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (!qinfo->queries[i].syncobj) { - err = -ENOENT; - goto error; - } - } qinfo->count = reset.count; qinfo->nperfmons = reset.nperfmons; return 0; - -error: - __v3d_performance_query_info_free(qinfo, i); - return err; } static int @@ -734,11 +750,8 @@ 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; struct v3d_performance_query_info *qinfo = &job->performance_query; - unsigned int i, j; int err; if (!job) { @@ -768,42 +781,13 @@ v3d_get_cpu_copy_performance_query_params(struct drm_file *file_priv, if (!qinfo->queries) return -ENOMEM; - syncs = u64_to_user_ptr(copy.syncs); - kperfmon_ids = u64_to_user_ptr(copy.kperfmon_ids); + err = copy_query_info(qinfo, 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 (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 < copy.nperfmons; j++) { - if (get_user(id, ids_pointer++)) { - err = -EFAULT; - goto error; - } - - qinfo->queries[i].kperfmon_ids[j] = id; - } - - qinfo->queries[i].syncobj = drm_syncobj_find(file_priv, sync); - if (!qinfo->queries[i].syncobj) { - err = -ENOENT; - goto error; - } - } qinfo->count = copy.count; qinfo->nperfmons = copy.nperfmons; qinfo->ncounters = copy.ncounters; @@ -815,10 +799,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(qinfo, i); - return err; } /* Whenever userspace sets ioctl extensions, v3d_get_extensions parses data From patchwork Tue Jul 9 16:34:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728342 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46878C3DA4B for ; Tue, 9 Jul 2024 16:34:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CDAB710E601; Tue, 9 Jul 2024 16:34:40 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="CY2NcBEq"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id B0A4310E5F7 for ; Tue, 9 Jul 2024 16:34:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=OZogu4MvFKWN7s6sNFTqleLYLLYBx/yLoC5rBaIOQXA=; b=CY2NcBEqppxUw58uTbujJxbq5Q XuxYWBOx+rsf7gSucBwtel2vp3xuRlNgvwNUTBVJ5N0fvNplV0AqkgzybBpvk2KJfKUrAXO792PpX 6O8QXIMqbpiE7PqI2NKDZeQkWLStFXGGWo+28LN2c0ioW75PTvUMBr0s2TU8mYPglbYQkmU5YGafU 0IJlJZ0AiqHUEPCWVxB9eETaMhED7+cA7B52zkGwZzkAgzONKoyfbDUpbjuiLGkidN0O+8z2Yx75D bufCpbFPkAgRfMWkwIXac8FUanoR50exHWhzpdBoyRx/2Ka95mUaiygw98AwmlvRGEB1WD/QVNPWO u0of/bLg==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDna-00Cson-0t; Tue, 09 Jul 2024 18:34:38 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 10/12] drm/v3d: Size the kperfmon_ids array at runtime Date: Tue, 9 Jul 2024 17:34:23 +0100 Message-ID: <20240709163425.58276-11-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin --- 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(-) diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h index 38c80168da51..00fe5d993175 100644 --- a/drivers/gpu/drm/v3d/v3d_drv.h +++ b/drivers/gpu/drm/v3d/v3d_drv.h @@ -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; diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c index 173801aa54ee..fc8730264386 100644 --- a/drivers/gpu/drm/v3d/v3d_sched.c +++ b/drivers/gpu/drm/v3d/v3d_sched.c @@ -94,8 +94,10 @@ __v3d_performance_query_info_free(struct v3d_performance_query_info *qinfo, if (qinfo->queries) { unsigned int i; - for (i = 0; i < count; i++) + for (i = 0; i < count; i++) { drm_syncobj_put(qinfo->queries[i].syncobj); + kvfree(qinfo->queries[i].kperfmon_ids); + } kvfree(qinfo->queries); } diff --git a/drivers/gpu/drm/v3d/v3d_submit.c b/drivers/gpu/drm/v3d/v3d_submit.c index a2e55ba8222b..e1a7622a43f9 100644 --- a/drivers/gpu/drm/v3d/v3d_submit.c +++ b/drivers/gpu/drm/v3d/v3d_submit.c @@ -674,10 +674,20 @@ copy_query_info(struct v3d_performance_query_info *qinfo, 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; } @@ -687,6 +697,7 @@ copy_query_info(struct v3d_performance_query_info *qinfo, query->syncobj = drm_syncobj_find(fpriv, sync); if (!query->syncobj) { + kvfree(query->kperfmon_ids); err = -ENOENT; goto error; } @@ -721,9 +732,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; qinfo->queries = kvmalloc_array(reset.count, @@ -770,9 +778,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; qinfo->queries = kvmalloc_array(copy.count, From patchwork Tue Jul 9 16:34:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728344 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E701FC3DA49 for ; Tue, 9 Jul 2024 16:34:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CC69310E604; Tue, 9 Jul 2024 16:34:45 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="QU3tRvfO"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6212510E5FF for ; Tue, 9 Jul 2024 16:34:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=xBW7XR4ZrtbBobrjCg8ZGDl5Mwm78Q2a3rHXKS5vq2A=; b=QU3tRvfOi7rQ8QnV7EWOvQNsW9 rrzOFDJZI409CL2zecvJxzk0VfatJVpTdCWBMVm4/ExjCuRJuRwCVQSGiMJOW3F14SNFLJnbJVSDB +TPlcv0vY6goVDnUHe32FB7eiaeTQmiVTxpUU4R86IT6XuXXeqtgqMs81UiYp05plNL/BBE5JOcLI CNj8sXHcgmzdntwJSe8RFk4nkJRf4FoPHSG7nh2DAoaq/av16X5FNBHfGQ93aVSxebxFVwz0tx582 dA/BbLdigZVURIH6ll06j/8BOrL8xjBtN9Jysroz07Ob3xEXZ9IXjKVLEnzstQH4h0GO7JVfT+Dsd Dc9Y7K9w==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDna-00Csot-NG; Tue, 09 Jul 2024 18:34:38 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 11/12] drm/v3d: Do not use intermediate storage when copying performance query results Date: Tue, 9 Jul 2024 17:34:24 +0100 Message-ID: <20240709163425.58276-12-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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. Signed-off-by: Tvrtko Ursulin Reviewed-by: Iago Toral Quiroga --- drivers/gpu/drm/v3d/v3d_sched.c | 60 ++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/drivers/gpu/drm/v3d/v3d_sched.c b/drivers/gpu/drm/v3d/v3d_sched.c index fc8730264386..77f795e38fad 100644 --- a/drivers/gpu/drm/v3d/v3d_sched.c +++ b/drivers/gpu/drm/v3d/v3d_sched.c @@ -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,23 @@ 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_dev *v3d = job->base.v3d; - struct v3d_perfmon *perfmon; - u64 counter_values[V3D_MAX_COUNTERS]; + unsigned int i, j, offset; - for (int i = 0; i < performance_query->nperfmons; i++) { - perfmon = v3d_perfmon_find(v3d_priv, - performance_query->queries[query].kperfmon_ids[i]); + for (i = 0, offset = 0; + i < performance_query->nperfmons; + i++, offset += DRM_V3D_MAX_PERF_COUNTERS) { + struct v3d_performance_query *q = + &performance_query->queries[query]; + struct v3d_perfmon *perfmon; + + perfmon = v3d_perfmon_find(v3d_priv, q->kperfmon_ids[i]); if (!perfmon) { DRM_DEBUG("Failed to find perfmon."); continue; @@ -524,14 +534,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 From patchwork Tue Jul 9 16:34:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 13728345 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C1F63C2BD09 for ; Tue, 9 Jul 2024 16:34:49 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id DB7F910E605; Tue, 9 Jul 2024 16:34:48 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=igalia.com header.i=@igalia.com header.b="UhhiPuYM"; dkim-atps=neutral Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0E99110E602 for ; Tue, 9 Jul 2024 16:34:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=l/dXv6tiFHIDsiK5FlFzp9gPWIuOg2Aj/F8rF0rd7CQ=; b=UhhiPuYMlNiGRZOzHN0nNad//h J/llxKeT77APXYCaz77tuTq77LCBhMvqj3LQlWhVcXwGvpR7gKLSIACFIY6URHuGxynqLyW8uOD/8 opTEfL75iZ5qmL8zxs8qFLdrz/Z0PzPdvUPHMFATYLmPXgVPTHZgtukwYb45RBMWNQEtnR1l2AqJJ LRqvPZjxD0SCF40szFh5PmqXam0jOjxr3KQoX/hrSC6uiUVEXep8Nw5SxLccoc+LuYuqx5xwXd1VT ozaxZjjXKvOmDKJGxrU2y5FNQRCkogevv4QoTbptinZIFbfImpWNIY6o+2cJqQp4w87p2rlK+z8KE IcN/zCFA==; Received: from [84.69.19.168] (helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1sRDnb-00Csp0-Co; Tue, 09 Jul 2024 18:34:39 +0200 From: Tvrtko Ursulin To: dri-devel@lists.freedesktop.org Cc: =?utf-8?q?Ma=C3=ADra_Canal?= , kernel-dev@igalia.com, Tvrtko Ursulin Subject: [PATCH 12/12] drm/v3d: Move perfmon init completely into own unit Date: Tue, 9 Jul 2024 17:34:25 +0100 Message-ID: <20240709163425.58276-13-tursulin@igalia.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240709163425.58276-1-tursulin@igalia.com> References: <20240709163425.58276-1-tursulin@igalia.com> MIME-Version: 1.0 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" From: Tvrtko Ursulin 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 792d16b5375d ("drm/v3d: Move perfmon init completely into own unit"). Signed-off-by: Tvrtko Ursulin References: 792d16b5375d ("drm/v3d: Move perfmon init completely into own unit") --- 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(-) diff --git a/drivers/gpu/drm/v3d/v3d_drv.c b/drivers/gpu/drm/v3d/v3d_drv.c index a47f00b443d3..491c638a4d74 100644 --- a/drivers/gpu/drm/v3d/v3d_drv.c +++ b/drivers/gpu/drm/v3d/v3d_drv.c @@ -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)) { diff --git a/drivers/gpu/drm/v3d/v3d_drv.h b/drivers/gpu/drm/v3d/v3d_drv.h index 00fe5d993175..6d2d34cd135c 100644 --- a/drivers/gpu/drm/v3d/v3d_drv.h +++ b/drivers/gpu/drm/v3d/v3d_drv.h @@ -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); diff --git a/drivers/gpu/drm/v3d/v3d_perfmon.c b/drivers/gpu/drm/v3d/v3d_perfmon.c index b7d0b02e1a95..cd7f1eedf17f 100644 --- a/drivers/gpu/drm/v3d/v3d_perfmon.c +++ b/drivers/gpu/drm/v3d/v3d_perfmon.c @@ -195,6 +195,23 @@ static const struct v3d_perf_counter_desc v3d_v71_performance_counters[] = { {"QPU", "QPU-stalls-other", "[QPU] Stalled qcycles waiting for any other reason (vary/W/Z)"}, }; +void v3d_perfmon_init(struct v3d_dev *v3d) +{ + const struct v3d_perf_counter_desc *counters = NULL; + unsigned int max = 0; + + if (v3d->ver >= 71) { + counters = v3d_v71_performance_counters; + max = ARRAY_SIZE(v3d_v71_performance_counters); + } else if (v3d->ver >= 42) { + counters = v3d_v42_performance_counters; + max = ARRAY_SIZE(v3d_v42_performance_counters); + } + + v3d->perfmon_info.max_counters = max; + v3d->perfmon_info.counters = counters; +} + void v3d_perfmon_get(struct v3d_perfmon *perfmon) { if (perfmon) @@ -321,7 +338,7 @@ int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data, /* Make sure all counters are valid. */ for (i = 0; i < req->ncounters; i++) { - if (req->counters[i] >= v3d->max_counters) + if (req->counters[i] >= v3d->perfmon_info.max_counters) return -EINVAL; } @@ -416,26 +433,15 @@ int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data, return -EINVAL; } - /* Make sure that the counter ID is valid */ - if (req->counter >= v3d->max_counters) - return -EINVAL; - - BUILD_BUG_ON(ARRAY_SIZE(v3d_v42_performance_counters) != - V3D_V42_NUM_PERFCOUNTERS); - BUILD_BUG_ON(ARRAY_SIZE(v3d_v71_performance_counters) != - V3D_V71_NUM_PERFCOUNTERS); - BUILD_BUG_ON(V3D_MAX_COUNTERS < V3D_V42_NUM_PERFCOUNTERS); - BUILD_BUG_ON(V3D_MAX_COUNTERS < V3D_V71_NUM_PERFCOUNTERS); - BUILD_BUG_ON((V3D_MAX_COUNTERS != V3D_V42_NUM_PERFCOUNTERS) && - (V3D_MAX_COUNTERS != V3D_V71_NUM_PERFCOUNTERS)); - - if (v3d->ver >= 71) - counter = &v3d_v71_performance_counters[req->counter]; - else if (v3d->ver >= 42) - counter = &v3d_v42_performance_counters[req->counter]; - else + if (!v3d->perfmon_info.max_counters) return -EOPNOTSUPP; + /* Make sure that the counter ID is valid */ + if (req->counter >= v3d->perfmon_info.max_counters) + return -EINVAL; + + counter = &v3d->perfmon_info.counters[req->counter]; + strscpy(req->name, counter->name, sizeof(req->name)); strscpy(req->category, counter->category, sizeof(req->category)); strscpy(req->description, counter->description, sizeof(req->description)); diff --git a/drivers/gpu/drm/v3d/v3d_performance_counters.h b/drivers/gpu/drm/v3d/v3d_performance_counters.h index 131b2909522a..d919a2fc9449 100644 --- a/drivers/gpu/drm/v3d/v3d_performance_counters.h +++ b/drivers/gpu/drm/v3d/v3d_performance_counters.h @@ -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