minigbm: close bo handle in bo destroy

In dri path the bo handle is not closed in bo destroy. This was
resulting in memory leak. Closed the bo handle in bo destroy.

BUG=b:80546783
TEST=graphis autotest suite

Change-Id: I8d9c7fbc87fd80d03be7d9a98ac8aa95f0d175ed
Signed-off-by: Satyajit Sahu <satyajit.sahu@amd.com>
Reviewed-on: https://chromium-review.googlesource.com/1117975
Commit-Ready: Martin Roth <martinroth@chromium.org>
Tested-by: Martin Roth <martinroth@chromium.org>
Reviewed-by: Gurchetan Singh <gurchetansingh@chromium.org>
diff --git a/dri.c b/dri.c
index ae491bb..f62ef5c 100644
--- a/dri.c
+++ b/dri.c
@@ -87,6 +87,21 @@
 }
 
 /*
+ * Close Gem Handle
+ */
+static void close_gem_handle(uint32_t handle, int fd)
+{
+	struct drm_gem_close gem_close;
+	int ret = 0;
+
+	memset(&gem_close, 0, sizeof(gem_close));
+	gem_close.handle = handle;
+	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
+	if (ret)
+		drv_log("DRM_IOCTL_GEM_CLOSE failed (handle=%x) error %d\n", handle, ret);
+}
+
+/*
  * The caller is responsible for setting drv->priv to a structure that derives from dri_driver.
  */
 int dri_init(struct driver *drv, const char *dri_so_path, const char *driver_suffix)
@@ -194,12 +209,12 @@
 
 	if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_STRIDE, &stride)) {
 		ret = -errno;
-		goto free_image;
+		goto close_handle;
 	}
 
 	if (!dri->image_extension->queryImage(bo->priv, __DRI_IMAGE_ATTRIB_OFFSET, &offset)) {
 		ret = -errno;
-		goto free_image;
+		goto close_handle;
 	}
 
 	bo->strides[0] = stride;
@@ -208,6 +223,8 @@
 	bo->total_size = offset + bo->sizes[0];
 	return 0;
 
+close_handle:
+	close_gem_handle(bo->handles[0].u32, bo->drv->fd);
 free_image:
 	dri->image_extension->destroyImage(bo->priv);
 	return ret;
@@ -243,6 +260,7 @@
 	struct dri_driver *dri = bo->drv->priv;
 
 	assert(bo->priv);
+	close_gem_handle(bo->handles[0].u32, bo->drv->fd);
 	dri->image_extension->destroyImage(bo->priv);
 	bo->priv = NULL;
 	return 0;