From patchwork Thu Jul 18 16:21:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zack Rusin X-Patchwork-Id: 13736608 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 E582FC3DA64 for ; Thu, 18 Jul 2024 16:22:49 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C10DA10EA2D; Thu, 18 Jul 2024 16:22:45 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=broadcom.com header.i=@broadcom.com header.b="IGJ0PIk3"; dkim-atps=neutral Received: from mail-qk1-f175.google.com (mail-qk1-f175.google.com [209.85.222.175]) by gabe.freedesktop.org (Postfix) with ESMTPS id 262F710EA11 for ; Thu, 18 Jul 2024 16:22:44 +0000 (UTC) Received: by mail-qk1-f175.google.com with SMTP id af79cd13be357-79f06c9c929so44057085a.0 for ; Thu, 18 Jul 2024 09:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1721319763; x=1721924563; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=D1Q+WtxgLGLDV4vft3U4bEIvX489UvKjK9odMCfURBQ=; b=IGJ0PIk337ZZDXDnlh/XIcMSNK0HRWs0rVbdaceoGMA8Lxj/TIfrR+7jjK04fDsYV8 IOgY4/7eW3XzoGY300rpif4St5pAT1C2FDGAM1KPEj0ShAxUdsnWKCeK+3bN58AyUPLC dktE4gXrUb55Pbq8Z2Q6ZhZP70wBSmMENWUNY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721319763; x=1721924563; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=D1Q+WtxgLGLDV4vft3U4bEIvX489UvKjK9odMCfURBQ=; b=u18ce7VuOZeOIwrRWdtmiugmjfP805WOTUrUyWk3ovtwWSKlohzTQADd+4nxvw7k/n HnxJZ1936HeiqMJmGW75ddPlGd36P85flqj4NVBGuy6EaonVoC7sdxucsIRLqCfCgVfy jP8aVhEiKfruy+RSY8NFOL9HOKi+Z4jE5nOsaTQp5lCzd9E1QrapH96ft/C7i0uUHMdj wJwQcP+qW5EiYXh8e84IvjuJZk4SYs+anMhNChB3vLoW7wpQI0t1OlmGdUCBM3Z3ivXm dEzTpc18mj0cnGZ/h5maqE/Z7sqZ+qzhUf49PHWBG7jEUabPvjU1NMAg6SNBLlhZcLxg ZlPQ== X-Gm-Message-State: AOJu0YzjnP+pbpwfgdu61CnEsEifvTRUPPfVmuJsU7SsV2Nomww+rQzR mDwk28h8zuqqekOvx3xjh3V6chUSvXaE9nygGb9zuTfVE0lkDSKzpHAZ1VVsVXwvjgNDo3/3mIo ci/pcPr9PFa7hzKiBoqDwwvXYvqTSdagxTm9UujWmD8FstXCB5O6zgt9Sk2YDDO6jFKOJmhzf25 5rU+BAxKcxh9bCE7Trt3Qkeg8ma+m4oGW4fn0WMlUHJcyspS3bTQ== X-Google-Smtp-Source: AGHT+IEdl8+RC76OQ/SK31nHszE9Isn3wn4hK4Trz7TXZp/OrJK0xyKEU1+KUSPfdLdKypd2qSoYYA== X-Received: by 2002:a05:620a:4481:b0:79f:12e9:1e51 with SMTP id af79cd13be357-7a193af1430mr250841485a.5.1721319762918; Thu, 18 Jul 2024 09:22:42 -0700 (PDT) Received: from vertex.vmware.com (pool-173-49-113-140.phlapa.fios.verizon.net. [173.49.113.140]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a1938f8ae8sm39988285a.70.2024.07.18.09.22.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jul 2024 09:22:42 -0700 (PDT) From: Zack Rusin To: dri-devel@lists.freedesktop.org Cc: Broadcom internal kernel review list , ian.forbes@broadcom.com, martin.krastev@broadcom.com, maaz.mombasawala@broadcom.com, Zack Rusin , stable@vger.kernel.org Subject: [PATCH v4 1/4] drm/vmwgfx: Fix a deadlock in dma buf fence polling Date: Thu, 18 Jul 2024 12:21:41 -0400 Message-ID: <20240718162239.13085-2-zack.rusin@broadcom.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240718162239.13085-1-zack.rusin@broadcom.com> References: <20240718162239.13085-1-zack.rusin@broadcom.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" Introduce a version of the fence ops that on release doesn't remove the fence from the pending list, and thus doesn't require a lock to fix poll->fence wait->fence unref deadlocks. vmwgfx overwrites the wait callback to iterate over the list of all fences and update their status, to do that it holds a lock to prevent the list modifcations from other threads. The fence destroy callback both deletes the fence and removes it from the list of pending fences, for which it holds a lock. dma buf polling cb unrefs a fence after it's been signaled: so the poll calls the wait, which signals the fences, which are being destroyed. The destruction tries to acquire the lock on the pending fences list which it can never get because it's held by the wait from which it was called. Old bug, but not a lot of userspace apps were using dma-buf polling interfaces. Fix those, in particular this fixes KDE stalls/deadlock. Signed-off-by: Zack Rusin Fixes: 2298e804e96e ("drm/vmwgfx: rework to new fence interface, v2") Cc: Broadcom internal kernel review list Cc: dri-devel@lists.freedesktop.org Cc: # v6.2+ --- drivers/gpu/drm/vmwgfx/vmwgfx_fence.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c index 5efc6a766f64..588d50ababf6 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_fence.c @@ -32,7 +32,6 @@ #define VMW_FENCE_WRAP (1 << 31) struct vmw_fence_manager { - int num_fence_objects; struct vmw_private *dev_priv; spinlock_t lock; struct list_head fence_list; @@ -124,13 +123,13 @@ static void vmw_fence_obj_destroy(struct dma_fence *f) { struct vmw_fence_obj *fence = container_of(f, struct vmw_fence_obj, base); - struct vmw_fence_manager *fman = fman_from_fence(fence); - spin_lock(&fman->lock); - list_del_init(&fence->head); - --fman->num_fence_objects; - spin_unlock(&fman->lock); + if (!list_empty(&fence->head)) { + spin_lock(&fman->lock); + list_del_init(&fence->head); + spin_unlock(&fman->lock); + } fence->destroy(fence); } @@ -257,7 +256,6 @@ static const struct dma_fence_ops vmw_fence_ops = { .release = vmw_fence_obj_destroy, }; - /* * Execute signal actions on fences recently signaled. * This is done from a workqueue so we don't have to execute @@ -355,7 +353,6 @@ static int vmw_fence_obj_init(struct vmw_fence_manager *fman, goto out_unlock; } list_add_tail(&fence->head, &fman->fence_list); - ++fman->num_fence_objects; out_unlock: spin_unlock(&fman->lock); @@ -403,7 +400,7 @@ static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman, u32 passed_seqno) { u32 goal_seqno; - struct vmw_fence_obj *fence; + struct vmw_fence_obj *fence, *next_fence; if (likely(!fman->seqno_valid)) return false; @@ -413,7 +410,7 @@ static bool vmw_fence_goal_new_locked(struct vmw_fence_manager *fman, return false; fman->seqno_valid = false; - list_for_each_entry(fence, &fman->fence_list, head) { + list_for_each_entry_safe(fence, next_fence, &fman->fence_list, head) { if (!list_empty(&fence->seq_passed_actions)) { fman->seqno_valid = true; vmw_fence_goal_write(fman->dev_priv,