In commit 
be8a42ae60 we inroduced a refcount problem, where on the
drm_gem_prime_fd_to_handle() error path we'll call dma_buf_put() for
self imported dma buffers.
Fix this by taking a reference on the dma buffer in the .gem_import
hook instead of assuming the caller had taken one. Besides fixing the
bug this is also more logical.
Signed-off-by: Imre Deak <imre.deak@intel.com>
Cc: stable@vger.kernel.org
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Dave Airlie <airlied@redhat.com>
                         * refcount on gem itself instead of f_count of dmabuf.
                         */
                        drm_gem_object_reference(obj);
-                       dma_buf_put(dma_buf);
                        return obj;
                }
        }
        if (IS_ERR(attach))
                return ERR_PTR(PTR_ERR(attach));
 
+       get_dma_buf(dma_buf);
+
        sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
        if (IS_ERR_OR_NULL(sgt)) {
                ret = PTR_ERR(sgt);
        dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
 fail_detach:
        dma_buf_detach(dma_buf, attach);
+       dma_buf_put(dma_buf);
+
        return ERR_PTR(ret);
 }
 EXPORT_SYMBOL(drm_gem_prime_import);
                goto fail;
 
        mutex_unlock(&file_priv->prime.lock);
+
+       dma_buf_put(dma_buf);
+
        return 0;
 
 fail:
 
                         * refcount on gem itself instead of f_count of dmabuf.
                         */
                        drm_gem_object_reference(obj);
-                       dma_buf_put(dma_buf);
                        return obj;
                }
        }
        if (IS_ERR(attach))
                return ERR_PTR(-EINVAL);
 
+       get_dma_buf(dma_buf);
 
        sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
        if (IS_ERR_OR_NULL(sgt)) {
        dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
 err_buf_detach:
        dma_buf_detach(dma_buf, attach);
+       dma_buf_put(dma_buf);
+
        return ERR_PTR(ret);
 }
 
 
                         * refcount on gem itself instead of f_count of dmabuf.
                         */
                        drm_gem_object_reference(&obj->base);
-                       dma_buf_put(dma_buf);
                        return &obj->base;
                }
        }
        if (IS_ERR(attach))
                return ERR_CAST(attach);
 
+       get_dma_buf(dma_buf);
+
        obj = i915_gem_object_alloc(dev);
        if (obj == NULL) {
                ret = -ENOMEM;
 
 fail_detach:
        dma_buf_detach(dma_buf, attach);
+       dma_buf_put(dma_buf);
+
        return ERR_PTR(ret);
 }
 
                         * refcount on gem itself instead of f_count of dmabuf.
                         */
                        drm_gem_object_reference(obj);
-                       dma_buf_put(buffer);
                        return obj;
                }
        }
 
        if (IS_ERR(attach))
                return ERR_CAST(attach);
 
+       get_dma_buf(dma_buf);
+
        sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
        if (IS_ERR(sg)) {
                ret = PTR_ERR(sg);
        dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
 fail_detach:
        dma_buf_detach(dma_buf, attach);
+       dma_buf_put(dma_buf);
+
        return ERR_PTR(ret);
 }