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 5D975C9EC8D for ; Mon, 12 Jan 2026 14:40:04 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C562110E3F1; Mon, 12 Jan 2026 14:40:03 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (2048-bit key; unprotected) header.d=collabora.com header.i=@collabora.com header.b="CA4hKwoj"; dkim-atps=neutral Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) by gabe.freedesktop.org (Postfix) with ESMTPS id 7313E10E3F1 for ; Mon, 12 Jan 2026 14:40:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1768228800; bh=q1K8cL4jzMGK0wjsy9g3Vr0t4eFVzqTsxZezJ5sczLE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=CA4hKwojCKeunZ7OdfzSPYv1BLMcfvO/zvrlhbJ9z+uL72woo3AMKdl36GY/H9gXR sTOdtnXPCpPoCgJ/AygtQrxEZIUwpOZVvixWTXXLBQYyEPYrS5haHEoClV0JXjYiSl icle9Dl2sIhB+YOjhhvUHrz7CX281GeeVXSDk2vMPnjBiAG9IwZyX0DmwthRadcfTM D2ork6QiIH4wavEJEN0mMNyDv4ghmbg7vdiujUQSAcfudyWz0gwKZQwYyoN/008TbV K+0QFi0ebarFBtDU8AYvc6IokMpPcaFGVqvbJ2nP+iwxbzvNFnOtbe4fVCYxZYjayV qkNC+Ayafxl2g== Received: from fedora (unknown [IPv6:2a01:e0a:2c:6930:d919:a6e:5ea1:8a9f]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (prime256v1) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: bbrezillon) by bali.collaboradmins.com (Postfix) with ESMTPSA id 357EC17E0333; Mon, 12 Jan 2026 15:40:00 +0100 (CET) Date: Mon, 12 Jan 2026 15:39:53 +0100 From: Boris Brezillon To: Steven Price Cc: Liviu Dudau , =?UTF-8?B?QWRyacOhbg==?= Larumbe , dri-devel@lists.freedesktop.org, David Airlie , Simona Vetter , Akash Goel , Rob Clark , Sean Paul , Konrad Dybcio , Akhil P Oommen , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Dmitry Osipenko , Chris Diamand , Danilo Krummrich , Matthew Brost , Thomas =?UTF-8?B?SGVsbHN0csO2bQ==?= , Alice Ryhl , kernel@collabora.com Subject: Re: [PATCH v1 8/9] drm/panthor: Track the number of mmap on a BO Message-ID: <20260112153953.61eb20dc@fedora> In-Reply-To: References: <20260109130801.1239558-1-boris.brezillon@collabora.com> <20260109130801.1239558-9-boris.brezillon@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.3.1 (GTK 3.24.51; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit 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" On Mon, 12 Jan 2026 12:33:33 +0000 Steven Price wrote: > On 09/01/2026 13:08, Boris Brezillon wrote: > > This will be used to order things by reclaimability. > > > > Signed-off-by: Boris Brezillon > > --- > > drivers/gpu/drm/panthor/panthor_gem.c | 44 +++++++++++++++++++++++++-- > > drivers/gpu/drm/panthor/panthor_gem.h | 3 ++ > > 2 files changed, 45 insertions(+), 2 deletions(-) > > > > diff --git a/drivers/gpu/drm/panthor/panthor_gem.c b/drivers/gpu/drm/panthor/panthor_gem.c > > index 44f05bd957e7..458d22380e96 100644 > > --- a/drivers/gpu/drm/panthor/panthor_gem.c > > +++ b/drivers/gpu/drm/panthor/panthor_gem.c > > @@ -484,6 +484,7 @@ static void panthor_gem_print_info(struct drm_printer *p, unsigned int indent, > > drm_printf_indent(p, indent, "vmap_use_count=%u\n", > > refcount_read(&bo->cmap.vaddr_use_count)); > > drm_printf_indent(p, indent, "vaddr=%p\n", bo->cmap.vaddr); > > + drm_printf_indent(p, indent, "mmap_count=%u\n", refcount_read(&bo->cmap.mmap_count)); > > } > > > > static int panthor_gem_pin_locked(struct drm_gem_object *obj) > > @@ -600,6 +601,13 @@ static int panthor_gem_mmap(struct drm_gem_object *obj, struct vm_area_struct *v > > if (is_cow_mapping(vma->vm_flags)) > > return -EINVAL; > > > > + if (!refcount_inc_not_zero(&bo->cmap.mmap_count)) { > > + dma_resv_lock(obj->resv, NULL); > > + if (!refcount_inc_not_zero(&bo->cmap.mmap_count)) > > + refcount_set(&bo->cmap.mmap_count, 1); > > + dma_resv_unlock(obj->resv); > > + } > > + > > vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); > > vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); > > if (should_map_wc(bo)) > > @@ -732,10 +740,42 @@ static vm_fault_t panthor_gem_fault(struct vm_fault *vmf) > > return blocking_page_setup(vmf, bo, page_offset, true); > > } > > > > +static void panthor_gem_vm_open(struct vm_area_struct *vma) > > +{ > > + struct panthor_gem_object *bo = to_panthor_bo(vma->vm_private_data); > > + > > + /* mmap_count must have been incremented at mmap time, so it can't be > > + * zero here. > > + */ > > + if (!drm_gem_is_imported(&bo->base)) > > + drm_WARN_ON(bo->base.dev, !refcount_inc_not_zero(&bo->cmap.mmap_count)); > > + > > + drm_gem_vm_open(vma); > > +} > > + > > +static void panthor_gem_vm_close(struct vm_area_struct *vma) > > +{ > > + struct panthor_gem_object *bo = to_panthor_bo(vma->vm_private_data); > > + > > + if (drm_gem_is_imported(&bo->base)) > > + goto out; > > + > > + if (refcount_dec_not_one(&bo->cmap.mmap_count)) > > + goto out; > > + > > + dma_resv_lock(bo->base.resv, NULL); > > + if (!refcount_dec_not_one(&bo->cmap.mmap_count)) > > + refcount_set(&bo->cmap.mmap_count, 0); > > + dma_resv_unlock(bo->base.resv); > > I don't think this logic is safe. Holding the resv_lock doesn't protect > against another thread doing a refcount_inc_not_zero() without holding > the lock. > > I think you can just replace the if() part with a refcount_dec() call, > the lock AFAICT is needed because the following patch wants to be sure > that !!mmap_count is stable when resv_lock is held. I wish I could, but refcount_dec() doesn't let me do the 1 -> 0 without complaining :P. > > I also feel you should invert the conditino for refcount_dec_not_one, > leading to the following which I feel is easier to read: > > static void panthor_gem_vm_close(struct vm_area_struct *vma) > { > [...] > > if (!refcount_dec_not_one(&bo->cmap.mmap_count)) { > dma_resv_lock(bo->base.resv, NULL); > refcount_dec(&bo->cmap.mmap_count); > dma_resv_unlock(bo->base.resv); > } The best I can do is: if (!refcount_dec_not_one(&bo->cmap.mmap_count)) { dma_resv_lock(bo->base.resv, NULL); if (!refcount_dec_not_one(&bo->cmap.mmap_count)) refcount_set(&bo->cmap.mmap_count, 0); dma_resv_unlock(bo->base.resv); } so we only take the lock when absolutely needed, but the 1 -> 0 transition still has to be done with "if (dec_not_one) set(0)". > > drm_gem_object_put(&bo->base); > }