From: Gerd Hoffmann Subject: [PATCH] drm/ttm: fix mmap refcounting Date: Wed, 13 Nov 2019 14:56:12 +0100 Message-ID: <20191113135612.19679-1-kraxel@redhat.com> Content-Type: text/plain; charset=WINDOWS-1252 Content-Transfer-Encoding: quoted-printable Return-path: Sender: linux-kernel-owner@vger.kernel.org To: drm@redhat.com Cc: Gerd Hoffmann , Maarten Lankhorst , Maxime Ripard , Sean Paul , David Airlie , Daniel Vetter , "open list:DRM DRIVERS" , open list List-Id: dri-devel@lists.freedesktop.org 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. Fix that by dropping the gem object reference once the ttm mmap completed (and ttm refcount got bumped). 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. Fixes: 231927d939f0 ("drm/ttm: add drm_gem_ttm_mmap()") Signed-off-by: Gerd Hoffmann --- drivers/gpu/drm/drm_gem.c | 24 ++++++++++++++---------- drivers/gpu/drm/drm_gem_ttm_helper.c | 13 ++++++++++++- 2 files changed, 26 insertions(+), 11 deletions(-) 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, un= signed long obj_size, =09if (obj_size < vma->vm_end - vma->vm_start) =09=09return -EINVAL; =20 +=09/* Take a ref for this mapping of the object, so that the fault +=09 * handler can dereference the mmap offset's pointer to the object. +=09 * This reference is cleaned up by the corresponding vm_close +=09 * (which should happen whether the vma was created by this call, or +=09 * by a vm_open due to mremap or partial unmap or whatever). +=09 */ +=09drm_gem_object_get(obj); + =09if (obj->funcs && obj->funcs->mmap) { =09=09/* Remove the fake offset */ =09=09vma->vm_pgoff -=3D drm_vma_node_start(&obj->vma_node); =20 =09=09ret =3D obj->funcs->mmap(obj, vma); -=09=09if (ret) +=09=09if (ret) { +=09=09=09drm_gem_object_put_unlocked(obj); =09=09=09return ret; +=09=09} =09=09WARN_ON(!(vma->vm_flags & VM_DONTEXPAND)); =09} else { =09=09if (obj->funcs && obj->funcs->vm_ops) =09=09=09vma->vm_ops =3D obj->funcs->vm_ops; =09=09else if (dev->driver->gem_vm_ops) =09=09=09vma->vm_ops =3D dev->driver->gem_vm_ops; -=09=09else +=09=09else { +=09=09=09drm_gem_object_put_unlocked(obj); =09=09=09return -EINVAL; +=09=09} =20 =09=09vma->vm_flags |=3D VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; =09=09vma->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, uns= igned long obj_size, =20 =09vma->vm_private_data =3D obj; =20 -=09/* Take a ref for this mapping of the object, so that the fault -=09 * handler can dereference the mmap offset's pointer to the object. -=09 * This reference is cleaned up by the corresponding vm_close -=09 * (which should happen whether the vma was created by this call, or -=09 * by a vm_open due to mremap or partial unmap or whatever). -=09 */ -=09drm_gem_object_get(obj); - =09return 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, =09=09 struct vm_area_struct *vma) { =09struct ttm_buffer_object *bo =3D drm_gem_ttm_of_gem(gem); +=09int ret; =20 -=09return ttm_bo_mmap_obj(vma, bo); +=09ret =3D ttm_bo_mmap_obj(vma, bo); +=09if (ret < 0) +=09=09return ret; + +=09/* +=09 * ttm has its own object refcounting, so drop gem reference +=09 * to avoid double accounting counting. +=09 */ +=09drm_gem_object_put_unlocked(gem); + +=09return 0; } EXPORT_SYMBOL(drm_gem_ttm_mmap); =20 --=20 2.18.1