dri-devel.lists.freedesktop.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] drm/shmem-helper: Fix a couple of error path bugs
@ 2022-11-29 20:02 Rob Clark
  2022-11-29 20:02 ` [PATCH 1/2] drm/shmem-helper: Remove errant put in error path Rob Clark
  2022-11-29 20:02 ` [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths Rob Clark
  0 siblings, 2 replies; 10+ messages in thread
From: Rob Clark @ 2022-11-29 20:02 UTC (permalink / raw)
  To: dri-devel; +Cc: Rob Clark, open list, Eric Anholt, Noralf Trønnes

From: Rob Clark <robdclark@chromium.org>

A couple fixes for error paths that userspace could manage to trigger.

Rob Clark (2):
  drm/shmem-helper: Remove errant put in error path
  drm/shmem-helper: Avoid vm_open error paths

 drivers/gpu/drm/drm_gem_shmem_helper.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

-- 
2.38.1


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH 1/2] drm/shmem-helper: Remove errant put in error path
  2022-11-29 20:02 [PATCH 0/2] drm/shmem-helper: Fix a couple of error path bugs Rob Clark
@ 2022-11-29 20:02 ` Rob Clark
  2022-11-29 20:13   ` [1/2] " Guenter Roeck
  2022-11-30 11:19   ` [PATCH 1/2] " Daniel Vetter
  2022-11-29 20:02 ` [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths Rob Clark
  1 sibling, 2 replies; 10+ messages in thread
From: Rob Clark @ 2022-11-29 20:02 UTC (permalink / raw)
  To: dri-devel
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	Thomas Zimmermann

From: Rob Clark <robdclark@chromium.org>

drm_gem_shmem_mmap() doesn't own this reference!

Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Clark <robdclark@chromium.org>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
 1 file changed, 1 deletion(-)

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index 35138f8a375c..110a9eac2af8 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
 
 	ret = drm_gem_shmem_get_pages(shmem);
 	if (ret) {
-		drm_gem_vm_close(vma);
 		return ret;
 	}
 
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:02 [PATCH 0/2] drm/shmem-helper: Fix a couple of error path bugs Rob Clark
  2022-11-29 20:02 ` [PATCH 1/2] drm/shmem-helper: Remove errant put in error path Rob Clark
@ 2022-11-29 20:02 ` Rob Clark
  2022-11-29 20:32   ` [2/2] " Guenter Roeck
  2022-11-30 14:03   ` [PATCH 2/2] " Daniel Vetter
  1 sibling, 2 replies; 10+ messages in thread
From: Rob Clark @ 2022-11-29 20:02 UTC (permalink / raw)
  To: dri-devel
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	Thomas Zimmermann

From: Rob Clark <robdclark@chromium.org>

vm_open() is not allowed to fail.  Fortunately we are guaranteed that
the pages are already pinned, and only need to increment the refcnt.  So
just increment it directly.

Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
Cc: stable@vger.kernel.org
Signed-off-by: Rob Clark <robdclark@chromium.org>
---
 drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
index 110a9eac2af8..9885ba64127f 100644
--- a/drivers/gpu/drm/drm_gem_shmem_helper.c
+++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
@@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
 {
 	struct drm_gem_object *obj = vma->vm_private_data;
 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
-	int ret;
 
 	WARN_ON(shmem->base.import_attach);
 
-	ret = drm_gem_shmem_get_pages(shmem);
-	WARN_ON_ONCE(ret != 0);
+	mutex_lock(&shmem->pages_lock);
+
+	/*
+	 * We should have already pinned the pages, vm_open() just grabs
+	 * an additional reference for the new mm the vma is getting
+	 * copied into.
+	 */
+	WARN_ON_ONCE(!shmem->pages_use_count);
+
+	shmem->pages_use_count++;
+	mutex_unlock(&shmem->pages_lock);
 
 	drm_gem_vm_open(vma);
 }
-- 
2.38.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [1/2] drm/shmem-helper: Remove errant put in error path
  2022-11-29 20:02 ` [PATCH 1/2] drm/shmem-helper: Remove errant put in error path Rob Clark
@ 2022-11-29 20:13   ` Guenter Roeck
  2022-11-30 11:19   ` [PATCH 1/2] " Daniel Vetter
  1 sibling, 0 replies; 10+ messages in thread
From: Guenter Roeck @ 2022-11-29 20:13 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	dri-devel, Thomas Zimmermann

On Tue, Nov 29, 2022 at 12:02:41PM -0800, Rob Clark wrote:
> From: Rob Clark <robdclark@chromium.org>
> 
> drm_gem_shmem_mmap() doesn't own this reference!
> 

I think the impact should be explained further.

> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> Cc: stable@vger.kernel.org
> Signed-off-by: Rob Clark <robdclark@chromium.org>
> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 35138f8a375c..110a9eac2af8 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
>  
>  	ret = drm_gem_shmem_get_pages(shmem);
>  	if (ret) {
> -		drm_gem_vm_close(vma);
>  		return ret;
>  	}

Drop the now unnecessary { }

Also, consider adding Reported-by: and possibly Suggested-by:.

Thanks,
Guenter

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:02 ` [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths Rob Clark
@ 2022-11-29 20:32   ` Guenter Roeck
  2022-11-29 20:47     ` Rob Clark
  2022-11-30 14:03   ` [PATCH 2/2] " Daniel Vetter
  1 sibling, 1 reply; 10+ messages in thread
From: Guenter Roeck @ 2022-11-29 20:32 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	dri-devel, Thomas Zimmermann

On Tue, Nov 29, 2022 at 12:02:42PM -0800, Rob Clark wrote:
> From: Rob Clark <robdclark@chromium.org>
> 
> vm_open() is not allowed to fail.  Fortunately we are guaranteed that
> the pages are already pinned, and only need to increment the refcnt.  So
> just increment it directly.

I don't know anything about drm or gem, but I am wondering _how_
this would be guaranteed. Would it be through the pin function ?
Just wondering, because that function does not seem to be mandatory.

> 
> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> Cc: stable@vger.kernel.org
> Signed-off-by: Rob Clark <robdclark@chromium.org>
> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 110a9eac2af8..9885ba64127f 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
>  {
>  	struct drm_gem_object *obj = vma->vm_private_data;
>  	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
> -	int ret;
>  
>  	WARN_ON(shmem->base.import_attach);
>  
> -	ret = drm_gem_shmem_get_pages(shmem);
> -	WARN_ON_ONCE(ret != 0);
> +	mutex_lock(&shmem->pages_lock);
> +
> +	/*
> +	 * We should have already pinned the pages, vm_open() just grabs

should or guaranteed ? This sounds a bit weaker than the commit
description.

> +	 * an additional reference for the new mm the vma is getting
> +	 * copied into.
> +	 */
> +	WARN_ON_ONCE(!shmem->pages_use_count);
> +
> +	shmem->pages_use_count++;
> +	mutex_unlock(&shmem->pages_lock);

The previous code, in that situation, would not increment pages_use_count,
and it would not set not set shmem->pages. Hopefully, it would not try to
do anything with the pages it was unable to get. The new code assumes that
shmem->pages is valid even if pages_use_count is 0, while at the same time
taking into account that this can possibly happen (or the WARN_ON_ONCE
would not be needed).

Again, I don't know anything about gem and drm, but it seems to me that
there might now be a severe problem later on if the WARN_ON_ONCE()
ever triggers.

Thanks,
Guenter

>  
>  	drm_gem_vm_open(vma);
>  }

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:32   ` [2/2] " Guenter Roeck
@ 2022-11-29 20:47     ` Rob Clark
  2022-11-30  0:13       ` Guenter Roeck
  2022-11-30 11:21       ` Daniel Vetter
  0 siblings, 2 replies; 10+ messages in thread
From: Rob Clark @ 2022-11-29 20:47 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	dri-devel, Thomas Zimmermann

On Tue, Nov 29, 2022 at 12:32 PM Guenter Roeck <linux@roeck-us.net> wrote:
>
> On Tue, Nov 29, 2022 at 12:02:42PM -0800, Rob Clark wrote:
> > From: Rob Clark <robdclark@chromium.org>
> >
> > vm_open() is not allowed to fail.  Fortunately we are guaranteed that
> > the pages are already pinned, and only need to increment the refcnt.  So
> > just increment it directly.
>
> I don't know anything about drm or gem, but I am wondering _how_
> this would be guaranteed. Would it be through the pin function ?
> Just wondering, because that function does not seem to be mandatory.

We've pinned the pages already in mmap.. vm->open() is perhaps not the
best name for the callback function, but it is called for copying an
existing vma into a new process (and for some other cases which do not
apply here because VM_DONTEXPAND).

(Other drivers pin pages in the fault handler, where there is actually
potential to return an error, but that change was a bit more like
re-writing shmem helper ;-))

BR,
-R

> >
> > Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> > Cc: stable@vger.kernel.org
> > Signed-off-by: Rob Clark <robdclark@chromium.org>
> > ---
> >  drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
> >  1 file changed, 11 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > index 110a9eac2af8..9885ba64127f 100644
> > --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> > +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > @@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
> >  {
> >       struct drm_gem_object *obj = vma->vm_private_data;
> >       struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
> > -     int ret;
> >
> >       WARN_ON(shmem->base.import_attach);
> >
> > -     ret = drm_gem_shmem_get_pages(shmem);
> > -     WARN_ON_ONCE(ret != 0);
> > +     mutex_lock(&shmem->pages_lock);
> > +
> > +     /*
> > +      * We should have already pinned the pages, vm_open() just grabs
>
> should or guaranteed ? This sounds a bit weaker than the commit
> description.
>
> > +      * an additional reference for the new mm the vma is getting
> > +      * copied into.
> > +      */
> > +     WARN_ON_ONCE(!shmem->pages_use_count);
> > +
> > +     shmem->pages_use_count++;
> > +     mutex_unlock(&shmem->pages_lock);
>
> The previous code, in that situation, would not increment pages_use_count,
> and it would not set not set shmem->pages. Hopefully, it would not try to
> do anything with the pages it was unable to get. The new code assumes that
> shmem->pages is valid even if pages_use_count is 0, while at the same time
> taking into account that this can possibly happen (or the WARN_ON_ONCE
> would not be needed).
>
> Again, I don't know anything about gem and drm, but it seems to me that
> there might now be a severe problem later on if the WARN_ON_ONCE()
> ever triggers.
>
> Thanks,
> Guenter
>
> >
> >       drm_gem_vm_open(vma);
> >  }

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:47     ` Rob Clark
@ 2022-11-30  0:13       ` Guenter Roeck
  2022-11-30 11:21       ` Daniel Vetter
  1 sibling, 0 replies; 10+ messages in thread
From: Guenter Roeck @ 2022-11-30  0:13 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	dri-devel, Thomas Zimmermann

On 11/29/22 12:47, Rob Clark wrote:
> On Tue, Nov 29, 2022 at 12:32 PM Guenter Roeck <linux@roeck-us.net> wrote:
>>
>> On Tue, Nov 29, 2022 at 12:02:42PM -0800, Rob Clark wrote:
>>> From: Rob Clark <robdclark@chromium.org>
>>>
>>> vm_open() is not allowed to fail.  Fortunately we are guaranteed that
>>> the pages are already pinned, and only need to increment the refcnt.  So
>>> just increment it directly.
>>
>> I don't know anything about drm or gem, but I am wondering _how_
>> this would be guaranteed. Would it be through the pin function ?
>> Just wondering, because that function does not seem to be mandatory.
> 
> We've pinned the pages already in mmap.. vm->open() is perhaps not the
> best name for the callback function, but it is called for copying an
> existing vma into a new process (and for some other cases which do not
> apply here because VM_DONTEXPAND).
> 
> (Other drivers pin pages in the fault handler, where there is actually
> potential to return an error, but that change was a bit more like
> re-writing shmem helper ;-))
> 

Maybe add a bit of that (where the pinning happened) to the commit description
and to the patch itself ?

> BR,
> -R
> 
>>>
>>> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
>>> Cc: stable@vger.kernel.org
>>> Signed-off-by: Rob Clark <robdclark@chromium.org>
>>> ---
>>>   drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
>>>   1 file changed, 11 insertions(+), 3 deletions(-)
>>>
>>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> index 110a9eac2af8..9885ba64127f 100644
>>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
>>> @@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
>>>   {
>>>        struct drm_gem_object *obj = vma->vm_private_data;
>>>        struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
>>> -     int ret;
>>>
>>>        WARN_ON(shmem->base.import_attach);
>>>
>>> -     ret = drm_gem_shmem_get_pages(shmem);
>>> -     WARN_ON_ONCE(ret != 0);
>>> +     mutex_lock(&shmem->pages_lock);
>>> +
>>> +     /*
>>> +      * We should have already pinned the pages, vm_open() just grabs
>>
>> should or guaranteed ? This sounds a bit weaker than the commit
>> description.
>>
like ... the pages were already pinned in (mmap function).

>>> +      * an additional reference for the new mm the vma is getting
>>> +      * copied into.
>>> +      */
>>> +     WARN_ON_ONCE(!shmem->pages_use_count);

If the code can't be trusted and still needs the warning, how about
something like the following ?

	if (WARN_ON_ONCE(!shmem->pages_use_count)) {
		mutex_unlock(&shmem->pages_lock);
		return;
	}

Thanks,
Guenter

>>> +
>>> +     shmem->pages_use_count++;
>>> +     mutex_unlock(&shmem->pages_lock);
>>
>> The previous code, in that situation, would not increment pages_use_count,
>> and it would not set not set shmem->pages. Hopefully, it would not try to
>> do anything with the pages it was unable to get. The new code assumes that
>> shmem->pages is valid even if pages_use_count is 0, while at the same time
>> taking into account that this can possibly happen (or the WARN_ON_ONCE
>> would not be needed).
>>
>> Again, I don't know anything about gem and drm, but it seems to me that
>> there might now be a severe problem later on if the WARN_ON_ONCE()
>> ever triggers.
>>
>> Thanks,
>> Guenter
>>
>>>
>>>        drm_gem_vm_open(vma);
>>>   }


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/2] drm/shmem-helper: Remove errant put in error path
  2022-11-29 20:02 ` [PATCH 1/2] drm/shmem-helper: Remove errant put in error path Rob Clark
  2022-11-29 20:13   ` [1/2] " Guenter Roeck
@ 2022-11-30 11:19   ` Daniel Vetter
  1 sibling, 0 replies; 10+ messages in thread
From: Daniel Vetter @ 2022-11-30 11:19 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, Thomas Zimmermann, open list, stable, Eric Anholt,
	Noralf Trønnes, dri-devel

On Tue, Nov 29, 2022 at 12:02:41PM -0800, Rob Clark wrote:
> From: Rob Clark <robdclark@chromium.org>
> 
> drm_gem_shmem_mmap() doesn't own this reference!
> 
> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> Cc: stable@vger.kernel.org
> Signed-off-by: Rob Clark <robdclark@chromium.org>

With Guenter's nits addressed:

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>


> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 1 -
>  1 file changed, 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 35138f8a375c..110a9eac2af8 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -623,7 +623,6 @@ int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct
>  
>  	ret = drm_gem_shmem_get_pages(shmem);
>  	if (ret) {
> -		drm_gem_vm_close(vma);
>  		return ret;
>  	}
>  
> -- 
> 2.38.1
> 

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:47     ` Rob Clark
  2022-11-30  0:13       ` Guenter Roeck
@ 2022-11-30 11:21       ` Daniel Vetter
  1 sibling, 0 replies; 10+ messages in thread
From: Daniel Vetter @ 2022-11-30 11:21 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, open list, stable, Eric Anholt, Noralf Trønnes,
	dri-devel, Thomas Zimmermann, Guenter Roeck

On Tue, Nov 29, 2022 at 12:47:42PM -0800, Rob Clark wrote:
> On Tue, Nov 29, 2022 at 12:32 PM Guenter Roeck <linux@roeck-us.net> wrote:
> >
> > On Tue, Nov 29, 2022 at 12:02:42PM -0800, Rob Clark wrote:
> > > From: Rob Clark <robdclark@chromium.org>
> > >
> > > vm_open() is not allowed to fail.  Fortunately we are guaranteed that
> > > the pages are already pinned, and only need to increment the refcnt.  So
> > > just increment it directly.
> >
> > I don't know anything about drm or gem, but I am wondering _how_
> > this would be guaranteed. Would it be through the pin function ?
> > Just wondering, because that function does not seem to be mandatory.
> 
> We've pinned the pages already in mmap.. vm->open() is perhaps not the
> best name for the callback function, but it is called for copying an
> existing vma into a new process (and for some other cases which do not
> apply here because VM_DONTEXPAND).
> 
> (Other drivers pin pages in the fault handler, where there is actually
> potential to return an error, but that change was a bit more like
> re-writing shmem helper ;-))

Yhea vm_ops->open should really be called vm_ops->dupe or ->copy or
something like that ...
-Daniel

> 
> BR,
> -R
> 
> > >
> > > Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> > > Cc: stable@vger.kernel.org
> > > Signed-off-by: Rob Clark <robdclark@chromium.org>
> > > ---
> > >  drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
> > >  1 file changed, 11 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > > index 110a9eac2af8..9885ba64127f 100644
> > > --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> > > +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> > > @@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
> > >  {
> > >       struct drm_gem_object *obj = vma->vm_private_data;
> > >       struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
> > > -     int ret;
> > >
> > >       WARN_ON(shmem->base.import_attach);
> > >
> > > -     ret = drm_gem_shmem_get_pages(shmem);
> > > -     WARN_ON_ONCE(ret != 0);
> > > +     mutex_lock(&shmem->pages_lock);
> > > +
> > > +     /*
> > > +      * We should have already pinned the pages, vm_open() just grabs
> >
> > should or guaranteed ? This sounds a bit weaker than the commit
> > description.
> >
> > > +      * an additional reference for the new mm the vma is getting
> > > +      * copied into.
> > > +      */
> > > +     WARN_ON_ONCE(!shmem->pages_use_count);
> > > +
> > > +     shmem->pages_use_count++;
> > > +     mutex_unlock(&shmem->pages_lock);
> >
> > The previous code, in that situation, would not increment pages_use_count,
> > and it would not set not set shmem->pages. Hopefully, it would not try to
> > do anything with the pages it was unable to get. The new code assumes that
> > shmem->pages is valid even if pages_use_count is 0, while at the same time
> > taking into account that this can possibly happen (or the WARN_ON_ONCE
> > would not be needed).
> >
> > Again, I don't know anything about gem and drm, but it seems to me that
> > there might now be a severe problem later on if the WARN_ON_ONCE()
> > ever triggers.
> >
> > Thanks,
> > Guenter
> >
> > >
> > >       drm_gem_vm_open(vma);
> > >  }

-- 
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths
  2022-11-29 20:02 ` [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths Rob Clark
  2022-11-29 20:32   ` [2/2] " Guenter Roeck
@ 2022-11-30 14:03   ` Daniel Vetter
  1 sibling, 0 replies; 10+ messages in thread
From: Daniel Vetter @ 2022-11-30 14:03 UTC (permalink / raw)
  To: Rob Clark
  Cc: Rob Clark, Thomas Zimmermann, open list, stable, Eric Anholt,
	Noralf Trønnes, dri-devel

On Tue, Nov 29, 2022 at 12:02:42PM -0800, Rob Clark wrote:
> From: Rob Clark <robdclark@chromium.org>
>
> vm_open() is not allowed to fail.  Fortunately we are guaranteed that
> the pages are already pinned, and only need to increment the refcnt.  So
> just increment it directly.

Please mention hare that the only issue is the mutex_lock_interruptible,
and the only way we've found to hit this is if you send a signal to the
original process in a fork() at _just_ the right time.

With that: Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>

>
> Fixes: 2194a63a818d ("drm: Add library for shmem backed GEM objects")
> Cc: stable@vger.kernel.org
> Signed-off-by: Rob Clark <robdclark@chromium.org>
> ---
>  drivers/gpu/drm/drm_gem_shmem_helper.c | 14 +++++++++++---
>  1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c
> index 110a9eac2af8..9885ba64127f 100644
> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c
> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c
> @@ -571,12 +571,20 @@ static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
>  {
>       struct drm_gem_object *obj = vma->vm_private_data;
>       struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
> -     int ret;
>
>       WARN_ON(shmem->base.import_attach);
>
> -     ret = drm_gem_shmem_get_pages(shmem);
> -     WARN_ON_ONCE(ret != 0);
> +     mutex_lock(&shmem->pages_lock);
> +
> +     /*
> +      * We should have already pinned the pages, vm_open() just grabs
> +      * an additional reference for the new mm the vma is getting
> +      * copied into.
> +      */
> +     WARN_ON_ONCE(!shmem->pages_use_count);
> +
> +     shmem->pages_use_count++;
> +     mutex_unlock(&shmem->pages_lock);
>
>       drm_gem_vm_open(vma);
>  }
> --
> 2.38.1
>

--
Daniel Vetter
Software Engineer, Intel Corporation
http://blog.ffwll.ch

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-11-30 14:03 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-29 20:02 [PATCH 0/2] drm/shmem-helper: Fix a couple of error path bugs Rob Clark
2022-11-29 20:02 ` [PATCH 1/2] drm/shmem-helper: Remove errant put in error path Rob Clark
2022-11-29 20:13   ` [1/2] " Guenter Roeck
2022-11-30 11:19   ` [PATCH 1/2] " Daniel Vetter
2022-11-29 20:02 ` [PATCH 2/2] drm/shmem-helper: Avoid vm_open error paths Rob Clark
2022-11-29 20:32   ` [2/2] " Guenter Roeck
2022-11-29 20:47     ` Rob Clark
2022-11-30  0:13       ` Guenter Roeck
2022-11-30 11:21       ` Daniel Vetter
2022-11-30 14:03   ` [PATCH 2/2] " Daniel Vetter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).