All of lore.kernel.org
 help / color / mirror / Atom feed
From: Boris Brezillon <boris.brezillon@collabora.com>
To: Daniel Vetter <daniel@ffwll.ch>
Cc: "Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
	"David Airlie" <airlied@gmail.com>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Gurchetan Singh" <gurchetansingh@chromium.org>,
	"Chia-I Wu" <olvaffe@gmail.com>,
	"Maarten Lankhorst" <maarten.lankhorst@linux.intel.com>,
	"Maxime Ripard" <mripard@kernel.org>,
	"Thomas Zimmermann" <tzimmermann@suse.de>,
	"Christian König" <christian.koenig@amd.com>,
	"Qiang Yu" <yuq825@gmail.com>,
	"Steven Price" <steven.price@arm.com>,
	"Emma Anholt" <emma@anholt.net>, "Melissa Wen" <mwen@igalia.com>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	kernel@collabora.com, virtualization@lists.linux-foundation.org
Subject: Re: [PATCH v19 09/30] drm/shmem-helper: Add and use lockless drm_gem_shmem_get_pages()
Date: Tue, 30 Jan 2024 11:10:33 +0100	[thread overview]
Message-ID: <20240130111033.5c01054b@collabora.com> (raw)
In-Reply-To: <Zbi0lQG15vz6iHJK@phenom.ffwll.local>

On Tue, 30 Jan 2024 09:34:29 +0100
Daniel Vetter <daniel@ffwll.ch> wrote:

> On Fri, Jan 26, 2024 at 07:43:29PM +0300, Dmitry Osipenko wrote:
> > On 1/26/24 13:18, Boris Brezillon wrote:  
> > > On Thu, 25 Jan 2024 18:24:04 +0100
> > > Daniel Vetter <daniel@ffwll.ch> wrote:
> > >   
> > >> On Fri, Jan 05, 2024 at 09:46:03PM +0300, Dmitry Osipenko wrote:  
> > >>> Add lockless drm_gem_shmem_get_pages() helper that skips taking reservation
> > >>> lock if pages_use_count is non-zero, leveraging from atomicity of the
> > >>> refcount_t. Make drm_gem_shmem_mmap() to utilize the new helper.
> > >>>
> > >>> Acked-by: Maxime Ripard <mripard@kernel.org>
> > >>> Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
> > >>> Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
> > >>> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> > >>> ---
> > >>>  drivers/gpu/drm/drm_gem_shmem_helper.c | 19 +++++++++++++++----
> > >>>  1 file changed, 15 insertions(+), 4 deletions(-)
> > >>>
> > >>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> index cacf0f8c42e2..1c032513abf1 100644
> > >>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> @@ -226,6 +226,20 @@ void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
> > >>>  }
> > >>>  EXPORT_SYMBOL_GPL(drm_gem_shmem_put_pages_locked);
> > >>>  
> > >>> +static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
> > >>> +{
> > >>> +	int ret;    
> > >>
> > >> Just random drive-by comment: a might_lock annotation here might be good,
> > >> or people could hit some really interesting bugs that are rather hard to
> > >> reproduce ...  
> > > 
> > > Actually, being able to acquire a ref in a dma-signalling path on an
> > > object we know for sure already has refcount >= 1 (because we previously
> > > acquired a ref in a path where dma_resv_lock() was allowed), was the
> > > primary reason I suggested moving to this atomic-refcount approach.
> > > 
> > > In the meantime, drm_gpuvm has evolved in a way that allows me to not
> > > take the ref in the dma-signalling path (the gpuvm_bo object now holds
> > > the ref, and it's acquired/released outside the dma-signalling path).
> > > 
> > > Not saying we shouldn't add this might_lock(), but others might have
> > > good reasons to have this function called in a path where locking
> > > is not allowed.  
> > 
> > For Panthor the might_lock indeed won't be a appropriate, thanks for
> > reminding about it. I'll add explanatory comment to the code.  
> 
> Hm these kind of tricks feel very dangerous to me. I think it would be
> good to split up the two cases into two functions:
> 
> 1. first one does only the atomic_inc and splats if the refcount is zero.
> I think something in the name that denotes that we're incrementing a
> borrowed pages reference would be good here, so like get_borrowed_pages
> (there's not really a naming convention for these in the kernel).
> Unfortunately no rust so we can't enforce that you provide the right kind
> of borrowed reference at compile time.

Yeah, I also considered adding a dedicated function for that use case
at some point, instead of abusing get_pages(). Given I no longer need
it, we can probably add this might_lock() and defer the addition of this
get_borrowed_pages() helper until someone actually needs it.

> 
> 2. second one has the might_lock.
> 
> This way you force callers to think what they're doing and ideally
> document where the borrowed reference is from, and ideally document that
> in the code. Otherwise we'll end up with way too much "works in testing,
> but is a nice CVE" code :-/

Totally agree with you on that point.

WARNING: multiple messages have this Message-ID (diff)
From: Boris Brezillon <boris.brezillon@collabora.com>
To: Daniel Vetter <daniel@ffwll.ch>
Cc: kernel@collabora.com, "Thomas Zimmermann" <tzimmermann@suse.de>,
	"Emma Anholt" <emma@anholt.net>,
	"Christian König" <christian.koenig@amd.com>,
	dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
	"Maxime Ripard" <mripard@kernel.org>,
	"Gurchetan Singh" <gurchetansingh@chromium.org>,
	"Melissa Wen" <mwen@igalia.com>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Dmitry Osipenko" <dmitry.osipenko@collabora.com>,
	"Steven Price" <steven.price@arm.com>,
	"David Airlie" <airlied@gmail.com>,
	virtualization@lists.linux-foundation.org,
	"Qiang Yu" <yuq825@gmail.com>
Subject: Re: [PATCH v19 09/30] drm/shmem-helper: Add and use lockless drm_gem_shmem_get_pages()
Date: Tue, 30 Jan 2024 11:10:33 +0100	[thread overview]
Message-ID: <20240130111033.5c01054b@collabora.com> (raw)
In-Reply-To: <Zbi0lQG15vz6iHJK@phenom.ffwll.local>

On Tue, 30 Jan 2024 09:34:29 +0100
Daniel Vetter <daniel@ffwll.ch> wrote:

> On Fri, Jan 26, 2024 at 07:43:29PM +0300, Dmitry Osipenko wrote:
> > On 1/26/24 13:18, Boris Brezillon wrote:  
> > > On Thu, 25 Jan 2024 18:24:04 +0100
> > > Daniel Vetter <daniel@ffwll.ch> wrote:
> > >   
> > >> On Fri, Jan 05, 2024 at 09:46:03PM +0300, Dmitry Osipenko wrote:  
> > >>> Add lockless drm_gem_shmem_get_pages() helper that skips taking reservation
> > >>> lock if pages_use_count is non-zero, leveraging from atomicity of the
> > >>> refcount_t. Make drm_gem_shmem_mmap() to utilize the new helper.
> > >>>
> > >>> Acked-by: Maxime Ripard <mripard@kernel.org>
> > >>> Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
> > >>> Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
> > >>> Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
> > >>> ---
> > >>>  drivers/gpu/drm/drm_gem_shmem_helper.c | 19 +++++++++++++++----
> > >>>  1 file changed, 15 insertions(+), 4 deletions(-)
> > >>>
> > >>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> index cacf0f8c42e2..1c032513abf1 100644
> > >>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > >>> @@ -226,6 +226,20 @@ void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
> > >>>  }
> > >>>  EXPORT_SYMBOL_GPL(drm_gem_shmem_put_pages_locked);
> > >>>  
> > >>> +static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
> > >>> +{
> > >>> +	int ret;    
> > >>
> > >> Just random drive-by comment: a might_lock annotation here might be good,
> > >> or people could hit some really interesting bugs that are rather hard to
> > >> reproduce ...  
> > > 
> > > Actually, being able to acquire a ref in a dma-signalling path on an
> > > object we know for sure already has refcount >= 1 (because we previously
> > > acquired a ref in a path where dma_resv_lock() was allowed), was the
> > > primary reason I suggested moving to this atomic-refcount approach.
> > > 
> > > In the meantime, drm_gpuvm has evolved in a way that allows me to not
> > > take the ref in the dma-signalling path (the gpuvm_bo object now holds
> > > the ref, and it's acquired/released outside the dma-signalling path).
> > > 
> > > Not saying we shouldn't add this might_lock(), but others might have
> > > good reasons to have this function called in a path where locking
> > > is not allowed.  
> > 
> > For Panthor the might_lock indeed won't be a appropriate, thanks for
> > reminding about it. I'll add explanatory comment to the code.  
> 
> Hm these kind of tricks feel very dangerous to me. I think it would be
> good to split up the two cases into two functions:
> 
> 1. first one does only the atomic_inc and splats if the refcount is zero.
> I think something in the name that denotes that we're incrementing a
> borrowed pages reference would be good here, so like get_borrowed_pages
> (there's not really a naming convention for these in the kernel).
> Unfortunately no rust so we can't enforce that you provide the right kind
> of borrowed reference at compile time.

Yeah, I also considered adding a dedicated function for that use case
at some point, instead of abusing get_pages(). Given I no longer need
it, we can probably add this might_lock() and defer the addition of this
get_borrowed_pages() helper until someone actually needs it.

> 
> 2. second one has the might_lock.
> 
> This way you force callers to think what they're doing and ideally
> document where the borrowed reference is from, and ideally document that
> in the code. Otherwise we'll end up with way too much "works in testing,
> but is a nice CVE" code :-/

Totally agree with you on that point.

  parent reply	other threads:[~2024-01-30 10:10 UTC|newest]

Thread overview: 141+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-05 18:45 [PATCH v19 00/30] Add generic memory shrinker to VirtIO-GPU and Panfrost DRM drivers Dmitry Osipenko
2024-01-05 18:45 ` Dmitry Osipenko
2024-01-05 18:45 ` [PATCH v19 01/30] drm/gem: Change locked/unlocked postfix of drm_gem_v/unmap() function names Dmitry Osipenko
2024-01-05 18:45   ` Dmitry Osipenko
2024-01-05 18:45 ` [PATCH v19 02/30] drm/gem: Add _locked postfix to functions that have unlocked counterpart Dmitry Osipenko
2024-01-05 18:45   ` Dmitry Osipenko
2024-01-05 18:45 ` [PATCH v19 03/30] drm/gem: Document locking rule of vmap and evict callbacks Dmitry Osipenko
2024-01-05 18:45   ` Dmitry Osipenko
2024-01-25  7:55   ` Boris Brezillon
2024-01-25  7:55     ` Boris Brezillon
2024-01-05 18:45 ` [PATCH v19 04/30] drm/shmem-helper: Make all exported symbols GPL Dmitry Osipenko
2024-01-05 18:45   ` Dmitry Osipenko
2024-01-05 18:45 ` [PATCH v19 05/30] drm/shmem-helper: Refactor locked/unlocked functions Dmitry Osipenko
2024-01-05 18:45   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 06/30] drm/shmem-helper: Remove obsoleted is_iomem test Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 07/30] drm/shmem-helper: Add and use pages_pin_count Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 08/30] drm/shmem-helper: Use refcount_t for pages_use_count Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 09/30] drm/shmem-helper: Add and use lockless drm_gem_shmem_get_pages() Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25 17:24   ` Daniel Vetter
2024-01-25 17:24     ` Daniel Vetter
2024-01-25 21:52     ` Dmitry Osipenko
2024-01-26 10:18     ` Boris Brezillon
2024-01-26 10:18       ` Boris Brezillon
2024-01-26 16:43       ` Dmitry Osipenko
2024-01-26 16:43         ` Dmitry Osipenko
2024-01-30  8:34         ` Daniel Vetter
2024-01-30  8:34           ` Daniel Vetter
2024-01-30 10:05           ` Dmitry Osipenko
2024-01-30 10:05             ` Dmitry Osipenko
2024-01-30 10:10           ` Boris Brezillon [this message]
2024-01-30 10:10             ` Boris Brezillon
2024-02-01 18:53             ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 10/30] drm/shmem-helper: Switch drm_gem_shmem_vmap/vunmap to use pin/unpin Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 11/30] drm/shmem-helper: Use refcount_t for vmap_use_count Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 12/30] drm/shmem-helper: Prepare drm_gem_shmem_free() to shrinker addition Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  8:01   ` Boris Brezillon
2024-01-25  8:01     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 13/30] drm/shmem-helper: Make drm_gem_shmem_get_pages() public Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  8:02   ` Boris Brezillon
2024-01-25  8:02     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 14/30] drm/shmem-helper: Add drm_gem_shmem_put_pages() Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  8:02   ` Boris Brezillon
2024-01-25  8:02     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 15/30] drm/shmem-helper: Avoid lockdep warning when pages are released Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  8:02   ` Boris Brezillon
2024-01-25  8:02     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 16/30] drm/lima: Explicitly get and put drm-shmem pages Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 17/30] drm/panfrost: Fix the error path in panfrost_mmu_map_fault_addr() Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25 16:47   ` Steven Price
2024-01-25 16:47     ` Steven Price
2024-01-25 21:41   ` Dmitry Osipenko
2024-01-25 21:41     ` Dmitry Osipenko
2024-01-25 21:44     ` Dmitry Osipenko
2024-01-25 21:44       ` Dmitry Osipenko
2024-01-26  9:08   ` AngeloGioacchino Del Regno
2024-01-26  9:08     ` AngeloGioacchino Del Regno
2024-04-04 15:23   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 18/30] drm/panfrost: Explicitly get and put drm-shmem pages Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  8:10   ` Boris Brezillon
2024-01-25  8:10     ` Boris Brezillon
2024-01-25 16:47   ` Steven Price
2024-01-25 16:47     ` Steven Price
2024-01-26  9:39     ` Boris Brezillon
2024-01-26  9:39       ` Boris Brezillon
2024-01-26 11:26       ` Steven Price
2024-01-26 11:26         ` Steven Price
2024-01-05 18:46 ` [PATCH v19 19/30] drm/virtio: " Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 20/30] drm/v3d: " Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 21/30] drm/shmem-helper: Change sgt allocation policy Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 22/30] drm/shmem-helper: Add common memory shrinker Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  9:07   ` Boris Brezillon
2024-01-25  9:07     ` Boris Brezillon
2024-01-30  8:39     ` Daniel Vetter
2024-01-30  8:39       ` Daniel Vetter
2024-01-30 10:04       ` Dmitry Osipenko
2024-01-30 10:04         ` Dmitry Osipenko
2024-01-25 10:19   ` Boris Brezillon
2024-01-25 10:19     ` Boris Brezillon
2024-01-25 21:56     ` Dmitry Osipenko
2024-01-25 21:56       ` Dmitry Osipenko
2024-01-26  9:55       ` Boris Brezillon
2024-01-26  9:55         ` Boris Brezillon
2024-01-26 16:27         ` Dmitry Osipenko
2024-01-26 16:27           ` Dmitry Osipenko
2024-01-26 18:12           ` Boris Brezillon
2024-01-26 18:12             ` Boris Brezillon
2024-01-29  6:16             ` Dmitry Osipenko
2024-01-29  6:16               ` Dmitry Osipenko
2024-01-29  8:55               ` Boris Brezillon
2024-01-29  8:55                 ` Boris Brezillon
2024-01-29  9:06                 ` Boris Brezillon
2024-01-29  9:06                   ` Boris Brezillon
2024-01-29  9:01   ` Boris Brezillon
2024-01-29  9:01     ` Boris Brezillon
2024-01-29  9:25     ` Dmitry Osipenko
2024-01-29  9:25       ` Dmitry Osipenko
2025-03-04 10:29   ` Thomas Zimmermann
2025-03-04 10:59     ` Dmitry Osipenko
2025-03-04 11:43       ` Thomas Zimmermann
2025-03-05 11:26         ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 23/30] drm/shmem-helper: Export drm_gem_shmem_get_pages_sgt_locked() Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  9:09   ` Boris Brezillon
2024-01-25  9:09     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 24/30] drm/shmem-helper: Optimize unlocked get_pages_sgt() Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  9:28   ` Boris Brezillon
2024-01-25  9:28     ` Boris Brezillon
2024-01-05 18:46 ` [PATCH v19 25/30] drm/shmem-helper: Don't free refcounted GEM Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 26/30] drm/shmem-helper: Turn warnings about imported GEM into errors Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 27/30] drm/virtio: Pin display framebuffer BO Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 28/30] drm/virtio: Attach shmem BOs dynamically Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 29/30] drm/virtio: Support shmem shrinking Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-05 18:46 ` [PATCH v19 30/30] drm/panfrost: Switch to generic memory shrinker Dmitry Osipenko
2024-01-05 18:46   ` Dmitry Osipenko
2024-01-25  9:49   ` Boris Brezillon
2024-01-25  9:49     ` Boris Brezillon
2024-01-25 12:04     ` Dmitry Osipenko
2024-01-25 12:04       ` Dmitry Osipenko

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=20240130111033.5c01054b@collabora.com \
    --to=boris.brezillon@collabora.com \
    --cc=airlied@gmail.com \
    --cc=christian.koenig@amd.com \
    --cc=daniel@ffwll.ch \
    --cc=dmitry.osipenko@collabora.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=emma@anholt.net \
    --cc=gurchetansingh@chromium.org \
    --cc=kernel@collabora.com \
    --cc=kraxel@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maarten.lankhorst@linux.intel.com \
    --cc=mripard@kernel.org \
    --cc=mwen@igalia.com \
    --cc=olvaffe@gmail.com \
    --cc=steven.price@arm.com \
    --cc=tzimmermann@suse.de \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=yuq825@gmail.com \
    /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.