From patchwork Wed Aug 14 19:27:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zack Rusin X-Patchwork-Id: 13763873 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 7BD11C52D7F for ; Wed, 14 Aug 2024 19:28:50 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D556510E52C; Wed, 14 Aug 2024 19:28:49 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=broadcom.com header.i=@broadcom.com header.b="fQI+57pF"; dkim-atps=neutral Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) by gabe.freedesktop.org (Postfix) with ESMTPS id BF20B10E52C for ; Wed, 14 Aug 2024 19:28:48 +0000 (UTC) Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6b7a0ef0e75so1203686d6.1 for ; Wed, 14 Aug 2024 12:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=broadcom.com; s=google; t=1723663727; x=1724268527; darn=lists.freedesktop.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=2kGcJSbkmwivszXfrnvHRLGl+Xm4jBx0fR1WLjpBERQ=; b=fQI+57pFUqYEel45RJX9hK/FENLiNyOrNIossMZnrkwBNug2wJ1Bmy4eyZoGzB9ya0 lB4LOjE8ZVa14vzUq2rMGO+mT4Ry6L1vWLBG4mE2Vpfm14D8GK0lxybBDXDX8NsXj4LZ 5L2dfIb7N9U9I3W9KpF6YzbPTfCEyPlJQfFUo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723663727; x=1724268527; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2kGcJSbkmwivszXfrnvHRLGl+Xm4jBx0fR1WLjpBERQ=; b=OvXE/So/bR51zT6PITwJcNqWcHCEk+ROGUW16b6ZDL5m95ZKGWjzDlmDlGX8lUqpBR bYNOEpYY30VGjTLGoi4kB2sNuMGm9f563+16apZnngPwuCsDnvM/EZCCFhyF9zAmf6sv OnmvHncT3o7BrMupRUbo20Qb1v+ELXPNJ+kiQJzMi5qaPQ2WYq8w/qmerTI/DNb046AH xkbr+UjWFZ68VW0e16xcpggIWx0o9aBRxD9xf0r5Ct0MN1GaADII+fZU7fHKOvtrmcUb d42q/4GEmpjnM/N/njC5bVVdaU4p7PZYVxIkJTA8+kE3QR1+W+f2753Jp7+sNxo17J1M uwzg== X-Gm-Message-State: AOJu0Yx+Qh1AGz9ogKLTSLbZ+bTxua3F5UHCv0Y/L7r6QIIJH3zFH86X y5qkGJU7B4M9954WVjpsyJnLZdnEclKzcOe8fdkgd020wOkrH0ngEHCDgZEW/VN6dAOBR2uG8zs mu8kCbVx9Bf4kbp3v/ZGr/othJ6EiNwbfpqg0tlV+BL4l53F87dFsMd7B6TA4LguyId5QEid/PZ Qt5qWGjWmB2nNHfyvvJmY/9KW1vVDx7mTSfGGC11KOJ5ZdO9D1Ww== X-Google-Smtp-Source: AGHT+IG8b5osBujmbU9Eqa4kkoATG/ukAt8sZRngeOd+97Hz5ZKIY5B7NmFVvLKCrkMsVZw80DsrBQ== X-Received: by 2002:a05:6214:3291:b0:6b5:49c9:ed4f with SMTP id 6a1803df08f44-6bf5d1e5c76mr38029936d6.34.1723663727478; Wed, 14 Aug 2024 12:28:47 -0700 (PDT) Received: from localhost.localdomain (pool-173-49-113-140.phlapa.fios.verizon.net. [173.49.113.140]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6bd82e68277sm46902976d6.134.2024.08.14.12.28.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Aug 2024 12:28:47 -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 v2 1/2] drm/vmwgfx: Prevent unmapping active read buffers Date: Wed, 14 Aug 2024 15:27:59 -0400 Message-ID: <20240814192824.56750-1-zack.rusin@broadcom.com> X-Mailer: git-send-email 2.43.0 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" The kms paths keep a persistent map active to read and compare the cursor buffer. These maps can race with each other in simple scenario where: a) buffer "a" mapped for update b) buffer "a" mapped for compare c) do the compare d) unmap "a" for compare e) update the cursor f) unmap "a" for update At step "e" the buffer has been unmapped and the read contents is bogus. Prevent unmapping of active read buffers by simply keeping a count of how many paths have currently active maps and unmap only when the count reaches 0. v2: Update doc strings Fixes: 485d98d472d5 ("drm/vmwgfx: Add support for CursorMob and CursorBypass 4") Cc: Broadcom internal kernel review list Cc: dri-devel@lists.freedesktop.org Cc: # v5.19+ Signed-off-by: Zack Rusin Reviewed-by: Ian Forbes Reviewed-by: Maaz Mombasawala --- drivers/gpu/drm/vmwgfx/vmwgfx_bo.c | 13 +++++++++++-- drivers/gpu/drm/vmwgfx/vmwgfx_bo.h | 3 +++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c index f42ebc4a7c22..a0e433fbcba6 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.c @@ -360,6 +360,8 @@ void *vmw_bo_map_and_cache_size(struct vmw_bo *vbo, size_t size) void *virtual; int ret; + atomic_inc(&vbo->map_count); + virtual = ttm_kmap_obj_virtual(&vbo->map, ¬_used); if (virtual) return virtual; @@ -383,11 +385,17 @@ void *vmw_bo_map_and_cache_size(struct vmw_bo *vbo, size_t size) */ void vmw_bo_unmap(struct vmw_bo *vbo) { + int map_count; + if (vbo->map.bo == NULL) return; - ttm_bo_kunmap(&vbo->map); - vbo->map.bo = NULL; + map_count = atomic_dec_return(&vbo->map_count); + + if (!map_count) { + ttm_bo_kunmap(&vbo->map); + vbo->map.bo = NULL; + } } @@ -421,6 +429,7 @@ static int vmw_bo_init(struct vmw_private *dev_priv, vmw_bo->tbo.priority = 3; vmw_bo->res_tree = RB_ROOT; xa_init(&vmw_bo->detached_resources); + atomic_set(&vmw_bo->map_count, 0); params->size = ALIGN(params->size, PAGE_SIZE); drm_gem_private_object_init(vdev, &vmw_bo->tbo.base, params->size); diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.h b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.h index 62b4342d5f7c..43b5439ec9f7 100644 --- a/drivers/gpu/drm/vmwgfx/vmwgfx_bo.h +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_bo.h @@ -71,6 +71,8 @@ struct vmw_bo_params { * @map: Kmap object for semi-persistent mappings * @res_tree: RB tree of resources using this buffer object as a backing MOB * @res_prios: Eviction priority counts for attached resources + * @map_count: The number of currently active maps. Will differ from the + * cpu_writers because it includes kernel maps. * @cpu_writers: Number of synccpu write grabs. Protected by reservation when * increased. May be decreased without reservation. * @dx_query_ctx: DX context if this buffer object is used as a DX query MOB @@ -90,6 +92,7 @@ struct vmw_bo { u32 res_prios[TTM_MAX_BO_PRIORITY]; struct xarray detached_resources; + atomic_t map_count; atomic_t cpu_writers; /* Not ref-counted. Protected by binding_mutex */ struct vmw_resource *dx_query_ctx;