From: Thomas Zimmermann Subject: Re: [PATCH] drm/ttm: fix mmap refcounting Date: Thu, 14 Nov 2019 09:33:11 +0100 Message-ID: <7c5beaf0-8fbc-d513-fffe-2140bbe5318f@suse.de> References: <20191113135612.19679-1-kraxel@redhat.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="9Jn5K0veYJiURwnjYY9vK4q8DXTeZXrqv" Return-path: In-Reply-To: <20191113135612.19679-1-kraxel@redhat.com> Sender: linux-kernel-owner@vger.kernel.org To: Gerd Hoffmann , drm@redhat.com Cc: David Airlie , "open list:DRM DRIVERS" , open list , Sean Paul List-Id: dri-devel@lists.freedesktop.org This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --9Jn5K0veYJiURwnjYY9vK4q8DXTeZXrqv Content-Type: multipart/mixed; boundary="pgQwq5GsLGYMTNOHVRlAFHRzbpq562mdd"; protected-headers="v1" From: Thomas Zimmermann To: Gerd Hoffmann , drm@redhat.com Cc: David Airlie , "open list:DRM DRIVERS" , open list , Sean Paul Message-ID: <7c5beaf0-8fbc-d513-fffe-2140bbe5318f@suse.de> Subject: Re: [PATCH] drm/ttm: fix mmap refcounting References: <20191113135612.19679-1-kraxel@redhat.com> In-Reply-To: <20191113135612.19679-1-kraxel@redhat.com> --pgQwq5GsLGYMTNOHVRlAFHRzbpq562mdd Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable Am 13.11.19 um 14:56 schrieb Gerd Hoffmann: > When mapping ttm objects via drm_gem_ttm_mmap() helper > drm_gem_mmap_obj() will take an object reference. That gets > never released due to ttm having its own reference counting. >=20 > Fix that by dropping the gem object reference once the ttm mmap > completed (and ttm refcount got bumped). >=20 > For that to work properly the drm_gem_object_get() call in > drm_gem_ttm_mmap() must be moved so it happens before calling > obj->funcs->mmap(), otherwise the gem refcount would go down > to zero. >=20 > Fixes: 231927d939f0 ("drm/ttm: add drm_gem_ttm_mmap()") > Signed-off-by: Gerd Hoffmann Tested-by: Thomas Zimmermann > --- > drivers/gpu/drm/drm_gem.c | 24 ++++++++++++++---------- > drivers/gpu/drm/drm_gem_ttm_helper.c | 13 ++++++++++++- > 2 files changed, 26 insertions(+), 11 deletions(-) >=20 > diff --git a/drivers/gpu/drm/drm_gem.c b/drivers/gpu/drm/drm_gem.c > index 2f2b889096b0..000fa4a1899f 100644 > --- a/drivers/gpu/drm/drm_gem.c > +++ b/drivers/gpu/drm/drm_gem.c > @@ -1105,21 +1105,33 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj= , unsigned long obj_size, > if (obj_size < vma->vm_end - vma->vm_start) > return -EINVAL; > =20 > + /* Take a ref for this mapping of the object, so that the fault > + * handler can dereference the mmap offset's pointer to the object. > + * This reference is cleaned up by the corresponding vm_close > + * (which should happen whether the vma was created by this call, or > + * by a vm_open due to mremap or partial unmap or whatever). > + */ > + drm_gem_object_get(obj); > + > if (obj->funcs && obj->funcs->mmap) { > /* Remove the fake offset */ > vma->vm_pgoff -=3D drm_vma_node_start(&obj->vma_node); > =20 > ret =3D obj->funcs->mmap(obj, vma); > - if (ret) > + if (ret) { > + drm_gem_object_put_unlocked(obj); > return ret; > + } > WARN_ON(!(vma->vm_flags & VM_DONTEXPAND)); > } else { > if (obj->funcs && obj->funcs->vm_ops) > vma->vm_ops =3D obj->funcs->vm_ops; > else if (dev->driver->gem_vm_ops) > vma->vm_ops =3D dev->driver->gem_vm_ops; > - else > + else { > + drm_gem_object_put_unlocked(obj); > return -EINVAL; > + } > =20 > vma->vm_flags |=3D VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; > vma->vm_page_prot =3D pgprot_writecombine(vm_get_page_prot(vma->vm_f= lags)); > @@ -1128,14 +1140,6 @@ int drm_gem_mmap_obj(struct drm_gem_object *obj,= unsigned long obj_size, > =20 > vma->vm_private_data =3D obj; > =20 > - /* Take a ref for this mapping of the object, so that the fault > - * handler can dereference the mmap offset's pointer to the object. > - * This reference is cleaned up by the corresponding vm_close > - * (which should happen whether the vma was created by this call, or > - * by a vm_open due to mremap or partial unmap or whatever). > - */ > - drm_gem_object_get(obj); > - > return 0; > } > EXPORT_SYMBOL(drm_gem_mmap_obj); > diff --git a/drivers/gpu/drm/drm_gem_ttm_helper.c b/drivers/gpu/drm/drm= _gem_ttm_helper.c > index 7412bfc5c05a..605a8a3da7f9 100644 > --- a/drivers/gpu/drm/drm_gem_ttm_helper.c > +++ b/drivers/gpu/drm/drm_gem_ttm_helper.c > @@ -64,8 +64,19 @@ int drm_gem_ttm_mmap(struct drm_gem_object *gem, > struct vm_area_struct *vma) > { > struct ttm_buffer_object *bo =3D drm_gem_ttm_of_gem(gem); > + int ret; > =20 > - return ttm_bo_mmap_obj(vma, bo); > + ret =3D ttm_bo_mmap_obj(vma, bo); > + if (ret < 0) > + return ret; > + > + /* > + * ttm has its own object refcounting, so drop gem reference > + * to avoid double accounting counting. > + */ > + drm_gem_object_put_unlocked(gem); > + > + return 0; > } > EXPORT_SYMBOL(drm_gem_ttm_mmap); > =20 >=20 --=20 Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 N=C3=BCrnberg, Germany (HRB 36809, AG N=C3=BCrnberg) Gesch=C3=A4ftsf=C3=BChrer: Felix Imend=C3=B6rffer --pgQwq5GsLGYMTNOHVRlAFHRzbpq562mdd-- --9Jn5K0veYJiURwnjYY9vK4q8DXTeZXrqv Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEchf7rIzpz2NEoWjlaA3BHVMLeiMFAl3NEUsACgkQaA3BHVML eiNGfwf/Yi4/R/f+0J9m9UP4gtjyDkc/mrGpf/8fL8vZLR4Cj2vG8m5MCv7BjojA rYkTAIppb1Gmctydrs5nawQE/hqiYUyLvzJfWhgXCu6ZaqXwgdiy9qVY6PKzBeQK p2l9G3X2n3z4maHCaI/7Xc5GG1Nsh5SU9dsz1szHQplDKsPcBbYk6CBF03vZQEDP HBY6rud4tltBQliVfCqbcvLBenzYAlBBTLmLH5dH6/MtJD3YEj2qTg6lWH3WpY8/ ASkCXmPyvFO7yF9ZdoAFks6VTinPgcODjl8l9QEpPvLleOayBe+IzLmxNRrPeeCU HG05UPVP7BPD1AufrvlRjWdFMyTvWA== =1fOC -----END PGP SIGNATURE----- --9Jn5K0veYJiURwnjYY9vK4q8DXTeZXrqv--