From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796380740; b=zsHFbbz5Yw0zfK8z1Vjz5+CxcVZbLEyu4fmTEpXkpKlUK6rpI5iXKmlW+i+qle+oBuFY 1l5+cSWRIdTWh2wFrTAAbgV1Gc99PMcr2rls86hfP7SpuB0gbCoHdfqaFhMybuIP8tiyp c5hTm8sPmi+IIDwW1usBoSEjLFn+V6W7jzRwS3L3Tqvih9kXkRCfWnkT3TgQ4EcxV9drC ceQUdEsTdjzKZKbqPiYnMo0dR42ahJiV6AYdhXmH6Erb5TKXovGod1A3fwvKIzETeG2t5 AGuyo7gG3QVSki+9GpFlR/FuoORaDtbCKujcq/VpnAFoPTct6zfk23WEI7tTvEeqppg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796380740; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=OezQ7wsk91mfqsgPZhMbeto+I9INXhIL33feMAtQbRA=; b=heczQIdHh+lety7mqR+UoMxOJuGPPzP7Shm5p4w5N5XIcApCa1JgTClYBJBZnNCxXmc6 D0IC66waYE60o7BlveB0aGYz+AgVepbuwAnmNSxHaZnHd8pt83HKZuKsSi+koP33r5M93 jpkXNOWEmxgHUgFhizH2ronF6O/cWxLUw1zuOd3JHNpA7PiYlNwmL1fFrbG3PPY0hhG+x IxjuXYeKLYMyFceCD7PPwe+V+B/JNef7adREt5+zpvwXiVFlAnZ1oHmaTslLPROuN4EPy obbNFmRUu27LfuHvjbEpGoL8G0L2Em1X5029jLHneujVkp/wUV8FIxyEzh1ntCk8Z6g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ITlmRHeB; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9F838606A33A; Fri, 4 Dec 2026 10:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=ITlmRHeB Received: by smtp.kernel.org (Postfix) with ESMTPSA id 145C71F000C2; Fri, 4 Dec 2026 10:38:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796380740; bh=OezQ7wsk91mfqsgPZhMbeto+I9INXhIL33feMAtQbRA=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=ITlmRHeB6DeyOmkmDW/crXyC4RMvgtyguKxKytpMyVC9eXMnwIaCYC9ixQhh2UNsm GYIv/QT9Q5s7VSvFX5cQXh9fi6WgBUckTpf2laPW3T+DgZhp71NH2gbUsFh3zweV1d 4svr2AkkrNzZtzModztfOTZzi8GAiYNJQ2D7H86A= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Iago Toral Quiroga , =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH 7.3 405/742] drm/vc4: Start shader code at varying offsets in its BO Date: Fri, 4 Dec 2026 19:04:29 +0900 Message-ID: <20261204100645.854302575@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Maíra Canal commit c0e1ed41ad86cf5230d64dc5f2c6d2e3aeb59937 upstream. VC4 is affected by the HW erratum GFXH-16: the QPUs can get stuck in a loop where instruction cache loads are evicted before they are used. The QPUs of a slice share one instruction cache. Shader BOs are page aligned and the code always starts at offset 0, so instructions at the same position in different shaders end up mapping to the same cache sets. When the four QPUs of a slice run four different shaders, their fills evict each other, no instruction executes and the hangcheck resets the GPU. Place each shader's code at one of four offsets, 256 bytes apart, inside its BO, and add the offset when relocating the shader record. Shader code is position independent, as the validator only accepts relative branches. The offsets are tracked per client, so shaders a given client creates in sequence (which tend to run together) start in different sets. Notice that this approach makes the livelock unlikely, but not impossible. The next commit will address the remaining cases. On a Raspberry Pi 3 running the issue's reproducer, GPU resets went from dozens in an hour to none. Cc: stable@vger.kernel.org Fixes: 463873d57014 ("drm/vc4: Add an API for creating GPU shaders in GEM BOs.") Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/16652 Reviewed-by: Iago Toral Quiroga Link: https://patch.msgid.link/20261118-vc4-icache-workaround-v1-3-5b8adc2c84fe@igalia.com Signed-off-by: Maíra Canal Signed-off-by: Greg Kroah-Hartman --- drivers/gpu/drm/vc4/vc4_bo.c | 43 ++++++++++++++++++++++------- drivers/gpu/drm/vc4/vc4_drv.h | 10 ++++++ drivers/gpu/drm/vc4/vc4_validate.c | 3 +- drivers/gpu/drm/vc4/vc4_validate_shaders.c | 7 ++-- 4 files changed, 48 insertions(+), 15 deletions(-) --- a/drivers/gpu/drm/vc4/vc4_bo.c +++ b/drivers/gpu/drm/vc4/vc4_bo.c @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -836,6 +837,19 @@ int vc4_mmap_bo_ioctl(struct drm_device return 0; } +/* + * QPUs sharing a slice's instruction cache can livelock evicting each + * other's fills. Considering that shader BOs are page aligned, any code + * at offset 0 maps every shader to the same cache sets. This makes a + * livelock more likely to happen due to GFXH-16. + * + * Start each shader at one of four offsets, cycled per client, to spread + * them across sets. The use of four different offsets is motivated by the + * number of QPUs per slice (4 QPUs). + */ +#define VC4_SHADER_OFFSET_STEP 256 +#define VC4_SHADER_OFFSET_NUM 4 + int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *file_priv) @@ -844,6 +858,7 @@ vc4_create_shader_bo_ioctl(struct drm_de struct vc4_file *vc4file = file_priv->driver_priv; struct vc4_dev *vc4 = to_vc4_dev(dev); struct vc4_bo *bo = NULL; + u32 offset, size; int ret; if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4)) @@ -865,29 +880,37 @@ vc4_create_shader_bo_ioctl(struct drm_de return -EINVAL; } + offset = ((u32)atomic_inc_return(&vc4file->shader_offset_idx) % + VC4_SHADER_OFFSET_NUM) * VC4_SHADER_OFFSET_STEP; + + if (check_add_overflow(offset, args->size, &size)) + return -EINVAL; + ret = vc4_grab_bin_bo(vc4, vc4file); if (ret) return ret; - bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER); + bo = vc4_bo_create(dev, size, true, VC4_BO_TYPE_V3D_SHADER); if (IS_ERR(bo)) return PTR_ERR(bo); bo->madv = VC4_MADV_WILLNEED; + bo->shader_offset = offset; - if (copy_from_user(bo->base.vaddr, - (void __user *)(uintptr_t)args->data, - args->size)) { + /* Clear the memory around the code, which may come from the BO + * cache. + */ + memset(bo->base.vaddr, 0, offset); + if (copy_from_user(bo->base.vaddr + offset, + (void __user *)(uintptr_t)args->data, + args->size)) { ret = -EFAULT; goto fail; } - /* Clear the rest of the memory from allocating from the BO - * cache. - */ - memset(bo->base.vaddr + args->size, 0, - bo->base.base.size - args->size); + memset(bo->base.vaddr + offset + args->size, 0, + bo->base.base.size - offset - args->size); - bo->validated_shader = vc4_validate_shader(&bo->base); + bo->validated_shader = vc4_validate_shader(&bo->base, offset); if (!bo->validated_shader) { ret = -EINVAL; goto fail; --- a/drivers/gpu/drm/vc4/vc4_drv.h +++ b/drivers/gpu/drm/vc4/vc4_drv.h @@ -265,6 +265,9 @@ struct vc4_bo { */ struct vc4_validated_shader_info *validated_shader; + /* Byte offset of the shader code in the BO. */ + u32 shader_offset; + /* One of enum vc4_kernel_bo_type, or VC4_BO_TYPE_COUNT + i * for user-allocated labels. */ @@ -799,6 +802,11 @@ struct vc4_file { struct xarray perfmons; bool bin_bo_used; + + /* Counter picking the start offset of the next shader BO's code, + * see vc4_create_shader_bo_ioctl(). + */ + atomic_t shader_offset_idx; }; static inline struct vc4_exec_info * @@ -1085,7 +1093,7 @@ bool vc4_check_tex_size(struct vc4_exec_ /* vc4_validate_shader.c */ struct vc4_validated_shader_info * -vc4_validate_shader(struct drm_gem_dma_object *shader_obj); +vc4_validate_shader(struct drm_gem_dma_object *shader_obj, u32 offset); /* vc4_perfmon.c */ void vc4_perfmon_get(struct vc4_perfmon *perfmon); --- a/drivers/gpu/drm/vc4/vc4_validate.c +++ b/drivers/gpu/drm/vc4/vc4_validate.c @@ -854,7 +854,8 @@ validate_gl_shader_rec(struct drm_device void *uniform_data_u; uint32_t tex, uni; - *(uint32_t *)(pkt_v + o) = bo[i]->dma_addr + src_offset; + *(uint32_t *)(pkt_v + o) = bo[i]->dma_addr + src_offset + + to_vc4_bo(&bo[i]->base)->shader_offset; if (src_offset != 0) { DRM_DEBUG("Shaders must be at offset 0 of " --- a/drivers/gpu/drm/vc4/vc4_validate_shaders.c +++ b/drivers/gpu/drm/vc4/vc4_validate_shaders.c @@ -779,7 +779,7 @@ vc4_handle_branch_target(struct vc4_shad } struct vc4_validated_shader_info * -vc4_validate_shader(struct drm_gem_dma_object *shader_obj) +vc4_validate_shader(struct drm_gem_dma_object *shader_obj, u32 offset) { struct vc4_dev *vc4 = to_vc4_dev(shader_obj->base.dev); bool found_shader_end = false; @@ -793,8 +793,9 @@ vc4_validate_shader(struct drm_gem_dma_o return NULL; memset(&validation_state, 0, sizeof(validation_state)); - validation_state.shader = shader_obj->vaddr; - validation_state.max_ip = shader_obj->base.size / sizeof(uint64_t); + validation_state.shader = shader_obj->vaddr + offset; + validation_state.max_ip = (shader_obj->base.size - offset) / + sizeof(uint64_t); reset_validation_state(&validation_state);