From mboxrd@z Thu Jan 1 00:00:00 1970 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 02ED5CD4F3C for ; Fri, 15 May 2026 02:35:11 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 29E4810E612; Fri, 15 May 2026 02:35:11 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=Nvidia.com header.i=@Nvidia.com header.b="FbaXuC7e"; dkim-atps=neutral Received: from SA9PR02CU001.outbound.protection.outlook.com (mail-southcentralusazon11013006.outbound.protection.outlook.com [40.93.196.6]) by gabe.freedesktop.org (Postfix) with ESMTPS id D798A10E612 for ; Fri, 15 May 2026 02:35:09 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jCRS9C54CRA8PVShmlp2UYrvTUEbCGjj5IyXvhFEEX1EM88j06nVtvoYnvQYVN080uI9cAgGUZYtD51FdWHIIrI03h46iNsOccvIyb883EDIjMoBoDgFOAuVrVmGLaN9CDkFi0xdGg+jfSJCClIQImec42tq7Oh3jy1maNG6AvP+WPmSl+PFd/FPilyCy50oWv9lyhXMTwExnjocyoWX3h9yetfxAIhfhKHy/Gp1x36bKAkcNv17BENn2Yftt/GZUMa9GKuzFkxipEZszMRXT6v8FB78WrXa0JHGIluya5QwcK0T4Zd4nrlvuGdPdFqr02AV5gjlxF2FyoxSUpPzqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=UnetqmXittDaN/ciA3HZxILGrGHT71bB3zFnsrjN8IE=; b=fbBJ+9xC6FjNxhzRUiv1REASoPFDZLVcEXrdh97lqF8Y9n741kN8ia9pZ2NAGA7x2gr5LOhZ8vwJBMWnjwcFE3D7OrHjKNfM+57/VqufdTlWoOpD11Rw9l2J29GP6HV16VEvHgFqnC6Mr1SyEzyC042hVak2nj+/e4iATDBrpxV5LL94QriROnPG/zgC4Vf91RgU0Arm75OEYAP6/9vb2ZDJ+PdElaogwvKcfEdgWAY3lYdcd812OM+AVZ5EfiSA1PBJagIai/EQgZzw5YjeoVC2dUuJsoQ36pMDuATPsuXPEY501qWHJqvI2ocFuqPbeOI/8mNX7SDFP5f1EdoLzg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=UnetqmXittDaN/ciA3HZxILGrGHT71bB3zFnsrjN8IE=; b=FbaXuC7erZNzTIDtYgA4HOPrq86IxNs3x6KzNQEBUkgFCCicosQxftfsJIFeLI1XhHTlCOHLcv2+v7VoRvVObRzndjeCuJsRFj/wXNPJKiz46JbqK02fC3Z5W8Lms9RjYwgmrrov77Yo9s5Lo74JT7HvAZ/WbLD8YWd2yOrlAXir7kxSoCWZ9GRB8PkZt7UFuIyHRSEt/yY6Cjc03W9cMS4QMjbeCghZutmia2TRoM2gR0l4jnQV5BefJB0iRW4AG1sXBBG1qsPq4nI/PzFDyd4lVDlmpG98NHgQDvtK2hcRH0iwEiNMCcJKPvrZo3At9pw6eEihShH/fiRQ5Oy91A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from SJ2PR12MB9161.namprd12.prod.outlook.com (2603:10b6:a03:566::20) by DS7PR12MB5768.namprd12.prod.outlook.com (2603:10b6:8:77::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9913.12; Fri, 15 May 2026 02:35:01 +0000 Received: from SJ2PR12MB9161.namprd12.prod.outlook.com ([fe80::d9d1:8c49:a703:b017]) by SJ2PR12MB9161.namprd12.prod.outlook.com ([fe80::d9d1:8c49:a703:b017%4]) with mapi id 15.21.0025.016; Fri, 15 May 2026 02:35:00 +0000 From: Mikko Perttunen Date: Fri, 15 May 2026 11:34:51 +0900 Subject: [PATCH 1/2] gpu: host1x: Allow entries in BO caches to be freed Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260515-host1x-bocache-leak-v1-1-a0375f68aeab@nvidia.com> References: <20260515-host1x-bocache-leak-v1-0-a0375f68aeab@nvidia.com> In-Reply-To: <20260515-host1x-bocache-leak-v1-0-a0375f68aeab@nvidia.com> To: Thierry Reding , David Airlie , Simona Vetter , Jonathan Hunter , Akhil R , Herbert Xu , "David S. Miller" , Aaron Kling Cc: dri-devel@lists.freedesktop.org, linux-tegra@vger.kernel.org, linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org, Mikko Perttunen X-Mailer: b4 0.16-dev X-ClientProxiedBy: TYCP286CA0131.JPNP286.PROD.OUTLOOK.COM (2603:1096:400:2b6::13) To SJ2PR12MB9161.namprd12.prod.outlook.com (2603:10b6:a03:566::20) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ2PR12MB9161:EE_|DS7PR12MB5768:EE_ X-MS-Office365-Filtering-Correlation-Id: 9da03d72-cad5-4eb6-95a6-08deb22a8fc7 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|366016|376014|7416014|1800799024|10070799003|22082099003|18002099003|56012099003|11063799003; X-Microsoft-Antispam-Message-Info: mLZu7wpeOIMdJdkYS+ctNXIUeAe07Gj1auMLEWA0jxI1RsvqkHVHw5dO3nPh1uVdwSF2zni9Iphn9q9xAC4Ka/2rBCEk+74tpCSSo4PVYDPh886cnx+PyX8dUtif2kv853zG8NbU2ei6phT9RJpHnVjEX+utlnpmz348fLlTVS3RC5mUzXCpQshEDNrA1n8yiwYmtAR4IJ0xi+yF8jkDcHZPZfZa2BnyqkgRoCYeZBkMJxIYdqnO9CrLE5Wyjh5HHy7Qxq7OM9Xa10nzL300e+1y2U4ZQMiEB25TLczgzL5lWm7I/alUlOgXKd2Qa9JgMqZmzI/5sW2roQV4utEGYZJntL5kSzdj32UbBFsdTr00yJ7/XRsc4Bat1duQpd4XNAi5rkDdMNGxN2B4kh4Prt9gyWhDf1KFny8G9Mk5VqLaJrG0PCO+ghc6jRjxjbXAHVo/rWd0TyBD9XNdPxUV1EB4vKKEVPvKTq+9AC+XeSh65FVqSClR0x4zwwNQqXQyU6blI3Boda13zDaWq+ae060qND5na2UBJJVeTPul7NpRzDwVFXOGBqA3S/9cMug+M4P2dnq9UecSTuygjmhOiNmLGC6zG8KIHFQJxn2NmtVq7JvBj7MasjDN+3QPQq9/q9jHG2LS7Yq8JORli+SPAaL77V3OYKMuQRK+MOByHmEi8RQXOKiLUulm+sbv/Ez2 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:SJ2PR12MB9161.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(366016)(376014)(7416014)(1800799024)(10070799003)(22082099003)(18002099003)(56012099003)(11063799003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 2 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?YTI3RXE0NWVrdVA4QXM0cGRsbTJBMHpydUc1VEhJSFNNdVFCQ1d5ZVd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yWmwwcmZhTlY4bEVEV3p4T2ZzeXQyTVE5Zk5aSmZo?= =?utf-8?B?ZVhYUWNOb2pwTTZHVXFyRVRsL2tVbTIrZlRtT2pwQi9pNzc5NFhZSkNqWlZN?= =?utf-8?B?OHpnQUNjOFJ4Njc4bkQxeVpOWmpMTk5TWGdpQW11dnRsOE5Pc0JjZkN5cThU?= =?utf-8?B?MW9mYnRuMWxoRytZcDRybDM1L1ZLRk50Z29QdWU2VTZsSUhDYzBweXlzZkwr?= =?utf-8?B?c09jMlJvVUFOM1l6d1RSM2NxSUtkeXBEcSt5c3hvTWVIN3BOeFlVUUREU3Ux?= =?utf-8?B?b0JUd0xXNTlyd3hZRndvQk5SOHBBYW1aN0k5NDJiU1p1VGVLUExJYkMveVV3?= =?utf-8?B?VDFsd2k0NkhCOEcweFZqc1dYR2ZnRmpQNmk5N3Z4UXFPWEN1cC9YNVVaSDBN?= =?utf-8?B?eDRJNXpWSmtnRUNuT3dTZmE2WER4aklBL3ZGTUQwN1FWNUYxWlRKaGlhK2Ru?= =?utf-8?B?YlZMeUdOYWhINkFDWC9vV2Rzcys3SWRwdlFIaVQxOUt2RzIwTVNBYm9NN1dY?= =?utf-8?Q?e9bBwWXWhfWChOuCWF7jfZGZF/shjfe7dGV07i03CDnNJ?= X-MS-Exchange-AntiSpam-MessageData-1: WDHbyXayXiSqzw== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9da03d72-cad5-4eb6-95a6-08deb22a8fc7 X-MS-Exchange-CrossTenant-AuthSource: SJ2PR12MB9161.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 May 2026 02:35:00.6052 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 7m4LxUxNNH/+JfDEH8aPDqOBaqIBnpKhPeEBoL/Qtwdj3EiRLQ26BJLcHzaIdhBe418mVN46jcw3nqgtCyZCsw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS7PR12MB5768 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" When a buffer object is pinned via host1x_bo_pin() with a cache, the resulting mapping is kept in the cache so it can be reused on subsequent pins. Each mapping held a reference to the underlying host1x_bo (taken in tegra_bo_pin / gather_bo_pin), so as long as a mapping was cached, the bo itself could not be freed. However, the only way to remove the cached mapping was through the free path of the buffer object. This meant that if a bo got cached, it could never get freed again. Resolve the circularity by holding a weak reference to the bo from the cache side. This is done by having the .pin callbacks not bump the bo's refcount -- instead the common Host1x bo code does so, except for the cache reference. Also move the remove-cache-mapping-on-free code into a common function inside Host1x code. This is only called from the TegraDRM GEM buffers since those are the only ones that can be cached at the moment. Reported-by: Aaron Kling Fixes: 1f39b1dfa53c ("drm/tegra: Implement buffer object cache") Signed-off-by: Mikko Perttunen --- drivers/gpu/drm/tegra/gem.c | 13 ++------- drivers/gpu/drm/tegra/submit.c | 3 +-- drivers/gpu/host1x/bus.c | 60 +++++++++++++++++++++++++++++++++++++++++- include/linux/host1x.h | 7 +++++ 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/drivers/gpu/drm/tegra/gem.c b/drivers/gpu/drm/tegra/gem.c index d2bae88ad545..2377e2b76397 100644 --- a/drivers/gpu/drm/tegra/gem.c +++ b/drivers/gpu/drm/tegra/gem.c @@ -69,7 +69,7 @@ static struct host1x_bo_mapping *tegra_bo_pin(struct device *dev, struct host1x_ return ERR_PTR(-ENOMEM); kref_init(&map->ref); - map->bo = host1x_bo_get(bo); + map->bo = bo; map->direction = direction; map->dev = dev; @@ -170,7 +170,6 @@ static void tegra_bo_unpin(struct host1x_bo_mapping *map) kfree(map->sgt); } - host1x_bo_put(map->bo); kfree(map); } @@ -509,17 +508,9 @@ static struct tegra_bo *tegra_bo_import(struct drm_device *drm, void tegra_bo_free_object(struct drm_gem_object *gem) { struct tegra_drm *tegra = gem->dev->dev_private; - struct host1x_bo_mapping *mapping, *tmp; struct tegra_bo *bo = to_tegra_bo(gem); - /* remove all mappings of this buffer object from any caches */ - list_for_each_entry_safe(mapping, tmp, &bo->base.mappings, list) { - if (mapping->cache) - host1x_bo_unpin(mapping); - else - dev_err(gem->dev->dev, "mapping %p stale for device %s\n", mapping, - dev_name(mapping->dev)); - } + host1x_bo_clear_cached_mappings(&bo->base); if (tegra->domain) { tegra_bo_iommu_unmap(tegra, bo); diff --git a/drivers/gpu/drm/tegra/submit.c b/drivers/gpu/drm/tegra/submit.c index 3009b8b9e619..e5841857c937 100644 --- a/drivers/gpu/drm/tegra/submit.c +++ b/drivers/gpu/drm/tegra/submit.c @@ -76,7 +76,7 @@ gather_bo_pin(struct device *dev, struct host1x_bo *bo, enum dma_data_direction return ERR_PTR(-ENOMEM); kref_init(&map->ref); - map->bo = host1x_bo_get(bo); + map->bo = bo; map->direction = direction; map->dev = dev; @@ -117,7 +117,6 @@ static void gather_bo_unpin(struct host1x_bo_mapping *map) dma_unmap_sgtable(map->dev, map->sgt, map->direction, 0); sg_free_table(map->sgt); kfree(map->sgt); - host1x_bo_put(map->bo); kfree(map); } diff --git a/drivers/gpu/host1x/bus.c b/drivers/gpu/host1x/bus.c index f814eb4941c0..772e05a7b45b 100644 --- a/drivers/gpu/host1x/bus.c +++ b/drivers/gpu/host1x/bus.c @@ -887,6 +887,20 @@ int host1x_client_resume(struct host1x_client *client) } EXPORT_SYMBOL(host1x_client_resume); +/** + * host1x_bo_pin() - Create a DMA mapping for the buffer object + * @dev: Device onto which DMA map to + * @bo: Buffer object to map + * @dir: DMA direction + * @cache: Cache in which to store mapping, or NULL + * + * Creates a DMA mapping pointing to @bo for @dev. The refcount of @bo is incremented + * until host1x_bo_unpin is called. + * + * If @cache is specified, the mapping is also stored in the cache and not released + * until @bo is freed (refcount drops to zero). This improves performance when a buffer + * is pinned and unpinned frequently as in the case of display use. + */ struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo, enum dma_data_direction dir, struct host1x_bo_cache *cache) @@ -899,6 +913,7 @@ struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo list_for_each_entry(mapping, &cache->mappings, entry) { if (mapping->bo == bo && mapping->direction == dir) { kref_get(&mapping->ref); + host1x_bo_get(bo); goto unlock; } } @@ -908,6 +923,8 @@ struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo if (IS_ERR(mapping)) goto unlock; + host1x_bo_get(bo); + spin_lock(&mapping->bo->lock); list_add_tail(&mapping->list, &bo->mappings); spin_unlock(&mapping->bo->lock); @@ -918,7 +935,12 @@ struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo list_add_tail(&mapping->entry, &cache->mappings); - /* bump reference count to track the copy in the cache */ + /* + * Bump the mapping reference count to track the mapping in the cache, + * but do not bump the BO's refcount. This allows the BO to still get freed, + * triggering the release of the cache mapping through + * host1x_bo_clear_cached_mappings. + */ kref_get(&mapping->ref); } @@ -948,9 +970,17 @@ static void __host1x_bo_unpin(struct kref *ref) mapping->bo->ops->unpin(mapping); } +/** + * host1x_bo_unpin() - Release an established DMA mapping of a buffer object + * @mapping: Mapping to release + * + * Unmaps the given @mapping, unless it is cached. Decreases the refcount on + * the underlying buffer object. + */ void host1x_bo_unpin(struct host1x_bo_mapping *mapping) { struct host1x_bo_cache *cache = mapping->cache; + struct host1x_bo *bo = mapping->bo; if (cache) mutex_lock(&cache->lock); @@ -959,5 +989,33 @@ void host1x_bo_unpin(struct host1x_bo_mapping *mapping) if (cache) mutex_unlock(&cache->lock); + + host1x_bo_put(bo); } EXPORT_SYMBOL(host1x_bo_unpin); + +/** + * host1x_bo_clear_cached_mappings() - Remove all cached mappings pointing at a bo + * @bo: Buffer object to release mappings of + * + * Drops references to any mappings pointing to @bo left in any caches. This must + * be called by any host1x_bo implementers that may be pinned with caching enabled + * before freeing the bo. + */ +void host1x_bo_clear_cached_mappings(struct host1x_bo *bo) +{ + struct host1x_bo_mapping *mapping, *tmp; + struct host1x_bo_cache *cache; + + list_for_each_entry_safe(mapping, tmp, &bo->mappings, list) { + cache = mapping->cache; + if (WARN_ON(!cache)) + continue; + + mutex_lock(&mapping->cache->lock); + WARN_ON(kref_read(&mapping->ref) != 1); + __host1x_bo_unpin(&mapping->ref); + mutex_unlock(&mapping->cache->lock); + } +} +EXPORT_SYMBOL(host1x_bo_clear_cached_mappings); diff --git a/include/linux/host1x.h b/include/linux/host1x.h index 5e7a63143a4a..d8f052a85b75 100644 --- a/include/linux/host1x.h +++ b/include/linux/host1x.h @@ -143,6 +143,12 @@ static inline struct host1x_bo_mapping *to_host1x_bo_mapping(struct kref *ref) return container_of(ref, struct host1x_bo_mapping, ref); } +/** + * struct host1x_bo_ops - operations implemented by a host1x_bo provider + * + * @pin: create a DMA mapping. Implementation must not touch the bo's refcount. + * @unpin: destroy a DMA mapping. Implementation must not touch the bo's refcount. + */ struct host1x_bo_ops { struct host1x_bo *(*get)(struct host1x_bo *bo); void (*put)(struct host1x_bo *bo); @@ -181,6 +187,7 @@ struct host1x_bo_mapping *host1x_bo_pin(struct device *dev, struct host1x_bo *bo enum dma_data_direction dir, struct host1x_bo_cache *cache); void host1x_bo_unpin(struct host1x_bo_mapping *map); +void host1x_bo_clear_cached_mappings(struct host1x_bo *bo); static inline void *host1x_bo_mmap(struct host1x_bo *bo) { -- 2.53.0