From patchwork Tue Dec 26 17:33:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Markus Elfring X-Patchwork-Id: 183326 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp1001942dyb; Tue, 26 Dec 2023 09:34:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IG7xmagPtd9h8jf1ITersfTBSERwBxNQpKEIuBPYg6xwmjkBZw7z38x/s73CZsb2yJ+eASx X-Received: by 2002:a17:90b:3148:b0:28b:8e2c:e5d1 with SMTP id ip8-20020a17090b314800b0028b8e2ce5d1mr3702695pjb.45.1703612092473; Tue, 26 Dec 2023 09:34:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703612092; cv=none; d=google.com; s=arc-20160816; b=fNUnovDIkz+W6LpVbt+ToQtB3eycYQyGEBw662ZFVeVCIx3vibOlCVj0IF1/9k9h60 pEL6GV3CDJyvaTwo/qnHUrgBO19DpL9IiYrQP7v5jOvnfxw2IANKheb8kT0tMuUYkukP /RI0AqIR/3qlcyYyYFYwnWSZHO38hUVqC/qqu9shu4BEtf0lzexM3PZT8fDahUdbsMJc 6fbK5cTHfb/RToBKmvhjQjeFIsWnBLvWLaOEppHhaO47qBW9hxFgeWlqO894gX1j0NhF +89gcu6QyYPyC7RP0G8QRMT/UcgzXVzoejB+0iQBdeuFc6hnudynCX+rGGIBKH2BHEo7 2bmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=ui-outboundreport:content-transfer-encoding:subject:from:cc:to :content-language:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=+rs5RmvR8yoLEeGj5f51IfJlLG5pToU2UWDnTVTgvY4=; fh=60vkliJMaNMQ9vWVETCOLiNxdcVoGFT7eofn8OFibpU=; b=Fcxx8K2bgBmvMC3+3aGT9+kKx65Izo75AM3hiyARTFyyBlf0zq9jv+zGUTo1Aa8SB/ JwFxyRVB+1N9nLjHWm3W/9AX5swrwWmknXg1A3ZM9d9od0rv9LXyJtj9zz0BlI3GX+p0 QN0qUphZB3OWAyQhgfhbqYfb5C3CFiv2vkpL+eZZirkaAbzzldfAgSb504BmfME1YtnB VWT5PCZWwp+LKHL24w4gEwT2i/7/dG3Saca5bHMNg+G4gC7x/gqGa0xIhaTcjHXOhH5i 2wdvm52DtmLpPCjKzMwvVBS7HSzBekff4Yp9NcIoMz4F3fRhiuFeX+y+wnv7EkIHAO3E u7GQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@web.de header.s=s29768273 header.b=bG5SFJ+b; spf=pass (google.com: domain of linux-kernel+bounces-11596-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11596-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=web.de Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id br24-20020a17090b0f1800b0028bee279612si9265667pjb.27.2023.12.26.09.34.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Dec 2023 09:34:52 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-11596-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@web.de header.s=s29768273 header.b=bG5SFJ+b; spf=pass (google.com: domain of linux-kernel+bounces-11596-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-11596-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=web.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 4FC2228324E for ; Tue, 26 Dec 2023 17:34:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4A98350250; Tue, 26 Dec 2023 17:34:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=web.de header.i=markus.elfring@web.de header.b="bG5SFJ+b" X-Original-To: linux-kernel@vger.kernel.org Received: from mout.web.de (mout.web.de [217.72.192.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 63F264F885; Tue, 26 Dec 2023 17:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=web.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=web.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1703612004; x=1704216804; i=markus.elfring@web.de; bh=AuhcszHwHjGP9Acn89BVl5MLLXrMpR0aVvDveRatoik=; h=X-UI-Sender-Class:Date:To:Cc:From:Subject; b=bG5SFJ+bYfMdlpGE1cElBR5x1ijpvWLrkp6loXWkOoDmOoBXR0L53mMfHRgyu1v2 RrVDJqRD9hd6SX+zDKcIjeq77aF3DPWOogjNATijcmLPRbqn8bMO8tqucz3Ndz6JK 3IYE/RQgrbtSIijS9YSHaveF+38NNWHbb55wi6bgiOujYezqxmIITs3Vqh468A7mn nCfwUKWtqf4aGEJ469/wks35vDHZ1oZ9TsfMmEcRlEAR8wj1VOGSIQpRP2vfwOANe FMjGchaP6nVU2vm1F/G/kQCr3CN/Vstyf0BaFKXJVbDeP2K+cxsy5FRhzYVhmZLC8 2bkjcKS04SntJZ0o1g== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Received: from [192.168.178.21] ([94.31.85.95]) by smtp.web.de (mrweb106 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MElV3-1rSOi72PWd-00GAan; Tue, 26 Dec 2023 18:33:24 +0100 Message-ID: <189166d6-b76a-412a-bfcf-8ba14578a317@web.de> Date: Tue, 26 Dec 2023 18:33:23 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-GB To: dri-devel@lists.freedesktop.org, kernel-janitors@vger.kernel.org, Daniel Vetter , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann Cc: LKML , cocci@inria.fr From: Markus Elfring Subject: [PATCH] drm/vc4: Improve exception handling in vc4_validate_shader() X-Provags-ID: V03:K1:cqYAChBIlAwDCrpDesRT8Rcxu7TED955U2F15R9o1Lvz0JJr2FN uTKhH5RtR8P3nvpkDaA6FP3Z3AbzDluXJ9BYxHm7kZw+Wsl6SUnXMBcxkzReBqCZJWFYIZD +qKKAvP4dAuXEx9vhylypm1hlJdFyORz9GujswISk/AkMx8D9gvS82GVer/xy+zvDsQstS6 U9M8IklMQJXMOEO59DWqA== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:riS9T522lis=;57fc1AQoeN3UHWwK3JukaIDA71E oQuOiPMRZaDTXd0YVGVRZE+jXL0z8W6Bujhd0Q9IZ5uRgiedPwsqmvoKDuW37PaceJ6sRPCpG D9/G21S0YOVs/dUqtHHm1uaJKuVXYwqaiDSFeiu9dkywsjC2ZLP1iSSb00Gxfj35YRtH8R1k5 HEY3Q5yST+1Y1SkCN92+dxUsOe8FkGKOUyUBAwN4bgfanTwcVZNjHwGy4IFAP1MZeHJQl/iv7 OXuLrgiqUBG0OW+goOGFqXiZtFnQgpbXEIBFdoMJ93uTUetUCOQJLY9AqrUrqZjQeulaAzK8U 4DKjLvIt908q0FbLhPffo4sOGBIMOmqvw9/ZM4W1pi6KrlqCCn69asLMstQcLwRoqcl3VcNyE wUIzIRa5SDAJ6/adhbIQJUu0vJLB8cYfmQFwz2XVOS8feiujbhaxruhTDUdWrQwAsibSunSWr EDOFHdlqumaLo0HoLHhH1fIrrmPw3gdn3q66xrqPxGMO7iT6azfn3+RgKVwOmk9Kd/2dXP5ml 2/a3S9zs4LF7GDVYN7U9RDmY5GsIyCzxdBjaWwc2Fvs2hCmrHT5GDVNiU1eAIUZ3uEnrSR1hT QBf7vNXjqE7lO2qZEg8hANyH3xR2wJpL4RuZ+w9rDLT+96EIpWdsghb99ljNFB4uSOdVYqsAB PQyCLQcYNadkiurQTQ6rL5QnuKTBEznJ5Qh8+vjvSaiFvMtLksunegF9uF1/8f5T2fRmAOsKK QegbLA3QlO62jPozlTViu3vNSxP5mlJQDFqjqQld6cN8kHgXgifllQsotBPf6JGllZ7NToWOi L+gghKSFDbhLQGhWd64phzNSbrkSJYhTBaFvPZV9ywHCsjQPlVtIQyyM4hB5AjZpjwApcFhpp o/4ct1Fi/qDO81XIE9h8bIYrfK5uvPCE5AlHoZmF9ebd/Oc+Pr6pqwZjZ+dveKVLpv5YPZdjd qMj77gRzkgSUok4y7iyPo6/3pQ0= X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786366753474091583 X-GMAIL-MSGID: 1786366753474091583 From: Markus Elfring Date: Tue, 26 Dec 2023 18:22:54 +0100 Memory releases were triggered during error handling only over the label “fail”. 1. Return directly after a call of the function “kcalloc” failed at the beginning. 2. Reduce the data processing at the end to a required kfree() call order also by adjusting jump targets. 3. Delete an initialisation for the variable “validated_shader” which became unnecessary with this refactoring. Signed-off-by: Markus Elfring --- drivers/gpu/drm/vc4/vc4_validate_shaders.c | 42 +++++++++++----------- 1 file changed, 21 insertions(+), 21 deletions(-) -- 2.43.0 diff --git a/drivers/gpu/drm/vc4/vc4_validate_shaders.c b/drivers/gpu/drm/vc4/vc4_validate_shaders.c index 9745f8810eca..d76787bb7d51 100644 --- a/drivers/gpu/drm/vc4/vc4_validate_shaders.c +++ b/drivers/gpu/drm/vc4/vc4_validate_shaders.c @@ -783,7 +783,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) int shader_end_ip = 0; uint32_t last_thread_switch_ip = -3; uint32_t ip; - struct vc4_validated_shader_info *validated_shader = NULL; + struct vc4_validated_shader_info *validated_shader; struct vc4_shader_validation_state validation_state; if (WARN_ON_ONCE(vc4->is_vc5)) @@ -799,14 +799,14 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) kcalloc(BITS_TO_LONGS(validation_state.max_ip), sizeof(unsigned long), GFP_KERNEL); if (!validation_state.branch_targets) - goto fail; + return NULL; validated_shader = kcalloc(1, sizeof(*validated_shader), GFP_KERNEL); if (!validated_shader) - goto fail; + goto free_targets; if (!vc4_validate_branches(&validation_state)) - goto fail; + goto free_shader; for (ip = 0; ip < validation_state.max_ip; ip++) { uint64_t inst = validation_state.shader[ip]; @@ -815,7 +815,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) validation_state.ip = ip; if (!vc4_handle_branch_target(&validation_state)) - goto fail; + goto free_offsets; if (ip == last_thread_switch_ip + 3) { /* Reset r0-r3 live clamp data */ @@ -842,12 +842,12 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) if (!check_instruction_writes(validated_shader, &validation_state)) { DRM_DEBUG("Bad write at ip %d\n", ip); - goto fail; + goto free_offsets; } if (!check_instruction_reads(validated_shader, &validation_state)) - goto fail; + goto free_offsets; if (sig == QPU_SIG_PROG_END) { found_shader_end = true; @@ -861,7 +861,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) if (ip < last_thread_switch_ip + 3) { DRM_DEBUG("Thread switch too soon after " "last switch at ip %d\n", ip); - goto fail; + goto free_offsets; } last_thread_switch_ip = ip; } @@ -872,26 +872,26 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) if (!check_instruction_writes(validated_shader, &validation_state)) { DRM_DEBUG("Bad LOAD_IMM write at ip %d\n", ip); - goto fail; + goto free_offsets; } break; case QPU_SIG_BRANCH: if (!check_branch(inst, validated_shader, &validation_state, ip)) - goto fail; + goto free_offsets; if (ip < last_thread_switch_ip + 3) { DRM_DEBUG("Branch in thread switch at ip %d", ip); - goto fail; + goto free_offsets; } break; default: DRM_DEBUG("Unsupported QPU signal %d at " "instruction %d\n", sig, ip); - goto fail; + goto free_offsets; } /* There are two delay slots after program end is signaled @@ -905,7 +905,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) DRM_DEBUG("shader failed to terminate before " "shader BO end at %zd\n", shader_obj->base.size); - goto fail; + goto free_offsets; } /* Might corrupt other thread */ @@ -913,7 +913,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) validation_state.all_registers_used) { DRM_DEBUG("Shader uses threading, but uses the upper " "half of the registers, too\n"); - goto fail; + goto free_offsets; } /* If we did a backwards branch and we haven't emitted a uniforms @@ -927,7 +927,7 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) */ if (validation_state.needs_uniform_address_for_loop) { if (!require_uniform_address_uniform(validated_shader)) - goto fail; + goto free_offsets; validated_shader->uniforms_size += 4; } @@ -943,12 +943,12 @@ vc4_validate_shader(struct drm_gem_dma_object *shader_obj) return validated_shader; -fail: +free_offsets: + kfree(validated_shader->uniform_addr_offsets); + kfree(validated_shader->texture_samples); +free_shader: + kfree(validated_shader); +free_targets: kfree(validation_state.branch_targets); - if (validated_shader) { - kfree(validated_shader->uniform_addr_offsets); - kfree(validated_shader->texture_samples); - kfree(validated_shader); - } return NULL; }