All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@collabora.com>
To: Steven Price <steven.price@arm.com>
Cc: "Liviu Dudau" <liviu.dudau@arm.com>,
	"Adrián Larumbe" <adrian.larumbe@collabora.com>,
	dri-devel@lists.freedesktop.org,
	"David Airlie" <airlied@gmail.com>,
	"Simona Vetter" <simona@ffwll.ch>,
	"Akash Goel" <akash.goel@arm.com>,
	"Rob Clark" <robin.clark@oss.qualcomm.com>,
	"Sean Paul" <sean@poorly.run>,
	"Konrad Dybcio" <konradybcio@kernel.org>,
	"Akhil P Oommen" <akhilpo@oss.qualcomm.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
	"Chris Diamand" <chris.diamand@arm.com>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Matthew Brost" <matthew.brost@intel.com>,
	"Thomas Hellström" <thomas.hellstrom@linux.intel.com>,
	"Alice Ryhl" <aliceryhl@google.com>,
	kernel@collabora.com
Subject: Re: [PATCH v1 8/9] drm/panthor: Track the number of mmap on a BO
Date: Mon, 12 Jan 2026 15:39:53 +0100	[thread overview]
Message-ID: <20260112153953.61eb20dc@fedora> (raw)
In-Reply-To: <c86e341d-0dd2-4a97-b047-f62f2aa64c7e@arm.com>

On Mon, 12 Jan 2026 12:33:33 +0000
Steven Price <steven.price@arm.com> wrote:

> On 09/01/2026 13:08, Boris Brezillon wrote:
> > This will be used to order things by reclaimability.
> > 
> > Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
> > ---
> >  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);
> }

  reply	other threads:[~2026-01-12 14:40 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-09 13:07 [PATCH v1 0/9] drm/panthor: Add a GEM shrinker Boris Brezillon
2026-01-09 13:07 ` [PATCH v1 1/9] drm/gem: Consider GEM object reclaimable if shrinking fails Boris Brezillon
2026-01-12  9:25   ` Alice Ryhl
2026-01-12 10:02     ` Boris Brezillon
2026-01-15 13:28   ` Liviu Dudau
2026-01-09 13:07 ` [PATCH v1 2/9] drm/gpuvm: Validate BOs in the extobj list when VM is resv protected Boris Brezillon
2026-01-09 19:38   ` Danilo Krummrich
2026-01-12  7:30     ` Boris Brezillon
2026-01-09 13:07 ` [PATCH v1 3/9] drm/panthor: Move panthor_gems_debugfs_init() to panthor_gem.c Boris Brezillon
2026-01-12 11:27   ` Steven Price
2026-01-15 13:39   ` Liviu Dudau
2026-01-09 13:07 ` [PATCH v1 4/9] drm/panthor: Group panthor_kernel_bo_xxx() helpers Boris Brezillon
2026-01-12 11:29   ` Steven Price
2026-01-15 13:41   ` Liviu Dudau
2026-01-09 13:07 ` [PATCH v1 5/9] drm/panthor: Part ways with drm_gem_shmem_object Boris Brezillon
2026-01-12 12:06   ` Steven Price
2026-01-12 14:17     ` Boris Brezillon
2026-01-12 16:03       ` Steven Price
2026-01-12 16:45         ` Boris Brezillon
2026-01-21 11:11       ` Akash Goel
2026-01-21 15:17         ` Boris Brezillon
2026-01-15 16:51   ` Liviu Dudau
2026-01-15 17:27     ` Boris Brezillon
2026-01-15 17:45       ` Liviu Dudau
2026-01-16 12:09         ` Steven Price
2026-01-09 13:07 ` [PATCH v1 6/9] drm/panthor: Lazily allocate pages on mmap() Boris Brezillon
2026-01-12 12:15   ` Steven Price
2026-01-12 14:32     ` Boris Brezillon
2026-01-12 16:41       ` Steven Price
2026-01-12 16:50         ` Boris Brezillon
2026-01-15 17:34   ` Liviu Dudau
2026-01-15 19:27     ` Boris Brezillon
2026-01-16  8:19   ` kernel test robot
2026-01-09 13:07 ` [PATCH v1 7/9] drm/panthor: Split panthor_vm_prepare_map_op_ctx() to prepare for reclaim Boris Brezillon
2026-01-12 12:21   ` Steven Price
2026-01-15 17:40   ` Liviu Dudau
2026-01-09 13:08 ` [PATCH v1 8/9] drm/panthor: Track the number of mmap on a BO Boris Brezillon
2026-01-12 12:33   ` Steven Price
2026-01-12 14:39     ` Boris Brezillon [this message]
2026-01-12 15:19       ` Alice Ryhl
2026-01-12 15:49         ` Boris Brezillon
2026-01-12 15:51           ` Alice Ryhl
2026-01-12 16:06             ` Boris Brezillon
2026-01-12 16:49       ` Steven Price
2026-01-12 16:59         ` Boris Brezillon
2026-01-12 17:10           ` Steven Price
2026-01-12 17:18             ` Boris Brezillon
2026-01-13 12:26             ` Boris Brezillon
2026-01-09 13:08 ` [PATCH v1 9/9] drm/panthor: Add a GEM shrinker Boris Brezillon
2026-01-14 15:05   ` Steven Price
2026-01-15 10:50     ` Boris Brezillon
2026-01-15 11:24       ` Steven Price
2026-01-15 12:01         ` Boris Brezillon
2026-01-15 13:56   ` Akash Goel
2026-01-15 14:36     ` Boris Brezillon
2026-01-15 14:37     ` Boris Brezillon
2026-01-21 11:49   ` Akash Goel
2026-01-21 14:52     ` Boris Brezillon
2026-01-28 11:21       ` Akash Goel
2026-01-28 15:52         ` Boris Brezillon
2026-01-28 16:26           ` Akash Goel
2026-01-12  8:37 ` [PATCH v1 0/9] " Boris Brezillon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260112153953.61eb20dc@fedora \
    --to=boris.brezillon@collabora.com \
    --cc=adrian.larumbe@collabora.com \
    --cc=airlied@gmail.com \
    --cc=akash.goel@arm.com \
    --cc=akhilpo@oss.qualcomm.com \
    --cc=aliceryhl@google.com \
    --cc=chris.diamand@arm.com \
    --cc=dakr@kernel.org \
    --cc=dmitry.osipenko@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=kernel@collabora.com \
    --cc=konradybcio@kernel.org \
    --cc=liviu.dudau@arm.com \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=matthew.brost@intel.com \
    --cc=mripard@kernel.org \
    --cc=robin.clark@oss.qualcomm.com \
    --cc=sean@poorly.run \
    --cc=simona@ffwll.ch \
    --cc=steven.price@arm.com \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tzimmermann@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.